Skip to content

Commit

Permalink
Enable 'new' actions only if client exists
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrePTJ committed May 5, 2020
1 parent d5e6efd commit 24a5582
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 29 deletions.
95 changes: 75 additions & 20 deletions src/app/activitydialog.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "activitydialog.h"
#include "ui_activitydialog.h"

#include "datareader.h"
#include "helpers.h"

#include "kemai/kimairequestfactory.h"

#include <QPushButton>
#include <QTimer>

using namespace kemai::app;
using namespace kemai::client;
Expand All @@ -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()
Expand All @@ -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<Customers>();
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<Projects>())
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);
}
36 changes: 27 additions & 9 deletions src/app/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -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;
Expand Down Expand Up @@ -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<ActivityWidget*>(mUi->stackedWidget->widget(id)))
if (id == mActivitySId)
{
activityWidget->refresh();
if (auto activityWidget = qobject_cast<ActivityWidget*>(mUi->stackedWidget->widget(id)))
{
activityWidget->refresh();
}
}

refreshClient();
}

mCurrentSId = id;
}

void MainWindow::onSystemTrayActivated(QSystemTrayIcon::ActivationReason reason)
Expand Down
2 changes: 2 additions & 0 deletions src/app/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -40,6 +41,7 @@ private slots:
// keep stacked widgets ids
int mActivitySId;
int mSettingsSId;
int mCurrentSId = -1;

// Actions
QAction* mActQuit = nullptr;
Expand Down

0 comments on commit 24a5582

Please sign in to comment.