@@ -1,113 +1,193 | |||||
1 | #include "Catalogue/CatalogueEventsWidget.h" |
|
1 | #include "Catalogue/CatalogueEventsWidget.h" | |
2 | #include "ui_CatalogueEventsWidget.h" |
|
2 | #include "ui_CatalogueEventsWidget.h" | |
3 |
|
3 | |||
4 | #include <Catalogue/CatalogueController.h> |
|
4 | #include <Catalogue/CatalogueController.h> | |
5 | #include <Catalogue/CatalogueEventsTableModel.h> |
|
5 | #include <Catalogue/CatalogueEventsTableModel.h> | |
6 | #include <CatalogueDao.h> |
|
6 | #include <CatalogueDao.h> | |
7 | #include <DBCatalogue.h> |
|
7 | #include <DBCatalogue.h> | |
8 | #include <SqpApplication.h> |
|
8 | #include <SqpApplication.h> | |
9 |
|
9 | |||
|
10 | #include <QDialog> | |||
|
11 | #include <QDialogButtonBox> | |||
|
12 | #include <QListWidget> | |||
|
13 | ||||
10 |
|
14 | |||
11 | /// Format of the dates appearing in the label of a cursor |
|
15 | /// Format of the dates appearing in the label of a cursor | |
12 | const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss"); |
|
16 | const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss"); | |
13 |
|
17 | |||
14 | struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate { |
|
18 | struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate { | |
15 |
|
19 | |||
16 | CatalogueEventsTableModel *m_Model = nullptr; |
|
20 | CatalogueEventsTableModel *m_Model = nullptr; | |
|
21 | QString m_ZoneForTimeMode; | |||
|
22 | QString m_ZoneForGraphMode; | |||
17 |
|
23 | |||
18 | void setEvents(const QVector<DBEvent> &events, QTableView *tableView) |
|
24 | void setEvents(const QVector<DBEvent> &events, QTableView *tableView) | |
19 | { |
|
25 | { | |
20 | tableView->setSortingEnabled(false); |
|
26 | tableView->setSortingEnabled(false); | |
21 | m_Model->setEvents(events); |
|
27 | m_Model->setEvents(events); | |
22 | tableView->setSortingEnabled(true); |
|
28 | tableView->setSortingEnabled(true); | |
23 | } |
|
29 | } | |
24 |
|
30 | |||
25 | void addEvent(const DBEvent &event, QTableView *tableView) |
|
31 | void addEvent(const DBEvent &event, QTableView *tableView) | |
26 | { |
|
32 | { | |
27 | tableView->setSortingEnabled(false); |
|
33 | tableView->setSortingEnabled(false); | |
28 | m_Model->addEvent(event); |
|
34 | m_Model->addEvent(event); | |
29 | tableView->setSortingEnabled(true); |
|
35 | tableView->setSortingEnabled(true); | |
30 | } |
|
36 | } | |
31 |
|
37 | |||
32 | void removeEvent(const DBEvent &event, QTableView *tableView) |
|
38 | void removeEvent(const DBEvent &event, QTableView *tableView) | |
33 | { |
|
39 | { | |
34 | tableView->setSortingEnabled(false); |
|
40 | tableView->setSortingEnabled(false); | |
35 | m_Model->removeEvent(event); |
|
41 | m_Model->removeEvent(event); | |
36 | tableView->setSortingEnabled(true); |
|
42 | tableView->setSortingEnabled(true); | |
37 | } |
|
43 | } | |
38 | }; |
|
|||
39 |
|
44 | |||
|
45 | QStringList selectZone(QWidget *parent, const QStringList &availableZones, | |||
|
46 | const QStringList &selectedZones, bool allowMultiSelection, | |||
|
47 | const QPoint &location) | |||
|
48 | { | |||
|
49 | QDialog d(parent, Qt::Tool); | |||
|
50 | d.setWindowTitle("Choose a zone"); | |||
|
51 | auto layout = new QVBoxLayout{&d}; | |||
|
52 | layout->setContentsMargins(0, 0, 0, 0); | |||
|
53 | auto listWidget = new QListWidget{&d}; | |||
|
54 | layout->addWidget(listWidget); | |||
|
55 | ||||
|
56 | QSet<QListWidgetItem *> checkedItems; | |||
|
57 | for (auto zone : availableZones) { | |||
|
58 | auto item = new QListWidgetItem{zone}; | |||
|
59 | item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable); | |||
|
60 | if (selectedZones.contains(zone)) { | |||
|
61 | item->setCheckState(Qt::Checked); | |||
|
62 | checkedItems << item; | |||
|
63 | } | |||
|
64 | else { | |||
|
65 | item->setCheckState(Qt::Unchecked); | |||
|
66 | } | |||
|
67 | ||||
|
68 | listWidget->addItem(item); | |||
|
69 | } | |||
|
70 | ||||
|
71 | auto buttonBox = new QDialogButtonBox{QDialogButtonBox::Ok, &d}; | |||
|
72 | layout->addWidget(buttonBox); | |||
|
73 | ||||
|
74 | QObject::connect(buttonBox, &QDialogButtonBox::accepted, &d, &QDialog::accept); | |||
|
75 | QObject::connect(buttonBox, &QDialogButtonBox::rejected, &d, &QDialog::reject); | |||
|
76 | ||||
|
77 | QObject::connect(listWidget, &QListWidget::itemChanged, | |||
|
78 | [&checkedItems, allowMultiSelection, listWidget](auto item) { | |||
|
79 | if (item->checkState() == Qt::Checked) { | |||
|
80 | if (!allowMultiSelection) { | |||
|
81 | for (auto checkedItem : checkedItems) { | |||
|
82 | listWidget->blockSignals(true); | |||
|
83 | checkedItem->setCheckState(Qt::Unchecked); | |||
|
84 | listWidget->blockSignals(false); | |||
|
85 | } | |||
|
86 | ||||
|
87 | checkedItems.clear(); | |||
|
88 | } | |||
|
89 | checkedItems << item; | |||
|
90 | } | |||
|
91 | else { | |||
|
92 | checkedItems.remove(item); | |||
|
93 | } | |||
|
94 | }); | |||
|
95 | ||||
|
96 | QStringList result; | |||
|
97 | ||||
|
98 | d.setMinimumWidth(120); | |||
|
99 | d.resize(d.minimumSizeHint()); | |||
|
100 | d.move(location); | |||
|
101 | if (d.exec() == QDialog::Accepted) { | |||
|
102 | for (auto item : checkedItems) { | |||
|
103 | result += item->text(); | |||
|
104 | } | |||
|
105 | } | |||
|
106 | ||||
|
107 | return result; | |||
|
108 | } | |||
|
109 | }; | |||
40 |
|
110 | |||
41 | CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent) |
|
111 | CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent) | |
42 | : QWidget(parent), |
|
112 | : QWidget(parent), | |
43 | ui(new Ui::CatalogueEventsWidget), |
|
113 | ui(new Ui::CatalogueEventsWidget), | |
44 | impl{spimpl::make_unique_impl<CatalogueEventsWidgetPrivate>()} |
|
114 | impl{spimpl::make_unique_impl<CatalogueEventsWidgetPrivate>()} | |
45 | { |
|
115 | { | |
46 | ui->setupUi(this); |
|
116 | ui->setupUi(this); | |
47 |
|
117 | |||
48 | impl->m_Model = new CatalogueEventsTableModel{this}; |
|
118 | impl->m_Model = new CatalogueEventsTableModel{this}; | |
49 | ui->tableView->setModel(impl->m_Model); |
|
119 | ui->tableView->setModel(impl->m_Model); | |
50 |
|
120 | |||
51 | ui->tableView->setSortingEnabled(true); |
|
121 | ui->tableView->setSortingEnabled(true); | |
52 | ui->tableView->setDragDropMode(QAbstractItemView::DragDrop); |
|
122 | ui->tableView->setDragDropMode(QAbstractItemView::DragDrop); | |
53 | ui->tableView->setDragEnabled(true); |
|
123 | ui->tableView->setDragEnabled(true); | |
54 |
|
124 | |||
55 | connect(ui->btnTime, &QToolButton::clicked, [this](auto checked) { |
|
125 | connect(ui->btnTime, &QToolButton::clicked, [this](auto checked) { | |
56 | if (checked) { |
|
126 | if (checked) { | |
57 | ui->btnChart->setChecked(false); |
|
127 | ui->btnChart->setChecked(false); | |
|
128 | impl->m_ZoneForTimeMode | |||
|
129 | = impl->selectZone(this, {"Zone 1", "Zone 2", "Zone 3", "Zone 4"}, | |||
|
130 | {impl->m_ZoneForTimeMode}, false, | |||
|
131 | this->mapToGlobal(ui->btnTime->frameGeometry().center())) | |||
|
132 | .value(0); | |||
58 | } |
|
133 | } | |
59 | }); |
|
134 | }); | |
60 |
|
135 | |||
61 | connect(ui->btnChart, &QToolButton::clicked, [this](auto checked) { |
|
136 | connect(ui->btnChart, &QToolButton::clicked, [this](auto checked) { | |
62 | if (checked) { |
|
137 | if (checked) { | |
63 | ui->btnTime->setChecked(false); |
|
138 | ui->btnTime->setChecked(false); | |
|
139 | impl->m_ZoneForGraphMode | |||
|
140 | = impl->selectZone(this, {"Zone 1", "Zone 2", "Zone 3", "Zone 4"}, | |||
|
141 | {impl->m_ZoneForGraphMode}, false, | |||
|
142 | this->mapToGlobal(ui->btnChart->frameGeometry().center())) | |||
|
143 | .value(0); | |||
64 | } |
|
144 | } | |
65 | }); |
|
145 | }); | |
66 |
|
146 | |||
67 | auto emitSelection = [this]() { |
|
147 | auto emitSelection = [this]() { | |
68 | QVector<DBEvent> events; |
|
148 | QVector<DBEvent> events; | |
69 | for (auto rowIndex : ui->tableView->selectionModel()->selectedRows()) { |
|
149 | for (auto rowIndex : ui->tableView->selectionModel()->selectedRows()) { | |
70 | events << impl->m_Model->getEvent(rowIndex.row()); |
|
150 | events << impl->m_Model->getEvent(rowIndex.row()); | |
71 | } |
|
151 | } | |
72 |
|
152 | |||
73 | emit this->eventsSelected(events); |
|
153 | emit this->eventsSelected(events); | |
74 | }; |
|
154 | }; | |
75 |
|
155 | |||
76 | connect(ui->tableView, &QTableView::clicked, emitSelection); |
|
156 | connect(ui->tableView, &QTableView::clicked, emitSelection); | |
77 | connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, emitSelection); |
|
157 | connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, emitSelection); | |
78 |
|
158 | |||
79 | connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, [this]() { |
|
159 | connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, [this]() { | |
80 | auto isNotMultiSelection = ui->tableView->selectionModel()->selectedRows().count() <= 1; |
|
160 | auto isNotMultiSelection = ui->tableView->selectionModel()->selectedRows().count() <= 1; | |
81 | ui->btnChart->setEnabled(isNotMultiSelection); |
|
161 | ui->btnChart->setEnabled(isNotMultiSelection); | |
82 | ui->btnTime->setEnabled(isNotMultiSelection); |
|
162 | ui->btnTime->setEnabled(isNotMultiSelection); | |
83 | }); |
|
163 | }); | |
84 |
|
164 | |||
85 | ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); |
|
165 | ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); | |
86 | ui->tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); |
|
166 | ui->tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); | |
87 | ui->tableView->horizontalHeader()->setSortIndicatorShown(true); |
|
167 | ui->tableView->horizontalHeader()->setSortIndicatorShown(true); | |
88 | } |
|
168 | } | |
89 |
|
169 | |||
90 | CatalogueEventsWidget::~CatalogueEventsWidget() |
|
170 | CatalogueEventsWidget::~CatalogueEventsWidget() | |
91 | { |
|
171 | { | |
92 | delete ui; |
|
172 | delete ui; | |
93 | } |
|
173 | } | |
94 |
|
174 | |||
95 | void CatalogueEventsWidget::populateWithCatalogues(const QVector<DBCatalogue> &catalogues) |
|
175 | void CatalogueEventsWidget::populateWithCatalogues(const QVector<DBCatalogue> &catalogues) | |
96 | { |
|
176 | { | |
97 | auto &dao = sqpApp->catalogueController().getDao(); |
|
177 | auto &dao = sqpApp->catalogueController().getDao(); | |
98 |
|
178 | |||
99 | QSet<QUuid> eventIds; |
|
179 | QSet<QUuid> eventIds; | |
100 | QVector<DBEvent> events; |
|
180 | QVector<DBEvent> events; | |
101 |
|
181 | |||
102 | for (auto catalogue : catalogues) { |
|
182 | for (auto catalogue : catalogues) { | |
103 | auto catalogueEvents = dao.getCatalogueEvents(catalogue); |
|
183 | auto catalogueEvents = dao.getCatalogueEvents(catalogue); | |
104 | for (auto event : catalogueEvents) { |
|
184 | for (auto event : catalogueEvents) { | |
105 | if (!eventIds.contains(event.getUniqId())) { |
|
185 | if (!eventIds.contains(event.getUniqId())) { | |
106 | events << event; |
|
186 | events << event; | |
107 | eventIds.insert(event.getUniqId()); |
|
187 | eventIds.insert(event.getUniqId()); | |
108 | } |
|
188 | } | |
109 | } |
|
189 | } | |
110 | } |
|
190 | } | |
111 |
|
191 | |||
112 | impl->setEvents(events, ui->tableView); |
|
192 | impl->setEvents(events, ui->tableView); | |
113 | } |
|
193 | } |
General Comments 0
You need to be logged in to leave comments.
Login now