##// END OF EJS Templates
Discard an added event remove it now.
perrinel -
r1321:fba51a5eef20
parent child
Show More
@@ -1,84 +1,84
1 1 #ifndef SCIQLOP_CATALOGUECONTROLLER_H
2 2 #define SCIQLOP_CATALOGUECONTROLLER_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 6 #include <Data/SqpRange.h>
7 7
8 8 #include <QLoggingCategory>
9 9 #include <QObject>
10 10 #include <QUuid>
11 11
12 12 #include <Common/spimpl.h>
13 13
14 14 #include <memory>
15 15
16 16 class DBCatalogue;
17 17 class DBEvent;
18 18 class DBEventProduct;
19 19
20 20 Q_DECLARE_LOGGING_CATEGORY(LOG_CatalogueController)
21 21
22 22 class DataSourceItem;
23 23 class Variable;
24 24
25 25 /**
26 26 * @brief The CatalogueController class aims to handle catalogues and event using the CatalogueAPI
27 27 * library.
28 28 */
29 29 class SCIQLOP_CORE_EXPORT CatalogueController : public QObject {
30 30 Q_OBJECT
31 31 public:
32 32 explicit CatalogueController(QObject *parent = 0);
33 33 virtual ~CatalogueController();
34 34
35 35 // DB
36 36 QStringList getRepositories() const;
37 37 void addDB(const QString &dbPath);
38 38 void saveDB(const QString &destinationPath, const QString &repository);
39 39
40 40 // Event
41 41 /// retrieveEvents with empty repository retrieve them from the default repository
42 42 std::list<std::shared_ptr<DBEvent> > retrieveEvents(const QString &repository) const;
43 43 std::list<std::shared_ptr<DBEvent> > retrieveAllEvents() const;
44 44
45 45 void addEvent(std::shared_ptr<DBEvent> event);
46 46 void updateEvent(std::shared_ptr<DBEvent> event);
47 47 void updateEventProduct(std::shared_ptr<DBEventProduct> eventProduct);
48 48 void removeEvent(std::shared_ptr<DBEvent> event);
49 49 // void trashEvent(std::shared_ptr<DBEvent> event);
50 50 // void restore(std::shared_ptr<DBEvent> event);
51 51 void saveEvent(std::shared_ptr<DBEvent> event);
52 void discardEvent(std::shared_ptr<DBEvent> event);
52 void discardEvent(std::shared_ptr<DBEvent> event, bool &removed);
53 53 bool eventHasChanges(std::shared_ptr<DBEvent> event) const;
54 54
55 55 // Catalogue
56 56 std::list<std::shared_ptr<DBEvent> >
57 57 retrieveEventsFromCatalogue(std::shared_ptr<DBCatalogue> catalogue) const;
58 58 // bool createCatalogue(const QString &name, QVector<QUuid> eventList);
59 59 /// retrieveEvents with empty repository retrieve them from the default repository
60 60 std::list<std::shared_ptr<DBCatalogue> > retrieveCatalogues(const QString &repository
61 61 = QString()) const;
62 62 void updateCatalogue(std::shared_ptr<DBCatalogue> catalogue);
63 63 void removeCatalogue(std::shared_ptr<DBCatalogue> catalogue);
64 64 void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue);
65 65
66 66 void saveAll();
67 67 bool hasChanges() const;
68 68
69 69 /// Returns the MIME data associated to a list of variables
70 70 QByteArray mimeDataForEvents(const QVector<std::shared_ptr<DBEvent> > &events) const;
71 71
72 72 /// Returns the list of variables contained in a MIME data
73 73 QVector<std::shared_ptr<DBEvent> > eventsForMimeData(const QByteArray &mimeData) const;
74 74
75 75 public slots:
76 76 /// Manage init/end of the controller
77 77 void initialize();
78 78
79 79 private:
80 80 class CatalogueControllerPrivate;
81 81 spimpl::unique_impl_ptr<CatalogueControllerPrivate> impl;
82 82 };
83 83
84 84 #endif // SCIQLOP_CATALOGUECONTROLLER_H
@@ -1,437 +1,466
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 <QDataStream>
16 16 #include <QMutex>
17 17 #include <QThread>
18 18
19 19 #include <QDir>
20 20 #include <QStandardPaths>
21 21
22 22 Q_LOGGING_CATEGORY(LOG_CatalogueController, "CatalogueController")
23 23
24 24 namespace {
25 25
26 26 static QString REPOSITORY_WORK_SUFFIX = QString{"_work"};
27 27 static QString REPOSITORY_TRASH_SUFFIX = QString{"_trash"};
28 28 }
29 29
30 30 class CatalogueController::CatalogueControllerPrivate {
31 31
32 32 public:
33 33 explicit CatalogueControllerPrivate(CatalogueController *parent) : m_Q{parent} {}
34 34
35 35 CatalogueDao m_CatalogueDao;
36 36
37 37 QStringList m_RepositoryList;
38 38 CatalogueController *m_Q;
39 39
40 40 QSet<QString> m_EventKeysWithChanges;
41 41
42 42 QString eventUniqueKey(const std::shared_ptr<DBEvent> &event) const;
43 43
44 44 void copyDBtoDB(const QString &dbFrom, const QString &dbTo);
45 45 QString toWorkRepository(QString repository);
46 46 QString toSyncRepository(QString repository);
47 47 void savAllDB();
48 48
49 49 void saveEvent(std::shared_ptr<DBEvent> event, bool persist = true);
50 50 void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue, bool persist = true);
51 51 };
52 52
53 53 CatalogueController::CatalogueController(QObject *parent)
54 54 : impl{spimpl::make_unique_impl<CatalogueControllerPrivate>(this)}
55 55 {
56 56 qCDebug(LOG_CatalogueController()) << tr("CatalogueController construction")
57 57 << QThread::currentThread();
58 58 }
59 59
60 60 CatalogueController::~CatalogueController()
61 61 {
62 62 qCDebug(LOG_CatalogueController()) << tr("CatalogueController destruction")
63 63 << QThread::currentThread();
64 64 }
65 65
66 66 QStringList CatalogueController::getRepositories() const
67 67 {
68 68 return impl->m_RepositoryList;
69 69 }
70 70
71 71 void CatalogueController::addDB(const QString &dbPath)
72 72 {
73 73 QDir dbDir(dbPath);
74 74 if (dbDir.exists()) {
75 75 auto dirName = dbDir.dirName();
76 76
77 77 if (std::find(impl->m_RepositoryList.cbegin(), impl->m_RepositoryList.cend(), dirName)
78 78 != impl->m_RepositoryList.cend()) {
79 79 qCCritical(LOG_CatalogueController())
80 80 << tr("Impossible to addDB that is already loaded");
81 81 }
82 82
83 83 if (!impl->m_CatalogueDao.addDB(dbPath, dirName)) {
84 84 qCCritical(LOG_CatalogueController())
85 85 << tr("Impossible to addDB %1 from %2 ").arg(dirName, dbPath);
86 86 }
87 87 else {
88 88 impl->m_RepositoryList << dirName;
89 89 impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName));
90 90 }
91 91 }
92 92 else {
93 93 qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB that not exists: ")
94 94 << dbPath;
95 95 }
96 96 }
97 97
98 98 void CatalogueController::saveDB(const QString &destinationPath, const QString &repository)
99 99 {
100 100 if (!impl->m_CatalogueDao.saveDB(destinationPath, repository)) {
101 101 qCCritical(LOG_CatalogueController())
102 102 << tr("Impossible to saveDB %1 from %2 ").arg(repository, destinationPath);
103 103 }
104 104 }
105 105
106 106 std::list<std::shared_ptr<DBEvent> >
107 107 CatalogueController::retrieveEvents(const QString &repository) const
108 108 {
109 109 QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository;
110 110
111 111 auto eventsShared = std::list<std::shared_ptr<DBEvent> >{};
112 112 auto events = impl->m_CatalogueDao.getEvents(impl->toWorkRepository(dbDireName));
113 113 for (auto event : events) {
114 114 eventsShared.push_back(std::make_shared<DBEvent>(event));
115 115 }
116 116 return eventsShared;
117 117 }
118 118
119 119 std::list<std::shared_ptr<DBEvent> > CatalogueController::retrieveAllEvents() const
120 120 {
121 121 auto eventsShared = std::list<std::shared_ptr<DBEvent> >{};
122 122 for (auto repository : impl->m_RepositoryList) {
123 123 eventsShared.splice(eventsShared.end(), retrieveEvents(repository));
124 124 }
125 125
126 126 return eventsShared;
127 127 }
128 128
129 129 std::list<std::shared_ptr<DBEvent> >
130 130 CatalogueController::retrieveEventsFromCatalogue(std::shared_ptr<DBCatalogue> catalogue) const
131 131 {
132 132 auto eventsShared = std::list<std::shared_ptr<DBEvent> >{};
133 133 auto events = impl->m_CatalogueDao.getCatalogueEvents(*catalogue);
134 134 for (auto event : events) {
135 135 eventsShared.push_back(std::make_shared<DBEvent>(event));
136 136 }
137 137 return eventsShared;
138 138 }
139 139
140 140 void CatalogueController::updateEvent(std::shared_ptr<DBEvent> event)
141 141 {
142 142 event->setRepository(impl->toWorkRepository(event->getRepository()));
143 143
144 144 auto uniqueId = impl->eventUniqueKey(event);
145 145 impl->m_EventKeysWithChanges.insert(uniqueId);
146 146
147 147 impl->m_CatalogueDao.updateEvent(*event);
148 148 }
149 149
150 150 void CatalogueController::updateEventProduct(std::shared_ptr<DBEventProduct> eventProduct)
151 151 {
152 152 impl->m_CatalogueDao.updateEventProduct(*eventProduct);
153 153 }
154 154
155 155 void CatalogueController::removeEvent(std::shared_ptr<DBEvent> event)
156 156 {
157 157 // Remove it from both repository and repository_work
158 158 event->setRepository(impl->toWorkRepository(event->getRepository()));
159 159 impl->m_CatalogueDao.removeEvent(*event);
160 160 event->setRepository(impl->toSyncRepository(event->getRepository()));
161 161 impl->m_CatalogueDao.removeEvent(*event);
162 162 impl->savAllDB();
163 163 }
164 164
165 165 void CatalogueController::addEvent(std::shared_ptr<DBEvent> event)
166 166 {
167 167 event->setRepository(impl->toWorkRepository(event->getRepository()));
168 168
169 169 auto eventTemp = *event;
170 170 impl->m_CatalogueDao.addEvent(eventTemp);
171 171
172 172 // Call update is necessary at the creation of add Event if it has some tags or some event
173 173 // products
174 174 if (!event->getEventProducts().empty() || !event->getTags().empty()) {
175 175
176 176 auto eventProductsTemp = eventTemp.getEventProducts();
177 177 auto eventProductTempUpdated = std::list<DBEventProduct>{};
178 178 for (auto eventProductTemp : eventProductsTemp) {
179 179 eventProductTemp.setEvent(eventTemp);
180 180 eventProductTempUpdated.push_back(eventProductTemp);
181 181 }
182 182 eventTemp.setEventProducts(eventProductTempUpdated);
183 183
184 184 impl->m_CatalogueDao.updateEvent(eventTemp);
185 185 }
186 186
187 <<<<<<< Upstream, based on feature/CatalogueDevelop2
187 188 auto uniqueId = impl->eventUniqueKey(event);
188 189 impl->m_EventKeysWithChanges.insert(uniqueId);
190 =======
191
192 // update event parameter
193 auto uniqIdPredicate = std::make_shared<ComparaisonPredicate>(
194 QString{"uniqId"}, event->getUniqId(), ComparaisonOperation::EQUALEQUAL);
195
196 auto workRepositoryPredicate = std::make_shared<ComparaisonPredicate>(
197 QString{"repository"}, impl->toWorkRepository(event->getRepository()),
198 ComparaisonOperation::EQUALEQUAL);
199
200 auto workPred = std::make_shared<CompoundPredicate>(CompoundOperation::AND);
201 workPred->AddRequestPredicate(uniqIdPredicate);
202 workPred->AddRequestPredicate(workRepositoryPredicate);
203
204
205 auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
206 *event = workEvent;
207 >>>>>>> 575bb1a Discard an added event remove it now.
189 208 }
190 209
191 210 void CatalogueController::saveEvent(std::shared_ptr<DBEvent> event)
192 211 {
193 212 impl->saveEvent(event, true);
194 213 impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event));
195 214 }
196 215
197 void CatalogueController::discardEvent(std::shared_ptr<DBEvent> event)
216 void CatalogueController::discardEvent(std::shared_ptr<DBEvent> event, bool &removed)
198 217 {
199 218 auto uniqIdPredicate = std::make_shared<ComparaisonPredicate>(
200 219 QString{"uniqId"}, event->getUniqId(), ComparaisonOperation::EQUALEQUAL);
201 220
202 221 auto syncRepositoryPredicate = std::make_shared<ComparaisonPredicate>(
203 222 QString{"repository"}, impl->toSyncRepository(event->getRepository()),
204 223 ComparaisonOperation::EQUALEQUAL);
205 224
206 225 auto syncPred = std::make_shared<CompoundPredicate>(CompoundOperation::AND);
207 226 syncPred->AddRequestPredicate(uniqIdPredicate);
208 227 syncPred->AddRequestPredicate(syncRepositoryPredicate);
209 228
210 229
211 230 auto workRepositoryPredicate = std::make_shared<ComparaisonPredicate>(
212 231 QString{"repository"}, impl->toWorkRepository(event->getRepository()),
213 232 ComparaisonOperation::EQUALEQUAL);
214 233
215 234 auto workPred = std::make_shared<CompoundPredicate>(CompoundOperation::AND);
216 235 workPred->AddRequestPredicate(uniqIdPredicate);
217 236 workPred->AddRequestPredicate(workRepositoryPredicate);
218 237
219 238
220 239 auto syncEvent = impl->m_CatalogueDao.getEvent(syncPred);
221 impl->m_CatalogueDao.copyEvent(syncEvent, impl->toWorkRepository(event->getRepository()), true);
222
223 auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
224 *event = workEvent;
225 impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event));
240 if (!syncEvent.getUniqId().isNull()) {
241 removed = false;
242 impl->m_CatalogueDao.copyEvent(syncEvent, impl->toWorkRepository(event->getRepository()),
243 true);
244
245 auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
246 *event = workEvent;
247 impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event));
248 }
249 else {
250 removed = true;
251 // Since the element wasn't in sync repository. Discard it means remove it
252 event->setRepository(impl->toWorkRepository(event->getRepository()));
253 impl->m_CatalogueDao.removeEvent(*event);
254 }
226 255 }
227 256
228 257 bool CatalogueController::eventHasChanges(std::shared_ptr<DBEvent> event) const
229 258 {
230 259 return impl->m_EventKeysWithChanges.contains(impl->eventUniqueKey(event));
231 260 }
232 261
233 262 std::list<std::shared_ptr<DBCatalogue> >
234 263 CatalogueController::retrieveCatalogues(const QString &repository) const
235 264 {
236 265 QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository;
237 266
238 267 auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{};
239 268 auto catalogues = impl->m_CatalogueDao.getCatalogues(impl->toWorkRepository(dbDireName));
240 269 for (auto catalogue : catalogues) {
241 270 cataloguesShared.push_back(std::make_shared<DBCatalogue>(catalogue));
242 271 }
243 272 return cataloguesShared;
244 273 }
245 274
246 275 void CatalogueController::updateCatalogue(std::shared_ptr<DBCatalogue> catalogue)
247 276 {
248 277 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
249 278
250 279 impl->m_CatalogueDao.updateCatalogue(*catalogue);
251 280 }
252 281
253 282 void CatalogueController::removeCatalogue(std::shared_ptr<DBCatalogue> catalogue)
254 283 {
255 284 // Remove it from both repository and repository_work
256 285 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
257 286 impl->m_CatalogueDao.removeCatalogue(*catalogue);
258 287 catalogue->setRepository(impl->toSyncRepository(catalogue->getRepository()));
259 288 impl->m_CatalogueDao.removeCatalogue(*catalogue);
260 289 }
261 290
262 291 void CatalogueController::saveCatalogue(std::shared_ptr<DBCatalogue> catalogue)
263 292 {
264 293 impl->saveCatalogue(catalogue, true);
265 294 }
266 295
267 296 void CatalogueController::saveAll()
268 297 {
269 298 for (auto repository : impl->m_RepositoryList) {
270 299 // Save Event
271 300 auto events = this->retrieveEvents(repository);
272 301 for (auto event : events) {
273 302 impl->saveEvent(event, false);
274 303 }
275 304
276 305 // Save Catalogue
277 306 auto catalogues = this->retrieveCatalogues(repository);
278 307 for (auto catalogue : catalogues) {
279 308 impl->saveCatalogue(catalogue, false);
280 309 }
281 310 }
282 311
283 312 impl->savAllDB();
284 313 impl->m_EventKeysWithChanges.clear();
285 314 }
286 315
287 316 bool CatalogueController::hasChanges() const
288 317 {
289 318 return !impl->m_EventKeysWithChanges.isEmpty(); // TODO: catalogues
290 319 }
291 320
292 321 QByteArray
293 322 CatalogueController::mimeDataForEvents(const QVector<std::shared_ptr<DBEvent> > &events) const
294 323 {
295 324 auto encodedData = QByteArray{};
296 325
297 326 QMap<QString, QVariantList> idsPerRepository;
298 327 for (auto event : events) {
299 328 idsPerRepository[event->getRepository()] << event->getUniqId();
300 329 }
301 330
302 331 QDataStream stream{&encodedData, QIODevice::WriteOnly};
303 332 stream << idsPerRepository;
304 333
305 334 return encodedData;
306 335 }
307 336
308 337 QVector<std::shared_ptr<DBEvent> >
309 338 CatalogueController::eventsForMimeData(const QByteArray &mimeData) const
310 339 {
311 340 auto events = QVector<std::shared_ptr<DBEvent> >{};
312 341 QDataStream stream{mimeData};
313 342
314 343 QMap<QString, QVariantList> idsPerRepository;
315 344 stream >> idsPerRepository;
316 345
317 346 for (auto it = idsPerRepository.cbegin(); it != idsPerRepository.cend(); ++it) {
318 347 auto repository = it.key();
319 348 auto allRepositoryEvent = retrieveEvents(repository);
320 349 for (auto uuid : it.value()) {
321 350 for (auto repositoryEvent : allRepositoryEvent) {
322 351 if (uuid.toUuid() == repositoryEvent->getUniqId()) {
323 352 events << repositoryEvent;
324 353 }
325 354 }
326 355 }
327 356 }
328 357
329 358 return events;
330 359 }
331 360
332 361 void CatalogueController::initialize()
333 362 {
334 363 qCDebug(LOG_CatalogueController()) << tr("CatalogueController init")
335 364 << QThread::currentThread();
336 365
337 366 impl->m_CatalogueDao.initialize();
338 367 auto defaultRepositoryLocation
339 368 = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
340 369
341 370 QDir defaultRepositoryLocationDir;
342 371 if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation)) {
343 372 defaultRepositoryLocationDir.cd(defaultRepositoryLocation);
344 373 auto defaultRepository = defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT);
345 374
346 375 qCInfo(LOG_CatalogueController()) << tr("Persistant data loading from: ")
347 376 << defaultRepository;
348 377
349 378 QDir dbDir(defaultRepository);
350 379 impl->m_RepositoryList << REPOSITORY_DEFAULT;
351 380 if (dbDir.exists()) {
352 381 auto dirName = dbDir.dirName();
353 382
354 383 if (impl->m_CatalogueDao.addDB(defaultRepository, dirName)) {
355 384 impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName));
356 385 }
357 386 }
358 387 else {
359 388 qCInfo(LOG_CatalogueController()) << tr("Initialisation of Default repository detected")
360 389 << defaultRepository;
361 390 }
362 391 }
363 392 else {
364 393 qCWarning(LOG_CatalogueController())
365 394 << tr("Cannot load the persistent default repository from ")
366 395 << defaultRepositoryLocation;
367 396 }
368 397
369 398 qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END");
370 399 }
371 400
372 401 QString CatalogueController::CatalogueControllerPrivate::eventUniqueKey(
373 402 const std::shared_ptr<DBEvent> &event) const
374 403 {
375 404 return event->getUniqId().toString().append(event->getRepository());
376 405 }
377 406
378 407 void CatalogueController::CatalogueControllerPrivate::copyDBtoDB(const QString &dbFrom,
379 408 const QString &dbTo)
380 409 {
381 410 // auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{};
382 411 auto catalogues = m_CatalogueDao.getCatalogues(dbFrom);
383 412 auto events = m_CatalogueDao.getEvents(dbFrom);
384 413 for (auto catalogue : catalogues) {
385 414 m_CatalogueDao.copyCatalogue(catalogue, dbTo, true);
386 415 }
387 416
388 417 for (auto event : events) {
389 418 m_CatalogueDao.copyEvent(event, dbTo, true);
390 419 }
391 420 }
392 421
393 422 QString CatalogueController::CatalogueControllerPrivate::toWorkRepository(QString repository)
394 423 {
395 424 auto syncRepository = toSyncRepository(repository);
396 425
397 426 return QString("%1%2").arg(syncRepository, REPOSITORY_WORK_SUFFIX);
398 427 }
399 428
400 429 QString CatalogueController::CatalogueControllerPrivate::toSyncRepository(QString repository)
401 430 {
402 431 auto syncRepository = repository;
403 432 if (repository.endsWith(REPOSITORY_WORK_SUFFIX)) {
404 433 syncRepository.remove(REPOSITORY_WORK_SUFFIX);
405 434 }
406 435 else if (repository.endsWith(REPOSITORY_TRASH_SUFFIX)) {
407 436 syncRepository.remove(REPOSITORY_TRASH_SUFFIX);
408 437 }
409 438 return syncRepository;
410 439 }
411 440
412 441 void CatalogueController::CatalogueControllerPrivate::savAllDB()
413 442 {
414 443 for (auto repository : m_RepositoryList) {
415 444 auto defaultRepositoryLocation
416 445 = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
417 446 m_CatalogueDao.saveDB(defaultRepositoryLocation, repository);
418 447 }
419 448 }
420 449
421 450 void CatalogueController::CatalogueControllerPrivate::saveEvent(std::shared_ptr<DBEvent> event,
422 451 bool persist)
423 452 {
424 453 m_CatalogueDao.copyEvent(*event, toSyncRepository(event->getRepository()), true);
425 454 if (persist) {
426 455 savAllDB();
427 456 }
428 457 }
429 458
430 459 void CatalogueController::CatalogueControllerPrivate::saveCatalogue(
431 460 std::shared_ptr<DBCatalogue> catalogue, bool persist)
432 461 {
433 462 m_CatalogueDao.copyCatalogue(*catalogue, toSyncRepository(catalogue->getRepository()), true);
434 463 if (persist) {
435 464 savAllDB();
436 465 }
437 466 }
@@ -1,464 +1,470
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 #include <QMessageBox>
18 18
19 19 Q_LOGGING_CATEGORY(LOG_CatalogueEventsWidget, "CatalogueEventsWidget")
20 20
21 21 /// Fixed size of the validation column
22 22 const auto VALIDATION_COLUMN_SIZE = 35;
23 23
24 24 struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate {
25 25
26 26 CatalogueEventsModel *m_Model = nullptr;
27 27 QStringList m_ZonesForTimeMode;
28 28 QString m_ZoneForGraphMode;
29 29 QVector<std::shared_ptr<DBCatalogue> > m_DisplayedCatalogues;
30 30 bool m_AllEventDisplayed = false;
31 31
32 32 VisualizationWidget *m_VisualizationWidget = nullptr;
33 33
34 34 void setEvents(const QVector<std::shared_ptr<DBEvent> > &events, CatalogueEventsWidget *widget)
35 35 {
36 36 widget->ui->treeView->setSortingEnabled(false);
37 37 m_Model->setEvents(events);
38 38 widget->ui->treeView->setSortingEnabled(true);
39 39
40 40 for (auto event : events) {
41 41 if (sqpApp->catalogueController().eventHasChanges(event)) {
42 42 auto index = m_Model->indexOf(event);
43 43 widget->setEventChanges(event, true);
44 44 }
45 45 }
46 46 }
47 47
48 48 void addEvent(const std::shared_ptr<DBEvent> &event, QTreeView *treeView)
49 49 {
50 50 treeView->setSortingEnabled(false);
51 51 m_Model->addEvent(event);
52 52 treeView->setSortingEnabled(true);
53 53 }
54 54
55 55 void removeEvent(const std::shared_ptr<DBEvent> &event, QTreeView *treeView)
56 56 {
57 57 treeView->setSortingEnabled(false);
58 58 m_Model->removeEvent(event);
59 59 treeView->setSortingEnabled(true);
60 60 }
61 61
62 62 QStringList getAvailableVisualizationZoneList() const
63 63 {
64 64 if (m_VisualizationWidget) {
65 65 if (auto tab = m_VisualizationWidget->currentTabWidget()) {
66 66 return tab->availableZoneWidgets();
67 67 }
68 68 }
69 69
70 70 return QStringList{};
71 71 }
72 72
73 73 QStringList selectZone(QWidget *parent, const QStringList &selectedZones,
74 74 bool allowMultiSelection, const QPoint &location)
75 75 {
76 76 auto availableZones = getAvailableVisualizationZoneList();
77 77 if (availableZones.isEmpty()) {
78 78 return QStringList{};
79 79 }
80 80
81 81 QDialog d(parent, Qt::Tool);
82 82 d.setWindowTitle("Choose a zone");
83 83 auto layout = new QVBoxLayout{&d};
84 84 layout->setContentsMargins(0, 0, 0, 0);
85 85 auto listWidget = new QListWidget{&d};
86 86 layout->addWidget(listWidget);
87 87
88 88 QSet<QListWidgetItem *> checkedItems;
89 89 for (auto zone : availableZones) {
90 90 auto item = new QListWidgetItem{zone};
91 91 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
92 92 if (selectedZones.contains(zone)) {
93 93 item->setCheckState(Qt::Checked);
94 94 checkedItems << item;
95 95 }
96 96 else {
97 97 item->setCheckState(Qt::Unchecked);
98 98 }
99 99
100 100 listWidget->addItem(item);
101 101 }
102 102
103 103 auto buttonBox = new QDialogButtonBox{QDialogButtonBox::Ok, &d};
104 104 layout->addWidget(buttonBox);
105 105
106 106 QObject::connect(buttonBox, &QDialogButtonBox::accepted, &d, &QDialog::accept);
107 107 QObject::connect(buttonBox, &QDialogButtonBox::rejected, &d, &QDialog::reject);
108 108
109 109 QObject::connect(listWidget, &QListWidget::itemChanged,
110 110 [&checkedItems, allowMultiSelection, listWidget](auto item) {
111 111 if (item->checkState() == Qt::Checked) {
112 112 if (!allowMultiSelection) {
113 113 for (auto checkedItem : checkedItems) {
114 114 listWidget->blockSignals(true);
115 115 checkedItem->setCheckState(Qt::Unchecked);
116 116 listWidget->blockSignals(false);
117 117 }
118 118
119 119 checkedItems.clear();
120 120 }
121 121 checkedItems << item;
122 122 }
123 123 else {
124 124 checkedItems.remove(item);
125 125 }
126 126 });
127 127
128 128 QStringList result;
129 129
130 130 d.setMinimumWidth(120);
131 131 d.resize(d.minimumSizeHint());
132 132 d.move(location);
133 133 if (d.exec() == QDialog::Accepted) {
134 134 for (auto item : checkedItems) {
135 135 result += item->text();
136 136 }
137 137 }
138 138 else {
139 139 result = selectedZones;
140 140 }
141 141
142 142 return result;
143 143 }
144 144
145 145 void updateForTimeMode(QTreeView *treeView)
146 146 {
147 147 auto selectedRows = treeView->selectionModel()->selectedRows();
148 148
149 149 if (selectedRows.count() == 1) {
150 150 auto event = m_Model->getEvent(selectedRows.first());
151 151 if (event) {
152 152 if (m_VisualizationWidget) {
153 153 if (auto tab = m_VisualizationWidget->currentTabWidget()) {
154 154
155 155 for (auto zoneName : m_ZonesForTimeMode) {
156 156 if (auto zone = tab->getZoneWithName(zoneName)) {
157 157 SqpRange eventRange;
158 158 eventRange.m_TStart = event->getTStart();
159 159 eventRange.m_TEnd = event->getTEnd();
160 160 zone->setZoneRange(eventRange);
161 161 }
162 162 }
163 163 }
164 164 else {
165 165 qCWarning(LOG_CatalogueEventsWidget())
166 166 << "updateTimeZone: no tab found in the visualization";
167 167 }
168 168 }
169 169 else {
170 170 qCWarning(LOG_CatalogueEventsWidget())
171 171 << "updateTimeZone: visualization widget not found";
172 172 }
173 173 }
174 174 }
175 175 else {
176 176 qCWarning(LOG_CatalogueEventsWidget())
177 177 << "updateTimeZone: not compatible with multiple events selected";
178 178 }
179 179 }
180 180
181 181 void updateForGraphMode(QTreeView *treeView)
182 182 {
183 183 auto selectedRows = treeView->selectionModel()->selectedRows();
184 184
185 185 if (selectedRows.count() == 1) {
186 186 auto event = m_Model->getEvent(selectedRows.first());
187 187 if (m_VisualizationWidget) {
188 188 if (auto tab = m_VisualizationWidget->currentTabWidget()) {
189 189 if (auto zone = tab->getZoneWithName(m_ZoneForGraphMode)) {
190 190 // TODO
191 191 }
192 192 }
193 193 else {
194 194 qCWarning(LOG_CatalogueEventsWidget())
195 195 << "updateGraphMode: no tab found in the visualization";
196 196 }
197 197 }
198 198 else {
199 199 qCWarning(LOG_CatalogueEventsWidget())
200 200 << "updateGraphMode: visualization widget not found";
201 201 }
202 202 }
203 203 else {
204 204 qCWarning(LOG_CatalogueEventsWidget())
205 205 << "updateGraphMode: not compatible with multiple events selected";
206 206 }
207 207 }
208 208
209 209 void getSelectedItems(
210 210 QTreeView *treeView, QVector<std::shared_ptr<DBEvent> > &events,
211 211 QVector<QPair<std::shared_ptr<DBEvent>, std::shared_ptr<DBEventProduct> > > &eventProducts)
212 212 {
213 213 for (auto rowIndex : treeView->selectionModel()->selectedRows()) {
214 214 auto itemType = m_Model->itemTypeOf(rowIndex);
215 215 if (itemType == CatalogueEventsModel::ItemType::Event) {
216 216 events << m_Model->getEvent(rowIndex);
217 217 }
218 218 else if (itemType == CatalogueEventsModel::ItemType::EventProduct) {
219 219 eventProducts << qMakePair(m_Model->getParentEvent(rowIndex),
220 220 m_Model->getEventProduct(rowIndex));
221 221 }
222 222 }
223 223 }
224 224 };
225 225
226 226 CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent)
227 227 : QWidget(parent),
228 228 ui(new Ui::CatalogueEventsWidget),
229 229 impl{spimpl::make_unique_impl<CatalogueEventsWidgetPrivate>()}
230 230 {
231 231 ui->setupUi(this);
232 232
233 233 impl->m_Model = new CatalogueEventsModel{this};
234 234 ui->treeView->setModel(impl->m_Model);
235 235
236 236 ui->treeView->setSortingEnabled(true);
237 237 ui->treeView->setDragDropMode(QAbstractItemView::DragDrop);
238 238 ui->treeView->setDragEnabled(true);
239 239
240 240 connect(ui->btnTime, &QToolButton::clicked, [this](auto checked) {
241 241 if (checked) {
242 242 ui->btnChart->setChecked(false);
243 243 impl->m_ZonesForTimeMode
244 244 = impl->selectZone(this, impl->m_ZonesForTimeMode, true,
245 245 this->mapToGlobal(ui->btnTime->frameGeometry().center()));
246 246
247 247 impl->updateForTimeMode(ui->treeView);
248 248 }
249 249 });
250 250
251 251 connect(ui->btnChart, &QToolButton::clicked, [this](auto checked) {
252 252 if (checked) {
253 253 ui->btnTime->setChecked(false);
254 254 impl->m_ZoneForGraphMode
255 255 = impl->selectZone(this, {impl->m_ZoneForGraphMode}, false,
256 256 this->mapToGlobal(ui->btnChart->frameGeometry().center()))
257 257 .value(0);
258 258
259 259 impl->updateForGraphMode(ui->treeView);
260 260 }
261 261 });
262 262
263 263 connect(ui->btnRemove, &QToolButton::clicked, [this]() {
264 264 QVector<std::shared_ptr<DBEvent> > events;
265 265 QVector<QPair<std::shared_ptr<DBEvent>, std::shared_ptr<DBEventProduct> > > eventProducts;
266 266 impl->getSelectedItems(ui->treeView, events, eventProducts);
267 267
268 268 if (!events.isEmpty() && eventProducts.isEmpty()) {
269 269
270 270 if (QMessageBox::warning(this, tr("Remove Event(s)"),
271 271 tr("The selected event(s) will be completly removed "
272 272 "from the repository!\nAre you sure you want to continue?"),
273 273 QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
274 274 == QMessageBox::Yes) {
275 275
276 276 for (auto event : events) {
277 277 sqpApp->catalogueController().removeEvent(event);
278 278 impl->removeEvent(event, ui->treeView);
279 279 }
280 280 }
281 281 }
282 282 });
283 283
284 284 connect(ui->treeView, &QTreeView::clicked, this, &CatalogueEventsWidget::emitSelection);
285 285 connect(ui->treeView->selectionModel(), &QItemSelectionModel::selectionChanged, this,
286 286 &CatalogueEventsWidget::emitSelection);
287 287
288 288 ui->btnRemove->setEnabled(false); // Disabled by default when nothing is selected
289 289 connect(ui->treeView->selectionModel(), &QItemSelectionModel::selectionChanged, [this]() {
290 290 auto isNotMultiSelection = ui->treeView->selectionModel()->selectedRows().count() <= 1;
291 291 ui->btnChart->setEnabled(isNotMultiSelection);
292 292 ui->btnTime->setEnabled(isNotMultiSelection);
293 293
294 294 if (isNotMultiSelection && ui->btnTime->isChecked()) {
295 295 impl->updateForTimeMode(ui->treeView);
296 296 }
297 297 else if (isNotMultiSelection && ui->btnChart->isChecked()) {
298 298 impl->updateForGraphMode(ui->treeView);
299 299 }
300 300
301 301 QVector<std::shared_ptr<DBEvent> > events;
302 302 QVector<QPair<std::shared_ptr<DBEvent>, std::shared_ptr<DBEventProduct> > > eventProducts;
303 303 impl->getSelectedItems(ui->treeView, events, eventProducts);
304 304 ui->btnRemove->setEnabled(!events.isEmpty() && eventProducts.isEmpty());
305 305 });
306 306
307 307 ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
308 308 ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::Tags,
309 309 QHeaderView::Stretch);
310 310 ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::Validation,
311 311 QHeaderView::Fixed);
312 312 ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::Name,
313 313 QHeaderView::Interactive);
314 314 ui->treeView->header()->resizeSection((int)CatalogueEventsModel::Column::Validation,
315 315 VALIDATION_COLUMN_SIZE);
316 316 ui->treeView->header()->setSortIndicatorShown(true);
317 317
318 318 connect(impl->m_Model, &CatalogueEventsModel::modelSorted, [this]() {
319 319 auto allEvents = impl->m_Model->events();
320 320 for (auto event : allEvents) {
321 321 setEventChanges(event, sqpApp->catalogueController().eventHasChanges(event));
322 322 }
323 323 });
324 324
325 325 populateWithAllEvents();
326 326 }
327 327
328 328 CatalogueEventsWidget::~CatalogueEventsWidget()
329 329 {
330 330 delete ui;
331 331 }
332 332
333 333 void CatalogueEventsWidget::setVisualizationWidget(VisualizationWidget *visualization)
334 334 {
335 335 impl->m_VisualizationWidget = visualization;
336 336 }
337 337
338 338 void CatalogueEventsWidget::addEvent(const std::shared_ptr<DBEvent> &event)
339 339 {
340 340 impl->addEvent(event, ui->treeView);
341 341 }
342 342
343 343 void CatalogueEventsWidget::setEventChanges(const std::shared_ptr<DBEvent> &event, bool hasChanges)
344 344 {
345 345 impl->m_Model->refreshEvent(event);
346 346
347 347 auto eventIndex = impl->m_Model->indexOf(event);
348 348 auto validationIndex
349 349 = eventIndex.sibling(eventIndex.row(), (int)CatalogueEventsModel::Column::Validation);
350 350
351 351 if (validationIndex.isValid()) {
352 352 if (hasChanges) {
353 353 if (ui->treeView->indexWidget(validationIndex) == nullptr) {
354 354 auto widget = CatalogueExplorerHelper::buildValidationWidget(
355 355 ui->treeView,
356 356 [this, event]() {
357 357 sqpApp->catalogueController().saveEvent(event);
358 358 setEventChanges(event, false);
359 359 },
360 360 [this, event]() {
361 sqpApp->catalogueController().discardEvent(event);
362 setEventChanges(event, false);
363 impl->m_Model->refreshEvent(event, true);
361 bool removed = false;
362 sqpApp->catalogueController().discardEvent(event, removed);
363 if (removed) {
364 impl->m_Model->removeEvent(event);
365 }
366 else {
367 setEventChanges(event, false);
368 impl->m_Model->refreshEvent(event, true);
369 }
364 370 emitSelection();
365 371 });
366 372 ui->treeView->setIndexWidget(validationIndex, widget);
367 373 }
368 374 }
369 375 else {
370 376 // Note: the widget is destroyed
371 377 ui->treeView->setIndexWidget(validationIndex, nullptr);
372 378 }
373 379 }
374 380 else {
375 381 qCWarning(LOG_CatalogueEventsWidget())
376 382 << "setEventChanges: the event is not displayed in the model.";
377 383 }
378 384 }
379 385
380 386 QVector<std::shared_ptr<DBCatalogue> > CatalogueEventsWidget::displayedCatalogues() const
381 387 {
382 388 return impl->m_DisplayedCatalogues;
383 389 }
384 390
385 391 bool CatalogueEventsWidget::isAllEventsDisplayed() const
386 392 {
387 393 return impl->m_AllEventDisplayed;
388 394 }
389 395
390 396 bool CatalogueEventsWidget::isEventDisplayed(const std::shared_ptr<DBEvent> &event) const
391 397 {
392 398 return impl->m_Model->indexOf(event).isValid();
393 399 }
394 400
395 401 void CatalogueEventsWidget::populateWithCatalogues(
396 402 const QVector<std::shared_ptr<DBCatalogue> > &catalogues)
397 403 {
398 404 impl->m_DisplayedCatalogues = catalogues;
399 405 impl->m_AllEventDisplayed = false;
400 406
401 407 QSet<QUuid> eventIds;
402 408 QVector<std::shared_ptr<DBEvent> > events;
403 409
404 410 for (auto catalogue : catalogues) {
405 411 auto catalogueEvents = sqpApp->catalogueController().retrieveEventsFromCatalogue(catalogue);
406 412 for (auto event : catalogueEvents) {
407 413 if (!eventIds.contains(event->getUniqId())) {
408 414 events << event;
409 415 eventIds.insert(event->getUniqId());
410 416 }
411 417 }
412 418 }
413 419
414 420 impl->setEvents(events, this);
415 421 }
416 422
417 423 void CatalogueEventsWidget::populateWithAllEvents()
418 424 {
419 425 impl->m_DisplayedCatalogues.clear();
420 426 impl->m_AllEventDisplayed = true;
421 427
422 428 auto allEvents = sqpApp->catalogueController().retrieveAllEvents();
423 429
424 430 QVector<std::shared_ptr<DBEvent> > events;
425 431 for (auto event : allEvents) {
426 432 events << event;
427 433 }
428 434
429 435 impl->setEvents(events, this);
430 436 }
431 437
432 438 void CatalogueEventsWidget::clear()
433 439 {
434 440 impl->m_DisplayedCatalogues.clear();
435 441 impl->m_AllEventDisplayed = false;
436 442 impl->setEvents({}, this);
437 443 }
438 444
439 445 void CatalogueEventsWidget::refresh()
440 446 {
441 447 if (isAllEventsDisplayed()) {
442 448 populateWithAllEvents();
443 449 }
444 450 else if (!impl->m_DisplayedCatalogues.isEmpty()) {
445 451 populateWithCatalogues(impl->m_DisplayedCatalogues);
446 452 }
447 453 }
448 454
449 455 void CatalogueEventsWidget::emitSelection()
450 456 {
451 457 QVector<std::shared_ptr<DBEvent> > events;
452 458 QVector<QPair<std::shared_ptr<DBEvent>, std::shared_ptr<DBEventProduct> > > eventProducts;
453 459 impl->getSelectedItems(ui->treeView, events, eventProducts);
454 460
455 461 if (!events.isEmpty() && eventProducts.isEmpty()) {
456 462 emit eventsSelected(events);
457 463 }
458 464 else if (events.isEmpty() && !eventProducts.isEmpty()) {
459 465 emit eventProductsSelected(eventProducts);
460 466 }
461 467 else {
462 468 emit selectionCleared();
463 469 }
464 470 }
@@ -1,211 +1,211
1 1 #include "Catalogue/CatalogueInspectorWidget.h"
2 2 #include "ui_CatalogueInspectorWidget.h"
3 3
4 4 #include <Common/DateUtils.h>
5 5 #include <DBCatalogue.h>
6 6 #include <DBEvent.h>
7 7 #include <DBEventProduct.h>
8 8 #include <DBTag.h>
9 9
10 10 struct CatalogueInspectorWidget::CatalogueInspectorWidgetPrivate {
11 11 std::shared_ptr<DBCatalogue> m_DisplayedCatalogue = nullptr;
12 12 std::shared_ptr<DBEvent> m_DisplayedEvent = nullptr;
13 13 std::shared_ptr<DBEventProduct> m_DisplayedEventProduct = nullptr;
14 14
15 15 void connectCatalogueUpdateSignals(CatalogueInspectorWidget *inspector,
16 16 Ui::CatalogueInspectorWidget *ui);
17 17 void connectEventUpdateSignals(CatalogueInspectorWidget *inspector,
18 18 Ui::CatalogueInspectorWidget *ui);
19 19 };
20 20
21 21 CatalogueInspectorWidget::CatalogueInspectorWidget(QWidget *parent)
22 22 : QWidget(parent),
23 23 ui(new Ui::CatalogueInspectorWidget),
24 24 impl{spimpl::make_unique_impl<CatalogueInspectorWidgetPrivate>()}
25 25 {
26 26 ui->setupUi(this);
27 27 showPage(Page::Empty);
28 28
29 29 impl->connectCatalogueUpdateSignals(this, ui);
30 30 impl->connectEventUpdateSignals(this, ui);
31 31 }
32 32
33 33 CatalogueInspectorWidget::~CatalogueInspectorWidget()
34 34 {
35 35 delete ui;
36 36 }
37 37
38 38 void CatalogueInspectorWidget::CatalogueInspectorWidgetPrivate::connectCatalogueUpdateSignals(
39 39 CatalogueInspectorWidget *inspector, Ui::CatalogueInspectorWidget *ui)
40 40 {
41 41 connect(ui->leCatalogueName, &QLineEdit::editingFinished, [ui, inspector, this]() {
42 42 if (ui->leCatalogueName->text() != m_DisplayedCatalogue->getName()) {
43 43 m_DisplayedCatalogue->setName(ui->leCatalogueName->text());
44 44 emit inspector->catalogueUpdated(m_DisplayedCatalogue);
45 45 }
46 46 });
47 47
48 48 connect(ui->leCatalogueAuthor, &QLineEdit::editingFinished, [ui, inspector, this]() {
49 49 if (ui->leCatalogueAuthor->text() != m_DisplayedCatalogue->getAuthor()) {
50 50 m_DisplayedCatalogue->setAuthor(ui->leCatalogueAuthor->text());
51 51 emit inspector->catalogueUpdated(m_DisplayedCatalogue);
52 52 }
53 53 });
54 54 }
55 55
56 56 void CatalogueInspectorWidget::CatalogueInspectorWidgetPrivate::connectEventUpdateSignals(
57 57 CatalogueInspectorWidget *inspector, Ui::CatalogueInspectorWidget *ui)
58 58 {
59 59 connect(ui->leEventName, &QLineEdit::editingFinished, [ui, inspector, this]() {
60 60 if (ui->leEventName->text() != m_DisplayedEvent->getName()) {
61 61 m_DisplayedEvent->setName(ui->leEventName->text());
62 62 emit inspector->eventUpdated(m_DisplayedEvent);
63 63 }
64 64 });
65 65
66 66 connect(ui->leEventTags, &QLineEdit::editingFinished, [ui, inspector, this]() {
67 auto tags = ui->leEventTags->text().split(QRegExp("\\s+"));
67 auto tags = ui->leEventTags->text().split(QRegExp("\\s+"), QString::SkipEmptyParts);
68 68 std::list<QString> tagNames;
69 69 for (auto tag : tags) {
70 70 tagNames.push_back(tag);
71 71 }
72 72
73 73 if (m_DisplayedEvent->getTagsNames() != tagNames) {
74 74 m_DisplayedEvent->setTagsNames(tagNames);
75 75 emit inspector->eventUpdated(m_DisplayedEvent);
76 76 }
77 77 });
78 78
79 79 connect(ui->leEventProduct, &QLineEdit::editingFinished, [ui, inspector, this]() {
80 80 if (ui->leEventProduct->text() != m_DisplayedEventProduct->getProductId()) {
81 81 auto oldProductId = m_DisplayedEventProduct->getProductId();
82 82 m_DisplayedEventProduct->setProductId(ui->leEventProduct->text());
83 83
84 84 auto eventProducts = m_DisplayedEvent->getEventProducts();
85 85 for (auto &eventProduct : eventProducts) {
86 86 if (eventProduct.getProductId() == oldProductId) {
87 87 eventProduct.setProductId(m_DisplayedEventProduct->getProductId());
88 88 }
89 89 }
90 90 m_DisplayedEvent->setEventProducts(eventProducts);
91 91
92 92 emit inspector->eventUpdated(m_DisplayedEvent);
93 93 }
94 94 });
95 95
96 96 connect(ui->dateTimeEventTStart, &QDateTimeEdit::editingFinished, [ui, inspector, this]() {
97 97 auto time = DateUtils::secondsSinceEpoch(ui->dateTimeEventTStart->dateTime());
98 98 if (time != m_DisplayedEventProduct->getTStart()) {
99 99 m_DisplayedEventProduct->setTStart(time);
100 100
101 101 auto eventProducts = m_DisplayedEvent->getEventProducts();
102 102 for (auto &eventProduct : eventProducts) {
103 103 if (eventProduct.getProductId() == m_DisplayedEventProduct->getProductId()) {
104 104 eventProduct.setTStart(m_DisplayedEventProduct->getTStart());
105 105 }
106 106 }
107 107 m_DisplayedEvent->setEventProducts(eventProducts);
108 108
109 109 emit inspector->eventUpdated(m_DisplayedEvent);
110 110 }
111 111 });
112 112
113 113 connect(ui->dateTimeEventTEnd, &QDateTimeEdit::editingFinished, [ui, inspector, this]() {
114 114 auto time = DateUtils::secondsSinceEpoch(ui->dateTimeEventTEnd->dateTime());
115 115 if (time != m_DisplayedEventProduct->getTEnd()) {
116 116 m_DisplayedEventProduct->setTEnd(time);
117 117
118 118 auto eventProducts = m_DisplayedEvent->getEventProducts();
119 119 for (auto &eventProduct : eventProducts) {
120 120 if (eventProduct.getProductId() == m_DisplayedEventProduct->getProductId()) {
121 121 eventProduct.setTEnd(m_DisplayedEventProduct->getTEnd());
122 122 }
123 123 }
124 124 m_DisplayedEvent->setEventProducts(eventProducts);
125 125
126 126 emit inspector->eventUpdated(m_DisplayedEvent);
127 127 }
128 128 });
129 129 }
130 130
131 131 void CatalogueInspectorWidget::showPage(CatalogueInspectorWidget::Page page)
132 132 {
133 133 ui->stackedWidget->setCurrentIndex(static_cast<int>(page));
134 134 }
135 135
136 136 CatalogueInspectorWidget::Page CatalogueInspectorWidget::currentPage() const
137 137 {
138 138 return static_cast<Page>(ui->stackedWidget->currentIndex());
139 139 }
140 140
141 141 void CatalogueInspectorWidget::setEvent(const std::shared_ptr<DBEvent> &event)
142 142 {
143 143 impl->m_DisplayedEvent = event;
144 144
145 145 blockSignals(true);
146 146
147 147 showPage(Page::EventProperties);
148 148 ui->leEventName->setEnabled(true);
149 149 ui->leEventName->setText(event->getName());
150 150 ui->leEventProduct->setEnabled(false);
151 151 ui->leEventProduct->setText(
152 152 QString::number(event->getEventProducts().size()).append(" product(s)"));
153 153
154 154 QString tagList;
155 155 auto tags = event->getTagsNames();
156 156 for (auto tag : tags) {
157 157 tagList += tag;
158 158 tagList += ' ';
159 159 }
160 160
161 161 ui->leEventTags->setEnabled(true);
162 162 ui->leEventTags->setText(tagList);
163 163
164 164 ui->dateTimeEventTStart->setEnabled(false);
165 165 ui->dateTimeEventTEnd->setEnabled(false);
166 166
167 167 ui->dateTimeEventTStart->setDateTime(DateUtils::dateTime(event->getTStart()));
168 168 ui->dateTimeEventTEnd->setDateTime(DateUtils::dateTime(event->getTEnd()));
169 169
170 170 blockSignals(false);
171 171 }
172 172
173 173 void CatalogueInspectorWidget::setEventProduct(const std::shared_ptr<DBEvent> &event,
174 174 const std::shared_ptr<DBEventProduct> &eventProduct)
175 175 {
176 176
177 177 impl->m_DisplayedEvent = event;
178 178 impl->m_DisplayedEventProduct = eventProduct;
179 179
180 180 blockSignals(true);
181 181
182 182 showPage(Page::EventProperties);
183 183 ui->leEventName->setEnabled(false);
184 184 ui->leEventName->setText(event->getName());
185 185 ui->leEventProduct->setEnabled(false);
186 186 ui->leEventProduct->setText(eventProduct->getProductId());
187 187
188 188 ui->leEventTags->setEnabled(false);
189 189 ui->leEventTags->clear();
190 190
191 191 ui->dateTimeEventTStart->setEnabled(true);
192 192 ui->dateTimeEventTEnd->setEnabled(true);
193 193
194 194 ui->dateTimeEventTStart->setDateTime(DateUtils::dateTime(eventProduct->getTStart()));
195 195 ui->dateTimeEventTEnd->setDateTime(DateUtils::dateTime(eventProduct->getTEnd()));
196 196
197 197 blockSignals(false);
198 198 }
199 199
200 200 void CatalogueInspectorWidget::setCatalogue(const std::shared_ptr<DBCatalogue> &catalogue)
201 201 {
202 202 impl->m_DisplayedCatalogue = catalogue;
203 203
204 204 blockSignals(true);
205 205
206 206 showPage(Page::CatalogueProperties);
207 207 ui->leCatalogueName->setText(catalogue->getName());
208 208 ui->leCatalogueAuthor->setText(catalogue->getAuthor());
209 209
210 210 blockSignals(false);
211 211 }
General Comments 0
You need to be logged in to leave comments. Login now