From ca35df241f4c7dcbfcb68d7ac17b1bc6b95ba4c6 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Thu, 11 Feb 2016 07:30:33 +0100 Subject: [PATCH] Added support for binding simple data to combo boxes. (not tested) --- core/autoform.h | 29 +++++++++++++++++++++++++++++ core/combodata.cpp | 33 +++++++++++++++++++++++++++++++++ core/combodata.h | 25 +++++++++++++++++++++++++ core/context.cpp | 6 +++--- core/core.pro | 6 ++++-- 5 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 core/combodata.cpp create mode 100644 core/combodata.h diff --git a/core/autoform.h b/core/autoform.h index 0ad1603..84fa1b0 100644 --- a/core/autoform.h +++ b/core/autoform.h @@ -8,10 +8,12 @@ #include #include #include +#include #include "iform.h" #include "service.h" #include "ivalidator.h" +#include "combodata.h" template @@ -48,6 +50,10 @@ public: } } + void registerBinding(QComboBox *combo, const QList &values) { + m_bindCombos[combo] = values; + } + void registerValidator(IValidator *validator) { m_validators.append(validator); } @@ -60,6 +66,7 @@ private: QSharedPointer m_entity; QList m_bindWidgets; QList m_validators; + QHash > m_bindCombos; bool m_newRec; void bindToUi() { @@ -67,6 +74,24 @@ private: const char* prop = widget->metaObject()->userProperty().name(); widget->setProperty(prop, ((QObject*)m_entity.data())->property(widget->objectName().toStdString().c_str())); } + + foreach (QComboBox *combo, m_bindCombos.keys()) { + int idx = 0; + QVariant field = ((QObject*)m_entity.data())->property(combo->objectName().toStdString().c_str()); + + combo->clear(); + for (int i = 0; i < m_bindCombos[combo].size(); i++) { + ComboData data = m_bindCombos[combo][i]; + combo->addItem(data.label(), data.index()); + + if (field == data.index()) { + idx = i; + } + } + + combo->setCurrentIndex(idx); + } + bindOtherToUi(); } @@ -83,6 +108,10 @@ private: ((QObject*)m_entity.data())->setProperty(widget->objectName().toStdString().c_str(), widget->property(prop)); } + foreach (QComboBox *combo, m_bindCombos.keys()) { + ((QObject*)m_entity.data())->setProperty(combo->objectName().toStdString().c_str(), combo->currentData()); + } + return bindOtherToData(); } diff --git a/core/combodata.cpp b/core/combodata.cpp new file mode 100644 index 0000000..b7ffef3 --- /dev/null +++ b/core/combodata.cpp @@ -0,0 +1,33 @@ +#include "combodata.h" + +ComboData::ComboData(const QVariant &index, const QString &label) +{ + m_index = index; + m_label = label; +} + +ComboData::~ComboData() +{ + +} +QVariant ComboData::index() const +{ + return m_index; +} + +void ComboData::setIndex(const QVariant &index) +{ + m_index = index; +} +QString ComboData::label() const +{ + return m_label; +} + +void ComboData::setLabel(const QString &label) +{ + m_label = label; +} + + + diff --git a/core/combodata.h b/core/combodata.h new file mode 100644 index 0000000..37d7ff9 --- /dev/null +++ b/core/combodata.h @@ -0,0 +1,25 @@ +#ifndef COMBODATA_H +#define COMBODATA_H + +#include + +#include "core_global.h" + +class CORESHARED_EXPORT ComboData +{ +public: + ComboData(const QVariant &index, const QString &label); + ~ComboData(); + + QVariant index() const; + void setIndex(const QVariant &index); + + QString label() const; + void setLabel(const QString &label); + +private: + QVariant m_index; + QString m_label; +}; + +#endif // COMBODATA_H diff --git a/core/context.cpp b/core/context.cpp index a28b97c..f23e585 100644 --- a/core/context.cpp +++ b/core/context.cpp @@ -49,7 +49,7 @@ void Context::loadPlugins() m_plugins.append(new Users); m_plugins.append(new Roles); - QDir pluginsDir(qApp->applicationDirPath() + "/../plugins"); + QDir pluginsDir(qApp->applicationDirPath() + "/../../plugins"); foreach (QString fileName, pluginsDir.entryList(QStringList() << "*.so" << "*.dll")) { QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName)); @@ -96,10 +96,10 @@ void Context::destroy() m_dbOpened = false; } - if (m_settings != NULL && m_settings->parent() == NULL) + /*if (m_settings != NULL && m_settings->parent() == NULL) { delete m_settings; - } + }*/ foreach (IPlugin *plugin, m_plugins) { diff --git a/core/core.pro b/core/core.pro index 509247f..474644a 100644 --- a/core/core.pro +++ b/core/core.pro @@ -43,7 +43,8 @@ SOURCES += \ savefilterdialog.cpp \ filterdialog.cpp \ itablemodel.cpp \ - iservice.cpp + iservice.cpp \ + combodata.cpp HEADERS += core.h\ core_global.h \ @@ -84,7 +85,8 @@ HEADERS += core.h\ filterdialog.h \ itablemodel.h \ data/core_global.h \ - iservice.h + iservice.h \ + combodata.h unix { target.path = /usr/lib