src/MonNodes.cpp

changeset 310
bdaac24b86ed
child 312
251b9aaae916
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/MonNodes.cpp	Mon Jun 27 21:12:19 2022 +0200
@@ -0,0 +1,163 @@
+/**
+ * MonNodes.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 "MonNodes.h"
+#include "EditSupplier.h"
+#include "MainWindow.h"
+#include "config.h"
+
+
+
+/*
+ * Build the table and buttons on the mainscreen.
+ * Don't use a ui file, do it dynamicly.
+ */
+MonNodes::MonNodes(QWidget *parent) : QDialog(parent)
+{
+    qDebug() << "MonNodes start";
+
+    gridLayout = new QGridLayout(this);
+    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
+    tableNodes = new QTableWidget(this);
+    tableNodes->setObjectName(QString::fromUtf8("tableNodes"));
+    tableNodes->setEnabled(true);
+    QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
+    sizePolicy.setHorizontalStretch(0);
+    sizePolicy.setVerticalStretch(0);
+    sizePolicy.setHeightForWidth(tableNodes->sizePolicy().hasHeightForWidth());
+    tableNodes->setSizePolicy(sizePolicy);
+    tableNodes->setMinimumSize(QSize(974, 0));
+    gridLayout->addWidget(tableNodes, 0, 0, 1, 1);
+
+    groupBox = new QGroupBox(this);
+    groupBox->setObjectName(QString::fromUtf8("groupBox"));
+    groupBox->setEnabled(true);
+    groupBox->setFlat(false);
+    horizontalLayout = new QHBoxLayout(groupBox);
+    horizontalLayout->setSpacing(6);
+    horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
+    horizontalLayout->setContentsMargins(0, 0, 0, 0);
+    quitButton = new QPushButton(groupBox);
+    quitButton->setObjectName(QString::fromUtf8("quitButton"));
+    quitButton->setMinimumSize(QSize(80, 24));
+    quitButton->setText(tr("Quit"));
+    QIcon icon;
+    icon.addFile(QString::fromUtf8(":icons/silk/door_out.png"), QSize(), QIcon::Normal, QIcon::Off);
+    quitButton->setIcon(icon);
+    horizontalLayout->addWidget(quitButton, 0, Qt::AlignCenter);
+    gridLayout->addWidget(groupBox, 1, 0, 1, 1);
+
+    connect(quitButton, SIGNAL(clicked()), parent, SLOT(fromMonNodes()));
+    connect(this, SIGNAL(setStatus(QString)), parent, SLOT(statusMsg(QString)));
+    connect(parent, SIGNAL(updateNodes(QString)), this, SLOT(refreshNodes(QString)));
+    emit refreshTable();
+}
+
+
+void MonNodes::refreshTable()
+{
+    QTableWidgetItem *item;
+
+    qDebug() << "MonNodes reload";
+
+    QSqlQuery query("SELECT record,node,online,group_id,lastseen,net_address,up_interval FROM mon_nodes ORDER BY node");
+    const QStringList labels({tr("Node"), tr("Status"), tr("Group"), tr("Last seen"), tr("Address"), tr("Interval"), tr("Edit")});
+
+    this->tableNodes->setColumnCount(7);
+    this->tableNodes->setColumnWidth(0, 200);	/* Node		*/
+    this->tableNodes->setColumnWidth(1, 100);	/* Status	*/
+    this->tableNodes->setColumnWidth(2, 150);	/* Group	*/
+    this->tableNodes->setColumnWidth(3, 200);	/* Last seen	*/
+    this->tableNodes->setColumnWidth(4, 120);	/* Address	*/
+    this->tableNodes->setColumnWidth(5,  90);	/* Interval	*/
+    this->tableNodes->setColumnWidth(6,  90);	/* Edit button	*/
+    this->tableNodes->setRowCount(query.size());
+    this->tableNodes->setHorizontalHeaderLabels(labels);
+    this->tableNodes->verticalHeader()->hide();
+    /* Set the widget size to 1054 x 575 in the ui. */
+
+    query.first();
+    for (int i = 0 ; i < query.size() ; i++ ) {
+	this->tableNodes->setItem(i, 0, new QTableWidgetItem(query.value("node").toString()));
+
+	if (query.value("online").toInt()) {
+	    item = new QTableWidgetItem(QString("Ok"));
+	} else {
+	    item = new QTableWidgetItem(QString("Offline"));
+	    item->setForeground(QBrush(QColor(Qt::red)));
+	}
+	item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+	this->tableNodes->setItem(i, 1, item);
+
+	item = new QTableWidgetItem(query.value("group_id").toString());
+	item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+	this->tableNodes->setItem(i, 2, item);
+
+	item = new QTableWidgetItem(query.value("lastseen").toDateTime().toString("dd MMM yyyy HH:mm:ss"));
+	item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+	this->tableNodes->setItem(i, 3, item);
+
+	this->tableNodes->setItem(i, 4, new QTableWidgetItem(query.value("net_address").toString()));
+
+	item = new QTableWidgetItem(query.value("up_interval").toString());
+	item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+	this->tableNodes->setItem(i, 5, item);
+
+	/* Add the Edit button */
+	QWidget* pWidget = new QWidget();
+	QPushButton* btn_edit = new QPushButton();
+	btn_edit->setObjectName(QString("%1").arg(query.value("record").toString()));	/* Send record with the button */
+	btn_edit->setText(tr("Details"));
+	connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_editButton_clicked()));
+	QHBoxLayout* pLayout = new QHBoxLayout(pWidget);
+	pLayout->addWidget(btn_edit);
+	pLayout->setContentsMargins(5, 0, 5, 0);
+	pWidget->setLayout(pLayout);
+	this->tableNodes->setCellWidget(i, 6, pWidget);
+	query.next();
+    }
+    emit setStatus(QString(tr("Total items: %1")).arg(query.size()));
+}
+
+
+MonNodes::~MonNodes() {}
+
+
+void MonNodes::refreshNodes(QString node)
+{
+    qDebug() << "refreshNodes" << node;
+    emit refreshTable();
+}
+
+
+void MonNodes::edit(int recno)
+{
+//    EditSupplier 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 MonNodes::on_editButton_clicked()
+{
+    QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
+    int recno = pb->objectName().toInt();
+    edit(recno);
+}
+
+

mercurial