Mon, 27 Jun 2022 21:12:19 +0200
Added monitor nodes overview
310 | 1 | /** |
2 | * MonCO2meters.cpp is part of bmsapp. | |
3 | * | |
4 | * bmsapp is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 3 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * bmsapp is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | #include "MonCO2meters.h" | |
18 | #include "EditSupplier.h" | |
19 | #include "MainWindow.h" | |
20 | #include "config.h" | |
21 | ||
22 | ||
23 | ||
24 | /* | |
25 | * Build the table and buttons on the mainscreen. | |
26 | * Don't use a ui file, do it dynamicly. | |
27 | */ | |
28 | MonCO2meters::MonCO2meters(QWidget *parent) : QDialog(parent) | |
29 | { | |
30 | qDebug() << "MonCO2meters start"; | |
31 | ||
32 | gridLayout = new QGridLayout(this); | |
33 | gridLayout->setObjectName(QString::fromUtf8("gridLayout")); | |
34 | tableSuppliers = new QTableWidget(this); | |
35 | tableSuppliers->setObjectName(QString::fromUtf8("tableSuppliers")); | |
36 | tableSuppliers->setEnabled(true); | |
37 | QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); | |
38 | sizePolicy.setHorizontalStretch(0); | |
39 | sizePolicy.setVerticalStretch(0); | |
40 | sizePolicy.setHeightForWidth(tableSuppliers->sizePolicy().hasHeightForWidth()); | |
41 | tableSuppliers->setSizePolicy(sizePolicy); | |
42 | tableSuppliers->setMinimumSize(QSize(1054, 0)); | |
43 | gridLayout->addWidget(tableSuppliers, 0, 0, 1, 1); | |
44 | ||
45 | groupBox = new QGroupBox(this); | |
46 | groupBox->setObjectName(QString::fromUtf8("groupBox")); | |
47 | groupBox->setEnabled(true); | |
48 | groupBox->setFlat(false); | |
49 | horizontalLayout = new QHBoxLayout(groupBox); | |
50 | horizontalLayout->setSpacing(6); | |
51 | horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout")); | |
52 | horizontalLayout->setContentsMargins(0, 0, 0, 0); | |
53 | quitButton = new QPushButton(groupBox); | |
54 | quitButton->setObjectName(QString::fromUtf8("quitButton")); | |
55 | quitButton->setMinimumSize(QSize(80, 24)); | |
56 | quitButton->setText(tr("Quit")); | |
57 | QIcon icon; | |
58 | icon.addFile(QString::fromUtf8(":icons/silk/door_out.png"), QSize(), QIcon::Normal, QIcon::Off); | |
59 | quitButton->setIcon(icon); | |
60 | horizontalLayout->addWidget(quitButton, 0, Qt::AlignLeft); | |
61 | ||
62 | insertButton = new QPushButton(groupBox); | |
63 | insertButton->setObjectName(QString::fromUtf8("insertButton")); | |
64 | insertButton->setMinimumSize(QSize(80, 24)); | |
65 | insertButton->setText(tr("New")); | |
66 | QIcon icon1; | |
67 | icon1.addFile(QString::fromUtf8(":icons/silk/table_row_insert.png"), QSize(), QIcon::Normal, QIcon::Off); | |
68 | insertButton->setIcon(icon1); | |
69 | horizontalLayout->addWidget(insertButton, 0, Qt::AlignRight); | |
70 | gridLayout->addWidget(groupBox, 1, 0, 1, 1); | |
71 | ||
72 | connect(quitButton, SIGNAL(clicked()), parent, SLOT(fromMonCO2meters())); | |
73 | connect(insertButton, SIGNAL(clicked()), this, SLOT(on_insertButton_clicked())); | |
74 | connect(this, SIGNAL(setStatus(QString)), parent, SLOT(statusMsg(QString))); | |
75 | emit refreshTable(); | |
76 | } | |
77 | ||
78 | ||
79 | void MonCO2meters::refreshTable() | |
80 | { | |
81 | qDebug() << "MonCO2meters reload"; | |
82 | ||
83 | // query.exec("SELECT record,node,alias,online FROM mon_co2meters ORDER BY node,alias"); | |
84 | QSqlQuery query("SELECT * FROM inventory_suppliers ORDER BY name"); | |
85 | const QStringList labels({tr("Name"), tr("Address"), tr("City"), tr("Country"), tr("Phone"), tr("Edit")}); | |
86 | ||
87 | this->tableSuppliers->setColumnCount(6); | |
88 | this->tableSuppliers->setColumnWidth(0, 250); /* Name */ | |
89 | this->tableSuppliers->setColumnWidth(1, 250); /* Address */ | |
90 | this->tableSuppliers->setColumnWidth(2, 200); /* City */ | |
91 | this->tableSuppliers->setColumnWidth(3, 120); /* Country */ | |
92 | this->tableSuppliers->setColumnWidth(4, 120); /* Phone */ | |
93 | this->tableSuppliers->setColumnWidth(5, 90); /* Edit button */ | |
94 | this->tableSuppliers->setRowCount(query.size()); | |
95 | this->tableSuppliers->setHorizontalHeaderLabels(labels); | |
96 | this->tableSuppliers->verticalHeader()->hide(); | |
97 | /* Set the widget size to 1054 x 575 in the ui. */ | |
98 | ||
99 | query.first(); | |
100 | for (int ridx = 0 ; ridx < query.size() ; ridx++ ) { | |
101 | this->tableSuppliers->setItem(ridx, 0, new QTableWidgetItem(query.value(1).toString())); | |
102 | this->tableSuppliers->setItem(ridx, 1, new QTableWidgetItem(query.value(2).toString())); | |
103 | this->tableSuppliers->setItem(ridx, 2, new QTableWidgetItem(query.value(3).toString())); | |
104 | this->tableSuppliers->setItem(ridx, 3, new QTableWidgetItem(query.value(5).toString())); | |
105 | this->tableSuppliers->setItem(ridx, 4, new QTableWidgetItem(query.value(8).toString())); | |
106 | ||
107 | /* Add the Edit button */ | |
108 | QWidget* pWidget = new QWidget(); | |
109 | QPushButton* btn_edit = new QPushButton(); | |
110 | btn_edit->setObjectName(QString("%1").arg(query.value(0).toString())); /* Send record with the button */ | |
111 | btn_edit->setText(tr("Edit")); | |
112 | connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_editButton_clicked())); | |
113 | QHBoxLayout* pLayout = new QHBoxLayout(pWidget); | |
114 | pLayout->addWidget(btn_edit); | |
115 | pLayout->setContentsMargins(5, 0, 5, 0); | |
116 | pWidget->setLayout(pLayout); | |
117 | this->tableSuppliers->setCellWidget(ridx, 5, pWidget); | |
118 | query.next(); | |
119 | } | |
120 | emit setStatus(QString(tr("Total items: %1")).arg(query.size())); | |
121 | } | |
122 | ||
123 | ||
124 | MonCO2meters::~MonCO2meters() {} | |
125 | ||
126 | ||
127 | void MonCO2meters::edit(int recno) | |
128 | { | |
129 | EditSupplier dialog(recno, this); | |
130 | /* Signal from editor if a refresh is needed */ | |
131 | connect(&dialog, SIGNAL(entry_changed()), this, SLOT(refreshTable())); | |
132 | dialog.setModal(true); | |
133 | dialog.exec(); | |
134 | } | |
135 | ||
136 | ||
137 | void MonCO2meters::on_editButton_clicked() | |
138 | { | |
139 | QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender()); | |
140 | int recno = pb->objectName().toInt(); | |
141 | edit(recno); | |
142 | } | |
143 | ||
144 | ||
145 | void MonCO2meters::on_insertButton_clicked() | |
146 | { | |
147 | edit(-1); | |
148 | } | |
149 |