WidgetFactory.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2004 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 #ifndef GUI_WIDGETFACTORY_H
00025 #define GUI_WIDGETFACTORY_H
00026 
00027 #include <vector>
00028 #include <QUiLoader>
00029 
00030 #include <Base/Factory.h>
00031 #include <Base/PyObjectBase.h>
00032 #include "DlgPreferencesImp.h"
00033 #include "DlgCustomizeImp.h"
00034 #include "PropertyPage.h"
00035 
00036 namespace Gui {
00037   namespace Dialog{
00038     class PreferencePage;
00039   }
00047 class GuiExport WidgetFactoryInst : public Base::Factory
00048 {
00049 public:
00050     static WidgetFactoryInst& instance();
00051     static void destruct ();
00052 
00053     QWidget* createWidget (const char* sName, QWidget* parent=0) const;
00054     Gui::Dialog::PreferencePage* createPreferencePage (const char* sName, QWidget* parent=0) const;
00055     QWidget* createPrefWidget(const char* sName, QWidget* parent, const char* sPref);
00056 
00057 private:
00058     static WidgetFactoryInst* _pcSingleton;
00059 
00060     WidgetFactoryInst(){}
00061     ~WidgetFactoryInst(){}
00062 };
00063 
00064 inline WidgetFactoryInst& WidgetFactory()
00065 {
00066     return WidgetFactoryInst::instance();
00067 }
00068 
00069 // --------------------------------------------------------------------
00070 
00077 class UiLoader : public QUiLoader
00078 {
00079 public:
00080     UiLoader(QObject* parent=0);
00081     virtual ~UiLoader();
00082 
00087     QWidget* createWidget(const QString & className, QWidget * parent=0, 
00088                           const QString& name =QString());
00089 private:
00090     QStringList cw;
00091 };
00092 
00093 // --------------------------------------------------------------------
00094 
00100 template <class CLASS>
00101 class WidgetProducer : public Base::AbstractProducer
00102 {
00103 public:
00107     WidgetProducer ()
00108     {
00109         const char* cname = CLASS::staticMetaObject.className();
00110         WidgetFactoryInst::instance().AddProducer(cname, this);
00111     }
00112 
00113     virtual ~WidgetProducer (){}
00114 
00118     virtual void* Produce () const
00119     {
00120         return (void*)(new CLASS);
00121     }
00122 };
00123 
00124 // --------------------------------------------------------------------
00125 
00131 template <class CLASS>
00132 class PrefPageProducer : public Base::AbstractProducer
00133 {
00134 public:
00138     PrefPageProducer (const char* group)
00139     {
00140         const char* cname = CLASS::staticMetaObject.className();
00141         if (strcmp(cname, Gui::Dialog::PreferencePage::staticMetaObject.className()) == 0)
00142             qWarning("The class '%s' lacks of Q_OBJECT macro", typeid(CLASS).name());
00143         if (WidgetFactoryInst::instance().CanProduce(cname)) {
00144             qWarning("The preference page class '%s' is already registered", cname);
00145         }
00146         else {
00147             WidgetFactoryInst::instance().AddProducer(cname, this);
00148             Gui::Dialog::DlgPreferencesImp::addPage(cname, group);
00149         }
00150     }
00151 
00152     virtual ~PrefPageProducer (){}
00153 
00157     virtual void* Produce () const
00158     {
00159         return (void*)(new CLASS);
00160     }
00161 };
00162 
00168 class GuiExport PrefPageUiProducer : public Base::AbstractProducer
00169 {
00170 public:
00174     PrefPageUiProducer (const char* filename, const char* group);
00175     virtual ~PrefPageUiProducer ();
00179     virtual void* Produce () const;
00180 
00181 private:
00182     QString fn;
00183 };
00184 
00185 // --------------------------------------------------------------------
00186 
00192 template <class CLASS>
00193 class CustomPageProducer : public Base::AbstractProducer
00194 {
00195 public:
00199     CustomPageProducer ()
00200     {
00201         const char* cname = CLASS::staticMetaObject.className();
00202         if (strcmp(cname, Gui::Dialog::CustomizeActionPage::staticMetaObject.className()) == 0)
00203             qWarning("The class '%s' lacks of Q_OBJECT macro", typeid(CLASS).name());
00204         if (WidgetFactoryInst::instance().CanProduce(cname)) {
00205             qWarning("The preference page class '%s' is already registered", cname);
00206         }
00207         else {
00208             WidgetFactoryInst::instance().AddProducer(cname, this);
00209             Gui::Dialog::DlgCustomizeImp::addPage(cname);
00210         }
00211     }
00212 
00213     virtual ~CustomPageProducer (){}
00214 
00218     virtual void* Produce () const
00219     {
00220         return (void*)(new CLASS);
00221     }
00222 };
00223 
00224 // --------------------------------------------------------------------
00225 
00231 class WidgetFactorySupplier
00232 {
00233 private:
00234     // Singleton
00235     WidgetFactorySupplier();
00236     static WidgetFactorySupplier *_pcSingleton;
00237 
00238 public:
00239     static WidgetFactorySupplier &instance();
00240     static void destruct();
00241     friend WidgetFactorySupplier &GetWidgetFactorySupplier();
00242 };
00243 
00244 inline WidgetFactorySupplier &GetWidgetFactorySupplier()
00245 {
00246     return WidgetFactorySupplier::instance();
00247 }
00248 
00249 // ----------------------------------------------------
00250 
00257 class ContainerDialog : public QDialog
00258 {
00259     Q_OBJECT
00260 
00261 public:
00262     ContainerDialog( QWidget* templChild );
00263     ~ContainerDialog();
00264 
00265     QPushButton* buttonOk; 
00266     QPushButton* buttonCancel; 
00268 private:
00269     QGridLayout* MyDialogLayout;
00270 };
00271 
00272 // ----------------------------------------------------
00273 
00326 class PyResource : public Base::PyObjectBase
00327 {
00328     // always start with Py_Header
00329     Py_Header;
00330 
00331 protected:
00332     ~PyResource();
00333 
00334 public:
00335     PyResource(PyTypeObject *T = &Type);
00336 
00337     void load(const char* name);
00338     bool connect(const char* sender, const char* signal, PyObject* cb);
00339 
00341     static PyObject *PyMake(PyObject *, PyObject *);
00342 
00343     //---------------------------------------------------------------------
00344     // python exports goes here +++++++++++++++++++++++++++++++++++++++++++
00345     //---------------------------------------------------------------------
00346     PyObject *_getattr(char *attr);             // __getattr__ function
00347     // getter setter
00348     int _setattr(char *attr, PyObject *value);  // __setattr__ function
00349 
00350     // methods
00351     PYFUNCDEF_D(PyResource, value);
00352     PYFUNCDEF_D(PyResource, setValue);
00353     PYFUNCDEF_D(PyResource, show);
00354     PYFUNCDEF_D(PyResource, connect);
00355 
00356 private:
00357     std::vector<class SignalConnect*> mySingals;
00358     QDialog* myDlg;
00359 };
00360 
00367 class SignalConnect : public QObject
00368 {
00369     Q_OBJECT
00370 
00371 public:
00372     SignalConnect( Base::PyObjectBase* res, PyObject* cb, QObject* sender);
00373     ~SignalConnect();
00374 
00375 public Q_SLOTS:
00376     void onExecute();
00377 
00378 private:
00379     PyObject* myResource;
00380     PyObject* myCallback;
00381     QObject*  mySender;
00382 };
00383 
00384 } // namespace Gui
00385 
00386 #endif // GUI_WIDGETFACTORY_H

Generated on Wed Nov 23 19:01:02 2011 for FreeCAD by  doxygen 1.6.1