initial commit Base app, core lib, plugin system

print
Zdenek Jonak 9 years ago
commit 53d56f2e13

@ -0,0 +1,29 @@
#include <context.h>
#include "accommodation.h"
#include <QDebug>
#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<Person>();
}
QString Accommodation::pluginId()
{
return "ACCOMMODATION";
}

@ -0,0 +1,25 @@
#ifndef ACCOMMODATION_H
#define ACCOMMODATION_H
#include "accommodation_global.h"
#include <context.h>
#include <iplugin.h>
#include <QObject>
#include <QtPlugin>
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

@ -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)

@ -0,0 +1,12 @@
#ifndef ACCOMMODATION_GLOBAL_H
#define ACCOMMODATION_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(ACCOMMODATION_LIBRARY)
# define ACCOMMODATIONSHARED_EXPORT Q_DECL_EXPORT
#else
# define ACCOMMODATIONSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // ACCOMMODATION_GLOBAL_H

@ -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();
}

@ -0,0 +1,25 @@
#ifndef ACCOMMODATIONFORM_H
#define ACCOMMODATIONFORM_H
#include <QWidget>
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

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AccommodationForm</class>
<widget class="QWidget" name="AccommodationForm">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>828</width>
<height>548</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QTableView" name="tableView"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

@ -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;
}

@ -0,0 +1,34 @@
#ifndef PERSON_H
#define PERSON_H
#include <QObject>
#include <QString>
#include <odb/core.hxx>
#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

@ -0,0 +1,31 @@
#include "dialog.h"
#include "ui_dialog.h"
#include <context.h>
#include <iplugin.h>
#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<Person> p(new Person());
p->setFirstName(ui->lineEdit->text());
p->setLastName(ui->lineEdit_2->text());
plugin->service<Person>()->save(p);
}

@ -0,0 +1,25 @@
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
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

@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>544</width>
<height>355</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>30</x>
<y>240</y>
<width>341</width>
<height>32</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>180</x>
<y>40</y>
<width>113</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_2">
<property name="geometry">
<rect>
<x>180</x>
<y>90</y>
<width>113</width>
<height>23</height>
</rect>
</property>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</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>Dialog</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>

@ -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

@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

@ -0,0 +1,61 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <context.h>
#include <iplugin.h>
#include <QPushButton>
#include <QVBoxLayout>
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");
}

@ -0,0 +1,30 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#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

@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>825</width>
<height>538</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QWidget" name="navigation" native="true">
<layout class="QVBoxLayout" name="verticalLayout_2"/>
</widget>
</item>
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="tabPosition">
<enum>QTabWidget::North</enum>
</property>
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>825</width>
<height>25</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionOpen_database"/>
<addaction name="actionExit"/>
</widget>
<addaction name="menuFile"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionExit">
<property name="text">
<string>Exit</string>
</property>
</action>
<action name="actionOpen_database">
<property name="text">
<string>Open database...</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

@ -0,0 +1,51 @@
#include <QDir>
#include <QApplication>
#include <QPluginLoader>
#include "context.h"
#include "iplugin.h"
#include <odb/sqlite/database.hxx>
Context &Context::instance()
{
static Context ctx;
return ctx;
}
QList<IPlugin *> 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<IPlugin*>(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;
}

@ -0,0 +1,25 @@
#ifndef CONTEXT_H
#define CONTEXT_H
#include <QList>
#include <odb/database.hxx>
class IPlugin;
class Context
{
public:
static Context &instance();
QList<IPlugin*> plugins();
void loadPlugins();
void openDb(const QString &path);
odb::database *db();
private:
Context();
QList<IPlugin*> m_plugins;
odb::database *m_db;
};
#endif // CONTEXT_H

@ -0,0 +1,6 @@
#include "core.h"
Core::Core()
{
}

@ -0,0 +1,13 @@
#ifndef CORE_H
#define CORE_H
#include "core_global.h"
class CORESHARED_EXPORT Core
{
public:
Core();
};
#endif // CORE_H

@ -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)

@ -0,0 +1,12 @@
#ifndef CORE_GLOBAL_H
#define CORE_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(CORE_LIBRARY)
# define CORESHARED_EXPORT Q_DECL_EXPORT
#else
# define CORESHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // CORE_GLOBAL_H

@ -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;
}

@ -0,0 +1,34 @@
#ifndef USER_H
#define USER_H
#include <QObject>
#include <QString>
#include <odb/core.hxx>
#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

@ -0,0 +1,39 @@
#ifndef IPLUGIN_H
#define IPLUGIN_H
#include <QString>
#include <QWidget>
#include <QtPlugin>
#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<class T>
Service<T> *service() {
(Service<T>*)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

@ -0,0 +1,48 @@
#ifndef SERVICE_H
#define SERVICE_H
#include <QList>
#include <QSharedPointer>
#include <odb/core.hxx>
#include <odb/transaction.hxx>
#include <odb/database.hxx>
#include <odb/result.hxx>
template<class T>
class Service
{
public:
Service() { }
QList<QSharedPointer<T> > all() {
odb::database *db = Context::instance().db();
odb::transaction tx(db->begin());
odb::result<T> res = db->template query<T>();
QList<QSharedPointer<T> > ret;
for (typename odb::result<T>::iterator it = res.begin(); it != res.end(); it++) {
QSharedPointer<T> entity = db->template load<T>(((T*)*it)->id());
ret.append(entity);
}
tx.commit();
return ret;
}
void save(QSharedPointer<T> entity) {
odb::database *db = Context::instance().db();
odb::transaction tx(db->begin());
db->persist(entity);
tx.commit();
}
QSharedPointer<T> loadById(int id) {
odb::database *db = Context::instance().db();
odb::transaction tx(db->begin());
db->template load<T>(id);
tx.commit();
}
};
#endif // SERVICE_H

@ -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

@ -0,0 +1,6 @@
TEMPLATE = subdirs
SUBDIRS += \
core \
application \
accommodation
Loading…
Cancel
Save