From 9cc62f6d875e6b0f5d5d6b15fc2ab012cba26d1d Mon Sep 17 00:00:00 2001 From: xuannt Date: Tue, 29 Jun 2021 10:46:30 +0700 Subject: [PATCH] Add first component to Qt Designer plugin --- components/components.pro | 21 ++++++- .../{resources.qrc => material_res.qrc} | 0 components/plugin/materialtoggleplugin.cpp | 62 +++++++++++++++++++ components/plugin/materialtoggleplugin.h | 27 ++++++++ components/plugin/qtmaterialplugins.cpp | 11 ++++ components/plugin/qtmaterialplugins.h | 20 ++++++ examples/examples.pro | 6 +- examples/main.cpp | 2 +- examples/mainwindow.cpp | 4 ++ examples/plugindemoform.cpp | 14 +++++ examples/plugindemoform.h | 22 +++++++ examples/plugindemoform.ui | 29 +++++++++ qt-material-widgets.pro | 2 - 13 files changed, 213 insertions(+), 7 deletions(-) rename components/{resources.qrc => material_res.qrc} (100%) create mode 100644 components/plugin/materialtoggleplugin.cpp create mode 100644 components/plugin/materialtoggleplugin.h create mode 100644 components/plugin/qtmaterialplugins.cpp create mode 100644 components/plugin/qtmaterialplugins.h create mode 100644 examples/plugindemoform.cpp create mode 100644 examples/plugindemoform.h create mode 100644 examples/plugindemoform.ui diff --git a/components/components.pro b/components/components.pro index d6080d5..e5a3196 100644 --- a/components/components.pro +++ b/components/components.pro @@ -1,6 +1,21 @@ +greaterThan(QT_MAJOR_VERSION, 4) { + QT += widgets designer +} + +lessThan(QT_MAJOR_VERSION, 5) { + CONFIG += designer +} + +CONFIG += plugin release TEMPLATE = lib -CONFIG += staticlib +TARGET = $$qtLibraryTarget(qt-material-widget) +target.path = $$[QT_INSTALL_PLUGINS]/designer +INSTALLS += target +INCLUDEPATH += . + SOURCES = \ + plugin/materialtoggleplugin.cpp \ + plugin/qtmaterialplugins.cpp \ qtmaterialavatar.cpp \ lib/qtmaterialstyle.cpp \ lib/qtmaterialtheme.cpp \ @@ -49,6 +64,8 @@ SOURCES = \ qtmateriallist.cpp \ qtmateriallistitem.cpp HEADERS = \ + plugin/materialtoggleplugin.h \ + plugin/qtmaterialplugins.h \ qtmaterialavatar_p.h \ qtmaterialavatar.h \ lib/qtmaterialstyle_p.h \ @@ -127,4 +144,4 @@ HEADERS = \ qtmateriallistitem.h \ qtmateriallistitem_p.h RESOURCES += \ - resources.qrc + material_res.qrc diff --git a/components/resources.qrc b/components/material_res.qrc similarity index 100% rename from components/resources.qrc rename to components/material_res.qrc diff --git a/components/plugin/materialtoggleplugin.cpp b/components/plugin/materialtoggleplugin.cpp new file mode 100644 index 0000000..7a29126 --- /dev/null +++ b/components/plugin/materialtoggleplugin.cpp @@ -0,0 +1,62 @@ +#include "materialtoggleplugin.h" +#include "qtmaterialtoggle.h" +#include + +MaterialTogglePlugin:: +MaterialTogglePlugin(QObject* parent) : + QObject(parent), + initialized(false) +{ +} + +QString MaterialTogglePlugin:: +name() const +{ + return "Material Toggle"; +} + +QString MaterialTogglePlugin:: +group() const +{ + return tr("Qt Meterial Widgets"); +} + +QString MaterialTogglePlugin:: +toolTip() const +{ + return tr("Material toggle button"); +} + +QString MaterialTogglePlugin:: +whatsThis() const +{ + return tr("Material toggle button"); +} + +QString MaterialTogglePlugin:: +includeFile() const +{ + return "qtmaterialtoggle.h"; +} + +QIcon MaterialTogglePlugin:: +icon() const +{ + return QIcon(); +} + +bool MaterialTogglePlugin:: +isContainer() const +{ + return false; +} + +QWidget * MaterialTogglePlugin:: +createWidget(QWidget *parent) +{ + return new QtMaterialToggle(parent); +} + +#if QT_VERSION < QT_VERSION_CHECK(5,0,0) +Q_EXPORT_PLUGIN2(MaterialTogglePlugin, MaterialTogglePlugin) +#endif diff --git a/components/plugin/materialtoggleplugin.h b/components/plugin/materialtoggleplugin.h new file mode 100644 index 0000000..74a38eb --- /dev/null +++ b/components/plugin/materialtoggleplugin.h @@ -0,0 +1,27 @@ +#ifndef MATERIALTOGGLEPLUGIN_H +#define MATERIALTOGGLEPLUGIN_H + +#include + +class MaterialTogglePlugin : public QObject, public QDesignerCustomWidgetInterface +{ + Q_OBJECT +public: + explicit MaterialTogglePlugin(QObject *parent = nullptr); + + QString name() const; + QString group() const; + QString toolTip() const; + QString whatsThis() const; + QString includeFile() const; + QIcon icon() const; + + bool isContainer() const; + + QWidget *createWidget(QWidget *parent); + +private: + bool initialized; +}; + +#endif // MATERIALTOGGLEPLUGIN_H diff --git a/components/plugin/qtmaterialplugins.cpp b/components/plugin/qtmaterialplugins.cpp new file mode 100644 index 0000000..7c5e25b --- /dev/null +++ b/components/plugin/qtmaterialplugins.cpp @@ -0,0 +1,11 @@ +#include "qtmaterialplugins.h" +#include "materialtoggleplugin.h" + +QtMaterialPlugins::QtMaterialPlugins(QObject *parent) : QObject(parent) +{ + m_plugins << new MaterialTogglePlugin(this); +} + +QList QtMaterialPlugins::customWidgets() const { + return m_plugins; +} diff --git a/components/plugin/qtmaterialplugins.h b/components/plugin/qtmaterialplugins.h new file mode 100644 index 0000000..31735ad --- /dev/null +++ b/components/plugin/qtmaterialplugins.h @@ -0,0 +1,20 @@ +#ifndef QTMATERIALPLUGINS_H +#define QTMATERIALPLUGINS_H + +#include + +class QtMaterialPlugins : public QObject, public QDesignerCustomWidgetCollectionInterface +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "com.meta-vi.QDesignerCustomWidgetCollectionInterface") + Q_INTERFACES(QDesignerCustomWidgetCollectionInterface) +public: + explicit QtMaterialPlugins(QObject *parent = nullptr); + QList customWidgets() const; + +private: + QList m_plugins; + +}; + +#endif // QTMATERIALPLUGINS_H diff --git a/examples/examples.pro b/examples/examples.pro index 35d2438..5fa2de9 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -7,6 +7,7 @@ SOURCES = mainwindow.cpp \ badgesettingseditor.cpp \ checkboxsettingseditor.cpp \ fabsettingseditor.cpp \ + plugindemoform.cpp \ raisedbuttonsettingseditor.cpp \ flatbuttonsettingseditor.cpp \ iconbuttonsettingseditor.cpp \ @@ -29,6 +30,7 @@ HEADERS = mainwindow.h \ badgesettingseditor.h \ checkboxsettingseditor.h \ fabsettingseditor.h \ + plugindemoform.h \ raisedbuttonsettingseditor.h \ flatbuttonsettingseditor.h \ iconbuttonsettingseditor.h \ @@ -46,10 +48,9 @@ HEADERS = mainwindow.h \ appbarsettingseditor.h \ autocompletesettingseditor.h \ menusettingseditor.h -LIBS += $$top_builddir/components/$(OBJECTS_DIR)/libcomponents.a +LIBS += -L../components/ -lqt-material-widget INCLUDEPATH += $$top_srcdir/components/ TARGET = examples-exe -PRE_TARGETDEPS += $$top_builddir/components/$(OBJECTS_DIR)/libcomponents.a RESOURCES += \ examples.qrc @@ -61,6 +62,7 @@ FORMS += \ fabsettingsform.ui \ flatbuttonsettingsform.ui \ iconbuttonsettingsform.ui \ + plugindemoform.ui \ progresssettingsform.ui \ circularprogresssettingsform.ui \ slidersettingsform.ui \ diff --git a/examples/main.cpp b/examples/main.cpp index 38f8761..42510fa 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -6,7 +6,7 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); - Q_INIT_RESOURCE(resources); + Q_INIT_RESOURCE(material_res); MainWindow window; window.show(); diff --git a/examples/mainwindow.cpp b/examples/mainwindow.cpp index cc4992e..74cad5b 100644 --- a/examples/mainwindow.cpp +++ b/examples/mainwindow.cpp @@ -23,6 +23,7 @@ #include "appbarsettingseditor.h" #include "autocompletesettingseditor.h" #include "menusettingseditor.h" +#include "plugindemoform.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -42,6 +43,7 @@ MainWindow::MainWindow(QWidget *parent) setCentralWidget(widget); + PluginDemoForm *pluginFrm = new PluginDemoForm; AvatarSettingsEditor *avatar = new AvatarSettingsEditor; BadgeSettingsEditor *badge = new BadgeSettingsEditor; CheckBoxSettingsEditor *checkbox = new CheckBoxSettingsEditor; @@ -64,6 +66,7 @@ MainWindow::MainWindow(QWidget *parent) AutoCompleteSettingsEditor *autocomplete = new AutoCompleteSettingsEditor; MenuSettingsEditor *menu = new MenuSettingsEditor; + stack->addWidget(pluginFrm); stack->addWidget(appBar); stack->addWidget(autocomplete); stack->addWidget(avatar); @@ -86,6 +89,7 @@ MainWindow::MainWindow(QWidget *parent) stack->addWidget(textField); stack->addWidget(toggle); + list->addItem("Plugins"); list->addItem("App Bar"); list->addItem("Auto Complete"); list->addItem("Avatar"); diff --git a/examples/plugindemoform.cpp b/examples/plugindemoform.cpp new file mode 100644 index 0000000..8d59c30 --- /dev/null +++ b/examples/plugindemoform.cpp @@ -0,0 +1,14 @@ +#include "plugindemoform.h" +#include "ui_plugindemoform.h" + +PluginDemoForm::PluginDemoForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::PluginDemoForm) +{ + ui->setupUi(this); +} + +PluginDemoForm::~PluginDemoForm() +{ + delete ui; +} diff --git a/examples/plugindemoform.h b/examples/plugindemoform.h new file mode 100644 index 0000000..3bc90bb --- /dev/null +++ b/examples/plugindemoform.h @@ -0,0 +1,22 @@ +#ifndef PLUGINDEMOFORM_H +#define PLUGINDEMOFORM_H + +#include + +namespace Ui { +class PluginDemoForm; +} + +class PluginDemoForm : public QWidget +{ + Q_OBJECT + +public: + explicit PluginDemoForm(QWidget *parent = nullptr); + ~PluginDemoForm(); + +private: + Ui::PluginDemoForm *ui; +}; + +#endif // PLUGINDEMOFORM_H diff --git a/examples/plugindemoform.ui b/examples/plugindemoform.ui new file mode 100644 index 0000000..4af9ffa --- /dev/null +++ b/examples/plugindemoform.ui @@ -0,0 +1,29 @@ + + + PluginDemoForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 60 + 20 + 64 + 48 + + + + + + + diff --git a/qt-material-widgets.pro b/qt-material-widgets.pro index 5654f10..9d6631f 100644 --- a/qt-material-widgets.pro +++ b/qt-material-widgets.pro @@ -1,5 +1,3 @@ -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets - TARGET = qt-material-widgets TEMPLATE = subdirs SUBDIRS = components examples