-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemblyview.cpp
2574 lines (2074 loc) · 80.6 KB
/
assemblyview.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
/*
* assemblyview.cpp
*
* AssemblyView class: a view of a Assembly.
* 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 <QMenu>
#include <qaction.h>
#include <qtabbar.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <QDockWidget>
#include <qlayout.h>
#include <qwhatsthis.h>
#include <qtextbrowser.h>
#include <qbuttongroup.h>
#include <qlabel.h>
#include <BRepTools.hxx>
#include <TopoDS_Compound.hxx>
#include "constants.h"
#include "units.h"
#include "cursorfactory.h"
#include "dimension.h"
#include "command.h"
#include "model.h"
#include "part.h"
#include "openglview.h"
#include "lignumcadmainwindow.h"
#include "designbookview.h"
#include "listviewitem.h"
#include "ocsolid.h"
#include "ochiddendraw.h"
#include "offsetinfodialog.h"
#include "lcdefaultlengthconstraint.h"
#include "pagefactory.h"
#include "subassembly.h"
#include "subassemblyview.h"
#include "assemblyconfigdialog.h"
#include "assemblyadddialog.h"
#include "assemblyconstraintform.h"
#include "assemblyview.h"
#include "lcdebug.h"
#include <QMouseEvent>
/*!
* AssemblyViewCreate is a memento of the current state of a Assembly and its view.
* It handles creating and deleting both objects.
*/
class AssemblyViewCreate : 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 assembly and view are stored as XML.
QDomDocument xml_doc_;
public:
/*!
* Create a memento of the current state of the assembly and its view.
* \param assembly_view view object of the assembly.
*/
AssemblyViewCreate ( AssemblyView* assembly_view )
{
design_book_view_ = assembly_view->parent();
QDomElement root = xml_doc_.createElement( lC::STR::MEMENTO );
root.setAttribute( lC::STR::NAME, assembly_view->dbURL().toString() );
assembly_view->assembly()->write( root );
assembly_view->write( root );
xml_doc_.appendChild( root );
}
//! Destructor does not do much.
~AssemblyViewCreate ( void ) {}
/*!
* (Re)create the assembly object and its view as specified in
* the XML representation.
*/
void create ( void )
{
QDomNodeList assembly_list = xml_doc_.elementsByTagName( lC::STR::ASSEMBLY );
if ( assembly_list.length() > 0 ) {
QDomNodeList assembly_view_list =
xml_doc_.elementsByTagName( lC::STR::ASSEMBLY_VIEW );
if ( assembly_view_list.length() > 0 ) {
uint assembly_id =
assembly_list.item(0).toElement().attribute( lC::STR::ID ).toUInt();
new Assembly( assembly_id, assembly_list.item(0).toElement(),
design_book_view_->model() );
AssemblyView* assembly_view =
new AssemblyView( design_book_view_, assembly_view_list.item(0).toElement() );
design_book_view_->addPageView( assembly_view );
design_book_view_->showPageView( assembly_view );
}
}
}
/*!
* Delete the assembly object and its view as specified in the
* XML representation.
*/
void remove ( void )
{
QDomNodeList assembly_view_list =xml_doc_.elementsByTagName(lC::STR::ASSEMBLY_VIEW);
if ( assembly_view_list.length() > 0 ) {
QDomElement assembly_view_element = assembly_view_list.item(0).toElement();
QString assembly_path = assembly_view_element.attribute( lC::STR::ASSEMBLY );
AssemblyView* assembly_view =
dynamic_cast<AssemblyView*>( design_book_view_->lookup( assembly_path ) );
design_book_view_->deletePage( assembly_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 assembly_list = xml_doc_.elementsByTagName( lC::STR::ASSEMBLY );
if ( assembly_list.length() > 0 ) {
QDomNode element = xml_rep->importNode( assembly_list.item(0), true );
rep_root.appendChild( element );
}
QDomNodeList assembly_view_list =
xml_doc_.elementsByTagName( lC::STR::ASSEMBLY_VIEW );
if ( assembly_view_list.length() > 0 ) {
QDomNode element = xml_rep->importNode( assembly_view_list.item(0), true );
rep_root.appendChild( element );
}
}
/*!
* If the creation of a assembly 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 assembly_list = xml_doc_.elementsByTagName( lC::STR::ASSEMBLY );
QDomNodeList assembly_view_list =
xml_doc_.elementsByTagName( lC::STR::ASSEMBLY_VIEW );
if ( assembly_list.length() == 0 || assembly_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() );
assembly_list.item(0).toElement().
setAttribute( lC::STR::NAME, rename->newDBURL().name() );
assembly_view_list.item(0).toElement().setAttribute( lC::STR::ASSEMBLY,
rename->newDBURL().toString() );
// Have to update the subassembly views as well!
QDomNodeList subassembly_view_list =
xml_doc_.elementsByTagName( lC::STR::SUBASSEMBLY_VIEW );
for ( int i = 0; i < subassembly_view_list.length(); i++ ) {
DBURL old_db_url =
subassembly_view_list.item(i).toElement().attribute(lC::STR::SUBASSEMBLY);
DBURL new_db_url( rename->newDBURL().toString(),
old_db_url.name() + '.' + old_db_url.type() );
subassembly_view_list.item(i).toElement().
setAttribute( lC::STR::SUBASSEMBLY, new_db_url.toString( ) );
}
return true;
}
return false;
}
};
/*!
* I don't know. Try to record the process of making assembly constraints
* so that they can be undone.
*/
class CreateConstraintCommand : public Command {
public:
CreateConstraintCommand ( const QString& name, DesignBookView* design_book_view,
Subassembly* subassembly )
: Command( name ), design_book_view_( design_book_view ),
db_url_( subassembly->dbURL() )
{
QListIterator< std::shared_ptr<AssemblyConstraint>> constraints =
subassembly->constraints().constraints();
constraints.toBack();
//TODO check if this is correct
if ( constraints.hasPrevious() ) {
QDomElement constraint_element =
xml_doc_.createElement( lC::STR::CREATE_CONSTRAINT);
constraints.previous()->write( constraint_element );
xml_doc_.appendChild( constraint_element );
}
}
//! \return the type of this command (create [an assembly] constraint)
Type type ( void ) const { return CreateConstraint; }
/*!
* Attempt to merge the argument command with this command so that
* they are undone together. I think these will have to stand alone, however.
* \param command command to attempt to merge.
* \return true if command was merged.
*/
bool merge ( Command* /*command*/ ) { return false; }
/*!
* Perform the constraint given in this command (i.e., redo constraint).
*/
void execute ( void )
{
Subassembly* subassembly =
dynamic_cast<Subassembly*>( design_book_view_->model()->lookup( db_url_ ) );
QDomElement constraint_element = xml_doc_.firstChild().firstChild().toElement();
subassembly->constraints().
updateConstraint( xml_doc_.firstChild().firstChild().toElement() );
}
/*!
* Restore the subassembly to its previous state (i.e. undo constraint).
*/
void unexecute ( void )
{
Subassembly* subassembly =
dynamic_cast<Subassembly*>( design_book_view_->model()->lookup( db_url_ ) );
QDomElement constraint_element = xml_doc_.firstChild().firstChild().toElement();
if ( constraint_element.attribute( lC::STR::SURFACE1 ).isEmpty() ) {
if ( constraint_element.attribute( lC::STR::SURFACE0 ).isEmpty() )
subassembly->constraints().cancelCurrent();
else
subassembly->constraints().removeFirstReference();
}
else
subassembly->constraints().removeLastReference();
}
/*!
* Write the operation to the dribble file in its XML representation.
* \param document document to append XML representation to.
*/
void write ( QDomDocument* xml_rep ) const
{
QDomElement constraint_element =
xml_rep->importNode( xml_doc_.firstChild(), true ).toElement();
constraint_element.setAttribute( lC::STR::SUBASSEMBLY, db_url_.toString() );
QDomNode root_node = xml_rep->firstChild();
if ( !root_node.isNull() )
xml_rep->replaceChild( constraint_element, root_node );
else
xml_rep->appendChild( constraint_element );
}
private:
//! Reference to Design Book view for looking up items.
DesignBookView* design_book_view_;
//! Path to subassembly.
DBURL db_url_;
//! XML representation of current state of constraint.
QDomDocument xml_doc_;
};
/*!
* I don't know. Try to record the process of deleting assembly constraints
* so that they can be redone.
*/
class DeleteConstraintCommand : public Command {
public:
DeleteConstraintCommand ( const QString& name, DesignBookView* design_book_view,
Subassembly* subassembly )
: Command( name ), design_book_view_( design_book_view ),
db_url_( subassembly->dbURL() )
{
QListIterator<std::shared_ptr<AssemblyConstraint>> constraints =
subassembly->constraints().constraints();
constraints.toBack();
//TODO check if correct
if ( constraints.hasPrevious() != 0 ) {
QDomElement constraint_element =
xml_doc_.createElement( lC::STR::DELETE_CONSTRAINT);
constraints.previous()->write( constraint_element );
xml_doc_.appendChild( constraint_element );
}
}
//! \return the type of this command (delete [an assembly] constraint)
Type type ( void ) const { return DeleteConstraint; }
/*!
* Attempt to merge the argument command with this command so that
* they are undone together. I think these will have to stand alone, however.
* \param command command to attempt to merge.
* \return true if command was merged.
*/
bool merge ( Command* /*command*/ ) { return false; }
/*!
* Perform the constraint given in this command (i.e., redo constraint).
*/
void execute ( void )
{
Subassembly* subassembly =
dynamic_cast<Subassembly*>( design_book_view_->model()->lookup( db_url_ ) );
QDomElement constraint_element = xml_doc_.firstChild().firstChild().toElement();
if ( constraint_element.attribute( lC::STR::SURFACE1 ).isEmpty() ) {
if ( constraint_element.attribute( lC::STR::SURFACE0 ).isEmpty() )
subassembly->constraints().cancelCurrent();
else
subassembly->constraints().removeFirstReference();
}
else
subassembly->constraints().removeLastReference();
}
/*!
* Restore the subassembly to its previous state (i.e. undo constraint).
*/
void unexecute ( void )
{
Subassembly* subassembly =
dynamic_cast<Subassembly*>( design_book_view_->model()->lookup( db_url_ ) );
QDomElement constraint_element = xml_doc_.firstChild().firstChild().toElement();
subassembly->constraints().
updateConstraint( xml_doc_.firstChild().firstChild().toElement() );
}
/*!
* Write the operation to the dribble file in its XML representation.
* \param document document to append XML representation to.
*/
void write ( QDomDocument* xml_rep ) const
{
QDomElement constraint_element =
xml_rep->importNode( xml_doc_.firstChild(), true ).toElement();
constraint_element.setAttribute( lC::STR::SUBASSEMBLY, db_url_.toString() );
QDomNode root_node = xml_rep->firstChild();
if ( !root_node.isNull() )
xml_rep->replaceChild( constraint_element, root_node );
else
xml_rep->appendChild( constraint_element );
}
private:
//! Reference to Design Book view for looking up items.
DesignBookView* design_book_view_;
//! Path to subassembly.
DBURL db_url_;
//! XML representation of current state of constraint.
QDomDocument xml_doc_;
};
/*!
* Take over the input duties when a constraint is being made. Seems to
* actually be generic now (except for offset constraints).
*/
class ConstraintInput : public InputObject {
Q_OBJECT
public:
/*!
* This class is embedded in an Assembly view, but C++
* does not have interior classes so it has to have a pointer to
* the parent.
* \param assembly_view parent assembly view.
*/
ConstraintInput ( AssemblyView* assembly_view )
: assembly_view_( assembly_view ), selection_type_( PICK, FACE )
{}
// Implemention of InputObject interface
//! \return the current selection type (always PICK, but the filter
//! OpenGL name changes).
SelectionType selectionType ( void ) const
{
return selection_type_;
}
//! \return true does need selection scan.
bool needsPrepressMouseCoordinates ( void ) const { return true; }
/*!
* Add whatever entries this class wants to have in the OpenGLView's
* context menu. Also do any other UI tweaks, such as a custom cursor.
*
* Note: This can now be invoked at any time during a constraint's
* construction, so this method has to be more sensitive to the
* current status of the constraint.
*
* \param context_menu main OpenGL view's context menu.
*/
void startDisplay ( QMenu* context_menu )
{
context_menu_ = context_menu;
QAction* cancel_action = assembly_view_->lCMW()->getUi()->cancelAssemblyConstraintAction;
separator_id_ = context_menu_->addSeparator();
context_menu_->addAction(cancel_action);
connect( cancel_action, SIGNAL( activated() ), SLOT( cancelCurrent() ) );
status_ = subassembly_->constraints().status();
switch ( status_ ) {
case Invalid:
selection_type_.filter_ = INCLUSIVE;
assembly_view_->view()->
setCursor( CursorFactory::instance().
cursor( CursorFactory::ASSEMBLY_CONSTRAINT ) );
break;
case OK:
selection_type_.filter_ = EXCLUSIVE;
assembly_view_->view()->
setCursor( CursorFactory::instance().
cursor( CursorFactory::ASSEMBLY_CONSTRAINTPLUS ) );
case ConstraintComplete:
break;
case PlacementComplete:
break;
}
}
/*!
* When changing the current page remove the context dialog action and
* undo any cursor changes.
*/
void stopDisplay ( QMenu* /*context_menu*/ )
{
QAction* cancel_action = assembly_view_->lCMW()->getUi()->cancelAssemblyConstraintAction;
cancel_action->disconnect();
context_menu_->removeAction( cancel_action);
context_menu_->removeAction( separator_id_ );
assembly_view_->view()->unsetCursor();
}
/*!
* Respond to the mouse moving about the display. For this class, PageView
* handles the highlighting.
*/
void mousePrepress ( QMouseEvent* /*me*/, const SelectedNames& /*selected*/ )
{}
/*!
* This class operates in pick mode, so take the selected face and
* make sure it is OK with the constraint.
* \param me Qt mouse event.
* \param selected list of selected items.
*/
void mousePress ( QMouseEvent* me, const SelectedNames& selected )
{
if ( selected.empty() ) {
me->ignore();
status_ = Invalid;
return;
}
SelectedNames::const_iterator f = selected.begin();
if ( (*f).second.size() < 2 ) {
me->ignore();
status_ = Invalid;
return;
}
//TODO check this works
std::shared_ptr<FigureViewBase> tmpFig = (assembly_view_-> figureSelectionNames()[ (*f).second[0] ]);
SubassemblyView* sv =
dynamic_cast<SubassemblyView*>( tmpFig.get() );
// Validate this geometry against the current constraint, but don't
// emit the constraint changed signal until the user releases the mouse
// button (seems to make more sense, now).
status_ = subassembly_->constraints().validate( sv->geomPath( (*f).second ) );
if ( status_ == Invalid ) {
QWhatsThis::showText(QCursor::pos (), tr( "The selected face is invalid." ) );
// me->ignore();
}
}
/*!
* Since it runs in pick mode, there is nothing to drag.
*/
void mouseDrag ( QMouseEvent* /*me*/, const SelectedNames& /*selected*/ ) {}
/*!
* When the mouse is released, we can either keep asking for more
* surfaces or report that the constraint is complete.
*/
bool mouseRelease ( QMouseEvent* /*me*/, const SelectedNames& /*selected*/ )
{
switch ( status_ ) {
case OK:
// Now ready to signal that the constraint has changed.
subassembly_->constraints().apply();
CommandHistory::instance().
addCommand( new CreateConstraintCommand( "create constraint",
assembly_view_->parent(),
subassembly_ ) );
selection_type_.filter_ = EXCLUSIVE;
assembly_view_->view()->
setCursor(CursorFactory::instance().cursor(CursorFactory::
ASSEMBLY_CONSTRAINTPLUS));
return false; // Pick another.
case Invalid:
return false; // Try again.
case ConstraintComplete:
return false; // Try again.
case PlacementComplete:
return false; // Try again.
}
// The constraint (and possibly the placement) is complete. Now
// actually perform the subassembly transformation. Signals that
// the constraint has changed (again).
subassembly_->constraints().transform();
assembly_view_->figureModified();
CommandHistory::instance().
addCommand( new CreateConstraintCommand( "create constraint",
assembly_view_->parent(),
subassembly_ ) );
return true;
}
//! Can't think of anything for this to do.
void mouseDoubleClick ( QMouseEvent* /*me*/ ) {}
/*!
* The only keyboard input currently of interest is the Escape key to
* cancel the creation of the current constraint.
*/
void keyPress ( QKeyEvent* ke )
{
if ( ke->key() == Qt::Key_Escape )
cancelCurrent();
else
ke->ignore();
}
/*!
* This object is reused as subassemblies are created. Set the subassembly
* which is being modified.
* \param subassembly subassembly gaining constraint.
* \param subassembly_gl_name used to limit surface picks to: first, the
* subassembly; then, the remainder of the items in the assembly.
*/
void setSubassembly ( Subassembly* subassembly, GLuint subassembly_gl_name )
{
subassembly_ = subassembly;
connect( subassembly_, SIGNAL( constraintCanceled() ), SLOT(cancelOperation()));
connect( subassembly_, SIGNAL( constraintChanged( const AssemblyConstraint*,
const AssemblyConstraint* ) ),
SLOT(constraintChanged( const AssemblyConstraint*,
const AssemblyConstraint* ) ) );
selection_type_.filter_ = INCLUSIVE;
selection_type_.figure_ = subassembly_gl_name;
}
public slots:
/*!
* Remove the elements of the user interface relating to this operation
* and alert the parent view that this operation has been canceled.
*/
void cancelOperation ( void )
{
QAction* cancel_action = assembly_view_->lCMW()->getUi()->cancelAssemblyConstraintAction;
cancel_action->disconnect();
context_menu_ ->removeAction( cancel_action );
context_menu_->removeAction( separator_id_ );
disconnect( subassembly_, SIGNAL( constraintCanceled() ),
this, SLOT( cancelOperation() ) );
disconnect( subassembly_, SIGNAL( constraintChanged( const AssemblyConstraint*,
const AssemblyConstraint* ) ),
this, SLOT(constraintChanged( const AssemblyConstraint*,
const AssemblyConstraint* ) ) );
emit done ( false );
assembly_view_->constraintCanceled();
}
signals:
void done ( bool );
private slots:
/*!
* Cancel the current constraint.
*/
void cancelCurrent ( void )
{
CommandHistory::instance().
addCommand( new DeleteConstraintCommand( "delete constraint",
assembly_view_->parent(),
subassembly_ ) );
// This causes the subassembly to emit a signal which we catch
// below to tear down the user interface.
subassembly_->constraints().cancelCurrent();
}
/*!
* The assembly constraint has somehow changed. Note that this can be called
* as a result of either user input or undo/redo operations.
*/
void constraintChanged ( const AssemblyConstraint* /*old_constraint*/,
const AssemblyConstraint* new_constraint )
{
if ( new_constraint->reference0().empty() ) {
selection_type_.filter_ = INCLUSIVE;
assembly_view_->view()->
setCursor( CursorFactory::instance().
cursor( CursorFactory::ASSEMBLY_CONSTRAINT ) );
}
else if ( new_constraint->reference1().empty() ) {
selection_type_.filter_ = EXCLUSIVE;
assembly_view_->view()->
setCursor(CursorFactory::instance().cursor(CursorFactory::
ASSEMBLY_CONSTRAINTPLUS));
}
else {
// Evidently we've completed the constraint.
// Reset the UI.
QAction* cancel_action=assembly_view_->lCMW()->getUi()->cancelAssemblyConstraintAction;
cancel_action->disconnect();
context_menu_ ->removeAction( cancel_action );
context_menu_->removeAction( separator_id_ );
assembly_view_->view()->unsetCursor();
emit done (false );
assembly_view_->constraintComplete();
disconnect( subassembly_, SIGNAL( constraintCanceled() ),
this, SLOT( cancelOperation() ) );
disconnect( subassembly_,
SIGNAL( constraintChanged( const AssemblyConstraint*,
const AssemblyConstraint* ) ),
this, SLOT(constraintChanged( const AssemblyConstraint*,
const AssemblyConstraint* ) ) );
// Also need to signal PlacementComplete (if so)....
if ( subassembly_->constraints().status() == PlacementComplete )
assembly_view_->placementComplete();
}
}
private:
//! OpenGL view context menu.
QMenu* context_menu_;
//! Separator for Cancel action.
QAction* separator_id_;
//! Parent assembly view.
AssemblyView* assembly_view_;
//! Current subassembly being constrained.
Subassembly* subassembly_;
//! Includes selection name of subassembly and filter flag.
SelectionType selection_type_;
//! Status of last attempted surface pick.
AssemblyConstraintStatus status_;
};
/*!
* Take over the input duties when a constraint is being made. Seems to
* actually be generic now (except for offset constraints).
*/
class OffsetConstraintInput : public InputObject {
Q_OBJECT
public:
/*!
* This class is embedded in an Assembly view, but C++
* does not have interior classes so it has to have a pointer to
* the parent.
* \param assembly_view parent assembly view.
*/
OffsetConstraintInput ( AssemblyView* assembly_view )
: assembly_view_( assembly_view ), selection_type_( PICK, FACE )
{}
// Implemention of InputObject interface
//! \return the current selection type (always PICK, but the filter
//! OpenGL name changes).
SelectionType selectionType ( void ) const
{
return selection_type_;
}
//! \return true does need selection scan.
bool needsPrepressMouseCoordinates ( void ) const { return true; }
/*!
* Add whatever entries this class wants to have in the OpenGLView's
* context menu. Also do any other UI tweaks, such as a custom cursor.
*
* Note: This can now be invoked at any time during a constraint's
* construction, so this method has to be more sensitive to the
* current status of the constraint.
*
* \param context_menu main OpenGL view's context menu.
*/
void startDisplay ( QMenu* context_menu )
{
context_menu_ = context_menu;
QAction* cancel_action = assembly_view_->lCMW()->getUi()->cancelAssemblyConstraintAction;
separator_id_ = context_menu_->addSeparator();
context_menu_->addAction( cancel_action );
connect( cancel_action, SIGNAL( activated() ), SLOT( cancelCurrent() ) );
status_ = subassembly_->constraints().status();
switch ( status_ ) {
case Invalid:
selection_type_.filter_ = INCLUSIVE;
assembly_view_->view()->
setCursor( CursorFactory::instance().
cursor( CursorFactory::ASSEMBLY_CONSTRAINT ) );
break;
case OK:
selection_type_.filter_ = EXCLUSIVE;
assembly_view_->view()->
setCursor( CursorFactory::instance().
cursor( CursorFactory::ASSEMBLY_CONSTRAINTPLUS ) );
case ConstraintComplete:
break;
case PlacementComplete:
break;
}
}
/*!
* When changing the current page, remove the context dialog action and
* undo any cursor changes.
*/
void stopDisplay ( QMenu* /*context_menu*/ )
{
QAction* cancel_action = assembly_view_->lCMW()->getUi()->cancelAssemblyConstraintAction;
cancel_action->disconnect();
context_menu_->removeAction( cancel_action );
context_menu_->removeAction( separator_id_ );
assembly_view_->view()->unsetCursor();
}
/*!
* Respond to the mouse moving about the display. For this class, PageView
* handles the highlighting.
*/
void mousePrepress ( QMouseEvent* /*me*/, const SelectedNames& /*selected*/ )
{}
/*!
* This class operates in pick mode, so take the selected face and
* make sure it is OK with the constraint.
* \param me Qt mouse event.
* \param selected list of selected items.
*/
void mousePress ( QMouseEvent* me, const SelectedNames& selected )
{
if ( selected.empty() ) {
me->ignore();
status_ = Invalid;
return;
}
SelectedNames::const_iterator f = selected.begin();
if ( (*f).second.size() < 2 ) {
me->ignore();
status_ = Invalid;
return;
}
//TODO check this works
std::shared_ptr<FigureViewBase> tmpFig = (assembly_view_-> figureSelectionNames()[ (*f).second[0] ]);
SubassemblyView* sv =
dynamic_cast<SubassemblyView*>( tmpFig.get() );
// Validate this geometry against the current constraint, but don't
// emit the constraint changed signal until the user releases the mouse
// button (seems to make more sense, now).
status_ = subassembly_->constraints().validate( sv->geomPath( (*f).second ) );
if ( status_ == Invalid ) {
QWhatsThis::showText( QCursor::pos(), tr( "The selected face is invalid." ) );
#if 0
me->ignore();
#endif
}
}
/*!
* Since it runs in pick mode, there is nothing to drag.
*/
void mouseDrag ( QMouseEvent* /*me*/, const SelectedNames& /*selected*/ ) {}
/*!
* When the mouse is released, we can either keep asking for more
* surfaces or report that the constraint is complete.
*/
bool mouseRelease ( QMouseEvent* /*me*/, const SelectedNames& /*selected*/ )
{
switch ( status_ ) {
case OK:
// Now ready to signal that the constraint has changed.
subassembly_->constraints().apply();
CommandHistory::instance().
addCommand( new CreateConstraintCommand( "create constraint",
assembly_view_->parent(),
subassembly_ ) );
selection_type_.filter_ = EXCLUSIVE;
assembly_view_->view()->
setCursor(CursorFactory::instance().cursor(CursorFactory::
ASSEMBLY_CONSTRAINTPLUS));
return false; // Pick another.
case Invalid:
return false; // Try again.
case ConstraintComplete:
return false; // Try again.
case PlacementComplete:
return false; // Try again.
}
// Prompt for the size of the offset.
double offset = 0;
int ret = assembly_view_->editOffset( subassembly_->constraints().current()->
type(), offset );
if ( ret == QDialog::Rejected ) {
cancelCurrent();
return true;
}
// Apply the offset.
subassembly_->constraints().setOffset( offset );
// The constraint (and possibly the placement) is complete. Now
// actually perform the subassembly transformation. Signals that
// the constraint has changed (again).
subassembly_->constraints().transform();
assembly_view_->figureModified();
CommandHistory::instance().
addCommand( new CreateConstraintCommand( "create constraint",
assembly_view_->parent(),
subassembly_ ) );
return true;
}
//! Can't think of anything for this to do.
void mouseDoubleClick ( QMouseEvent* /*me*/ ) {}
/*!
* The only keyboard input currently of interest is the Escape key to
* cancel the creation of the current constraint.
*/
void keyPress ( QKeyEvent* ke )
{
if ( ke->key() == Qt::Key_Escape )
cancelCurrent();
else
ke->ignore();
}
/*!
* This object is reused as subassemblies are created. Set the subassembly
* which is being modified.
* \param subassembly subassembly gaining constraint.
* \param subassembly_gl_name used to limit surface picks to: first, the
* subassembly; then, the remainder of the items in the assembly.
*/
void setSubassembly ( Subassembly* subassembly, GLuint subassembly_gl_name )
{
subassembly_ = subassembly;
connect( subassembly_, SIGNAL( constraintCanceled() ), SLOT(cancelOperation()));
connect( subassembly_, SIGNAL( constraintChanged( const AssemblyConstraint*,
const AssemblyConstraint* ) ),
SLOT(constraintChanged( const AssemblyConstraint*,
const AssemblyConstraint* ) ) );
selection_type_.filter_ = INCLUSIVE;
selection_type_.figure_ = subassembly_gl_name;
}
public slots:
/*!
* Remove the elements of the user interface relating to this operation
* and alert the parent view that this operation has been canceled.
*/
void cancelOperation ( void )
{
QAction* cancel_action = assembly_view_->lCMW()->getUi()->cancelAssemblyConstraintAction;
cancel_action->disconnect();
context_menu_->removeAction( cancel_action );
context_menu_->removeAction( separator_id_ );
disconnect( subassembly_, SIGNAL( constraintCanceled() ),
this, SLOT( cancelOperation() ) );
disconnect( subassembly_, SIGNAL( constraintChanged( const AssemblyConstraint*,
const AssemblyConstraint* ) ),
this, SLOT(constraintChanged( const AssemblyConstraint*,
const AssemblyConstraint* ) ) );
emit done ( false );
assembly_view_->constraintCanceled();
}
signals: