Selection.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Juergen Riegel         <juergen.riegel@web.de>          *
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 #ifndef GUI_SELECTION_H
00025 #define GUI_SELECTION_H
00026 
00027 // Std. configurations
00028 
00029 #include <string>
00030 #include <vector>
00031 #include <list>
00032 #include <map>
00033 #include <CXX/Objects.hxx>
00034 
00035 #include <Base/Observer.h>
00036 #include <Base/Type.h>
00037 #include <App/Document.h>
00038 #include <App/DocumentObject.h>
00039 
00040 #include <Gui/SelectionObject.h>
00041 
00042 namespace App
00043 {
00044   class DocumentObject;
00045   class Document;
00046 }
00047 
00048 namespace Gui
00049 {
00050 
00051     class SelectionFilter;
00052 
00062 class GuiExport SelectionChanges
00063 {
00064 public:
00065     enum MsgType {
00066         AddSelection,
00067         RmvSelection,
00068         SetSelection,
00069         ClrSelection,
00070         SetPreselect,
00071         RmvPreselect
00072     };
00073 
00074     MsgType Type;
00075 
00076     const char* pDocName;
00077     const char* pObjectName;
00078     const char* pSubName;
00079     float x;
00080     float y;
00081     float z;
00082 };
00083 
00084 } //namespace Gui
00085 
00086 
00087 
00088 // Export an instance of the base class (to avoid warning C4275, see also 
00089 // C++ Language Reference/General Rules and Limitations on MSDN for more details.)
00090 //
00091 // For compiler gcc4.1 we need to define the template class outside namespace 'Gui'
00092 // otherwise we get the compiler error: 
00093 // 'explicit instantiation of 'class Base::Subject<const Gui::SelectionChanges&>'
00094 // in namespace 'Gui' (which does not enclose namespace 'Base')
00095 // 
00096 // It seems that this costruct is not longer needed for gcc4.4 and even leads to
00097 // errors under Mac OS X. Thus, we check for version between 4.1 and 4.4.
00098 // It seems that for Mac OS X this can be completely ignored
00099 
00100 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && !defined(FC_OS_MACOSX)
00101 #define GNUC_VERSION (((__GNUC__)<<16)+((__GNUC_MINOR__)<<8))
00102 #if GNUC_VERSION >= 0x040100 && GNUC_VERSION < 0x040400
00103 template class GuiExport Base::Subject<const Gui::SelectionChanges&>;
00104 #endif
00105 #undef GNUC_VERSION
00106 #endif
00107 
00108 namespace Gui
00109 {
00110 
00117 class GuiExport SelectionObserver
00118 {
00119 
00120 public:
00122     SelectionObserver();
00123     virtual ~SelectionObserver();
00124     bool blockConnection(bool block);
00125     bool isConnectionBlocked() const;
00126 
00128     void attachSelection();
00130     void detachSelection();
00131 
00132 private:
00133     virtual void onSelectionChanged(const SelectionChanges& msg) = 0;
00134 
00135 private:
00136     typedef boost::signals::connection Connection;
00137     Connection connectSelection;
00138 };
00139 
00147 class GuiExport SelectionObserverPython : public SelectionObserver
00148 {
00149 
00150 public:
00152     SelectionObserverPython(const Py::Object& obj);
00153     virtual ~SelectionObserverPython();
00154 
00155     static void addObserver(const Py::Object& obj);
00156     static void removeObserver(const Py::Object& obj);
00157 
00158 private:
00159     void onSelectionChanged(const SelectionChanges& msg);
00160     void addSelection(const SelectionChanges&);
00161     void removeSelection(const SelectionChanges&);
00162     void setSelection(const SelectionChanges&);
00163     void clearSelection(const SelectionChanges&);
00164     void setPreselection(const SelectionChanges&);
00165     void removePreselection(const SelectionChanges&);
00166 
00167 private:
00168     Py::Object inst;
00169     static std::vector<SelectionObserverPython*> _instances;
00170 };
00171 
00176 class SelectionGate 
00177 {
00178 public:
00179     virtual bool allow(App::Document*,App::DocumentObject*, const char*)=0;
00180 };
00181 
00182 
00198 class GuiExport SelectionSingleton : public Base::Subject<const SelectionChanges&>
00199 {
00200 public:
00202     bool addSelection(const char* pDocName, const char* pObjectName=0, const char* pSubName=0, float x=0, float y=0, float z=0);
00204     void rmvSelection(const char* pDocName, const char* pObjectName=0, const char* pSubName=0);
00206     void setSelection(const char* pDocName, const std::vector<App::DocumentObject*>&);
00208     void clearSelection(const char* pDocName=0);
00210     void clearCompleteSelection();
00212     bool isSelected(const char* pDocName, const char* pObjectName=0, const char* pSubName=0) const;
00214     bool isSelected(App::DocumentObject*, const char* pSubName=0) const;
00215 
00217     bool setPreselect(const char* pDocName, const char* pObjectName, const char* pSubName, float x=0, float y=0, float z=0);
00219     void rmvPreselect();
00221     void setPreselectCoord(float x, float y, float z);
00223     const SelectionChanges& getPreselection(void) const;
00225     void addSelectionGate(Gui::SelectionGate *gate);
00227     void rmvSelectionGate(void);
00228 
00229 
00236     unsigned int countObjectsOfType(const Base::Type& typeId, const char* pDocName=0) const;
00237 
00242     unsigned int countObjectsOfType(const char* typeName, const char* pDocName=0) const;
00243 
00249     std::vector<App::DocumentObject*> getObjectsOfType(const Base::Type& typeId, const char* pDocName=0) const;
00250 
00255     std::vector<App::DocumentObject*> getObjectsOfType(const char* typeName, const char* pDocName=0) const;
00259     template<typename T> inline std::vector<T*> getObjectsOfType(const char* pDocName=0) const;
00260 
00261     struct SelObj {
00262         const char* DocName;
00263         const char* FeatName;
00264         const char* SubName;
00265         const char* TypeName;
00266         App::Document* pDoc;
00267         App::DocumentObject*  pObject;
00268         float x,y,z;
00269     };
00270 
00272     boost::signal<void (const SelectionChanges& msg)> signalSelectionChanged;
00273 
00279     std::vector<SelObj> getSelection(const char* pDocName=0) const;
00285     std::vector<Gui::SelectionObject> getSelectionEx(const char* pDocName=0,Base::Type typeId=App::DocumentObject::getClassTypeId()) const;
00286 
00288     std::vector<SelObj> getCompleteSelection() const;
00289     bool hasSelection() const;
00290     bool hasSelection(const char* doc) const;
00291 
00293     unsigned int size(void) const {
00294         return static_cast<unsigned int>(_SelList.size());
00295     }
00296 
00297     static SelectionSingleton& instance(void);
00298     static void destruct (void);
00299     friend class SelectionFilter;
00300 
00301     // Python interface
00302     static PyMethodDef    Methods[];
00303 
00304 protected:
00305     static PyObject *sAddSelection        (PyObject *self,PyObject *args,PyObject *kwd);
00306     static PyObject *sRemoveSelection     (PyObject *self,PyObject *args,PyObject *kwd);
00307     static PyObject *sClearSelection      (PyObject *self,PyObject *args,PyObject *kwd);
00308     static PyObject *sIsSelected          (PyObject *self,PyObject *args,PyObject *kwd);
00309     static PyObject *sGetSelection        (PyObject *self,PyObject *args,PyObject *kwd);
00310     static PyObject *sGetSelectionEx      (PyObject *self,PyObject *args,PyObject *kwd);
00311     static PyObject *sAddSelObserver      (PyObject *self,PyObject *args,PyObject *kwd);
00312     static PyObject *sRemSelObserver      (PyObject *self,PyObject *args,PyObject *kwd);
00313     static PyObject *saddSelectionGate    (PyObject *self,PyObject *args,PyObject *kwd);
00314     static PyObject *sremoveSelectionGate (PyObject *self,PyObject *args,PyObject *kwd);
00315 
00316 protected:
00318     SelectionSingleton();
00320     virtual ~SelectionSingleton();
00321 
00323     void slotRenamedObject(const App::DocumentObject&);
00324     void slotDeletedObject(const App::DocumentObject&);
00325 
00327     App::Document* getDocument(const char* pDocName=0) const;
00328 
00329     SelectionChanges CurrentPreselection;
00330 
00331     struct _SelObj {
00332         std::string DocName;
00333         std::string FeatName;
00334         std::string SubName;
00335         std::string TypeName;
00336         App::Document* pDoc;
00337         App::DocumentObject* pObject;
00338         float x,y,z;
00339     };
00340     std::list<_SelObj> _SelList;
00341 
00342     static SelectionSingleton* _pcSingleton;
00343 
00344     std::string DocName;
00345     std::string FeatName;
00346     std::string SubName;
00347     float hx,hy,hz;
00348 
00349     Gui::SelectionGate *ActiveGate;
00350 };
00351 
00355 template<typename T>
00356 inline std::vector<T*> SelectionSingleton::getObjectsOfType(const char* pDocName) const
00357 {
00358     std::vector<T*> type;
00359     std::vector<App::DocumentObject*> obj = this->getObjectsOfType(T::getClassTypeId(), pDocName);
00360     type.reserve(obj.size());
00361     for (std::vector<App::DocumentObject*>::iterator it = obj.begin(); it != obj.end(); ++it)
00362         type.push_back(static_cast<T*>(*it));
00363     return type;
00364 }
00365 
00367 inline SelectionSingleton& Selection(void)
00368 {
00369     return SelectionSingleton::instance();
00370 }
00371 
00372 } //namespace Gui
00373 
00374 #endif // GUI_SELECTION_H

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