/
Document.cpp
2500 lines (2208 loc) · 95.2 KB
/
Document.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
/***************************************************************************
* Copyright (c) 2004 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <algorithm>
# include <QAbstractButton>
# include <qapplication.h>
# include <qdir.h>
# include <qfileinfo.h>
# include <QKeySequence>
# include <QTextStream>
# include <qmessagebox.h>
# include <qstatusbar.h>
# include <boost_signals2.hpp>
# include <boost_bind_bind.hpp>
# include <Inventor/actions/SoSearchAction.h>
# include <Inventor/nodes/SoSeparator.h>
#endif
#include <cctype>
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Matrix.h>
#include <Base/Reader.h>
#include <Base/Writer.h>
#include <Base/Tools.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <App/DocumentObjectGroup.h>
#include <App/Transactions.h>
#include <App/AutoTransaction.h>
#include <App/GeoFeatureGroupExtension.h>
#include "Application.h"
#include "MainWindow.h"
#include "Tree.h"
#include "Document.h"
#include "DocumentPy.h"
#include "Command.h"
#include "Control.h"
#include "FileDialog.h"
#include "View3DInventor.h"
#include "View3DInventorViewer.h"
#include "BitmapFactory.h"
#include "ViewProviderDocumentObject.h"
#include "ViewProviderDocumentObjectGroup.h"
#include "Selection.h"
#include "WaitCursor.h"
#include "Thumbnail.h"
#include "ViewProviderLink.h"
FC_LOG_LEVEL_INIT("Gui",true,true)
using namespace Gui;
namespace bp = boost::placeholders;
namespace Gui {
// Pimpl class
struct DocumentP
{
Thumbnail thumb;
int _iWinCount;
int _iDocId;
bool _isClosing;
bool _isModified;
bool _isTransacting;
bool _changeViewTouchDocument;
int _editMode;
ViewProvider* _editViewProvider;
App::DocumentObject* _editingObject;
ViewProviderDocumentObject* _editViewProviderParent;
std::string _editSubname;
std::string _editSubElement;
Base::Matrix4D _editingTransform;
View3DInventorViewer* _editingViewer;
std::set<const App::DocumentObject*> _editObjs;
Application* _pcAppWnd;
// the doc/Document
App::Document* _pcDocument;
/// List of all registered views
std::list<Gui::BaseView*> baseViews;
/// List of all registered views
std::list<Gui::BaseView*> passiveViews;
std::map<const App::DocumentObject*,ViewProviderDocumentObject*> _ViewProviderMap;
std::map<SoSeparator *,ViewProviderDocumentObject*> _CoinMap;
std::map<std::string,ViewProvider*> _ViewProviderMapAnnotation;
std::list<ViewProviderDocumentObject*> _redoViewProviders;
typedef boost::signals2::connection Connection;
Connection connectNewObject;
Connection connectDelObject;
Connection connectCngObject;
Connection connectRenObject;
Connection connectActObject;
Connection connectSaveDocument;
Connection connectRestDocument;
Connection connectStartLoadDocument;
Connection connectFinishLoadDocument;
Connection connectShowHidden;
Connection connectFinishRestoreObject;
Connection connectExportObjects;
Connection connectImportObjects;
Connection connectFinishImportObjects;
Connection connectUndoDocument;
Connection connectRedoDocument;
Connection connectRecomputed;
Connection connectSkipRecompute;
Connection connectTransactionAppend;
Connection connectTransactionRemove;
Connection connectTouchedObject;
Connection connectChangePropertyEditor;
typedef boost::signals2::shared_connection_block ConnectionBlock;
ConnectionBlock connectActObjectBlocker;
};
} // namespace Gui
/* TRANSLATOR Gui::Document */
/// @namespace Gui @class Document
int Document::_iDocCount = 0;
Document::Document(App::Document* pcDocument,Application * app)
{
d = new DocumentP;
d->_iWinCount = 1;
// new instance
d->_iDocId = (++_iDocCount);
d->_isClosing = false;
d->_isModified = false;
d->_isTransacting = false;
d->_pcAppWnd = app;
d->_pcDocument = pcDocument;
d->_editViewProvider = 0;
d->_editingObject = 0;
d->_editViewProviderParent = 0;
d->_editingViewer = 0;
d->_editMode = 0;
// Setup the connections
d->connectNewObject = pcDocument->signalNewObject.connect
(boost::bind(&Gui::Document::slotNewObject, this, bp::_1));
d->connectDelObject = pcDocument->signalDeletedObject.connect
(boost::bind(&Gui::Document::slotDeletedObject, this, bp::_1));
d->connectCngObject = pcDocument->signalChangedObject.connect
(boost::bind(&Gui::Document::slotChangedObject, this, bp::_1, bp::_2));
d->connectRenObject = pcDocument->signalRelabelObject.connect
(boost::bind(&Gui::Document::slotRelabelObject, this, bp::_1));
d->connectActObject = pcDocument->signalActivatedObject.connect
(boost::bind(&Gui::Document::slotActivatedObject, this, bp::_1));
d->connectActObjectBlocker = boost::signals2::shared_connection_block
(d->connectActObject, false);
d->connectSaveDocument = pcDocument->signalSaveDocument.connect
(boost::bind(&Gui::Document::Save, this, bp::_1));
d->connectRestDocument = pcDocument->signalRestoreDocument.connect
(boost::bind(&Gui::Document::Restore, this, bp::_1));
d->connectStartLoadDocument = App::GetApplication().signalStartRestoreDocument.connect
(boost::bind(&Gui::Document::slotStartRestoreDocument, this, bp::_1));
d->connectFinishLoadDocument = App::GetApplication().signalFinishRestoreDocument.connect
(boost::bind(&Gui::Document::slotFinishRestoreDocument, this, bp::_1));
d->connectShowHidden = App::GetApplication().signalShowHidden.connect
(boost::bind(&Gui::Document::slotShowHidden, this, bp::_1));
d->connectChangePropertyEditor = pcDocument->signalChangePropertyEditor.connect
(boost::bind(&Gui::Document::slotChangePropertyEditor, this, bp::_1, bp::_2));
d->connectFinishRestoreObject = pcDocument->signalFinishRestoreObject.connect
(boost::bind(&Gui::Document::slotFinishRestoreObject, this, bp::_1));
d->connectExportObjects = pcDocument->signalExportViewObjects.connect
(boost::bind(&Gui::Document::exportObjects, this, bp::_1, bp::_2));
d->connectImportObjects = pcDocument->signalImportViewObjects.connect
(boost::bind(&Gui::Document::importObjects, this, bp::_1, bp::_2, bp::_3));
d->connectFinishImportObjects = pcDocument->signalFinishImportObjects.connect
(boost::bind(&Gui::Document::slotFinishImportObjects, this, bp::_1));
d->connectUndoDocument = pcDocument->signalUndo.connect
(boost::bind(&Gui::Document::slotUndoDocument, this, bp::_1));
d->connectRedoDocument = pcDocument->signalRedo.connect
(boost::bind(&Gui::Document::slotRedoDocument, this, bp::_1));
d->connectRecomputed = pcDocument->signalRecomputed.connect
(boost::bind(&Gui::Document::slotRecomputed, this, bp::_1));
d->connectSkipRecompute = pcDocument->signalSkipRecompute.connect
(boost::bind(&Gui::Document::slotSkipRecompute, this, bp::_1, bp::_2));
d->connectTouchedObject = pcDocument->signalTouchedObject.connect
(boost::bind(&Gui::Document::slotTouchedObject, this, bp::_1));
d->connectTransactionAppend = pcDocument->signalTransactionAppend.connect
(boost::bind(&Gui::Document::slotTransactionAppend, this, bp::_1, bp::_2));
d->connectTransactionRemove = pcDocument->signalTransactionRemove.connect
(boost::bind(&Gui::Document::slotTransactionRemove, this, bp::_1, bp::_2));
// pointer to the python class
// NOTE: As this Python object doesn't get returned to the interpreter we
// mustn't increment it (Werner Jan-12-2006)
_pcDocPy = new Gui::DocumentPy(this);
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Document");
if (hGrp->GetBool("UsingUndo",true)) {
d->_pcDocument->setUndoMode(1);
// set the maximum stack size
d->_pcDocument->setMaxUndoStackSize(hGrp->GetInt("MaxUndoSize",20));
}
d->_changeViewTouchDocument = hGrp->GetBool("ChangeViewProviderTouchDocument", true);
}
Document::~Document()
{
// disconnect everything to avoid to be double-deleted
// in case an exception is raised somewhere
d->connectNewObject.disconnect();
d->connectDelObject.disconnect();
d->connectCngObject.disconnect();
d->connectRenObject.disconnect();
d->connectActObject.disconnect();
d->connectSaveDocument.disconnect();
d->connectRestDocument.disconnect();
d->connectStartLoadDocument.disconnect();
d->connectFinishLoadDocument.disconnect();
d->connectShowHidden.disconnect();
d->connectFinishRestoreObject.disconnect();
d->connectExportObjects.disconnect();
d->connectImportObjects.disconnect();
d->connectFinishImportObjects.disconnect();
d->connectUndoDocument.disconnect();
d->connectRedoDocument.disconnect();
d->connectRecomputed.disconnect();
d->connectSkipRecompute.disconnect();
d->connectTransactionAppend.disconnect();
d->connectTransactionRemove.disconnect();
d->connectTouchedObject.disconnect();
d->connectChangePropertyEditor.disconnect();
// e.g. if document gets closed from within a Python command
d->_isClosing = true;
// calls Document::detachView() and alter the view list
std::list<Gui::BaseView*> temp = d->baseViews;
for(std::list<Gui::BaseView*>::iterator it=temp.begin();it!=temp.end();++it)
(*it)->deleteSelf();
std::map<const App::DocumentObject*,ViewProviderDocumentObject*>::iterator jt;
for (jt = d->_ViewProviderMap.begin();jt != d->_ViewProviderMap.end(); ++jt)
delete jt->second;
std::map<std::string,ViewProvider*>::iterator it2;
for (it2 = d->_ViewProviderMapAnnotation.begin();it2 != d->_ViewProviderMapAnnotation.end(); ++it2)
delete it2->second;
// remove the reference from the object
Base::PyGILStateLocker lock;
_pcDocPy->setInvalid();
_pcDocPy->DecRef();
delete d;
}
//*****************************************************************************************************
// 3D viewer handling
//*****************************************************************************************************
bool Document::setEdit(Gui::ViewProvider* p, int ModNum, const char *subname)
{
ViewProviderDocumentObject* vp = dynamic_cast<ViewProviderDocumentObject*>(p);
if (!vp) {
FC_ERR("cannot edit non ViewProviderDocumentObject");
return false;
}
// Fix regression: https://forum.freecadweb.org/viewtopic.php?f=19&t=43629&p=371972#p371972
// When an object is already in edit mode a subsequent call for editing is only possible
// when resetting the currently edited object.
if (d->_editViewProvider) {
_resetEdit();
}
auto obj = vp->getObject();
if(!obj->getNameInDocument()) {
FC_ERR("cannot edit detached object");
return false;
}
std::string _subname;
if(!subname || !subname[0]) {
// No subname reference is given, we try to extract one from the current
// selection in order to obtain the correct transformation matrix below
auto sels = Gui::Selection().getCompleteSelection(false);
App::DocumentObject *parentObj = 0;
for(auto &sel : sels) {
if(!sel.pObject || !sel.pObject->getNameInDocument())
continue;
if(!parentObj)
parentObj = sel.pObject;
else if(parentObj!=sel.pObject) {
FC_LOG("Cannot deduce subname for editing, more than one parent?");
parentObj = 0;
break;
}
auto sobj = parentObj->getSubObject(sel.SubName);
if(!sobj || (sobj!=obj && sobj->getLinkedObject(true)!= obj)) {
FC_LOG("Cannot deduce subname for editing, subname mismatch");
parentObj = 0;
break;
}
_subname = sel.SubName;
}
if(parentObj) {
FC_LOG("deduced editing reference " << parentObj->getFullName() << '.' << _subname);
subname = _subname.c_str();
obj = parentObj;
vp = dynamic_cast<ViewProviderDocumentObject*>(
Application::Instance->getViewProvider(obj));
if(!vp || !vp->getDocument()) {
FC_ERR("invliad view provider for parent object");
return false;
}
if(vp->getDocument()!=this)
return vp->getDocument()->setEdit(vp,ModNum,subname);
}
}
if (d->_ViewProviderMap.find(obj) == d->_ViewProviderMap.end()) {
// We can actually support editing external object, by calling
// View3DInventViewer::setupEditingRoot() before exiting from
// ViewProvider::setEditViewer(), which transfer all child node of the view
// provider into an editing node inside the viewer of this document. And
// that's may actually be the case, as the subname referenced sub object
// is allowed to be in other documents.
//
// We just disabling editing external parent object here, for bug
// tracking purpose. Because, bringing an unrelated external object to
// the current view for editing will confuse user, and is certainly a
// bug. By right, the top parent object should always belong to the
// editing document, and the actually editing sub object can be
// external.
//
// So, you can either call setEdit() with subname set to 0, which cause
// the code above to auto detect selection context, and dispatch the
// editing call to the correct document. Or, supply subname yourself,
// and make sure you get the document right.
//
FC_ERR("cannot edit object '" << obj->getNameInDocument() << "': not found in document "
<< "'" << getDocument()->getName() << "'");
return false;
}
d->_editingTransform = Base::Matrix4D();
// Geo feature group now handles subname like link group. So no need of the
// following code.
//
// if(!subname || !subname[0]) {
// auto group = App::GeoFeatureGroupExtension::getGroupOfObject(obj);
// if(group) {
// auto ext = group->getExtensionByType<App::GeoFeatureGroupExtension>();
// d->_editingTransform = ext->globalGroupPlacement().toMatrix();
// }
// }
auto sobj = obj->getSubObject(subname,0,&d->_editingTransform);
if(!sobj || !sobj->getNameInDocument()) {
FC_ERR("Invalid sub object '" << obj->getFullName()
<< '.' << (subname?subname:"") << "'");
return false;
}
auto svp = vp;
if(sobj!=obj) {
svp = dynamic_cast<ViewProviderDocumentObject*>(
Application::Instance->getViewProvider(sobj));
if(!svp) {
FC_ERR("Cannot edit '" << sobj->getFullName() << "' without view provider");
return false;
}
}
View3DInventor *view3d = dynamic_cast<View3DInventor *>(getActiveView());
// if the currently active view is not the 3d view search for it and activate it
if (view3d)
getMainWindow()->setActiveWindow(view3d);
else
view3d = dynamic_cast<View3DInventor *>(setActiveView(vp));
Application::Instance->setEditDocument(this);
d->_editViewProviderParent = vp;
d->_editSubElement.clear();
d->_editSubname.clear();
if(subname) {
const char *element = Data::ComplexGeoData::findElementName(subname);
if(element) {
d->_editSubname = std::string(subname,element-subname);
d->_editSubElement = element;
}else
d->_editSubname = subname;
}
auto sobjs = obj->getSubObjectList(subname);
d->_editObjs.clear();
d->_editObjs.insert(sobjs.begin(),sobjs.end());
d->_editingObject = sobj;
d->_editMode = ModNum;
d->_editViewProvider = svp->startEditing(ModNum);
if(!d->_editViewProvider) {
d->_editViewProviderParent = 0;
d->_editObjs.clear();
d->_editingObject = 0;
FC_LOG("object '" << sobj->getFullName() << "' refuse to edit");
return false;
}
if(view3d) {
view3d->getViewer()->setEditingViewProvider(d->_editViewProvider,ModNum);
d->_editingViewer = view3d->getViewer();
}
Gui::TaskView::TaskDialog* dlg = Gui::Control().activeDialog();
if (dlg)
dlg->setDocumentName(this->getDocument()->getName());
if (d->_editViewProvider->isDerivedFrom(ViewProviderDocumentObject::getClassTypeId())) {
auto vpd = static_cast<ViewProviderDocumentObject*>(d->_editViewProvider);
vpd->getDocument()->signalInEdit(*vpd);
}
App::AutoTransaction::setEnable(false);
return true;
}
const Base::Matrix4D &Document::getEditingTransform() const {
return d->_editingTransform;
}
void Document::setEditingTransform(const Base::Matrix4D &mat) {
d->_editObjs.clear();
d->_editingTransform = mat;
View3DInventor *activeView = dynamic_cast<View3DInventor *>(getActiveView());
if (activeView)
activeView->getViewer()->setEditingTransform(mat);
}
void Document::resetEdit(void) {
Application::Instance->setEditDocument(0);
}
void Document::_resetEdit(void)
{
std::list<Gui::BaseView*>::iterator it;
if (d->_editViewProvider) {
for (it = d->baseViews.begin();it != d->baseViews.end();++it) {
View3DInventor *activeView = dynamic_cast<View3DInventor *>(*it);
if (activeView)
activeView->getViewer()->resetEditingViewProvider();
}
d->_editViewProvider->finishEditing();
// Have to check d->_editViewProvider below, because there is a chance
// the editing object gets deleted inside the above call to
// 'finishEditing()', which will trigger our slotDeletedObject(), which
// nullifies _editViewProvider.
if (d->_editViewProvider && d->_editViewProvider->isDerivedFrom(ViewProviderDocumentObject::getClassTypeId())) {
auto vpd = static_cast<ViewProviderDocumentObject*>(d->_editViewProvider);
vpd->getDocument()->signalResetEdit(*vpd);
}
d->_editViewProvider = 0;
// The logic below is not necessary anymore, because this method is
// changed into a private one, _resetEdit(). And the exposed
// resetEdit() above calls into Application->setEditDocument(0) which
// will prevent recursive calling.
#if 0
// Nullify the member variable before calling finishEditing().
// This is to avoid a possible stack overflow when a view provider wrongly
// invokes the document's resetEdit() method.
ViewProvider* editViewProvider = d->_editViewProvider;
d->_editViewProvider = nullptr;
editViewProvider->finishEditing();
if (editViewProvider->isDerivedFrom(ViewProviderDocumentObject::getClassTypeId()))
signalResetEdit(*(static_cast<ViewProviderDocumentObject*>(editViewProvider)));
#endif
App::GetApplication().closeActiveTransaction();
}
d->_editViewProviderParent = 0;
d->_editingViewer = 0;
d->_editObjs.clear();
d->_editingObject = 0;
if(Application::Instance->editDocument() == this)
Application::Instance->setEditDocument(0);
}
ViewProvider *Document::getInEdit(ViewProviderDocumentObject **parentVp,
std::string *subname, int *mode, std::string *subelement) const
{
if(parentVp) *parentVp = d->_editViewProviderParent;
if(subname) *subname = d->_editSubname;
if(subelement) *subelement = d->_editSubElement;
if(mode) *mode = d->_editMode;
if (d->_editViewProvider) {
// there is only one 3d view which is in edit mode
View3DInventor *activeView = dynamic_cast<View3DInventor *>(getActiveView());
if (activeView && activeView->getViewer()->isEditingViewProvider())
return d->_editViewProvider;
}
return 0;
}
void Document::setInEdit(ViewProviderDocumentObject *parentVp, const char *subname) {
if (d->_editViewProvider) {
d->_editViewProviderParent = parentVp;
d->_editSubname = subname?subname:"";
}
}
void Document::setAnnotationViewProvider(const char* name, ViewProvider *pcProvider)
{
std::list<Gui::BaseView*>::iterator vIt;
// already in ?
std::map<std::string,ViewProvider*>::iterator it = d->_ViewProviderMapAnnotation.find(name);
if (it != d->_ViewProviderMapAnnotation.end())
removeAnnotationViewProvider(name);
// add
d->_ViewProviderMapAnnotation[name] = pcProvider;
// cycling to all views of the document
for (vIt = d->baseViews.begin();vIt != d->baseViews.end();++vIt) {
View3DInventor *activeView = dynamic_cast<View3DInventor *>(*vIt);
if (activeView)
activeView->getViewer()->addViewProvider(pcProvider);
}
}
ViewProvider * Document::getAnnotationViewProvider(const char* name) const
{
std::map<std::string,ViewProvider*>::const_iterator it = d->_ViewProviderMapAnnotation.find(name);
return ( (it != d->_ViewProviderMapAnnotation.end()) ? it->second : 0 );
}
void Document::removeAnnotationViewProvider(const char* name)
{
std::map<std::string,ViewProvider*>::iterator it = d->_ViewProviderMapAnnotation.find(name);
std::list<Gui::BaseView*>::iterator vIt;
// cycling to all views of the document
for (vIt = d->baseViews.begin();vIt != d->baseViews.end();++vIt) {
View3DInventor *activeView = dynamic_cast<View3DInventor *>(*vIt);
if (activeView)
activeView->getViewer()->removeViewProvider(it->second);
}
delete it->second;
d->_ViewProviderMapAnnotation.erase(it);
}
ViewProvider* Document::getViewProvider(const App::DocumentObject* Feat) const
{
std::map<const App::DocumentObject*,ViewProviderDocumentObject*>::const_iterator
it = d->_ViewProviderMap.find( Feat );
return ( (it != d->_ViewProviderMap.end()) ? it->second : 0 );
}
std::vector<ViewProvider*> Document::getViewProvidersOfType(const Base::Type& typeId) const
{
std::vector<ViewProvider*> Objects;
for (std::map<const App::DocumentObject*,ViewProviderDocumentObject*>::const_iterator it =
d->_ViewProviderMap.begin(); it != d->_ViewProviderMap.end(); ++it ) {
if (it->second->getTypeId().isDerivedFrom(typeId))
Objects.push_back(it->second);
}
return Objects;
}
ViewProvider *Document::getViewProviderByName(const char* name) const
{
// first check on feature name
App::DocumentObject *pcFeat = getDocument()->getObject(name);
if (pcFeat)
{
std::map<const App::DocumentObject*,ViewProviderDocumentObject*>::const_iterator
it = d->_ViewProviderMap.find( pcFeat );
if (it != d->_ViewProviderMap.end())
return it->second;
} else {
// then try annotation name
std::map<std::string,ViewProvider*>::const_iterator it2 = d->_ViewProviderMapAnnotation.find( name );
if (it2 != d->_ViewProviderMapAnnotation.end())
return it2->second;
}
return 0;
}
bool Document::isShow(const char* name)
{
ViewProvider* pcProv = getViewProviderByName(name);
return pcProv ? pcProv->isShow() : false;
}
/// put the feature in show
void Document::setShow(const char* name)
{
ViewProvider* pcProv = getViewProviderByName(name);
if (pcProv && pcProv->getTypeId().isDerivedFrom(ViewProviderDocumentObject::getClassTypeId())) {
((ViewProviderDocumentObject*)pcProv)->Visibility.setValue(true);
}
}
/// set the feature in Noshow
void Document::setHide(const char* name)
{
ViewProvider* pcProv = getViewProviderByName(name);
if (pcProv && pcProv->getTypeId().isDerivedFrom(ViewProviderDocumentObject::getClassTypeId())) {
((ViewProviderDocumentObject*)pcProv)->Visibility.setValue(false);
}
}
/// set the feature in Noshow
void Document::setPos(const char* name, const Base::Matrix4D& rclMtrx)
{
ViewProvider* pcProv = getViewProviderByName(name);
if (pcProv)
pcProv->setTransformation(rclMtrx);
}
//*****************************************************************************************************
// Document
//*****************************************************************************************************
void Document::slotNewObject(const App::DocumentObject& Obj)
{
ViewProviderDocumentObject* pcProvider = static_cast<ViewProviderDocumentObject*>(getViewProvider(&Obj));
if (!pcProvider) {
//Base::Console().Log("Document::slotNewObject() called\n");
std::string cName = Obj.getViewProviderNameStored();
for(;;) {
if (cName.empty()) {
// handle document object with no view provider specified
FC_LOG(Obj.getFullName() << " has no view provider specified");
return;
}
Base::BaseClass* base = static_cast<Base::BaseClass*>(
Base::Type::createInstanceByName(cName.c_str(),true));
pcProvider = Base::freecad_dynamic_cast<ViewProviderDocumentObject>(base);
if (!pcProvider) {
// type not derived from ViewProviderDocumentObject!!!
FC_ERR("Invalid view provider type '" << cName << "' for " << Obj.getFullName());
delete base;
return;
} else if (cName!=Obj.getViewProviderName() && !pcProvider->allowOverride(Obj)) {
FC_WARN("View provider type '" << cName << "' does not support " << Obj.getFullName());
delete base;
pcProvider = 0;
cName = Obj.getViewProviderName();
} else
break;
}
setModified(true);
d->_ViewProviderMap[&Obj] = pcProvider;
d->_CoinMap[pcProvider->getRoot()] = pcProvider;
pcProvider->setStatus(Gui::ViewStatus::TouchDocument, d->_changeViewTouchDocument);
try {
// if successfully created set the right name and calculate the view
//FIXME: Consider to change argument of attach() to const pointer
pcProvider->attach(const_cast<App::DocumentObject*>(&Obj));
pcProvider->updateView();
pcProvider->setActiveMode();
}
catch(const Base::MemoryException& e){
FC_ERR("Memory exception in " << Obj.getFullName() << " thrown: " << e.what());
}
catch(Base::Exception &e){
e.ReportException();
}
#ifndef FC_DEBUG
catch(...){
FC_ERR("Unknown exception in Feature " << Obj.getFullName() << " thrown");
}
#endif
}else{
try {
pcProvider->reattach(const_cast<App::DocumentObject*>(&Obj));
} catch(Base::Exception &e){
e.ReportException();
}
}
if (pcProvider) {
std::list<Gui::BaseView*>::iterator vIt;
// cycling to all views of the document
for (vIt = d->baseViews.begin();vIt != d->baseViews.end();++vIt) {
View3DInventor *activeView = dynamic_cast<View3DInventor *>(*vIt);
if (activeView)
activeView->getViewer()->addViewProvider(pcProvider);
}
// adding to the tree
signalNewObject(*pcProvider);
pcProvider->pcDocument = this;
// it is possible that a new viewprovider already claims children
handleChildren3D(pcProvider);
if (d->_isTransacting) {
d->_redoViewProviders.push_back(pcProvider);
}
}
}
void Document::slotDeletedObject(const App::DocumentObject& Obj)
{
std::list<Gui::BaseView*>::iterator vIt;
setModified(true);
//Base::Console().Log("Document::slotDeleteObject() called\n");
// cycling to all views of the document
ViewProvider* viewProvider = getViewProvider(&Obj);
if(!viewProvider) return;
if (d->_editViewProvider==viewProvider || d->_editViewProviderParent==viewProvider)
_resetEdit();
else if(Application::Instance->editDocument()) {
auto editDoc = Application::Instance->editDocument();
if(editDoc->d->_editViewProvider==viewProvider ||
editDoc->d->_editViewProviderParent==viewProvider)
Application::Instance->setEditDocument(0);
}
handleChildren3D(viewProvider,true);
#if 0 // With this we can show child objects again if this method was called by undo
viewProvider->onDelete(std::vector<std::string>());
#endif
if (viewProvider && viewProvider->getTypeId().isDerivedFrom
(ViewProviderDocumentObject::getClassTypeId())) {
// go through the views
for (vIt = d->baseViews.begin();vIt != d->baseViews.end();++vIt) {
View3DInventor *activeView = dynamic_cast<View3DInventor *>(*vIt);
if (activeView)
activeView->getViewer()->removeViewProvider(viewProvider);
}
// removing from tree
signalDeletedObject(*(static_cast<ViewProviderDocumentObject*>(viewProvider)));
}
viewProvider->beforeDelete();
}
void Document::beforeDelete() {
auto editDoc = Application::Instance->editDocument();
if(editDoc) {
auto vp = dynamic_cast<ViewProviderDocumentObject*>(editDoc->d->_editViewProvider);
auto vpp = dynamic_cast<ViewProviderDocumentObject*>(editDoc->d->_editViewProviderParent);
if(editDoc == this ||
(vp && vp->getDocument()==this) ||
(vpp && vpp->getDocument()==this))
{
Application::Instance->setEditDocument(0);
}
}
for(auto &v : d->_ViewProviderMap)
v.second->beforeDelete();
}
void Document::slotChangedObject(const App::DocumentObject& Obj, const App::Property& Prop)
{
//Base::Console().Log("Document::slotChangedObject() called\n");
ViewProvider* viewProvider = getViewProvider(&Obj);
if (viewProvider) {
try {
viewProvider->update(&Prop);
if(d->_editingViewer
&& d->_editingObject
&& d->_editViewProviderParent
&& (Prop.isDerivedFrom(App::PropertyPlacement::getClassTypeId())
// Issue ID 0004230 : getName() can return null in which case strstr() crashes
|| (Prop.getName() && strstr(Prop.getName(),"Scale")))
&& d->_editObjs.count(&Obj))
{
Base::Matrix4D mat;
auto sobj = d->_editViewProviderParent->getObject()->getSubObject(
d->_editSubname.c_str(),0,&mat);
if(sobj == d->_editingObject && d->_editingTransform!=mat) {
d->_editingTransform = mat;
d->_editingViewer->setEditingTransform(d->_editingTransform);
}
}
}
catch(const Base::MemoryException& e) {
FC_ERR("Memory exception in " << Obj.getFullName() << " thrown: " << e.what());
}
catch(Base::Exception& e){
e.ReportException();
}
catch(const std::exception& e){
FC_ERR("C++ exception in " << Obj.getFullName() << " thrown " << e.what());
}
catch (...) {
FC_ERR("Cannot update representation for " << Obj.getFullName());
}
handleChildren3D(viewProvider);
if (viewProvider->isDerivedFrom(ViewProviderDocumentObject::getClassTypeId()))
signalChangedObject(static_cast<ViewProviderDocumentObject&>(*viewProvider), Prop);
}
// a property of an object has changed
if(!Prop.testStatus(App::Property::NoModify) && !isModified()) {
FC_LOG(Prop.getFullName() << " modified");
setModified(true);
}
getMainWindow()->updateActions(true);
}
void Document::slotRelabelObject(const App::DocumentObject& Obj)
{
ViewProvider* viewProvider = getViewProvider(&Obj);
if (viewProvider && viewProvider->isDerivedFrom(ViewProviderDocumentObject::getClassTypeId())) {
signalRelabelObject(*(static_cast<ViewProviderDocumentObject*>(viewProvider)));
}
}
void Document::slotTransactionAppend(const App::DocumentObject& obj, App::Transaction* transaction)
{
ViewProvider* viewProvider = getViewProvider(&obj);
if (viewProvider && viewProvider->isDerivedFrom(ViewProviderDocumentObject::getClassTypeId())) {
transaction->addObjectDel(viewProvider);
}
}
void Document::slotTransactionRemove(const App::DocumentObject& obj, App::Transaction* transaction)
{
std::map<const App::DocumentObject*,ViewProviderDocumentObject*>::const_iterator
it = d->_ViewProviderMap.find(&obj);
if (it != d->_ViewProviderMap.end()) {
ViewProvider* viewProvider = it->second;
auto itC = d->_CoinMap.find(viewProvider->getRoot());
if(itC != d->_CoinMap.end())
d->_CoinMap.erase(itC);
d->_ViewProviderMap.erase(&obj);
// transaction being a nullptr indicates that undo/redo is off and the object
// can be safely deleted
if (transaction)
transaction->addObjectNew(viewProvider);
else
delete viewProvider;
}
}
void Document::slotActivatedObject(const App::DocumentObject& Obj)
{
ViewProvider* viewProvider = getViewProvider(&Obj);
if (viewProvider && viewProvider->isDerivedFrom(ViewProviderDocumentObject::getClassTypeId())) {
signalActivatedObject(*(static_cast<ViewProviderDocumentObject*>(viewProvider)));
}
}
void Document::slotUndoDocument(const App::Document& doc)
{
if (d->_pcDocument != &doc)
return;
signalUndoDocument(*this);
getMainWindow()->updateActions();
}
void Document::slotRedoDocument(const App::Document& doc)
{
if (d->_pcDocument != &doc)
return;
signalRedoDocument(*this);
getMainWindow()->updateActions();
}
void Document::slotRecomputed(const App::Document& doc)
{
if (d->_pcDocument != &doc)
return;
getMainWindow()->updateActions();
TreeWidget::updateStatus();
}
// This function is called when some asks to recompute a document that is marked
// as 'SkipRecompute'. We'll check if we are the current document, and if either
// not given an explicit recomputing object list, or the given single object is
// the eidting object or the active object. If the conditions are met, we'll
// force recompute only that object and all its dependent objects.
void Document::slotSkipRecompute(const App::Document& doc, const std::vector<App::DocumentObject*> &objs)
{
if (d->_pcDocument != &doc)
return;
if(objs.size()>1 ||
App::GetApplication().getActiveDocument()!=&doc ||
!doc.testStatus(App::Document::AllowPartialRecompute))
return;
App::DocumentObject *obj = 0;
auto editDoc = Application::Instance->editDocument();
if(editDoc) {
auto vp = dynamic_cast<ViewProviderDocumentObject*>(editDoc->getInEdit());
if(vp)
obj = vp->getObject();
}
if(!obj)
obj = doc.getActiveObject();
if(!obj || !obj->getNameInDocument() || (objs.size() && objs.front()!=obj))
return;
obj->recomputeFeature(true);
}
void Document::slotTouchedObject(const App::DocumentObject &Obj)
{
getMainWindow()->updateActions(true);
if(!isModified()) {
FC_LOG(Obj.getFullName() << " touched");
setModified(true);
}
}
void Document::addViewProvider(Gui::ViewProviderDocumentObject* vp)
{
// Hint: The undo/redo first adds the view provider to the Gui
// document before adding the objects to the App document.
// the view provider is added by TransactionViewProvider and an
// object can be there only once
assert(d->_ViewProviderMap.find(vp->getObject()) == d->_ViewProviderMap.end());
vp->setStatus(Detach, false);
d->_ViewProviderMap[vp->getObject()] = vp;
d->_CoinMap[vp->getRoot()] = vp;
}
void Document::setModified(bool b)
{
if(d->_isModified == b)
return;
d->_isModified = b;
std::list<MDIView*> mdis = getMDIViews();
for (std::list<MDIView*>::iterator it = mdis.begin(); it != mdis.end(); ++it) {
(*it)->setWindowModified(b);
}
}
bool Document::isModified() const
{
return d->_isModified;
}
ViewProviderDocumentObject* Document::getViewProviderByPathFromTail(SoPath * path) const
{
// Get the lowest root node in the pick path!
for (int i = 0; i < path->getLength(); i++) {
SoNode *node = path->getNodeFromTail(i);
if (node->isOfType(SoSeparator::getClassTypeId())) {
auto it = d->_CoinMap.find(static_cast<SoSeparator*>(node));
if(it!=d->_CoinMap.end())
return it->second;
}
}
return 0;
}
ViewProviderDocumentObject* Document::getViewProviderByPathFromHead(SoPath * path) const