commit 53d56f2e13e80e793cceef174caea34a652a5de5 Author: Zdenek Jonak Date: Wed Oct 28 19:21:55 2015 +0100 initial commit Base app, core lib, plugin system diff --git a/accommodation/accommodation.cpp b/accommodation/accommodation.cpp new file mode 100644 index 0000000..29c02f4 --- /dev/null +++ b/accommodation/accommodation.cpp @@ -0,0 +1,29 @@ +#include + +#include "accommodation.h" +#include +#include "accommodationform.h" + +#include "data/person.h" + +Accommodation::Accommodation() +{ +} + +QString Accommodation::pluginName() +{ + return "Ubytovani"; +} + +void Accommodation::init() +{ + qDebug() << "init accomodation"; + m_ui = new AccommodationForm(); + m_service = new Service(); +} + +QString Accommodation::pluginId() +{ + return "ACCOMMODATION"; +} + diff --git a/accommodation/accommodation.h b/accommodation/accommodation.h new file mode 100644 index 0000000..1b7a8d3 --- /dev/null +++ b/accommodation/accommodation.h @@ -0,0 +1,25 @@ +#ifndef ACCOMMODATION_H +#define ACCOMMODATION_H + +#include "accommodation_global.h" +#include +#include +#include +#include + +class ACCOMMODATIONSHARED_EXPORT Accommodation : public QObject, IPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID PluginInterface_iid FILE "accommodation.json") + Q_INTERFACES(IPlugin) + +public: + Accommodation(); + + QString pluginName() Q_DECL_OVERRIDE; + void init() Q_DECL_OVERRIDE; + QString pluginId() Q_DECL_OVERRIDE; + +}; + +#endif // ACCOMMODATION_H diff --git a/accommodation/accommodation.json b/accommodation/accommodation.json new file mode 100644 index 0000000..e69de29 diff --git a/accommodation/accommodation.pro b/accommodation/accommodation.pro new file mode 100644 index 0000000..233dd9a --- /dev/null +++ b/accommodation/accommodation.pro @@ -0,0 +1,48 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2015-10-28T15:27:14 +# +#------------------------------------------------- + +QT += widgets + +TARGET = accommodation +TEMPLATE = lib + +DEFINES += ACCOMMODATION_LIBRARY + +SOURCES += accommodation.cpp \ + accommodationform.cpp \ + data/person.cpp \ + dialog.cpp + +HEADERS += accommodation.h\ + accommodation_global.h \ + accommodationform.h \ + data/person.h \ + dialog.h + +unix { + target.path = /usr/lib + INSTALLS += target +} + +win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../core/release/ -lcore +else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../core/debug/ -lcore +else:unix: LIBS += -L$$OUT_PWD/../core/ -lcore + +INCLUDEPATH += $$PWD/../core +DEPENDPATH += $$PWD/../core + +DESTDIR = ../plugins + +OTHER_FILES += \ + accommodation.json + +FORMS += \ + accommodationform.ui \ + dialog.ui + +ODB_FILES = accommodation/data/person.h +H_DIR = $$PWD/data/*.h +include(../odb.pri) diff --git a/accommodation/accommodation_global.h b/accommodation/accommodation_global.h new file mode 100644 index 0000000..c5dddf9 --- /dev/null +++ b/accommodation/accommodation_global.h @@ -0,0 +1,12 @@ +#ifndef ACCOMMODATION_GLOBAL_H +#define ACCOMMODATION_GLOBAL_H + +#include + +#if defined(ACCOMMODATION_LIBRARY) +# define ACCOMMODATIONSHARED_EXPORT Q_DECL_EXPORT +#else +# define ACCOMMODATIONSHARED_EXPORT Q_DECL_IMPORT +#endif + +#endif // ACCOMMODATION_GLOBAL_H diff --git a/accommodation/accommodationform.cpp b/accommodation/accommodationform.cpp new file mode 100644 index 0000000..0c57a40 --- /dev/null +++ b/accommodation/accommodationform.cpp @@ -0,0 +1,22 @@ +#include "accommodationform.h" +#include "ui_accommodationform.h" + +#include "dialog.h" + +AccommodationForm::AccommodationForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::AccommodationForm) +{ + ui->setupUi(this); +} + +AccommodationForm::~AccommodationForm() +{ + delete ui; +} + +void AccommodationForm::on_pushButton_clicked() +{ + Dialog d; + d.open(); +} diff --git a/accommodation/accommodationform.h b/accommodation/accommodationform.h new file mode 100644 index 0000000..0fcdcd9 --- /dev/null +++ b/accommodation/accommodationform.h @@ -0,0 +1,25 @@ +#ifndef ACCOMMODATIONFORM_H +#define ACCOMMODATIONFORM_H + +#include + +namespace Ui { +class AccommodationForm; +} + +class AccommodationForm : public QWidget +{ + Q_OBJECT + +public: + explicit AccommodationForm(QWidget *parent = 0); + ~AccommodationForm(); + +private slots: + void on_pushButton_clicked(); + +private: + Ui::AccommodationForm *ui; +}; + +#endif // ACCOMMODATIONFORM_H diff --git a/accommodation/accommodationform.ui b/accommodation/accommodationform.ui new file mode 100644 index 0000000..68a7aa7 --- /dev/null +++ b/accommodation/accommodationform.ui @@ -0,0 +1,31 @@ + + + AccommodationForm + + + + 0 + 0 + 828 + 548 + + + + Form + + + + + + PushButton + + + + + + + + + + + diff --git a/accommodation/data/person.cpp b/accommodation/data/person.cpp new file mode 100644 index 0000000..e4304b7 --- /dev/null +++ b/accommodation/data/person.cpp @@ -0,0 +1,35 @@ +#include "person.h" + +Person::Person() +{ +} +int Person::getId() const +{ + return id; +} + +void Person::setId(int value) +{ + id = value; +} +QString Person::getFirstName() const +{ + return firstName; +} + +void Person::setFirstName(const QString &value) +{ + firstName = value; +} +QString Person::getLastName() const +{ + return lastName; +} + +void Person::setLastName(const QString &value) +{ + lastName = value; +} + + + diff --git a/accommodation/data/person.h b/accommodation/data/person.h new file mode 100644 index 0000000..3c1e828 --- /dev/null +++ b/accommodation/data/person.h @@ -0,0 +1,34 @@ +#ifndef PERSON_H +#define PERSON_H + +#include +#include + +#include + +#pragma db object +class Person : public QObject +{ + Q_OBJECT +public: + Person(); + + int getId() const; + void setId(int value); + + QString getFirstName() const; + void setFirstName(const QString &value); + + QString getLastName() const; + void setLastName(const QString &value); + +private: + friend class odb::access; +#pragma db id auto + int id; + QString firstName; + QString lastName; + +}; + +#endif // PERSON_H diff --git a/accommodation/dialog.cpp b/accommodation/dialog.cpp new file mode 100644 index 0000000..2319acc --- /dev/null +++ b/accommodation/dialog.cpp @@ -0,0 +1,31 @@ +#include "dialog.h" +#include "ui_dialog.h" + +#include +#include +#include "data/person.h" + +#include "accommodation-odb.hxx" + +Dialog::Dialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::Dialog) +{ + ui->setupUi(this); +} + +Dialog::~Dialog() +{ + delete ui; +} + +void Dialog::on_buttonBox_accepted() +{ + IPlugin *plugin = Context::instance().plugins().at(0); + QSharedPointer p(new Person()); + + p->setFirstName(ui->lineEdit->text()); + p->setLastName(ui->lineEdit_2->text()); + + plugin->service()->save(p); +} diff --git a/accommodation/dialog.h b/accommodation/dialog.h new file mode 100644 index 0000000..5e8c556 --- /dev/null +++ b/accommodation/dialog.h @@ -0,0 +1,25 @@ +#ifndef DIALOG_H +#define DIALOG_H + +#include + +namespace Ui { +class Dialog; +} + +class Dialog : public QDialog +{ + Q_OBJECT + +public: + explicit Dialog(QWidget *parent = 0); + ~Dialog(); + +private slots: + void on_buttonBox_accepted(); + +private: + Ui::Dialog *ui; +}; + +#endif // DIALOG_H diff --git a/accommodation/dialog.ui b/accommodation/dialog.ui new file mode 100644 index 0000000..5ef4676 --- /dev/null +++ b/accommodation/dialog.ui @@ -0,0 +1,88 @@ + + + Dialog + + + + 0 + 0 + 544 + 355 + + + + Dialog + + + + + 30 + 240 + 341 + 32 + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + 180 + 40 + 113 + 23 + + + + + + + 180 + 90 + 113 + 23 + + + + + + + + buttonBox + accepted() + Dialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + Dialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/application/application.pro b/application/application.pro new file mode 100644 index 0000000..488b680 --- /dev/null +++ b/application/application.pro @@ -0,0 +1,27 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2015-10-28T15:23:55 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = prodejna +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + +HEADERS += mainwindow.h + +FORMS += mainwindow.ui + +win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../core/release/ -lcore +else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../core/debug/ -lcore +else:unix: LIBS += -L$$OUT_PWD/../core/ -lcore + +INCLUDEPATH += $$PWD/../core +DEPENDPATH += $$PWD/../core diff --git a/application/main.cpp b/application/main.cpp new file mode 100644 index 0000000..b48f94e --- /dev/null +++ b/application/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/application/mainwindow.cpp b/application/mainwindow.cpp new file mode 100644 index 0000000..309b4ba --- /dev/null +++ b/application/mainwindow.cpp @@ -0,0 +1,61 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +#include +#include + +#include +#include + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + + Context::instance().loadPlugins(); + int i = 0; + + foreach (IPlugin *plugin, Context::instance().plugins()) { + QPushButton *plugButton = new QPushButton(this); + plugButton->setText(plugin->pluginName()); + ui->navigation->layout()->addWidget(plugButton); + plugButton->setProperty(PLUGIN_INDEX, i); + i++; + connect(plugButton, SIGNAL(clicked()), this, SLOT(openPlugin()) ); + } + + ((QVBoxLayout*)ui->navigation->layout())->addStretch(1); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::on_actionExit_triggered() +{ + this->close(); +} + +void MainWindow::openPlugin() +{ + QVariant var = QObject::sender()->property(PLUGIN_INDEX); + IPlugin *plugin = Context::instance().plugins().at(var.toInt()); + + for (int i = 0; i < ui->tabWidget->count(); i++) { + if (ui->tabWidget->widget(i)->objectName() == plugin->pluginId()) { + ui->tabWidget->setCurrentIndex(i); + return; + } + } + + ui->tabWidget->addTab(plugin->ui(), QIcon(), plugin->pluginName()); + ui->tabWidget->widget(ui->tabWidget->count() - 1)->setObjectName(plugin->pluginId()); + ui->tabWidget->setCurrentIndex(ui->tabWidget->count() - 1); +} + +void MainWindow::on_actionOpen_database_triggered() +{ + Context::instance().openDb("/home/jony/db.db"); +} diff --git a/application/mainwindow.h b/application/mainwindow.h new file mode 100644 index 0000000..a33e03b --- /dev/null +++ b/application/mainwindow.h @@ -0,0 +1,30 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +#define PLUGIN_INDEX "plug_index" + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private slots: + void on_actionExit_triggered(); + void openPlugin(); + + void on_actionOpen_database_triggered(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/application/mainwindow.ui b/application/mainwindow.ui new file mode 100644 index 0000000..7b13053 --- /dev/null +++ b/application/mainwindow.ui @@ -0,0 +1,89 @@ + + + MainWindow + + + + 0 + 0 + 825 + 538 + + + + MainWindow + + + + + + + + + + + + QTabWidget::North + + + QTabWidget::Rounded + + + 0 + + + + Tab 1 + + + + + Tab 2 + + + + + + + + + + 0 + 0 + 825 + 25 + + + + + File + + + + + + + + + TopToolBarArea + + + false + + + + + + Exit + + + + + Open database... + + + + + + + diff --git a/core/context.cpp b/core/context.cpp new file mode 100644 index 0000000..216dc74 --- /dev/null +++ b/core/context.cpp @@ -0,0 +1,51 @@ +#include +#include +#include + +#include "context.h" +#include "iplugin.h" + +#include + +Context &Context::instance() +{ + static Context ctx; + return ctx; +} + +QList Context::plugins() +{ + return m_plugins; +} + +void Context::loadPlugins() +{ + QDir pluginsDir(qApp->applicationDirPath() + "/../plugins"); + + foreach (QString fileName, pluginsDir.entryList(QStringList() << "*.so")) { + QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName)); + QObject *p = pluginLoader.instance(); + + if (p != NULL) { + IPlugin *plugin = qobject_cast(p); + if (plugin != NULL) { + plugin->init(); + m_plugins.append(plugin); + } + } + } +} + +void Context::openDb(const QString &path) +{ + if (m_db != NULL) { + delete m_db; + } + + m_db = new odb::sqlite::database(path.toStdString()); +} + +Context::Context() +{ + m_db = NULL; +} diff --git a/core/context.h b/core/context.h new file mode 100644 index 0000000..6491f3e --- /dev/null +++ b/core/context.h @@ -0,0 +1,25 @@ +#ifndef CONTEXT_H +#define CONTEXT_H + +#include +#include + +class IPlugin; + +class Context +{ +public: + static Context &instance(); + QList plugins(); + void loadPlugins(); + void openDb(const QString &path); + odb::database *db(); + +private: + Context(); + QList m_plugins; + odb::database *m_db; + +}; + +#endif // CONTEXT_H diff --git a/core/core.cpp b/core/core.cpp new file mode 100644 index 0000000..a517063 --- /dev/null +++ b/core/core.cpp @@ -0,0 +1,6 @@ +#include "core.h" + + +Core::Core() +{ +} diff --git a/core/core.h b/core/core.h new file mode 100644 index 0000000..dab36ee --- /dev/null +++ b/core/core.h @@ -0,0 +1,13 @@ +#ifndef CORE_H +#define CORE_H + +#include "core_global.h" + +class CORESHARED_EXPORT Core +{ + +public: + Core(); +}; + +#endif // CORE_H diff --git a/core/core.pro b/core/core.pro new file mode 100644 index 0000000..0b2c799 --- /dev/null +++ b/core/core.pro @@ -0,0 +1,32 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2015-10-28T15:25:33 +# +#------------------------------------------------- + +QT += widgets + +TARGET = core +TEMPLATE = lib + +DEFINES += CORE_LIBRARY + +SOURCES += core.cpp \ + data/user.cpp \ + context.cpp + +HEADERS += core.h\ + core_global.h \ + iplugin.h \ + service.h \ + data/user.h \ + context.h + +unix { + target.path = /usr/lib + INSTALLS += target +} + +ODB_FILES = core/data/user.h +H_DIR = $$PWD/data/*.h +include(../odb.pri) diff --git a/core/core_global.h b/core/core_global.h new file mode 100644 index 0000000..b519481 --- /dev/null +++ b/core/core_global.h @@ -0,0 +1,12 @@ +#ifndef CORE_GLOBAL_H +#define CORE_GLOBAL_H + +#include + +#if defined(CORE_LIBRARY) +# define CORESHARED_EXPORT Q_DECL_EXPORT +#else +# define CORESHARED_EXPORT Q_DECL_IMPORT +#endif + +#endif // CORE_GLOBAL_H diff --git a/core/data/user.cpp b/core/data/user.cpp new file mode 100644 index 0000000..f48cb77 --- /dev/null +++ b/core/data/user.cpp @@ -0,0 +1,38 @@ +#include "user.h" + +User::User() +{ +} + +int User::getId() const +{ + return id; +} + +void User::setId(int value) +{ + id = value; +} + +QString User::getLogin() const +{ + return login; +} + +void User::setLogin(const QString &value) +{ + login = value; +} + +QString User::getPassword() const +{ + return password; +} + +void User::setPassword(const QString &value) +{ + password = value; +} + + + diff --git a/core/data/user.h b/core/data/user.h new file mode 100644 index 0000000..90502c7 --- /dev/null +++ b/core/data/user.h @@ -0,0 +1,34 @@ +#ifndef USER_H +#define USER_H + +#include +#include +#include + +#pragma db object +class User : public QObject +{ + Q_OBJECT + +public: + User(); + + int getId() const; + void setId(int value); + + QString getLogin() const; + void setLogin(const QString &value); + + QString getPassword() const; + void setPassword(const QString &value); + +private: + friend class odb::access; + +#pragma db id auto + int id; + QString login; + QString password; +}; + +#endif // USER_H diff --git a/core/iplugin.h b/core/iplugin.h new file mode 100644 index 0000000..8891604 --- /dev/null +++ b/core/iplugin.h @@ -0,0 +1,39 @@ +#ifndef IPLUGIN_H +#define IPLUGIN_H + +#include +#include +#include + +#include "service.h" + +class IPlugin +{ + +public: + IPlugin() { + m_ui = NULL; + } + + virtual ~IPlugin() { } + virtual QString pluginName() = 0; + virtual QString pluginId() = 0; + virtual void init() = 0; + virtual QWidget *ui() { + return m_ui; + } + template + Service *service() { + (Service*)m_service; + } + +protected: + QWidget *m_ui; + void *m_service; +}; + +#define PluginInterface_iid "cz.itsolved.prodejna.IPlugin" + +Q_DECLARE_INTERFACE(IPlugin, PluginInterface_iid) + +#endif // IPLUGIN_H diff --git a/core/service.h b/core/service.h new file mode 100644 index 0000000..00617e1 --- /dev/null +++ b/core/service.h @@ -0,0 +1,48 @@ +#ifndef SERVICE_H +#define SERVICE_H + +#include +#include + +#include +#include +#include +#include + +template +class Service +{ +public: + Service() { } + + QList > all() { + odb::database *db = Context::instance().db(); + odb::transaction tx(db->begin()); + odb::result res = db->template query(); + + QList > ret; + for (typename odb::result::iterator it = res.begin(); it != res.end(); it++) { + QSharedPointer entity = db->template load(((T*)*it)->id()); + ret.append(entity); + } + + tx.commit(); + return ret; + } + + void save(QSharedPointer entity) { + odb::database *db = Context::instance().db(); + odb::transaction tx(db->begin()); + db->persist(entity); + tx.commit(); + } + + QSharedPointer loadById(int id) { + odb::database *db = Context::instance().db(); + odb::transaction tx(db->begin()); + db->template load(id); + tx.commit(); + } +}; + +#endif // SERVICE_H diff --git a/odb.pri b/odb.pri new file mode 100755 index 0000000..8f6d8fd --- /dev/null +++ b/odb.pri @@ -0,0 +1,77 @@ +win32 { + LIBS += -L"d:/prac/qt/lib" + INCLUDEPATH += d:/prac/odb/libodb-2.4.0 + INCLUDEPATH += d:/prac/odb/libodb-qt-2.4.0 + INCLUDEPATH += d:/prac/odb/libodb-sqlite-2.4.0 + INCLUDEPATH += d:/prac/odb/sqlite +} + +win32 { + CONFIG(debug, debug|release) { + LIBS += -lodb-d -lodb-sqlite-d -lodb-qt-d + }else { + LIBS += -lodb -lodb-sqlite -lodb-qt + } +} + +unix { + LIBS += -lodb -lodb-sqlite -lodb-qt +} + +ODB_FLAGS = --database sqlite --profile qt --generate-schema --generate-query --generate-session --at-once --input-name $$TARGET --schema-format sql + +win32 { + ODB_PATH = d:\prac\odb\odb-2.4.0-i686-windows\bin\odb +} +unix { + ODB_PATH = odb +} + +# Select the database we are going to use. +# +DEFINES += DATABASE_SQLITE + +# Suppress unknown pragmas GCC warnings. +# +#QMAKE_CXXFLAGS_WARN_ON = $$QMAKE_CXXFLAGS_WARN_ON -Wno-unknown-pragmas + +# ODB compilation rules. Normally you don't need to change anything here. +# + +# Add the Qt headers directory to the ODB include directory list. +# +ODB_FLAGS += -I $$[QT_INSTALL_HEADERS] +ODB_FLAGS += -I $$[QT_INSTALL_HEADERS]/QtCore + +win32 { + ODB_FLAGS += -I d:/prac/odb/libodb-2.4.0 +} + +# Newer versions of QtCreator do builds in a separate directory. As a +# result, we need to append the source directory to ODB files. +# +for(dir, ODB_FILES) { + ODB_PWD_FILES += $$PWD/$${dir} +} + +win32 { + H_DIR ~= s,/,\\,g +} +export(H_DIR) + + +odb.name = odb ${QMAKE_FILE_IN} +odb.input = ODB_PWD_FILES +odb.output = $$TARGET-odb.cxx +odb.commands = $$ODB_PATH $$ODB_FLAGS ${QMAKE_FILE_IN} +odb.depends = $$ODB_PWD_FILES +odb.variable_out = SOURCES +odb.CONFIG = target_predeps +odb.clean = $$TARGET-odb.cxx $$TARGET-odb.hxx $$TARGET-odb.ixx $$TARGET.sql +QMAKE_EXTRA_COMPILERS += odb + +odbhc.target = odbhc +odbhc.commands = $(COPY) $$H_DIR . +QMAKE_EXTRA_TARGETS += odbhc + +PRE_TARGETDEPS += odbhc diff --git a/prodejna.pro b/prodejna.pro new file mode 100644 index 0000000..949751e --- /dev/null +++ b/prodejna.pro @@ -0,0 +1,6 @@ +TEMPLATE = subdirs + +SUBDIRS += \ + core \ + application \ + accommodation