Added base classes for plugin UI.
This commit is contained in:
+15
-12
@@ -7,15 +7,18 @@
|
||||
#include <QSharedPointer>
|
||||
#include <QMetaMethod>
|
||||
#include <QDebug>
|
||||
#include <QVariant>
|
||||
|
||||
#include "iform.h"
|
||||
#include "service.h"
|
||||
#include "ivalidator.h"
|
||||
|
||||
|
||||
template <class T>
|
||||
class AutoForm : public QWidget
|
||||
class AutoForm : public IForm
|
||||
{
|
||||
public:
|
||||
explicit AutoForm(QWidget *parent = 0) : QWidget(parent) {
|
||||
explicit AutoForm(QWidget *parent = 0) : IForm(parent) {
|
||||
m_newRec = false;
|
||||
}
|
||||
|
||||
@@ -31,6 +34,14 @@ public:
|
||||
bindToUi();
|
||||
}
|
||||
|
||||
QSharedPointer<T> entity() {
|
||||
return m_entity;
|
||||
}
|
||||
|
||||
void setNewRec(bool isNew) {
|
||||
m_newRec = isNew;
|
||||
}
|
||||
|
||||
void registerBinding(QWidget *widget) {
|
||||
if (!m_bindWidgets.contains(widget)) {
|
||||
m_bindWidgets.append(widget);
|
||||
@@ -70,11 +81,6 @@ private:
|
||||
return true;
|
||||
}
|
||||
|
||||
signals:
|
||||
void recordAdded(QSharedPointer<T> entity);
|
||||
void recordUpdated(QSharedPointer<T> entity);
|
||||
void validationError(QString errMessage);
|
||||
|
||||
public slots:
|
||||
bool saveRecord() {
|
||||
if (!bindToData())
|
||||
@@ -82,13 +88,10 @@ public slots:
|
||||
return false;
|
||||
}
|
||||
|
||||
Service<T> service;
|
||||
service.save(m_entity);
|
||||
|
||||
if (m_newRec) {
|
||||
emit recordAdded(m_entity);
|
||||
emit recordAdded();
|
||||
} else {
|
||||
emit recordUpdated(m_entity);
|
||||
emit recordUpdated();
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
+1
-1
@@ -39,7 +39,7 @@ void Context::loadPlugins()
|
||||
IPlugin *corePlugin = new CorePlugin();
|
||||
m_plugins.append(corePlugin);
|
||||
|
||||
QDir pluginsDir(qApp->applicationDirPath() + "/../../plugins");
|
||||
QDir pluginsDir(qApp->applicationDirPath() + "/../plugins");
|
||||
|
||||
foreach (QString fileName, pluginsDir.entryList(QStringList() << "*.so" << "*.dll")) {
|
||||
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
|
||||
|
||||
@@ -6,5 +6,6 @@
|
||||
#include "iplugin.h"
|
||||
#include "imetadataplugin.h"
|
||||
#include "transaction.h"
|
||||
#include "gridform.h"
|
||||
|
||||
#endif // CORE_H
|
||||
|
||||
+15
-2
@@ -19,7 +19,11 @@ SOURCES += \
|
||||
emptystringvalidator.cpp \
|
||||
data/role.cpp \
|
||||
data/permission.cpp \
|
||||
coreplugin.cpp
|
||||
coreplugin.cpp \
|
||||
igridform.cpp \
|
||||
defaultformhandler.cpp \
|
||||
formdialog.cpp \
|
||||
iform.cpp
|
||||
|
||||
HEADERS += core.h\
|
||||
core_global.h \
|
||||
@@ -37,7 +41,12 @@ HEADERS += core.h\
|
||||
data/permission.h \
|
||||
data/core-data.h \
|
||||
coreplugin.h \
|
||||
define.h
|
||||
define.h \
|
||||
gridform.h \
|
||||
igridform.h \
|
||||
defaultformhandler.h \
|
||||
formdialog.h \
|
||||
iform.h
|
||||
|
||||
unix {
|
||||
target.path = /usr/lib
|
||||
@@ -56,3 +65,7 @@ RESOURCES += \
|
||||
|
||||
DISTFILES += \
|
||||
metaData.json
|
||||
|
||||
FORMS += \
|
||||
gridform.ui \
|
||||
formdialog.ui
|
||||
|
||||
+13
-13
@@ -1,13 +1,13 @@
|
||||
#ifndef COREDATA_H
|
||||
#define COREDATA_H
|
||||
|
||||
class User;
|
||||
class Permission;
|
||||
class Role;
|
||||
|
||||
#include "user.h"
|
||||
#include "role.h"
|
||||
#include "permission.h"
|
||||
|
||||
#endif // COREDATA_H
|
||||
|
||||
#ifndef COREDATA_H
|
||||
#define COREDATA_H
|
||||
|
||||
class User;
|
||||
class Permission;
|
||||
class Role;
|
||||
|
||||
#include "user.h"
|
||||
#include "role.h"
|
||||
#include "permission.h"
|
||||
|
||||
#endif // COREDATA_H
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "defaultformhandler.h"
|
||||
|
||||
DefaultFormHandler::DefaultFormHandler()
|
||||
{
|
||||
m_dialog = new FormDialog();
|
||||
}
|
||||
|
||||
DefaultFormHandler::~DefaultFormHandler()
|
||||
{
|
||||
delete m_dialog;
|
||||
}
|
||||
|
||||
void DefaultFormHandler::showForm(IForm *formWidget)
|
||||
{
|
||||
m_dialog->setForm(formWidget);
|
||||
m_dialog->setModal(true);
|
||||
m_dialog->show();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef DEFAULTFORMHANDLER_H
|
||||
#define DEFAULTFORMHANDLER_H
|
||||
|
||||
#include "formdialog.h"
|
||||
#include "iform.h"
|
||||
|
||||
#include "core_global.h"
|
||||
|
||||
class CORESHARED_EXPORT IFormHandler
|
||||
{
|
||||
public:
|
||||
IFormHandler() {}
|
||||
virtual ~IFormHandler() {}
|
||||
|
||||
virtual void showForm(IForm *formWidget) = 0;
|
||||
};
|
||||
|
||||
class DefaultFormHandler : public IFormHandler
|
||||
{
|
||||
public:
|
||||
DefaultFormHandler();
|
||||
virtual ~DefaultFormHandler();
|
||||
|
||||
private:
|
||||
FormDialog *m_dialog;
|
||||
|
||||
// IFormHandler interface
|
||||
public:
|
||||
void showForm(IForm *formWidget) override;
|
||||
};
|
||||
|
||||
#endif // DEFAULTFORMHANDLER_H
|
||||
@@ -0,0 +1,31 @@
|
||||
#include "formdialog.h"
|
||||
#include "ui_formdialog.h"
|
||||
|
||||
FormDialog::FormDialog(QWidget *parent) :
|
||||
QDialog(parent),
|
||||
ui(new Ui::FormDialog)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
m_formSet = false;
|
||||
m_form = NULL;
|
||||
}
|
||||
|
||||
FormDialog::~FormDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void FormDialog::setForm(IForm *formWidget)
|
||||
{
|
||||
if (m_form == NULL)
|
||||
{
|
||||
ui->verticalLayout->addWidget(formWidget);
|
||||
m_form = formWidget;
|
||||
setGeometry(formWidget->geometry());
|
||||
}
|
||||
}
|
||||
|
||||
void FormDialog::on_buttonBox_accepted()
|
||||
{
|
||||
m_form->saveRecord();
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef FORMDIALOG_H
|
||||
#define FORMDIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
#include <QWidget>
|
||||
|
||||
#include "iform.h"
|
||||
|
||||
namespace Ui {
|
||||
class FormDialog;
|
||||
}
|
||||
|
||||
class FormDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit FormDialog(QWidget *parent = 0);
|
||||
~FormDialog();
|
||||
|
||||
void setForm(IForm *formWidget);
|
||||
|
||||
private slots:
|
||||
void on_buttonBox_accepted();
|
||||
|
||||
private:
|
||||
bool m_formSet;
|
||||
IForm *m_form;
|
||||
Ui::FormDialog *ui;
|
||||
};
|
||||
|
||||
#endif // FORMDIALOG_H
|
||||
@@ -0,0 +1,79 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>FormDialog</class>
|
||||
<widget class="QDialog" name="FormDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>543</width>
|
||||
<height>383</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Dialog</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>9</number>
|
||||
</property>
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>accepted()</signal>
|
||||
<receiver>FormDialog</receiver>
|
||||
<slot>accept()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>248</x>
|
||||
<y>254</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>157</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>rejected()</signal>
|
||||
<receiver>FormDialog</receiver>
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>316</x>
|
||||
<y>260</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>286</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
#ifndef GRIDFORM_H
|
||||
#define GRIDFORM_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
#include "autoform.h"
|
||||
#include "autotablemodel.h"
|
||||
#include "context.h"
|
||||
#include "iplugin.h"
|
||||
|
||||
#include "igridform.h"
|
||||
|
||||
template<class T>
|
||||
class GridForm : public IGridForm
|
||||
{
|
||||
|
||||
public:
|
||||
explicit GridForm(QWidget *parent = 0) :
|
||||
IGridForm(parent)
|
||||
{
|
||||
m_form = NULL;
|
||||
m_tableModel = NULL;
|
||||
m_formHandler = new DefaultFormHandler();
|
||||
}
|
||||
virtual ~GridForm()
|
||||
{
|
||||
delete m_formHandler;
|
||||
}
|
||||
|
||||
void setForm(AutoForm<T> *form) {
|
||||
Q_ASSERT(m_form == NULL);
|
||||
|
||||
m_form = form;
|
||||
//m_form->setParent(this);
|
||||
|
||||
connect(m_form, SIGNAL(recordAdded()), this, SLOT(saveNew()));
|
||||
connect(m_form, SIGNAL(recordUpdated()), this, SLOT(saveUpdate()));
|
||||
}
|
||||
|
||||
void setTableModel(AutoTableModel<T> *tableModel) {
|
||||
Q_ASSERT(m_tableModel == NULL);
|
||||
|
||||
m_tableModel = tableModel;
|
||||
}
|
||||
|
||||
void setFormHandler(IFormHandler *handler) {
|
||||
delete m_formHandler;
|
||||
m_formHandler = handler;
|
||||
}
|
||||
|
||||
public slots:
|
||||
void fillData() {
|
||||
if (m_tableModel == NULL) {
|
||||
Q_ASSERT(false);
|
||||
return;
|
||||
}
|
||||
|
||||
m_tableModel->setData(service()->all());
|
||||
tableView()->setModel(m_tableModel);
|
||||
}
|
||||
|
||||
protected slots:
|
||||
void saveNew() {
|
||||
service()->save(m_form->entity());
|
||||
m_tableModel->addRow(m_form->entity());
|
||||
emit dataChanged();
|
||||
}
|
||||
|
||||
void saveUpdate() {
|
||||
service()->update(m_form->entity());
|
||||
emit dataChanged();
|
||||
}
|
||||
|
||||
private:
|
||||
AutoForm<T> *m_form;
|
||||
AutoTableModel<T> *m_tableModel;
|
||||
IFormHandler *m_formHandler;
|
||||
|
||||
Service<T> *service() {
|
||||
IPlugin *plugin = Context::instance().plugin(pluginId());
|
||||
if (plugin == NULL) {
|
||||
Q_ASSERT(false);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Service<T> *service = plugin->service<T>();
|
||||
if (service == NULL) {
|
||||
Q_ASSERT(false);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return service;
|
||||
}
|
||||
|
||||
// IGridForm interface
|
||||
protected:
|
||||
void handleNewRecord() override
|
||||
{
|
||||
if (m_form == NULL)
|
||||
{
|
||||
Q_ASSERT(false);
|
||||
return;
|
||||
}
|
||||
|
||||
m_form->setEntity(QSharedPointer<T>(new T()));
|
||||
m_form->setNewRec(true);
|
||||
m_formHandler->showForm(m_form);
|
||||
}
|
||||
|
||||
void handleEditRecord() override
|
||||
{
|
||||
if (m_form == NULL || m_tableModel == NULL || tableView()->currentIndex().row() < 0)
|
||||
{
|
||||
Q_ASSERT(false);
|
||||
return;
|
||||
}
|
||||
|
||||
m_form->setEntity(m_tableModel->itemFromIndex(tableView()->currentIndex()));
|
||||
m_form->setNewRec(false);
|
||||
m_formHandler->showForm(m_form);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // GRIDFORM_H
|
||||
@@ -0,0 +1,88 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>GridForm</class>
|
||||
<widget class="QWidget" name="GridForm">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>680</width>
|
||||
<height>480</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QWidget" name="widget" native="true">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QToolButton" name="btnNew">
|
||||
<property name="text">
|
||||
<string>N</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="btnEdit">
|
||||
<property name="text">
|
||||
<string>E</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="btnDelete">
|
||||
<property name="text">
|
||||
<string>D</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="btnFilter">
|
||||
<property name="text">
|
||||
<string>F</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="btnPrint">
|
||||
<property name="text">
|
||||
<string>P</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QTableView" name="tableView">
|
||||
<property name="selectionMode">
|
||||
<enum>QAbstractItemView::SingleSelection</enum>
|
||||
</property>
|
||||
<property name="selectionBehavior">
|
||||
<enum>QAbstractItemView::SelectRows</enum>
|
||||
</property>
|
||||
<property name="sortingEnabled">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
@@ -0,0 +1,10 @@
|
||||
#include "iform.h"
|
||||
|
||||
IForm::IForm(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
}
|
||||
|
||||
IForm::~IForm()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef IFORM_H
|
||||
#define IFORM_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class IForm : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit IForm(QWidget *parent = 0);
|
||||
virtual ~IForm();
|
||||
|
||||
signals:
|
||||
void recordAdded();
|
||||
void recordUpdated();
|
||||
void validationError(QString errMessage);
|
||||
|
||||
public slots:
|
||||
virtual bool saveRecord() = 0;
|
||||
};
|
||||
|
||||
#endif // IFORM_H
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "igridform.h"
|
||||
|
||||
#include "ui_gridform.h"
|
||||
|
||||
IGridForm::IGridForm(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
ui(new Ui::GridForm)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
}
|
||||
|
||||
IGridForm::~IGridForm()
|
||||
{
|
||||
}
|
||||
|
||||
void IGridForm::setPluginId(const QString &pluginId)
|
||||
{
|
||||
m_pluginId = pluginId;
|
||||
}
|
||||
|
||||
QString IGridForm::pluginId()
|
||||
{
|
||||
return m_pluginId;
|
||||
}
|
||||
|
||||
QTableView *IGridForm::tableView()
|
||||
{
|
||||
return ui->tableView;
|
||||
}
|
||||
|
||||
|
||||
void IGridForm::on_btnNew_clicked()
|
||||
{
|
||||
handleNewRecord();
|
||||
}
|
||||
|
||||
void IGridForm::on_btnEdit_clicked()
|
||||
{
|
||||
handleEditRecord();
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#ifndef IGRIDFORM_H
|
||||
#define IGRIDFORM_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QString>
|
||||
#include <QTableView>
|
||||
|
||||
#include "defaultformhandler.h"
|
||||
#include "core_global.h"
|
||||
|
||||
namespace Ui {
|
||||
class GridForm;
|
||||
}
|
||||
|
||||
class CORESHARED_EXPORT IGridForm : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit IGridForm(QWidget *parent = 0);
|
||||
virtual ~IGridForm();
|
||||
|
||||
void setPluginId(const QString &pluginId);
|
||||
QString pluginId();
|
||||
QTableView *tableView();
|
||||
|
||||
signals:
|
||||
void dataChanged();
|
||||
|
||||
public slots:
|
||||
virtual void fillData() = 0;
|
||||
|
||||
protected slots:
|
||||
virtual void saveNew() = 0;
|
||||
virtual void saveUpdate() = 0;
|
||||
|
||||
protected:
|
||||
virtual void handleNewRecord() = 0;
|
||||
virtual void handleEditRecord() = 0;
|
||||
//virtual void handleDeleteRecord() = 0;
|
||||
|
||||
private slots:
|
||||
void on_btnNew_clicked();
|
||||
void on_btnEdit_clicked();
|
||||
|
||||
private:
|
||||
QString m_pluginId;
|
||||
IFormHandler *m_formHandler;
|
||||
Ui::GridForm *ui;
|
||||
};
|
||||
|
||||
#endif // IGRIDFORM_H
|
||||
@@ -48,6 +48,16 @@ public:
|
||||
tx.commit();
|
||||
}
|
||||
|
||||
void update(QSharedPointer<T> entity) {
|
||||
odb::database *db = Context::instance().db();
|
||||
|
||||
Q_ASSERT(db);
|
||||
|
||||
Transaction tx;
|
||||
db->update(entity);
|
||||
tx.commit();
|
||||
}
|
||||
|
||||
QSharedPointer<T> loadById(int id) {
|
||||
odb::database *db = Context::instance().db();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user