first commit
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
#include "cconfigdialog.h"
|
||||
#include "ui_cconfigdialog.h"
|
||||
|
||||
#include <QFileDialog>
|
||||
#include <QDir>
|
||||
|
||||
|
||||
cConfigDialog::cConfigDialog(QWidget *parent) :
|
||||
QDialog(parent),
|
||||
ui(new Ui::cConfigDialog)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
ui->m_lpButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
|
||||
}
|
||||
|
||||
cConfigDialog::~cConfigDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void cConfigDialog::setFile(const QString& szFile)
|
||||
{
|
||||
ui->m_lpFile->setText(szFile);
|
||||
}
|
||||
|
||||
QString cConfigDialog::file()
|
||||
{
|
||||
return(ui->m_lpFile->text());
|
||||
}
|
||||
|
||||
void cConfigDialog::on_m_lpBrowse_clicked()
|
||||
{
|
||||
QString szFile = ui->m_lpFile->text();
|
||||
if(szFile.isEmpty())
|
||||
szFile = QDir::homePath();
|
||||
|
||||
szFile = QFileDialog::getOpenFileName(this, tr("Select Database"), szFile, tr("Database Files (*.db)"));
|
||||
if(szFile.isNull())
|
||||
return;
|
||||
ui->m_lpFile->setText(szFile);
|
||||
}
|
||||
|
||||
void cConfigDialog::on_m_lpFile_textChanged(const QString &arg1)
|
||||
{
|
||||
if(arg1.isEmpty())
|
||||
ui->m_lpButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
|
||||
else
|
||||
ui->m_lpButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef CCONFIGDIALOG_H
|
||||
#define CCONFIGDIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
|
||||
namespace Ui {
|
||||
class cConfigDialog;
|
||||
}
|
||||
|
||||
class cConfigDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit cConfigDialog(QWidget *parent = 0);
|
||||
~cConfigDialog();
|
||||
|
||||
void setFile(const QString& szFile);
|
||||
QString file();
|
||||
private slots:
|
||||
void on_m_lpBrowse_clicked();
|
||||
|
||||
void on_m_lpFile_textChanged(const QString &arg1);
|
||||
|
||||
private:
|
||||
Ui::cConfigDialog *ui;
|
||||
};
|
||||
|
||||
#endif // CCONFIGDIALOG_H
|
||||
@@ -0,0 +1,85 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>cConfigDialog</class>
|
||||
<widget class="QDialog" name="cConfigDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>72</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>SQLite Plugin Config</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Database File:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="m_lpFile"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="m_lpBrowse">
|
||||
<property name="text">
|
||||
<string>...</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QDialogButtonBox" name="m_lpButtonBox">
|
||||
<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>m_lpButtonBox</sender>
|
||||
<signal>accepted()</signal>
|
||||
<receiver>cConfigDialog</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>m_lpButtonBox</sender>
|
||||
<signal>rejected()</signal>
|
||||
<receiver>cConfigDialog</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,336 @@
|
||||
#include "csqliteplugin.h"
|
||||
#include "cconfigdialog.h"
|
||||
|
||||
#include <QSqlError>
|
||||
#include <QSettings>
|
||||
#include <QSqlQuery>
|
||||
#include <QMessageBox>
|
||||
|
||||
|
||||
bool cSQLitePlugin::connect()
|
||||
{
|
||||
QSettings settings;
|
||||
|
||||
m_db = QSqlDatabase::addDatabase("QSQLITE");
|
||||
m_db.setHostName(settings.value(QString("plugins/%1/hostName").arg(pluginName())).toString());
|
||||
m_db.setDatabaseName(settings.value(QString("plugins/%1/databaseName").arg(pluginName())).toString());
|
||||
|
||||
return(open());
|
||||
}
|
||||
|
||||
bool cSQLitePlugin::init()
|
||||
{
|
||||
QSettings settings;
|
||||
|
||||
m_db = QSqlDatabase::addDatabase("QSQLITE");
|
||||
m_db.setHostName(settings.value(QString("plugins/%1/hostName").arg(pluginName())).toString());
|
||||
m_db.setDatabaseName(settings.value(QString("plugins/%1/databaseName").arg(pluginName())).toString());
|
||||
|
||||
m_bConnected = m_db.open();
|
||||
|
||||
if(m_bConnected)
|
||||
{
|
||||
QStringList tables = m_db.tables();
|
||||
if(tables.count())
|
||||
{
|
||||
if(QMessageBox::question(0, "Tables exist", "Database not empty.\nExisting tables will be deleted.\nContinue?", QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::No)
|
||||
return(m_bConnected);
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
for(int z = 0;z < tables.count();z++)
|
||||
query.exec(QString("DROP TABLE %1;").arg(tables.at(z)));
|
||||
|
||||
if(!query.exec("CREATE TABLE settings ( `var` VARCHAR(100) NOT NULL , `val` VARCHAR(100) NOT NULL )"))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
m_db.close();
|
||||
m_bConnected = false;
|
||||
return(m_bConnected);
|
||||
}
|
||||
|
||||
if(!query.exec("INSERT INTO settings values ('version', '1.0');"))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
m_db.close();
|
||||
m_bConnected = false;
|
||||
return(m_bConnected);
|
||||
}
|
||||
|
||||
if(!query.exec("CREATE TABLE ingredient_group ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(100) NOT NULL )"))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
m_db.close();
|
||||
m_bConnected = false;
|
||||
return(m_bConnected);
|
||||
}
|
||||
|
||||
if(!query.exec("CREATE TABLE ingredient ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(100) NOT NULL, ingredient_group_id INTEGER NOT NULL )"))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
m_db.close();
|
||||
m_bConnected = false;
|
||||
return(m_bConnected);
|
||||
}
|
||||
|
||||
if(!query.exec("CREATE TABLE ingredient_values ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `ingredient_id` INTEGER NOT NULL, `ingredient_number` INTEGER NOT NULL, `value` REAL, FOREIGN KEY(ingredient_id) REFERENCES ingredient(id) )"))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
m_db.close();
|
||||
m_bConnected = false;
|
||||
return(m_bConnected);
|
||||
}
|
||||
|
||||
createGroup("default");
|
||||
}
|
||||
else
|
||||
m_szLastError = m_db.lastError().text();
|
||||
|
||||
return(m_bConnected);
|
||||
}
|
||||
|
||||
bool cSQLitePlugin::open()
|
||||
{
|
||||
m_bConnected = m_db.open();
|
||||
|
||||
if(m_bConnected)
|
||||
{
|
||||
QSqlQuery query(m_db);
|
||||
if(!query.exec("SELECT * FROM settings"))
|
||||
{
|
||||
init();
|
||||
|
||||
if(!query.exec("SELECT * FROM settings"))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
m_db.close();
|
||||
m_bConnected = false;
|
||||
return(m_bConnected);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
m_szLastError = m_db.lastError().text();
|
||||
|
||||
return(m_bConnected);
|
||||
}
|
||||
|
||||
QString cSQLitePlugin::lastError()
|
||||
{
|
||||
return(m_szLastError);
|
||||
}
|
||||
|
||||
bool cSQLitePlugin::config()
|
||||
{
|
||||
cConfigDialog dlg;
|
||||
QSettings settings;
|
||||
dlg.setFile(settings.value(QString("plugins/%1/databaseName").arg(pluginName())).toString());
|
||||
|
||||
if(dlg.exec() == QDialog::Accepted)
|
||||
{
|
||||
m_db.close();
|
||||
settings.setValue(QString("plugins/%1/databaseName").arg(pluginName()), dlg.file());
|
||||
m_db.setDatabaseName(settings.value(QString("plugins/%1/databaseName").arg(pluginName())).toString());
|
||||
return(open());
|
||||
}
|
||||
return(m_bConnected);
|
||||
}
|
||||
|
||||
qint32 cSQLitePlugin::exists(const QString& szName, const QString& szGroup)
|
||||
{
|
||||
if(!m_bConnected)
|
||||
return(-1);
|
||||
|
||||
qint32 iGroup = existsGroup(szGroup);
|
||||
if(iGroup == -1)
|
||||
return(-1);
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
|
||||
if(!query.exec(QString("SELECT id FROM ingredient WHERE name = \"%1\" AND ingredient_group_id=%2").arg(szName).arg(iGroup)))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
return(-1);
|
||||
}
|
||||
query.first();
|
||||
if(!query.isValid())
|
||||
return(-1);
|
||||
|
||||
return(query.value(0).toInt());
|
||||
}
|
||||
|
||||
qint32 cSQLitePlugin::existsGroup(const QString& szGroup)
|
||||
{
|
||||
if(!m_bConnected)
|
||||
return(-1);
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
|
||||
if(!query.exec(QString("SELECT id FROM ingredient_group WHERE name = \"%1\"").arg(szGroup)))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
return(-1);
|
||||
}
|
||||
query.first();
|
||||
if(!query.isValid())
|
||||
return(-1);
|
||||
|
||||
return(query.value(0).toInt());
|
||||
}
|
||||
|
||||
qint32 cSQLitePlugin::existsIngredient(qint32 id, qint16 ingredientNumber)
|
||||
{
|
||||
if(!m_bConnected)
|
||||
return(-1);
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
|
||||
if(!query.exec(QString("SELECT id FROM ingredient_values WHERE ingredient_id = %1 AND ingredient_number = %2").arg(id).arg(ingredientNumber)))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
return(-1);
|
||||
}
|
||||
query.first();
|
||||
if(!query.isValid())
|
||||
return(-1);
|
||||
|
||||
return(query.value(0).toInt());
|
||||
}
|
||||
|
||||
qint32 cSQLitePlugin::create(const QString& szName, const QString& szGroup)
|
||||
{
|
||||
if(!m_bConnected)
|
||||
return(-1);
|
||||
|
||||
if(exists(szName, szGroup) != -1)
|
||||
return(-1);
|
||||
|
||||
qint32 iGroup = existsGroup(szGroup);
|
||||
if(iGroup == -1)
|
||||
iGroup = createGroup(szGroup);
|
||||
if(iGroup == -1)
|
||||
return(-1);
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
if(!query.exec(QString("INSERT INTO ingredient (name, ingredient_group_id) VALUES (\"%1\", %2)").arg(szName).arg(iGroup)))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
return(-1);
|
||||
}
|
||||
return(exists(szName, szGroup));
|
||||
}
|
||||
|
||||
qint32 cSQLitePlugin::createGroup(const QString& szGroup)
|
||||
{
|
||||
if(!m_bConnected)
|
||||
return(-1);
|
||||
|
||||
if(existsGroup(szGroup) != -1)
|
||||
return(-1);
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
if(!query.exec(QString("INSERT INTO ingredient_group (name) VALUES (\"%1\")").arg(szGroup)))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
return(-1);
|
||||
}
|
||||
return(existsGroup(szGroup));
|
||||
}
|
||||
|
||||
bool cSQLitePlugin::set(qint32 id, qint16 ingredientNumber, qreal value)
|
||||
{
|
||||
if(!m_bConnected)
|
||||
return(-1);
|
||||
|
||||
if(existsIngredient(id, ingredientNumber) != -1)
|
||||
return(-1);
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
if(!query.exec(QString("INSERT INTO ingredient_values (ingredient_id, ingredient_number, value) VALUES (%1, %2, %3)").arg(id).arg(ingredientNumber).arg(value)))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
return(-1);
|
||||
}
|
||||
return(existsIngredient(id, ingredientNumber));
|
||||
}
|
||||
|
||||
INGREDIENT_LIST cSQLitePlugin::ingredients()
|
||||
{
|
||||
if(!m_bConnected)
|
||||
return(INGREDIENT_LIST());
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
if(!query.exec(QString("SELECT ingredient.name, ingredient_group.name FROM ingredient, ingredient_group WHERE ingredient.ingredient_group_id = ingredient_group.id ORDER BY ingredient_group.name, ingredient.name")))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
return(INGREDIENT_LIST());
|
||||
}
|
||||
|
||||
INGREDIENT_LIST list;
|
||||
while(query.next())
|
||||
{
|
||||
INGREDIENT i;
|
||||
i.szGroup = query.value(1).toString();
|
||||
i.szIngredient = query.value(0).toString();
|
||||
list.append(i);
|
||||
}
|
||||
|
||||
return(list);
|
||||
}
|
||||
|
||||
QStringList cSQLitePlugin::groups()
|
||||
{
|
||||
if(!m_bConnected)
|
||||
return(QStringList());
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
if(!query.exec(QString("SELECT name FROM ingredient_group ORDER BY name")))
|
||||
{
|
||||
m_szLastError = query.lastError().text();
|
||||
return(QStringList());
|
||||
}
|
||||
|
||||
QStringList list;
|
||||
while(query.next())
|
||||
list.append(query.value(0).toString());
|
||||
|
||||
return(list);
|
||||
}
|
||||
|
||||
bool cSQLitePlugin::beginTransaction()
|
||||
{
|
||||
if(!m_bConnected)
|
||||
return(false);
|
||||
return(m_db.transaction());
|
||||
}
|
||||
|
||||
bool cSQLitePlugin::endTransaction()
|
||||
{
|
||||
if(!m_bConnected)
|
||||
return(false);
|
||||
return(m_db.commit());
|
||||
}
|
||||
|
||||
QString cSQLitePlugin::author()
|
||||
{
|
||||
return("Herwig Birke");
|
||||
}
|
||||
|
||||
QString cSQLitePlugin::company()
|
||||
{
|
||||
return("WIN-DESIGN");
|
||||
}
|
||||
|
||||
QString cSQLitePlugin::link()
|
||||
{
|
||||
return("http://www.windesign.at");
|
||||
}
|
||||
|
||||
QString cSQLitePlugin::description()
|
||||
{
|
||||
return("This is the description.");
|
||||
}
|
||||
|
||||
QString cSQLitePlugin::copyright()
|
||||
{
|
||||
return("This is the copyright.");
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
#ifndef CSQLITEPLUGIN_H
|
||||
#define CSQLITEPLUGIN_H
|
||||
|
||||
|
||||
#include "cdbinterface.h"
|
||||
|
||||
#include <QtCore>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QSqlDatabase>
|
||||
|
||||
|
||||
/*!
|
||||
\brief
|
||||
|
||||
*/
|
||||
class cSQLitePlugin : public QObject, public cDBInterface
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PLUGIN_METADATA(IID "cSQLitePlugin" FILE "dSQLite.json")
|
||||
Q_INTERFACES(cDBInterface)
|
||||
|
||||
public:
|
||||
/*!
|
||||
\brief
|
||||
|
||||
\return qint16
|
||||
*/
|
||||
qint16 pluginAPIVersion() { return(1); }
|
||||
/*!
|
||||
\brief
|
||||
|
||||
\return QString
|
||||
*/
|
||||
QString pluginName() { return("SQLite DB"); }
|
||||
/*!
|
||||
\brief
|
||||
|
||||
\return qint16
|
||||
*/
|
||||
qint16 pluginVersion() { return(1); }
|
||||
/*!
|
||||
\brief
|
||||
|
||||
\return iType
|
||||
*/
|
||||
iType pluginType() { return(cInterface::iTypeDB); }
|
||||
|
||||
/*!
|
||||
\brief
|
||||
|
||||
\return bool
|
||||
*/
|
||||
bool connect();
|
||||
/*!
|
||||
\brief
|
||||
|
||||
\return bool
|
||||
*/
|
||||
bool init();
|
||||
/*!
|
||||
\brief
|
||||
|
||||
\return QString
|
||||
*/
|
||||
QString lastError();
|
||||
|
||||
/*!
|
||||
\brief
|
||||
|
||||
\return bool
|
||||
*/
|
||||
bool config();
|
||||
|
||||
qint32 exists(const QString& szName, const QString& szGroup);
|
||||
qint32 existsGroup(const QString& szGroup);
|
||||
qint32 existsIngredient(qint32 id, qint16 ingredientNumber);
|
||||
qint32 create(const QString& szName, const QString &szGroup);
|
||||
qint32 createGroup(const QString& szGroup);
|
||||
bool set(qint32 id, qint16 ingredientNumber, qreal value);
|
||||
INGREDIENT_LIST ingredients();
|
||||
QStringList groups();
|
||||
bool beginTransaction();
|
||||
bool endTransaction();
|
||||
|
||||
QString author();
|
||||
QString company();
|
||||
QString link();
|
||||
QString description();
|
||||
QString copyright();
|
||||
|
||||
protected:
|
||||
/*!
|
||||
\brief
|
||||
|
||||
\return bool
|
||||
*/
|
||||
bool open();
|
||||
|
||||
QSqlDatabase m_db; /*!< TODO: describe */
|
||||
QString m_szLastError; /*!< TODO: describe */
|
||||
};
|
||||
|
||||
#endif // CSQLITEPLUGIN_H
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"Keys" : [ ]
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#-------------------------------------------------
|
||||
#
|
||||
# Project created by QtCreator 2016-02-09T11:18:10
|
||||
#
|
||||
#-------------------------------------------------
|
||||
|
||||
QT += core gui sql
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
|
||||
TARGET = dSQLite
|
||||
TEMPLATE = lib
|
||||
CONFIG += plugin
|
||||
|
||||
SOURCES += csqliteplugin.cpp \
|
||||
cconfigdialog.cpp
|
||||
|
||||
HEADERS += csqliteplugin.h \
|
||||
cconfigdialog.h
|
||||
|
||||
DESTDIR = ../bin/plugins
|
||||
INCLUDEPATH += ../Kooky
|
||||
|
||||
DISTFILES += dSQLite.json
|
||||
|
||||
unix {
|
||||
target.path = /usr/lib
|
||||
INSTALLS += target
|
||||
}
|
||||
|
||||
FORMS += \
|
||||
cconfigdialog.ui
|
||||
Reference in New Issue
Block a user