Skip to content

Commit

Permalink
Refs #94. Working new and delete dashboard features.
Browse files Browse the repository at this point in the history
  • Loading branch information
SBriere committed Mar 14, 2024
1 parent 9dacdf6 commit daa6ab0
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool DashboardsComManager::processNetworkReply(QNetworkReply *reply)
if (reply->operation()==QNetworkAccessManager::PostOperation){
if (!handled){
handled=handleDataReply(reply_path, reply_data, reply_query);
if (handled) emit postResultsOK(reply_path);
if (handled) emit postResultsOK(reply_path, reply_data);
}
}

Expand Down
2 changes: 2 additions & 0 deletions client/src/services/DashboardsService/DashboardsComManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ private slots:
void dataReceived(QList<QJsonObject> items, QUrlQuery reply_query);
void dashboardsReceived(QList<QJsonObject> dashboards, QUrlQuery reply_query);

void postResultsOK(QString reply_path, QString reply_data);

};

#endif // DASHCOMMANAGER_H
164 changes: 144 additions & 20 deletions client/src/services/DashboardsService/DashboardsConfigWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "DashboardsConfigWidget.h"
#include "ui_DashboardsConfigWidget.h"

#include "GlobalMessageBox.h"

DashboardsConfigWidget::DashboardsConfigWidget(ComManager *comManager, const int &id_site, const int &id_project, QWidget *parent)
: QWidget(parent)
, ui(new Ui::DashboardsConfigWidget)
Expand All @@ -22,6 +24,7 @@ DashboardsConfigWidget::DashboardsConfigWidget(ComManager *comManager, const int
ui->frameButtons->hide();
ui->frameVersionsInfos->setVisible(is_super);
ui->frameVersionsButtons->hide();
ui->btnDeleteVersion->hide();

// Query dashboards
QUrlQuery args;
Expand Down Expand Up @@ -62,6 +65,23 @@ void DashboardsConfigWidget::queryDashboard(const int &id_dashboard)
m_dashComManager->doGet(DASHBOARDS_USER_PATH, args);
}

void DashboardsConfigWidget::clearDetails()
{
ui->txtName->clear();
ui->txtDescription->clear();
ui->cmbVersion->clear();
ui->txtDefinition->clear();

}

void DashboardsConfigWidget::validateDetails()
{
bool valid = false;
valid = !ui->txtName->text().isEmpty() && !ui->txtDefinition->toPlainText().isEmpty();

ui->btnSave->setEnabled(valid);
}

void DashboardsConfigWidget::processDashboardsReply(QList<QJsonObject> dashboards, QUrlQuery reply_query)
{
if (reply_query.hasQueryItem(WEB_QUERY_ID_DASHBOARD)){
Expand Down Expand Up @@ -182,7 +202,6 @@ void DashboardsConfigWidget::processDashboardsReply(QList<QJsonObject> dashboard
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}
ui->lstDashboards->addItem(item);

}
}
}
Expand Down Expand Up @@ -216,13 +235,31 @@ void DashboardsConfigWidget::handleNetworkError(QNetworkReply::NetworkError erro

void DashboardsConfigWidget::dashComDeleteOK(QString path, int id)
{

if (path == DASHBOARDS_USER_PATH && m_listDashboards_items.contains(id)){
delete m_listDashboards_items[id];
m_listDashboards_items.remove(id);
m_listDashboards_projects.remove(id);
m_listDashboards_sites.remove(id);
ui->lstDashboards->setCurrentRow(-1);
ui->frameEditor->hide();
}
}

void DashboardsConfigWidget::dashComPostOK(QString path)
void DashboardsConfigWidget::dashComPostOK(QString path, QString data)
{
ui->wdgMessages->addMessage(Message(Message::MESSAGE_OK, tr("Données sauvegardées")));
ui->btnEdit->setChecked(false);
ui->frameDashboards->show();

// Ensure item is selected in list
QJsonParseError json_error;
QJsonDocument data_list = QJsonDocument::fromJson(data.toUtf8(), &json_error);
if (json_error.error == QJsonParseError::NoError){
int id_dashboard = data_list.object()["id_dashboard"].toInt();
if (m_listDashboards_items.contains(id_dashboard)){
ui->lstDashboards->setCurrentItem(m_listDashboards_items[id_dashboard]);
}
}
}

void DashboardsConfigWidget::nextMessageWasShown(Message current_message)
Expand Down Expand Up @@ -316,6 +353,7 @@ void DashboardsConfigWidget::on_btnEdit_toggled(bool checked)
ui->frameSpecific->setEnabled(checked);
ui->frameButtons->setVisible(checked);
ui->btnEdit->setVisible(!checked);
validateDetails();
}

