| 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 |
#include <sstream> |
| 24 |
|
| 25 |
#include <functional> |
| 26 |
|
| 27 |
#include "DocumentObserver.h" |
| 28 |
#include "Application.h" |
| 29 |
#include "Document.h" |
| 30 |
#include "ViewProviderDocumentObject.h" |
| 31 |
#include <App/Document.h> |
| 32 |
|
| 33 |
|
| 34 |
using namespace Gui; |
| 35 |
namespace sp = std::placeholders; |
| 36 |
|
| 37 |
|
| 38 |
DocumentT::DocumentT() = default; |
| 39 |
|
| 40 |
DocumentT::DocumentT(Document* doc) |
| 41 |
{ |
| 42 |
document = doc->getDocument()->getName(); |
| 43 |
} |
| 44 |
|
| 45 |
DocumentT::DocumentT(const std::string& name) |
| 46 |
{ |
| 47 |
document = name; |
| 48 |
} |
| 49 |
|
| 50 |
DocumentT::DocumentT(const DocumentT& doc) |
| 51 |
{ |
| 52 |
document = doc.document; |
| 53 |
} |
| 54 |
|
| 55 |
DocumentT::~DocumentT() = default; |
| 56 |
|
| 57 |
void DocumentT::operator=(const DocumentT& doc) |
| 58 |
{ |
| 59 |
if (this == &doc) { |
| 60 |
return; |
| 61 |
} |
| 62 |
document = doc.document; |
| 63 |
} |
| 64 |
|
| 65 |
void DocumentT::operator=(const Document* doc) |
| 66 |
{ |
| 67 |
document = doc->getDocument()->getName(); |
| 68 |
} |
| 69 |
|
| 70 |
void DocumentT::operator=(const std::string& name) |
| 71 |
{ |
| 72 |
document = name; |
| 73 |
} |
| 74 |
|
| 75 |
Document* DocumentT::getDocument() const |
| 76 |
{ |
| 77 |
return Application::Instance->getDocument(document.c_str()); |
| 78 |
} |
| 79 |
|
| 80 |
std::string DocumentT::getDocumentName() const |
| 81 |
{ |
| 82 |
return document; |
| 83 |
} |
| 84 |
|
| 85 |
std::string DocumentT::getGuiDocumentPython() const |
| 86 |
{ |
| 87 |
std::stringstream str; |
| 88 |
Document* doc = Application::Instance->activeDocument(); |
| 89 |
if (doc && document == doc->getDocument()->getName()) { |
| 90 |
str << "Gui.ActiveDocument"; |
| 91 |
} |
| 92 |
else { |
| 93 |
str << "Gui.getDocument(\"" << document << "\")"; |
| 94 |
} |
| 95 |
return str.str(); |
| 96 |
} |
| 97 |
|
| 98 |
std::string DocumentT::getAppDocumentPython() const |
| 99 |
{ |
| 100 |
std::stringstream str; |
| 101 |
Document* doc = Application::Instance->activeDocument(); |
| 102 |
if (doc && document == doc->getDocument()->getName()) { |
| 103 |
str << "App.ActiveDocument"; |
| 104 |
} |
| 105 |
else { |
| 106 |
str << "App.getDocument(\"" << document << "\")"; |
| 107 |
} |
| 108 |
return str.str(); |
| 109 |
} |
| 110 |
|
| 111 |
// ----------------------------------------------------------------------------- |
| 112 |
|
| 113 |
ViewProviderT::ViewProviderT() = default; |
| 114 |
|
| 115 |
ViewProviderT::ViewProviderT(const ViewProviderT& other) |
| 116 |
{ |
| 117 |
*this = other; |
| 118 |
} |
| 119 |
|
| 120 |
ViewProviderT::ViewProviderT(ViewProviderT&& other) |
| 121 |
{ |
| 122 |
*this = std::move(other); |
| 123 |
} |
| 124 |
|
| 125 |
ViewProviderT::ViewProviderT(const ViewProviderDocumentObject* obj) |
| 126 |
{ |
| 127 |
*this = obj; |
| 128 |
} |
| 129 |
|
| 130 |
ViewProviderT::~ViewProviderT() = default; |
| 131 |
|
| 132 |
ViewProviderT& ViewProviderT::operator=(const ViewProviderT& obj) |
| 133 |
{ |
| 134 |
if (this == &obj) { |
| 135 |
return *this; |
| 136 |
} |
| 137 |
object = obj.object; |
| 138 |
document = obj.document; |
| 139 |
return *this; |
| 140 |
} |
| 141 |
|
| 142 |
ViewProviderT& ViewProviderT::operator=(ViewProviderT&& obj) |
| 143 |
{ |
| 144 |
if (this == &obj) { |
| 145 |
return *this; |
| 146 |
} |
| 147 |
object = std::move(obj.object); |
| 148 |
document = std::move(obj.document); |
| 149 |
return *this; |
| 150 |
} |
| 151 |
|
| 152 |
void ViewProviderT::operator=(const ViewProviderDocumentObject* obj) |
| 153 |
{ |
| 154 |
if (!obj) { |
| 155 |
object.clear(); |
| 156 |
document.clear(); |
| 157 |
} |
| 158 |
else { |
| 159 |
object = obj->getObject()->getNameInDocument(); |
| 160 |
document = obj->getObject()->getDocument()->getName(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
bool ViewProviderT::operator==(const ViewProviderT& other) const |
| 165 |
{ |
| 166 |
return document == other.document && object == other.object; |
| 167 |
} |
| 168 |
|
| 169 |
Document* ViewProviderT::getDocument() const |
| 170 |
{ |
| 171 |
return Application::Instance->getDocument(document.c_str()); |
| 172 |
} |
| 173 |
|
| 174 |
const std::string& ViewProviderT::getDocumentName() const |
| 175 |
{ |
| 176 |
return document; |
| 177 |
} |
| 178 |
|
| 179 |
std::string ViewProviderT::getGuiDocumentPython() const |
| 180 |
{ |
| 181 |
DocumentT doct(document); |
| 182 |
return doct.getGuiDocumentPython(); |
| 183 |
} |
| 184 |
|
| 185 |
std::string ViewProviderT::getAppDocumentPython() const |
| 186 |
{ |
| 187 |
DocumentT doct(document); |
| 188 |
return doct.getAppDocumentPython(); |
| 189 |
} |
| 190 |
|
| 191 |
ViewProviderDocumentObject* ViewProviderT::getViewProvider() const |
| 192 |
{ |
| 193 |
ViewProviderDocumentObject* obj = nullptr; |
| 194 |
Document* doc = getDocument(); |
| 195 |
if (doc) { |
| 196 |
obj = freecad_cast<ViewProviderDocumentObject*>(doc->getViewProviderByName(object.c_str())); |
| 197 |
} |
| 198 |
return obj; |
| 199 |
} |
| 200 |
|
| 201 |
const std::string& ViewProviderT::getObjectName() const |
| 202 |
{ |
| 203 |
return object; |
| 204 |
} |
| 205 |
|
| 206 |
std::string ViewProviderT::getObjectPython() const |
| 207 |
{ |
| 208 |
std::stringstream str; |
| 209 |
Document* doc = Application::Instance->activeDocument(); |
| 210 |
if (doc && document == doc->getDocument()->getName()) { |
| 211 |
str << "Gui.ActiveDocument."; |
| 212 |
} |
| 213 |
else { |
| 214 |
str << "Gui.getDocument(\"" << document << "\")."; |
| 215 |
} |
| 216 |
|
| 217 |
str << object; |
| 218 |
return str.str(); |
| 219 |
} |
| 220 |
|
| 221 |
// ----------------------------------------------------------------------------- |
| 222 |
|
| 223 |
class DocumentWeakPtrT::Private |
| 224 |
{ |
| 225 |
public: |
| 226 |
Private(Gui::Document* doc) |
| 227 |
: _document(doc) |
| 228 |
{ |
| 229 |
if (doc) { |
| 230 |
// NOLINTBEGIN |
| 231 |
connectApplicationDeletedDocument = doc->signalDeleteDocument.connect( |
| 232 |
std::bind(&Private::deletedDocument, this, sp::_1) |
| 233 |
); |
| 234 |
// NOLINTEND |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
void deletedDocument(const Gui::Document& doc) |
| 239 |
{ |
| 240 |
if (_document == &doc) { |
| 241 |
reset(); |
| 242 |
} |
| 243 |
} |
| 244 |
void reset() |
| 245 |
{ |
| 246 |
connectApplicationDeletedDocument.disconnect(); |
| 247 |
_document = nullptr; |
| 248 |
} |
| 249 |
|
| 250 |
Gui::Document* _document; |
| 251 |
using Connection = boost::signals2::scoped_connection; |
| 252 |
Connection connectApplicationDeletedDocument; |
| 253 |
}; |
| 254 |
|
| 255 |
DocumentWeakPtrT::DocumentWeakPtrT(Gui::Document* doc) noexcept |
| 256 |
: d(new Private(doc)) |
| 257 |
{} |
| 258 |
|
| 259 |
DocumentWeakPtrT::~DocumentWeakPtrT() = default; |
| 260 |
|
| 261 |
void DocumentWeakPtrT::reset() noexcept |
| 262 |
{ |
| 263 |
d->reset(); |
| 264 |
} |
| 265 |
|
| 266 |
bool DocumentWeakPtrT::expired() const noexcept |
| 267 |
{ |
| 268 |
return (d->_document == nullptr); |
| 269 |
} |
| 270 |
|
| 271 |
Gui::Document* DocumentWeakPtrT::operator*() const noexcept |
| 272 |
{ |
| 273 |
return d->_document; |
| 274 |
} |
| 275 |
|
| 276 |
Gui::Document* DocumentWeakPtrT::operator->() const noexcept |
| 277 |
{ |
| 278 |
return d->_document; |
| 279 |
} |
| 280 |
|
| 281 |
// ----------------------------------------------------------------------------- |
| 282 |
|
| 283 |
class ViewProviderWeakPtrT::Private |
| 284 |
{ |
| 285 |
public: |
| 286 |
Private(ViewProviderDocumentObject* obj) |
| 287 |
: object(obj) |
| 288 |
{ |
| 289 |
set(obj); |
| 290 |
} |
| 291 |
void deletedDocument(const Gui::Document& doc) |
| 292 |
{ |
| 293 |
// When deleting document then there is no way to undo it |
| 294 |
if (object && object->getDocument() == &doc) { |
| 295 |
reset(); |
| 296 |
} |
| 297 |
} |
| 298 |
void createdObject(const Gui::ViewProvider& obj) noexcept |
| 299 |
{ |
| 300 |
// When undoing the removal |
| 301 |
if (object == &obj) { |
| 302 |
indocument = true; |
| 303 |
} |
| 304 |
} |
| 305 |
void deletedObject(const Gui::ViewProvider& obj) noexcept |
| 306 |
{ |
| 307 |
if (object == &obj) { |
| 308 |
indocument = false; |
| 309 |
} |
| 310 |
} |
| 311 |
void reset() |
| 312 |
{ |
| 313 |
connectApplicationDeletedDocument.disconnect(); |
| 314 |
connectDocumentCreatedObject.disconnect(); |
| 315 |
connectDocumentDeletedObject.disconnect(); |
| 316 |
object = nullptr; |
| 317 |
indocument = false; |
| 318 |
} |
| 319 |
void set(ViewProviderDocumentObject* obj) |
| 320 |
{ |
| 321 |
object = obj; |
| 322 |
try { |
| 323 |
if (obj) { |
| 324 |
// NOLINTBEGIN |
| 325 |
Gui::Document* doc = obj->getDocument(); |
| 326 |
indocument = true; |
| 327 |
connectApplicationDeletedDocument = doc->signalDeleteDocument.connect( |
| 328 |
std::bind(&Private::deletedDocument, this, sp::_1) |
| 329 |
); |
| 330 |
connectDocumentCreatedObject = doc->signalNewObject.connect( |
| 331 |
std::bind(&Private::createdObject, this, sp::_1) |
| 332 |
); |
| 333 |
connectDocumentDeletedObject = doc->signalDeletedObject.connect( |
| 334 |
std::bind(&Private::deletedObject, this, sp::_1) |
| 335 |
); |
| 336 |
// NOLINTEND |
| 337 |
} |
| 338 |
} |
| 339 |
catch (const Base::RuntimeError&) { |
| 340 |
// getDocument() may raise an exception |
| 341 |
object = nullptr; |
| 342 |
indocument = false; |
| 343 |
} |
| 344 |
} |
| 345 |
ViewProviderDocumentObject* get() const |
| 346 |
{ |
| 347 |
return indocument ? object : nullptr; |
| 348 |
} |
| 349 |
|
| 350 |
Gui::ViewProviderDocumentObject* object; |
| 351 |
bool indocument {false}; |
| 352 |
using Connection = boost::signals2::scoped_connection; |
| 353 |
Connection connectApplicationDeletedDocument; |
| 354 |
Connection connectDocumentCreatedObject; |
| 355 |
Connection connectDocumentDeletedObject; |
| 356 |
}; |
| 357 |
|
| 358 |
ViewProviderWeakPtrT::ViewProviderWeakPtrT(ViewProviderDocumentObject* obj) |
| 359 |
: d(new Private(obj)) |
| 360 |
{} |
| 361 |
|
| 362 |
ViewProviderWeakPtrT::ViewProviderWeakPtrT(ViewProviderWeakPtrT&&) = default; |
| 363 |
|
| 364 |
ViewProviderWeakPtrT& ViewProviderWeakPtrT::operator=(ViewProviderWeakPtrT&&) = default; |
| 365 |
|
| 366 |
ViewProviderWeakPtrT::~ViewProviderWeakPtrT() = default; |
| 367 |
|
| 368 |
ViewProviderDocumentObject* ViewProviderWeakPtrT::_get() const noexcept |
| 369 |
{ |
| 370 |
return d->get(); |
| 371 |
} |
| 372 |
|
| 373 |
void ViewProviderWeakPtrT::reset() |
| 374 |
{ |
| 375 |
d->reset(); |
| 376 |
} |
| 377 |
|
| 378 |
bool ViewProviderWeakPtrT::expired() const noexcept |
| 379 |
{ |
| 380 |
return !d->indocument; |
| 381 |
} |
| 382 |
|
| 383 |
ViewProviderWeakPtrT& ViewProviderWeakPtrT::operator=(ViewProviderDocumentObject* p) |
| 384 |
{ |
| 385 |
d->reset(); |
| 386 |
d->set(p); |
| 387 |
return *this; |
| 388 |
} |
| 389 |
|
| 390 |
ViewProviderDocumentObject* ViewProviderWeakPtrT::operator*() const noexcept |
| 391 |
{ |
| 392 |
return d->get(); |
| 393 |
} |
| 394 |
|
| 395 |
ViewProviderDocumentObject* ViewProviderWeakPtrT::operator->() const noexcept |
| 396 |
{ |
| 397 |
return d->get(); |
| 398 |
} |
| 399 |
|
| 400 |
bool ViewProviderWeakPtrT::operator==(const ViewProviderWeakPtrT& p) const noexcept |
| 401 |
{ |
| 402 |
return d->get() == p.d->get(); |
| 403 |
} |
| 404 |
|
| 405 |
bool ViewProviderWeakPtrT::operator!=(const ViewProviderWeakPtrT& p) const noexcept |
| 406 |
{ |
| 407 |
return d->get() != p.d->get(); |
| 408 |
} |
| 409 |
|
| 410 |
// ----------------------------------------------------------------------------- |
| 411 |
|
| 412 |
DocumentObserver::DocumentObserver() = default; |
| 413 |
|
| 414 |
DocumentObserver::DocumentObserver(Document* doc) |
| 415 |
{ |
| 416 |
attachDocument(doc); |
| 417 |
} |
| 418 |
|
| 419 |
DocumentObserver::~DocumentObserver() = default; |
| 420 |
|
| 421 |
void DocumentObserver::attachDocument(Document* doc) |
| 422 |
{ |
| 423 |
detachDocument(); |
| 424 |
|
| 425 |
if (!doc) { |
| 426 |
return; |
| 427 |
} |
| 428 |
|
| 429 |
// NOLINTBEGIN |
| 430 |
this->connectDocumentCreatedObject = doc->signalNewObject.connect( |
| 431 |
std::bind(&DocumentObserver::slotCreatedObject, this, sp::_1) |
| 432 |
); |
| 433 |
this->connectDocumentDeletedObject = doc->signalDeletedObject.connect( |
| 434 |
std::bind(&DocumentObserver::slotDeletedObject, this, sp::_1) |
| 435 |
); |
| 436 |
this->connectDocumentChangedObject = doc->signalChangedObject.connect( |
| 437 |
std::bind(&DocumentObserver::slotChangedObject, this, sp::_1, sp::_2) |
| 438 |
); |
| 439 |
this->connectDocumentRelabelObject = doc->signalRelabelObject.connect( |
| 440 |
std::bind(&DocumentObserver::slotRelabelObject, this, sp::_1) |
| 441 |
); |
| 442 |
this->connectDocumentActivateObject = doc->signalActivatedObject.connect( |
| 443 |
std::bind(&DocumentObserver::slotActivatedObject, this, sp::_1) |
| 444 |
); |
| 445 |
this->connectDocumentEditObject = doc->signalInEdit.connect( |
| 446 |
std::bind(&DocumentObserver::slotEnterEditObject, this, sp::_1) |
| 447 |
); |
| 448 |
this->connectDocumentResetObject = doc->signalResetEdit.connect( |
| 449 |
std::bind(&DocumentObserver::slotResetEditObject, this, sp::_1) |
| 450 |
); |
| 451 |
this->connectDocumentUndo = doc->signalUndoDocument.connect( |
| 452 |
std::bind(&DocumentObserver::slotUndoDocument, this, sp::_1) |
| 453 |
); |
| 454 |
this->connectDocumentRedo = doc->signalRedoDocument.connect( |
| 455 |
std::bind(&DocumentObserver::slotRedoDocument, this, sp::_1) |
| 456 |
); |
| 457 |
this->connectDocumentDelete = doc->signalDeleteDocument.connect( |
| 458 |
std::bind(&DocumentObserver::slotDeleteDocument, this, sp::_1) |
| 459 |
); |
| 460 |
// NOLINTEND |
| 461 |
} |
| 462 |
|
| 463 |
void DocumentObserver::detachDocument() |
| 464 |
{ |
| 465 |
this->connectDocumentCreatedObject.disconnect(); |
| 466 |
this->connectDocumentDeletedObject.disconnect(); |
| 467 |
this->connectDocumentChangedObject.disconnect(); |
| 468 |
this->connectDocumentRelabelObject.disconnect(); |
| 469 |
this->connectDocumentActivateObject.disconnect(); |
| 470 |
this->connectDocumentEditObject.disconnect(); |
| 471 |
this->connectDocumentResetObject.disconnect(); |
| 472 |
this->connectDocumentUndo.disconnect(); |
| 473 |
this->connectDocumentRedo.disconnect(); |
| 474 |
this->connectDocumentDelete.disconnect(); |
| 475 |
} |
| 476 |
|
| 477 |
void DocumentObserver::slotUndoDocument(const Document& /*Doc*/) |
| 478 |
{} |
| 479 |
|
| 480 |
void DocumentObserver::slotRedoDocument(const Document& /*Doc*/) |
| 481 |
{} |
| 482 |
|
| 483 |
void DocumentObserver::slotDeleteDocument(const Document& /*Doc*/) |
| 484 |
{} |
| 485 |
|
| 486 |
void DocumentObserver::slotCreatedObject(const ViewProviderDocumentObject& /*Obj*/) |
| 487 |
{} |
| 488 |
|
| 489 |
void DocumentObserver::slotDeletedObject(const ViewProviderDocumentObject& /*Obj*/) |
| 490 |
{} |
| 491 |
|
| 492 |
void DocumentObserver::slotChangedObject(const ViewProviderDocumentObject& /*Obj*/, const App::Property& /*Prop*/) |
| 493 |
{} |
| 494 |
|
| 495 |
void DocumentObserver::slotRelabelObject(const ViewProviderDocumentObject& /*Obj*/) |
| 496 |
{} |
| 497 |
|
| 498 |
void DocumentObserver::slotActivatedObject(const ViewProviderDocumentObject& /*Obj*/) |
| 499 |
{} |
| 500 |
|
| 501 |
void DocumentObserver::slotEnterEditObject(const ViewProviderDocumentObject& /*Obj*/) |
| 502 |
{} |
| 503 |
|
| 504 |
void DocumentObserver::slotResetEditObject(const ViewProviderDocumentObject& /*Obj*/) |
| 505 |
{} |