| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Jürgen Riegel <[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 |
#ifndef GUI_DOCUMENT_H |
| 24 |
#define GUI_DOCUMENT_H |
| 25 |
|
| 26 |
#include <list> |
| 27 |
#include <map> |
| 28 |
#include <string> |
| 29 |
#include <boost/signals2.hpp> |
| 30 |
#include <QString> |
| 31 |
|
| 32 |
#include <Base/Persistence.h> |
| 33 |
#include <Gui/TreeItemMode.h> |
| 34 |
|
| 35 |
class SoNode; |
| 36 |
class SoPath; |
| 37 |
|
| 38 |
namespace Base |
| 39 |
{ |
| 40 |
class Matrix4D; |
| 41 |
} |
| 42 |
|
| 43 |
namespace App |
| 44 |
{ |
| 45 |
class Document; |
| 46 |
class DocumentObject; |
| 47 |
class DocumentObjectGroup; |
| 48 |
class Property; |
| 49 |
class Transaction; |
| 50 |
} // namespace App |
| 51 |
|
| 52 |
namespace Gui |
| 53 |
{ |
| 54 |
|
| 55 |
class BaseView; |
| 56 |
class MDIView; |
| 57 |
class View3DInventor; |
| 58 |
class ViewProvider; |
| 59 |
class ViewProviderDocumentObject; |
| 60 |
class Application; |
| 61 |
class DocumentPy; |
| 62 |
class TransactionViewProvider; |
| 63 |
|
| 64 |
enum class CreateViewMode |
| 65 |
{ |
| 66 |
Normal, |
| 67 |
Clone |
| 68 |
}; |
| 69 |
|
| 70 |
/** The Gui Document |
| 71 |
* This is the document on GUI level. Its main responsibility is keeping |
| 72 |
* track off open windows for a document and warning on unsaved closes. |
| 73 |
* All handled views on the document must inherit from MDIView |
| 74 |
* @see App::Document |
| 75 |
* @see MDIView |
| 76 |
* @author Jürgen Riegel |
| 77 |
*/ |
| 78 |
class GuiExport Document: public Base::Persistence |
| 79 |
{ |
| 80 |
public: |
| 81 |
Document(App::Document* pcDocument, Application* app); |
| 82 |
~Document() override; |
| 83 |
|
| 84 |
protected: |
| 85 |
/** @name I/O of the document */ |
| 86 |
//@{ |
| 87 |
/// This slot is connected to the App::Document::signalNewObject(...) |
| 88 |
void slotNewObject(const App::DocumentObject&); |
| 89 |
void slotDeletedObject(const App::DocumentObject&); |
| 90 |
void slotChangedObject(const App::DocumentObject&, const App::Property&); |
| 91 |
void slotRelabelObject(const App::DocumentObject&); |
| 92 |
void slotTransactionAppend(const App::DocumentObject&, App::Transaction*); |
| 93 |
void slotTransactionRemove(const App::DocumentObject&, App::Transaction*); |
| 94 |
void slotActivatedObject(const App::DocumentObject&); |
| 95 |
void slotStartRestoreDocument(const App::Document&); |
| 96 |
void slotFinishRestoreDocument(const App::Document&); |
| 97 |
void slotUndoDocument(const App::Document&); |
| 98 |
void slotRedoDocument(const App::Document&); |
| 99 |
void slotShowHidden(const App::Document&); |
| 100 |
void slotFinishImportObjects(const std::vector<App::DocumentObject*>&); |
| 101 |
void slotFinishRestoreObject(const App::DocumentObject& obj); |
| 102 |
void slotRecomputed(const App::Document&); |
| 103 |
void slotSkipRecompute(const App::Document& doc, const std::vector<App::DocumentObject*>& objs); |
| 104 |
void slotTouchedObject(const App::DocumentObject&); |
| 105 |
void slotChangePropertyEditor(const App::Document&, const App::Property&); |
| 106 |
void callSignalBeforeRecompute(); |
| 107 |
//@} |
| 108 |
|
| 109 |
public: |
| 110 |
void addViewProvider(Gui::ViewProviderDocumentObject*); |
| 111 |
|
| 112 |
/** @name Signals of the document */ |
| 113 |
//@{ |
| 114 |
/// signal on new Object |
| 115 |
mutable boost::signals2::signal<void(const Gui::ViewProviderDocumentObject&)> signalNewObject; |
| 116 |
/// signal on deleted Object |
| 117 |
mutable boost::signals2::signal<void(const Gui::ViewProviderDocumentObject&)> signalDeletedObject; |
| 118 |
/** signal on changed Object, the 2nd argument is the changed property |
| 119 |
of the referenced document object, not of the view provider */ |
| 120 |
mutable boost::signals2::signal<void(const Gui::ViewProviderDocumentObject&, const App::Property&)> |
| 121 |
signalChangedObject; |
| 122 |
/// signal on renamed Object |
| 123 |
mutable boost::signals2::signal<void(const Gui::ViewProviderDocumentObject&)> signalRelabelObject; |
| 124 |
/// signal on activated Object |
| 125 |
mutable boost::signals2::signal<void(const Gui::ViewProviderDocumentObject&)> signalActivatedObject; |
| 126 |
/// signal on activated Object in the tree (bold item) |
| 127 |
mutable boost::signals2::signal<void(const Gui::ViewProviderDocumentObject*, const char*)> |
| 128 |
signalActivatedViewProvider; |
| 129 |
/// signal on entering in edit mode |
| 130 |
mutable boost::signals2::signal<void(const Gui::ViewProviderDocumentObject&)> signalInEdit; |
| 131 |
/// signal on leaving edit mode |
| 132 |
mutable boost::signals2::signal<void(const Gui::ViewProviderDocumentObject&)> signalResetEdit; |
| 133 |
/// signal on changed Object, the 2nd argument is the highlite mode to use |
| 134 |
mutable boost::signals2::signal<void( |
| 135 |
const Gui::ViewProviderDocumentObject&, |
| 136 |
const Gui::HighlightMode&, |
| 137 |
bool, |
| 138 |
App::DocumentObject* parent, |
| 139 |
const char* subname |
| 140 |
)> |
| 141 |
signalHighlightObject; |
| 142 |
/// signal on changed Object, the 2nd argument is the highlite mode to use |
| 143 |
mutable boost::signals2::signal<void( |
| 144 |
const Gui::ViewProviderDocumentObject&, |
| 145 |
const Gui::TreeItemMode&, |
| 146 |
App::DocumentObject* parent, |
| 147 |
const char* subname |
| 148 |
)> |
| 149 |
signalExpandObject; |
| 150 |
/// signal on changed ShowInTree property in view provider |
| 151 |
mutable boost::signals2::signal<void(const Gui::ViewProviderDocumentObject&)> signalShowItem; |
| 152 |
/// signal on scrolling to an object |
| 153 |
mutable boost::signals2::signal<void(const Gui::ViewProviderDocumentObject&)> signalScrollToObject; |
| 154 |
/// signal on undo Document |
| 155 |
mutable boost::signals2::signal<void(const Gui::Document& doc)> signalUndoDocument; |
| 156 |
/// signal on redo Document |
| 157 |
mutable boost::signals2::signal<void(const Gui::Document& doc)> signalRedoDocument; |
| 158 |
/// signal on deleting Document |
| 159 |
mutable boost::signals2::signal<void(const Gui::Document& doc)> signalDeleteDocument; |
| 160 |
//@} |
| 161 |
|
| 162 |
/** @name I/O of the document */ |
| 163 |
//@{ |
| 164 |
unsigned int getMemSize() const override; |
| 165 |
/// Save the document |
| 166 |
bool save(); |
| 167 |
/// Save the document under a new file name |
| 168 |
bool saveAs(); |
| 169 |
/// Save a copy of the document under a new file name |
| 170 |
bool saveCopy(); |
| 171 |
/// Save all open document |
| 172 |
static void saveAll(); |
| 173 |
/// This method is used to save properties or very small amounts of data to an XML document. |
| 174 |
void Save(Base::Writer& writer) const override; |
| 175 |
/// This method is used to restore properties from an XML document. |
| 176 |
void Restore(Base::XMLReader& reader) override; |
| 177 |
/// This method is used to save large amounts of data to a binary file. |
| 178 |
void SaveDocFile(Base::Writer& writer) const override; |
| 179 |
/// This method is used to restore large amounts of data from a binary file. |
| 180 |
void RestoreDocFile(Base::Reader& reader) override; |
| 181 |
void exportObjects(const std::vector<App::DocumentObject*>&, Base::Writer&); |
| 182 |
void importObjects( |
| 183 |
const std::vector<App::DocumentObject*>&, |
| 184 |
Base::Reader&, |
| 185 |
const std::map<std::string, std::string>& nameMapping |
| 186 |
); |
| 187 |
/// Add all root objects of the given array to a group |
| 188 |
void addRootObjectsToGroup(const std::vector<App::DocumentObject*>&, App::DocumentObjectGroup*); |
| 189 |
//@} |
| 190 |
|
| 191 |
/// Observer message from the App doc |
| 192 |
void setModified(bool); |
| 193 |
bool isModified() const; |
| 194 |
|
| 195 |
/// Returns true if the document is about to be closed, false otherwise |
| 196 |
bool isAboutToClose() const; |
| 197 |
|
| 198 |
/// Getter for the App Document |
| 199 |
App::Document* getDocument() const; |
| 200 |
|
| 201 |
/** @name methods for View handling */ |
| 202 |
//@{ |
| 203 |
/// Getter for the active view |
| 204 |
Gui::MDIView* getActiveView() const; |
| 205 |
void setActiveWindow(Gui::MDIView* view); |
| 206 |
Gui::MDIView* getEditingViewOfViewProvider(Gui::ViewProvider*) const; |
| 207 |
Gui::MDIView* getViewOfViewProvider(const Gui::ViewProvider*) const; |
| 208 |
Gui::MDIView* getViewOfNode(SoNode*) const; |
| 209 |
/// Create a new view |
| 210 |
MDIView* createView(const Base::Type& typeId, CreateViewMode mode = CreateViewMode::Normal); |
| 211 |
/** send messages to the active view |
| 212 |
* Send a specific massage to the active view and is able to receive a |
| 213 |
* return message |
| 214 |
*/ |
| 215 |
/// send Messages to all views |
| 216 |
bool sendMsgToViews(const char* pMsg); |
| 217 |
/** Sends the message \a pMsg to the views of type \a typeid and stops with |
| 218 |
* the first view that supports the message and returns \a ppReturn. The very |
| 219 |
* first checked view is the current active view. |
| 220 |
* If a view supports the message true is returned and false otherwise. |
| 221 |
*/ |
| 222 |
bool sendMsgToFirstView(const Base::Type& typeId, const char* pMsg, const char** ppReturn); |
| 223 |
/// Attach a view (get called by the MDIView constructor) |
| 224 |
void attachView(Gui::BaseView* pcView, bool bPassiv = false); |
| 225 |
/// Detach a view (get called by the MDIView destructor) |
| 226 |
void detachView(Gui::BaseView* pcView, bool bPassiv = false); |
| 227 |
/// helper for selection |
| 228 |
ViewProviderDocumentObject* getViewProviderByPathFromTail(SoPath* path) const; |
| 229 |
/// helper for selection |
| 230 |
ViewProviderDocumentObject* getViewProviderByPathFromHead(SoPath* path) const; |
| 231 |
/// Get all view providers along the path and the corresponding node index in the path |
| 232 |
std::vector<std::pair<ViewProviderDocumentObject*, int>> getViewProvidersByPath(SoPath* path) const; |
| 233 |
/// call update on all attached views |
| 234 |
void onUpdate(); |
| 235 |
/// call relabel to all attached views |
| 236 |
void onRelabel(); |
| 237 |
/// returns a list of all attached MDI views |
| 238 |
std::list<MDIView*> getMDIViews() const; |
| 239 |
/// returns a list of all MDI views of a certain type |
| 240 |
std::list<MDIView*> getMDIViewsOfType(const Base::Type& typeId) const; |
| 241 |
MDIView* setActiveView( |
| 242 |
const ViewProviderDocumentObject* vp = nullptr, |
| 243 |
Base::Type typeId = Base::Type() |
| 244 |
); |
| 245 |
View3DInventor* openEditingView3D(const ViewProviderDocumentObject* vp); |
| 246 |
View3DInventor* openEditingView3D(const App::DocumentObject* obj); |
| 247 |
//@} |
| 248 |
|
| 249 |
/** @name View provider handling */ |
| 250 |
//@{ |
| 251 |
/// Get the view provider for that object |
| 252 |
ViewProvider* getViewProvider(const App::DocumentObject*) const; |
| 253 |
ViewProviderDocumentObject* getViewProvider(SoNode* node) const; |
| 254 |
/// set an annotation view provider |
| 255 |
void setAnnotationViewProvider(const char* name, ViewProvider* pcProvider); |
| 256 |
/// get an annotation view provider |
| 257 |
ViewProvider* getAnnotationViewProvider(const char* name) const; |
| 258 |
/// return true if the view provider is added as an annotation view provider |
| 259 |
bool isAnnotationViewProvider(const ViewProvider* vp) const; |
| 260 |
/// remove an annotation view provider from the document and return it |
| 261 |
ViewProvider* takeAnnotationViewProvider(const char* name); |
| 262 |
/// remove and delete an annotation view provider |
| 263 |
void removeAnnotationViewProvider(const char* name); |
| 264 |
/// test if the feature is in show |
| 265 |
bool isShow(const char* name); |
| 266 |
/// put the feature in show |
| 267 |
void setShow(const char* name); |
| 268 |
/// set the feature in Noshow |
| 269 |
void setHide(const char* name); |
| 270 |
/// set the feature transformation (only viewing) |
| 271 |
void setPos(const char* name, const Base::Matrix4D& rclMtrx); |
| 272 |
std::vector<ViewProvider*> getViewProvidersOfType(const Base::Type& typeId) const; |
| 273 |
ViewProvider* getViewProviderByName(const char* name) const; |
| 274 |
/// set the ViewProvider in special edit mode |
| 275 |
bool setEdit(Gui::ViewProvider* p, int ModNum = 0, const char* subname = nullptr); |
| 276 |
const Base::Matrix4D& getEditingTransform() const; |
| 277 |
void setEditingTransform(const Base::Matrix4D& mat); |
| 278 |
/// reset from edit mode, this cause all document to reset edit |
| 279 |
void resetEdit(); |
| 280 |
/// reset edit of this document |
| 281 |
void _resetEdit(); |
| 282 |
/// set if the edit asks for restore or not. |
| 283 |
void setEditRestore(bool val); |
| 284 |
/// get the in edit ViewProvider or NULL |
| 285 |
ViewProvider* getInEdit( |
| 286 |
ViewProviderDocumentObject** parentVp = nullptr, |
| 287 |
std::string* subname = nullptr, |
| 288 |
int* mode = nullptr, |
| 289 |
std::string* subElement = nullptr |
| 290 |
) const; |
| 291 |
/// set the in edit ViewProvider subname reference |
| 292 |
void setInEdit(ViewProviderDocumentObject* parentVp, const char* subname); |
| 293 |
/** Add or remove view provider from scene graphs of all views |
| 294 |
* |
| 295 |
* It calls ViewProvider::canAddToSceneGraph() to decide whether to add the |
| 296 |
* view provider or remove it |
| 297 |
*/ |
| 298 |
void toggleInSceneGraph(ViewProvider* vp); |
| 299 |
//@} |
| 300 |
|
| 301 |
/** @name methods for the UNDO REDO handling */ |
| 302 |
//@{ |
| 303 |
/// Open a new Undo transaction on the document |
| 304 |
void openCommand(const char* sName = nullptr); |
| 305 |
/// Commit the Undo transaction on the document |
| 306 |
void commitCommand(); |
| 307 |
/// Abort the Undo transaction on the document |
| 308 |
void abortCommand(); |
| 309 |
/// Check if an Undo transaction is open |
| 310 |
bool hasPendingCommand() const; |
| 311 |
/// Get an Undo string vector with the Undo names |
| 312 |
std::vector<std::string> getUndoVector() const; |
| 313 |
/// Get an Redo string vector with the Redo names |
| 314 |
std::vector<std::string> getRedoVector() const; |
| 315 |
/// Will UNDO one or more steps |
| 316 |
void undo(int iSteps); |
| 317 |
/// Will REDO one or more steps |
| 318 |
void redo(int iSteps); |
| 319 |
/** Check if the document is performing undo/redo transaction |
| 320 |
* |
| 321 |
* Unlike App::Document::isPerformingTransaction(), Gui::Document will |
| 322 |
* report transacting when triggering grouped undo/redo in other documents |
| 323 |
*/ |
| 324 |
bool isPerformingTransaction() const; |
| 325 |
//@} |
| 326 |
|
| 327 |
/// handles the application close event |
| 328 |
bool canClose(bool checkModify = true, bool checkLink = false); |
| 329 |
bool isLastView(); |
| 330 |
|
| 331 |
/// called by Application before being deleted |
| 332 |
void beforeDelete(); |
| 333 |
|
| 334 |
PyObject* getPyObject() override; |
| 335 |
|
| 336 |
const char* getCameraSettings() const; |
| 337 |
bool saveCameraSettings(const char*) const; |
| 338 |
|
| 339 |
/// get all tree root objects (objects that are at the root of the object tree) |
| 340 |
std::vector<App::DocumentObject*> getTreeRootObjects() const; |
| 341 |
|
| 342 |
protected: |
| 343 |
// pointer to the python class |
| 344 |
Gui::DocumentPy* _pcDocPy; |
| 345 |
|
| 346 |
private: |
| 347 |
bool trySetEdit(Gui::ViewProvider* p, int ModNum, const char* subname); |
| 348 |
void resetIfEditing(); |
| 349 |
// handles the scene graph nodes to correctly group child and parents |
| 350 |
void handleChildren3D(ViewProvider* viewProvider, bool deleting = false); |
| 351 |
|
| 352 |
/// Check other documents for the same transaction ID |
| 353 |
bool checkTransactionID(bool undo, int iSteps); |
| 354 |
/// Ask for user interaction if saving has failed |
| 355 |
bool askIfSavingFailed(const QString&); |
| 356 |
/// Warn if saving a document from an older FreeCAD version (returns false if user cancels) |
| 357 |
bool warnIfOlderVersion(); |
| 358 |
|
| 359 |
struct DocumentP* d; |
| 360 |
static int _iDocCount; |
| 361 |
|
| 362 |
mutable std::string cameraSettings; |
| 363 |
|
| 364 |
/** @name attributes for the UNDO REDO facility |
| 365 |
*/ |
| 366 |
//@{ |
| 367 |
/// undo names list |
| 368 |
std::list<std::string> listUndoNames; |
| 369 |
/// redo names list |
| 370 |
std::list<std::string> listRedoNames; |
| 371 |
//@} |
| 372 |
|
| 373 |
friend class TransactionViewProvider; |
| 374 |
}; |
| 375 |
|
| 376 |
} // namespace Gui |
| 377 |
|
| 378 |
|
| 379 |
#endif // GUI_DOCUMENT_H |