-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.cpp
551 lines (438 loc) · 14.1 KB
/
command.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
* command.cpp
*
* Command classes: Execute lignumCAD commands (undo/redo, dribble)
* Copyright (C) 2002 lignum Computing, Inc. <lignumcad@lignumcomputing.com>
* $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <qapplication.h>
#include <qfile.h>
#include <qmessagebox.h>
#include <qtextstream.h>
#include <qregexp.h>
#if defined(Q_OS_UNIX)
extern "C" {
#include <stdlib.h>
#include <execinfo.h>
}
#endif
#include "constants.h"
#include "model.h"
#include "modelitem.h"
#include "line.h"
#include "constrainthistory.h"
#include "command.h"
Command::Command ( const QString& name )
: name_( name )
{}
Command::~Command ( void )
{}
QString Command::name ( void ) const
{
return name_;
}
CommandHistory CommandHistory::command_history_;
CommandHistory& CommandHistory::instance ( void )
{
return command_history_;
}
CommandHistory::CommandHistory ( void )
: QObject( 0 ), current_( -1 ), file_( 0 ), stream_( 0 ),
document_( 0 )
{
setObjectName( "command_history" );
file_ = new QFile( "history.xml" );
if ( file_->open( QIODevice::WriteOnly ) ) {
stream_ = new QTextStream( file_ );
document_ = new QDomDocument( "LIGNUMCAD-HISTORY" );
*stream_ << "<LIGNUMCAD-HISTORY version=\"1\">" << endl;
}
}
CommandHistory::~CommandHistory ( void )
{
if ( document_ != 0 )
delete document_;
if ( stream_ != 0 ) {
*stream_ << "</LIGNUMCAD-HISTORY>" << endl;
delete stream_;
}
if ( file_ != 0 ) {
file_->close();
delete file_;
}
}
void CommandHistory::addCommand ( Command* command )
{
// Write this command to the dribble file. Maybe later we'll try
// to reduce the quantity of information written out (like compressing
// view modifications).
command->write( document_ );
*stream_ << document_->firstChild();
static_cast<QFile*>(stream_->device())->flush();
// If the current command is not the end of the list, then
// this command basically truncates the remainder of the history
// and we now take a new course into the future.
if ( current_ < (int)history_.count() - 1 ) {
QList< std::shared_ptr<Command> > commands;
for ( int i = 0; i <= current_; i++ ) {
commands.append( history_.at( 0 ) );
history_.takeAt( 0 );
}
history_.clear();
history_ = commands;
}
// Can this command be merged into the current command?
if ( history_.last() != 0 ) {
if ( !history_.last()->merge( command ) ) {
history_.append( std::shared_ptr<Command>(command) );
current_++;
}
else
delete command;
}
else {
history_.append( std::shared_ptr<Command>(command) );
current_++;
}
emitUndoRedo();
}
void CommandHistory::undo ( void )
{
// I guess the main complication here is that allowing redo
// means that you cannot discard commands after they are undone.
// Therefore, the current command to be undone may be somewhere
// in the middle of the list.
if ( current_ > -1 ) {
history_.at( current_ )->unexecute();
*stream_ << "<undo command=\"" << history_.at( current_ )->name() << "\"/>"
<< endl;
static_cast<QFile*>(stream_->device())->flush();
current_--;
}
emitUndoRedo();
}
void CommandHistory::redo ( void )
{
if ( current_ > -1 ) {
// current command is somewhere in the middle of the list
if ( current_ < (int)history_.count() - 1 ) {
current_++;
history_.at( current_ )->execute();
*stream_ << "<redo command=\"" << history_.at( current_ )->name() << "\"/>"
<< endl;
static_cast<QFile*>(stream_->device())->flush();
}
}
else {
// current command is at beginning of list (i.e., no more undoable
// commands)
if ( history_.count() > 0 ) {
current_++;
history_.at( current_ )->execute();
*stream_ << "<redo command=\"" << history_.at( current_ )->name() << "\"/>"
<< endl;
static_cast<QFile*>(stream_->device())->flush();
}
}
emitUndoRedo();
}
void CommandHistory::emitUndoRedo ( void )
{
bool undo_available = false;
bool redo_available = false;
if ( current_ >= 0 && current_ < (int)history_.count() )
undo_available = true;
if ( current_ + 1 >= 0 && current_ + 1 < (int)history_.count() )
redo_available = true;
emit undoRedoChanged( undo_available, redo_available );
}
void CommandHistory::flushOnSegV ( void )
{
// In an emergency, write a stack trace to the history file and then
// close the dribble file's output stream.
cerr << "Got a segv" << endl;
if ( stream_ != 0 ) {
static_cast<QFile*>(stream_->device())->flush();
#if defined(Q_OS_UNIX)
{
const int BTSIZE = 200;
void* array[BTSIZE];
size_t size;
char** strings;
size = backtrace( array, BTSIZE );
strings = backtrace_symbols( array, size );
*stream_ << "<backtrace>" << endl;
for ( size_t i = 0; i < size; ++i )
*stream_ << strings[i] << endl;
*stream_ << "</backtrace>" << endl;
free( strings );
}
#endif
stream_->device()->close();
}
}
CreateCommand::CreateCommand ( const QString& name, CreateObject* creator )
: Command( name ), creator_( creator )
{}
CreateCommand::~CreateCommand ( void )
{
if ( creator_ != 0 ) delete creator_;
}
bool CreateCommand::merge ( Command* command )
{
if ( command->type() == Rename )
return creator_->mergeRename( dynamic_cast<RenameCommand*>( command ) );
return false;
}
void CreateCommand::execute ( void )
{
creator_->create();
}
void CreateCommand::unexecute ( void )
{
creator_->remove();
}
void CreateCommand::write ( QDomDocument* document ) const
{
QDomElement create_element = document->createElement( lC::STR::CREATE );
QDomNode root_node = document->firstChild();
if ( !root_node.isNull() )
document->replaceChild( create_element, root_node );
else
document->appendChild( create_element );
creator_->write( document );
}
DeleteCommand::DeleteCommand ( const QString& name, CreateObject* creator )
: Command( name ), creator_( creator )
{}
DeleteCommand::~DeleteCommand ( void )
{
if ( creator_ != 0 ) delete creator_;
}
void DeleteCommand::execute ( void )
{
creator_->remove();
}
void DeleteCommand::unexecute ( void )
{
creator_->create();
}
void DeleteCommand::write ( QDomDocument* document ) const
{
QDomElement delete_element = document->createElement( lC::STR::DELETE );
QDomNode root_node = document->firstChild();
if ( !root_node.isNull() )
document->replaceChild( delete_element, root_node );
else
document->appendChild( delete_element );
creator_->write( document );
}
RenameCommand::RenameCommand ( const QString& name, Model* model,
const DBURL& old_db_url, const DBURL& new_db_url)
: Command( name ), model_( model ),
old_db_url_( old_db_url ), new_db_url_( new_db_url )
{}
void RenameCommand::execute ( void )
{
ModelItem* item = model_->lookup( old_db_url_ );
if ( item == 0 ) {
cerr << "Yikes, item did not exist!" << endl;
return;
}
item->setName( new_db_url_.name() );
}
void RenameCommand::unexecute ( void )
{
ModelItem* item = model_->lookup( new_db_url_ );
if ( item == 0 ) {
cerr << "Yikes, item did not exist!" << endl;
return;
}
item->setName( old_db_url_.name() );
}
void RenameCommand::write ( QDomDocument* document ) const
{
QDomElement rename_element = document->createElement( lC::STR::RENAME );
QDomNode root_node = document->firstChild();
if ( !root_node.isNull() )
document->replaceChild( rename_element, root_node );
else
document->appendChild( rename_element );
rename_element.setAttribute( lC::STR::OLD_URL, old_db_url_.toString() );
rename_element.setAttribute( lC::STR::NEW_URL, new_db_url_.toString() );
}
MoveLinesCommand::MoveLinesCommand ( const QString& name,
Model* model )
: Command( name ), model_( model ), xml_rep_( 0 )
{
}
MoveLinesCommand::~MoveLinesCommand ( void )
{
if ( xml_rep_ != 0 ) delete xml_rep_;
}
bool MoveLinesCommand::merge ( Command* /*command*/ ) { return false; }
void MoveLinesCommand::add ( Space2D::ConstrainedLine* line, double old_offset )
{
lines_.append( std::shared_ptr<MoveLine>(new MoveLine( line->dbURL(), old_offset, line->offset() )) );
}
void MoveLinesCommand::add ( QDomDocument* xml_rep )
{
xml_rep_ = xml_rep;
}
void MoveLinesCommand::execute ( void )
{
QListIterator< std::shared_ptr<MoveLine> > i( lines_ );
while ( i.hasNext() ) {
Space2D::ConstrainedLine* line =
dynamic_cast<Space2D::ConstrainedLine*>( model_->lookup( i.peekNext()->db_url_ ) );
if ( line == 0 ) {
cerr << "Yikes, line did not exist!" << endl;
continue;
}
line->setOffset( i.peekNext()->new_offset_ );
i.next();
}
if ( xml_rep_ != 0 ) {
QDomElement reconstraints = xml_rep_->firstChild().toElement();
QDomNode old_constraints = reconstraints.firstChild();
QDomElement new_constraints = old_constraints.nextSibling().toElement();
QDomNode n = new_constraints.firstChild();
while ( !n.isNull() ) {
QDomElement constrained_line = n.toElement();
DBURL db_url = constrained_line.attribute( lC::STR::URL );
Space2D::ConstrainedLine* line =
dynamic_cast<Space2D::ConstrainedLine*>( model_->lookup( db_url ) );
if ( line == 0 ) {
cerr << "Yikes, line did not exist!" << endl;
return;
}
QDomElement new_constraint = constrained_line.firstChild().toElement();
line->setConstraint( new_constraint );
n = n.nextSibling();
}
}
}
void MoveLinesCommand::unexecute ( void )
{
if ( xml_rep_ != 0 ) {
QDomElement reconstraints = xml_rep_->firstChild().toElement();
QDomElement old_constraints = reconstraints.firstChild().toElement();
QDomNode n = old_constraints.firstChild();
while ( !n.isNull() ) {
QDomElement constrained_line = n.toElement();
DBURL db_url = constrained_line.attribute( lC::STR::URL );
Space2D::ConstrainedLine* line =
dynamic_cast<Space2D::ConstrainedLine*>( model_->lookup( db_url ) );
if ( line == 0 ) {
cerr << "Yikes, line did not exist!" << endl;
return;
}
QDomElement old_constraint = constrained_line.firstChild().toElement();
line->setConstraint( old_constraint );
n = n.nextSibling();
}
}
QListIterator< std::shared_ptr<MoveLine> > i( lines_ );
while ( i.hasNext() ) {
Space2D::ConstrainedLine* line =
dynamic_cast<Space2D::ConstrainedLine*>( model_->lookup( i.peekNext()->db_url_ ) );
if ( line == 0 ) {
cerr << "Yikes, line did not exist!" << endl;
continue;
}
line->setOffset( i.peekNext()->old_offset_ );
i.next();
}
}
void MoveLinesCommand::write ( QDomDocument* document ) const
{
QDomElement move_element = document->createElement( lC::STR::MOVE_LINES );
QDomNode root_node = document->firstChild();
if ( !root_node.isNull() )
document->replaceChild( move_element, root_node );
else
document->appendChild( move_element );
QListIterator< std::shared_ptr<MoveLine> > i( lines_ );
while ( i.hasNext() ) {
QDomElement line_element = document->createElement( lC::STR::MOVE_LINE );
line_element.setAttribute( lC::STR::URL, i.peekNext()->db_url_.toString() );
line_element.setAttribute( lC::STR::OLD_OFFSET,
lC::format( i.peekNext()->old_offset_ ) );
line_element.setAttribute( lC::STR::NEW_OFFSET,
lC::format( i.peekNext()->new_offset_ ) );
move_element.appendChild( line_element );
i.next();
}
if ( xml_rep_ != 0 ) {
QDomNode reconstraints = document->importNode( xml_rep_->firstChild(),true);
move_element.appendChild( reconstraints );
}
}
ReconstrainCommand::ReconstrainCommand ( const QString& name, Model* model,
QDomDocument* constraints )
: Command( name ), model_( model ), xml_rep_( constraints )
{}
ReconstrainCommand::~ReconstrainCommand ( void )
{
if ( xml_rep_ != 0 ) delete xml_rep_;
}
void ReconstrainCommand::execute ( void )
{
QDomElement reconstraints = xml_rep_->firstChild().toElement();
QDomNode old_constraints = reconstraints.firstChild();
QDomElement new_constraints = old_constraints.nextSibling().toElement();
QDomNode n = new_constraints.firstChild();
while ( !n.isNull() ) {
QDomElement constrained_line = n.toElement();
DBURL db_url = constrained_line.attribute( lC::STR::URL );
Space2D::ConstrainedLine* line =
dynamic_cast<Space2D::ConstrainedLine*>( model_->lookup( db_url ) );
if ( line == 0 ) {
cerr << "Yikes, line did not exist!" << endl;
return;
}
QDomElement new_constraint = constrained_line.firstChild().toElement();
line->setConstraint( new_constraint );
n = n.nextSibling();
}
}
void ReconstrainCommand::unexecute ( void )
{
QDomElement reconstraints = xml_rep_->firstChild().toElement();
QDomElement old_constraints = reconstraints.firstChild().toElement();
QDomNode n = old_constraints.firstChild();
while ( !n.isNull() ) {
QDomElement constrained_line = n.toElement();
DBURL db_url = constrained_line.attribute( lC::STR::URL );
Space2D::ConstrainedLine* line =
dynamic_cast<Space2D::ConstrainedLine*>( model_->lookup( db_url ) );
if ( line == 0 ) {
cerr << "Yikes, line did not exist!" << endl;
return;
}
QDomElement old_constraint = constrained_line.firstChild().toElement();
line->setConstraint( old_constraint );
n = n.nextSibling();
}
}
void ReconstrainCommand::write ( QDomDocument* document ) const
{
// This is suggestive a pattern which might be followed by the
// other commands...
*document = xml_rep_->cloneNode().toDocument();
}