##// END OF EJS Templates
New tool dialog to select a zone on time and chart modes
trabillard -
r1168:0d09b45e277b
parent child
Show More
@@ -1,113 +1,193
1 1 #include "Catalogue/CatalogueEventsWidget.h"
2 2 #include "ui_CatalogueEventsWidget.h"
3 3
4 4 #include <Catalogue/CatalogueController.h>
5 5 #include <Catalogue/CatalogueEventsTableModel.h>
6 6 #include <CatalogueDao.h>
7 7 #include <DBCatalogue.h>
8 8 #include <SqpApplication.h>
9 9
10 #include <QDialog>
11 #include <QDialogButtonBox>
12 #include <QListWidget>
13
10 14
11 15 /// Format of the dates appearing in the label of a cursor
12 16 const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss");
13 17
14 18 struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate {
15 19
16 20 CatalogueEventsTableModel *m_Model = nullptr;
21 QString m_ZoneForTimeMode;
22 QString m_ZoneForGraphMode;
17 23
18 24 void setEvents(const QVector<DBEvent> &events, QTableView *tableView)
19 25 {
20 26 tableView->setSortingEnabled(false);
21 27 m_Model->setEvents(events);
22 28 tableView->setSortingEnabled(true);
23 29 }
24 30
25 31 void addEvent(const DBEvent &event, QTableView *tableView)
26 32 {
27 33 tableView->setSortingEnabled(false);
28 34 m_Model->addEvent(event);
29 35 tableView->setSortingEnabled(true);
30 36 }
31 37
32 38 void removeEvent(const DBEvent &event, QTableView *tableView)
33 39 {
34 40 tableView->setSortingEnabled(false);
35 41 m_Model->removeEvent(event);
36 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 111 CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent)
42 112 : QWidget(parent),
43 113 ui(new Ui::CatalogueEventsWidget),
44 114 impl{spimpl::make_unique_impl<CatalogueEventsWidgetPrivate>()}
45 115 {
46 116 ui->setupUi(this);
47 117
48 118 impl->m_Model = new CatalogueEventsTableModel{this};
49 119 ui->tableView->setModel(impl->m_Model);
50 120
51 121 ui->tableView->setSortingEnabled(true);
52 122 ui->tableView->setDragDropMode(QAbstractItemView::DragDrop);
53 123 ui->tableView->setDragEnabled(true);
54 124
55 125 connect(ui->btnTime, &QToolButton::clicked, [this](auto checked) {
56 126 if (checked) {
57 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 136 connect(ui->btnChart, &QToolButton::clicked, [this](auto checked) {
62 137 if (checked) {
63 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 147 auto emitSelection = [this]() {
68 148 QVector<DBEvent> events;
69 149 for (auto rowIndex : ui->tableView->selectionModel()->selectedRows()) {
70 150 events << impl->m_Model->getEvent(rowIndex.row());
71 151 }
72 152
73 153 emit this->eventsSelected(events);
74 154 };
75 155
76 156 connect(ui->tableView, &QTableView::clicked, emitSelection);
77 157 connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, emitSelection);
78 158
79 159 connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, [this]() {
80 160 auto isNotMultiSelection = ui->tableView->selectionModel()->selectedRows().count() <= 1;
81 161 ui->btnChart->setEnabled(isNotMultiSelection);
82 162 ui->btnTime->setEnabled(isNotMultiSelection);
83 163 });
84 164
85 165 ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
86 166 ui->tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
87 167 ui->tableView->horizontalHeader()->setSortIndicatorShown(true);
88 168 }
89 169
90 170 CatalogueEventsWidget::~CatalogueEventsWidget()
91 171 {
92 172 delete ui;
93 173 }
94 174
95 175 void CatalogueEventsWidget::populateWithCatalogues(const QVector<DBCatalogue> &catalogues)
96 176 {
97 177 auto &dao = sqpApp->catalogueController().getDao();
98 178
99 179 QSet<QUuid> eventIds;
100 180 QVector<DBEvent> events;
101 181
102 182 for (auto catalogue : catalogues) {
103 183 auto catalogueEvents = dao.getCatalogueEvents(catalogue);
104 184 for (auto event : catalogueEvents) {
105 185 if (!eventIds.contains(event.getUniqId())) {
106 186 events << event;
107 187 eventIds.insert(event.getUniqId());
108 188 }
109 189 }
110 190 }
111 191
112 192 impl->setEvents(events, ui->tableView);
113 193 }
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved

Status change > Approved

You need to be logged in to leave comments. Login now