From e338fe6b7000e37f0bb34e35bc91071b647625bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Tr=C3=B6ger?= Date: Thu, 9 Mar 2017 08:05:21 +0100 Subject: [PATCH] Expose transaction events to document observer --- src/App/Application.cpp | 18 +++++++++ src/App/Application.h | 9 +++++ src/App/Document.cpp | 4 ++ src/App/Document.h | 6 +++ src/App/DocumentObserverPython.cpp | 62 ++++++++++++++++++++++++++++++ src/App/DocumentObserverPython.h | 9 +++++ 6 files changed, 108 insertions(+) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 6550031a83..fb03814abb 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -425,6 +425,9 @@ Document* Application::newDocument(const char * Name, const char * UserName) _pActiveDoc->signalRedo.connect(boost::bind(&App::Application::slotRedoDocument, this, _1)); _pActiveDoc->signalRecomputedObject.connect(boost::bind(&App::Application::slotRecomputedObject, this, _1)); _pActiveDoc->signalRecomputed.connect(boost::bind(&App::Application::slotRecomputed, this, _1)); + _pActiveDoc->signalOpenTransaction.connect(boost::bind(&App::Application::slotOpenTransaction, this, _1, _2)); + _pActiveDoc->signalCommitTransaction.connect(boost::bind(&App::Application::slotCommitTransaction, this, _1)); + _pActiveDoc->signalAbortTransaction.connect(boost::bind(&App::Application::slotAbortTransaction, this, _1)); // make sure that the active document is set in case no GUI is up { @@ -1018,6 +1021,21 @@ void Application::slotRecomputed(const Document& doc) this->signalRecomputed(doc); } +void Application::slotOpenTransaction(const Document& d, string s) +{ + this->signalOpenTransaction(d, s); +} + +void Application::slotCommitTransaction(const Document& d) +{ + this->signalCommitTransaction(d); +} + +void Application::slotAbortTransaction(const Document& d) +{ + this->signalAbortTransaction(d); +} + //************************************************************************** // Init, Destruct and singleton diff --git a/src/App/Application.h b/src/App/Application.h index 3a9252767c..5bc2599fbc 100644 --- a/src/App/Application.h +++ b/src/App/Application.h @@ -143,6 +143,12 @@ public: boost::signal signalRecomputed; /// signal on recomputed document object boost::signal signalObjectRecomputed; + // signal on opened transaction + boost::signal signalOpenTransaction; + // signal a commited transaction + boost::signal signalCommitTransaction; + // signal an aborted transaction + boost::signal signalAbortTransaction; //@} /** @name Signals of property changes @@ -277,6 +283,9 @@ protected: void slotRedoDocument(const App::Document&); void slotRecomputedObject(const App::DocumentObject&); void slotRecomputed(const App::Document&); + void slotOpenTransaction(const App::Document&, std::string); + void slotCommitTransaction(const App::Document&); + void slotAbortTransaction(const App::Document&); //@} private: diff --git a/src/App/Document.cpp b/src/App/Document.cpp index 73c434b695..90fdcc7bd0 100644 --- a/src/App/Document.cpp +++ b/src/App/Document.cpp @@ -961,6 +961,8 @@ void Document::openTransaction(const char* name) d->activeUndoTransaction->Name = name; else d->activeUndoTransaction->Name = ""; + + signalOpenTransaction(*this, name); } } @@ -1000,6 +1002,7 @@ void Document::commitTransaction() delete mUndoTransactions.front(); mUndoTransactions.pop_front(); } + signalCommitTransaction(*this); } } @@ -1014,6 +1017,7 @@ void Document::abortTransaction() // destroy the undo delete d->activeUndoTransaction; d->activeUndoTransaction = 0; + signalAbortTransaction(*this); } } diff --git a/src/App/Document.h b/src/App/Document.h index a41677391c..582a802e7c 100644 --- a/src/App/Document.h +++ b/src/App/Document.h @@ -148,6 +148,12 @@ public: const std::map&)> signalImportViewObjects; boost::signal signalRecomputed; boost::signal signalRecomputedObject; + //signal a new opened transaction + boost::signal signalOpenTransaction; + // signal a commited transaction + boost::signal signalCommitTransaction; + // signal an aborted transaction + boost::signal signalAbortTransaction; //@} /** @name File handling of the document */ diff --git a/src/App/DocumentObserverPython.cpp b/src/App/DocumentObserverPython.cpp index 9637a1c211..621eb96760 100644 --- a/src/App/DocumentObserverPython.cpp +++ b/src/App/DocumentObserverPython.cpp @@ -83,6 +83,13 @@ DocumentObserverPython::DocumentObserverPython(const Py::Object& obj) : inst(obj (&DocumentObserverPython::slotRecomputedObject, this, _1)); this->connectDocumentRecomputed = App::GetApplication().signalRecomputed.connect(boost::bind (&DocumentObserverPython::slotRecomputedDocument, this, _1)); + + this->connectDocumentOpenTransaction = App::GetApplication().signalOpenTransaction.connect(boost::bind + (&DocumentObserverPython::slotOpenTransaction, this, _1, _2)); + this->connectDocumentCommitTransaction = App::GetApplication().signalCommitTransaction.connect(boost::bind + (&DocumentObserverPython::slotCommitTransaction, this, _1)); + this->connectDocumentAbortTransaction = App::GetApplication().signalAbortTransaction.connect(boost::bind + (&DocumentObserverPython::slotAbortTransaction, this, _1)); } DocumentObserverPython::~DocumentObserverPython() @@ -99,6 +106,9 @@ DocumentObserverPython::~DocumentObserverPython() this->connectDocumentChangedObject.disconnect(); this->connectDocumentObjectRecomputed.disconnect(); this->connectDocumentRecomputed.disconnect(); + this->connectDocumentOpenTransaction.disconnect(); + this->connectDocumentCommitTransaction.disconnect(); + this->connectDocumentAbortTransaction.disconnect(); } void DocumentObserverPython::slotCreatedDocument(const App::Document& Doc) @@ -293,4 +303,56 @@ void DocumentObserverPython::slotRecomputedDocument(const App::Document& doc) Base::PyException e; // extract the Python error text e.ReportException(); } +} + +void DocumentObserverPython::slotOpenTransaction(const App::Document& doc, std::string str) +{ + Base::PyGILStateLocker lock; + try { + if (this->inst.hasAttr(std::string("slotOpenTransaction"))) { + Py::Callable method(this->inst.getAttr(std::string("slotOpenTransaction"))); + Py::Tuple args(2); + args.setItem(0, Py::Object(const_cast(doc).getPyObject(), true)); + args.setItem(1, Py::String(str)); + method.apply(args); + } + } + catch (Py::Exception&) { + Base::PyException e; // extract the Python error text + e.ReportException(); + } +} + +void DocumentObserverPython::slotCommitTransaction(const App::Document& doc) +{ + Base::PyGILStateLocker lock; + try { + if (this->inst.hasAttr(std::string("slotCommitTransaction"))) { + Py::Callable method(this->inst.getAttr(std::string("slotCommitTransaction"))); + Py::Tuple args(1); + args.setItem(0, Py::Object(const_cast(doc).getPyObject(), true)); + method.apply(args); + } + } + catch (Py::Exception&) { + Base::PyException e; // extract the Python error text + e.ReportException(); + } +} + +void DocumentObserverPython::slotAbortTransaction(const App::Document& doc) +{ + Base::PyGILStateLocker lock; + try { + if (this->inst.hasAttr(std::string("slotAbortTransaction"))) { + Py::Callable method(this->inst.getAttr(std::string("slotAbortTransaction"))); + Py::Tuple args(1); + args.setItem(0, Py::Object(const_cast(doc).getPyObject(), true)); + method.apply(args); + } + } + catch (Py::Exception&) { + Base::PyException e; // extract the Python error text + e.ReportException(); + } } \ No newline at end of file diff --git a/src/App/DocumentObserverPython.h b/src/App/DocumentObserverPython.h index 91ddcfad7d..7ddff06ee1 100644 --- a/src/App/DocumentObserverPython.h +++ b/src/App/DocumentObserverPython.h @@ -73,6 +73,12 @@ private: void slotRecomputedObject(const App::DocumentObject& Obj); /** Called when a observed document is recomputed */ void slotRecomputedDocument(const App::Document& Doc); + /** Called when a observed document opens a transaction */ + void slotOpenTransaction(const App::Document& Doc, std::string str); + /** Called when a observed document commits a transaction */ + void slotCommitTransaction(const App::Document& Doc); + /** Called when a observed document aborts a transaction */ + void slotAbortTransaction(const App::Document& Doc); private: Py::Object inst; @@ -90,6 +96,9 @@ private: Connection connectDocumentChangedObject; Connection connectDocumentObjectRecomputed; Connection connectDocumentRecomputed; + Connection connectDocumentOpenTransaction; + Connection connectDocumentCommitTransaction; + Connection connectDocumentAbortTransaction; }; } //namespace App