Compare commits

..

No commits in common. "7343174056948ee45f2ed08692c8556e5f16a01a" and "3193e4469bb2aacd0c84b8d31d7eaa768da877e9" have entirely different histories.

8 changed files with 49 additions and 137 deletions

View File

@ -10,9 +10,7 @@ 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,4 +1,3 @@
pragma ComponentBehavior: Bound
import QtQuick import QtQuick
import QtQuick.Controls import QtQuick.Controls

View File

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

View File

@ -1,23 +0,0 @@
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,4 +1,3 @@
pragma ComponentBehavior: Bound
import QtQuick import QtQuick
import QtQuick.Controls import QtQuick.Controls

View File

@ -6,7 +6,6 @@
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);
@ -15,30 +14,16 @@ 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]() {
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL", m_connectionName); QString connectionName("main");
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(m_connectionName); QSqlDatabase::removeDatabase(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,16 +16,12 @@ 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