Compare commits

...

2 Commits

8 changed files with 137 additions and 49 deletions

View File

@ -10,7 +10,9 @@ qt_add_qml_module(${PROJECT_NAME}
database.cpp database.h database.cpp database.h
QML_FILES QML_FILES
ErrorNotification.qml ErrorNotification.qml
LoginPage.qml
Main.qml Main.qml
ReservationsPage.qml
SelectableLabel.qml SelectableLabel.qml
) )

View File

@ -1,3 +1,4 @@
pragma ComponentBehavior: Bound
import QtQuick import QtQuick
import QtQuick.Controls import QtQuick.Controls

59
src/LoginPage.qml Normal file
View File

@ -0,0 +1,59 @@
pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
id: page
title: qsTr("Login")
ColumnLayout {
Label {
text: qsTr("&User:")
}
TextField {
id: user
focus: true
validator: RegularExpressionValidator {
regularExpression: /[^s].*/
}
onAccepted: function () {
loginAction.trigger();
}
}
Label {
text: qsTr("&Password:")
}
TextField {
id: password
echoMode: TextInput.Password
onAccepted: function () {
loginAction.trigger();
}
}
Button {
action: loginAction
}
}
Action {
id: loginAction
enabled: user.acceptableInput
text: "&Login"
onTriggered: function () {
Database.open(user.text, password.text);
}
}
}

View File

@ -1,49 +1,35 @@
pragma ComponentBehavior: Bound
import QtQuick import QtQuick
import QtQuick.Controls import QtQuick.Controls
import QtQuick.Layouts
import Camper import Camper
ApplicationWindow { ApplicationWindow {
height: 480 height: 480
title: qsTr("Camper") title: pageStack.title ? qsTr("%1 — Camper").arg(pageStack.title) : qsTr("Camper")
visible: true visible: true
width: 640 width: 640
ColumnLayout { StackView {
Label { id: pageStack
text: qsTr("&User:")
property string title: (currentItem as Page)?.title ?? ""
anchors.fill: parent
focus: true
initialItem: loginPage
}
Component {
id: loginPage
LoginPage {
} }
}
TextField { Component {
id: user id: reservationsPage
focus: true ReservationsPage {
validator: RegularExpressionValidator {
regularExpression: /[^s].*/
}
onAccepted: function () {
loginAction.trigger();
}
}
Label {
text: qsTr("&Password:")
}
TextField {
id: password
echoMode: TextInput.Password
onAccepted: function () {
loginAction.trigger();
}
}
Button {
action: loginAction
} }
} }
@ -59,22 +45,19 @@ ApplicationWindow {
} }
} }
Action {
id: loginAction
enabled: user.acceptableInput
text: "&Login"
onTriggered: function () {
Database.open(user.text, password.text);
}
}
Connections { Connections {
function onClosed() {
pageStack.replace(null, loginPage);
}
function onErrorOcurred(errorMessage) { function onErrorOcurred(errorMessage) {
errorNotification.show(errorMessage); errorNotification.show(errorMessage);
} }
function onOpened() {
pageStack.replace(reservationsPage);
}
target: Database target: Database
} }
} }

23
src/ReservationsPage.qml Normal file
View File

@ -0,0 +1,23 @@
pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Controls
Page {
title: qsTr("Reservations")
Button {
action: logoutAction
}
Action {
id: logoutAction
icon.name: "system-log-out"
shortcut: "Ctrl+L"
text: qsTr("&Log out")
onTriggered: function () {
Database.close();
}
}
}

View File

@ -1,3 +1,4 @@
pragma ComponentBehavior: Bound
import QtQuick import QtQuick
import QtQuick.Controls import QtQuick.Controls

View File

@ -6,6 +6,7 @@
Database::Database(QObject *parent) Database::Database(QObject *parent)
: QObject{parent} : QObject{parent}
, m_pool{} , m_pool{}
, m_connectionName{"main"}
{ {
m_pool.setMaxThreadCount(1); m_pool.setMaxThreadCount(1);
m_pool.setExpiryTimeout(-1); m_pool.setExpiryTimeout(-1);
@ -14,16 +15,30 @@ Database::Database(QObject *parent)
QFuture<void> Database::open(const QString &user, const QString &password) QFuture<void> Database::open(const QString &user, const QString &password)
{ {
return QtConcurrent::run(&m_pool, [this, user, password]() { return QtConcurrent::run(&m_pool, [this, user, password]() {
QString connectionName("main"); QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL", m_connectionName);
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL", connectionName);
db.setConnectOptions("service=camper; options=-csearch_path=camper,public"); db.setConnectOptions("service=camper; options=-csearch_path=camper,public");
if (!db.open(user, password)) { if (db.open(user, password)) {
emit opened();
} else {
const QString errorMessage(db.lastError().text()); const QString errorMessage(db.lastError().text());
db = QSqlDatabase(); // Otherwise removeDatabase complains is still being used. db = QSqlDatabase(); // Otherwise removeDatabase complains is still being used.
QSqlDatabase::removeDatabase(connectionName); QSqlDatabase::removeDatabase(m_connectionName);
emit errorOcurred(errorMessage); emit errorOcurred(errorMessage);
} }
}); });
} }
QFuture<void> Database::close()
{
return QtConcurrent::run(&m_pool, [this]() {
QSqlDatabase db = QSqlDatabase::database(m_connectionName);
if (!db.isValid()) {
return;
}
db.close();
QSqlDatabase::removeDatabase(m_connectionName);
emit closed();
});
}
#include "moc_database.cpp" #include "moc_database.cpp"

View File

@ -16,12 +16,16 @@ public:
explicit Database(QObject *parent = nullptr); explicit Database(QObject *parent = nullptr);
Q_INVOKABLE QFuture<void> open(const QString &user, const QString &password); Q_INVOKABLE QFuture<void> open(const QString &user, const QString &password);
Q_INVOKABLE QFuture<void> close();
signals: signals:
void closed();
void errorOcurred(const QString &errorMessage); void errorOcurred(const QString &errorMessage);
void opened();
private: private:
QThreadPool m_pool; QThreadPool m_pool;
QString m_connectionName;
}; };
#endif // DATABASE_H #endif // DATABASE_H