DocumentObserver.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 #endif
00028 
00029 #include <boost/signals.hpp>
00030 #include <boost/bind.hpp>
00031 
00032 #include "Application.h"
00033 #include "Document.h"
00034 #include "DocumentObject.h"
00035 #include "DocumentObserver.h"
00036 
00037 using namespace App;
00038 
00039 DocumentObserver::DocumentObserver() : _document(0)
00040 {
00041     this->connectApplicationCreatedDocument = App::GetApplication().signalNewDocument.connect(boost::bind
00042         (&DocumentObserver::slotCreatedDocument, this, _1));
00043     this->connectApplicationDeletedDocument = App::GetApplication().signalDeleteDocument.connect(boost::bind
00044         (&DocumentObserver::slotDeletedDocument, this, _1));
00045 }
00046 
00047 DocumentObserver::DocumentObserver(Document* doc) : _document(0)
00048 {
00049     // Connect to application and given document
00050     this->connectApplicationCreatedDocument = App::GetApplication().signalNewDocument.connect(boost::bind
00051         (&DocumentObserver::slotCreatedDocument, this, _1));
00052     this->connectApplicationDeletedDocument = App::GetApplication().signalDeleteDocument.connect(boost::bind
00053         (&DocumentObserver::slotDeletedDocument, this, _1));
00054     attachDocument(doc);
00055 }
00056 
00057 DocumentObserver::~DocumentObserver()
00058 {
00059     // disconnect from application and document
00060     this->connectApplicationCreatedDocument.disconnect();
00061     this->connectApplicationDeletedDocument.disconnect();
00062     detachDocument();
00063 }
00064 
00065 Document* DocumentObserver::getDocument() const
00066 {
00067     return this->_document;
00068 }
00069 
00070 void DocumentObserver::attachDocument(Document* doc)
00071 {
00072     if (_document != doc) {
00073         detachDocument();
00074         _document = doc;
00075 
00076         this->connectDocumentCreatedObject = _document->signalNewObject.connect(boost::bind
00077             (&DocumentObserver::slotCreatedObject, this, _1));
00078         this->connectDocumentDeletedObject = _document->signalDeletedObject.connect(boost::bind
00079             (&DocumentObserver::slotDeletedObject, this, _1));
00080         this->connectDocumentChangedObject = _document->signalChangedObject.connect(boost::bind
00081             (&DocumentObserver::slotChangedObject, this, _1, _2));
00082     }
00083 }
00084 
00085 void DocumentObserver::detachDocument()
00086 {
00087     if (this->_document) {
00088         this->_document = 0;
00089         this->connectDocumentCreatedObject.disconnect();
00090         this->connectDocumentDeletedObject.disconnect();
00091         this->connectDocumentChangedObject.disconnect();
00092     }
00093 }
00094 
00095 // -----------------------------------------------------------------------------
00096 
00097 DocumentObjectObserver::DocumentObjectObserver()
00098 {
00099 }
00100 
00101 DocumentObjectObserver::~DocumentObjectObserver()
00102 {
00103 }
00104 
00105 DocumentObjectObserver::const_iterator DocumentObjectObserver::begin() const
00106 {
00107     return _objects.begin();
00108 }
00109 
00110 DocumentObjectObserver::const_iterator DocumentObjectObserver::end() const
00111 {
00112     return _objects.end();
00113 }
00114 
00115 void DocumentObjectObserver::addToObservation(App::DocumentObject* obj)
00116 {
00117     _objects.insert(obj);
00118 }
00119 
00120 void DocumentObjectObserver::removeFromObservation(App::DocumentObject* obj)
00121 {
00122     _objects.erase(obj);
00123 }
00124 
00125 void DocumentObjectObserver::slotCreatedDocument(const App::Document&)
00126 {
00127 }
00128 
00129 void DocumentObjectObserver::slotDeletedDocument(const App::Document& Doc)
00130 {
00131     if (this->getDocument() == &Doc) {
00132         this->detachDocument();
00133         _objects.clear();
00134         cancelObservation();
00135     }
00136 }
00137 
00138 void DocumentObjectObserver::slotCreatedObject(const App::DocumentObject&)
00139 {
00140 }
00141 
00142 void DocumentObjectObserver::slotDeletedObject(const App::DocumentObject& Obj)
00143 {
00144     std::set<App::DocumentObject*>::iterator it = _objects.find
00145         (const_cast<App::DocumentObject*>(&Obj));
00146     if (it != _objects.end())
00147         _objects.erase(it);
00148     if (_objects.empty())
00149         cancelObservation();
00150 }
00151 
00152 void DocumentObjectObserver::slotChangedObject(const App::DocumentObject&,
00153                                                const App::Property&)
00154 {
00155 }

Generated on Wed Nov 23 19:00:08 2011 for FreeCAD by  doxygen 1.6.1