2020-06-18 18:52:29 +02:00
|
|
|
/*
|
2024-03-29 10:09:48 +01:00
|
|
|
Copyright 2006-2024 The QElectroTech Team
|
2020-06-18 18:52:29 +02:00
|
|
|
This file is part of QElectroTech.
|
|
|
|
|
|
|
|
QElectroTech is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
QElectroTech is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "summaryquerywidget.h"
|
2020-12-09 15:28:43 +01:00
|
|
|
|
2020-12-10 18:44:03 +01:00
|
|
|
#include "../../qetapp.h"
|
|
|
|
#include "../../qetinformation.h"
|
2020-06-18 18:52:29 +02:00
|
|
|
#include "ui_summaryquerywidget.h"
|
|
|
|
|
|
|
|
#include <QListWidgetItem>
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::SummaryQueryWidget
|
|
|
|
@param parent
|
|
|
|
*/
|
2020-06-18 18:52:29 +02:00
|
|
|
SummaryQueryWidget::SummaryQueryWidget(QWidget *parent) :
|
2020-08-16 14:22:54 +02:00
|
|
|
QWidget(parent),
|
2020-06-18 18:52:29 +02:00
|
|
|
ui(new Ui::SummaryQueryWidget)
|
|
|
|
{
|
2020-08-16 14:22:54 +02:00
|
|
|
ui->setupUi(this);
|
2020-06-18 18:52:29 +02:00
|
|
|
|
|
|
|
setUpItems();
|
|
|
|
fillSavedQuery();
|
2020-06-21 16:17:35 +02:00
|
|
|
|
|
|
|
connect(ui->m_config_gb, &ConfigSaveLoaderWidget::saveClicked, this, &SummaryQueryWidget::saveConfig);
|
|
|
|
connect(ui->m_config_gb, &ConfigSaveLoaderWidget::loadClicked, this, &SummaryQueryWidget::loadConfig);
|
2020-06-18 18:52:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::~SummaryQueryWidget
|
|
|
|
*/
|
2020-06-18 18:52:29 +02:00
|
|
|
SummaryQueryWidget::~SummaryQueryWidget()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::queryStr
|
|
|
|
@return The current query string
|
|
|
|
*/
|
2020-06-18 18:52:29 +02:00
|
|
|
QString SummaryQueryWidget::queryStr() const
|
|
|
|
{
|
|
|
|
//User define is own query
|
|
|
|
if (ui->m_edit_sql_query_cb->isChecked()) {
|
|
|
|
return ui->m_user_query_le->text();
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:21:12 -05:00
|
|
|
//Made a string list with the columns (keys) choosen by the user
|
2020-06-18 18:52:29 +02:00
|
|
|
QStringList keys = selectedKeys();
|
|
|
|
|
|
|
|
QString select ="SELECT ";
|
2020-11-02 19:13:56 +01:00
|
|
|
QString order_by = " ORDER BY ";
|
2020-06-18 18:52:29 +02:00
|
|
|
|
|
|
|
QString column;
|
|
|
|
bool first = true;
|
|
|
|
for (auto key: keys) {
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
column += ", ";
|
2020-11-02 19:13:56 +01:00
|
|
|
order_by +=", ";
|
2020-06-18 18:52:29 +02:00
|
|
|
}
|
|
|
|
column += key;
|
2020-11-02 19:13:56 +01:00
|
|
|
order_by += key;
|
2020-06-18 18:52:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString from = " FROM project_summary_view";
|
|
|
|
|
2020-11-02 19:13:56 +01:00
|
|
|
QString q(select + column + from + order_by);
|
2020-06-18 18:52:29 +02:00
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:00:14 +02:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::setQuery
|
|
|
|
@param query
|
2020-08-19 21:24:48 +02:00
|
|
|
set the current query to query.
|
2020-08-16 11:19:36 +02:00
|
|
|
If it's possible, rebuild the state of the widget from the query
|
|
|
|
*/
|
2020-06-21 16:00:14 +02:00
|
|
|
void SummaryQueryWidget::setQuery(const QString &query)
|
|
|
|
{
|
|
|
|
if (query.startsWith("SELECT"))
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
ui->m_user_query_le->setText(query);
|
|
|
|
|
|
|
|
QString select = query;
|
|
|
|
select.remove(0,7); //remove SELECT
|
|
|
|
select.truncate(select.indexOf("FROM")); //Truncate at FROM
|
|
|
|
select.replace(" ",""); //Remove white
|
|
|
|
|
|
|
|
//Get the select -> the item in the right list
|
|
|
|
QStringList split = select.split(",");
|
|
|
|
for (auto str : split)
|
|
|
|
{
|
|
|
|
for (auto item : m_items_list)
|
|
|
|
{
|
|
|
|
if (item->data(Qt::UserRole).toString() == str) {
|
|
|
|
ui->m_available_list->takeItem(ui->m_available_list->row(item));
|
|
|
|
ui->m_choosen_list->addItem(item);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:52:29 +02:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::setUpItems
|
|
|
|
*/
|
2020-06-18 18:52:29 +02:00
|
|
|
void SummaryQueryWidget::setUpItems()
|
|
|
|
{
|
2020-11-08 20:23:16 +01:00
|
|
|
for (auto key : QETInformation::diagramInfoKeys())
|
2020-06-18 18:52:29 +02:00
|
|
|
{
|
|
|
|
if (key == "filename" || key == "display_folio") {
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-08 20:23:16 +01:00
|
|
|
auto item = new QListWidgetItem(QETInformation::translatedInfoKey(key), ui->m_available_list);
|
2020-06-18 18:52:29 +02:00
|
|
|
item->setData(Qt::UserRole, key);
|
|
|
|
m_items_list << item;
|
|
|
|
}
|
|
|
|
auto item = new QListWidgetItem(tr("Position"), ui->m_available_list);
|
|
|
|
item->setData(Qt::UserRole, "pos");
|
|
|
|
m_items_list << item;
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:00:14 +02:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::fillSavedQuery
|
|
|
|
Fill the combo box of the saved query
|
|
|
|
*/
|
2020-06-18 18:52:29 +02:00
|
|
|
void SummaryQueryWidget::fillSavedQuery()
|
|
|
|
{
|
2020-06-21 16:17:35 +02:00
|
|
|
QFile file(QETApp::configDir() + "/summary.json");
|
|
|
|
if (file.open(QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
QJsonDocument jsd(QJsonDocument::fromJson(file.readAll()));
|
|
|
|
QJsonObject jso = jsd.object();
|
2020-06-18 18:52:29 +02:00
|
|
|
|
2020-06-21 16:17:35 +02:00
|
|
|
for (auto it = jso.begin() ; it != jso.end() ; ++it) {
|
|
|
|
ui->m_config_gb->addItem(it.key());
|
|
|
|
}
|
|
|
|
}
|
2020-06-18 18:52:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::updateQueryLine
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
void SummaryQueryWidget::updateQueryLine()
|
|
|
|
{
|
2020-06-18 18:52:29 +02:00
|
|
|
ui->m_user_query_le->setText(queryStr());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::selectedKeys
|
|
|
|
@return
|
|
|
|
*/
|
2020-06-18 18:52:29 +02:00
|
|
|
QStringList SummaryQueryWidget::selectedKeys() const
|
|
|
|
{
|
2022-12-04 08:21:12 -05:00
|
|
|
//Made a string list with the columns (keys) choosen by the user
|
2020-06-18 18:52:29 +02:00
|
|
|
QStringList keys;
|
|
|
|
int row = 0;
|
|
|
|
while (auto *item = ui->m_choosen_list->item(row))
|
|
|
|
{
|
|
|
|
keys.append(item->data(Qt::UserRole).toString());
|
|
|
|
++row;
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
}
|
2020-06-21 16:00:14 +02:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::on_m_available_list_itemDoubleClicked
|
|
|
|
@param item
|
|
|
|
*/
|
2020-06-18 18:52:29 +02:00
|
|
|
void SummaryQueryWidget::on_m_available_list_itemDoubleClicked(QListWidgetItem *item)
|
|
|
|
{
|
|
|
|
Q_UNUSED(item)
|
|
|
|
on_m_add_pb_clicked();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::on_m_choosen_list_itemDoubleClicked
|
|
|
|
@param item
|
|
|
|
*/
|
2020-06-18 18:52:29 +02:00
|
|
|
void SummaryQueryWidget::on_m_choosen_list_itemDoubleClicked(QListWidgetItem *item)
|
|
|
|
{
|
|
|
|
Q_UNUSED(item)
|
|
|
|
on_m_remove_pb_clicked();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::on_m_up_pb_clicked
|
|
|
|
*/
|
2020-06-18 18:52:29 +02:00
|
|
|
void SummaryQueryWidget::on_m_up_pb_clicked()
|
|
|
|
{
|
|
|
|
auto row = ui->m_choosen_list->currentRow();
|
|
|
|
if(row <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *item = ui->m_choosen_list->takeItem(row);
|
|
|
|
ui->m_choosen_list->insertItem(row-1, item);
|
|
|
|
ui->m_choosen_list->setCurrentItem(item);
|
|
|
|
|
|
|
|
updateQueryLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::on_m_add_pb_clicked
|
|
|
|
*/
|
2020-06-18 18:52:29 +02:00
|
|
|
void SummaryQueryWidget::on_m_add_pb_clicked()
|
|
|
|
{
|
|
|
|
if (auto *item = ui->m_available_list->takeItem(ui->m_available_list->currentRow())) {
|
|
|
|
ui->m_choosen_list->addItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateQueryLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::on_m_remove_pb_clicked
|
|
|
|
*/
|
2020-06-18 18:52:29 +02:00
|
|
|
void SummaryQueryWidget::on_m_remove_pb_clicked()
|
|
|
|
{
|
|
|
|
if (auto *item = ui->m_choosen_list->takeItem(ui->m_choosen_list->currentRow())) {
|
|
|
|
ui->m_available_list->addItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateQueryLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::on_m_down_pb_clicked
|
|
|
|
*/
|
2020-06-18 18:52:29 +02:00
|
|
|
void SummaryQueryWidget::on_m_down_pb_clicked()
|
|
|
|
{
|
|
|
|
auto row = ui->m_choosen_list->currentRow();
|
|
|
|
if (row == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *item = ui->m_choosen_list->takeItem(row);
|
|
|
|
ui->m_choosen_list->insertItem(row+1, item);
|
|
|
|
ui->m_choosen_list->setCurrentItem(item);
|
|
|
|
|
|
|
|
updateQueryLine();
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:00:14 +02:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::on_m_edit_sql_query_cb_clicked
|
|
|
|
*/
|
2020-06-18 18:52:29 +02:00
|
|
|
void SummaryQueryWidget::on_m_edit_sql_query_cb_clicked()
|
|
|
|
{
|
|
|
|
ui->m_user_query_le->setEnabled(ui->m_edit_sql_query_cb->isChecked());
|
|
|
|
ui->m_info_widget->setDisabled(ui->m_edit_sql_query_cb->isChecked());
|
|
|
|
|
|
|
|
if (ui->m_edit_sql_query_cb->isChecked() && !m_custom_query.isEmpty())
|
|
|
|
{
|
|
|
|
ui->m_user_query_le->setText(m_custom_query);
|
|
|
|
}
|
|
|
|
else if (!ui->m_edit_sql_query_cb->isChecked())
|
|
|
|
{
|
|
|
|
m_custom_query = ui->m_user_query_le->text();
|
|
|
|
updateQueryLine();
|
|
|
|
}
|
|
|
|
}
|
2020-06-21 16:00:14 +02:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::reset
|
|
|
|
Clear this widget aka set to initial state
|
|
|
|
*/
|
2020-06-21 16:00:14 +02:00
|
|
|
void SummaryQueryWidget::reset()
|
|
|
|
{
|
|
|
|
//Ugly hack to force to remove all selected infos
|
|
|
|
while (auto item = ui->m_choosen_list->takeItem(0)) {
|
|
|
|
ui->m_available_list->addItem(item);
|
|
|
|
}
|
|
|
|
ui->m_user_query_le->clear();
|
|
|
|
}
|
2020-06-21 16:17:35 +02:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::saveConfig
|
|
|
|
*/
|
2020-06-21 16:17:35 +02:00
|
|
|
void SummaryQueryWidget::saveConfig()
|
|
|
|
{
|
|
|
|
QFile file_(QETApp::configDir() + "/summary.json");
|
|
|
|
|
|
|
|
if (file_.open(QFile::ReadWrite))
|
|
|
|
{
|
|
|
|
QJsonDocument doc_(QJsonDocument::fromJson(file_.readAll()));
|
|
|
|
QJsonObject root_object;
|
|
|
|
|
|
|
|
if (!doc_.isEmpty())
|
|
|
|
{
|
|
|
|
root_object = doc_.object();
|
|
|
|
if (root_object.contains(ui->m_config_gb->text())) {
|
|
|
|
root_object.remove(ui->m_config_gb->text());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject object_;
|
|
|
|
object_.insert("query", queryStr());
|
|
|
|
root_object[ui->m_config_gb->text()] = object_;
|
|
|
|
|
|
|
|
doc_.setObject(root_object);
|
|
|
|
file_.resize(0);
|
|
|
|
file_.write(doc_.toJson());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief SummaryQueryWidget::loadConfig
|
|
|
|
*/
|
2020-06-21 16:17:35 +02:00
|
|
|
void SummaryQueryWidget::loadConfig()
|
|
|
|
{
|
|
|
|
auto name = ui->m_config_gb->selectedText();
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file_(QETApp::configDir() + "/summary.json");
|
|
|
|
if (!file_.open(QFile::ReadOnly)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonDocument doc_(QJsonDocument::fromJson(file_.readAll()));
|
|
|
|
QJsonObject object_ = doc_.object();
|
|
|
|
|
|
|
|
auto value = object_.value(name);
|
|
|
|
if (!value.isObject()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto value_object = value.toObject();
|
|
|
|
if (value_object.value("query").isString()) {
|
|
|
|
setQuery(value_object.value("query").toString());
|
|
|
|
}
|
|
|
|
}
|