##// END OF EJS Templates
Adaptation to the shared pointers of catalogue controller
trabillard -
r1176:20294bb49a24
parent child
Show More
@@ -1,70 +1,69
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
19 19 Q_DECLARE_LOGGING_CATEGORY(LOG_CatalogueController)
20 20
21 21 class DataSourceItem;
22 22 class Variable;
23 23
24 24 /**
25 25 * @brief The CatalogueController class aims to handle catalogues and event using the CatalogueAPI
26 26 * library.
27 27 */
28 28 class SCIQLOP_CORE_EXPORT CatalogueController : public QObject {
29 29 Q_OBJECT
30 30 public:
31 31 explicit CatalogueController(QObject *parent = 0);
32 32 virtual ~CatalogueController();
33 33
34 34 // DB
35 35 // QStringList getRepositories() const;
36 36 void addDB(const QString &dbPath);
37 37 void saveDB(const QString &destinationPath, const QString &repository);
38 38
39 39 // Event
40 40 // bool createEvent(const QString &name);
41 41 std::list<std::shared_ptr<DBEvent> > retrieveEvents(const QString &repository) const;
42 42 std::list<std::shared_ptr<DBEvent> > retrieveAllEvents() const;
43 43 std::list<std::shared_ptr<DBEvent> >
44 retrieveEventsFromCatalogue(const QString &repository,
45 std::shared_ptr<DBCatalogue> catalogue) const;
44 retrieveEventsFromCatalogue(std::shared_ptr<DBCatalogue> catalogue) const;
46 45 // void updateEvent(std::shared_ptr<DBEvent> event);
47 46 // void trashEvent(std::shared_ptr<DBEvent> event);
48 47 // void removeEvent(std::shared_ptr<DBEvent> event);
49 48 // void restore(QUuid eventId);
50 49 // void saveEvent(std::shared_ptr<DBEvent> event);
51 50
52 51 // Catalogue
53 52 // bool createCatalogue(const QString &name, QVector<QUuid> eventList);
54 53 std::list<std::shared_ptr<DBCatalogue> > getCatalogues(const QString &repository) const;
55 54 // void removeEvent(QUuid catalogueId, const QString &repository);
56 55 // void saveCatalogue(std::shared_ptr<DBEvent> event);
57 56
58 57 public slots:
59 58 /// Manage init/end of the controller
60 59 void initialize();
61 60 void finalize();
62 61
63 62 private:
64 63 void waitForFinish();
65 64
66 65 class CatalogueControllerPrivate;
67 66 spimpl::unique_impl_ptr<CatalogueControllerPrivate> impl;
68 67 };
69 68
70 69 #endif // SCIQLOP_CATALOGUECONTROLLER_H
@@ -1,162 +1,161
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 <DBTag.h>
12 12 #include <IRequestPredicate.h>
13 13
14 14 #include <QMutex>
15 15 #include <QThread>
16 16
17 17 #include <QDir>
18 18 #include <QStandardPaths>
19 19
20 20 Q_LOGGING_CATEGORY(LOG_CatalogueController, "CatalogueController")
21 21
22 22 namespace {
23 23
24 24 static QString REPOSITORY_WORK_SUFFIX = QString{"Work"};
25 25
26 26 }
27 27
28 28 class CatalogueController::CatalogueControllerPrivate {
29 29 public:
30 30 QMutex m_WorkingMutex;
31 31 CatalogueDao m_CatalogueDao;
32 32
33 33 std::list<QString> m_RepositoryList;
34 34 };
35 35
36 36 CatalogueController::CatalogueController(QObject *parent)
37 37 : impl{spimpl::make_unique_impl<CatalogueControllerPrivate>()}
38 38 {
39 39 qCDebug(LOG_CatalogueController()) << tr("CatalogueController construction")
40 40 << QThread::currentThread();
41 41 }
42 42
43 43 CatalogueController::~CatalogueController()
44 44 {
45 45 qCDebug(LOG_CatalogueController()) << tr("CatalogueController destruction")
46 46 << QThread::currentThread();
47 47 this->waitForFinish();
48 48 }
49 49
50 50 void CatalogueController::addDB(const QString &dbPath)
51 51 {
52 52 QDir dbDir(dbPath);
53 53 if (dbDir.exists()) {
54 54 auto dirName = dbDir.dirName();
55 55
56 56 if (std::find(impl->m_RepositoryList.cbegin(), impl->m_RepositoryList.cend(), dirName)
57 57 != impl->m_RepositoryList.cend()) {
58 58 qCCritical(LOG_CatalogueController())
59 59 << tr("Impossible to addDB that is already loaded");
60 60 }
61 61
62 62 if (!impl->m_CatalogueDao.addDB(dbPath, dirName)) {
63 63 qCCritical(LOG_CatalogueController())
64 64 << tr("Impossible to addDB %1 from %2 ").arg(dirName, dbPath);
65 65 }
66 66 else {
67 67 impl->m_RepositoryList.push_back(dirName);
68 68 }
69 69 }
70 70 else {
71 71 qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB that not exists: ")
72 72 << dbPath;
73 73 }
74 74 }
75 75
76 76 void CatalogueController::saveDB(const QString &destinationPath, const QString &repository)
77 77 {
78 78 if (!impl->m_CatalogueDao.saveDB(destinationPath, repository)) {
79 79 qCCritical(LOG_CatalogueController())
80 80 << tr("Impossible to saveDB %1 from %2 ").arg(repository, destinationPath);
81 81 }
82 82 }
83 83
84 84 std::list<std::shared_ptr<DBEvent> >
85 85 CatalogueController::retrieveEvents(const QString &repository) const
86 86 {
87 87 auto eventsShared = std::list<std::shared_ptr<DBEvent> >{};
88 88 auto events = impl->m_CatalogueDao.getEvents(repository);
89 89 for (auto event : events) {
90 90 eventsShared.push_back(std::make_shared<DBEvent>(event));
91 91 }
92 92 return eventsShared;
93 93 }
94 94
95 95 std::list<std::shared_ptr<DBEvent> > CatalogueController::retrieveAllEvents() const
96 96 {
97 97 auto eventsShared = std::list<std::shared_ptr<DBEvent> >{};
98 98 for (auto repository : impl->m_RepositoryList) {
99 99 eventsShared.splice(eventsShared.end(), retrieveEvents(repository));
100 100 }
101 101
102 102 return eventsShared;
103 103 }
104 104
105 105 std::list<std::shared_ptr<DBEvent> >
106 CatalogueController::retrieveEventsFromCatalogue(const QString &repository,
107 std::shared_ptr<DBCatalogue> catalogue) const
106 CatalogueController::retrieveEventsFromCatalogue(std::shared_ptr<DBCatalogue> catalogue) const
108 107 {
109 108 auto eventsShared = std::list<std::shared_ptr<DBEvent> >{};
110 109 auto events = impl->m_CatalogueDao.getCatalogueEvents(*catalogue);
111 110 for (auto event : events) {
112 111 eventsShared.push_back(std::make_shared<DBEvent>(event));
113 112 }
114 113 return eventsShared;
115 114 }
116 115
117 116 std::list<std::shared_ptr<DBCatalogue> >
118 117 CatalogueController::getCatalogues(const QString &repository) const
119 118 {
120 119 auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{};
121 120 auto catalogues = impl->m_CatalogueDao.getCatalogues(repository);
122 121 for (auto catalogue : catalogues) {
123 122 cataloguesShared.push_back(std::make_shared<DBCatalogue>(catalogue));
124 123 }
125 124 return cataloguesShared;
126 125 }
127 126
128 127 void CatalogueController::initialize()
129 128 {
130 129 qCDebug(LOG_CatalogueController()) << tr("CatalogueController init")
131 130 << QThread::currentThread();
132 131 impl->m_WorkingMutex.lock();
133 132 impl->m_CatalogueDao.initialize();
134 133 auto defaultRepositoryLocation
135 134 = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
136 135
137 136 QDir defaultRepositoryLocationDir;
138 137 if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation)) {
139 138 defaultRepositoryLocationDir.cd(defaultRepositoryLocation);
140 139 auto defaultRepository = defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT);
141 140 qCInfo(LOG_CatalogueController())
142 141 << tr("Persistant data loading from: ") << defaultRepository;
143 142 this->addDB(defaultRepository);
144 143 }
145 144 else {
146 145 qCWarning(LOG_CatalogueController())
147 146 << tr("Cannot load the persistent default repository from ")
148 147 << defaultRepositoryLocation;
149 148 }
150 149
151 150 qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END");
152 151 }
153 152
154 153 void CatalogueController::finalize()
155 154 {
156 155 impl->m_WorkingMutex.unlock();
157 156 }
158 157
159 158 void CatalogueController::waitForFinish()
160 159 {
161 160 QMutexLocker locker{&impl->m_WorkingMutex};
162 161 }
@@ -1,38 +1,38
1 1 #ifndef SCIQLOP_CATALOGUEEVENTSTABLEMODEL_H
2 2 #define SCIQLOP_CATALOGUEEVENTSTABLEMODEL_H
3 3
4 4 #include <Common/spimpl.h>
5 5 #include <QAbstractTableModel>
6 6
7 #include <DBEvent.h>
7 class DBEvent;
8 8
9 9 class CatalogueEventsTableModel : public QAbstractTableModel {
10 10 public:
11 11 CatalogueEventsTableModel(QObject *parent = nullptr);
12 12
13 void setEvents(const QVector<DBEvent> &events);
14 DBEvent getEvent(int row) const;
13 void setEvents(const QVector<std::shared_ptr<DBEvent> > &events);
14 std::shared_ptr<DBEvent> getEvent(int row) const;
15 15
16 void addEvent(const DBEvent &events);
17 void removeEvent(const DBEvent &events);
16 void addEvent(const std::shared_ptr<DBEvent> &events);
17 void removeEvent(const std::shared_ptr<DBEvent> &events);
18 18
19 19 // Model
20 20 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
21 21 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
22 22 Qt::ItemFlags flags(const QModelIndex &index) const override;
23 23 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
24 24 QVariant headerData(int section, Qt::Orientation orientation,
25 25 int role = Qt::DisplayRole) const override;
26 26 void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
27 27
28 28 Qt::DropActions supportedDragActions() const override;
29 29 QStringList mimeTypes() const override;
30 30 QMimeData *mimeData(const QModelIndexList &indexes) const override;
31 31
32 32
33 33 private:
34 34 class CatalogueEventsTableModelPrivate;
35 35 spimpl::unique_impl_ptr<CatalogueEventsTableModelPrivate> impl;
36 36 };
37 37
38 38 #endif // SCIQLOP_CATALOGUEEVENTSTABLEMODEL_H
@@ -1,40 +1,40
1 1 #ifndef SCIQLOP_CATALOGUEEVENTSWIDGET_H
2 2 #define SCIQLOP_CATALOGUEEVENTSWIDGET_H
3 3
4 4 #include <Common/spimpl.h>
5 5 #include <QLoggingCategory>
6 6 #include <QWidget>
7 7
8 8 class DBCatalogue;
9 9 class DBEvent;
10 10 class VisualizationWidget;
11 11
12 12 namespace Ui {
13 13 class CatalogueEventsWidget;
14 14 }
15 15
16 16 Q_DECLARE_LOGGING_CATEGORY(LOG_CatalogueEventsWidget)
17 17
18 18 class CatalogueEventsWidget : public QWidget {
19 19 Q_OBJECT
20 20
21 21 signals:
22 void eventsSelected(const QVector<DBEvent> &event);
22 void eventsSelected(const QVector<std::shared_ptr<DBEvent> > &event);
23 23
24 24 public:
25 25 explicit CatalogueEventsWidget(QWidget *parent = 0);
26 26 virtual ~CatalogueEventsWidget();
27 27
28 28 void setVisualizationWidget(VisualizationWidget *visualization);
29 29
30 30 public slots:
31 void populateWithCatalogues(const QVector<DBCatalogue> &catalogues);
31 void populateWithCatalogues(const QVector<std::shared_ptr<DBCatalogue> > &catalogues);
32 32
33 33 private:
34 34 Ui::CatalogueEventsWidget *ui;
35 35
36 36 class CatalogueEventsWidgetPrivate;
37 37 spimpl::unique_impl_ptr<CatalogueEventsWidgetPrivate> impl;
38 38 };
39 39
40 40 #endif // SCIQLOP_CATALOGUEEVENTSWIDGET_H
@@ -1,35 +1,36
1 1 #ifndef SCIQLOP_CATALOGUEINSPECTORWIDGET_H
2 2 #define SCIQLOP_CATALOGUEINSPECTORWIDGET_H
3 3
4 4 #include <QWidget>
5 #include <memory>
5 6
6 7 namespace Ui {
7 8 class CatalogueInspectorWidget;
8 9 }
9 10
10 11 class DBCatalogue;
11 12 class DBEvent;
12 13
13 14 class CatalogueInspectorWidget : public QWidget {
14 15 Q_OBJECT
15 16
16 17 public:
17 18 explicit CatalogueInspectorWidget(QWidget *parent = 0);
18 19 virtual ~CatalogueInspectorWidget();
19 20
20 21 /// Enum matching the pages inside the stacked widget
21 22 enum class Page { Empty, CatalogueProperties, EventProperties };
22 23
23 24 Page currentPage() const;
24 25
25 void setEvent(const DBEvent &event);
26 void setCatalogue(const DBCatalogue &catalogue);
26 void setEvent(const std::shared_ptr<DBEvent> &event);
27 void setCatalogue(const std::shared_ptr<DBCatalogue> &catalogue);
27 28
28 29 public slots:
29 30 void showPage(Page page);
30 31
31 32 private:
32 33 Ui::CatalogueInspectorWidget *ui;
33 34 };
34 35
35 36 #endif // SCIQLOP_CATALOGUEINSPECTORWIDGET_H
@@ -1,38 +1,38
1 1 #ifndef SCIQLOP_CATALOGUESIDEBARWIDGET_H
2 2 #define SCIQLOP_CATALOGUESIDEBARWIDGET_H
3 3
4 4 #include <Common/spimpl.h>
5 5 #include <QTreeWidgetItem>
6 6 #include <QWidget>
7 7
8 #include <DBCatalogue.h>
8 class DBCatalogue;
9 9
10 10 namespace Ui {
11 11 class CatalogueSideBarWidget;
12 12 }
13 13
14 14 class CatalogueSideBarWidget : public QWidget {
15 15 Q_OBJECT
16 16
17 17 signals:
18 void catalogueSelected(const QVector<DBCatalogue> &catalogues);
18 void catalogueSelected(const QVector<std::shared_ptr<DBCatalogue> > &catalogues);
19 19 void databaseSelected(const QStringList &databases);
20 20 void allEventsSelected();
21 21 void trashSelected();
22 22 void selectionCleared();
23 23
24 24 public:
25 25 explicit CatalogueSideBarWidget(QWidget *parent = 0);
26 26 virtual ~CatalogueSideBarWidget();
27 27
28 28 private:
29 29 Ui::CatalogueSideBarWidget *ui;
30 30
31 31 class CatalogueSideBarWidgetPrivate;
32 32 spimpl::unique_impl_ptr<CatalogueSideBarWidgetPrivate> impl;
33 33
34 34 private slots:
35 35 void onContextMenuRequested(const QPoint &pos);
36 36 };
37 37
38 38 #endif // SCIQLOP_CATALOGUESIDEBARWIDGET_H
@@ -1,27 +1,28
1 1 #ifndef SCIQLOP_CATALOGUETREEWIDGETITEM_H
2 2 #define SCIQLOP_CATALOGUETREEWIDGETITEM_H
3 3
4 4 #include <Common/spimpl.h>
5 5 #include <QTreeWidgetItem>
6 6
7 #include <DBCatalogue.h>
7 class DBCatalogue;
8 8
9 9
10 10 class CatalogueTreeWidgetItem : public QTreeWidgetItem {
11 11 public:
12 CatalogueTreeWidgetItem(DBCatalogue catalogue, int type = QTreeWidgetItem::Type);
12 CatalogueTreeWidgetItem(std::shared_ptr<DBCatalogue> catalogue,
13 int type = QTreeWidgetItem::Type);
13 14
14 15 QVariant data(int column, int role) const override;
15 16 void setData(int column, int role, const QVariant &value) override;
16 17
17 18 /// Returns the catalogue represented by the item
18 DBCatalogue catalogue() const;
19 std::shared_ptr<DBCatalogue> catalogue() const;
19 20
20 21 void setHasChanges(bool value);
21 22
22 23 private:
23 24 class CatalogueTreeWidgetItemPrivate;
24 25 spimpl::unique_impl_ptr<CatalogueTreeWidgetItemPrivate> impl;
25 26 };
26 27
27 28 #endif // SCIQLOP_CATALOGUETREEWIDGETITEM_H
@@ -1,185 +1,185
1 1 #include "Catalogue/CatalogueEventsTableModel.h"
2 2
3 3 #include <Common/DateUtils.h>
4 4 #include <Common/MimeTypesDef.h>
5 5 #include <DBEvent.h>
6 6 #include <DBTag.h>
7 7 #include <Data/SqpRange.h>
8 8 #include <QMimeData>
9 9 #include <SqpApplication.h>
10 10 #include <Time/TimeController.h>
11 11
12 12 struct CatalogueEventsTableModel::CatalogueEventsTableModelPrivate {
13 QVector<DBEvent> m_Events;
13 QVector<std::shared_ptr<DBEvent> > m_Events;
14 14
15 15 enum class Column { Event, TStart, TEnd, Tags, Product, NbColumn };
16 16 QStringList columnNames()
17 17 {
18 18 return QStringList{tr("Event"), tr("TStart"), tr("TEnd"), tr("Tags"), tr("Product")};
19 19 }
20 20
21 QVariant eventData(int col, const DBEvent &event) const
21 QVariant eventData(int col, const std::shared_ptr<DBEvent> &event) const
22 22 {
23 23 switch (static_cast<Column>(col)) {
24 24 case Column::Event:
25 return event.getName();
25 return event->getName();
26 26 case Column::TStart:
27 return DateUtils::dateTime(event.getTStart());
27 return DateUtils::dateTime(event->getTStart());
28 28 case Column::TEnd:
29 return DateUtils::dateTime(event.getTEnd());
29 return DateUtils::dateTime(event->getTEnd());
30 30 case Column::Product:
31 return event.getProduct();
31 return event->getProduct();
32 32 case Column::Tags: {
33 33 QString tagList;
34 auto tags = const_cast<DBEvent *>(&event)->getTags();
34 auto tags = event->getTags();
35 35 for (auto tag : tags) {
36 36 tagList += tag.getName();
37 37 tagList += ' ';
38 38 }
39 39
40 40 return tagList;
41 41 }
42 42 default:
43 43 break;
44 44 }
45 45
46 46 Q_ASSERT(false);
47 47 return QStringLiteral("Unknown Data");
48 48 }
49 49 };
50 50
51 51 CatalogueEventsTableModel::CatalogueEventsTableModel(QObject *parent)
52 52 : QAbstractTableModel(parent),
53 53 impl{spimpl::make_unique_impl<CatalogueEventsTableModelPrivate>()}
54 54 {
55 55 }
56 56
57 void CatalogueEventsTableModel::setEvents(const QVector<DBEvent> &events)
57 void CatalogueEventsTableModel::setEvents(const QVector<std::shared_ptr<DBEvent> > &events)
58 58 {
59 59 beginResetModel();
60 60 impl->m_Events = events;
61 61 endResetModel();
62 62 }
63 63
64 DBEvent CatalogueEventsTableModel::getEvent(int row) const
64 std::shared_ptr<DBEvent> CatalogueEventsTableModel::getEvent(int row) const
65 65 {
66 66 return impl->m_Events.value(row);
67 67 }
68 68
69 void CatalogueEventsTableModel::addEvent(const DBEvent &events)
69 void CatalogueEventsTableModel::addEvent(const std::shared_ptr<DBEvent> &events)
70 70 {
71 71 beginInsertRows(QModelIndex(), impl->m_Events.count() - 1, impl->m_Events.count() - 1);
72 72 // impl->m_Events.append(event); TODO
73 73 endInsertRows();
74 74 }
75 75
76 void CatalogueEventsTableModel::removeEvent(const DBEvent &events)
76 void CatalogueEventsTableModel::removeEvent(const std::shared_ptr<DBEvent> &events)
77 77 {
78 78 // TODO
79 79 auto index = -1; // impl->m_Events.indexOf(event);
80 80 if (index >= 0) {
81 81 beginRemoveRows(QModelIndex(), index, index);
82 82 impl->m_Events.removeAt(index);
83 83 endRemoveRows();
84 84 }
85 85 }
86 86
87 87 int CatalogueEventsTableModel::rowCount(const QModelIndex &parent) const
88 88 {
89 89 int r = impl->m_Events.count();
90 90 return r;
91 91 }
92 92
93 93 int CatalogueEventsTableModel::columnCount(const QModelIndex &parent) const
94 94 {
95 95 int c = static_cast<int>(CatalogueEventsTableModelPrivate::Column::NbColumn);
96 96 return c;
97 97 }
98 98
99 99 Qt::ItemFlags CatalogueEventsTableModel::flags(const QModelIndex &index) const
100 100 {
101 101 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
102 102 }
103 103
104 104 QVariant CatalogueEventsTableModel::data(const QModelIndex &index, int role) const
105 105 {
106 106 if (index.isValid()) {
107 107 auto event = getEvent(index.row());
108 108
109 109 switch (role) {
110 110 case Qt::DisplayRole:
111 111 return impl->eventData(index.column(), event);
112 112 break;
113 113 }
114 114 }
115 115
116 116 return QVariant{};
117 117 }
118 118
119 119 QVariant CatalogueEventsTableModel::headerData(int section, Qt::Orientation orientation,
120 120 int role) const
121 121 {
122 122 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
123 123 return impl->columnNames().value(section);
124 124 }
125 125
126 126 return QVariant();
127 127 }
128 128
129 129 void CatalogueEventsTableModel::sort(int column, Qt::SortOrder order)
130 130 {
131 131 std::sort(impl->m_Events.begin(), impl->m_Events.end(),
132 132 [this, column, order](auto e1, auto e2) {
133 133 auto data1 = impl->eventData(column, e1);
134 134 auto data2 = impl->eventData(column, e2);
135 135
136 136 auto result = data1.toString() < data2.toString();
137 137
138 138 return order == Qt::AscendingOrder ? result : !result;
139 139 });
140 140
141 141 emit dataChanged(QModelIndex(), QModelIndex());
142 142 }
143 143
144 144 Qt::DropActions CatalogueEventsTableModel::supportedDragActions() const
145 145 {
146 146 return Qt::CopyAction | Qt::MoveAction;
147 147 }
148 148
149 149 QStringList CatalogueEventsTableModel::mimeTypes() const
150 150 {
151 151 return {MIME_TYPE_EVENT_LIST, MIME_TYPE_TIME_RANGE};
152 152 }
153 153
154 154 QMimeData *CatalogueEventsTableModel::mimeData(const QModelIndexList &indexes) const
155 155 {
156 156 auto mimeData = new QMimeData;
157 157
158 QVector<DBEvent> eventList;
158 QVector<std::shared_ptr<DBEvent> > eventList;
159 159
160 160 SqpRange firstTimeRange;
161 161 for (const auto &index : indexes) {
162 162 if (index.column() == 0) { // only the first column
163 163 auto event = getEvent(index.row());
164 164 if (eventList.isEmpty()) {
165 165 // Gets the range of the first variable
166 firstTimeRange.m_TStart = event.getTStart();
167 firstTimeRange.m_TEnd = event.getTEnd();
166 firstTimeRange.m_TStart = event->getTStart();
167 firstTimeRange.m_TEnd = event->getTEnd();
168 168 }
169 169
170 170 eventList << event;
171 171 }
172 172 }
173 173
174 174 auto eventsEncodedData
175 175 = QByteArray{}; // sqpApp->catalogueController().->mimeDataForEvents(eventList); //TODO
176 176 mimeData->setData(MIME_TYPE_EVENT_LIST, eventsEncodedData);
177 177
178 178 if (eventList.count() == 1) {
179 179 // No time range MIME data if multiple events are dragged
180 180 auto timeEncodedData = TimeController::mimeDataForTimeRange(firstTimeRange);
181 181 mimeData->setData(MIME_TYPE_TIME_RANGE, timeEncodedData);
182 182 }
183 183
184 184 return mimeData;
185 185 }
@@ -1,292 +1,291
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 #include <Visualization/VisualizationTabWidget.h>
10 10 #include <Visualization/VisualizationWidget.h>
11 11 #include <Visualization/VisualizationZoneWidget.h>
12 12
13 13 #include <QDialog>
14 14 #include <QDialogButtonBox>
15 15 #include <QListWidget>
16 16
17 17 Q_LOGGING_CATEGORY(LOG_CatalogueEventsWidget, "CatalogueEventsWidget")
18 18
19 19 /// Format of the dates appearing in the label of a cursor
20 20 const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss");
21 21
22 22 struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate {
23 23
24 24 CatalogueEventsTableModel *m_Model = nullptr;
25 25 QStringList m_ZonesForTimeMode;
26 26 QString m_ZoneForGraphMode;
27 27
28 28 VisualizationWidget *m_VisualizationWidget = nullptr;
29 29
30 void setEvents(const QVector<DBEvent> &events, QTableView *tableView)
30 void setEvents(const QVector<std::shared_ptr<DBEvent> > &events, QTableView *tableView)
31 31 {
32 32 tableView->setSortingEnabled(false);
33 33 m_Model->setEvents(events);
34 34 tableView->setSortingEnabled(true);
35 35 }
36 36
37 void addEvent(const DBEvent &event, QTableView *tableView)
37 void addEvent(const std::shared_ptr<DBEvent> &event, QTableView *tableView)
38 38 {
39 39 tableView->setSortingEnabled(false);
40 40 m_Model->addEvent(event);
41 41 tableView->setSortingEnabled(true);
42 42 }
43 43
44 void removeEvent(const DBEvent &event, QTableView *tableView)
44 void removeEvent(const std::shared_ptr<DBEvent> &event, QTableView *tableView)
45 45 {
46 46 tableView->setSortingEnabled(false);
47 47 m_Model->removeEvent(event);
48 48 tableView->setSortingEnabled(true);
49 49 }
50 50
51 51 QStringList getAvailableVisualizationZoneList() const
52 52 {
53 53 if (m_VisualizationWidget) {
54 54 if (auto tab = m_VisualizationWidget->currentTabWidget()) {
55 55 return tab->availableZoneWidgets();
56 56 }
57 57 }
58 58
59 59 return QStringList{};
60 60 }
61 61
62 62 QStringList selectZone(QWidget *parent, const QStringList &selectedZones,
63 63 bool allowMultiSelection, const QPoint &location)
64 64 {
65 65 auto availableZones = getAvailableVisualizationZoneList();
66 66 if (availableZones.isEmpty()) {
67 67 return QStringList{};
68 68 }
69 69
70 70 QDialog d(parent, Qt::Tool);
71 71 d.setWindowTitle("Choose a zone");
72 72 auto layout = new QVBoxLayout{&d};
73 73 layout->setContentsMargins(0, 0, 0, 0);
74 74 auto listWidget = new QListWidget{&d};
75 75 layout->addWidget(listWidget);
76 76
77 77 QSet<QListWidgetItem *> checkedItems;
78 78 for (auto zone : availableZones) {
79 79 auto item = new QListWidgetItem{zone};
80 80 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
81 81 if (selectedZones.contains(zone)) {
82 82 item->setCheckState(Qt::Checked);
83 83 checkedItems << item;
84 84 }
85 85 else {
86 86 item->setCheckState(Qt::Unchecked);
87 87 }
88 88
89 89 listWidget->addItem(item);
90 90 }
91 91
92 92 auto buttonBox = new QDialogButtonBox{QDialogButtonBox::Ok, &d};
93 93 layout->addWidget(buttonBox);
94 94
95 95 QObject::connect(buttonBox, &QDialogButtonBox::accepted, &d, &QDialog::accept);
96 96 QObject::connect(buttonBox, &QDialogButtonBox::rejected, &d, &QDialog::reject);
97 97
98 98 QObject::connect(listWidget, &QListWidget::itemChanged,
99 99 [&checkedItems, allowMultiSelection, listWidget](auto item) {
100 100 if (item->checkState() == Qt::Checked) {
101 101 if (!allowMultiSelection) {
102 102 for (auto checkedItem : checkedItems) {
103 103 listWidget->blockSignals(true);
104 104 checkedItem->setCheckState(Qt::Unchecked);
105 105 listWidget->blockSignals(false);
106 106 }
107 107
108 108 checkedItems.clear();
109 109 }
110 110 checkedItems << item;
111 111 }
112 112 else {
113 113 checkedItems.remove(item);
114 114 }
115 115 });
116 116
117 117 QStringList result;
118 118
119 119 d.setMinimumWidth(120);
120 120 d.resize(d.minimumSizeHint());
121 121 d.move(location);
122 122 if (d.exec() == QDialog::Accepted) {
123 123 for (auto item : checkedItems) {
124 124 result += item->text();
125 125 }
126 126 }
127 127 else {
128 128 result = selectedZones;
129 129 }
130 130
131 131 return result;
132 132 }
133 133
134 134 void updateForTimeMode(QTableView *tableView)
135 135 {
136 136 auto selectedRows = tableView->selectionModel()->selectedRows();
137 137
138 138 if (selectedRows.count() == 1) {
139 139 auto event = m_Model->getEvent(selectedRows.first().row());
140 140 if (m_VisualizationWidget) {
141 141 if (auto tab = m_VisualizationWidget->currentTabWidget()) {
142 142
143 143 for (auto zoneName : m_ZonesForTimeMode) {
144 144 if (auto zone = tab->getZoneWithName(zoneName)) {
145 145 SqpRange eventRange;
146 eventRange.m_TStart = event.getTStart();
147 eventRange.m_TEnd = event.getTEnd();
146 eventRange.m_TStart = event->getTStart();
147 eventRange.m_TEnd = event->getTEnd();
148 148 zone->setZoneRange(eventRange);
149 149 }
150 150 }
151 151 }
152 152 else {
153 153 qCWarning(LOG_CatalogueEventsWidget())
154 154 << "updateTimeZone: no tab found in the visualization";
155 155 }
156 156 }
157 157 else {
158 158 qCWarning(LOG_CatalogueEventsWidget())
159 159 << "updateTimeZone: visualization widget not found";
160 160 }
161 161 }
162 162 else {
163 163 qCWarning(LOG_CatalogueEventsWidget())
164 164 << "updateTimeZone: not compatible with multiple events selected";
165 165 }
166 166 }
167 167
168 168 void updateForGraphMode(QTableView *tableView)
169 169 {
170 170 auto selectedRows = tableView->selectionModel()->selectedRows();
171 171
172 172 if (selectedRows.count() == 1) {
173 173 auto event = m_Model->getEvent(selectedRows.first().row());
174 174 if (m_VisualizationWidget) {
175 175 if (auto tab = m_VisualizationWidget->currentTabWidget()) {
176 176 if (auto zone = tab->getZoneWithName(m_ZoneForGraphMode)) {
177 177 // TODO
178 178 }
179 179 }
180 180 else {
181 181 qCWarning(LOG_CatalogueEventsWidget())
182 182 << "updateGraphMode: no tab found in the visualization";
183 183 }
184 184 }
185 185 else {
186 186 qCWarning(LOG_CatalogueEventsWidget())
187 187 << "updateGraphMode: visualization widget not found";
188 188 }
189 189 }
190 190 else {
191 191 qCWarning(LOG_CatalogueEventsWidget())
192 192 << "updateGraphMode: not compatible with multiple events selected";
193 193 }
194 194 }
195 195 };
196 196
197 197 CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent)
198 198 : QWidget(parent),
199 199 ui(new Ui::CatalogueEventsWidget),
200 200 impl{spimpl::make_unique_impl<CatalogueEventsWidgetPrivate>()}
201 201 {
202 202 ui->setupUi(this);
203 203
204 204 impl->m_Model = new CatalogueEventsTableModel{this};
205 205 ui->tableView->setModel(impl->m_Model);
206 206
207 207 ui->tableView->setSortingEnabled(true);
208 208 ui->tableView->setDragDropMode(QAbstractItemView::DragDrop);
209 209 ui->tableView->setDragEnabled(true);
210 210
211 211 connect(ui->btnTime, &QToolButton::clicked, [this](auto checked) {
212 212 if (checked) {
213 213 ui->btnChart->setChecked(false);
214 214 impl->m_ZonesForTimeMode
215 215 = impl->selectZone(this, impl->m_ZonesForTimeMode, true,
216 216 this->mapToGlobal(ui->btnTime->frameGeometry().center()));
217 217
218 218 impl->updateForTimeMode(ui->tableView);
219 219 }
220 220 });
221 221
222 222 connect(ui->btnChart, &QToolButton::clicked, [this](auto checked) {
223 223 if (checked) {
224 224 ui->btnTime->setChecked(false);
225 225 impl->m_ZoneForGraphMode
226 226 = impl->selectZone(this, {impl->m_ZoneForGraphMode}, false,
227 227 this->mapToGlobal(ui->btnChart->frameGeometry().center()))
228 228 .value(0);
229 229
230 230 impl->updateForGraphMode(ui->tableView);
231 231 }
232 232 });
233 233
234 234 auto emitSelection = [this]() {
235 QVector<DBEvent> events;
235 QVector<std::shared_ptr<DBEvent> > events;
236 236 for (auto rowIndex : ui->tableView->selectionModel()->selectedRows()) {
237 237 events << impl->m_Model->getEvent(rowIndex.row());
238 238 }
239 239
240 240 emit this->eventsSelected(events);
241 241 };
242 242
243 243 connect(ui->tableView, &QTableView::clicked, emitSelection);
244 244 connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, emitSelection);
245 245
246 246 connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, [this]() {
247 247 auto isNotMultiSelection = ui->tableView->selectionModel()->selectedRows().count() <= 1;
248 248 ui->btnChart->setEnabled(isNotMultiSelection);
249 249 ui->btnTime->setEnabled(isNotMultiSelection);
250 250
251 251 if (isNotMultiSelection && ui->btnTime->isChecked()) {
252 252 impl->updateForTimeMode(ui->tableView);
253 253 }
254 254 else if (isNotMultiSelection && ui->btnChart->isChecked()) {
255 255 impl->updateForGraphMode(ui->tableView);
256 256 }
257 257 });
258 258
259 259 ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
260 260 ui->tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
261 261 ui->tableView->horizontalHeader()->setSortIndicatorShown(true);
262 262 }
263 263
264 264 CatalogueEventsWidget::~CatalogueEventsWidget()
265 265 {
266 266 delete ui;
267 267 }
268 268
269 269 void CatalogueEventsWidget::setVisualizationWidget(VisualizationWidget *visualization)
270 270 {
271 271 impl->m_VisualizationWidget = visualization;
272 272 }
273 273
274 void CatalogueEventsWidget::populateWithCatalogues(const QVector<DBCatalogue> &catalogues)
274 void CatalogueEventsWidget::populateWithCatalogues(
275 const QVector<std::shared_ptr<DBCatalogue> > &catalogues)
275 276 {
276 auto &dao = sqpApp->catalogueController().getDao();
277
278 277 QSet<QUuid> eventIds;
279 QVector<DBEvent> events;
278 QVector<std::shared_ptr<DBEvent> > events;
280 279
281 280 for (auto catalogue : catalogues) {
282 auto catalogueEvents = dao.getCatalogueEvents(catalogue);
281 auto catalogueEvents = sqpApp->catalogueController().retrieveEventsFromCatalogue(catalogue);
283 282 for (auto event : catalogueEvents) {
284 if (!eventIds.contains(event.getUniqId())) {
283 if (!eventIds.contains(event->getUniqId())) {
285 284 events << event;
286 eventIds.insert(event.getUniqId());
285 eventIds.insert(event->getUniqId());
287 286 }
288 287 }
289 288 }
290 289
291 290 impl->setEvents(events, ui->tableView);
292 291 }
@@ -1,56 +1,56
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 <DBTag.h>
8 8
9 9 CatalogueInspectorWidget::CatalogueInspectorWidget(QWidget *parent)
10 10 : QWidget(parent), ui(new Ui::CatalogueInspectorWidget)
11 11 {
12 12 ui->setupUi(this);
13 13 showPage(Page::Empty);
14 14 }
15 15
16 16 CatalogueInspectorWidget::~CatalogueInspectorWidget()
17 17 {
18 18 delete ui;
19 19 }
20 20
21 21 void CatalogueInspectorWidget::showPage(CatalogueInspectorWidget::Page page)
22 22 {
23 23 ui->stackedWidget->setCurrentIndex(static_cast<int>(page));
24 24 }
25 25
26 26 CatalogueInspectorWidget::Page CatalogueInspectorWidget::currentPage() const
27 27 {
28 28 return static_cast<Page>(ui->stackedWidget->currentIndex());
29 29 }
30 30
31 void CatalogueInspectorWidget::setEvent(const DBEvent &event)
31 void CatalogueInspectorWidget::setEvent(const std::shared_ptr<DBEvent> &event)
32 32 {
33 33 showPage(Page::EventProperties);
34 ui->leEventName->setText(event.getName());
35 ui->leEventMission->setText(event.getMission());
36 ui->leEventProduct->setText(event.getProduct());
34 ui->leEventName->setText(event->getName());
35 ui->leEventMission->setText(event->getMission());
36 ui->leEventProduct->setText(event->getProduct());
37 37
38 38 QString tagList;
39 auto tags = const_cast<DBEvent *>(&event)->getTags();
39 auto tags = event->getTags();
40 40 for (auto tag : tags) {
41 41 tagList += tag.getName();
42 42 tagList += ' ';
43 43 }
44 44
45 45 ui->leEventTags->setText(tagList);
46 46
47 ui->dateTimeEventTStart->setDateTime(DateUtils::dateTime(event.getTStart()));
48 ui->dateTimeEventTEnd->setDateTime(DateUtils::dateTime(event.getTEnd()));
47 ui->dateTimeEventTStart->setDateTime(DateUtils::dateTime(event->getTStart()));
48 ui->dateTimeEventTEnd->setDateTime(DateUtils::dateTime(event->getTEnd()));
49 49 }
50 50
51 void CatalogueInspectorWidget::setCatalogue(const DBCatalogue &catalogue)
51 void CatalogueInspectorWidget::setCatalogue(const std::shared_ptr<DBCatalogue> &catalogue)
52 52 {
53 53 showPage(Page::CatalogueProperties);
54 ui->leCatalogueName->setText(catalogue.getName());
55 ui->leCatalogueAuthor->setText(catalogue.getAuthor());
54 ui->leCatalogueName->setText(catalogue->getName());
55 ui->leCatalogueAuthor->setText(catalogue->getAuthor());
56 56 }
@@ -1,203 +1,198
1 1 #include "Catalogue/CatalogueSideBarWidget.h"
2 2 #include "ui_CatalogueSideBarWidget.h"
3 3 #include <SqpApplication.h>
4 4
5 5 #include <Catalogue/CatalogueController.h>
6 6 #include <Catalogue/CatalogueTreeWidgetItem.h>
7 7 #include <CatalogueDao.h>
8 8 #include <ComparaisonPredicate.h>
9 9 #include <DBCatalogue.h>
10 10
11 11 #include <QMenu>
12 12
13 13
14 14 constexpr auto ALL_EVENT_ITEM_TYPE = QTreeWidgetItem::UserType;
15 15 constexpr auto TRASH_ITEM_TYPE = QTreeWidgetItem::UserType + 1;
16 16 constexpr auto CATALOGUE_ITEM_TYPE = QTreeWidgetItem::UserType + 2;
17 17 constexpr auto DATABASE_ITEM_TYPE = QTreeWidgetItem::UserType + 3;
18 18
19 19
20 20 struct CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate {
21 21
22 QHash<QTreeWidgetItem *, DBCatalogue> m_CatalogueMap;
23
24 22 void configureTreeWidget(QTreeWidget *treeWidget);
25 23 QTreeWidgetItem *addDatabaseItem(const QString &name, QTreeWidget *treeWidget);
26 24 QTreeWidgetItem *getDatabaseItem(const QString &name, QTreeWidget *treeWidget);
27 void addCatalogueItem(const DBCatalogue &catalogue, QTreeWidgetItem *parentDatabaseItem);
25 void addCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue,
26 QTreeWidgetItem *parentDatabaseItem);
28 27 };
29 28
30 29 CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent)
31 30 : QWidget(parent),
32 31 ui(new Ui::CatalogueSideBarWidget),
33 32 impl{spimpl::make_unique_impl<CatalogueSideBarWidgetPrivate>()}
34 33 {
35 34 ui->setupUi(this);
36 35 impl->configureTreeWidget(ui->treeWidget);
37 36
38 37 ui->treeWidget->setColumnCount(2);
39 38 ui->treeWidget->header()->setStretchLastSection(false);
40 39 ui->treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
41 40 ui->treeWidget->header()->setSectionResizeMode(0, QHeaderView::Stretch);
42 41
43 42 auto emitSelection = [this]() {
44 43
45 44 auto selectedItems = ui->treeWidget->selectedItems();
46 45 if (selectedItems.isEmpty()) {
47 46 emit this->selectionCleared();
48 47 }
49 48 else {
50 QVector<DBCatalogue> catalogues;
49 QVector<std::shared_ptr<DBCatalogue> > catalogues;
51 50 QStringList databases;
52 51 int selectionType = selectedItems.first()->type();
53 52
54 53 for (auto item : ui->treeWidget->selectedItems()) {
55 54 if (item->type() == selectionType) {
56 55 switch (selectionType) {
57 56 case CATALOGUE_ITEM_TYPE:
58 57 catalogues.append(
59 58 static_cast<CatalogueTreeWidgetItem *>(item)->catalogue());
60 59 break;
61 60 case DATABASE_ITEM_TYPE:
62 61 selectionType = DATABASE_ITEM_TYPE;
63 62 databases.append(item->text(0));
64 63 case ALL_EVENT_ITEM_TYPE: // fallthrough
65 64 case TRASH_ITEM_TYPE: // fallthrough
66 65 default:
67 66 break;
68 67 }
69 68 }
70 69 else {
71 70 // Incoherent multi selection
72 71 selectionType = -1;
73 72 break;
74 73 }
75 74 }
76 75
77 76 switch (selectionType) {
78 77 case CATALOGUE_ITEM_TYPE:
79 78 emit this->catalogueSelected(catalogues);
80 79 break;
81 80 case DATABASE_ITEM_TYPE:
82 81 emit this->databaseSelected(databases);
83 82 break;
84 83 case ALL_EVENT_ITEM_TYPE:
85 84 emit this->allEventsSelected();
86 85 break;
87 86 case TRASH_ITEM_TYPE:
88 87 emit this->trashSelected();
89 88 break;
90 89 default:
91 90 emit this->selectionCleared();
92 91 break;
93 92 }
94 93 }
95 94
96 95
97 96 };
98 97
99 98 connect(ui->treeWidget, &QTreeWidget::itemClicked, emitSelection);
100 99 connect(ui->treeWidget, &QTreeWidget::currentItemChanged, emitSelection);
101 100
102 101 ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
103 102 connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested, this,
104 103 &CatalogueSideBarWidget::onContextMenuRequested);
105 104 }
106 105
107 106 CatalogueSideBarWidget::~CatalogueSideBarWidget()
108 107 {
109 108 delete ui;
110 109 }
111 110
112 111 void CatalogueSideBarWidget::onContextMenuRequested(const QPoint &pos)
113 112 {
114 113 QMenu menu{this};
115 114
116 115 auto currentItem = ui->treeWidget->currentItem();
117 116 switch (currentItem->type()) {
118 117 case CATALOGUE_ITEM_TYPE:
119 118 menu.addAction("Rename",
120 119 [this, currentItem]() { ui->treeWidget->editItem(currentItem); });
121 120 break;
122 121 case DATABASE_ITEM_TYPE:
123 122 break;
124 123 case ALL_EVENT_ITEM_TYPE:
125 124 break;
126 125 case TRASH_ITEM_TYPE:
127 126 menu.addAction("Empty Trash", []() {
128 127 // TODO
129 128 });
130 129 break;
131 130 default:
132 131 break;
133 132 }
134 133
135 134 if (!menu.isEmpty()) {
136 135 menu.exec(ui->treeWidget->mapToGlobal(pos));
137 136 }
138 137 }
139 138
140 139 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::configureTreeWidget(
141 140 QTreeWidget *treeWidget)
142 141 {
143 142 auto allEventsItem = new QTreeWidgetItem{{"All Events"}, ALL_EVENT_ITEM_TYPE};
144 143 allEventsItem->setIcon(0, QIcon(":/icones/allEvents.png"));
145 144 treeWidget->addTopLevelItem(allEventsItem);
146 145
147 146 auto trashItem = new QTreeWidgetItem{{"Trash"}, TRASH_ITEM_TYPE};
148 147 trashItem->setIcon(0, QIcon(":/icones/trash.png"));
149 148 treeWidget->addTopLevelItem(trashItem);
150 149
151 150 auto separator = new QFrame{treeWidget};
152 151 separator->setFrameShape(QFrame::HLine);
153 152 auto separatorItem = new QTreeWidgetItem{};
154 153 separatorItem->setFlags(Qt::NoItemFlags);
155 154 treeWidget->addTopLevelItem(separatorItem);
156 155 treeWidget->setItemWidget(separatorItem, 0, separator);
157 156
158 auto &dao = sqpApp->catalogueController().getDao();
159 auto allPredicate = std::make_shared<ComparaisonPredicate>(QString{"uniqId"}, "-1",
160 ComparaisonOperation::DIFFERENT);
161
162 157 auto db = addDatabaseItem("Default", treeWidget);
163 158
164 auto catalogues = dao.getCatalogues(allPredicate);
159 auto catalogues = sqpApp->catalogueController().getCatalogues("Default");
165 160 for (auto catalogue : catalogues) {
166 161 addCatalogueItem(catalogue, db);
167 162 }
168 163
169 164 treeWidget->expandAll();
170 165 }
171 166
172 167 QTreeWidgetItem *
173 168 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addDatabaseItem(const QString &name,
174 169 QTreeWidget *treeWidget)
175 170 {
176 171 auto databaseItem = new QTreeWidgetItem{{name}, DATABASE_ITEM_TYPE};
177 172 databaseItem->setIcon(0, QIcon{":/icones/database.png"});
178 173 treeWidget->addTopLevelItem(databaseItem);
179 174
180 175 return databaseItem;
181 176 }
182 177
183 178 QTreeWidgetItem *
184 179 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getDatabaseItem(const QString &name,
185 180 QTreeWidget *treeWidget)
186 181 {
187 182 for (auto i = 0; i < treeWidget->topLevelItemCount(); ++i) {
188 183 auto item = treeWidget->topLevelItem(i);
189 184 if (item->type() == DATABASE_ITEM_TYPE && item->text(0) == name) {
190 185 return item;
191 186 }
192 187 }
193 188
194 189 return nullptr;
195 190 }
196 191
197 192 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addCatalogueItem(
198 const DBCatalogue &catalogue, QTreeWidgetItem *parentDatabaseItem)
193 const std::shared_ptr<DBCatalogue> &catalogue, QTreeWidgetItem *parentDatabaseItem)
199 194 {
200 195 auto catalogueItem = new CatalogueTreeWidgetItem{catalogue, CATALOGUE_ITEM_TYPE};
201 196 catalogueItem->setIcon(0, QIcon{":/icones/catalogue.png"});
202 197 parentDatabaseItem->addChild(catalogueItem);
203 198 }
@@ -1,90 +1,93
1 1 #include "Catalogue/CatalogueTreeWidgetItem.h"
2 2
3 3 #include <memory>
4 4
5 #include <DBCatalogue.h>
5 6 #include <QBoxLayout>
6 7 #include <QToolButton>
7 8
8 9 const auto VALIDATION_BUTTON_ICON_SIZE = 12;
9 10
10 11 struct CatalogueTreeWidgetItem::CatalogueTreeWidgetItemPrivate {
11 12
12 DBCatalogue m_Catalogue;
13 std::shared_ptr<DBCatalogue> m_Catalogue;
13 14
14 CatalogueTreeWidgetItemPrivate(DBCatalogue catalogue) : m_Catalogue(catalogue) {}
15 CatalogueTreeWidgetItemPrivate(std::shared_ptr<DBCatalogue> catalogue) : m_Catalogue(catalogue)
16 {
17 }
15 18 };
16 19
17 20
18 CatalogueTreeWidgetItem::CatalogueTreeWidgetItem(DBCatalogue catalogue, int type)
21 CatalogueTreeWidgetItem::CatalogueTreeWidgetItem(std::shared_ptr<DBCatalogue> catalogue, int type)
19 22 : QTreeWidgetItem(type),
20 23 impl{spimpl::make_unique_impl<CatalogueTreeWidgetItemPrivate>(catalogue)}
21 24 {
22 25 setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
23 26 }
24 27
25 28 QVariant CatalogueTreeWidgetItem::data(int column, int role) const
26 29 {
27 30 if (column == 0) {
28 31 switch (role) {
29 32 case Qt::EditRole: // fallthrough
30 33 case Qt::DisplayRole:
31 return impl->m_Catalogue.getName();
34 return impl->m_Catalogue->getName();
32 35 default:
33 36 break;
34 37 }
35 38 }
36 39
37 40 return QTreeWidgetItem::data(column, role);
38 41 }
39 42
40 43 void CatalogueTreeWidgetItem::setData(int column, int role, const QVariant &value)
41 44 {
42 45 if (role == Qt::EditRole && column == 0) {
43 46 auto newName = value.toString();
44 if (newName != impl->m_Catalogue.getName()) {
47 if (newName != impl->m_Catalogue->getName()) {
45 48 setText(0, newName);
46 impl->m_Catalogue.setName(newName);
49 impl->m_Catalogue->setName(newName);
47 50 setHasChanges(true);
48 51 }
49 52 }
50 53 else {
51 54 QTreeWidgetItem::setData(column, role, value);
52 55 }
53 56 }
54 57
55 DBCatalogue CatalogueTreeWidgetItem::catalogue() const
58 std::shared_ptr<DBCatalogue> CatalogueTreeWidgetItem::catalogue() const
56 59 {
57 60 return impl->m_Catalogue;
58 61 }
59 62
60 63 void CatalogueTreeWidgetItem::setHasChanges(bool value)
61 64 {
62 65 if (value) {
63 66 auto widet = new QWidget{treeWidget()};
64 67
65 68 auto layout = new QHBoxLayout{widet};
66 69 layout->setContentsMargins(0, 0, 0, 0);
67 70 layout->setSpacing(0);
68 71
69 72 auto btnValid = new QToolButton{widet};
70 73 btnValid->setIcon(QIcon{":/icones/save"});
71 74 btnValid->setIconSize(QSize{VALIDATION_BUTTON_ICON_SIZE, VALIDATION_BUTTON_ICON_SIZE});
72 75 btnValid->setAutoRaise(true);
73 76 QObject::connect(btnValid, &QToolButton::clicked, [this]() { setHasChanges(false); });
74 77 layout->addWidget(btnValid);
75 78
76 79 auto btnDiscard = new QToolButton{widet};
77 80 btnDiscard->setIcon(QIcon{":/icones/discard"});
78 81 btnDiscard->setIconSize(QSize{VALIDATION_BUTTON_ICON_SIZE, VALIDATION_BUTTON_ICON_SIZE});
79 82 btnDiscard->setAutoRaise(true);
80 83 QObject::connect(btnDiscard, &QToolButton::clicked, [this]() { setHasChanges(false); });
81 84 layout->addWidget(btnDiscard);
82 85
83 86 treeWidget()->setItemWidget(this, 1, {widet});
84 87 treeWidget()->resizeColumnToContents(1);
85 88 }
86 89 else {
87 90 // Note: the widget is destroyed
88 91 treeWidget()->setItemWidget(this, 1, nullptr);
89 92 }
90 93 }
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