#include <QDialog>
#include <QCloseEvent>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QCheckBox>
#include <QTableView>
#include <QStandardItemModel>
#include <QSignalMapper>
#include <QSortFilterProxyModel>
#include <QHeaderView>
#include <QMenu>
#include <QScrollBar>
#include <QFileDialog>
#include <QItemSelectionModel>
#include <fstream>
#include "VariablesBuffer.h"
#include "StringFunctions.h"
#include "CommandsBuffer.h"
#include "WindowInclude.h"
#include <unistd.h> /* Unix standard function definitions */
class WindowTimeMonitoring : public WindowInclude {
Q_OBJECT
private:
CommandsBuffer * commands;
VariablesBuffer * variables;
/** @brief Reference to the signal mapper for the filter */
/** @brief Reference to the filter data */
/** @brief Reference to the header of the table to show data */
std
::map<std
::string,
QCheckBox *> mapAviables;
std
::map<std
::string,
QCheckBox *> mapFilter;
/** @brief Reference to model with data */
public slots:
void filterAction();
void clearSlot();
void exportSlot();
void udpateTimeSlot();
public:
WindowTimeMonitoring
(VariablesBuffer
* variables, CommandsBuffer
* commands,
QWidget *parent
= nullptr
);
void addCommand(Command * command);
virtual ~WindowTimeMonitoring();
};
#include <QDialog>
#include <QCloseEvent>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QCheckBox>
#include <QTableView>
#include <QStandardItemModel>
#include <QSignalMapper>
#include <QSortFilterProxyModel>
#include <QHeaderView>
#include <QMenu>
#include <QScrollBar>
#include <QFileDialog>
#include <QItemSelectionModel>
#include <fstream>
#include "VariablesBuffer.h"
#include "StringFunctions.h"
#include "CommandsBuffer.h"
#include "WindowInclude.h"
#include <unistd.h> /* Unix standard function definitions */
class WindowTimeMonitoring : public WindowInclude {
Q_OBJECT
private:
QHBoxLayout * layoutMonitoring;
CommandsBuffer * commands;
VariablesBuffer * variables;
/** @brief Reference to the signal mapper for the filter */
QSignalMapper* signalMapper;
/** @brief Reference to the filter data */
QSortFilterProxyModel *filterModel = NULL;
/** @brief Reference to the header of the table to show data */
QHeaderView *tableHeader = NULL;
std::map<std::string, QCheckBox *> mapAviables;
std::map<std::string, QCheckBox *> mapFilter;
QTableView * dataTable;
/** @brief Reference to model with data */
QStandardItemModel *model;
public slots:
void filterAction();
void clearSlot();
void exportSlot();
void udpateTimeSlot();
public:
WindowTimeMonitoring(VariablesBuffer * variables, CommandsBuffer * commands, QWidget *parent = nullptr);
void addCommand(Command * command);
virtual ~WindowTimeMonitoring();
};
To copy to clipboard, switch view to plain text mode
#include "WindowTimeMonitoring.h"
WindowTimeMonitoring
::WindowTimeMonitoring(QWidget *parent
) : QDialog(parent
) {
this->setWindowTitle("Time Monitoring");
this->resize(800,600);
std::string modules("ARM1, ARM2, ARM3");
std::vector<std::string> listaModulos = strFunctions::split(modules, ',');
QObject::connect(buttonClear,
SIGNAL(clicked
()),
this,
SLOT(clearSlot
()));
QObject::connect(buttonClose,
SIGNAL(clicked
()),
this,
SLOT(closeSlot
()));
QObject::connect(buttonExport,
SIGNAL(clicked
()),
this,
SLOT(exportSlot
()));
QObject::connect(buttonRecal,
SIGNAL(clicked
()),
this,
SLOT(udpateTimeSlot
()));
layoutButtons->addWidget(buttonRecal);
layoutButtons->addWidget(buttonClear);
layoutButtons->addWidget(buttonClose);
layoutButtons->addWidget(buttonExport);
for(auto item: listaModulos) {
QObject::connect(mapFilter
[item
],
SIGNAL(clicked
()),
this,
SLOT(filterAction
()));
mapFilter[item]->setChecked(true);
layoutFilter->addWidget(mapFilter[item]);
}
groupFilter->setLayout(layoutFilter);
this->model->setColumnCount(5);
model
->setHeaderData
(0, Qt
::Horizontal,
QObject::tr("Time"));
model
->setHeaderData
(1, Qt
::Horizontal,
QObject::tr("Milliseconds"));
model
->setHeaderData
(2, Qt
::Horizontal,
QObject::tr("Time Spend"));
model
->setHeaderData
(3, Qt
::Horizontal,
QObject::tr("Module"));
model
->setHeaderData
(4, Qt
::Horizontal,
QObject::tr("Message"));
this->dataTable->setSortingEnabled(false);
this->dataTable->sortByColumn(1, Qt::AscendingOrder);
this->dataTable->setModel(this->model);
dataTable
->horizontalHeader
()->setSectionResizeMode
(QHeaderView::Stretch);
layoutMonitoring->addWidget(dataTable);
groupTimeMonitoring->setLayout(layoutMonitoring);
layoutMain->addWidget(groupAviable);
layoutMain->addWidget(groupFilter);
layoutMain->addWidget(groupTimeMonitoring);
layoutMain->addLayout(layoutButtons);
this->setLayout(layoutMain);
}
void WindowTimeMonitoring::udpateTimeSlot() {
int rows = auxModel->rowCount();
for (int i = 0; i < rows; i++) {
if (i == 0) {
} else {
unsigned long aux1 = auxModel->data(auxModel->index(i,1)).toString().toLong();
unsigned long aux2 = auxModel->data(auxModel->index(i-1,1)).toString().toLong();
}
}
}
void WindowTimeMonitoring::clearSlot() {
model->clear();
model->removeRows( 0, model->rowCount());
}
void WindowTimeMonitoring::exportSlot() {
tr("Select File"), "", tr("All Files (*)"));
if (! aux_filename.isEmpty()) {
QString textData
("Time,Milliseconds,Time Spend, Module, Message");
int rows = auxModel->rowCount();
int columns = auxModel->columnCount();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
textData += auxModel->data(auxModel->index(i,j)).toString();
textData += ", "; // for .csv file format
}
textData += "\n"; // (optional: for new line segmentation)
}
std::ofstream MyFile(aux_filename.toStdString());
MyFile << textData.toStdString();
MyFile.close();
}
}
/** **************************************************************************
* void WindowShowBufferPackaged::filterAction(QString cadena)
* @brief Filter table buffer data.
* @details Filter the table with the buffer data.
*
* @param cadena Conditions to filter the table. The first character is
* the column to apply the filter.
****************************************************************************/
void WindowTimeMonitoring::filterAction() {
std::vector<std::string> lista;
for(auto item: mapFilter) {
if (item.second->isChecked()) {
lista.push_back(item.first);
}
}
if (lista.size() == mapFilter.size()) {
this->dataTable->setModel(model);
//QRegExp filterString = QRegExp("", Qt::CaseInsensitive);
//filterModel->setFilterRegExp(filterString);
} else {
for (std::string item: lista) {
cadena
+= aux
+ QString(item.
c_str());
aux = "|";
}
cadena += ")$";
this->filterModel->setSourceModel(model);
//QRegExp filterString = QRegExp("", Qt::CaseInsensitive);
//filterModel->setFilterRegExp(filterString);
filterModel->setFilterRegExp(filterString);
filterModel->setFilterKeyColumn(-1);
this->dataTable->setModel(filterModel);
}
}
void WindowTimeMonitoring::addCommand(Command * command) {
this->model->setColumnCount(5);
QList<QStandardItem *> items;
items.append(item);
items.append(item);
items.append(item);
items.append(item);
items.append(item);
this->model->appendRow(items);
}
WindowTimeMonitoring::~WindowTimeMonitoring() {
// TODO Auto-generated destructor stub
}
#include "WindowTimeMonitoring.h"
WindowTimeMonitoring::WindowTimeMonitoring(QWidget *parent) : QDialog(parent) {
this->setWindowTitle("Time Monitoring");
this->resize(800,600);
std::string modules("ARM1, ARM2, ARM3");
QVBoxLayout * layoutMain = new QVBoxLayout();
std::vector<std::string> listaModulos = strFunctions::split(modules, ',');
QHBoxLayout * layoutButtons = new QHBoxLayout();
QPushButton *buttonClear = new QPushButton("Clear table");
QObject::connect(buttonClear, SIGNAL(clicked()), this, SLOT(clearSlot()));
QPushButton *buttonClose = new QPushButton("Close");
QObject::connect(buttonClose, SIGNAL(clicked()), this, SLOT(closeSlot()));
QPushButton *buttonExport = new QPushButton("Export to file");
QObject::connect(buttonExport, SIGNAL(clicked()), this, SLOT(exportSlot()));
QPushButton *buttonRecal = new QPushButton("Calculate spend time");
QObject::connect(buttonRecal, SIGNAL(clicked()), this, SLOT(udpateTimeSlot()));
layoutButtons->addWidget(buttonRecal);
layoutButtons->addWidget(buttonClear);
layoutButtons->addWidget(buttonClose);
layoutButtons->addWidget(buttonExport);
QGroupBox *groupFilter = new QGroupBox("Filter Monitoring:");
QHBoxLayout * layoutFilter = new QHBoxLayout();
for(auto item: listaModulos) {
mapFilter[item] = new QCheckBox(QString::fromStdString(item));
QObject::connect(mapFilter[item], SIGNAL(clicked()), this, SLOT(filterAction()));
mapFilter[item]->setChecked(true);
layoutFilter->addWidget(mapFilter[item]);
}
groupFilter->setLayout(layoutFilter);
this->model = new QStandardItemModel();
this->model->setColumnCount(5);
this->filterModel = new QSortFilterProxyModel();
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Time"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Milliseconds"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Time Spend"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Module"));
model->setHeaderData(4, Qt::Horizontal, QObject::tr("Message"));
QGroupBox *groupTimeMonitoring = new QGroupBox("Time Monitoring:");
layoutMonitoring = new QHBoxLayout();
dataTable = new QTableView();
this->dataTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
this->dataTable->setSortingEnabled(false);
this->dataTable->sortByColumn(1, Qt::AscendingOrder);
this->dataTable->setModel(this->model);
dataTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
layoutMonitoring->addWidget(dataTable);
groupTimeMonitoring->setLayout(layoutMonitoring);
layoutMain->addWidget(groupAviable);
layoutMain->addWidget(groupFilter);
layoutMain->addWidget(groupTimeMonitoring);
layoutMain->addLayout(layoutButtons);
this->setLayout(layoutMain);
}
void WindowTimeMonitoring::udpateTimeSlot() {
QStandardItemModel *auxModel = (QStandardItemModel*)this->dataTable->model();
int rows = auxModel->rowCount();
for (int i = 0; i < rows; i++) {
if (i == 0) {
auxModel->setItem(i, 2, new QStandardItem("0"));
} else {
unsigned long aux1 = auxModel->data(auxModel->index(i,1)).toString().toLong();
unsigned long aux2 = auxModel->data(auxModel->index(i-1,1)).toString().toLong();
QString s = QString::number(aux1 - aux2);
auxModel->setItem(i, 2, new QStandardItem(s));
}
}
}
void WindowTimeMonitoring::clearSlot() {
model->clear();
model->removeRows( 0, model->rowCount());
}
void WindowTimeMonitoring::exportSlot() {
QString aux_filename = QFileDialog::getSaveFileName(this,
tr("Select File"), "", tr("All Files (*)"));
if (! aux_filename.isEmpty()) {
QStandardItemModel *auxModel = (QStandardItemModel*)this->dataTable->model();
QString textData("Time,Milliseconds,Time Spend, Module, Message");
int rows = auxModel->rowCount();
int columns = auxModel->columnCount();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
textData += auxModel->data(auxModel->index(i,j)).toString();
textData += ", "; // for .csv file format
}
textData += "\n"; // (optional: for new line segmentation)
}
std::ofstream MyFile(aux_filename.toStdString());
MyFile << textData.toStdString();
MyFile.close();
}
}
/** **************************************************************************
* void WindowShowBufferPackaged::filterAction(QString cadena)
* @brief Filter table buffer data.
* @details Filter the table with the buffer data.
*
* @param cadena Conditions to filter the table. The first character is
* the column to apply the filter.
****************************************************************************/
void WindowTimeMonitoring::filterAction() {
std::vector<std::string> lista;
for(auto item: mapFilter) {
if (item.second->isChecked()) {
lista.push_back(item.first);
}
}
if (lista.size() == mapFilter.size()) {
this->dataTable->setModel(model);
//QRegExp filterString = QRegExp("", Qt::CaseInsensitive);
//filterModel->setFilterRegExp(filterString);
} else {
QString cadena("^(");
QString aux;
for (std::string item: lista) {
cadena += aux + QString(item.c_str());
aux = "|";
}
cadena += ")$";
this->filterModel->setSourceModel(model);
//QRegExp filterString = QRegExp("", Qt::CaseInsensitive);
//filterModel->setFilterRegExp(filterString);
QRegExp filterString = QRegExp(cadena, Qt::CaseInsensitive);
filterModel->setFilterRegExp(filterString);
filterModel->setFilterKeyColumn(-1);
this->dataTable->setModel(filterModel);
}
}
void WindowTimeMonitoring::addCommand(Command * command) {
QStandardItem *item;
this->model->setColumnCount(5);
QList<QStandardItem *> items;
item = new QStandardItem(QString(command->getTimeStr().c_str()));
items.append(item);
item = new QStandardItem(QString::number(command->getMiliSeconds()));
items.append(item);
item = new QStandardItem(QString(""));
items.append(item);
item = new QStandardItem(QString(command->origen.c_str()));
items.append(item);
item = new QStandardItem(QString(command->params.c_str()));
items.append(item);
this->model->appendRow(items);
}
WindowTimeMonitoring::~WindowTimeMonitoring() {
// TODO Auto-generated destructor stub
}
To copy to clipboard, switch view to plain text mode
Bookmarks