Skip to content

Commit

Permalink
add observer for GUI documents
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jan 22, 2017
1 parent 72c7a8a commit bc84e88
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Gui/Application.cpp
Expand Up @@ -230,10 +230,10 @@ struct PyMethodDef FreeCADGui_methods[] = {

Gui::MDIView* Application::activeView(void) const
{
if (activeDocument())
return activeDocument()->getActiveView();
else
return NULL;
if (activeDocument())
return activeDocument()->getActiveView();
else
return NULL;
}

} // namespace Gui
Expand Down
2 changes: 2 additions & 0 deletions src/Gui/CMakeLists.txt
Expand Up @@ -1070,6 +1070,7 @@ SET(FreeCADGui_CPP_SRCS
Document.cpp
DocumentModel.cpp
DocumentPyImp.cpp
DocumentObserver.cpp
ExpressionBinding.cpp
GraphicsViewZoom.cpp
ExpressionCompleter.cpp
Expand All @@ -1094,6 +1095,7 @@ SET(FreeCADGui_SRCS
BitmapFactory.h
Document.h
DocumentModel.h
DocumentObserver.h
ExpressionBinding.cpp
ExpressionCompleter.h
FreeCADGuiInit.py
Expand Down
137 changes: 137 additions & 0 deletions src/Gui/DocumentObserver.cpp
@@ -0,0 +1,137 @@
/***************************************************************************
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/


#include "PreCompiled.h"

#ifndef _PreComp_
# include <sstream>
#endif

#include <boost/signals.hpp>
#include <boost/bind.hpp>

#include "Application.h"
#include "Document.h"
#include "ViewProviderDocumentObject.h"
#include "DocumentObserver.h"

using namespace Gui;


DocumentObserver::DocumentObserver()
{
}

DocumentObserver::~DocumentObserver()
{
}

void DocumentObserver::attachDocument(Document* doc)
{
detachDocument();

if (doc == nullptr)
return;

this->connectDocumentCreatedObject = doc->signalNewObject.connect(boost::bind
(&DocumentObserver::slotCreatedObject, this, _1));
this->connectDocumentDeletedObject = doc->signalDeletedObject.connect(boost::bind
(&DocumentObserver::slotDeletedObject, this, _1));
this->connectDocumentChangedObject = doc->signalChangedObject.connect(boost::bind
(&DocumentObserver::slotChangedObject, this, _1, _2));
this->connectDocumentRelabelObject = doc->signalRelabelObject.connect(boost::bind
(&DocumentObserver::slotRelabelObject, this, _1));
this->connectDocumentActivateObject = doc->signalActivatedObject.connect(boost::bind
(&DocumentObserver::slotActivatedObject, this, _1));
this->connectDocumentEditObject = doc->signalInEdit.connect(boost::bind
(&DocumentObserver::slotEnterEditObject, this, _1));
this->connectDocumentResetObject = doc->signalResetEdit.connect(boost::bind
(&DocumentObserver::slotResetEditObject, this, _1));
this->connectDocumentUndo = doc->signalUndoDocument.connect(boost::bind
(&DocumentObserver::slotUndoDocument, this, _1));
this->connectDocumentRedo = doc->signalRedoDocument.connect(boost::bind
(&DocumentObserver::slotRedoDocument, this, _1));
}

void DocumentObserver::detachDocument()
{
this->connectDocumentCreatedObject.disconnect();
this->connectDocumentDeletedObject.disconnect();
this->connectDocumentChangedObject.disconnect();
this->connectDocumentRelabelObject.disconnect();
this->connectDocumentActivateObject.disconnect();
this->connectDocumentEditObject.disconnect();
this->connectDocumentResetObject.disconnect();
this->connectDocumentUndo.disconnect();
this->connectDocumentRedo.disconnect();
}

void DocumentObserver::enableNotifications(DocumentObserver::Notifications value)
{
this->connectDocumentCreatedObject.block(!(value & Create));
this->connectDocumentDeletedObject.block(!(value & Delete));
this->connectDocumentChangedObject.block(!(value & Change));
this->connectDocumentRelabelObject.block(!(value & Relabel));
this->connectDocumentActivateObject.block(!(value & Activate));
this->connectDocumentEditObject.block(!(value & Edit));
this->connectDocumentResetObject.block(!(value & Reset));
this->connectDocumentUndo.block(!(value & Undo));
this->connectDocumentRedo.block(!(value & Redo));
}

void DocumentObserver::slotUndoDocument(const Document& /*Doc*/)
{
}

void DocumentObserver::slotRedoDocument(const Document& /*Doc*/)
{
}

void DocumentObserver::slotCreatedObject(const ViewProviderDocumentObject& /*Obj*/)
{
}

void DocumentObserver::slotDeletedObject(const ViewProviderDocumentObject& /*Obj*/)
{
}

void DocumentObserver::slotChangedObject(const ViewProviderDocumentObject& /*Obj*/,
const App::Property& /*Prop*/)
{
}

void DocumentObserver::slotRelabelObject(const ViewProviderDocumentObject& /*Obj*/)
{
}

void DocumentObserver::slotActivatedObject(const ViewProviderDocumentObject& /*Obj*/)
{
}

void DocumentObserver::slotEnterEditObject(const ViewProviderDocumentObject& /*Obj*/)
{
}

void DocumentObserver::slotResetEditObject(const ViewProviderDocumentObject& /*Obj*/)
{
}
115 changes: 115 additions & 0 deletions src/Gui/DocumentObserver.h
@@ -0,0 +1,115 @@
/***************************************************************************
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/


#ifndef GUI_DOCUMENTOBSERVER_H
#define GUI_DOCUMENTOBSERVER_H

#include <Base/BaseClass.h>
#include <boost/signals.hpp>
#include <QFlags>

namespace App { class Property; }
namespace Gui
{
class Document;
class ViewProviderDocumentObject;

/**
* The DocumentObserver class simplifies the step to write classes that listen
* to what happens inside a document.
* This is very useful for classes that needs to be notified when an observed
* object has changed.
*
* @author Werner Mayer
*/
class GuiExport DocumentObserver
{
public:
enum Notification {
None = 0x0000,
Create = 0x0001,
Delete = 0x0002,
Change = 0x0004,
Relabel = 0x0008,
Activate = 0x0010,
Edit = 0x0020,
Reset = 0x0040,
Undo = 0x0080,
Redo = 0x0100,
All = 0x01ff
};
Q_DECLARE_FLAGS(Notifications, Notification)

/// Constructor
DocumentObserver();
virtual ~DocumentObserver();

/** Attaches to another document, the old document
* is not longer observed then.
*/
void attachDocument(Document*);
/** Detaches from the current document, the document
* is not longer observed then.
*/
void detachDocument();
/** Activates the connection depending on the given value.
*/
void enableNotifications(Notifications value);

private:
/** Notifies when an object has been created. */
virtual void slotCreatedObject(const ViewProviderDocumentObject& Obj);
/** Notifies when the object is about to be removed. */
virtual void slotDeletedObject(const ViewProviderDocumentObject& Obj);
/** The property of an observed object has changed */
virtual void slotChangedObject(const ViewProviderDocumentObject& Obj,
const App::Property& Prop);
/** Notifies when the object has been relabeled. */
virtual void slotRelabelObject(const ViewProviderDocumentObject& Obj);
/** Notifies when the object has been activated. */
virtual void slotActivatedObject(const ViewProviderDocumentObject& Obj);
/** Notifies when the object entered edit mode. */
virtual void slotEnterEditObject(const ViewProviderDocumentObject& Obj);
/** Notifies when the object resets edit mode. */
virtual void slotResetEditObject(const ViewProviderDocumentObject& Obj);
/** Notifies on undo */
virtual void slotUndoDocument(const Document& Doc);
/** Notifies on redo */
virtual void slotRedoDocument(const Document& Doc);

private:
typedef boost::BOOST_SIGNALS_NAMESPACE::scoped_connection Connection;
Connection connectDocumentCreatedObject;
Connection connectDocumentDeletedObject;
Connection connectDocumentChangedObject;
Connection connectDocumentRelabelObject;
Connection connectDocumentActivateObject;
Connection connectDocumentEditObject;
Connection connectDocumentResetObject;
Connection connectDocumentUndo;
Connection connectDocumentRedo;
};

} //namespace Gui

#endif // GUI_DOCUMENTOBSERVER_H

0 comments on commit bc84e88

Please sign in to comment.