24
#include "PreCompiled.h"
32
#include "DocumentObserver.h"
33
#include "Application.h"
35
#include "ViewProviderDocumentObject.h"
36
#include <App/Document.h>
40
namespace sp = std::placeholders;
43
DocumentT::DocumentT() = default;
45
DocumentT::DocumentT(Document* doc)
47
document = doc->getDocument()->getName();
50
DocumentT::DocumentT(const std::string& name)
55
DocumentT::DocumentT(const DocumentT& doc)
57
document = doc.document;
60
DocumentT::~DocumentT() = default;
62
void DocumentT::operator=(const DocumentT& doc)
66
document = doc.document;
69
void DocumentT::operator=(const Document* doc)
71
document = doc->getDocument()->getName();
74
void DocumentT::operator=(const std::string& name)
79
Document* DocumentT::getDocument() const
81
return Application::Instance->getDocument(document.c_str());
84
std::string DocumentT::getDocumentName() const
89
std::string DocumentT::getGuiDocumentPython() const
91
std::stringstream str;
92
Document* doc = Application::Instance->activeDocument();
93
if (doc && document == doc->getDocument()->getName()) {
94
str << "Gui.ActiveDocument";
97
str << "Gui.getDocument(\""
104
std::string DocumentT::getAppDocumentPython() const
106
std::stringstream str;
107
Document* doc = Application::Instance->activeDocument();
108
if (doc && document == doc->getDocument()->getName()) {
109
str << "App.ActiveDocument";
112
str << "App.getDocument(\""
121
ViewProviderT::ViewProviderT() = default;
123
ViewProviderT::ViewProviderT(const ViewProviderT& other)
128
ViewProviderT::ViewProviderT(ViewProviderT &&other)
130
*this = std::move(other);
133
ViewProviderT::ViewProviderT(const ViewProviderDocumentObject* obj)
138
ViewProviderT::~ViewProviderT() = default;
140
ViewProviderT & ViewProviderT::operator=(const ViewProviderT& obj)
145
document = obj.document;
149
ViewProviderT &ViewProviderT::operator=(ViewProviderT&& obj)
153
object = std::move(obj.object);
154
document = std::move(obj.document);
158
void ViewProviderT::operator=(const ViewProviderDocumentObject* obj)
165
object = obj->getObject()->getNameInDocument();
166
document = obj->getObject()->getDocument()->getName();
170
bool ViewProviderT::operator==(const ViewProviderT &other) const {
171
return document == other.document
172
&& object == other.object;
175
Document* ViewProviderT::getDocument() const
177
return Application::Instance->getDocument(document.c_str());
180
const std::string& ViewProviderT::getDocumentName() const
185
std::string ViewProviderT::getGuiDocumentPython() const
187
DocumentT doct(document);
188
return doct.getGuiDocumentPython();
191
std::string ViewProviderT::getAppDocumentPython() const
193
DocumentT doct(document);
194
return doct.getAppDocumentPython();
197
ViewProviderDocumentObject* ViewProviderT::getViewProvider() const
199
ViewProviderDocumentObject* obj = nullptr;
200
Document* doc = getDocument();
202
obj = dynamic_cast<ViewProviderDocumentObject*>(doc->getViewProviderByName(object.c_str()));
207
const std::string& ViewProviderT::getObjectName() const
212
std::string ViewProviderT::getObjectPython() const
214
std::stringstream str;
215
Document* doc = Application::Instance->activeDocument();
216
if (doc && document == doc->getDocument()->getName()) {
217
str << "Gui.ActiveDocument.";
220
str << "Gui.getDocument(\""
231
class DocumentWeakPtrT::Private {
233
Private(Gui::Document* doc) : _document(doc) {
236
connectApplicationDeletedDocument = doc->signalDeleteDocument.connect(std::bind
237
(&Private::deletedDocument, this, sp::_1));
242
void deletedDocument(const Gui::Document& doc) {
243
if (_document == &doc)
247
connectApplicationDeletedDocument.disconnect();
251
Gui::Document* _document;
252
using Connection = boost::signals2::scoped_connection;
253
Connection connectApplicationDeletedDocument;
256
DocumentWeakPtrT::DocumentWeakPtrT(Gui::Document* doc) noexcept
257
: d(new Private(doc))
261
DocumentWeakPtrT::~DocumentWeakPtrT() = default;
263
void DocumentWeakPtrT::reset() noexcept
268
bool DocumentWeakPtrT::expired() const noexcept
270
return (d->_document == nullptr);
273
Gui::Document* DocumentWeakPtrT::operator*() const noexcept
278
Gui::Document* DocumentWeakPtrT::operator->() const noexcept
285
class ViewProviderWeakPtrT::Private {
287
Private(ViewProviderDocumentObject* obj) : object(obj) {
290
void deletedDocument(const Gui::Document& doc) {
292
if (object && object->getDocument() == &doc) {
296
void createdObject(const Gui::ViewProvider& obj) noexcept {
298
if (object == &obj) {
302
void deletedObject(const Gui::ViewProvider& obj) noexcept {
303
if (object == &obj) {
308
connectApplicationDeletedDocument.disconnect();
309
connectDocumentCreatedObject.disconnect();
310
connectDocumentDeletedObject.disconnect();
314
void set(ViewProviderDocumentObject* obj) {
319
Gui::Document* doc = obj->getDocument();
321
connectApplicationDeletedDocument = doc->signalDeleteDocument.connect(std::bind
322
(&Private::deletedDocument, this, sp::_1));
323
connectDocumentCreatedObject = doc->signalNewObject.connect(std::bind
324
(&Private::createdObject, this, sp::_1));
325
connectDocumentDeletedObject = doc->signalDeletedObject.connect(std::bind
326
(&Private::deletedObject, this, sp::_1));
330
catch (const Base::RuntimeError&) {
336
ViewProviderDocumentObject* get() const {
337
return indocument ? object : nullptr;
340
Gui::ViewProviderDocumentObject* object;
341
bool indocument{false};
342
using Connection = boost::signals2::scoped_connection;
343
Connection connectApplicationDeletedDocument;
344
Connection connectDocumentCreatedObject;
345
Connection connectDocumentDeletedObject;
348
ViewProviderWeakPtrT::ViewProviderWeakPtrT(ViewProviderDocumentObject* obj)
349
: d(new Private(obj))
353
ViewProviderWeakPtrT::~ViewProviderWeakPtrT() = default;
355
ViewProviderDocumentObject* ViewProviderWeakPtrT::_get() const noexcept
360
void ViewProviderWeakPtrT::reset()
365
bool ViewProviderWeakPtrT::expired() const noexcept
367
return !d->indocument;
370
ViewProviderWeakPtrT& ViewProviderWeakPtrT::operator= (ViewProviderDocumentObject* p)
377
ViewProviderDocumentObject* ViewProviderWeakPtrT::operator*() const noexcept
382
ViewProviderDocumentObject* ViewProviderWeakPtrT::operator->() const noexcept
387
bool ViewProviderWeakPtrT::operator== (const ViewProviderWeakPtrT& p) const noexcept
389
return d->get() == p.d->get();
392
bool ViewProviderWeakPtrT::operator!= (const ViewProviderWeakPtrT& p) const noexcept
394
return d->get() != p.d->get();
399
DocumentObserver::DocumentObserver() = default;
401
DocumentObserver::DocumentObserver(Document* doc)
406
DocumentObserver::~DocumentObserver() = default;
408
void DocumentObserver::attachDocument(Document* doc)
416
this->connectDocumentCreatedObject = doc->signalNewObject.connect(std::bind
417
(&DocumentObserver::slotCreatedObject, this, sp::_1));
418
this->connectDocumentDeletedObject = doc->signalDeletedObject.connect(std::bind
419
(&DocumentObserver::slotDeletedObject, this, sp::_1));
420
this->connectDocumentChangedObject = doc->signalChangedObject.connect(std::bind
421
(&DocumentObserver::slotChangedObject, this, sp::_1, sp::_2));
422
this->connectDocumentRelabelObject = doc->signalRelabelObject.connect(std::bind
423
(&DocumentObserver::slotRelabelObject, this, sp::_1));
424
this->connectDocumentActivateObject = doc->signalActivatedObject.connect(std::bind
425
(&DocumentObserver::slotActivatedObject, this, sp::_1));
426
this->connectDocumentEditObject = doc->signalInEdit.connect(std::bind
427
(&DocumentObserver::slotEnterEditObject, this, sp::_1));
428
this->connectDocumentResetObject = doc->signalResetEdit.connect(std::bind
429
(&DocumentObserver::slotResetEditObject, this, sp::_1));
430
this->connectDocumentUndo = doc->signalUndoDocument.connect(std::bind
431
(&DocumentObserver::slotUndoDocument, this, sp::_1));
432
this->connectDocumentRedo = doc->signalRedoDocument.connect(std::bind
433
(&DocumentObserver::slotRedoDocument, this, sp::_1));
434
this->connectDocumentDelete = doc->signalDeleteDocument.connect(std::bind
435
(&DocumentObserver::slotDeleteDocument, this, sp::_1));
439
void DocumentObserver::detachDocument()
441
this->connectDocumentCreatedObject.disconnect();
442
this->connectDocumentDeletedObject.disconnect();
443
this->connectDocumentChangedObject.disconnect();
444
this->connectDocumentRelabelObject.disconnect();
445
this->connectDocumentActivateObject.disconnect();
446
this->connectDocumentEditObject.disconnect();
447
this->connectDocumentResetObject.disconnect();
448
this->connectDocumentUndo.disconnect();
449
this->connectDocumentRedo.disconnect();
450
this->connectDocumentDelete.disconnect();
453
void DocumentObserver::slotUndoDocument(const Document& )
457
void DocumentObserver::slotRedoDocument(const Document& )
461
void DocumentObserver::slotDeleteDocument(const Document& )
465
void DocumentObserver::slotCreatedObject(const ViewProviderDocumentObject& )
469
void DocumentObserver::slotDeletedObject(const ViewProviderDocumentObject& )
473
void DocumentObserver::slotChangedObject(const ViewProviderDocumentObject& ,
474
const App::Property& )
478
void DocumentObserver::slotRelabelObject(const ViewProviderDocumentObject& )
482
void DocumentObserver::slotActivatedObject(const ViewProviderDocumentObject& )
486
void DocumentObserver::slotEnterEditObject(const ViewProviderDocumentObject& )
490
void DocumentObserver::slotResetEditObject(const ViewProviderDocumentObject& )