Auto status change to "Under Review"
@@ -1,338 +1,329 | |||
|
1 | 1 | #include <Catalogue/CatalogueController.h> |
|
2 | 2 | |
|
3 | 3 | #include <Variable/Variable.h> |
|
4 | 4 | |
|
5 | 5 | #include <CatalogueDao.h> |
|
6 | 6 | |
|
7 | 7 | #include <ComparaisonPredicate.h> |
|
8 | 8 | #include <CompoundPredicate.h> |
|
9 | 9 | #include <DBCatalogue.h> |
|
10 | 10 | #include <DBEvent.h> |
|
11 | 11 | #include <DBEventProduct.h> |
|
12 | 12 | #include <DBTag.h> |
|
13 | 13 | #include <IRequestPredicate.h> |
|
14 | 14 | |
|
15 | 15 | #include <QMutex> |
|
16 | 16 | #include <QThread> |
|
17 | 17 | |
|
18 | 18 | #include <QDir> |
|
19 | 19 | #include <QStandardPaths> |
|
20 | 20 | |
|
21 | 21 | Q_LOGGING_CATEGORY(LOG_CatalogueController, "CatalogueController") |
|
22 | 22 | |
|
23 | 23 | namespace { |
|
24 | 24 | |
|
25 | 25 | static QString REPOSITORY_WORK_SUFFIX = QString{"_work"}; |
|
26 | 26 | static QString REPOSITORY_TRASH_SUFFIX = QString{"_trash"}; |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | 29 | class CatalogueController::CatalogueControllerPrivate { |
|
30 | 30 | |
|
31 | 31 | public: |
|
32 | 32 | explicit CatalogueControllerPrivate(CatalogueController *parent) : m_Q{parent} {} |
|
33 | 33 | |
|
34 | 34 | QMutex m_WorkingMutex; |
|
35 | 35 | CatalogueDao m_CatalogueDao; |
|
36 | 36 | |
|
37 | 37 | QStringList m_RepositoryList; |
|
38 | 38 | CatalogueController *m_Q; |
|
39 | 39 | |
|
40 | 40 | void copyDBtoDB(const QString &dbFrom, const QString &dbTo); |
|
41 | 41 | QString toWorkRepository(QString repository); |
|
42 | 42 | QString toSyncRepository(QString repository); |
|
43 | 43 | void savAllDB(); |
|
44 | 44 | |
|
45 | 45 | void saveEvent(std::shared_ptr<DBEvent> event, bool persist = true); |
|
46 | 46 | void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue, bool persist = true); |
|
47 | 47 | }; |
|
48 | 48 | |
|
49 | 49 | CatalogueController::CatalogueController(QObject *parent) |
|
50 | 50 | : impl{spimpl::make_unique_impl<CatalogueControllerPrivate>(this)} |
|
51 | 51 | { |
|
52 | 52 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController construction") |
|
53 | 53 | << QThread::currentThread(); |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | CatalogueController::~CatalogueController() |
|
57 | 57 | { |
|
58 | 58 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController destruction") |
|
59 | 59 | << QThread::currentThread(); |
|
60 | 60 | this->waitForFinish(); |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | QStringList CatalogueController::getRepositories() const |
|
64 | 64 | { |
|
65 | 65 | return impl->m_RepositoryList; |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | void CatalogueController::addDB(const QString &dbPath) |
|
69 | 69 | { |
|
70 | 70 | QDir dbDir(dbPath); |
|
71 | 71 | if (dbDir.exists()) { |
|
72 | 72 | auto dirName = dbDir.dirName(); |
|
73 | 73 | |
|
74 | 74 | if (std::find(impl->m_RepositoryList.cbegin(), impl->m_RepositoryList.cend(), dirName) |
|
75 | 75 | != impl->m_RepositoryList.cend()) { |
|
76 | 76 | qCCritical(LOG_CatalogueController()) |
|
77 | 77 | << tr("Impossible to addDB that is already loaded"); |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | if (!impl->m_CatalogueDao.addDB(dbPath, dirName)) { |
|
81 | 81 | qCCritical(LOG_CatalogueController()) |
|
82 | 82 | << tr("Impossible to addDB %1 from %2 ").arg(dirName, dbPath); |
|
83 | 83 | } |
|
84 | 84 | else { |
|
85 | 85 | impl->m_RepositoryList << dirName; |
|
86 | 86 | impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName)); |
|
87 | 87 | } |
|
88 | 88 | } |
|
89 | 89 | else { |
|
90 | 90 | qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB that not exists: ") |
|
91 | 91 | << dbPath; |
|
92 | 92 | } |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | void CatalogueController::saveDB(const QString &destinationPath, const QString &repository) |
|
96 | 96 | { |
|
97 | 97 | if (!impl->m_CatalogueDao.saveDB(destinationPath, repository)) { |
|
98 | 98 | qCCritical(LOG_CatalogueController()) |
|
99 | 99 | << tr("Impossible to saveDB %1 from %2 ").arg(repository, destinationPath); |
|
100 | 100 | } |
|
101 | 101 | } |
|
102 | 102 | |
|
103 | 103 | std::list<std::shared_ptr<DBEvent> > |
|
104 | 104 | CatalogueController::retrieveEvents(const QString &repository) const |
|
105 | 105 | { |
|
106 | 106 | QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository; |
|
107 | 107 | |
|
108 | 108 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; |
|
109 | 109 | auto events = impl->m_CatalogueDao.getEvents(impl->toWorkRepository(dbDireName)); |
|
110 | 110 | for (auto event : events) { |
|
111 | 111 | eventsShared.push_back(std::make_shared<DBEvent>(event)); |
|
112 | 112 | } |
|
113 | 113 | return eventsShared; |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | std::list<std::shared_ptr<DBEvent> > CatalogueController::retrieveAllEvents() const |
|
117 | 117 | { |
|
118 | 118 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; |
|
119 | 119 | for (auto repository : impl->m_RepositoryList) { |
|
120 | 120 | eventsShared.splice(eventsShared.end(), retrieveEvents(repository)); |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | return eventsShared; |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | std::list<std::shared_ptr<DBEvent> > |
|
127 | 127 | CatalogueController::retrieveEventsFromCatalogue(std::shared_ptr<DBCatalogue> catalogue) const |
|
128 | 128 | { |
|
129 | 129 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; |
|
130 | 130 | auto events = impl->m_CatalogueDao.getCatalogueEvents(*catalogue); |
|
131 | 131 | for (auto event : events) { |
|
132 | 132 | eventsShared.push_back(std::make_shared<DBEvent>(event)); |
|
133 | 133 | } |
|
134 | 134 | return eventsShared; |
|
135 | 135 | } |
|
136 | 136 | |
|
137 | 137 | void CatalogueController::updateEvent(std::shared_ptr<DBEvent> event) |
|
138 | 138 | { |
|
139 | 139 | event->setRepository(impl->toWorkRepository(event->getRepository())); |
|
140 | 140 | |
|
141 | 141 | impl->m_CatalogueDao.updateEvent(*event); |
|
142 | 142 | } |
|
143 | 143 | |
|
144 | 144 | void CatalogueController::removeEvent(std::shared_ptr<DBEvent> event) |
|
145 | 145 | { |
|
146 | 146 | // Remove it from both repository and repository_work |
|
147 | 147 | event->setRepository(impl->toWorkRepository(event->getRepository())); |
|
148 | 148 | impl->m_CatalogueDao.removeEvent(*event); |
|
149 | 149 | event->setRepository(impl->toSyncRepository(event->getRepository())); |
|
150 | 150 | impl->m_CatalogueDao.removeEvent(*event); |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | void CatalogueController::addEvent(std::shared_ptr<DBEvent> event) |
|
154 | 154 | { |
|
155 | 155 | event->setRepository(impl->toWorkRepository(event->getRepository())); |
|
156 | 156 | |
|
157 | 157 | auto eventTemp = *event; |
|
158 | 158 | impl->m_CatalogueDao.addEvent(eventTemp); |
|
159 | 159 | |
|
160 | 160 | // Call update is necessary at the creation of add Event if it has some tags or some event |
|
161 | 161 | // products |
|
162 | 162 | if (!event->getEventProducts().empty() || !event->getTags().empty()) { |
|
163 | 163 | |
|
164 | 164 | auto eventProductsTemp = eventTemp.getEventProducts(); |
|
165 | 165 | auto eventProductTempUpdated = std::list<DBEventProduct>{}; |
|
166 | 166 | for (auto eventProductTemp : eventProductsTemp) { |
|
167 | 167 | eventProductTemp.setEvent(eventTemp); |
|
168 | 168 | eventProductTempUpdated.push_back(eventProductTemp); |
|
169 | 169 | } |
|
170 | 170 | eventTemp.setEventProducts(eventProductTempUpdated); |
|
171 | 171 | |
|
172 | 172 | impl->m_CatalogueDao.updateEvent(eventTemp); |
|
173 | 173 | } |
|
174 | 174 | } |
|
175 | 175 | |
|
176 | 176 | void CatalogueController::saveEvent(std::shared_ptr<DBEvent> event) |
|
177 | 177 | { |
|
178 | 178 | impl->saveEvent(event, true); |
|
179 | 179 | } |
|
180 | 180 | |
|
181 | 181 | std::list<std::shared_ptr<DBCatalogue> > |
|
182 | 182 | CatalogueController::retrieveCatalogues(const QString &repository) const |
|
183 | 183 | { |
|
184 | 184 | QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository; |
|
185 | 185 | |
|
186 | 186 | auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{}; |
|
187 | 187 | auto catalogues = impl->m_CatalogueDao.getCatalogues(impl->toWorkRepository(dbDireName)); |
|
188 | 188 | for (auto catalogue : catalogues) { |
|
189 | 189 | cataloguesShared.push_back(std::make_shared<DBCatalogue>(catalogue)); |
|
190 | 190 | } |
|
191 | 191 | return cataloguesShared; |
|
192 | 192 | } |
|
193 | 193 | |
|
194 | 194 | void CatalogueController::updateCatalogue(std::shared_ptr<DBCatalogue> catalogue) |
|
195 | 195 | { |
|
196 | 196 | catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository())); |
|
197 | 197 | |
|
198 | 198 | impl->m_CatalogueDao.updateCatalogue(*catalogue); |
|
199 | 199 | } |
|
200 | 200 | |
|
201 | 201 | void CatalogueController::removeCatalogue(std::shared_ptr<DBCatalogue> catalogue) |
|
202 | 202 | { |
|
203 | 203 | // Remove it from both repository and repository_work |
|
204 | 204 | catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository())); |
|
205 | 205 | impl->m_CatalogueDao.removeCatalogue(*catalogue); |
|
206 | 206 | catalogue->setRepository(impl->toSyncRepository(catalogue->getRepository())); |
|
207 | 207 | impl->m_CatalogueDao.removeCatalogue(*catalogue); |
|
208 | 208 | } |
|
209 | 209 | |
|
210 | 210 | void CatalogueController::saveCatalogue(std::shared_ptr<DBCatalogue> catalogue) |
|
211 | 211 | { |
|
212 | 212 | impl->saveCatalogue(catalogue, true); |
|
213 | 213 | } |
|
214 | 214 | |
|
215 | 215 | void CatalogueController::saveAll() |
|
216 | 216 | { |
|
217 | 217 | for (auto repository : impl->m_RepositoryList) { |
|
218 | 218 | // Save Event |
|
219 | 219 | auto events = this->retrieveEvents(repository); |
|
220 | 220 | for (auto event : events) { |
|
221 | 221 | impl->saveEvent(event, false); |
|
222 | 222 | } |
|
223 | 223 | |
|
224 | 224 | // Save Catalogue |
|
225 | 225 | auto catalogues = this->retrieveCatalogues(repository); |
|
226 | 226 | for (auto catalogue : catalogues) { |
|
227 | 227 | impl->saveCatalogue(catalogue, false); |
|
228 | 228 | } |
|
229 | 229 | } |
|
230 | 230 | |
|
231 | 231 | impl->savAllDB(); |
|
232 | 232 | } |
|
233 | 233 | |
|
234 | 234 | void CatalogueController::initialize() |
|
235 | 235 | { |
|
236 | 236 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init") |
|
237 | 237 | << QThread::currentThread(); |
|
238 | 238 | impl->m_WorkingMutex.lock(); |
|
239 | 239 | impl->m_CatalogueDao.initialize(); |
|
240 | 240 | auto defaultRepositoryLocation |
|
241 | 241 | = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); |
|
242 | 242 | |
|
243 | 243 | QDir defaultRepositoryLocationDir; |
|
244 | 244 | if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation)) { |
|
245 | 245 | defaultRepositoryLocationDir.cd(defaultRepositoryLocation); |
|
246 | 246 | auto defaultRepository = defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT); |
|
247 | 247 | qCInfo(LOG_CatalogueController()) << tr("Persistant data loading from: ") |
|
248 | 248 | << defaultRepository; |
|
249 | 249 | this->addDB(defaultRepository); |
|
250 | 250 | } |
|
251 | 251 | else { |
|
252 | 252 | qCWarning(LOG_CatalogueController()) |
|
253 | 253 | << tr("Cannot load the persistent default repository from ") |
|
254 | 254 | << defaultRepositoryLocation; |
|
255 | 255 | } |
|
256 | 256 | |
|
257 | 257 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END"); |
|
258 | 258 | } |
|
259 | 259 | |
|
260 | 260 | void CatalogueController::finalize() |
|
261 | 261 | { |
|
262 | 262 | impl->m_WorkingMutex.unlock(); |
|
263 | 263 | } |
|
264 | 264 | |
|
265 | 265 | void CatalogueController::waitForFinish() |
|
266 | 266 | { |
|
267 | 267 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
268 | 268 | } |
|
269 | 269 | |
|
270 | 270 | void CatalogueController::CatalogueControllerPrivate::copyDBtoDB(const QString &dbFrom, |
|
271 | 271 | const QString &dbTo) |
|
272 | 272 | { |
|
273 | auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{}; | |
|
273 | // auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{}; | |
|
274 | 274 | auto catalogues = m_CatalogueDao.getCatalogues(dbFrom); |
|
275 | auto events = m_CatalogueDao.getEvents(dbFrom); | |
|
275 | 276 | for (auto catalogue : catalogues) { |
|
276 | cataloguesShared.push_back(std::make_shared<DBCatalogue>(catalogue)); | |
|
277 | m_CatalogueDao.copyCatalogue(catalogue, dbTo, true); | |
|
277 | 278 | } |
|
278 | 279 | |
|
279 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; | |
|
280 | auto events = m_CatalogueDao.getEvents(dbFrom); | |
|
281 | 280 | for (auto event : events) { |
|
282 | eventsShared.push_back(std::make_shared<DBEvent>(event)); | |
|
283 | } | |
|
284 | ||
|
285 | for (auto catalogue : cataloguesShared) { | |
|
286 | m_CatalogueDao.copyCatalogue(*catalogue, dbTo, true); | |
|
287 | } | |
|
288 | ||
|
289 | for (auto event : eventsShared) { | |
|
290 | m_CatalogueDao.copyEvent(*event, dbTo, true); | |
|
281 | m_CatalogueDao.copyEvent(event, dbTo, true); | |
|
291 | 282 | } |
|
292 | 283 | } |
|
293 | 284 | |
|
294 | 285 | QString CatalogueController::CatalogueControllerPrivate::toWorkRepository(QString repository) |
|
295 | 286 | { |
|
296 | 287 | auto syncRepository = toSyncRepository(repository); |
|
297 | 288 | |
|
298 | 289 | return QString("%1%2").arg(syncRepository, REPOSITORY_WORK_SUFFIX); |
|
299 | 290 | } |
|
300 | 291 | |
|
301 | 292 | QString CatalogueController::CatalogueControllerPrivate::toSyncRepository(QString repository) |
|
302 | 293 | { |
|
303 | 294 | auto syncRepository = repository; |
|
304 | 295 | if (repository.endsWith(REPOSITORY_WORK_SUFFIX)) { |
|
305 | 296 | syncRepository.remove(REPOSITORY_WORK_SUFFIX); |
|
306 | 297 | } |
|
307 | 298 | else if (repository.endsWith(REPOSITORY_TRASH_SUFFIX)) { |
|
308 | 299 | syncRepository.remove(REPOSITORY_TRASH_SUFFIX); |
|
309 | 300 | } |
|
310 | 301 | return syncRepository; |
|
311 | 302 | } |
|
312 | 303 | |
|
313 | 304 | void CatalogueController::CatalogueControllerPrivate::savAllDB() |
|
314 | 305 | { |
|
315 | 306 | for (auto repository : m_RepositoryList) { |
|
316 | 307 | auto defaultRepositoryLocation |
|
317 | 308 | = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); |
|
318 | 309 | m_CatalogueDao.saveDB(defaultRepositoryLocation, repository); |
|
319 | 310 | } |
|
320 | 311 | } |
|
321 | 312 | |
|
322 | 313 | void CatalogueController::CatalogueControllerPrivate::saveEvent(std::shared_ptr<DBEvent> event, |
|
323 | 314 | bool persist) |
|
324 | 315 | { |
|
325 |
m_CatalogueDao. |
|
|
316 | m_CatalogueDao.copyEvent(*event, toSyncRepository(event->getRepository()), true); | |
|
326 | 317 | if (persist) { |
|
327 | 318 | savAllDB(); |
|
328 | 319 | } |
|
329 | 320 | } |
|
330 | 321 | |
|
331 | 322 | void CatalogueController::CatalogueControllerPrivate::saveCatalogue( |
|
332 | 323 | std::shared_ptr<DBCatalogue> catalogue, bool persist) |
|
333 | 324 | { |
|
334 |
m_CatalogueDao. |
|
|
325 | m_CatalogueDao.copyCatalogue(*catalogue, toSyncRepository(catalogue->getRepository()), true); | |
|
335 | 326 | if (persist) { |
|
336 | 327 | savAllDB(); |
|
337 | 328 | } |
|
338 | 329 | } |
@@ -1,360 +1,364 | |||
|
1 | 1 | #include "Catalogue/CatalogueEventsWidget.h" |
|
2 | 2 | #include "ui_CatalogueEventsWidget.h" |
|
3 | 3 | |
|
4 | 4 | #include <Catalogue/CatalogueController.h> |
|
5 | 5 | #include <Catalogue/CatalogueEventsModel.h> |
|
6 | 6 | #include <Catalogue/CatalogueExplorerHelper.h> |
|
7 | 7 | #include <CatalogueDao.h> |
|
8 | 8 | #include <DBCatalogue.h> |
|
9 | 9 | #include <SqpApplication.h> |
|
10 | 10 | #include <Visualization/VisualizationTabWidget.h> |
|
11 | 11 | #include <Visualization/VisualizationWidget.h> |
|
12 | 12 | #include <Visualization/VisualizationZoneWidget.h> |
|
13 | 13 | |
|
14 | 14 | #include <QDialog> |
|
15 | 15 | #include <QDialogButtonBox> |
|
16 | 16 | #include <QListWidget> |
|
17 | 17 | |
|
18 | 18 | Q_LOGGING_CATEGORY(LOG_CatalogueEventsWidget, "CatalogueEventsWidget") |
|
19 | 19 | |
|
20 | 20 | /// Fixed size of the validation column |
|
21 | 21 | const auto VALIDATION_COLUMN_SIZE = 35; |
|
22 | 22 | |
|
23 | 23 | struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate { |
|
24 | 24 | |
|
25 | 25 | CatalogueEventsModel *m_Model = nullptr; |
|
26 | 26 | QStringList m_ZonesForTimeMode; |
|
27 | 27 | QString m_ZoneForGraphMode; |
|
28 | 28 | |
|
29 | 29 | VisualizationWidget *m_VisualizationWidget = nullptr; |
|
30 | 30 | |
|
31 | 31 | void setEvents(const QVector<std::shared_ptr<DBEvent> > &events, QTreeView *treeView) |
|
32 | 32 | { |
|
33 | 33 | treeView->setSortingEnabled(false); |
|
34 | 34 | m_Model->setEvents(events); |
|
35 | 35 | treeView->setSortingEnabled(true); |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | void addEvent(const std::shared_ptr<DBEvent> &event, QTreeView *treeView) |
|
39 | 39 | { |
|
40 | 40 | treeView->setSortingEnabled(false); |
|
41 | 41 | m_Model->addEvent(event); |
|
42 | 42 | treeView->setSortingEnabled(true); |
|
43 | 43 | } |
|
44 | 44 | |
|
45 | 45 | void removeEvent(const std::shared_ptr<DBEvent> &event, QTreeView *treeView) |
|
46 | 46 | { |
|
47 | 47 | treeView->setSortingEnabled(false); |
|
48 | 48 | m_Model->removeEvent(event); |
|
49 | 49 | treeView->setSortingEnabled(true); |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | QStringList getAvailableVisualizationZoneList() const |
|
53 | 53 | { |
|
54 | 54 | if (m_VisualizationWidget) { |
|
55 | 55 | if (auto tab = m_VisualizationWidget->currentTabWidget()) { |
|
56 | 56 | return tab->availableZoneWidgets(); |
|
57 | 57 | } |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | return QStringList{}; |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | QStringList selectZone(QWidget *parent, const QStringList &selectedZones, |
|
64 | 64 | bool allowMultiSelection, const QPoint &location) |
|
65 | 65 | { |
|
66 | 66 | auto availableZones = getAvailableVisualizationZoneList(); |
|
67 | 67 | if (availableZones.isEmpty()) { |
|
68 | 68 | return QStringList{}; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | QDialog d(parent, Qt::Tool); |
|
72 | 72 | d.setWindowTitle("Choose a zone"); |
|
73 | 73 | auto layout = new QVBoxLayout{&d}; |
|
74 | 74 | layout->setContentsMargins(0, 0, 0, 0); |
|
75 | 75 | auto listWidget = new QListWidget{&d}; |
|
76 | 76 | layout->addWidget(listWidget); |
|
77 | 77 | |
|
78 | 78 | QSet<QListWidgetItem *> checkedItems; |
|
79 | 79 | for (auto zone : availableZones) { |
|
80 | 80 | auto item = new QListWidgetItem{zone}; |
|
81 | 81 | item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable); |
|
82 | 82 | if (selectedZones.contains(zone)) { |
|
83 | 83 | item->setCheckState(Qt::Checked); |
|
84 | 84 | checkedItems << item; |
|
85 | 85 | } |
|
86 | 86 | else { |
|
87 | 87 | item->setCheckState(Qt::Unchecked); |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | listWidget->addItem(item); |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | auto buttonBox = new QDialogButtonBox{QDialogButtonBox::Ok, &d}; |
|
94 | 94 | layout->addWidget(buttonBox); |
|
95 | 95 | |
|
96 | 96 | QObject::connect(buttonBox, &QDialogButtonBox::accepted, &d, &QDialog::accept); |
|
97 | 97 | QObject::connect(buttonBox, &QDialogButtonBox::rejected, &d, &QDialog::reject); |
|
98 | 98 | |
|
99 | 99 | QObject::connect(listWidget, &QListWidget::itemChanged, |
|
100 | 100 | [&checkedItems, allowMultiSelection, listWidget](auto item) { |
|
101 | 101 | if (item->checkState() == Qt::Checked) { |
|
102 | 102 | if (!allowMultiSelection) { |
|
103 | 103 | for (auto checkedItem : checkedItems) { |
|
104 | 104 | listWidget->blockSignals(true); |
|
105 | 105 | checkedItem->setCheckState(Qt::Unchecked); |
|
106 | 106 | listWidget->blockSignals(false); |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | checkedItems.clear(); |
|
110 | 110 | } |
|
111 | 111 | checkedItems << item; |
|
112 | 112 | } |
|
113 | 113 | else { |
|
114 | 114 | checkedItems.remove(item); |
|
115 | 115 | } |
|
116 | 116 | }); |
|
117 | 117 | |
|
118 | 118 | QStringList result; |
|
119 | 119 | |
|
120 | 120 | d.setMinimumWidth(120); |
|
121 | 121 | d.resize(d.minimumSizeHint()); |
|
122 | 122 | d.move(location); |
|
123 | 123 | if (d.exec() == QDialog::Accepted) { |
|
124 | 124 | for (auto item : checkedItems) { |
|
125 | 125 | result += item->text(); |
|
126 | 126 | } |
|
127 | 127 | } |
|
128 | 128 | else { |
|
129 | 129 | result = selectedZones; |
|
130 | 130 | } |
|
131 | 131 | |
|
132 | 132 | return result; |
|
133 | 133 | } |
|
134 | 134 | |
|
135 | 135 | void updateForTimeMode(QTreeView *treeView) |
|
136 | 136 | { |
|
137 | 137 | auto selectedRows = treeView->selectionModel()->selectedRows(); |
|
138 | 138 | |
|
139 | 139 | if (selectedRows.count() == 1) { |
|
140 | 140 | auto event = m_Model->getEvent(selectedRows.first()); |
|
141 | 141 | if (event) { |
|
142 | 142 | if (m_VisualizationWidget) { |
|
143 | 143 | if (auto tab = m_VisualizationWidget->currentTabWidget()) { |
|
144 | 144 | |
|
145 | 145 | for (auto zoneName : m_ZonesForTimeMode) { |
|
146 | 146 | if (auto zone = tab->getZoneWithName(zoneName)) { |
|
147 | 147 | SqpRange eventRange; |
|
148 | 148 | eventRange.m_TStart = event->getTStart(); |
|
149 | 149 | eventRange.m_TEnd = event->getTEnd(); |
|
150 | 150 | zone->setZoneRange(eventRange); |
|
151 | 151 | } |
|
152 | 152 | } |
|
153 | 153 | } |
|
154 | 154 | else { |
|
155 | 155 | qCWarning(LOG_CatalogueEventsWidget()) |
|
156 | 156 | << "updateTimeZone: no tab found in the visualization"; |
|
157 | 157 | } |
|
158 | 158 | } |
|
159 | 159 | else { |
|
160 | 160 | qCWarning(LOG_CatalogueEventsWidget()) |
|
161 | 161 | << "updateTimeZone: visualization widget not found"; |
|
162 | 162 | } |
|
163 | 163 | } |
|
164 | 164 | } |
|
165 | 165 | else { |
|
166 | 166 | qCWarning(LOG_CatalogueEventsWidget()) |
|
167 | 167 | << "updateTimeZone: not compatible with multiple events selected"; |
|
168 | 168 | } |
|
169 | 169 | } |
|
170 | 170 | |
|
171 | 171 | void updateForGraphMode(QTreeView *treeView) |
|
172 | 172 | { |
|
173 | 173 | auto selectedRows = treeView->selectionModel()->selectedRows(); |
|
174 | 174 | |
|
175 | 175 | if (selectedRows.count() == 1) { |
|
176 | 176 | auto event = m_Model->getEvent(selectedRows.first()); |
|
177 | 177 | if (m_VisualizationWidget) { |
|
178 | 178 | if (auto tab = m_VisualizationWidget->currentTabWidget()) { |
|
179 | 179 | if (auto zone = tab->getZoneWithName(m_ZoneForGraphMode)) { |
|
180 | 180 | // TODO |
|
181 | 181 | } |
|
182 | 182 | } |
|
183 | 183 | else { |
|
184 | 184 | qCWarning(LOG_CatalogueEventsWidget()) |
|
185 | 185 | << "updateGraphMode: no tab found in the visualization"; |
|
186 | 186 | } |
|
187 | 187 | } |
|
188 | 188 | else { |
|
189 | 189 | qCWarning(LOG_CatalogueEventsWidget()) |
|
190 | 190 | << "updateGraphMode: visualization widget not found"; |
|
191 | 191 | } |
|
192 | 192 | } |
|
193 | 193 | else { |
|
194 | 194 | qCWarning(LOG_CatalogueEventsWidget()) |
|
195 | 195 | << "updateGraphMode: not compatible with multiple events selected"; |
|
196 | 196 | } |
|
197 | 197 | } |
|
198 | 198 | }; |
|
199 | 199 | |
|
200 | 200 | CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent) |
|
201 | 201 | : QWidget(parent), |
|
202 | 202 | ui(new Ui::CatalogueEventsWidget), |
|
203 | 203 | impl{spimpl::make_unique_impl<CatalogueEventsWidgetPrivate>()} |
|
204 | 204 | { |
|
205 | 205 | ui->setupUi(this); |
|
206 | 206 | |
|
207 | 207 | impl->m_Model = new CatalogueEventsModel{this}; |
|
208 | 208 | ui->treeView->setModel(impl->m_Model); |
|
209 | 209 | |
|
210 | 210 | ui->treeView->setSortingEnabled(true); |
|
211 | 211 | ui->treeView->setDragDropMode(QAbstractItemView::DragDrop); |
|
212 | 212 | ui->treeView->setDragEnabled(true); |
|
213 | 213 | |
|
214 | 214 | connect(ui->btnTime, &QToolButton::clicked, [this](auto checked) { |
|
215 | 215 | if (checked) { |
|
216 | 216 | ui->btnChart->setChecked(false); |
|
217 | 217 | impl->m_ZonesForTimeMode |
|
218 | 218 | = impl->selectZone(this, impl->m_ZonesForTimeMode, true, |
|
219 | 219 | this->mapToGlobal(ui->btnTime->frameGeometry().center())); |
|
220 | 220 | |
|
221 | 221 | impl->updateForTimeMode(ui->treeView); |
|
222 | 222 | } |
|
223 | 223 | }); |
|
224 | 224 | |
|
225 | 225 | connect(ui->btnChart, &QToolButton::clicked, [this](auto checked) { |
|
226 | 226 | if (checked) { |
|
227 | 227 | ui->btnTime->setChecked(false); |
|
228 | 228 | impl->m_ZoneForGraphMode |
|
229 | 229 | = impl->selectZone(this, {impl->m_ZoneForGraphMode}, false, |
|
230 | 230 | this->mapToGlobal(ui->btnChart->frameGeometry().center())) |
|
231 | 231 | .value(0); |
|
232 | 232 | |
|
233 | 233 | impl->updateForGraphMode(ui->treeView); |
|
234 | 234 | } |
|
235 | 235 | }); |
|
236 | 236 | |
|
237 | 237 | auto emitSelection = [this]() { |
|
238 | 238 | QVector<std::shared_ptr<DBEvent> > events; |
|
239 | 239 | QVector<QPair<std::shared_ptr<DBEvent>, std::shared_ptr<DBEventProduct> > > eventProducts; |
|
240 | 240 | |
|
241 | 241 | for (auto rowIndex : ui->treeView->selectionModel()->selectedRows()) { |
|
242 | 242 | |
|
243 | 243 | auto itemType = impl->m_Model->itemTypeOf(rowIndex); |
|
244 | 244 | if (itemType == CatalogueEventsModel::ItemType::Event) { |
|
245 | 245 | events << impl->m_Model->getEvent(rowIndex); |
|
246 | 246 | } |
|
247 | 247 | else if (itemType == CatalogueEventsModel::ItemType::EventProduct) { |
|
248 | 248 | eventProducts << qMakePair(impl->m_Model->getParentEvent(rowIndex), |
|
249 | 249 | impl->m_Model->getEventProduct(rowIndex)); |
|
250 | 250 | } |
|
251 | 251 | } |
|
252 | 252 | |
|
253 | 253 | if (!events.isEmpty() && eventProducts.isEmpty()) { |
|
254 | 254 | emit this->eventsSelected(events); |
|
255 | 255 | } |
|
256 | 256 | else if (events.isEmpty() && !eventProducts.isEmpty()) { |
|
257 | 257 | emit this->eventProductsSelected(eventProducts); |
|
258 | 258 | } |
|
259 | 259 | else { |
|
260 | 260 | emit this->selectionCleared(); |
|
261 | 261 | } |
|
262 | 262 | }; |
|
263 | 263 | |
|
264 | 264 | connect(ui->treeView, &QTreeView::clicked, emitSelection); |
|
265 | 265 | connect(ui->treeView->selectionModel(), &QItemSelectionModel::selectionChanged, emitSelection); |
|
266 | 266 | |
|
267 | 267 | connect(ui->treeView->selectionModel(), &QItemSelectionModel::selectionChanged, [this]() { |
|
268 | 268 | auto isNotMultiSelection = ui->treeView->selectionModel()->selectedRows().count() <= 1; |
|
269 | 269 | ui->btnChart->setEnabled(isNotMultiSelection); |
|
270 | 270 | ui->btnTime->setEnabled(isNotMultiSelection); |
|
271 | 271 | |
|
272 | 272 | if (isNotMultiSelection && ui->btnTime->isChecked()) { |
|
273 | 273 | impl->updateForTimeMode(ui->treeView); |
|
274 | 274 | } |
|
275 | 275 | else if (isNotMultiSelection && ui->btnChart->isChecked()) { |
|
276 | 276 | impl->updateForGraphMode(ui->treeView); |
|
277 | 277 | } |
|
278 | 278 | }); |
|
279 | 279 | |
|
280 | 280 | ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents); |
|
281 | 281 | ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::Name, |
|
282 | 282 | QHeaderView::Stretch); |
|
283 | 283 | ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::Validation, |
|
284 | 284 | QHeaderView::Fixed); |
|
285 | 285 | ui->treeView->header()->resizeSection((int)CatalogueEventsModel::Column::Validation, |
|
286 | 286 | VALIDATION_COLUMN_SIZE); |
|
287 | 287 | ui->treeView->header()->setSortIndicatorShown(true); |
|
288 | 288 | |
|
289 | 289 | connect(impl->m_Model, &CatalogueEventsModel::modelSorted, [this]() { |
|
290 | 290 | auto allEvents = impl->m_Model->events(); |
|
291 | 291 | for (auto event : allEvents) { |
|
292 | 292 | setEventChanges(event, impl->m_Model->eventsHasChanges(event)); |
|
293 | 293 | } |
|
294 | 294 | }); |
|
295 | 295 | } |
|
296 | 296 | |
|
297 | 297 | CatalogueEventsWidget::~CatalogueEventsWidget() |
|
298 | 298 | { |
|
299 | 299 | delete ui; |
|
300 | 300 | } |
|
301 | 301 | |
|
302 | 302 | void CatalogueEventsWidget::setVisualizationWidget(VisualizationWidget *visualization) |
|
303 | 303 | { |
|
304 | 304 | impl->m_VisualizationWidget = visualization; |
|
305 | 305 | } |
|
306 | 306 | |
|
307 | 307 | void CatalogueEventsWidget::setEventChanges(const std::shared_ptr<DBEvent> &event, bool hasChanges) |
|
308 | 308 | { |
|
309 | 309 | impl->m_Model->refreshEvent(event); |
|
310 | 310 | |
|
311 | 311 | auto eventIndex = impl->m_Model->indexOf(event); |
|
312 | 312 | auto validationIndex |
|
313 | 313 | = eventIndex.sibling(eventIndex.row(), (int)CatalogueEventsModel::Column::Validation); |
|
314 | 314 | |
|
315 | 315 | if (hasChanges) { |
|
316 | 316 | if (ui->treeView->indexWidget(validationIndex) == nullptr) { |
|
317 | 317 | auto widget = CatalogueExplorerHelper::buildValidationWidget( |
|
318 | ui->treeView, [this, event]() { setEventChanges(event, false); }, | |
|
318 | ui->treeView, | |
|
319 | [this, event]() { | |
|
320 | sqpApp->catalogueController().saveEvent(event); | |
|
321 | setEventChanges(event, false); | |
|
322 | }, | |
|
319 | 323 | [this, event]() { setEventChanges(event, false); }); |
|
320 | 324 | ui->treeView->setIndexWidget(validationIndex, widget); |
|
321 | 325 | } |
|
322 | 326 | } |
|
323 | 327 | else { |
|
324 | 328 | // Note: the widget is destroyed |
|
325 | 329 | ui->treeView->setIndexWidget(validationIndex, nullptr); |
|
326 | 330 | } |
|
327 | 331 | |
|
328 | 332 | impl->m_Model->setEventHasChanges(event, hasChanges); |
|
329 | 333 | } |
|
330 | 334 | |
|
331 | 335 | void CatalogueEventsWidget::populateWithCatalogues( |
|
332 | 336 | const QVector<std::shared_ptr<DBCatalogue> > &catalogues) |
|
333 | 337 | { |
|
334 | 338 | QSet<QUuid> eventIds; |
|
335 | 339 | QVector<std::shared_ptr<DBEvent> > events; |
|
336 | 340 | |
|
337 | 341 | for (auto catalogue : catalogues) { |
|
338 | 342 | auto catalogueEvents = sqpApp->catalogueController().retrieveEventsFromCatalogue(catalogue); |
|
339 | 343 | for (auto event : catalogueEvents) { |
|
340 | 344 | if (!eventIds.contains(event->getUniqId())) { |
|
341 | 345 | events << event; |
|
342 | 346 | eventIds.insert(event->getUniqId()); |
|
343 | 347 | } |
|
344 | 348 | } |
|
345 | 349 | } |
|
346 | 350 | |
|
347 | 351 | impl->setEvents(events, ui->treeView); |
|
348 | 352 | } |
|
349 | 353 | |
|
350 | 354 | void CatalogueEventsWidget::populateWithAllEvents() |
|
351 | 355 | { |
|
352 | 356 | auto allEvents = sqpApp->catalogueController().retrieveAllEvents(); |
|
353 | 357 | |
|
354 | 358 | QVector<std::shared_ptr<DBEvent> > events; |
|
355 | 359 | for (auto event : allEvents) { |
|
356 | 360 | events << event; |
|
357 | 361 | } |
|
358 | 362 | |
|
359 | 363 | impl->setEvents(events, ui->treeView); |
|
360 | 364 | } |
General Comments 3
Status change > Approved
You need to be logged in to leave comments.
Login now