void DashboardsConfigWidget::on_btnCancel_clicked()
Expand All @@ -325,6 +363,9 @@ void DashboardsConfigWidget::on_btnCancel_clicked()
if (ui->lstDashboards->currentItem()){
int id_dashboard = m_listDashboards_items.key(ui->lstDashboards->currentItem());
queryDashboard(id_dashboard);
}else{
ui->frameDashboards->show();
ui->frameEditor->hide();
}
}

Expand All @@ -337,19 +378,19 @@ void DashboardsConfigWidget::on_btnSave_clicked()

QListWidgetItem* item = ui->lstDashboards->currentItem();

if (!item){
qCritical() << "DashboardsConfigWidget::on_btnSave_clicked - No selected item!";
return;
int id_dashboard = 0;
if (item){
id_dashboard = item->data(Qt::UserRole).toInt(); // Editing item
}
int id_dashboard = item->data(Qt::UserRole).toInt();

data_obj["id_dashboard"] = id_dashboard;

if (m_comManager->isCurrentUserSuperAdmin()){
data_obj["dashboard_name"] = ui->txtName->text();
data_obj["dashboard_description"] = ui->txtDescription->toPlainText();

// Updated versions
if (ui->cmbVersion->currentData().toString() != ui->txtDefinition->toPlainText()){
if (ui->cmbVersion->currentData().toString() != ui->txtDefinition->toPlainText() || id_dashboard == 0){
data_obj["dashboard_version"] = ui->cmbVersion->count();
data_obj["dashboard_definition"] = ui->txtDefinition->toPlainText();
}
Expand All @@ -358,24 +399,42 @@ void DashboardsConfigWidget::on_btnSave_clicked()
if (ui->frameSpecific->isVisible()){
// Generate projects / sites list with enabled and fixed version
if (m_idProject>0){
for(int item_id: m_listDashboards_projects[id_dashboard]){
QJsonObject item;
item["id_project"] = item_id;
if (item_id == m_idProject){
item["dashboard_project_enabled"] = ui->chkEnabled->isChecked();
item["dashboard_project_version"] = ui->cmbFixedVersion->currentIndex();
if (id_dashboard > 0){
for(int item_id: m_listDashboards_projects[id_dashboard]){
QJsonObject item;
item["id_project"] = item_id;
if (item_id == m_idProject){
item["dashboard_project_enabled"] = ui->chkEnabled->isChecked();
item["dashboard_project_version"] = ui->cmbFixedVersion->currentIndex();
}
attached_items.append(item);
}
}else{
// New dashboard - append current
QJsonObject item;
item["id_project"] = m_idProject;
item["dashboard_project_enabled"] = ui->chkEnabled->isChecked();
item["dashboard_project_version"] = ui->cmbFixedVersion->currentIndex();
attached_items.append(item);
}
data_obj["dashboard_projects"] = attached_items;
}else{
for(int item_id: m_listDashboards_sites[id_dashboard]){
QJsonObject item;
item["id_site"] = item_id;
if (item_id == m_idSite){
item["dashboard_site_enabled"] = ui->chkEnabled->isChecked();
item["dashboard_site_version"] = ui->cmbFixedVersion->currentIndex();
if (id_dashboard > 0){
for(int item_id: m_listDashboards_sites[id_dashboard]){
QJsonObject item;
item["id_site"] = item_id;
if (item_id == m_idSite){
item["dashboard_site_enabled"] = ui->chkEnabled->isChecked();
item["dashboard_site_version"] = ui->cmbFixedVersion->currentIndex();
}
attached_items.append(item);
}
}else{
// New dashboard - append current
QJsonObject item;
item["id_site"] = m_idSite;
item["dashboard_project_enabled"] = ui->chkEnabled->isChecked();
item["dashboard_project_version"] = ui->cmbFixedVersion->currentIndex();
attached_items.append(item);
}
data_obj["dashboard_sites"] = attached_items;
Expand All @@ -387,3 +446,68 @@ void DashboardsConfigWidget::on_btnSave_clicked()
m_dashComManager->doPost(DASHBOARDS_USER_PATH, document.toJson());
}

void DashboardsConfigWidget::on_btnNewVersion_clicked()
{
ui->cmbVersion->addItem(QString::number(ui->cmbVersion->count()+1), "");
ui->cmbVersion->setCurrentIndex(ui->cmbVersion->count()-1);
}

void DashboardsConfigWidget::on_btnNewDashboard_clicked()
{
ui->lstDashboards->setCurrentRow(-1);
ui->btnEdit->setChecked(true);
clearDetails();
ui->cmbVersion->addItem("1", "{}");
ui->frameDashboards->hide();
ui->frameSpecific->show();
on_txtName_textChanged("");
ui->chkEnabled->setChecked(true);
ui->frameEditor->show();

}

void DashboardsConfigWidget::on_txtName_textChanged(const QString &arg1)
{
if (ui->btnEdit->isChecked()){
if (ui->txtName->text().isEmpty()){
ui->txtName->setStyleSheet("background-color: #ffaaaa; color:black;");
}else{
ui->txtName->setStyleSheet("");
}
validateDetails();
}
}

void DashboardsConfigWidget::on_txtDefinition_textChanged()
{
if (ui->btnEdit->isChecked()){
if (ui->txtDefinition->toPlainText().isEmpty()){
ui->txtDefinition->setStyleSheet("background-color: #ffaaaa; color:black;");
}else{
ui->txtDefinition->setStyleSheet("");
}
validateDetails();
}
}

void DashboardsConfigWidget::on_btnDeleteDashboard_clicked()
{
if (!ui->lstDashboards->currentItem())
return;

GlobalMessageBox msgbox;

GlobalMessageBox msg;
QMessageBox::StandardButton conf = msg.showYesNo(tr("Suppression"), tr("Êtes-vous sûr de vouloir supprimer le tableau de bord sélectionné?"));

if (conf == QMessageBox::Yes){
int id_dashboard = ui->lstDashboards->currentItem()->data(Qt::UserRole).toInt();
m_dashComManager->doDelete(DASHBOARDS_USER_PATH, id_dashboard);
}
}

void DashboardsConfigWidget::on_lstDashboards_currentRowChanged(int currentRow)
{
ui->btnDeleteDashboard->setEnabled(currentRow>=0);
}

10 changes: 9 additions & 1 deletion client/src/services/DashboardsService/DashboardsConfigWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ class DashboardsConfigWidget : public QWidget

void connectSignals();
void queryDashboard(const int& id_dashboard);
void clearDetails();
void validateDetails();

private slots:
void processDashboardsReply(QList<QJsonObject> dashboards, QUrlQuery reply_query);
void handleNetworkError(QNetworkReply::NetworkError error, QString error_msg, QNetworkAccessManager::Operation op, int status_code);
void dashComDeleteOK(QString path, int id);
void dashComPostOK(QString path);
void dashComPostOK(QString path, QString data);

void nextMessageWasShown(Message current_message);

Expand All @@ -49,6 +51,12 @@ private slots:
void on_btnEdit_toggled(bool checked);
void on_btnCancel_clicked();
void on_btnSave_clicked();
void on_btnNewVersion_clicked();
void on_btnNewDashboard_clicked();
void on_txtName_textChanged(const QString &arg1);
void on_txtDefinition_textChanged();
void on_btnDeleteDashboard_clicked();
void on_lstDashboards_currentRowChanged(int currentRow);
};

#endif // DASHBOARDSCONFIGWIDGET_H
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
</item>
<item>
<widget class="QToolButton" name="btnDeleteDashboard">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimumSize">
<size>
<width>32</width>
Expand Down

0 comments on commit daa6ab0

Please sign in to comment.