Add plugin system
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2016-2017 gtalent2@gmail.com
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "plugin.hpp"
|
||||
|
||||
namespace nostalgia {
|
||||
namespace studio {
|
||||
|
||||
void Plugin::addNewWizard(QString name, std::function<QVector<QWizardPage*>()> make) {
|
||||
m_newWizards.push_back({name, make});
|
||||
}
|
||||
|
||||
void Plugin::addImportWizard(QString name, std::function<QVector<QWizardPage*>()> make) {
|
||||
m_importWizards.push_back({name, make});
|
||||
}
|
||||
|
||||
QVector<WizardMaker> Plugin::newWizards() {
|
||||
return m_newWizards;
|
||||
}
|
||||
|
||||
QVector<WizardMaker> Plugin::importWizards() {
|
||||
return m_importWizards;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2016-2017 gtalent2@gmail.com
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QVector>
|
||||
#include <QWizardPage>
|
||||
|
||||
namespace nostalgia {
|
||||
namespace studio {
|
||||
|
||||
struct WizardMaker {
|
||||
QString name;
|
||||
std::function<QVector<QWizardPage*>()> make;
|
||||
};
|
||||
|
||||
class Plugin {
|
||||
private:
|
||||
QVector<WizardMaker> m_newWizards;
|
||||
QVector<WizardMaker> m_importWizards;
|
||||
|
||||
public:
|
||||
void addNewWizard(QString name, std::function<QVector<QWizardPage*>()> make);
|
||||
|
||||
void addImportWizard(QString name, std::function<QVector<QWizardPage*>()> make);
|
||||
|
||||
QVector<WizardMaker> newWizards();
|
||||
|
||||
QVector<WizardMaker> importWizards();
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#define PluginInterface_iid "net.drinkingtea.nostalgia.studio.Plugin"
|
||||
|
||||
Q_DECLARE_INTERFACE(nostalgia::studio::Plugin, PluginInterface_iid)
|
||||
@@ -132,6 +132,10 @@ WizardFormPage::~WizardFormPage() {
|
||||
}
|
||||
}
|
||||
|
||||
int WizardFormPage::accept() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WizardFormPage::initializePage() {
|
||||
for (auto it = m_fields.begin(); it != m_fields.end(); it++) {
|
||||
auto key = it.key();
|
||||
@@ -223,8 +227,8 @@ void WizardFormPage::addLineEdit(QString displayName, QString fieldName, QString
|
||||
m_currentLine++;
|
||||
}
|
||||
|
||||
void WizardFormPage::addPathBrowse(QString displayName, QString fieldName,
|
||||
QString defaultVal, QFileDialog::FileMode fileMode) {
|
||||
void WizardFormPage::addPathBrowse(QString displayName, QString fieldName, QString defaultVal,
|
||||
QFileDialog::FileMode fileMode, QString fileExtensions) {
|
||||
auto layout = new QHBoxLayout();
|
||||
auto lbl = new QLabel(displayName, this);
|
||||
auto le = new QLineEdit("", this);
|
||||
@@ -272,14 +276,18 @@ void WizardFormPage::addPathBrowse(QString displayName, QString fieldName,
|
||||
}
|
||||
);
|
||||
|
||||
connect(btn, &QPushButton::clicked, [this, defaultVal, le, fileMode]() {
|
||||
connect(btn, &QPushButton::clicked, [this, defaultVal, le, fileMode, fileExtensions]() {
|
||||
auto dir = defaultVal;
|
||||
if (dir == "") {
|
||||
dir = QDir::homePath();
|
||||
}
|
||||
if (fileMode == QFileDialog::Directory) {
|
||||
auto p = QFileDialog::getExistingDirectory(this, tr("Select Directory..."), defaultVal);
|
||||
auto p = QFileDialog::getExistingDirectory(this, tr("Select Directory..."), dir);
|
||||
if (p != "") {
|
||||
le->setText(p);
|
||||
}
|
||||
} else if (fileMode == QFileDialog::ExistingFile) {
|
||||
auto p = QFileDialog::getOpenFileName(this, tr("Select File..."), defaultVal);
|
||||
auto p = QFileDialog::getOpenFileName(this, tr("Select File..."), dir, fileExtensions);
|
||||
if (p != "") {
|
||||
le->setText(p);
|
||||
}
|
||||
@@ -317,13 +325,15 @@ Wizard::Wizard(QString windowTitle, QWidget *parent): QWizard(parent) {
|
||||
setModal(true);
|
||||
}
|
||||
|
||||
void Wizard::setAccept(std::function<void()> acceptFunc) {
|
||||
void Wizard::setAccept(std::function<int()> acceptFunc) {
|
||||
m_acceptFunc = acceptFunc;
|
||||
}
|
||||
|
||||
void Wizard::accept() {
|
||||
m_acceptFunc();
|
||||
QDialog::accept();
|
||||
auto page = dynamic_cast<WizardFormPage*>(currentPage());
|
||||
if (page == nullptr || page->accept() == 0) {
|
||||
QDialog::accept();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -69,6 +69,8 @@ class WizardFormPage: public QWizardPage {
|
||||
|
||||
~WizardFormPage();
|
||||
|
||||
virtual int accept();
|
||||
|
||||
void initializePage() override;
|
||||
|
||||
bool validatePage() override;
|
||||
@@ -79,8 +81,10 @@ class WizardFormPage: public QWizardPage {
|
||||
QString defaultVal = "",
|
||||
std::function<int(QString)> validator = [](QString) { return 0; });
|
||||
|
||||
void addPathBrowse(QString displayName, QString fieldName, QString defaultVal = QDir::homePath(),
|
||||
QFileDialog::FileMode fileMode = QFileDialog::AnyFile);
|
||||
void addPathBrowse(QString displayName, QString fieldName,
|
||||
QString defaultVal = QDir::homePath(),
|
||||
QFileDialog::FileMode fileMode = QFileDialog::AnyFile,
|
||||
QString fileExtensions = "");
|
||||
|
||||
void showValidationError(QString msg);
|
||||
};
|
||||
@@ -104,14 +108,13 @@ class WizardConclusionPage: public QWizardPage {
|
||||
|
||||
class Wizard: public QWizard {
|
||||
Q_OBJECT
|
||||
|
||||
private:
|
||||
std::function<void()> m_acceptFunc;
|
||||
std::function<int()> m_acceptFunc;
|
||||
|
||||
public:
|
||||
Wizard(QString windowTitle, QWidget *parent = 0);
|
||||
|
||||
void setAccept(std::function<void()> acceptFunc);
|
||||
void setAccept(std::function<int()> acceptFunc);
|
||||
|
||||
void accept();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user