-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotationview.cpp
1530 lines (1253 loc) · 51.1 KB
/
annotationview.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* annotationview.cpp
*
* Annotation View classes
* 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 <qaction.h>
#include <QMenu>
#include <qlineedit.h>
#include <qtextedit.h>
#include <qcursor.h>
#include <qpainter.h>
#include <qrect.h>
#include "lignumcadmainwindow.h"
#include "command.h"
#include "cursorfactory.h"
#include "page.h"
#include "pageview.h"
#include "designbookview.h"
#include "model.h"
#include "openglview.h"
#include "listviewitem.h"
#include "units.h"
#include "annotation.h"
#include "annotationinfodialog.h"
#include "annotationview.h"
#include <QMoveEvent>
namespace Space2D {
/*!
* Internal class which implements a memento of the annotation.
*/
class AnnotationViewCreate : 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 annotation and view are stored as XML.
QDomDocument xml_doc_;
public:
/*!
* Create a memento of the current state of the annotation and its view.
* \param annotation_view view object of the annotation.
*/
AnnotationViewCreate ( AnnotationView* annotation_view )
{
design_book_view_ = annotation_view->parent()->parent();
QDomElement root = xml_doc_.createElement( lC::STR::MEMENTO );
root.setAttribute( lC::STR::NAME, annotation_view->dbURL().toString() );
annotation_view->annotation()->write( root );
annotation_view->write( root );
xml_doc_.appendChild( root );
}
//! Destructor doesn't do much.
~AnnotationViewCreate ( void ) {}
/*!
* (Re)create the annotation object and its view as specified in
* the XML representation.
*/
void create ( void )
{
QDomNodeList memento_list = xml_doc_.elementsByTagName( lC::STR::MEMENTO );
if ( memento_list.length() > 0 ) {
DBURL url = memento_list.item(0).toElement().attribute( lC::STR::NAME );
// First, we have to find the parent page and view for the annotation.
DBURL parent = url.parent();
Page* page =
dynamic_cast<Page*>( design_book_view_->model()->lookup( parent ) );
PageView* page_view =
dynamic_cast<PageView*>( design_book_view_->lookup( parent ) );
// Now, we can extract the XML representations for the annotation
// and its view
QDomNodeList annotation_list =
xml_doc_.elementsByTagName( lC::STR::ANNOTATION );
if ( annotation_list.length() > 0 ) {
QDomNodeList annotation_view_list =
xml_doc_.elementsByTagName( lC::STR::ANNOTATION_VIEW );
if ( annotation_view_list.length() > 0 ) {
uint annotation_id =
annotation_list.item(0).toElement().attribute( lC::STR::ID ).toUInt();
new Annotation( annotation_id, annotation_list.item(0).toElement(),
page );
page_view->
addFigureView( new AnnotationView( annotation_view_list.
item(0).toElement(), page_view ) );
}
}
}
}
/*!
* Delete the annotation object and its view as specified in the
* XML representation.
*/
void remove ( void )
{
QDomNodeList memento_list = xml_doc_.elementsByTagName( lC::STR::MEMENTO );
if ( memento_list.length() > 0 ) {
DBURL url = memento_list.item(0).toElement().attribute( lC::STR::NAME );
AnnotationView* annotation_view =
dynamic_cast<AnnotationView*>( design_book_view_->lookup( url ) );
DBURL parent = url.parent();
PageView* page_view =
dynamic_cast<PageView*>( design_book_view_->lookup( parent ) );
page_view->removeFigureView( annotation_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 annotation_list =
xml_doc_.elementsByTagName( lC::STR::ANNOTATION );
if ( annotation_list.length() > 0 ) {
QDomNode element = xml_rep->importNode( annotation_list.item(0), true );
rep_root.appendChild( element );
}
QDomNodeList annotation_view_list =
xml_doc_.elementsByTagName( lC::STR::ANNOTATION_VIEW );
if ( annotation_view_list.length() > 0 ) {
QDomNode element = xml_rep->importNode( annotation_view_list.item(0),
true );
rep_root.appendChild( element );
}
QDomNodeList constraint_list =
xml_doc_.elementsByTagName( lC::STR::CONSTRAINTS );
if ( constraint_list.length() > 0 ) {
QDomNode element = xml_rep->importNode( constraint_list.item(0), true );
rep_root.appendChild( element );
}
QDomNodeList unconstraint_list =
xml_doc_.elementsByTagName( lC::STR::UNCONSTRAINTS );
if ( unconstraint_list.length() > 0 ) {
QDomNode element = xml_rep->importNode( unconstraint_list.item(0), true );
rep_root.appendChild( element );
}
}
/*!
* If the creation of a annotation 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.
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 annotation_list =
xml_doc_.elementsByTagName( lC::STR::ANNOTATION );
QDomNodeList annotation_view_list =
xml_doc_.elementsByTagName( lC::STR::ANNOTATION_VIEW );
if ( annotation_list.length() == 0 ||
annotation_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( ) );
annotation_list.item(0).toElement().
setAttribute( lC::STR::NAME, rename->newDBURL().name() );
annotation_view_list.item(0).toElement().setAttribute( lC::STR::ANNOTATION,
rename->newDBURL().toString( ) );
return true;
}
return false;
}
};
/*
* Construct a creation input object for an annotation.
*/
AnnotationCreateInput::AnnotationCreateInput ( Annotation* annotation,
AnnotationView* annotation_view )
: annotation_( annotation ), annotation_view_( annotation_view )
{}
/*
* Set up UI for creating an annotation.
*/
void AnnotationCreateInput::startDisplay ( QMenu* /*context_menu*/ )
{
QAction* cancel_action =
annotation_view_->parent()->lCMW()->getUi()->cancelAnnotationAction;
separator_id_ = annotation_view_->view()->contextMenu()->addSeparator();
annotation_view_->view()->contextMenu()->addAction ( cancel_action );
connect( cancel_action, SIGNAL( activated() ), SLOT( cancelOperation() ) );
annotation_view_->view()->
setCursor( CursorFactory::instance().cursor( CursorFactory::ANNOTATION ) );
}
/*
* Initialize the annotation's origin.
*/
void AnnotationCreateInput::mousePress ( QMouseEvent* me,
const SelectedNames& /*selected*/ )
{
Point origin = annotation_view_->view()->unproject2D( me->pos() );
// Hmm. I guess I don't see any point in this...
// UnitsBasis::instance()->round( origin );
annotation_->initialize( origin );
}
/*
* Resize the annotation text box.
*/
void AnnotationCreateInput::mouseDrag ( QMouseEvent* me,
const SelectedNames& /*selected*/ )
{
Point corner = annotation_view_->view()->unproject2D( me->pos() );
// Hmm. I guess I don't see any point in this...
// UnitsBasis::instance()->round( corner );
annotation_view_->moveCorner( corner );
}
/*
* Complete the annotation text box and put up the text editing dialog.
*/
bool AnnotationCreateInput::mouseRelease ( QMouseEvent* me,
const SelectedNames& /*selected*/ )
{
Point corner = annotation_view_->view()->unproject2D( me->pos() );
// Hmm. I guess I don't see any point in this...
// UnitsBasis::instance()->round( corner );
annotation_view_->moveCorner( corner );
int ret = annotation_view_->editAnnotationInformation();
if ( ret == QDialog::Rejected ) {
cancelOperation();
return true;
}
annotation_->finish();
PageView* page_view = annotation_view_->parent();
QAction* cancel_action = page_view->lCMW()->getUi()->cancelAnnotationAction;
cancel_action->disconnect();
annotation_view_->view()->contextMenu()->removeAction( cancel_action );
annotation_view_->view()->contextMenu()->removeAction( separator_id_ );
annotation_view_->unsetCursor();
annotation_view_->parent()->figureComplete();
annotation_view_->parent()->activateFigure( annotation_view_ );
CommandHistory::instance().
addCommand( new CreateCommand( "create annotation",
annotation_view_->memento() ) );
return false;
}
/*
* Cancel the operation on the Escape key.
*/
void AnnotationCreateInput::keyPress ( QKeyEvent* ke )
{
if ( ke->key() == Qt::Key_Escape )
cancelOperation();
else
ke->ignore();
}
/*
* Cancel the annotation creation.
*/
void AnnotationCreateInput::cancelOperation ( void )
{
PageView* page_view = annotation_view_->parent();
QAction* cancel_action = page_view->lCMW()->getUi()->cancelAnnotationAction;
cancel_action->disconnect();
annotation_view_->view()->contextMenu()->addAction( cancel_action );
annotation_view_->view()->contextMenu()->removeAction( separator_id_ );
page_view->removeFigureView( annotation_view_ );
page_view->cancelOperation();
}
/*!
* Internal class that handles the undo/redo command for moveing/resizing
* the annotation text box.
*/
class MoveAnnotationCommand : public Command {
public:
/*!
* Construct a command to undo/redo a move/resize of an annotation view.
* \param name name of command.
* \param design_book_view reference to Design Book View.
* \param db_url reference to annotation object.
* \param old_origin the old origin of the annotation (in model units)
* \param old_size the old size of the annotation box (in paper units)
* \param new_origin the new origin of the annotation (in model units)
* \param new_size the new size of the annotation box (in paper units)
*/
MoveAnnotationCommand ( const QString& name,
DesignBookView* design_book_view,
const DBURL& db_url,
Point old_origin,
Vector old_size,
Point new_origin,
Vector new_size )
: Command( name ), design_book_view_( design_book_view ),
db_url_( db_url ), old_origin_( old_origin ),
old_size_( old_size ), new_origin_( new_origin ), new_size_( new_size )
{}
//! Delete the move annotation undo/redo command.
~MoveAnnotationCommand ( void )
{}
//! \return command type.
Type type ( void ) const { return MoveAnnotation; }
//! \return if possible, merge the argument command into this command.
bool merge ( Command* /*command*/ ) { return false; }
//! (Re)execute the command (i.e., redo).
void execute ( void )
{
Model* model = design_book_view_->model();
Annotation* annotation = dynamic_cast<Annotation*>( model->lookup(db_url_));
annotation->setOrigin( new_origin_ );
AnnotationView* annotation_view =
dynamic_cast<AnnotationView*>( design_book_view_->lookup( db_url_ ) );
annotation_view->setSize( new_size_ );
}
//! Unexecute the command (i.e., undo).
void unexecute ( void )
{
Model* model = design_book_view_->model();
Annotation* annotation = dynamic_cast<Annotation*>( model->lookup(db_url_));
annotation->setOrigin( old_origin_ );
AnnotationView* annotation_view =
dynamic_cast<AnnotationView*>( design_book_view_->lookup( db_url_ ) );
annotation_view->setSize( old_size_ );
}
/*!
* Serialize this command.
* \param document XML representation to append this command to.
*/
void write ( QDomDocument* document ) const
{
QDomElement move_element = document->createElement( lC::STR::MOVE_ANNOTATION);
QDomNode root_node = document->firstChild();
if ( !root_node.isNull() )
document->replaceChild( move_element, root_node );
else
document->appendChild( move_element );
move_element.setAttribute( lC::STR::URL, db_url_.toString() );
move_element.setAttribute( lC::STR::OLD_X, lC::format( old_origin_[X] ) );
move_element.setAttribute( lC::STR::OLD_Y, lC::format( old_origin_[Y] ) );
move_element.setAttribute( lC::STR::OLD_WIDTH, lC::format( old_size_[X] ) );
move_element.setAttribute( lC::STR::OLD_HEIGHT, lC::format( old_size_[Y] ) );
move_element.setAttribute( lC::STR::NEW_X, lC::format( new_origin_[X] ) );
move_element.setAttribute( lC::STR::NEW_Y, lC::format( new_origin_[Y] ) );
move_element.setAttribute( lC::STR::NEW_WIDTH, lC::format( new_size_[X] ) );
move_element.setAttribute( lC::STR::NEW_HEIGHT, lC::format( new_size_[Y] ) );
}
private:
//! Design Book view containing this annotation.
DesignBookView* design_book_view_;
//! Reference of annotation.
DBURL db_url_;
//! Old annotation origin (in model units)
Point old_origin_;
//! Old annotation box size (in paper units)
Vector old_size_;
//! new annotation origin (in model units)
Point new_origin_;
//! New annotation box size (in paper units)
Vector new_size_;
};
/*!
* Construct an input object for modifying an annotation.
* \param annotation reference to the annotation.
* \param annotation_view reference to the annotation's view.
*/
AnnotationModifyInput::AnnotationModifyInput ( Annotation* annotation,
AnnotationView* annotation_view )
: selection_type_( TRANSIENT, FIGURE ),
annotation_( annotation ), annotation_view_( annotation_view ), xml_rep_( 0 )
{}
/*!
* Accept the scanning mouse position.
* \param me mouse position.
* \param selected selected name records.
*/
void AnnotationModifyInput::mousePrepress ( QMouseEvent* me,
const SelectedNames& selected )
{
if ( selected.empty() ) {
annotation_view_->unsetCursor();
return;
}
SelectedNames::const_iterator f;
for ( f = selected.begin(); f != selected.end(); ++f ) {
if ( (*f).second[0] == annotation_view_->selectionName() ) {
if ( (*f).second.size() > 1 ) {
GLuint g = (*f).second[1]; // (*(*f).second->rbegin()).first;
if ( annotation_view_->isGeometry( g ) ) {
annotation_view_->setCursor( g );
me->accept();
}
else {
annotation_view_->unsetCursor();
}
return; // Probably not the last word here
}
}
}
annotation_view_->unsetCursor();
}
void AnnotationModifyInput::mousePress ( QMouseEvent* me,
const SelectedNames& selected )
{
// Let the Page handle these events
if ( me->button() != Qt::LeftButton || selected.empty() ) {
me->ignore();
return;
}
SelectedNames::const_iterator f;
for ( f = selected.begin(); f != selected.end(); ++f ) {
if ( (*f).second[0] == annotation_view_->selectionName() ) {
if ( (*f).second.size() > 1 ) {
GLuint g = (*f).second[1]; // (*(*f).second->rbegin()).first;
if ( annotation_view_->isGeometry( g ) ) {
start_pnt_ = annotation_view_->view()->unproject2D( me->pos() );
// Hmm. I guess I don't see any point in this...
// UnitsBasis::instance()->round( start_pnt_ );
last_pnt_ = start_pnt_;
drag_handle_ = g;
old_origin_ = annotation_->origin();
old_size_ = annotation_view_->size();
selection_type_ = SelectionType( TRANSIENT, NONE );
return;
}
}
}
}
me->ignore();
}
void AnnotationModifyInput::mouseDrag ( QMouseEvent* me,
const SelectedNames& /*selected*/ )
{
if ( drag_handle_ == 0 ) return;
current_pnt_ = annotation_view_->view()->unproject2D( me->pos() );
// Hmm. I guess I don't see any point in this...
// UnitsBasis::instance()->round( current_pnt_ );
lC::ValidDelta ok = annotation_view_->adjust( drag_handle_,
last_pnt_, current_pnt_);
// last_pnt_ = current_pnt_;
if ( ok.dx_ok_ )
last_pnt_[X] = current_pnt_[X];
if ( ok.dy_ok_ )
last_pnt_[Y] = current_pnt_[Y];
annotation_view_->parent()->figureModified();
}
bool AnnotationModifyInput::mouseRelease ( QMouseEvent* /*me*/,
const SelectedNames& /*selected*/ )
{
if ( last_pnt_ != start_pnt_ ) {
MoveAnnotationCommand* command =
new MoveAnnotationCommand( "move annotation",
annotation_view_->parent()->parent(),
annotation_->dbURL(),
old_origin_, old_size_,
annotation_->origin(), annotation_view_->size());
CommandHistory::instance().addCommand( command );
}
selection_type_ = SelectionType( TRANSIENT, FIGURE );
drag_handle_ = 0;
// Might do a sanity check comparing to the original point (?)
// annotation_view_->unsetCursor(); //
// Continue in this mode until unselected
return false;
}
void AnnotationModifyInput::mouseDoubleClick ( QMouseEvent* /*me*/ )
{
annotation_view_->editInformation();
}
void AnnotationModifyInput::setAnnotation ( Annotation* annotation )
{
annotation_ = annotation;
}
class ChangeAnnotationCommand : public Command {
Model* model_;
DBURL db_url_;
QString old_text_;
QString new_text_;
public:
ChangeAnnotationCommand ( const QString& name, Model* model,const DBURL& db_url,
QString old_text, QString new_text )
: Command( name ), model_( model ), db_url_( db_url ),
old_text_( old_text ), new_text_( new_text )
{}
~ChangeAnnotationCommand ( void )
{}
Type type ( void ) const { return ChangeAnnotation; }
bool merge ( Command* /*command*/ ) { return false; }
void execute ( void )
{
Annotation* annotation = dynamic_cast<Annotation*>( model_->lookup(db_url_));
annotation->setText( new_text_ );
}
void unexecute ( void )
{
Annotation* annotation = dynamic_cast<Annotation*>( model_->lookup(db_url_));
annotation->setText( old_text_ );
}
void write ( QDomDocument* document ) const
{
QDomElement change_element =
document->createElement( lC::STR::CHANGE_ANNOTATION );
QDomNode root_node = document->firstChild();
if ( !root_node.isNull() )
document->replaceChild( change_element, root_node );
else
document->appendChild( change_element );
change_element.setAttribute( lC::STR::URL, db_url_.toString() );
change_element.setAttribute( lC::STR::OLD_TEXT, old_text_ );
change_element.setAttribute( lC::STR::NEW_TEXT, new_text_ );
}
};
AnnotationInfoDialog* AnnotationView::annotation_info_dialog_ = 0;
AnnotationView::AnnotationView ( Annotation* annotation, PageView* parent )
: FigureView( parent ), annotation_( annotation ),
create_input_( annotation, this ), modify_input_( annotation, this )
{
init();
}
AnnotationView::AnnotationView ( const QDomElement& xml_rep, PageView* parent )
: FigureView( parent ), annotation_( 0 ),
create_input_( 0, this ), modify_input_( 0, this )
{
DBURL db_url = xml_rep.attribute( lC::STR::ANNOTATION );
size_ = Vector( xml_rep.attribute( lC::STR::WIDTH ).toDouble(),
xml_rep.attribute( lC::STR::HEIGHT ).toDouble() );
annotation_ =
dynamic_cast< Annotation* >( model()->lookup( db_url ) );
init();
list_view_item_->setData( trC( lC::STR::RECTANGLE_FMT ).
arg( annotation_->origin()[X] ).
arg( annotation_->origin()[Y] ).
arg( width() ).
arg( height() ),
lC::DETAIL );
modify_input_.setAnnotation( annotation_ );
}
AnnotationView::AnnotationView ( const QString& name, const QDomElement& xml_rep,
Page* page, PageView* parent )
: FigureView( parent ), annotation_( 0 ),
create_input_( 0, this ), modify_input_( 0, this )
{
QDomDocument doc = xml_rep.ownerDocument();
QDomNodeList annotation_list = doc.elementsByTagName( lC::STR::ANNOTATION );
// Well, failure is not really an option...but it could happen if
// somebody put a partial XML document into the clipboard...
// In general, the failure modes of lignumCAD need substantially more
// consideration......
if ( annotation_list.count() > 0 )
annotation_ = new Annotation( page->newID(), name,
annotation_list.item(0).toElement(), page );
size_ = Vector( xml_rep.attribute( lC::STR::WIDTH ).toDouble(),
xml_rep.attribute( lC::STR::HEIGHT ).toDouble() );
init();
list_view_item_->setData( trC( lC::STR::RECTANGLE_FMT ).
arg( annotation_->origin()[X] ).
arg( annotation_->origin()[Y] ).
arg( width() ).
arg( height() ),
lC::DETAIL );
modify_input_.setAnnotation( annotation_ );
}
AnnotationView::~AnnotationView ( void )
{
if ( annotation_ )
annotation_->parent()->removeFigure( annotation_ );
delete list_view_item_;
}
/*!
* Decide whether the annotation or the dimension is being
* manipulated and return the responsible controller
*/
InputObject* AnnotationView::modifyInput ( void )
{
if ( isActivated() )
return &modify_input_;
return 0;
}
void AnnotationView::init ( void )
{
setObjectName( annotation_->name() + lC::STR::VIEW_EXT );
left_edge_.setSelectionName( view()->genSelectionName() );
right_edge_.setSelectionName( view()->genSelectionName() );
bottom_edge_.setSelectionName( view()->genSelectionName() );
top_edge_.setSelectionName( view()->genSelectionName() );
left_edge_.setCursorShape( Qt::SizeAllCursor );
right_edge_.setCursorShape( Qt::SizeAllCursor );
bottom_edge_.setCursorShape( Qt::SizeAllCursor );
top_edge_.setCursorShape( Qt::SizeAllCursor );
solid_annotation_.setSelectionName( view()->genSelectionName() );
solid_annotation_.setCursorShape( Qt::SizeAllCursor );
lb_handle_.setSelectionName( view()->genSelectionName() );
mb_handle_.setSelectionName( view()->genSelectionName() );
rb_handle_.setSelectionName( view()->genSelectionName() );
mr_handle_.setSelectionName( view()->genSelectionName() );
rt_handle_.setSelectionName( view()->genSelectionName() );
mt_handle_.setSelectionName( view()->genSelectionName() );
lt_handle_.setSelectionName( view()->genSelectionName() );
ml_handle_.setSelectionName( view()->genSelectionName() );
lb_handle_.setCursorShape( Qt::SizeBDiagCursor );
mb_handle_.setCursorShape( Qt::SizeVerCursor );
rb_handle_.setCursorShape( Qt::SizeFDiagCursor );
mr_handle_.setCursorShape( Qt::SizeHorCursor );
rt_handle_.setCursorShape( Qt::SizeBDiagCursor );
mt_handle_.setCursorShape( Qt::SizeVerCursor );
lt_handle_.setCursorShape( Qt::SizeFDiagCursor );
ml_handle_.setCursorShape( Qt::SizeHorCursor );
// Reverse lookup of GL selection name to object
annotation_objects_.insert( left_edge_.selectionName(), left_edge_ );
annotation_objects_.insert( right_edge_.selectionName(), right_edge_ );
annotation_objects_.insert( bottom_edge_.selectionName(), bottom_edge_ );
annotation_objects_.insert( top_edge_.selectionName(), top_edge_ );
annotation_objects_.insert( solid_annotation_.selectionName(),
solid_annotation_ );
annotation_objects_.insert( lb_handle_.selectionName(), lb_handle_ );
annotation_objects_.insert( mb_handle_.selectionName(), mb_handle_ );
annotation_objects_.insert( rb_handle_.selectionName(), rb_handle_ );
annotation_objects_.insert( mr_handle_.selectionName(), mr_handle_ );
annotation_objects_.insert( rt_handle_.selectionName(), rt_handle_ );
annotation_objects_.insert( mt_handle_.selectionName(), mt_handle_ );
annotation_objects_.insert( lt_handle_.selectionName(), lt_handle_ );
annotation_objects_.insert( ml_handle_.selectionName(), ml_handle_ );
adjustments_[left_edge_.selectionName()] = &AnnotationView::move;
adjustments_[right_edge_.selectionName()] = &AnnotationView::move;
adjustments_[bottom_edge_.selectionName()] = &AnnotationView::move;
adjustments_[top_edge_.selectionName()] = &AnnotationView::move;
adjustments_[solid_annotation_.selectionName()] = &AnnotationView::move;
adjustments_[ml_handle_.selectionName()] = &AnnotationView::resizeLeft;
adjustments_[mr_handle_.selectionName()] = &AnnotationView::resizeRight;
adjustments_[mb_handle_.selectionName()] = &AnnotationView::resizeBottom;
adjustments_[mt_handle_.selectionName()] = &AnnotationView::resizeTop;
adjustments_[lb_handle_.selectionName()] = &AnnotationView::resizeLeftBottom;
adjustments_[rb_handle_.selectionName()] = &AnnotationView::resizeRightBottom;
adjustments_[rt_handle_.selectionName()] = &AnnotationView::resizeRightTop;
adjustments_[lt_handle_.selectionName()] = &AnnotationView::resizeLeftTop;
if ( annotation_info_dialog_ == 0 )
annotation_info_dialog_ = new AnnotationInfoDialog( parent()->lCMW() );
ListViewItem* previous_item = parent()->previousItem( parent()->listViewItem(),
annotation_->id() );
list_view_item_ = new ListViewItem( parent()->listViewItem(), previous_item );
list_view_item_->setData( lC::formatName( annotation_->name() )
+ QString( " <%1>" ).arg( annotation_->id() ),
lC::NAME );
list_view_item_->setData( trC( lC::STR::ANNOTATION ), lC::TYPE );
list_view_item_->setData( trC( lC::STR::UNDEFINED ), lC::DETAIL );
//list_view_item_->setOpen( true );
//list_view_item_->setRenameEnabled( lC::NAME, true );
//TODO
//list_view_item_->listView()->ensureItemVisible( list_view_item_ );
//TODO
//connect( list_view_item_, SIGNAL( nameChanged( const QString& ) ),
// SLOT( listNameChanged( const QString& ) ) );
connect( annotation_, SIGNAL( initialized() ), SLOT( annotationInitialized()));
connect( annotation_, SIGNAL( moved() ), SLOT( annotationMoved() ) );
connect( annotation_, SIGNAL( nameChanged( const QString& ) ),
SLOT( updateName( const QString& ) ) );
}
CreateObject* AnnotationView::memento ( void )
{
return new AnnotationViewCreate( this );
}
// Implementation of FigureView interface
uint AnnotationView::id ( void ) const
{
return annotation_->id();
}
QString AnnotationView::name ( void ) const
{
return annotation_->name();
}
QString AnnotationView::type ( void ) const
{
return lC::STR::ANNOTATION;
}
DBURL AnnotationView::dbURL ( void ) const
{
return annotation_->dbURL();
}
QString AnnotationView::selectionText ( const std::vector<GLuint>& /*selection_name*/,
SelectionEntity /*entity*/ ) const
{
return annotation_->name();
}
void AnnotationView::setHighlighted ( bool highlight, SelectionEntity entity,
const std::vector<GLuint>& /*items*/ )
{
if ( entity == FIGURE )
FigureViewBase::setHighlighted( highlight );
}
void AnnotationView::setActivated ( bool activate, SelectionEntity entity,
const std::vector<GLuint>& /*items*/ )
{
if ( entity == FIGURE )
FigureViewBase::setActivated( activate );
}
/*!
* Determine if this selection is part of the geometry representation;
* this includes the handles if they happen to be highlighted, too.
* The purpose here is really just to distinguish between the
* lines and the dimension views.
*/
bool AnnotationView::isGeometry ( GLuint selection_name ) const
{
QHashIterator< int,GraphicsView > igv( annotation_objects_ );
while ( igv.hasNext() )
if ( (GLuint)igv.next().key() == selection_name )
return true;
return false;
}
void AnnotationView::editInformation ( void )
{
if ( FigureViewBase::isActivated() ) {
editAnnotationInformation();
}
}
int AnnotationView::editAnnotationInformation ( void )
{
annotation_info_dialog_->getUi()->nameEdit->
setText( lC::formatName( annotation_->name() ) );
annotation_info_dialog_->getUi()->annotationTextEdit->setText( annotation_->text() );
redo:
int ret = annotation_info_dialog_->exec();
if ( ret == QDialog::Rejected ) return ret;
bool modified = false;
// Cannot let the user choose a name which is already used for an object
// of this type on this page.
if ( annotation_info_dialog_->getUi()->nameEdit->isModified() ) {
// Pageview handles checking the name and putting up the error dialog
// if necessary.
int ret = parent()->uniqueFigureName( this,
annotation_info_dialog_->getUi()->nameEdit->text(),
annotation_->type() );
switch ( ret ) {
case lC::Rejected:
return QDialog::Rejected;
case lC::Redo:
annotation_info_dialog_->getUi()->nameEdit->
setText( lC::formatName( annotation_->name() ) );
goto redo;
}
}
if ( annotation_info_dialog_->getUi()->annotationTextEdit->document()->isModified() ) {
// If this a change applied after the annotation is created,
// then we need to record this as an undoable modification.
if ( annotation_->isComplete() ) {
ChangeAnnotationCommand* change_command =
new ChangeAnnotationCommand( "change annotation", model(),
annotation_->dbURL(),
annotation_->text(),
annotation_info_dialog_->getUi()->annotationTextEdit->document()->toPlainText() );
CommandHistory::instance().addCommand( change_command );
}
annotation_->setText( annotation_info_dialog_->getUi()->annotationTextEdit->document()->toPlainText() );
modified = true;
}
// Renaming the annotation itself needs to follow the annotation
// change command so that the rename is merged with the change
// command (if there was one).
if ( annotation_info_dialog_->getUi()->nameEdit->isModified() ) {
// Again, only if this is not the initial activation of this
// dialog do we want to create an undo-able rename.
if ( annotation_->isComplete() )
setName( annotation_info_dialog_->getUi()->nameEdit->text() );
else
annotation_->setName( annotation_info_dialog_->getUi()->nameEdit->text() );
modified = true;
}
if ( modified ) parent()->figureModified();
return ret;
}
void AnnotationView::draw ( void ) const
{
if ( !annotation_->isValid() ) return;
// If highlighted, draw an "X" at the origin and a box around the text.
if ( isHighlighted() || isActivated() ) {
glColor3ubv( lC::qCubv( view()->constraintPrimaryColor()));
double size = 1./16 * view()->scale();
double gap = size / 2.;
glBegin( GL_LINES );
Point p;
p = annotation_->origin() + Vector( gap, gap );
glVertex2dv( p );
p += Vector( size, size );
glVertex2dv( p );
p = annotation_->origin() + Vector( -gap, gap );
glVertex2dv( p );
p += Vector( -size, size );
glVertex2dv( p );
p = annotation_->origin() + Vector( -gap, -gap );
glVertex2dv( p );
p += Vector( -size, -size );
glVertex2dv( p );
p = annotation_->origin() + Vector( gap, -gap );
glVertex2dv( p );
p += Vector( size, -size );
glVertex2dv( p );
glEnd();