CatalogueEventsWidget.cpp
115 lines
| 4.2 KiB
| text/x-c
|
CppLexer
r1095 | #include "Catalogue/CatalogueEventsWidget.h" | |||
#include "ui_CatalogueEventsWidget.h" | ||||
r1129 | #include <Catalogue/CatalogueController.h> | |||
#include <CatalogueDao.h> | ||||
#include <DBCatalogue.h> | ||||
#include <SqpApplication.h> | ||||
/// Format of the dates appearing in the label of a cursor | ||||
const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss"); | ||||
r1105 | ||||
r1101 | struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate { | |||
void addEventItem(const QStringList &data, QTableWidget *tableWidget); | ||||
enum class Column { Event, TStart, TEnd, Tags, Product, NbColumn }; | ||||
QStringList columnNames() { return QStringList{"Event", "TStart", "TEnd", "Tags", "Product"}; } | ||||
r1129 | ||||
QVector<DBEvent> m_Events; | ||||
r1101 | }; | |||
r1095 | CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent) | |||
r1101 | : QWidget(parent), | |||
ui(new Ui::CatalogueEventsWidget), | ||||
impl{spimpl::make_unique_impl<CatalogueEventsWidgetPrivate>()} | ||||
r1095 | { | |||
ui->setupUi(this); | ||||
r1101 | ||||
connect(ui->btnTime, &QToolButton::clicked, [this](auto checked) { | ||||
if (checked) { | ||||
ui->btnChart->setChecked(false); | ||||
} | ||||
}); | ||||
connect(ui->btnChart, &QToolButton::clicked, [this](auto checked) { | ||||
if (checked) { | ||||
ui->btnTime->setChecked(false); | ||||
} | ||||
}); | ||||
r1105 | connect(ui->tableWidget, &QTableWidget::cellClicked, [this](auto row, auto column) { | |||
r1129 | auto event = impl->m_Events.value(row); | |||
r1105 | emit this->eventSelected(event); | |||
}); | ||||
connect(ui->tableWidget, &QTableWidget::currentItemChanged, | ||||
[this](auto current, auto previous) { | ||||
if (current && current->row() >= 0) { | ||||
r1129 | auto event = impl->m_Events.value(current->row()); | |||
r1105 | emit this->eventSelected(event); | |||
} | ||||
}); | ||||
connect(ui->tableWidget, &QTableWidget::itemSelectionChanged, [this]() { | ||||
auto selection = ui->tableWidget->selectedRanges(); | ||||
r1106 | auto isNotMultiSelection | |||
= selection.isEmpty() || (selection.count() == 1 && selection.first().rowCount() == 1); | ||||
ui->btnChart->setEnabled(isNotMultiSelection); | ||||
ui->btnTime->setEnabled(isNotMultiSelection); | ||||
r1105 | }); | |||
r1103 | ||||
r1101 | Q_ASSERT(impl->columnNames().count() == (int)CatalogueEventsWidgetPrivate::Column::NbColumn); | |||
ui->tableWidget->setColumnCount((int)CatalogueEventsWidgetPrivate::Column::NbColumn); | ||||
ui->tableWidget->setHorizontalHeaderLabels(impl->columnNames()); | ||||
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); | ||||
ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); | ||||
ui->tableWidget->horizontalHeader()->setSortIndicatorShown(true); | ||||
r1095 | } | |||
CatalogueEventsWidget::~CatalogueEventsWidget() | ||||
{ | ||||
delete ui; | ||||
} | ||||
r1101 | ||||
r1129 | void CatalogueEventsWidget::populateWithCatalogue(const DBCatalogue &catalogue) | |||
r1103 | { | |||
ui->tableWidget->clearContents(); | ||||
ui->tableWidget->setRowCount(0); | ||||
r1129 | auto &dao = sqpApp->catalogueController().getDao(); | |||
auto events = dao.getCatalogueEvents(catalogue); | ||||
for (auto event : events) { | ||||
impl->m_Events << event; | ||||
auto tags = event.getTags(); | ||||
QString tagList; | ||||
for (auto tag : tags) { | ||||
tagList += tag.getName(); | ||||
tagList += ' '; | ||||
} | ||||
impl->addEventItem({event.getName(), | ||||
DateUtils::dateTime(event.getTStart()).toString(DATETIME_FORMAT), | ||||
DateUtils::dateTime(event.getTEnd()).toString(DATETIME_FORMAT), tagList, | ||||
event.getProduct()}, | ||||
ui->tableWidget); | ||||
} | ||||
r1103 | } | |||
r1101 | void CatalogueEventsWidget::CatalogueEventsWidgetPrivate::addEventItem(const QStringList &data, | |||
QTableWidget *tableWidget) | ||||
{ | ||||
r1104 | tableWidget->setSortingEnabled(false); | |||
r1101 | auto row = tableWidget->rowCount(); | |||
tableWidget->setRowCount(row + 1); | ||||
for (auto i = 0; i < (int)Column::NbColumn; ++i) { | ||||
auto item = new QTableWidgetItem(data.value(i)); | ||||
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); | ||||
tableWidget->setItem(row, i, item); | ||||
} | ||||
r1104 | tableWidget->setSortingEnabled(true); | |||
r1101 | } | |||