/
Document.cpp
1478 lines (1279 loc) · 55 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 <QAbstractButton>
# include <qapplication.h>
# include <qdir.h>
# include <qfileinfo.h>
# include <QGLWidget>
# include <QKeySequence>
# include <qmessagebox.h>
# include <qstatusbar.h>
# include <boost/signals.hpp>
# include <boost/bind.hpp>
# include <Inventor/actions/SoSearchAction.h>
# include <Inventor/nodes/SoSeparator.h>
#endif
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Matrix.h>
#include <Base/Reader.h>
#include <Base/Writer.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <App/DocumentObjectGroup.h>
#include <App/Transactions.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 "Selection.h"
#include "WaitCursor.h"
#include "Thumbnail.h"
using namespace Gui;
namespace Gui {
// Pimpl class
struct DocumentP
{
Thumbnail thumb;
int _iWinCount;
int _iDocId;
bool _isClosing;
bool _isModified;
ViewProvider* _editViewProvider;
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<std::string,ViewProvider*> _ViewProviderMapAnnotation;
typedef boost::signals::connection Connection;
Connection connectNewObject;
Connection connectDelObject;
Connection connectCngObject;
Connection connectRenObject;
Connection connectActObject;
Connection connectSaveDocument;
Connection connectRestDocument;
Connection connectStartLoadDocument;
Connection connectFinishLoadDocument;
Connection connectExportObjects;
Connection connectImportObjects;
Connection connectUndoDocument;
Connection connectRedoDocument;
Connection connectTransactionAppend;
Connection connectTransactionRemove;
};
} // 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->_pcAppWnd = app;
d->_pcDocument = pcDocument;
d->_editViewProvider = 0;
// Setup the connections
d->connectNewObject = pcDocument->signalNewObject.connect
(boost::bind(&Gui::Document::slotNewObject, this, _1));
d->connectDelObject = pcDocument->signalDeletedObject.connect
(boost::bind(&Gui::Document::slotDeletedObject, this, _1));
d->connectCngObject = pcDocument->signalChangedObject.connect
(boost::bind(&Gui::Document::slotChangedObject, this, _1, _2));
d->connectRenObject = pcDocument->signalRelabelObject.connect
(boost::bind(&Gui::Document::slotRelabelObject, this, _1));
d->connectActObject = pcDocument->signalActivatedObject.connect
(boost::bind(&Gui::Document::slotActivatedObject, this, _1));
d->connectSaveDocument = pcDocument->signalSaveDocument.connect
(boost::bind(&Gui::Document::Save, this, _1));
d->connectRestDocument = pcDocument->signalRestoreDocument.connect
(boost::bind(&Gui::Document::Restore, this, _1));
d->connectStartLoadDocument = App::GetApplication().signalStartRestoreDocument.connect
(boost::bind(&Gui::Document::slotStartRestoreDocument, this, _1));
d->connectFinishLoadDocument = App::GetApplication().signalFinishRestoreDocument.connect
(boost::bind(&Gui::Document::slotFinishRestoreDocument, this, _1));
d->connectExportObjects = pcDocument->signalExportViewObjects.connect
(boost::bind(&Gui::Document::exportObjects, this, _1, _2));
d->connectImportObjects = pcDocument->signalImportViewObjects.connect
(boost::bind(&Gui::Document::importObjects, this, _1, _2, _3));
d->connectUndoDocument = pcDocument->signalUndo.connect
(boost::bind(&Gui::Document::slotUndoDocument, this, _1));
d->connectRedoDocument = pcDocument->signalRedo.connect
(boost::bind(&Gui::Document::slotRedoDocument, this, _1));
d->connectTransactionAppend = pcDocument->signalTransactionAppend.connect
(boost::bind(&Gui::Document::slotTransactionAppend, this, _1, _2));
d->connectTransactionRemove = pcDocument->signalTransactionRemove.connect
(boost::bind(&Gui::Document::slotTransactionRemove, this, _1, _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);
if (App::GetApplication().GetParameterGroupByPath
("User parameter:BaseApp/Preferences/Document")->GetBool("UsingUndo",true)){
d->_pcDocument->setUndoMode(1);
// set the maximum stack size
d->_pcDocument->setMaxUndoStackSize(App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Document")->GetInt("MaxUndoSize",20));
}
}
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->connectExportObjects.disconnect();
d->connectImportObjects.disconnect();
d->connectUndoDocument.disconnect();
d->connectRedoDocument.disconnect();
d->connectTransactionAppend.disconnect();
d->connectTransactionRemove.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
_pcDocPy->setInvalid();
_pcDocPy->DecRef();
delete d;
}
//*****************************************************************************************************
// 3D viewer handling
//*****************************************************************************************************
bool Document::setEdit(Gui::ViewProvider* p, int ModNum)
{
if (d->_editViewProvider)
resetEdit();
// is it really a ViewProvider of this document?
ViewProviderDocumentObject* vp = dynamic_cast<ViewProviderDocumentObject*>(p);
if (!vp)
return false;
if (d->_ViewProviderMap.find(vp->getObject()) == d->_ViewProviderMap.end())
return false;
View3DInventor *activeView = dynamic_cast<View3DInventor *>(getActiveView());
// if the currently active view is not the 3d view search for it and activate it
if (!activeView) {
activeView = dynamic_cast<View3DInventor *>(getViewOfViewProvider(p));
if (activeView)
getMainWindow()->setActiveWindow(activeView);
}
if (activeView && activeView->getViewer()->setEditingViewProvider(p,ModNum)) {
d->_editViewProvider = p;
Gui::TaskView::TaskDialog* dlg = Gui::Control().activeDialog();
if (dlg)
dlg->setDocumentName(this->getDocument()->getName());
if (d->_editViewProvider->isDerivedFrom(ViewProviderDocumentObject::getClassTypeId()))
signalInEdit(*(static_cast<ViewProviderDocumentObject*>(d->_editViewProvider)));
}
else {
return false;
}
return true;
}
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();
if (d->_editViewProvider->isDerivedFrom(ViewProviderDocumentObject::getClassTypeId()))
signalResetEdit(*(static_cast<ViewProviderDocumentObject*>(d->_editViewProvider)));
d->_editViewProvider = 0;
}
}
ViewProvider *Document::getInEdit(void) const
{
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::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.getViewProviderName();
if (cName.empty()) {
// handle document object with no view provider specified
Base::Console().Log("%s has no view provider specified\n", Obj.getTypeId().getName());
return;
}
setModified(true);
Base::BaseClass* base = static_cast<Base::BaseClass*>(Base::Type::createInstanceByName(cName.c_str(),true));
if (base) {
// type not derived from ViewProviderDocumentObject!!!
assert(base->getTypeId().isDerivedFrom(Gui::ViewProviderDocumentObject::getClassTypeId()));
pcProvider = static_cast<ViewProviderDocumentObject*>(base);
d->_ViewProviderMap[&Obj] = pcProvider;
try {
// if succesfully 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){
Base::Console().Error("Memory exception in '%s' thrown: %s\n",Obj.getNameInDocument(),e.what());
}
catch(Base::Exception &e){
e.ReportException();
}
#ifndef FC_DEBUG
catch(...){
Base::Console().Error("App::Document::_RecomputeFeature(): Unknown exception in Feature \"%s\" thrown\n",Obj.getNameInDocument());
}
#endif
}
else {
Base::Console().Warning("Gui::Document::slotNewObject() no view provider for the object %s found\n",cName.c_str());
}
}
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);
// it is possible that a new viewprovider already claims children
handleChildren3D(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 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) {
if (d->_editViewProvider == viewProvider)
resetEdit();
activeView->getViewer()->removeViewProvider(viewProvider);
}
}
// removing from tree
signalDeletedObject(*(static_cast<ViewProviderDocumentObject*>(viewProvider)));
}
}
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);
}
catch(const Base::MemoryException& e) {
Base::Console().Error("Memory exception in '%s' thrown: %s\n",Obj.getNameInDocument(),e.what());
}
catch(Base::Exception& e){
e.ReportException();
}
catch(const std::exception& e){
Base::Console().Error("C++ exception in '%s' thrown: %s\n",Obj.getNameInDocument(),e.what());
}
catch (...) {
Base::Console().Error("Cannot update representation for '%s'.\n", Obj.getNameInDocument());
}
handleChildren3D(viewProvider);
if (viewProvider->isDerivedFrom(ViewProviderDocumentObject::getClassTypeId()))
signalChangedObject(static_cast<ViewProviderDocumentObject&>(*viewProvider), Prop);
}
// a property of an object has changed
setModified(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;
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);
}
void Document::slotRedoDocument(const App::Document& doc)
{
if (d->_pcDocument != &doc)
return;
signalRedoDocument(*this);
}
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;
}
void Document::setModified(bool b)
{
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;
}
ViewProvider* Document::getViewProviderByPathFromTail(SoPath * path) const
{
// Make sure I'm the lowest LocHL in the pick path!
for (int i = 0; i < path->getLength(); i++) {
SoNode *node = path->getNodeFromTail(i);
if (node->isOfType(SoSeparator::getClassTypeId())) {
std::map<const App::DocumentObject*,ViewProviderDocumentObject*>::const_iterator it = d->_ViewProviderMap.begin();
for(;it!= d->_ViewProviderMap.end();++it)
if (node == it->second->getRoot())
return it->second;
}
}
return 0;
}
App::Document* Document::getDocument(void) const
{
return d->_pcDocument;
}
/// Save the document
bool Document::save(void)
{
if (d->_pcDocument->isSaved()) {
try {
Gui::WaitCursor wc;
Command::doCommand(Command::Doc,"App.getDocument(\"%s\").save()"
,d->_pcDocument->getName());
setModified(false);
}
catch (const Base::Exception& e) {
QMessageBox::critical(getMainWindow(), QObject::tr("Saving document failed"),
QString::fromLatin1(e.what()));
}
return true;
}
else {
return saveAs();
}
}
/// Save the document under a new file name
bool Document::saveAs(void)
{
getMainWindow()->showMessage(QObject::tr("Save document under new filename..."));
QString exe = qApp->applicationName();
QString fn = FileDialog::getSaveFileName(getMainWindow(), QObject::tr("Save %1 Document").arg(exe),
QString(), QString::fromLatin1("%1 %2 (*.FCStd)").arg(exe).arg(QObject::tr("Document")));
if (!fn.isEmpty()) {
QFileInfo fi;
fi.setFile(fn);
const char * DocName = App::GetApplication().getDocumentName(getDocument());
// save as new file name
try {
Gui::WaitCursor wc;
Command::doCommand(Command::Doc,"App.getDocument(\"%s\").saveAs(\"%s\")"
, DocName, (const char*)fn.toUtf8());
setModified(false);
getMainWindow()->appendRecentFile(fi.filePath());
}
catch (const Base::Exception& e) {
QMessageBox::critical(getMainWindow(), QObject::tr("Saving document failed"),
QString::fromLatin1(e.what()));
}
return true;
}
else {
getMainWindow()->showMessage(QObject::tr("Saving aborted"), 2000);
return false;
}
}
/// Save a copy of the document under a new file name
bool Document::saveCopy(void)
{
getMainWindow()->showMessage(QObject::tr("Save a copy of the document under new filename..."));
QString exe = qApp->applicationName();
QString fn = FileDialog::getSaveFileName(getMainWindow(), QObject::tr("Save %1 Document").arg(exe),
QString(), QObject::tr("%1 document (*.FCStd)").arg(exe));
if (!fn.isEmpty()) {
QFileInfo fi;
fi.setFile(fn);
const char * DocName = App::GetApplication().getDocumentName(getDocument());
// save as new file name
Gui::WaitCursor wc;
Command::doCommand(Command::Doc,"App.getDocument(\"%s\").saveCopy(\"%s\")"
, DocName, (const char*)fn.toUtf8());
return true;
}
else {
getMainWindow()->showMessage(QObject::tr("Saving aborted"), 2000);
return false;
}
}
unsigned int Document::getMemSize (void) const
{
unsigned int size = 0;
// size of the view providers in the document
std::map<const App::DocumentObject*,ViewProviderDocumentObject*>::const_iterator it;
for (it = d->_ViewProviderMap.begin(); it != d->_ViewProviderMap.end(); ++it)
size += it->second->getMemSize();
return size;
}
/**
* Adds a separate XML file to the projects file that contains information about the view providers.
*/
void Document::Save (Base::Writer &writer) const
{
// It's only possible to add extra information if force of XML is disabled
if (writer.isForceXML() == false) {
writer.addFile("GuiDocument.xml", this);
if (App::GetApplication().GetParameterGroupByPath
("User parameter:BaseApp/Preferences/Document")->GetBool("SaveThumbnail",false)) {
std::list<MDIView*> mdi = getMDIViews();
for (std::list<MDIView*>::iterator it = mdi.begin(); it != mdi.end(); ++it) {
if ((*it)->getTypeId().isDerivedFrom(View3DInventor::getClassTypeId())) {
View3DInventorViewer* view = static_cast<View3DInventor*>(*it)->getViewer();
d->thumb.setFileName(d->_pcDocument->FileName.getValue());
d->thumb.setSize(128);
d->thumb.setViewer(view);
d->thumb.Save(writer);
break;
}
}
}
}
}
/**
* Loads a separate XML file from the projects file with information about the view providers.
*/
void Document::Restore(Base::XMLReader &reader)
{
reader.addFile("GuiDocument.xml",this);
// hide all elements to avoid to update the 3d view when loading data files
// RestoreDocFile then restores the visibility status again
std::map<const App::DocumentObject*,ViewProviderDocumentObject*>::iterator it;
for (it = d->_ViewProviderMap.begin(); it != d->_ViewProviderMap.end(); ++it) {
it->second->startRestoring();
}
}
/**
* Restores the properties of the view providers.
*/
void Document::RestoreDocFile(Base::Reader &reader)
{
// We must create an XML parser to read from the input stream
Base::XMLReader xmlReader("GuiDocument.xml", reader);
xmlReader.FileVersion = reader.getFileVersion();
int i,Cnt;
xmlReader.readElement("Document");
long scheme = xmlReader.getAttributeAsInteger("SchemaVersion");
// At this stage all the document objects and their associated view providers exist.
// Now we must restore the properties of the view providers only.
//
// SchemeVersion "1"
if (scheme == 1) {
// read the viewproviders itself
xmlReader.readElement("ViewProviderData");
Cnt = xmlReader.getAttributeAsInteger("Count");
for (i=0 ;i<Cnt ;i++) {
xmlReader.readElement("ViewProvider");
std::string name = xmlReader.getAttribute("name");
bool expanded = false;
if (xmlReader.hasAttribute("expanded")) {
const char* attr = xmlReader.getAttribute("expanded");
if (strcmp(attr,"1") == 0) {
expanded = true;
}
}
ViewProvider* pObj = getViewProviderByName(name.c_str());
if (pObj) // check if this feature has been registered
pObj->Restore(xmlReader);
if (pObj && expanded) {
Gui::ViewProviderDocumentObject* vp = static_cast<Gui::ViewProviderDocumentObject*>(pObj);
this->signalExpandObject(*vp, Gui::Expand);
}
xmlReader.readEndElement("ViewProvider");
}
xmlReader.readEndElement("ViewProviderData");
// read camera settings
xmlReader.readElement("Camera");
const char* ppReturn = xmlReader.getAttribute("settings");
std::string sMsg = "SetCamera ";
sMsg += ppReturn;
if (strcmp(ppReturn, "") != 0) { // non-empty attribute
try {
const char** pReturnIgnore=0;
std::list<MDIView*> mdi = getMDIViews();
for (std::list<MDIView*>::iterator it = mdi.begin(); it != mdi.end(); ++it) {
if ((*it)->onHasMsg("SetCamera"))
(*it)->onMsg(sMsg.c_str(), pReturnIgnore);
}
}
catch (const Base::Exception& e) {
Base::Console().Error("%s\n", e.what());
}
}
}
xmlReader.readEndElement("Document");
// In the file GuiDocument.xml new data files might be added
if (!xmlReader.getFilenames().empty())
xmlReader.readFiles(static_cast<zipios::ZipInputStream&>(reader.getStream()));
// reset modified flag
setModified(false);
}
void Document::slotStartRestoreDocument(const App::Document& doc)
{
if (d->_pcDocument != &doc)
return;
// disable this signal while loading a document
d->connectActObject.block();
}
void Document::slotFinishRestoreDocument(const App::Document& doc)
{
if (d->_pcDocument != &doc)
return;
d->connectActObject.unblock();
App::DocumentObject* act = doc.getActiveObject();
if (act) {
ViewProvider* viewProvider = getViewProvider(act);
if (viewProvider && viewProvider->isDerivedFrom(ViewProviderDocumentObject::getClassTypeId())) {
signalActivatedObject(*(static_cast<ViewProviderDocumentObject*>(viewProvider)));
}
}
// some post-processing of view providers
std::map<const App::DocumentObject*,ViewProviderDocumentObject*>::iterator it;
for (it = d->_ViewProviderMap.begin(); it != d->_ViewProviderMap.end(); ++it) {
it->second->finishRestoring();
}
// reset modified flag
setModified(false);
}
/**
* Saves the properties of the view providers.
*/
void Document::SaveDocFile (Base::Writer &writer) const
{
writer.Stream() << "<?xml version='1.0' encoding='utf-8'?>" << std::endl
<< "<!--" << std::endl
<< " FreeCAD Document, see http://www.freecadweb.org for more information..."
<< std::endl << "-->" << std::endl;
writer.Stream() << "<Document SchemaVersion=\"1\">" << std::endl;
std::map<const App::DocumentObject*,ViewProviderDocumentObject*>::const_iterator it;
// writing the view provider names itself
writer.incInd(); // indentation for 'ViewProviderData Count'
writer.Stream() << writer.ind() << "<ViewProviderData Count=\""
<< d->_ViewProviderMap.size() <<"\">" << std::endl;
bool xml = writer.isForceXML();
//writer.setForceXML(true);
writer.incInd(); // indentation for 'ViewProvider name'
for(it = d->_ViewProviderMap.begin(); it != d->_ViewProviderMap.end(); ++it) {
const App::DocumentObject* doc = it->first;
ViewProvider* obj = it->second;
writer.Stream() << writer.ind() << "<ViewProvider name=\""
<< doc->getNameInDocument() << "\" "
<< "expanded=\"" << (doc->testStatus(App::Expand) ? 1:0)
<< "\">" << std::endl;
obj->Save(writer);
writer.Stream() << writer.ind() << "</ViewProvider>" << std::endl;
}
writer.setForceXML(xml);
writer.decInd(); // indentation for 'ViewProvider name'
writer.Stream() << writer.ind() << "</ViewProviderData>" << std::endl;
writer.decInd(); // indentation for 'ViewProviderData Count'
// set camera settings
QString viewPos;
if (d->_pcAppWnd->sendHasMsgToActiveView("GetCamera")) {
const char* ppReturn=0;
d->_pcAppWnd->sendMsgToActiveView("GetCamera",&ppReturn);
// remove the first line because it's a comment like '#Inventor V2.1 ascii'
QStringList lines = QString(QString::fromLatin1(ppReturn)).split(QLatin1String("\n"));
if (lines.size() > 1) {
lines.pop_front();
viewPos = lines.join(QLatin1String(" "));
}
}
writer.incInd(); // indentation for camera settings
writer.Stream() << writer.ind() << "<Camera settings=\""
<< (const char*)viewPos.toLatin1() <<"\"/>" << std::endl;
writer.decInd(); // indentation for camera settings
writer.Stream() << "</Document>" << std::endl;
}
void Document::exportObjects(const std::vector<App::DocumentObject*>& obj, Base::Writer& writer)
{
writer.Stream() << "<?xml version='1.0' encoding='utf-8'?>" << std::endl;
writer.Stream() << "<Document SchemaVersion=\"1\">" << std::endl;
std::map<const App::DocumentObject*,ViewProvider*> views;
for (std::vector<App::DocumentObject*>::const_iterator it = obj.begin(); it != obj.end(); ++it) {
Document* doc = Application::Instance->getDocument((*it)->getDocument());
if (doc) {
ViewProvider* vp = doc->getViewProvider(*it);
if (vp) views[*it] = vp;
}
}
// writing the view provider names itself
writer.incInd(); // indentation for 'ViewProviderData Count'
writer.Stream() << writer.ind() << "<ViewProviderData Count=\""
<< views.size() <<"\">" << std::endl;
bool xml = writer.isForceXML();
//writer.setForceXML(true);
writer.incInd(); // indentation for 'ViewProvider name'
std::map<const App::DocumentObject*,ViewProvider*>::const_iterator jt;
for (jt = views.begin(); jt != views.end(); ++jt) {
const App::DocumentObject* doc = jt->first;
ViewProvider* obj = jt->second;
writer.Stream() << writer.ind() << "<ViewProvider name=\""
<< doc->getNameInDocument() << "\" type=\""
<< obj->getTypeId().getName()
<< "\">" << std::endl;
obj->Save(writer);
writer.Stream() << writer.ind() << "</ViewProvider>" << std::endl;
}
writer.setForceXML(xml);
writer.decInd(); // indentation for 'ViewProvider name'
writer.Stream() << writer.ind() << "</ViewProviderData>" << std::endl;
writer.decInd(); // indentation for 'ViewProviderData Count'
writer.incInd(); // indentation for camera settings
writer.Stream() << writer.ind() << "<Camera settings=\"\"/>" << std::endl;
writer.decInd(); // indentation for camera settings
writer.Stream() << "</Document>" << std::endl;
}
void Document::importObjects(const std::vector<App::DocumentObject*>& obj, Base::Reader& reader,
const std::map<std::string, std::string>& nameMapping)
{
// We must create an XML parser to read from the input stream
Base::XMLReader xmlReader("GuiDocument.xml", reader);
xmlReader.readElement("Document");
long scheme = xmlReader.getAttributeAsInteger("SchemaVersion");
// At this stage all the document objects and their associated view providers exist.
// Now we must restore the properties of the view providers only.
//
// SchemeVersion "1"
if (scheme == 1) {
// read the viewproviders itself
xmlReader.readElement("ViewProviderData");
int Cnt = xmlReader.getAttributeAsInteger("Count");
std::vector<App::DocumentObject*>::const_iterator it = obj.begin();
for (int i=0;i<Cnt&&it!=obj.end();++i,++it) {
// The stored name usually doesn't match with the current name anymore
// thus we try to match by type. This should work because the order of
// objects should not have changed
xmlReader.readElement("ViewProvider");
std::string name = xmlReader.getAttribute("name");
std::map<std::string, std::string>::const_iterator jt = nameMapping.find(name);
if (jt != nameMapping.end())
name = jt->second;
Gui::ViewProvider* pObj = this->getViewProviderByName(name.c_str());