| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2018 Stefan Tröger <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#ifndef GUI_DOCUMENTOBSERVERPYTHON_H |
| 25 |
#define GUI_DOCUMENTOBSERVERPYTHON_H |
| 26 |
|
| 27 |
#include <FCGlobal.h> |
| 28 |
#include <boost/signals2.hpp> |
| 29 |
#include <CXX/Objects.hxx> |
| 30 |
#include <string> |
| 31 |
#include <vector> |
| 32 |
|
| 33 |
namespace App |
| 34 |
{ |
| 35 |
class Property; |
| 36 |
} |
| 37 |
|
| 38 |
namespace Gui |
| 39 |
{ |
| 40 |
|
| 41 |
class Document; |
| 42 |
class ViewProvider; |
| 43 |
class ViewProviderDocumentObject; |
| 44 |
|
| 45 |
/** |
| 46 |
* The DocumentObserverPython class is used to notify registered Python instances |
| 47 |
* whenever something happens to a document, like creation, destruction, adding or |
| 48 |
* removing viewproviders or when viewprovider property changes. This is the equivalent to the app |
| 49 |
* python document observer |
| 50 |
*/ |
| 51 |
class GuiExport DocumentObserverPython |
| 52 |
{ |
| 53 |
|
| 54 |
public: |
| 55 |
/// Constructor |
| 56 |
DocumentObserverPython(const Py::Object& obj); |
| 57 |
virtual ~DocumentObserverPython(); |
| 58 |
|
| 59 |
static void addObserver(const Py::Object& obj); |
| 60 |
static void removeObserver(const Py::Object& obj); |
| 61 |
|
| 62 |
private: |
| 63 |
/** Checks if a new document was created */ |
| 64 |
void slotCreatedDocument(const Gui::Document& Doc); |
| 65 |
/** Checks if the given document is about to be closed */ |
| 66 |
void slotDeletedDocument(const Gui::Document& Doc); |
| 67 |
/** Checks if the given document is relabeled */ |
| 68 |
void slotRelabelDocument(const Gui::Document& Doc); |
| 69 |
/** Checks if the given document is renamed */ |
| 70 |
void slotRenameDocument(const Gui::Document& Doc); |
| 71 |
/** Checks if the given document is activated */ |
| 72 |
void slotActivateDocument(const Gui::Document& Doc); |
| 73 |
/** Checks if a new object was added. */ |
| 74 |
void slotCreatedObject(const Gui::ViewProvider& Obj); |
| 75 |
/** Checks if the given object is about to be removed. */ |
| 76 |
void slotDeletedObject(const Gui::ViewProvider& Obj); |
| 77 |
/** The property of an observed object has changed */ |
| 78 |
void slotBeforeChangeObject(const Gui::ViewProvider& Obj, const App::Property& Prop); |
| 79 |
/** The property of an observed object has changed */ |
| 80 |
void slotChangedObject(const Gui::ViewProvider& Obj, const App::Property& Prop); |
| 81 |
/** The object was set into edit mode */ |
| 82 |
void slotInEdit(const Gui::ViewProviderDocumentObject& Obj); |
| 83 |
/** The has left edit mode */ |
| 84 |
void slotResetEdit(const Gui::ViewProviderDocumentObject& Obj); |
| 85 |
|
| 86 |
private: |
| 87 |
Py::Object inst; |
| 88 |
static std::vector<DocumentObserverPython*> _instances; |
| 89 |
|
| 90 |
using Connection = struct PythonObject |
| 91 |
{ |
| 92 |
boost::signals2::scoped_connection slot; |
| 93 |
Py::Object py; |
| 94 |
PyObject* ptr() |
| 95 |
{ |
| 96 |
return py.ptr(); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
Connection pyCreatedDocument; |
| 101 |
Connection pyDeletedDocument; |
| 102 |
Connection pyRelabelDocument; |
| 103 |
Connection pyRenameDocument; |
| 104 |
Connection pyActivateDocument; |
| 105 |
Connection pyCreatedObject; |
| 106 |
Connection pyDeletedObject; |
| 107 |
Connection pyBeforeChangeObject; |
| 108 |
Connection pyChangedObject; |
| 109 |
Connection pyInEdit; |
| 110 |
Connection pyResetEdit; |
| 111 |
}; |
| 112 |
|
| 113 |
} // namespace Gui |
| 114 |
|
| 115 |
#endif // GUI_DOCUMENTOBSERVERPYTHON_H |