diff options
author | Tobias Hunger <tobias.hunger@theqtcompany.com> | 2015-09-21 16:28:48 +0200 |
---|---|---|
committer | Tobias Hunger <tobias.hunger@theqtcompany.com> | 2015-09-22 09:22:46 +0000 |
commit | 4b77ae07186a91cf69930dc8b8cfaa099df5121e (patch) | |
tree | 153aa967626c9736dbd486df0f4b06f6bb48eaec | |
parent | 443f133d3efaa37eee555001ae6610dd33f474dc (diff) | |
download | qt-creator-4b77ae07186a91cf69930dc8b8cfaa099df5121e.tar.gz |
JsonWizard: Allow for custom widgets in the Field page
... instead of having a hard-coded list of widgets you can use.
Change-Id: Iedf7016412ce9d619fea5cdffe6dbf86beda92b0
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Benjamin Zeller <benjamin.zeller@canonical.com>
3 files changed, 31 insertions, 19 deletions
diff --git a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp index 9a60b62fc2..a3277a2938 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp @@ -73,25 +73,6 @@ namespace ProjectExplorer { // Helper: // -------------------------------------------------------------------- -static JsonFieldPage::Field *createFieldData(const QString &type) -{ - if (type == QLatin1String("Label")) - return new LabelField; - else if (type == QLatin1String("Spacer")) - return new SpacerField; - else if (type == QLatin1String("LineEdit")) - return new LineEditField; - else if (type == QLatin1String("TextEdit")) - return new TextEditField; - else if (type == QLatin1String("PathChooser")) - return new PathChooserField; - else if (type == QLatin1String("CheckBox")) - return new CheckBoxField; - else if (type == QLatin1String("ComboBox")) - return new ComboBoxField; - return 0; -} - class LineEditValidator : public QRegularExpressionValidator { public: @@ -909,6 +890,8 @@ void ComboBoxField::initializeData(MacroExpander *expander) // JsonFieldPage: // -------------------------------------------------------------------- +QHash<QString, JsonFieldPage::FieldFactory> JsonFieldPage::m_factories; + JsonFieldPage::JsonFieldPage(MacroExpander *expander, QWidget *parent) : WizardPage(parent), m_formLayout(new QFormLayout), @@ -935,6 +918,12 @@ JsonFieldPage::~JsonFieldPage() qDeleteAll(m_fields); } +void JsonFieldPage::registerFieldFactory(const QString &id, const JsonFieldPage::FieldFactory &ff) +{ + QTC_ASSERT(!m_factories.contains(id), return); + m_factories.insert(id, ff); +} + bool JsonFieldPage::setup(const QVariant &data) { QString errorMessage; @@ -1002,4 +991,11 @@ MacroExpander *JsonFieldPage::expander() return m_expander; } +JsonFieldPage::Field *JsonFieldPage::createFieldData(const QString &type) +{ + if (!m_factories.contains(type)) + return 0; + return m_factories.value(type)(); +} + } // namespace ProjectExplorer diff --git a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.h b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.h index 9423934255..1b1afc83d4 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.h +++ b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.h @@ -115,6 +115,9 @@ public: JsonFieldPage(Utils::MacroExpander *expander, QWidget *parent = 0); ~JsonFieldPage(); + typedef std::function<Field *()> FieldFactory; + static void registerFieldFactory(const QString &id, const FieldFactory &ff); + bool setup(const QVariant &data); bool isComplete() const; @@ -129,6 +132,10 @@ public: Utils::MacroExpander *expander(); private: + static QHash<QString, FieldFactory> m_factories; + + static Field *createFieldData(const QString &type); + QFormLayout *m_formLayout; QLabel *m_errorLabel; diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory_p.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory_p.cpp index 3d58ee2c8b..963ae3c4be 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory_p.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory_p.cpp @@ -31,6 +31,7 @@ #include "jsonwizardpagefactory_p.h" #include "jsonfieldpage.h" +#include "jsonfieldpage_p.h" #include "jsonfilepage.h" #include "jsonkitspage.h" #include "jsonprojectpage.h" @@ -53,6 +54,14 @@ namespace Internal { FieldPageFactory::FieldPageFactory() { setTypeIdsSuffix(QLatin1String("Fields")); + + JsonFieldPage::registerFieldFactory(QLatin1String("Label"), []() { return new LabelField; }); + JsonFieldPage::registerFieldFactory(QLatin1String("Spacer"), []() { return new SpacerField; }); + JsonFieldPage::registerFieldFactory(QLatin1String("LineEdit"), []() { return new LineEditField; }); + JsonFieldPage::registerFieldFactory(QLatin1String("TextEdit"), []() { return new TextEditField; }); + JsonFieldPage::registerFieldFactory(QLatin1String("PathChooser"), []() { return new PathChooserField; }); + JsonFieldPage::registerFieldFactory(QLatin1String("CheckBox"), []() { return new CheckBoxField; }); + JsonFieldPage::registerFieldFactory(QLatin1String("ComboBox"), []() { return new ComboBoxField; }); } Utils::WizardPage *FieldPageFactory::create(JsonWizard *wizard, Core::Id typeId, const QVariant &data) |