| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2021 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 |
#ifndef GUI_UILOADER_H |
| 24 |
#define GUI_UILOADER_H |
| 25 |
|
| 26 |
#if !defined(__MINGW32__) |
| 27 |
# define HAVE_QT_UI_TOOLS |
| 28 |
#endif |
| 29 |
|
| 30 |
#if defined(HAVE_QT_UI_TOOLS) |
| 31 |
# include <QUiLoader> |
| 32 |
#else |
| 33 |
# include <QObject> |
| 34 |
#endif |
| 35 |
|
| 36 |
#include <CXX/Extensions.hxx> |
| 37 |
#include <memory> |
| 38 |
|
| 39 |
|
| 40 |
QT_BEGIN_NAMESPACE |
| 41 |
class QLayout; |
| 42 |
class QAction; |
| 43 |
class QActionGroup; |
| 44 |
class QDir; |
| 45 |
class QIODevice; |
| 46 |
class QWidget; |
| 47 |
QT_END_NAMESPACE |
| 48 |
|
| 49 |
|
| 50 |
#if !defined(HAVE_QT_UI_TOOLS) |
| 51 |
class QUiLoader: public QObject |
| 52 |
{ |
| 53 |
Q_OBJECT |
| 54 |
public: |
| 55 |
explicit QUiLoader(QObject* parent = nullptr); |
| 56 |
~QUiLoader(); |
| 57 |
|
| 58 |
QStringList pluginPaths() const; |
| 59 |
void clearPluginPaths(); |
| 60 |
void addPluginPath(const QString& path); |
| 61 |
|
| 62 |
QWidget* load(QIODevice* device, QWidget* parentWidget = nullptr); |
| 63 |
QStringList availableWidgets() const; |
| 64 |
QStringList availableLayouts() const; |
| 65 |
|
| 66 |
virtual QWidget* createWidget( |
| 67 |
const QString& className, |
| 68 |
QWidget* parent = nullptr, |
| 69 |
const QString& name = QString() |
| 70 |
); |
| 71 |
virtual QLayout* createLayout( |
| 72 |
const QString& className, |
| 73 |
QObject* parent = nullptr, |
| 74 |
const QString& name = QString() |
| 75 |
); |
| 76 |
virtual QActionGroup* createActionGroup(QObject* parent = nullptr, const QString& name = QString()); |
| 77 |
virtual QAction* createAction(QObject* parent = nullptr, const QString& name = QString()); |
| 78 |
|
| 79 |
void setWorkingDirectory(const QDir& dir); |
| 80 |
QDir workingDirectory() const; |
| 81 |
|
| 82 |
void setLanguageChangeEnabled(bool enabled); |
| 83 |
bool isLanguageChangeEnabled() const; |
| 84 |
|
| 85 |
void setTranslationEnabled(bool enabled); |
| 86 |
bool isTranslationEnabled() const; |
| 87 |
|
| 88 |
QString errorString() const; |
| 89 |
|
| 90 |
private: |
| 91 |
Py::Object uiloader; |
| 92 |
}; |
| 93 |
#endif |
| 94 |
|
| 95 |
namespace Gui |
| 96 |
{ |
| 97 |
|
| 98 |
class PySideUicModule: public Py::ExtensionModule<PySideUicModule> |
| 99 |
{ |
| 100 |
|
| 101 |
public: |
| 102 |
PySideUicModule(); |
| 103 |
~PySideUicModule() override = default; |
| 104 |
|
| 105 |
private: |
| 106 |
Py::Object loadUiType(const Py::Tuple& args); |
| 107 |
Py::Object loadUi(const Py::Tuple& args); |
| 108 |
Py::Object createCustomWidget(const Py::Tuple&); |
| 109 |
}; |
| 110 |
|
| 111 |
/** |
| 112 |
* The UiLoader class provides the abitlity to use the widget factory |
| 113 |
* framework of FreeCAD within the framework provided by Qt. This class |
| 114 |
* extends QUiLoader by the creation of FreeCAD specific widgets. |
| 115 |
* @author Werner Mayer |
| 116 |
*/ |
| 117 |
class UiLoader: public QUiLoader |
| 118 |
{ |
| 119 |
protected: |
| 120 |
/** |
| 121 |
* A protected construct for UiLoader. |
| 122 |
* To create an instance of UiLoader @see UiLoader::newInstance() |
| 123 |
*/ |
| 124 |
explicit UiLoader(QObject* parent = nullptr); |
| 125 |
|
| 126 |
public: |
| 127 |
/** |
| 128 |
* Creates a new instance of a UiLoader. |
| 129 |
* |
| 130 |
* Due to its flaw the QUiLoader upon creation loads every available Qt |
| 131 |
* designer plugin it can find in QApplication::libraryPaths(). Some of |
| 132 |
* those plugins may perform some unexpected actions upon load which may |
| 133 |
* interfere with FreeCAD's functionality. Only way to avoid such behaviour |
| 134 |
* is to reset QApplication::libraryPaths, create a QUiLoader and then |
| 135 |
* restore the libs paths. Hence need for this function to wrap |
| 136 |
* construction. |
| 137 |
* |
| 138 |
* @see https://github.com/FreeCAD/FreeCAD/issues/8708 |
| 139 |
*/ |
| 140 |
static std::unique_ptr<UiLoader> newInstance(QObject* parent = nullptr); |
| 141 |
|
| 142 |
~UiLoader() override; |
| 143 |
|
| 144 |
/** |
| 145 |
* Creates a widget of the type \a className with the parent \a parent. |
| 146 |
* For more details see the documentation to QWidgetFactory. |
| 147 |
*/ |
| 148 |
QWidget* createWidget( |
| 149 |
const QString& className, |
| 150 |
QWidget* parent = nullptr, |
| 151 |
const QString& name = QString() |
| 152 |
) override; |
| 153 |
|
| 154 |
private: |
| 155 |
QStringList cw; |
| 156 |
}; |
| 157 |
|
| 158 |
// -------------------------------------------------------------------- |
| 159 |
|
| 160 |
class UiLoaderPy: public Py::PythonExtension<UiLoaderPy> |
| 161 |
{ |
| 162 |
public: |
| 163 |
static void init_type(); // announce properties and methods |
| 164 |
|
| 165 |
UiLoaderPy(); |
| 166 |
~UiLoaderPy() override; |
| 167 |
|
| 168 |
Py::Object repr() override; |
| 169 |
Py::Object createWidget(const Py::Tuple&); |
| 170 |
Py::Object load(const Py::Tuple&); |
| 171 |
|
| 172 |
Py::Object addPluginPath(const Py::Tuple&); |
| 173 |
Py::Object clearPluginPaths(const Py::Tuple&); |
| 174 |
Py::Object pluginPaths(const Py::Tuple&); |
| 175 |
Py::Object availableWidgets(const Py::Tuple&); |
| 176 |
Py::Object errorString(const Py::Tuple&); |
| 177 |
Py::Object isLanguageChangeEnabled(const Py::Tuple&); |
| 178 |
Py::Object setLanguageChangeEnabled(const Py::Tuple&); |
| 179 |
Py::Object setWorkingDirectory(const Py::Tuple&); |
| 180 |
Py::Object workingDirectory(const Py::Tuple&); |
| 181 |
|
| 182 |
private: |
| 183 |
static PyObject* PyMake(struct _typeobject*, PyObject*, PyObject*); |
| 184 |
|
| 185 |
private: |
| 186 |
std::unique_ptr<UiLoader> loader; |
| 187 |
}; |
| 188 |
|
| 189 |
} // namespace Gui |
| 190 |
|
| 191 |
#endif // GUI_UILOADER_H |