--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ProfileWaters.cpp Sun Mar 06 15:31:40 2022 +0100 @@ -0,0 +1,163 @@ +/** + * ProfileWaters.cpp is part of bmsapp. + * + * bmsapp 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 3 of the License, or + * (at your option) any later version. + * + * bmsapp 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 this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "ProfileWaters.h" +#include "EditProfileWater.h" +#include "../ui/ui_ProfileWaters.h" +#include "config.h" +#include "bmsapp.h" + + +ProfileWaters::ProfileWaters(QWidget *parent) : QDialog(parent), ui(new Ui::ProfileWaters) +{ + qDebug() << "ProfileWaters start"; + + ui->setupUi(this); + emit refreshTable(); + + setWindowTitle( QString("BMSapp - %1 - Profile Waters").arg(VERSIONSTRING) ); +} + + +void ProfileWaters::refreshTable() +{ + QString w; + QWidget* pWidget; + QLabel *label; + QHBoxLayout* pLayout; + + qDebug() << "ProfileWaters reload"; + + QSqlQuery query("SELECT * FROM profile_water ORDER BY name"); + const QStringList labels({tr("Name"), tr("Notes"), tr("Ca"), tr("Mg"), tr("Na"), tr("CaCO3"), tr("Cl"), tr("SO4"), tr("pH"), tr("Edit")}); + + ui->tableWaters->setColumnCount(10); + ui->tableWaters->setColumnWidth(0, 150); /* Name */ + ui->tableWaters->setColumnWidth(1, 375); /* Notes */ + ui->tableWaters->setColumnWidth(2, 75); /* Ca */ + ui->tableWaters->setColumnWidth(3, 75); /* Mg */ + ui->tableWaters->setColumnWidth(4, 75); /* Na */ + ui->tableWaters->setColumnWidth(5, 75); /* CaCO3 */ + ui->tableWaters->setColumnWidth(6, 75); /* Cl */ + ui->tableWaters->setColumnWidth(7, 75); /* SO4 */ + ui->tableWaters->setColumnWidth(8, 75); /* pH */ + ui->tableWaters->setColumnWidth(9, 80); /* Edit button */ + ui->tableWaters->setRowCount(query.size()); + ui->tableWaters->setHorizontalHeaderLabels(labels); + ui->tableWaters->verticalHeader()->hide(); + ui->tableWaters->setFixedSize(1130 + 24, 640); /* Even if this is too large, it works */ + + QTableWidgetItem *rightitem = new QTableWidgetItem(); + rightitem->setTextAlignment(Qt::AlignRight); + + query.first(); + for (int ridx = 0 ; ridx < query.size() ; ridx++ ) { + ui->tableWaters->setItem(ridx, 0, new QTableWidgetItem(query.value(1).toString())); /* Name */ + ui->tableWaters->setItem(ridx, 1, new QTableWidgetItem(query.value(9).toString())); /* Notes */ + + w = QString("%1").arg(query.value(2).toDouble(), 2, 'f', 1, '0' ); /* Ca */ + QTableWidgetItem *item = new QTableWidgetItem(w); + item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); + ui->tableWaters->setItem(ridx, 2, item); + + w = QString("%1").arg(query.value(7).toDouble(), 2, 'f', 1, '0' ); /* Mg */ + item = new QTableWidgetItem(w); + item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); + ui->tableWaters->setItem(ridx, 3, item); + + w = QString("%1").arg(query.value(6).toDouble(), 2, 'f', 1, '0' ); /* Na */ + item = new QTableWidgetItem(w); + item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); + ui->tableWaters->setItem(ridx, 4, item); + + w = QString("%1").arg(query.value(10).toDouble(), 2, 'f', 1, '0' ); /* CaCO3 */ + item = new QTableWidgetItem(w); + item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); + ui->tableWaters->setItem(ridx, 5, item); + + w = QString("%1").arg(query.value(5).toDouble(), 2, 'f', 1, '0' ); /* Cl */ + item = new QTableWidgetItem(w); + item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); + ui->tableWaters->setItem(ridx, 6, item); + + w = QString("%1").arg(query.value(4).toDouble(), 2, 'f', 1, '0' ); /* SO4 */ + item = new QTableWidgetItem(w); + item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); + ui->tableWaters->setItem(ridx, 7, item); + + w = QString("%1").arg(query.value(8).toDouble(), 2, 'f', 1, '0' ); /* pH */ + item = new QTableWidgetItem(w); + item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); + ui->tableWaters->setItem(ridx, 8, item); + + /* Add the Edit button */ + pWidget = new QWidget(); + QPushButton* btn_edit = new QPushButton(); + btn_edit->setObjectName(QString("%1").arg(query.value(0).toString())); /* Send record with the button */ + btn_edit->setText(tr("Edit")); + connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_editButton_clicked())); + pLayout = new QHBoxLayout(pWidget); + pLayout->addWidget(btn_edit); + pLayout->setContentsMargins(5, 0, 5, 0); + pWidget->setLayout(pLayout); + ui->tableWaters->setCellWidget(ridx, 9, pWidget); + query.next(); + } + + setWindowTitle( QString("BMSapp - %1 - Profile Waters").arg(VERSIONSTRING) ); +} + + +ProfileWaters::~ProfileWaters() +{ + qDebug() << "ProfileWaters done"; + delete ui; +} + + +void ProfileWaters::edit(int recno) +{ + qDebug() << "ProfileWaters edit:" << recno; + + EditProfileWater dialog(recno, this); + /* Signal from editor if a refresh is needed */ + connect(&dialog, SIGNAL(entry_changed()), this, SLOT(refreshTable())); + dialog.setModal(true); + dialog.exec(); +} + + +void ProfileWaters::on_editButton_clicked() +{ + QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender()); + int recno = pb->objectName().toInt(); + qDebug() << Q_FUNC_INFO << recno; + edit(recno); +} + + +void ProfileWaters::on_insertButton_clicked() +{ + qDebug() << Q_FUNC_INFO; + edit(-1); +} + + +void ProfileWaters::on_quitButton_clicked() +{ + emit firstWindow(); +} +