TEMPLATE = app
QT += qml quick sql
+!android: QT += widgets
CONFIG += c++11
SOURCES += main.cpp \
+++ /dev/null
-import QtQuick 2.7
-
-Rectangle {
- property alias mouseArea: mouseArea
- property alias textEdit: textEdit
-
- width: 360
- height: 360
-
- MouseArea {
- id: mouseArea
- anchors.fill: parent
- }
-
- TextEdit {
- id: textEdit
- text: qsTr("Enter some text...")
- verticalAlignment: Text.AlignVCenter
- anchors.top: parent.top
- anchors.horizontalCenter: parent.horizontalCenter
- anchors.topMargin: 20
- Rectangle {
- anchors.fill: parent
- anchors.margins: -10
- color: "transparent"
- border.width: 1
- }
- }
-}
#include "logging.h"
#include <QtSql/QSqlQuery>
#include <QStringList>
+#include <QVariantList>
DbConn::DbConn (QObject* parent) : QObject (parent)
{
return QSqlError();
}
-void DbConn::addService (const QVariant& name, const QVariant& cost) const
+void DbConn::addService (const QString& name, const int& cost) const
{
-
+ QSqlQuery q;
+ if (!q.prepare (this->sqlAddService) ) qCCritical (dbase) << "error while preparing: " << q.lastError();
+ q.addBindValue (name );
+ q.addBindValue (cost );
+ if (!q.exec() ) qCCritical (dbase) << "error: " << q.lastError();
+ else {
+ emit servicesChanged();
+ qCDebug (dbase) << "added new service " << name << ", costing " << cost;
+ }
}
-void DbConn::addCustomer (const QVariant& name, const QVariant& street, const QVariant& number,
- const QVariant& city, const QVariant& zip) const
+void DbConn::addCustomer (const QString& name, const QString& street, const QString& number,
+ const QString& city, const uint8_t& zip) const
{
-
+ QSqlQuery q;
+ if (!q.prepare (this->sqlAddCustomer) ) qCCritical (dbase) << "error while preparing: " << q.lastError();
+ q.addBindValue (name );
+ q.addBindValue (street);
+ q.addBindValue (number );
+ q.addBindValue (city);
+ q.addBindValue (zip );
+ if (!q.exec() ) qCCritical (dbase) << "error: " << q.lastError();
+ else {
+ emit customersChanged();
+ qCDebug (dbase) << "added new customer " << name << " in "
+ << street << " " << number << ", " << zip << " " << city;
+ }
}
-void DbConn::addOrder (const QVariant& customerID, QVariant& date, QVariant& items) const
+void DbConn::addOrder (const int& customerID, const QDate& date, const QList<int>& items) const
{
+ QSqlQuery q;
+ if (!q.prepare (this->sqlAddOrder) ) qCCritical (dbase) << "error while preparing: " << q.lastError();
+ q.addBindValue (customerID );
+ q.addBindValue (date );
+
+ if (!q.exec() ) qCCritical (dbase) << "error: " << q.lastError();
+ else emit ordersChanged();
+ QVariant orderID = q.lastInsertId();
+ q.clear();
+ if (!q.prepare (this->sqlAddOrderItem) ) qCCritical (dbase) << "error while preparing: " << q.lastError();
+ QList<QVariant> orderIDList;
+ for (int i = 0; i < items.length(); i++) {
+ orderIDList.append (orderID);
+ }
+ QVariantList variantItems;
+ for (int item : items) variantItems.append (item);
+ q.addBindValue (orderIDList);
+ q.addBindValue (variantItems);
+ if (!q.execBatch() ) qCCritical (dbase) << "error: " << q.lastError();
+ else emit orderitemsChanged();
}
-void DbConn::addInvoice (const QVariant& date, QVariant& OrderID) const
+void DbConn::addInvoice (const QDate& date, const int& orderID) const
{
-
+ QSqlQuery q;
+ if (!q.prepare (this->sqlAddInvoice) ) qCCritical (dbase) << "error while preparing: " << q.lastError();
+ q.addBindValue (date );
+ q.addBindValue (orderID );
+ if (!q.exec() ) qCCritical (dbase) << "error: " << q.lastError();
+ else emit invoicesChanged();
}
#include <QVariant>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
+#include <QList>
class DbConn : public QObject
{
"zip integer"
")"
};
+ const QLatin1String sqlAddCustomer {
+ "insert or ignore into"
+ " customers "
+ "(name, street, number, city, zip)"
+ " values "
+ "(?,?,?,?,?)"
+ };
const QLatin1String sqlCreateServicesTable {
"create table services("
"id integer primary key autoincrement,"
"costs integer"
")"
};
+ const QLatin1String sqlAddService {
+ "insert or ignore into"
+ " services "
+ "(name, costs)"
+ " values "
+ "(?,?)"
+ };
const QLatin1String sqlCreateOrdersTable {
"create table orders("
"id integer primary key autoincrement,"
"foreign key(customerid) references customers(id)"
")"
};
+ const QLatin1String sqlAddOrder {
+ "insert or ignore into"
+ " orders "
+ "(customerid, date)"
+ " values "
+ "(?,?)"
+ };
const QLatin1String sqlCreateOrderItemsTable {
"create table orderitems("
"id integer primary key autoincrement,"
"foreign key(serviceid) references services(id)"
")"
};
+ const QLatin1String sqlAddOrderItem {
+ "insert or ignore into"
+ " orderitems "
+ "(orderid, serviceid)"
+ " values "
+ "(?,?)"
+ };
const QLatin1String sqlCreateInvoiceTable {
"create table invoices("
"id integer primary key autoincrement,"
"foreign key(orderid) references orders(id)"
")"
};
+ const QLatin1String sqlAddInvoice {
+ "insert or ignore into"
+ " invoices "
+ "(orderid, date)"
+ " values "
+ "(?,?)"
+ };
public:
explicit DbConn (QObject* parent = nullptr);
signals:
+ void servicesChanged() const;
+ void customersChanged() const;
+ void ordersChanged() const;
+ void orderitemsChanged() const;
+ void invoicesChanged() const;
public slots:
- void addService (const QVariant& name, const QVariant& cost) const;
- void addCustomer (const QVariant& name, const QVariant& street, const QVariant& number,
- const QVariant& city, const QVariant& zip) const;
- void addOrder (const QVariant& customerID, QVariant& date, QVariant& items) const;
- void addInvoice (const QVariant& date, QVariant& OrderID) const;
+ void addService (const QString& name, const int& cost) const;
+ void addCustomer (const QString& name, const QString& street, const QString& number,
+ const QString& city, const uint8_t& zip) const;
+ void addOrder (const int& customerID, const QDate& date, const QList<int>& items) const;
+ void addInvoice (const QDate& date, const int& OrderID) const;
};
#endif // DBCONN_H
--- /dev/null
+import QtQuick 2.9
+import QtQuick.Dialogs 1.2
+import QtQuick.Controls 1.4
+import QtQuick.Layouts 1.3
+
+Dialog {
+ id: addcustomer
+ //visible: true
+ modality: Qt.WindowModal
+ title: qsTr("Add Customer")
+ standardButtons: StandardButton.Save | StandardButton.Cancel
+ onAccepted: dbconn.addCustomer(nameIn.text, streetIn.text, numberIn.text, cityIn.text, zipIn.text)
+ GridLayout {
+ columns: 2
+ Label {text: qsTr("Name")}
+ TextField {id: textIn}
+
+ Label {text: qsTr("Street")}
+ TextField {id: streetIn}
+
+ Label {text: qsTr("Number")}
+ TextField {id: numberIn}
+
+ Label {text: qsTr("City")}
+ TextField {id: cityIn}
+
+ Label {text: qsTr("Zip")}
+ TextField {id: zipIn; validator: IntValidator {bottom: 0;top: 99999}}
+ }
+}
--- /dev/null
+import QtQuick 2.9
+import QtQuick.Dialogs 1.2
+import QtQuick.Controls 1.4
+import QtQuick.Layouts 1.3
+
+
+Dialog {
+ id: addservice
+ //visible: true
+ modality: Qt.WindowModal
+ title: qsTr("Add Service")
+ standardButtons: StandardButton.Save | StandardButton.Cancel
+ onAccepted: dbconn.addService(nameIn.text, costIn.text)
+
+ GridLayout {
+ anchors.fill: parent
+ columns: 2
+ Label {
+ text: qsTr("Name:")
+ }
+ TextField {
+ id: nameIn
+ }
+
+ Label {
+ text: qsTr("Cost:")
+ }
+ TextField {
+ id: costIn
+ validator: IntValidator {bottom: 0}
+ }
+ }
+}
-#include <QGuiApplication>
+#include <QApplication>
#include <QQmlApplicationEngine>
+#include <QQmlContext>
+#include <QtGlobal>
-int main(int argc, char *argv[])
+#include "dbconn.h"
+
+int main (int argc, char* argv[])
{
- QGuiApplication app(argc, argv);
+
+ QApplication app (argc, argv);
QQmlApplicationEngine engine;
- engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
- if (engine.rootObjects().isEmpty())
+ auto root_context = engine.rootContext();
+ DbConn db;
+ root_context->setContextProperty ("dbconn", &db);
+
+ engine.load (QUrl (QStringLiteral ("qrc:/main.qml") ) );
+ if (engine.rootObjects().isEmpty() )
return -1;
return app.exec();
-import QtQuick 2.7
-import QtQuick.Window 2.2
+import QtQuick 2.9
+import QtQuick.Controls 1.4
-Window {
+import "AddBaseData/dialogs"
+
+ApplicationWindow {
+ id: root
visible: true
width: 640
height: 480
- title: qsTr("Hello World")
+ title: qsTr("Cleaning Service")
+ AddService { id: addservice }
+ AddCustomer { id: addcustomer }
- MainForm {
- anchors.fill: parent
- mouseArea.onClicked: {
- console.log(qsTr('Clicked on background. Text: "' + textEdit.text + '"'))
+ menuBar: MenuBar {
+ Menu {
+ title: qsTr("&File")
+ MenuItem {
+ text: qsTr("Add Service")
+ onTriggered: addservice.open()
+ }
+ MenuItem {
+ text: qsTr("Add Customer")
+ onTriggered: addcustomer.open()
+ }
+ MenuItem {
+ text: qsTr("&Quit")
+ onTriggered: Qt.quit()
+ }
}
+
}
+
}
<RCC>
<qresource prefix="/">
<file>main.qml</file>
- <file>MainForm.ui.qml</file>
+ </qresource>
+ <qresource prefix="/AddBaseData">
+ <file>dialogs/AddService.qml</file>
+ <file>dialogs/AddCustomer.qml</file>
</qresource>
</RCC>