| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 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 |
|
| 24 |
#ifndef GUI_WIDGETFACTORY_H |
| 25 |
#define GUI_WIDGETFACTORY_H |
| 26 |
|
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include <Base/Factory.h> |
| 30 |
#include "Dialogs/DlgCustomizeImp.h" |
| 31 |
#include "Dialogs/DlgPreferencesImp.h" |
| 32 |
#include "PropertyPage.h" |
| 33 |
#include <CXX/Extensions.hxx> |
| 34 |
|
| 35 |
QT_BEGIN_NAMESPACE |
| 36 |
class QDir; |
| 37 |
QT_END_NAMESPACE |
| 38 |
|
| 39 |
namespace Gui |
| 40 |
{ |
| 41 |
namespace Dialog |
| 42 |
{ |
| 43 |
class PreferencePage; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* The widget factory provides methods for the dynamic creation of widgets. |
| 48 |
* To create these widgets once they must be registered to the factory. |
| 49 |
* To register them use WidgetProducer or any subclasses; to register a |
| 50 |
* preference page use PrefPageProducer instead. |
| 51 |
* \author Werner Mayer |
| 52 |
*/ |
| 53 |
class GuiExport WidgetFactoryInst: public Base::Factory |
| 54 |
{ |
| 55 |
public: |
| 56 |
static WidgetFactoryInst& instance(); |
| 57 |
static void destruct(); |
| 58 |
|
| 59 |
QWidget* createWidget(const char* sName, QWidget* parent = nullptr) const; |
| 60 |
Gui::Dialog::PreferencePage* createPreferencePage(const char* sName, QWidget* parent = nullptr) const; |
| 61 |
QWidget* createPrefWidget(const char* sName, QWidget* parent, const char* sPref); |
| 62 |
|
| 63 |
private: |
| 64 |
static WidgetFactoryInst* _pcSingleton; |
| 65 |
|
| 66 |
WidgetFactoryInst() = default; |
| 67 |
~WidgetFactoryInst() override = default; |
| 68 |
}; |
| 69 |
|
| 70 |
inline WidgetFactoryInst& WidgetFactory() |
| 71 |
{ |
| 72 |
return WidgetFactoryInst::instance(); |
| 73 |
} |
| 74 |
|
| 75 |
// -------------------------------------------------------------------- |
| 76 |
|
| 77 |
/** |
| 78 |
* The WidgetProducer class is a value-based template class that provides |
| 79 |
* the ability to create widgets dynamically. |
| 80 |
* \author Werner Mayer |
| 81 |
*/ |
| 82 |
template<class CLASS> |
| 83 |
class WidgetProducer: public Base::AbstractProducer |
| 84 |
{ |
| 85 |
public: |
| 86 |
/** |
| 87 |
* Register a special type of widget to the WidgetFactoryInst. |
| 88 |
*/ |
| 89 |
WidgetProducer() |
| 90 |
{ |
| 91 |
const char* cname = CLASS::staticMetaObject.className(); |
| 92 |
WidgetFactoryInst::instance().AddProducer(cname, this); |
| 93 |
} |
| 94 |
|
| 95 |
~WidgetProducer() override = default; |
| 96 |
|
| 97 |
/** |
| 98 |
* Creates an instance of the specified widget. |
| 99 |
*/ |
| 100 |
void* Produce() const override |
| 101 |
{ |
| 102 |
return (new CLASS); |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
// -------------------------------------------------------------------- |
| 107 |
|
| 108 |
/** |
| 109 |
* The PrefPageProducer class is a value-based template class that provides |
| 110 |
* the ability to create preference pages dynamically. |
| 111 |
* \author Werner Mayer |
| 112 |
*/ |
| 113 |
template<class CLASS> |
| 114 |
class PrefPageProducer: public Base::AbstractProducer |
| 115 |
{ |
| 116 |
public: |
| 117 |
/** |
| 118 |
* Register a special type of preference page to the WidgetFactoryInst. |
| 119 |
*/ |
| 120 |
PrefPageProducer(const char* group) |
| 121 |
{ |
| 122 |
const char* cname = CLASS::staticMetaObject.className(); |
| 123 |
if (strcmp(cname, Gui::Dialog::PreferencePage::staticMetaObject.className()) == 0) { |
| 124 |
qWarning("The class '%s' lacks of Q_OBJECT macro", typeid(CLASS).name()); |
| 125 |
} |
| 126 |
if (WidgetFactoryInst::instance().CanProduce(cname)) { |
| 127 |
qWarning("The preference page class '%s' is already registered", cname); |
| 128 |
} |
| 129 |
else { |
| 130 |
WidgetFactoryInst::instance().AddProducer(cname, this); |
| 131 |
Gui::Dialog::DlgPreferencesImp::addPage(cname, group); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
~PrefPageProducer() override = default; |
| 136 |
|
| 137 |
/** |
| 138 |
* Creates an instance of the specified widget. |
| 139 |
*/ |
| 140 |
void* Produce() const override |
| 141 |
{ |
| 142 |
return (new CLASS); |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
/** |
| 147 |
* The PrefPageUiProducer class provides the ability to create preference pages |
| 148 |
* dynamically from an external UI file. |
| 149 |
* @author Werner Mayer |
| 150 |
*/ |
| 151 |
class GuiExport PrefPageUiProducer: public Base::AbstractProducer |
| 152 |
{ |
| 153 |
public: |
| 154 |
/** |
| 155 |
* Register a special type of preference page to the WidgetFactoryInst. |
| 156 |
*/ |
| 157 |
PrefPageUiProducer(const char* filename, const char* group); |
| 158 |
~PrefPageUiProducer() override; |
| 159 |
/** |
| 160 |
* Creates an instance of the specified widget. |
| 161 |
*/ |
| 162 |
void* Produce() const override; |
| 163 |
|
| 164 |
private: |
| 165 |
QString fn; |
| 166 |
}; |
| 167 |
|
| 168 |
/** |
| 169 |
* The PrefPagePyProducer class provides the ability to create preference pages |
| 170 |
* dynamically from a Python class. |
| 171 |
* @author Werner Mayer |
| 172 |
*/ |
| 173 |
class GuiExport PrefPagePyProducer: public Base::AbstractProducer |
| 174 |
{ |
| 175 |
public: |
| 176 |
/** |
| 177 |
* Register a special type of preference page to the WidgetFactoryInst. |
| 178 |
*/ |
| 179 |
PrefPagePyProducer(const Py::Object&, const char* group); |
| 180 |
~PrefPagePyProducer() override; |
| 181 |
/** |
| 182 |
* Creates an instance of the specified widget. |
| 183 |
*/ |
| 184 |
void* Produce() const override; |
| 185 |
|
| 186 |
private: |
| 187 |
Py::Object type; |
| 188 |
}; |
| 189 |
|
| 190 |
// -------------------------------------------------------------------- |
| 191 |
|
| 192 |
/** |
| 193 |
* The CustomPageProducer class is a value-based template class that provides |
| 194 |
* the ability to create custom pages dynamically. |
| 195 |
* \author Werner Mayer |
| 196 |
*/ |
| 197 |
template<class CLASS> |
| 198 |
class CustomPageProducer: public Base::AbstractProducer |
| 199 |
{ |
| 200 |
public: |
| 201 |
/** |
| 202 |
* Register a special type of customize page to the WidgetFactoryInst. |
| 203 |
*/ |
| 204 |
CustomPageProducer() |
| 205 |
{ |
| 206 |
const char* cname = CLASS::staticMetaObject.className(); |
| 207 |
if (strcmp(cname, Gui::Dialog::CustomizeActionPage::staticMetaObject.className()) == 0) { |
| 208 |
qWarning("The class '%s' lacks of Q_OBJECT macro", typeid(CLASS).name()); |
| 209 |
} |
| 210 |
if (WidgetFactoryInst::instance().CanProduce(cname)) { |
| 211 |
qWarning("The preference page class '%s' is already registered", cname); |
| 212 |
} |
| 213 |
else { |
| 214 |
WidgetFactoryInst::instance().AddProducer(cname, this); |
| 215 |
Gui::Dialog::DlgCustomizeImp::addPage(cname); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
~CustomPageProducer() override = default; |
| 220 |
|
| 221 |
/** |
| 222 |
* Creates an instance of the specified widget. |
| 223 |
*/ |
| 224 |
void* Produce() const override |
| 225 |
{ |
| 226 |
return (new CLASS); |
| 227 |
} |
| 228 |
}; |
| 229 |
|
| 230 |
// -------------------------------------------------------------------- |
| 231 |
|
| 232 |
/** |
| 233 |
* The widget factory supplier class registers all kinds of |
| 234 |
* preference pages and widgets. |
| 235 |
* \author Werner Mayer |
| 236 |
*/ |
| 237 |
class WidgetFactorySupplier |
| 238 |
{ |
| 239 |
private: |
| 240 |
// Singleton |
| 241 |
WidgetFactorySupplier(); |
| 242 |
static WidgetFactorySupplier* _pcSingleton; |
| 243 |
|
| 244 |
public: |
| 245 |
static WidgetFactorySupplier& instance(); |
| 246 |
static void destruct(); |
| 247 |
friend WidgetFactorySupplier& GetWidgetFactorySupplier(); |
| 248 |
}; |
| 249 |
|
| 250 |
inline WidgetFactorySupplier& GetWidgetFactorySupplier() |
| 251 |
{ |
| 252 |
return WidgetFactorySupplier::instance(); |
| 253 |
} |
| 254 |
|
| 255 |
// ---------------------------------------------------- |
| 256 |
|
| 257 |
/** |
| 258 |
* The ContainerDialog class acts as a container to embed any kinds of widgets that |
| 259 |
* do not inherit from QDialog. This class also provides an "Ok" and a "Cancel" button. |
| 260 |
* At most this class is used to embed widgets which are created from .ui files. |
| 261 |
* \author Werner Mayer |
| 262 |
*/ |
| 263 |
class ContainerDialog: public QDialog |
| 264 |
{ |
| 265 |
Q_OBJECT |
| 266 |
|
| 267 |
public: |
| 268 |
ContainerDialog(QWidget* templChild); |
| 269 |
~ContainerDialog() override; |
| 270 |
|
| 271 |
QPushButton* buttonOk; /**< The Ok button. */ |
| 272 |
QPushButton* buttonCancel; /**< The cancel button. */ |
| 273 |
|
| 274 |
private: |
| 275 |
QGridLayout* MyDialogLayout; |
| 276 |
}; |
| 277 |
|
| 278 |
// ---------------------------------------------------- |
| 279 |
|
| 280 |
/** |
| 281 |
* The PyResource class provides an interface to create widgets or to load .ui files from Python. |
| 282 |
* With |
| 283 |
* \code |
| 284 |
* d = Gui.createDialog("test.ui") |
| 285 |
* \endcode |
| 286 |
* |
| 287 |
* you can create a PyResource object containing the widget. If a relative file name |
| 288 |
* is given PyResource looks first in the current working directory and afterwards in |
| 289 |
* the home path where FreeCAD resides. |
| 290 |
* |
| 291 |
* If the appropriate .ui file cannot be found or creation fails an exception is thrown. |
| 292 |
* In case the widget in the .ui file does not inherit from QDialog it is embedded in a |
| 293 |
* \ref ContainerDialog object. |
| 294 |
* To show the widget you can call |
| 295 |
* \code |
| 296 |
* d.Show() |
| 297 |
* \endcode |
| 298 |
* |
| 299 |
* Furthermore it is possible to get or set values from any widgets inside |
| 300 |
* the parent widget or to connect a Python callback function with a widget. |
| 301 |
* \remark The callback function must have exactly one parameter. This parameter |
| 302 |
* points to the dialog you have just created. |
| 303 |
* \code |
| 304 |
* # define a callback function with one argument |
| 305 |
* def TestCall(obj): |
| 306 |
* # sets the value from lineedit if "Button_Name" was pressed |
| 307 |
* obj.setValue("lineedit", "text", "Show this text here!") |
| 308 |
* print "Button clicked" |
| 309 |
* |
| 310 |
* d = Gui.createDialog("test.ui") |
| 311 |
* d.connect("Button_Name", "clicked()", TestCall) |
| 312 |
* d.show() |
| 313 |
* \endcode |
| 314 |
* |
| 315 |
* If the button with the name "Button_Name" is clicked the message "Button clicked" is |
| 316 |
* printed. |
| 317 |
* For example if you have a QLineEdit inside your widget you can set the text with |
| 318 |
* \code |
| 319 |
* # sets "Show this text here!" to the text property |
| 320 |
* d.setValue("lineedit", "text", "Show this text here!") |
| 321 |
* d.show() |
| 322 |
* \endcode |
| 323 |
* |
| 324 |
* or retrieve the entered text with |
| 325 |
* \code |
| 326 |
* f = d.getValue("lineedit", "text") |
| 327 |
* print f |
| 328 |
* \endcode |
| 329 |
* |
| 330 |
* \author Werner Mayer |
| 331 |
*/ |
| 332 |
|
| 333 |
class PyResource: public Py::PythonExtension<PyResource> |
| 334 |
{ |
| 335 |
public: |
| 336 |
static void init_type(); // announce properties and methods |
| 337 |
|
| 338 |
PyResource(); |
| 339 |
~PyResource() override; |
| 340 |
|
| 341 |
void load(const char* name); |
| 342 |
bool connect(const char* sender, const char* signal, PyObject* cb); |
| 343 |
|
| 344 |
Py::Object repr() override; |
| 345 |
|
| 346 |
Py::Object value(const Py::Tuple&); |
| 347 |
Py::Object setValue(const Py::Tuple&); |
| 348 |
Py::Object show(const Py::Tuple&); |
| 349 |
Py::Object connect(const Py::Tuple&); |
| 350 |
|
| 351 |
private: |
| 352 |
std::vector<class SignalConnect*> mySignals; |
| 353 |
QDialog* myDlg; |
| 354 |
}; |
| 355 |
|
| 356 |
/** |
| 357 |
* The SignalConnect class provides the abitlity to make a connection |
| 358 |
* between the callback function of a Python object and the slot onExecute(). |
| 359 |
* This mechanism is used in the Python/Qt framework. |
| 360 |
* \author Werner Mayer |
| 361 |
*/ |
| 362 |
class SignalConnect: public QObject |
| 363 |
{ |
| 364 |
Q_OBJECT |
| 365 |
|
| 366 |
public: |
| 367 |
SignalConnect(PyObject* res, PyObject* cb); |
| 368 |
~SignalConnect() override; |
| 369 |
|
| 370 |
public Q_SLOTS: |
| 371 |
void onExecute(); |
| 372 |
|
| 373 |
private: |
| 374 |
PyObject* myResource; |
| 375 |
PyObject* myCallback; |
| 376 |
}; |
| 377 |
|
| 378 |
// ---------------------------------------------------- |
| 379 |
namespace Dialog |
| 380 |
{ |
| 381 |
|
| 382 |
/** Subclass that embeds a form from a Python class. |
| 383 |
* \author Werner Mayer |
| 384 |
*/ |
| 385 |
class GuiExport PreferencePagePython: public PreferencePage |
| 386 |
{ |
| 387 |
Q_OBJECT |
| 388 |
|
| 389 |
public: |
| 390 |
PreferencePagePython(const Py::Object& dlg, QWidget* parent = nullptr); |
| 391 |
~PreferencePagePython() override; |
| 392 |
|
| 393 |
void loadSettings() override; |
| 394 |
void saveSettings() override; |
| 395 |
|
| 396 |
protected: |
| 397 |
void changeEvent(QEvent* e) override; |
| 398 |
|
| 399 |
private: |
| 400 |
Py::Object page; |
| 401 |
}; |
| 402 |
|
| 403 |
} // namespace Dialog |
| 404 |
|
| 405 |
} // namespace Gui |
| 406 |
|
| 407 |
#endif // GUI_WIDGETFACTORY_H |