CatalogueEventsWidget.cpp
193 lines
| 6.9 KiB
| text/x-c
|
CppLexer
r1130 | #include "Catalogue/CatalogueEventsWidget.h" | |||
#include "ui_CatalogueEventsWidget.h" | ||||
r1162 | #include <Catalogue/CatalogueController.h> | |||
r1163 | #include <Catalogue/CatalogueEventsTableModel.h> | |||
r1162 | #include <CatalogueDao.h> | |||
#include <DBCatalogue.h> | ||||
#include <SqpApplication.h> | ||||
r1168 | #include <QDialog> | |||
#include <QDialogButtonBox> | ||||
#include <QListWidget> | ||||
r1162 | ||||
/// Format of the dates appearing in the label of a cursor | ||||
const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss"); | ||||
r1140 | ||||
r1136 | struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate { | |||
r1163 | CatalogueEventsTableModel *m_Model = nullptr; | |||
r1168 | QString m_ZoneForTimeMode; | |||
QString m_ZoneForGraphMode; | ||||
r1167 | ||||
void setEvents(const QVector<DBEvent> &events, QTableView *tableView) | ||||
{ | ||||
tableView->setSortingEnabled(false); | ||||
m_Model->setEvents(events); | ||||
tableView->setSortingEnabled(true); | ||||
} | ||||
void addEvent(const DBEvent &event, QTableView *tableView) | ||||
{ | ||||
tableView->setSortingEnabled(false); | ||||
m_Model->addEvent(event); | ||||
tableView->setSortingEnabled(true); | ||||
} | ||||
void removeEvent(const DBEvent &event, QTableView *tableView) | ||||
{ | ||||
tableView->setSortingEnabled(false); | ||||
m_Model->removeEvent(event); | ||||
tableView->setSortingEnabled(true); | ||||
} | ||||
r1136 | ||||
r1168 | QStringList selectZone(QWidget *parent, const QStringList &availableZones, | |||
const QStringList &selectedZones, bool allowMultiSelection, | ||||
const QPoint &location) | ||||
{ | ||||
QDialog d(parent, Qt::Tool); | ||||
d.setWindowTitle("Choose a zone"); | ||||
auto layout = new QVBoxLayout{&d}; | ||||
layout->setContentsMargins(0, 0, 0, 0); | ||||
auto listWidget = new QListWidget{&d}; | ||||
layout->addWidget(listWidget); | ||||
QSet<QListWidgetItem *> checkedItems; | ||||
for (auto zone : availableZones) { | ||||
auto item = new QListWidgetItem{zone}; | ||||
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable); | ||||
if (selectedZones.contains(zone)) { | ||||
item->setCheckState(Qt::Checked); | ||||
checkedItems << item; | ||||
} | ||||
else { | ||||
item->setCheckState(Qt::Unchecked); | ||||
} | ||||
listWidget->addItem(item); | ||||
} | ||||
auto buttonBox = new QDialogButtonBox{QDialogButtonBox::Ok, &d}; | ||||
layout->addWidget(buttonBox); | ||||
QObject::connect(buttonBox, &QDialogButtonBox::accepted, &d, &QDialog::accept); | ||||
QObject::connect(buttonBox, &QDialogButtonBox::rejected, &d, &QDialog::reject); | ||||
QObject::connect(listWidget, &QListWidget::itemChanged, | ||||
[&checkedItems, allowMultiSelection, listWidget](auto item) { | ||||
if (item->checkState() == Qt::Checked) { | ||||
if (!allowMultiSelection) { | ||||
for (auto checkedItem : checkedItems) { | ||||
listWidget->blockSignals(true); | ||||
checkedItem->setCheckState(Qt::Unchecked); | ||||
listWidget->blockSignals(false); | ||||
} | ||||
checkedItems.clear(); | ||||
} | ||||
checkedItems << item; | ||||
} | ||||
else { | ||||
checkedItems.remove(item); | ||||
} | ||||
}); | ||||
QStringList result; | ||||
d.setMinimumWidth(120); | ||||
d.resize(d.minimumSizeHint()); | ||||
d.move(location); | ||||
if (d.exec() == QDialog::Accepted) { | ||||
for (auto item : checkedItems) { | ||||
result += item->text(); | ||||
} | ||||
} | ||||
return result; | ||||
} | ||||
}; | ||||
r1136 | ||||
r1130 | CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent) | |||
r1136 | : QWidget(parent), | |||
ui(new Ui::CatalogueEventsWidget), | ||||
impl{spimpl::make_unique_impl<CatalogueEventsWidgetPrivate>()} | ||||
r1130 | { | |||
ui->setupUi(this); | ||||
r1136 | ||||
r1166 | impl->m_Model = new CatalogueEventsTableModel{this}; | |||
r1163 | ui->tableView->setModel(impl->m_Model); | |||
ui->tableView->setSortingEnabled(true); | ||||
r1167 | ui->tableView->setDragDropMode(QAbstractItemView::DragDrop); | |||
ui->tableView->setDragEnabled(true); | ||||
r1163 | ||||
r1136 | connect(ui->btnTime, &QToolButton::clicked, [this](auto checked) { | |||
if (checked) { | ||||
ui->btnChart->setChecked(false); | ||||
r1168 | impl->m_ZoneForTimeMode | |||
= impl->selectZone(this, {"Zone 1", "Zone 2", "Zone 3", "Zone 4"}, | ||||
{impl->m_ZoneForTimeMode}, false, | ||||
this->mapToGlobal(ui->btnTime->frameGeometry().center())) | ||||
.value(0); | ||||
r1136 | } | |||
}); | ||||
connect(ui->btnChart, &QToolButton::clicked, [this](auto checked) { | ||||
if (checked) { | ||||
ui->btnTime->setChecked(false); | ||||
r1168 | impl->m_ZoneForGraphMode | |||
= impl->selectZone(this, {"Zone 1", "Zone 2", "Zone 3", "Zone 4"}, | ||||
{impl->m_ZoneForGraphMode}, false, | ||||
this->mapToGlobal(ui->btnChart->frameGeometry().center())) | ||||
.value(0); | ||||
r1136 | } | |||
}); | ||||
r1165 | auto emitSelection = [this]() { | |||
r1164 | QVector<DBEvent> events; | |||
for (auto rowIndex : ui->tableView->selectionModel()->selectedRows()) { | ||||
events << impl->m_Model->getEvent(rowIndex.row()); | ||||
} | ||||
emit this->eventsSelected(events); | ||||
r1165 | }; | |||
r1140 | ||||
r1165 | connect(ui->tableView, &QTableView::clicked, emitSelection); | |||
connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, emitSelection); | ||||
r1140 | ||||
r1163 | connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, [this]() { | |||
auto isNotMultiSelection = ui->tableView->selectionModel()->selectedRows().count() <= 1; | ||||
r1141 | ui->btnChart->setEnabled(isNotMultiSelection); | |||
ui->btnTime->setEnabled(isNotMultiSelection); | ||||
r1140 | }); | |||
r1138 | ||||
r1163 | ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); | |||
ui->tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); | ||||
ui->tableView->horizontalHeader()->setSortIndicatorShown(true); | ||||
r1130 | } | |||
CatalogueEventsWidget::~CatalogueEventsWidget() | ||||
{ | ||||
delete ui; | ||||
} | ||||
r1136 | ||||
r1165 | void CatalogueEventsWidget::populateWithCatalogues(const QVector<DBCatalogue> &catalogues) | |||
r1138 | { | |||
r1162 | auto &dao = sqpApp->catalogueController().getDao(); | |||
r1165 | QSet<QUuid> eventIds; | |||
QVector<DBEvent> events; | ||||
for (auto catalogue : catalogues) { | ||||
auto catalogueEvents = dao.getCatalogueEvents(catalogue); | ||||
for (auto event : catalogueEvents) { | ||||
if (!eventIds.contains(event.getUniqId())) { | ||||
events << event; | ||||
eventIds.insert(event.getUniqId()); | ||||
} | ||||
} | ||||
r1162 | } | |||
r1138 | ||||
r1167 | impl->setEvents(events, ui->tableView); | |||
r1136 | } | |||