src/InventoryWaters.cpp

changeset 29
76846c99f827
child 46
404b79f6a681
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/InventoryWaters.cpp	Sat Feb 26 16:34:20 2022 +0100
@@ -0,0 +1,144 @@
+/**
+ * InventoryWaters.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 "InventoryWaters.h"
+#include "EditWater.h"
+#include "../ui/ui_InventoryWaters.h"
+#include "config.h"
+#include "bmsapp.h"
+
+
+InventoryWaters::InventoryWaters(QWidget *parent) : QDialog(parent), ui(new Ui::InventoryWaters)
+{
+    qDebug() << "InventoryWaters start";
+
+    ui->setupUi(this);
+    emit refreshTable();
+
+    setWindowTitle( QString("BMSapp - %1 - Inventory Waters").arg(VERSIONSTRING) );
+}
+
+
+void InventoryWaters::refreshTable()
+{
+    QString w;
+    QWidget* pWidget;
+    QLabel *label;
+    QHBoxLayout* pLayout;
+
+    qDebug() << "InventoryWaters reload";
+
+    QSqlQuery query("SELECT * FROM inventory_waters ORDER BY name");
+    const QStringList labels({tr("Name"), tr("Notes"), tr("Unlimited"), tr("Stock"), tr("Edit")});
+
+    ui->tableWaters->setColumnCount(5);
+    ui->tableWaters->setColumnWidth(0, 200);	/* Name		*/
+    ui->tableWaters->setColumnWidth(1, 500);	/* Notes	*/
+    ui->tableWaters->setColumnWidth(2,  75);	/* Unlimited	*/
+    ui->tableWaters->setColumnWidth(3,  75);	/* Stock	*/
+    ui->tableWaters->setColumnWidth(4,  80);	/* Edit button	*/
+    ui->tableWaters->setRowCount(query.size());
+    ui->tableWaters->setHorizontalHeaderLabels(labels);
+    ui->tableWaters->verticalHeader()->hide();
+    ui->tableWaters->setFixedSize(930 + 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(10).toString()));	/* Notes */
+	if (query.value(2).toInt()) {
+            pWidget = new QWidget();
+            label = new QLabel;
+            label->setPixmap(QPixmap(":icons/silk/tick.png"));
+            pLayout = new QHBoxLayout(pWidget);
+            pLayout->addWidget(label);
+            pLayout->setAlignment(Qt::AlignCenter);
+            pLayout->setContentsMargins(0, 0, 0, 0);
+            pWidget->setLayout(pLayout);
+            ui->tableWaters->setCellWidget(ridx, 2, pWidget);
+        } else {
+	    ui->tableWaters->removeCellWidget(ridx, 2);
+	}
+
+	w = QString("");
+        if (query.value(12).toDouble() > 0) {
+          w = QString("%1 L").arg(query.value(12).toDouble(), 2, 'f', 1, '0' );
+        }
+        QTableWidgetItem *item = new QTableWidgetItem(w);
+        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
+        ui->tableWaters->setItem(ridx, 3, 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, 4, pWidget);
+	query.next();
+    }
+
+    setWindowTitle( QString("BMSapp - %1 - Inventory Waters").arg(VERSIONSTRING) );
+}
+
+
+InventoryWaters::~InventoryWaters()
+{
+    qDebug() << "InventoryWaters done";
+    delete ui;
+}
+
+
+void InventoryWaters::edit(int recno)
+{
+    qDebug() << "InventoryWaters edit:" << recno;
+
+    EditWater 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 InventoryWaters::on_editButton_clicked()
+{
+    QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
+    int recno = pb->objectName().toInt();
+    qDebug() << Q_FUNC_INFO << recno;
+    edit(recno);
+}
+
+
+void InventoryWaters::on_insertButton_clicked()
+{
+    qDebug() << Q_FUNC_INFO;
+    edit(-1);
+}
+
+
+void InventoryWaters::on_quitButton_clicked()
+{
+    emit firstWindow();
+}
+

mercurial