-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawingview.cpp
470 lines (387 loc) · 13.6 KB
/
drawingview.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
/*
* drawingview.cpp
*
* DrawingView class: a view of a Drawing.
* Copyright (C) 2002 lignum Computing, Inc. <[email protected]>
* $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 <QMenu>
#include <qaction.h>
#include <qtabbar.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include "constants.h"
#include "command.h"
#include "model.h"
#include "lignumcadmainwindow.h"
#include "designbookview.h"
#include "listviewitem.h"
#include "dimension.h"
#include "pageinfodialog.h"
#include "pagefactory.h"
#include "drawingview.h"
/*!
* DrawingViewCreate is a memento of the current state of a Drawing and its view.
* It handles creating and deleting both objects.
*/
class DrawingViewCreate : public CreateObject {
//! When CommandHistory undoes or redoes the creation of this object,
//! the Design Book view is the only handle which is required. Everything
//! else can be looked up.
DesignBookView* design_book_view_;
//! The details of the drawing and view are stored as XML.
QDomDocument xml_doc_;
public:
/*!
* Create a memento of the current state of the drawing and its view.
* \param drawing_view view object of the drawing.
*/
DrawingViewCreate ( DrawingView* drawing_view )
{
design_book_view_ = drawing_view->parent();
QDomElement root = xml_doc_.createElement( lC::STR::MEMENTO );
root.setAttribute( lC::STR::NAME, drawing_view->dbURL().toString() );
drawing_view->drawing()->write( root );
drawing_view->write( root );
xml_doc_.appendChild( root );
}
//! Destructor does not do much.
~DrawingViewCreate ( void ) {}
/*!
* (Re)create the drawing object and its view as specified in
* the XML representation.
*/
void create ( void )
{
QDomNodeList drawing_list = xml_doc_.elementsByTagName( lC::STR::DRAWING );
if ( drawing_list.length() > 0 ) {
QDomNodeList drawing_view_list =
xml_doc_.elementsByTagName( lC::STR::DRAWING_VIEW );
if ( drawing_view_list.length() > 0 ) {
uint drawing_id =
drawing_list.item(0).toElement().attribute( lC::STR::ID ).toUInt();
new Drawing( drawing_id, drawing_list.item(0).toElement(),
design_book_view_->model() );
DrawingView* drawing_view =
new DrawingView( design_book_view_, drawing_view_list.item(0).toElement() );
design_book_view_->addPageView( drawing_view );
design_book_view_->showPageView( drawing_view );
}
}
}
/*!
* Delete the drawing object and its view as specified in the
* XML representation.
*/
void remove ( void )
{
QDomNodeList drawing_view_list =xml_doc_.elementsByTagName(lC::STR::DRAWING_VIEW);
if ( drawing_view_list.length() > 0 ) {
QDomElement drawing_view_element = drawing_view_list.item(0).toElement();
QString drawing_path = drawing_view_element.attribute( lC::STR::DRAWING );
DrawingView* drawing_view =
dynamic_cast<DrawingView*>( design_book_view_->lookup( drawing_path ) );
design_book_view_->deletePage( drawing_view );
}
}
/*!
* \return the XML representation.
*/
QDomDocument* representation ( void ) { return &xml_doc_; }
/*!
* Append the contents of the XML representation to the argument XML
* document.
* \param xml_rep CommandHistory's global XML document.
*/
void write ( QDomDocument* xml_rep )
{
QDomElement rep_root = xml_rep->firstChild().toElement();
QDomElement doc_root = xml_doc_.firstChild().toElement();
rep_root.setAttribute( lC::STR::NAME, doc_root.attribute( lC::STR::NAME ) );
QDomNodeList drawing_list = xml_doc_.elementsByTagName( lC::STR::DRAWING );
if ( drawing_list.length() > 0 ) {
QDomNode element = xml_rep->importNode( drawing_list.item(0), true );
rep_root.appendChild( element );
}
QDomNodeList drawing_view_list =
xml_doc_.elementsByTagName( lC::STR::DRAWING_VIEW );
if ( drawing_view_list.length() > 0 ) {
QDomNode element = xml_rep->importNode( drawing_view_list.item(0), true );
rep_root.appendChild( element );
}
}
/*!
* If the creation of a drawing is immediately followed by a rename,
* merge this into the representation rather than having a separate
* undo step.
* \param rename the rename command.
* \return true if the rename command applied to this object.
*/
bool mergeRename ( RenameCommand* rename )
{
// First, make sure the rename refers to this object. Start by extracting
// the old path (which is stored in the view element).
QDomNodeList memento_list = xml_doc_.elementsByTagName( lC::STR::MEMENTO );
if ( memento_list.length() > 0 ) {
QString path = memento_list.item(0).toElement().attribute( lC::STR::NAME );
if ( path != rename->oldDBURL().toString( ) )
return false;
// Additional sanity check: make sure the object and its view have elements.
QDomNodeList drawing_list = xml_doc_.elementsByTagName( lC::STR::DRAWING );
QDomNodeList drawing_view_list =
xml_doc_.elementsByTagName( lC::STR::DRAWING_VIEW );
if ( drawing_list.length() == 0 || drawing_view_list.length() == 0 )
return false;
// Update the name elements in the object and it's view.
memento_list.item(0).toElement().setAttribute( lC::STR::NAME,
rename->newDBURL().toString( ) );
drawing_list.item(0).toElement().
setAttribute( lC::STR::NAME, rename->newDBURL().name() );
drawing_view_list.item(0).toElement().setAttribute( lC::STR::DRAWING,
rename->newDBURL().toString( ) );
return true;
}
return false;
}
};
PageInfoDialog* DrawingView::config_dialog_ = 0;
DrawingView::DrawingView ( Drawing* drawing, DesignBookView* parent )
: PageView( parent ), drawing_( drawing )
{
init();
}
DrawingView::DrawingView ( DesignBookView* parent, const QDomElement& xml_rep )
: PageView( parent )
{
DBURL db_url( xml_rep.attribute( lC::STR::DRAWING ) );
drawing_ = dynamic_cast<Drawing*>( model()->lookup( db_url ) );
init();
}
DrawingView::~DrawingView ()
{
clearFigureViews();
delete list_view_item_;
if ( drawing_ )
model()->removePage( drawing_ );
}
// Canonical init routine.
void DrawingView::init ( void )
{
tabIcon = lC::lookupPixmap( "drawing.png" );
tabText = lC::formatTabName( drawing_->name() );
ListViewItem* previous_item = parent()->previousItem( drawing_->id() );
list_view_item_ = new ListViewItem( parent()->modelListItem(), previous_item );
list_view_item_->setData( lC::formatName( drawing_->name() )
+ QString( " <%1>" ).arg( drawing_->id(),
lC::NAME ) );
list_view_item_->setData( trC( lC::STR::DRAWING ),
lC::TYPE );
//TODO
//list_view_item_->setOpen( true );
//list_view_item_->setRenameEnabled( lC::NAME, true );
//connect( list_view_item_, SIGNAL( nameChanged( const QString& ) ),
// SLOT( listNameChanged( const QString& ) ) );
connect( drawing_, SIGNAL( nameChanged( const QString& ) ),
SLOT( updateName( const QString& ) ) );
if ( config_dialog_ == 0 )
config_dialog_ = new PageInfoDialog;
}
// Do any post-creation configuration.
bool DrawingView::configure ( void )
{
// Just set the name of the drawing. More later...
config_dialog_->getUi()->nameEdit->setText( lC::formatName( name() ) );
config_dialog_->getUi()->nameEdit->selectAll();
config_dialog_->getUi()->nameEdit->setFocus();
config_dialog_->getUi()->buttonOk->setDefault( true );
redo:
int ret = config_dialog_->exec();
if ( ret == QDialog::Rejected ) return false;
if ( config_dialog_->getUi()->nameEdit->isModified() ) {
// DesignBookView handles checking the name and putting up the error dialog
// if necessary.
int ret = parent()->uniquePageName( this,
config_dialog_->getUi()->nameEdit->text(),
drawing_->type() );
switch ( ret ) {
case lC::Rejected:
return false;
case lC::Redo:
config_dialog_->getUi()->nameEdit->setText( lC::formatName( drawing_->name() ) );
goto redo;
}
drawing_->setName( config_dialog_->getUi()->nameEdit->text() );
}
return true;
}
CreateObject* DrawingView::memento ( void )
{
return new DrawingViewCreate( this );
}
void DrawingView::setName ( const QString& name )
{
DBURL old_db_url = drawing_->dbURL();
drawing_->setName( name );
CommandHistory::instance().addCommand( new RenameCommand( "rename drawing",
model(),
old_db_url,
drawing_->dbURL() ) );
}
// Check a list view rename for uniqueness.
void DrawingView::listNameChanged ( const QString& name )
{
int ret = parent()->uniquePageName( this, name, drawing_->type() );
switch ( ret ) {
case lC::OK:
setName( name );
break;
case lC::Redo:
//TODO
//list_view_item_->startRename( lC::NAME );
case lC::Rejected:
updateName( drawing_->name() ); // Repaint list item with old name.
}
}
void DrawingView::show ( void ) const
{
lCMW()->getUi()->toolMenu->clear();
lCMW()->getUi()->toolMenu->addAction( lCMW()->getUi()->toolViewAction );
}
/*!
* Add this view's XML description to the output documennt
*/
void DrawingView::write ( QDomElement& xml_rep ) const
{
QDomDocument document = xml_rep.ownerDocument();
QDomElement view_element = document.createElement( lC::STR::DRAWING_VIEW );
view_element.setAttribute( lC::STR::DRAWING, drawing_->dbURL().toString() );
xml_rep.appendChild( view_element );
}
/*
* Append some useful actions to the OpenGL view context menu.
*/
void DrawingView::startDisplay ( QMenu* /*context_menu*/ )
{
connect( drawing_, SIGNAL( nameChanged( const QString& ) ),
lCMW(), SLOT( pageChanged( const QString& ) ) );
}
/*
* Clean up when are not the current page.
*/
void DrawingView::stopDisplay ( QMenu* /*context_menu*/ )
{
disconnect( drawing_, SIGNAL( nameChanged( const QString& ) ),
lCMW(), SLOT( pageChanged( const QString& ) ) );
}
/*!
* Draw the view.
*/
void DrawingView::draw ( void ) const
{}
/*!
* Draw (in GL_SELECT mode) the elements indicated by the given
* selection type. For speed, this is generally a simplified version
* of the view.
* \param select_type the selection mode type.
*/
void DrawingView::select ( SelectionType /*select_type*/ ) const
{
}
void DrawingView::updateName ( const QString& /*name*/ )
{
//TODO update name
tabText = lC::formatTabName( drawing_->name() );
list_view_item_->setData( lC::formatName( drawing_->name() )
+ QString( " <%1>" ).arg( drawing_->id() ),
lC::NAME );
}
/*
* Determine which figure to create from its simplified XML representation.
*/
void DrawingView::pasteFigure ( const QDomElement& /*xml_rep*/ )
{
}
void DrawingView::cancelOperation ( void )
{
SelectedNames none;
highlightFigures( none );
deactivateFigures();
emit newInformation( trC( lC::STR::NONE ) );
}
/*!
* Instance of assembly metadata for a part.
*/
class DrawingViewMetadata : public PageMetadata {
public:
/*!
* Construct a (what amounts to a const) drawing metadata object.
*/
DrawingViewMetadata ( void )
: PageMetadata ( lC::STR::DRAWING,
"new_drawing.png",
QT_TRANSLATE_NOOP( "lignumCADMainWindow", "&Drawing" ),
"&Drawing...",
"Insert a new Drawing",
"<p><b>Insert|Drawing</b></p><p>Click this button to create a new drawing. The drawing will contain the final dimensions needed to fabricate a part. (More than one part can also appear on a drawing.)</p>" )
{
// cout << "Constructing drawing metadata" << endl;
PageFactory::instance()->addPageMetadata( Drawing::PAGE_ID, this );
}
/*!
* Create a new drawing and its view.
* \return view handle for the drawing and its view.
*/
DrawingView* create ( DesignBookView* parent ) const
{
QString name = parent->uniqueName( &Drawing::newName, lC::STR::DRAWING );
Drawing* drawing = new Drawing( parent->model()->newID(), name,
parent->model() );
DrawingView* drawing_view = new DrawingView( drawing, parent );
parent->addPageView( drawing_view );
return drawing_view;
}
/*!
* Create a drawing from its XML representation.
* \return handle for the drawing.
*/
Drawing* create ( Model* parent, const QDomElement& xml_rep ) const
{
uint drawing_id = xml_rep.attribute( lC::STR::ID ).toUInt();
return new Drawing( drawing_id, xml_rep, parent );
}
/*!
* Create a drawing view from its XML representation.
* \return handle for the drawing view.
*/
DrawingView* create ( DesignBookView* parent, const QDomElement& xml_rep ) const
{
DrawingView* drawing_view = new DrawingView( parent, xml_rep );
parent->addPageView( drawing_view );
return drawing_view;
}
/*!
* Retrieve the QAction which can create this page.
* \param lCMW pointer to the main window where the actions are stored.
* \return the action associated with creating an instance of this page.
*/
QAction* action ( lignumCADMainWindow* lCMW ) const
{
return lCMW->getUi()->insertDrawingAction;
}
};
DrawingViewMetadata drawing_view_metadata;