| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 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_DOCUMENTOBSERVER_H |
| 24 |
#define GUI_DOCUMENTOBSERVER_H |
| 25 |
|
| 26 |
#include <Base/BaseClass.h> |
| 27 |
#include <boost/signals2.hpp> |
| 28 |
|
| 29 |
|
| 30 |
namespace App |
| 31 |
{ |
| 32 |
class Property; |
| 33 |
} |
| 34 |
namespace Gui |
| 35 |
{ |
| 36 |
class Document; |
| 37 |
class ViewProviderDocumentObject; |
| 38 |
|
| 39 |
/** |
| 40 |
* The DocumentT class is a helper class to store the name of a document. |
| 41 |
* This can be useful when you cannot rely on that the document still exists when you have to |
| 42 |
* access it. |
| 43 |
* |
| 44 |
* @author Werner Mayer |
| 45 |
*/ |
| 46 |
class GuiExport DocumentT |
| 47 |
{ |
| 48 |
public: |
| 49 |
/*! Constructor */ |
| 50 |
DocumentT(); |
| 51 |
/*! Constructor */ |
| 52 |
explicit DocumentT(Document*); |
| 53 |
/*! Constructor */ |
| 54 |
explicit DocumentT(const std::string&); |
| 55 |
/*! Constructor */ |
| 56 |
DocumentT(const DocumentT&); |
| 57 |
/*! Destructor */ |
| 58 |
~DocumentT(); |
| 59 |
/*! Assignment operator */ |
| 60 |
void operator=(const DocumentT&); |
| 61 |
/*! Assignment operator */ |
| 62 |
void operator=(const Document*); |
| 63 |
/*! Assignment operator */ |
| 64 |
void operator=(const std::string&); |
| 65 |
|
| 66 |
/*! Get a pointer to the document or 0 if it doesn't exist any more. */ |
| 67 |
Document* getDocument() const; |
| 68 |
/*! Get the name of the document. */ |
| 69 |
std::string getDocumentName() const; |
| 70 |
/*! Get the Gui::Document as Python command. */ |
| 71 |
std::string getGuiDocumentPython() const; |
| 72 |
/*! Get the App::Document as Python command. */ |
| 73 |
std::string getAppDocumentPython() const; |
| 74 |
|
| 75 |
private: |
| 76 |
std::string document; |
| 77 |
}; |
| 78 |
|
| 79 |
/** |
| 80 |
* The ViewProviderT class is a helper class to store the names of a view provider and its document. |
| 81 |
* This can be useful when you cannot rely on that the document or the object still exists when you |
| 82 |
* have to access it. |
| 83 |
* |
| 84 |
* @author Werner Mayer |
| 85 |
*/ |
| 86 |
class GuiExport ViewProviderT |
| 87 |
{ |
| 88 |
public: |
| 89 |
/*! Constructor */ |
| 90 |
ViewProviderT(); |
| 91 |
/*! Constructor */ |
| 92 |
ViewProviderT(const ViewProviderT&); |
| 93 |
/*! Constructor */ |
| 94 |
ViewProviderT(ViewProviderT&&); |
| 95 |
/*! Constructor */ |
| 96 |
explicit ViewProviderT(const ViewProviderDocumentObject*); |
| 97 |
/*! Destructor */ |
| 98 |
~ViewProviderT(); |
| 99 |
/*! Assignment operator */ |
| 100 |
ViewProviderT& operator=(const ViewProviderT&); |
| 101 |
/*! Assignment operator */ |
| 102 |
ViewProviderT& operator=(ViewProviderT&&); |
| 103 |
/*! Assignment operator */ |
| 104 |
void operator=(const ViewProviderDocumentObject*); |
| 105 |
/*! Equality operator */ |
| 106 |
bool operator==(const ViewProviderT&) const; |
| 107 |
|
| 108 |
/*! Get a pointer to the document or 0 if it doesn't exist any more. */ |
| 109 |
Document* getDocument() const; |
| 110 |
/*! Get the name of the document. */ |
| 111 |
const std::string& getDocumentName() const; |
| 112 |
/*! Get the Gui::Document as Python command. */ |
| 113 |
std::string getGuiDocumentPython() const; |
| 114 |
/*! Get the App::Document as Python command. */ |
| 115 |
std::string getAppDocumentPython() const; |
| 116 |
/*! Get a pointer to the document object or 0 if it doesn't exist any more. */ |
| 117 |
ViewProviderDocumentObject* getViewProvider() const; |
| 118 |
/*! Get the name of the document object. */ |
| 119 |
const std::string& getObjectName() const; |
| 120 |
/*! Get the document object as Python command. */ |
| 121 |
std::string getObjectPython() const; |
| 122 |
/*! Get a pointer to the document or 0 if it doesn't exist any more or the type doesn't match. */ |
| 123 |
template<typename T> |
| 124 |
inline T* getObjectAs() const |
| 125 |
{ |
| 126 |
return freecad_cast<T*>(getViewProvider()); |
| 127 |
} |
| 128 |
|
| 129 |
private: |
| 130 |
std::string document; |
| 131 |
std::string object; |
| 132 |
}; |
| 133 |
|
| 134 |
/** |
| 135 |
* @brief The DocumentWeakPtrT class |
| 136 |
*/ |
| 137 |
class GuiExport DocumentWeakPtrT |
| 138 |
{ |
| 139 |
public: |
| 140 |
explicit DocumentWeakPtrT(Gui::Document*) noexcept; |
| 141 |
~DocumentWeakPtrT(); |
| 142 |
|
| 143 |
/*! |
| 144 |
* \brief reset |
| 145 |
* Releases the reference to the managed object. After the call *this manages no object. |
| 146 |
*/ |
| 147 |
void reset() noexcept; |
| 148 |
/*! |
| 149 |
* \brief expired |
| 150 |
* \return true if the managed object has already been deleted, false otherwise. |
| 151 |
*/ |
| 152 |
bool expired() const noexcept; |
| 153 |
/*! |
| 154 |
* \brief operator * |
| 155 |
* \return pointer to the document |
| 156 |
*/ |
| 157 |
Gui::Document* operator*() const noexcept; |
| 158 |
/*! |
| 159 |
* \brief operator -> |
| 160 |
* \return pointer to the document |
| 161 |
*/ |
| 162 |
Gui::Document* operator->() const noexcept; |
| 163 |
|
| 164 |
// disable |
| 165 |
DocumentWeakPtrT(const DocumentWeakPtrT&) = delete; |
| 166 |
DocumentWeakPtrT& operator=(const DocumentWeakPtrT&) = delete; |
| 167 |
|
| 168 |
private: |
| 169 |
class Private; |
| 170 |
std::unique_ptr<Private> d; |
| 171 |
}; |
| 172 |
|
| 173 |
/** |
| 174 |
* @brief The ViewProviderWeakPtrT class |
| 175 |
*/ |
| 176 |
class GuiExport ViewProviderWeakPtrT |
| 177 |
{ |
| 178 |
public: |
| 179 |
explicit ViewProviderWeakPtrT(ViewProviderDocumentObject*); |
| 180 |
|
| 181 |
ViewProviderWeakPtrT(ViewProviderWeakPtrT&&); |
| 182 |
ViewProviderWeakPtrT& operator=(ViewProviderWeakPtrT&&); |
| 183 |
|
| 184 |
FC_DISABLE_COPY(ViewProviderWeakPtrT); |
| 185 |
|
| 186 |
~ViewProviderWeakPtrT(); |
| 187 |
|
| 188 |
/*! |
| 189 |
* \brief reset |
| 190 |
* Releases the reference to the managed object. After the call *this manages no object. |
| 191 |
*/ |
| 192 |
void reset(); |
| 193 |
/*! |
| 194 |
* \brief expired |
| 195 |
* \return true if the managed object has already been deleted, false otherwise. |
| 196 |
*/ |
| 197 |
bool expired() const noexcept; |
| 198 |
/*! |
| 199 |
* \brief operator = |
| 200 |
* Assignment operator |
| 201 |
*/ |
| 202 |
ViewProviderWeakPtrT& operator=(ViewProviderDocumentObject* p); |
| 203 |
/*! |
| 204 |
* \brief operator * |
| 205 |
* \return pointer to the document |
| 206 |
*/ |
| 207 |
ViewProviderDocumentObject* operator*() const noexcept; |
| 208 |
/*! |
| 209 |
* \brief operator -> |
| 210 |
* \return pointer to the document |
| 211 |
*/ |
| 212 |
ViewProviderDocumentObject* operator->() const noexcept; |
| 213 |
/*! |
| 214 |
* \brief operator == |
| 215 |
* \return true if both objects are equal, false otherwise |
| 216 |
*/ |
| 217 |
bool operator==(const ViewProviderWeakPtrT& p) const noexcept; |
| 218 |
/*! |
| 219 |
* \brief operator != |
| 220 |
* \return true if both objects are inequal, false otherwise |
| 221 |
*/ |
| 222 |
bool operator!=(const ViewProviderWeakPtrT& p) const noexcept; |
| 223 |
/*! Get a pointer to the object or 0 if it doesn't exist any more or the type doesn't match. */ |
| 224 |
template<typename T> |
| 225 |
inline T* get() const noexcept |
| 226 |
{ |
| 227 |
return freecad_cast<T*>(_get()); |
| 228 |
} |
| 229 |
|
| 230 |
private: |
| 231 |
ViewProviderDocumentObject* _get() const noexcept; |
| 232 |
|
| 233 |
class Private; |
| 234 |
std::unique_ptr<Private> d; |
| 235 |
}; |
| 236 |
|
| 237 |
/** |
| 238 |
* @brief The WeakPtrT class |
| 239 |
*/ |
| 240 |
template<class T> |
| 241 |
class WeakPtrT |
| 242 |
{ |
| 243 |
public: |
| 244 |
explicit WeakPtrT(T* t) |
| 245 |
: ptr(t) |
| 246 |
{} |
| 247 |
~WeakPtrT() = default; |
| 248 |
|
| 249 |
/*! |
| 250 |
* \brief reset |
| 251 |
* Releases the reference to the managed object. After the call *this manages no object. |
| 252 |
*/ |
| 253 |
void reset() |
| 254 |
{ |
| 255 |
ptr.reset(); |
| 256 |
} |
| 257 |
/*! |
| 258 |
* \brief expired |
| 259 |
* \return true if the managed object has already been deleted, false otherwise. |
| 260 |
*/ |
| 261 |
bool expired() const |
| 262 |
{ |
| 263 |
return ptr.expired(); |
| 264 |
} |
| 265 |
/*! |
| 266 |
* \brief operator = |
| 267 |
* Assignment operator |
| 268 |
*/ |
| 269 |
WeakPtrT<T>& operator=(T* p) |
| 270 |
{ |
| 271 |
ptr = p; |
| 272 |
return *this; |
| 273 |
} |
| 274 |
/*! |
| 275 |
* \brief operator * |
| 276 |
* \return pointer to the view provider |
| 277 |
*/ |
| 278 |
T* operator*() const |
| 279 |
{ |
| 280 |
return ptr.get<T>(); |
| 281 |
} |
| 282 |
/*! |
| 283 |
* \brief operator -> |
| 284 |
* \return pointer to the view provider |
| 285 |
*/ |
| 286 |
T* operator->() const |
| 287 |
{ |
| 288 |
return ptr.get<T>(); |
| 289 |
} |
| 290 |
/*! |
| 291 |
* \brief operator == |
| 292 |
* \return true if both objects are equal, false otherwise |
| 293 |
*/ |
| 294 |
bool operator==(const WeakPtrT<T>& p) const |
| 295 |
{ |
| 296 |
return ptr == p.ptr; |
| 297 |
} |
| 298 |
/*! |
| 299 |
* \brief operator != |
| 300 |
* \return true if both objects are inequal, false otherwise |
| 301 |
*/ |
| 302 |
bool operator!=(const WeakPtrT<T>& p) const |
| 303 |
{ |
| 304 |
return ptr != p.ptr; |
| 305 |
} |
| 306 |
/*! Get a pointer to the object or 0 if it doesn't exist any more. */ |
| 307 |
T* get() const noexcept |
| 308 |
{ |
| 309 |
return ptr.get<T>(); |
| 310 |
} |
| 311 |
|
| 312 |
// disable |
| 313 |
WeakPtrT(const WeakPtrT&) = delete; |
| 314 |
WeakPtrT& operator=(const WeakPtrT&) = delete; |
| 315 |
|
| 316 |
private: |
| 317 |
ViewProviderWeakPtrT ptr; |
| 318 |
}; |
| 319 |
|
| 320 |
#ifdef _MSC_VER |
| 321 |
# pragma warning(push) |
| 322 |
# pragma warning(disable : 4251) // MSVC emits warning C4251 too conservatively for our use-case |
| 323 |
#endif |
| 324 |
|
| 325 |
/** |
| 326 |
* The DocumentObserver class simplifies the step to write classes that listen |
| 327 |
* to what happens inside a document. |
| 328 |
* This is very useful for classes that needs to be notified when an observed |
| 329 |
* object has changed. |
| 330 |
* |
| 331 |
* @author Werner Mayer |
| 332 |
*/ |
| 333 |
class GuiExport DocumentObserver |
| 334 |
{ |
| 335 |
public: |
| 336 |
/// Constructor |
| 337 |
DocumentObserver(); |
| 338 |
explicit DocumentObserver(Document*); |
| 339 |
virtual ~DocumentObserver(); |
| 340 |
|
| 341 |
/** Attaches to another document, the old document |
| 342 |
* is not longer observed then. |
| 343 |
*/ |
| 344 |
void attachDocument(Document*); |
| 345 |
/** Detaches from the current document, the document |
| 346 |
* is not longer observed then. |
| 347 |
*/ |
| 348 |
void detachDocument(); |
| 349 |
|
| 350 |
private: |
| 351 |
/** Notifies when an object has been created. */ |
| 352 |
virtual void slotCreatedObject(const ViewProviderDocumentObject& Obj); |
| 353 |
/** Notifies when the object is about to be removed. */ |
| 354 |
virtual void slotDeletedObject(const ViewProviderDocumentObject& Obj); |
| 355 |
/** The property of an observed object has changed */ |
| 356 |
virtual void slotChangedObject(const ViewProviderDocumentObject& Obj, const App::Property& Prop); |
| 357 |
/** Notifies when the object has been relabeled. */ |
| 358 |
virtual void slotRelabelObject(const ViewProviderDocumentObject& Obj); |
| 359 |
/** Notifies when the object has been activated. */ |
| 360 |
virtual void slotActivatedObject(const ViewProviderDocumentObject& Obj); |
| 361 |
/** Notifies when the object entered edit mode. */ |
| 362 |
virtual void slotEnterEditObject(const ViewProviderDocumentObject& Obj); |
| 363 |
/** Notifies when the object resets edit mode. */ |
| 364 |
virtual void slotResetEditObject(const ViewProviderDocumentObject& Obj); |
| 365 |
/** Notifies on undo */ |
| 366 |
virtual void slotUndoDocument(const Document& Doc); |
| 367 |
/** Notifies on redo */ |
| 368 |
virtual void slotRedoDocument(const Document& Doc); |
| 369 |
/** Notifies on deletion */ |
| 370 |
virtual void slotDeleteDocument(const Document& Doc); |
| 371 |
|
| 372 |
private: |
| 373 |
using Connection = boost::signals2::scoped_connection; |
| 374 |
Connection connectDocumentCreatedObject; |
| 375 |
Connection connectDocumentDeletedObject; |
| 376 |
Connection connectDocumentChangedObject; |
| 377 |
Connection connectDocumentRelabelObject; |
| 378 |
Connection connectDocumentActivateObject; |
| 379 |
Connection connectDocumentEditObject; |
| 380 |
Connection connectDocumentResetObject; |
| 381 |
Connection connectDocumentUndo; |
| 382 |
Connection connectDocumentRedo; |
| 383 |
Connection connectDocumentDelete; |
| 384 |
}; |
| 385 |
|
| 386 |
#ifdef _MSC_VER |
| 387 |
# pragma warning(pop) |
| 388 |
#endif |
| 389 |
|
| 390 |
} // namespace Gui |
| 391 |
|
| 392 |
#endif // GUI_DOCUMENTOBSERVER_H |