From 973b7e97b1bddbfae343f4afd5cdf3edaacc3c50 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 3 May 2017 21:18:06 -0500 Subject: [PATCH] Add support for line edit validators in wizards --- src/studio/lib/newwizard.cpp | 30 +++++++++++++++++++++++------- src/studio/lib/newwizard.hpp | 16 +++++++++++++++- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/studio/lib/newwizard.cpp b/src/studio/lib/newwizard.cpp index 85a6a763..25d40da9 100644 --- a/src/studio/lib/newwizard.cpp +++ b/src/studio/lib/newwizard.cpp @@ -18,6 +18,8 @@ namespace nostalgia { namespace studio { +using std::function; + WizardSelect::WizardSelect() { m_listWidget = new QListWidget(this); auto layout = new QVBoxLayout(this); @@ -74,6 +76,17 @@ void WizardSelect::itemSelected(int row) { } +WizardConclusionPage::WizardConclusionPage(QString msg) { + auto text = new QLabel(msg, this); + auto layout = new QVBoxLayout(this); + layout->addWidget(text); + setLayout(layout); +} + +WizardConclusionPage::~WizardConclusionPage() { +} + + WizardFormPage::WizardFormPage() { m_layout = new QGridLayout(this); m_layout->setColumnMinimumWidth(0, 20); @@ -118,7 +131,7 @@ bool WizardFormPage::validatePage() { return retval; } -void WizardFormPage::addLineEdit(QString displayName, QString fieldName, QString defaultVal) { +void WizardFormPage::addLineEdit(QString displayName, QString fieldName, QString defaultVal, function validator) { auto lbl = new QLabel(displayName, this); auto le = new QLineEdit(this); lbl->setBuddy(le); @@ -126,18 +139,21 @@ void WizardFormPage::addLineEdit(QString displayName, QString fieldName, QString m_layout->addWidget(lbl, m_currentLine, 0); m_layout->addWidget(le, m_currentLine, 1); - m_fields[fieldName].defaultValue = defaultVal; - m_fields[fieldName].lineEdit = le; + auto field = &m_fields[fieldName]; + + field->defaultValue = defaultVal; + field->lineEdit = le; + field->validator = validator; registerField(fieldName, le); - connect(le, &QLineEdit::textChanged, [this, fieldName, le](QString txt) { - if (m_fields[fieldName].value == "" && txt != "") { + connect(le, &QLineEdit::textChanged, [this, fieldName, le, field](QString txt) { + if (field->value == "" && txt != "") { m_validFields++; - } else if (m_fields[fieldName].value != "" && txt == "") { + } else if (field->value != "" && txt == "") { m_validFields--; } - m_fields[fieldName].value = txt; + field->value = txt; emit completeChanged(); } ); diff --git a/src/studio/lib/newwizard.hpp b/src/studio/lib/newwizard.hpp index 60c9cbbc..cd05be8b 100644 --- a/src/studio/lib/newwizard.hpp +++ b/src/studio/lib/newwizard.hpp @@ -44,6 +44,7 @@ class WizardSelect: public QWizardPage { void itemSelected(int row); }; + class WizardFormPage: public QWizardPage { Q_OBJECT private: @@ -69,7 +70,9 @@ class WizardFormPage: public QWizardPage { bool validatePage() override; - void addLineEdit(QString displayName, QString fieldName, QString defaultVal = ""); + void addLineEdit(QString displayName, QString fieldName, + QString defaultVal = "", + std::function validator = [](QString) { return 0; }); void addDirBrowse(QString displayName, QString fieldName, QString defaultVal = QDir::homePath()); @@ -77,6 +80,17 @@ class WizardFormPage: public QWizardPage { }; +class WizardConclusionPage: public QWizardPage { + Q_OBJECT + private: + + public: + WizardConclusionPage(QString msg); + + virtual ~WizardConclusionPage(); +}; + + class Wizard: public QWizard { Q_OBJECT