From 24a55828172f8bfbad3139f02f1bc8826fd61c15 Mon Sep 17 00:00:00 2001 From: Alexandre Petitjean Date: Tue, 5 May 2020 23:09:44 +0200 Subject: [PATCH] Enable 'new' actions only if client exists --- src/app/activitydialog.cpp | 95 ++++++++++++++++++++++++++++++-------- src/app/mainwindow.cpp | 36 +++++++++++---- src/app/mainwindow.h | 2 + 3 files changed, 104 insertions(+), 29 deletions(-) diff --git a/src/app/activitydialog.cpp b/src/app/activitydialog.cpp index 1b9ff98..f4e56fb 100644 --- a/src/app/activitydialog.cpp +++ b/src/app/activitydialog.cpp @@ -1,9 +1,12 @@ #include "activitydialog.h" #include "ui_activitydialog.h" -#include "datareader.h" +#include "helpers.h" + +#include "kemai/kimairequestfactory.h" #include +#include using namespace kemai::app; using namespace kemai::client; @@ -13,20 +16,16 @@ ActivityDialog::ActivityDialog(QWidget* parent) : QDialog(parent), mUi(new Ui::A mUi->setupUi(this); enableSave(false); -// connect(mUi->leName, &QLineEdit::textChanged, this, &ActivityDialog::validateForm); -// -// const auto& countries = DataReader::countries(); -// for (auto it = countries.begin(); it != countries.end(); ++it) -// { -// mUi->cbCountry->addItem(it.value(), it.key()); -// } -// const auto& currencies = DataReader::currencies(); -// for (auto it = currencies.begin(); it != currencies.end(); ++it) -// { -// mUi->cbCurrency->addItem(it.value(), it.key()); -// } -// const auto& timezones = DataReader::timezones(); -// mUi->cbTimezone->addItems(timezones); + mClient = helpers::createClient(); + if (mClient) + { + connect(mClient.data(), &KimaiClient::replyReceived, this, &ActivityDialog::onClientReply); + QTimer::singleShot(200, [=]() { mClient->sendRequest(KimaiRequestFactory::customers()); }); + } + + connect(mUi->cbCustomer, &QComboBox::currentTextChanged, this, &ActivityDialog::onCbCustomerTextChanged); + connect(mUi->cbProject, &QComboBox::currentTextChanged, this, &ActivityDialog::validateForm); + connect(mUi->leName, &QLineEdit::textChanged, this, &ActivityDialog::validateForm); } ActivityDialog::~ActivityDialog() @@ -42,18 +41,74 @@ void ActivityDialog::setActivity(const Activity& activity) Activity ActivityDialog::activity() const { Activity activity; + activity.name = mUi->leName->text(); + + auto idCustomer = mUi->cbCustomer->currentData(Qt::UserRole).toInt(); + auto idProject = mUi->cbProject->currentData(Qt::UserRole).toInt(); + + if (idCustomer >= 0) + { + Project project; + project.customer.id = idCustomer; + project.id = idProject; + + activity.project = project; + } return activity; } void ActivityDialog::enableSave(bool enable) { -// auto btn = mUi->buttonBox->button(QDialogButtonBox::Save); -// if (btn) -// btn->setEnabled(enable); + auto btn = mUi->buttonBox->button(QDialogButtonBox::Save); + if (btn) + btn->setEnabled(enable); +} + +void ActivityDialog::onClientReply(const KimaiReply& reply) +{ + + switch (reply.method()) + { + case ApiMethod::Customers: { + auto customers = reply.get(); + mUi->cbCustomer->addItem("", -1); + for (const auto& customer : customers) + { + mUi->cbCustomer->addItem(customer.name, customer.id); + } + } + break; + + case ApiMethod::Projects: { + mUi->cbProject->addItem("", -1); + + for (const auto& project : reply.get()) + mUi->cbProject->addItem(project.name, project.id); + } + break; + + default: + break; + } +} + +void ActivityDialog::onCbCustomerTextChanged(const QString& text) +{ + mUi->cbProject->clear(); + + if (not text.isEmpty() and mClient) + { + auto customerId = mUi->cbCustomer->currentData().toInt(); + mClient->sendRequest(KimaiRequestFactory::projects(customerId)); + } + + validateForm(); } void ActivityDialog::validateForm() { -// bool nameOk = not mUi->leName->text().isEmpty(); -// enableSave(nameOk); + bool nameOk = not mUi->leName->text().isEmpty(); + bool projectOk = mUi->cbCustomer->currentText().isEmpty() or + (not mUi->cbCustomer->currentText().isEmpty() and not mUi->cbProject->currentText().isEmpty()); + enableSave(nameOk and projectOk); } diff --git a/src/app/mainwindow.cpp b/src/app/mainwindow.cpp index 1b4acbb..0d44e52 100644 --- a/src/app/mainwindow.cpp +++ b/src/app/mainwindow.cpp @@ -76,6 +76,7 @@ MainWindow::MainWindow() : QMainWindow(), mUi(new Ui::MainWindow) */ auto activityWidget = new ActivityWidget; mActivitySId = mUi->stackedWidget->addWidget(activityWidget); + mCurrentSId = mActivitySId; auto settingsWidget = new SettingsWidget; settingsWidget->setActivityWidgetIndex(mActivitySId); @@ -98,12 +99,7 @@ MainWindow::MainWindow() : QMainWindow(), mUi(new Ui::MainWindow) QTimer::singleShot(100, activityWidget, &ActivityWidget::refresh); // Get client - mClient = helpers::createClient(); - if (mClient) - { - connect(mClient.data(), &KimaiClient::requestError, this, &MainWindow::onClientError); - connect(mClient.data(), &KimaiClient::replyReceived, this, &MainWindow::onClientReply); - } + refreshClient(); } MainWindow::~MainWindow() @@ -122,6 +118,20 @@ void MainWindow::closeEvent(QCloseEvent* event) } } +void MainWindow::refreshClient() +{ + mClient = helpers::createClient(); + if (mClient) + { + connect(mClient.data(), &KimaiClient::requestError, this, &MainWindow::onClientError); + connect(mClient.data(), &KimaiClient::replyReceived, this, &MainWindow::onClientReply); + } + + mActNewCustomer->setEnabled(mClient != nullptr); + mActNewProject->setEnabled(mClient != nullptr); + mActNewActivity->setEnabled(mClient != nullptr); +} + void MainWindow::onClientError(const QString& errorMsg) { qDebug() << errorMsg; @@ -172,13 +182,21 @@ void MainWindow::onActionNewActivityTriggered() void MainWindow::onStackedCurrentChanged(int id) { - if (id == mActivitySId) + // Check if we left settings stack + if (mCurrentSId == mSettingsSId) { - if (auto activityWidget = qobject_cast(mUi->stackedWidget->widget(id))) + if (id == mActivitySId) { - activityWidget->refresh(); + if (auto activityWidget = qobject_cast(mUi->stackedWidget->widget(id))) + { + activityWidget->refresh(); + } } + + refreshClient(); } + + mCurrentSId = id; } void MainWindow::onSystemTrayActivated(QSystemTrayIcon::ActivationReason reason) diff --git a/src/app/mainwindow.h b/src/app/mainwindow.h index a28beed..ed1384f 100644 --- a/src/app/mainwindow.h +++ b/src/app/mainwindow.h @@ -24,6 +24,7 @@ class MainWindow : public QMainWindow void closeEvent(QCloseEvent* event); private slots: + void refreshClient(); void onClientError(const QString& errorMsg); void onClientReply(const client::KimaiReply& reply); void onActionSettingsTriggered(); @@ -40,6 +41,7 @@ private slots: // keep stacked widgets ids int mActivitySId; int mSettingsSId; + int mCurrentSId = -1; // Actions QAction* mActQuit = nullptr;