##// END OF EJS Templates
Merge pull request 150 from SCIQLOP-Initialisation develop...
perrinel -
r154:42e91aee0749 merge
parent child
Show More
@@ -0,0 +1,48
1 #ifndef SCIQLOP_DATASOURCEITEMACTION_H
2 #define SCIQLOP_DATASOURCEITEMACTION_H
3
4 #include <Common/spimpl.h>
5
6 #include <QLoggingCategory>
7 #include <QObject>
8
9 Q_DECLARE_LOGGING_CATEGORY(LOG_DataSourceItemAction)
10
11 class DataSourceItem;
12
13 /**
14 * @brief The DataSourceItemAction class represents an action on a data source item.
15 *
16 * An action is a function that will be executed when the slot execute() is called.
17 */
18 class DataSourceItemAction : public QObject {
19
20 Q_OBJECT
21
22 public:
23 /// Signature of the function associated to the action
24 using ExecuteFunction = std::function<void(DataSourceItem &dataSourceItem)>;
25
26 /**
27 * Ctor
28 * @param name the name of the action
29 * @param fun the function that will be called when the action is executed
30 * @sa execute()
31 */
32 explicit DataSourceItemAction(const QString &name, ExecuteFunction fun);
33
34 QString name() const noexcept;
35
36 /// Sets the data source item concerned by the action
37 void setDataSourceItem(DataSourceItem *dataSourceItem) noexcept;
38
39 public slots:
40 /// Executes the action
41 void execute();
42
43 private:
44 class DataSourceItemActionPrivate;
45 spimpl::unique_impl_ptr<DataSourceItemActionPrivate> impl;
46 };
47
48 #endif // SCIQLOP_DATASOURCEITEMACTION_H
@@ -0,0 +1,42
1 #include <DataSource/DataSourceItemAction.h>
2
3 Q_LOGGING_CATEGORY(LOG_DataSourceItemAction, "DataSourceItemAction")
4
5 struct DataSourceItemAction::DataSourceItemActionPrivate {
6 explicit DataSourceItemActionPrivate(const QString &name,
7 DataSourceItemAction::ExecuteFunction fun)
8 : m_Name{name}, m_Fun{std::move(fun)}, m_DataSourceItem{nullptr}
9 {
10 }
11
12 QString m_Name;
13 DataSourceItemAction::ExecuteFunction m_Fun;
14 /// Item associated to the action (can be null, in which case the action will not be executed)
15 DataSourceItem *m_DataSourceItem;
16 };
17
18 DataSourceItemAction::DataSourceItemAction(const QString &name, ExecuteFunction fun)
19 : impl{spimpl::make_unique_impl<DataSourceItemActionPrivate>(name, std::move(fun))}
20 {
21 }
22
23 QString DataSourceItemAction::name() const noexcept
24 {
25 return impl->m_Name;
26 }
27
28 void DataSourceItemAction::setDataSourceItem(DataSourceItem *dataSourceItem) noexcept
29 {
30 impl->m_DataSourceItem = dataSourceItem;
31 }
32
33 void DataSourceItemAction::execute()
34 {
35 if (impl->m_DataSourceItem) {
36 impl->m_Fun(*impl->m_DataSourceItem);
37 }
38 else {
39 qCDebug(LOG_DataSourceItemAction())
40 << tr("Can't execute action : no item has been associated");
41 }
42 }
@@ -1,73 +1,79
1 1 #ifndef SCIQLOP_DATASOURCECONTROLLER_H
2 2 #define SCIQLOP_DATASOURCECONTROLLER_H
3 3
4 4 #include <QLoggingCategory>
5 5 #include <QObject>
6 6 #include <QUuid>
7 7
8 8 #include <Common/spimpl.h>
9 9
10 10 Q_DECLARE_LOGGING_CATEGORY(LOG_DataSourceController)
11 11
12 12 class DataSourceItem;
13 13 class IDataProvider;
14 14
15 15 /**
16 16 * @brief The DataSourceController class aims to make the link between SciQlop and its plugins. This
17 17 * is the intermediate class that SciQlop has to use in the way to connect a data source. Please
18 18 * first use register method to initialize a plugin specified by its metadata name (JSON plugin
19 19 * source) then others specifics method will be able to access it. You can load a data source driver
20 20 * plugin then create a data source.
21 21 */
22 22 class DataSourceController : public QObject {
23 23 Q_OBJECT
24 24 public:
25 25 explicit DataSourceController(QObject *parent = 0);
26 26 virtual ~DataSourceController();
27 27
28 28 /**
29 29 * Registers a data source. The method delivers a unique id that can be used afterwards to
30 30 * access to the data source properties (structure, connection parameters, data provider, etc.)
31 31 * @param dataSourceName the name of the data source
32 32 * @return the unique id with which the data source has been registered
33 33 */
34 34 QUuid registerDataSource(const QString &dataSourceName) noexcept;
35 35
36 36 /**
37 37 * Sets the structure of a data source. The controller takes ownership of the structure.
38 38 * @param dataSourceUid the unique id with which the data source has been registered into the
39 39 * controller. If it is invalid, the method has no effect.
40 40 * @param dataSourceItem the structure of the data source
41 41 * @sa registerDataSource()
42 42 */
43 43 void setDataSourceItem(const QUuid &dataSourceUid,
44 44 std::unique_ptr<DataSourceItem> dataSourceItem) noexcept;
45 45
46 46 /**
47 47 * Sets the data provider used to retrieve data from of a data source. The controller takes
48 48 * ownership of the provider.
49 49 * @param dataSourceUid the unique id with which the data source has been registered into the
50 50 * controller. If it is invalid, the method has no effect.
51 51 * @param dataProvider the provider of the data source
52 52 * @sa registerDataSource()
53 53 */
54 54 void setDataProvider(const QUuid &dataSourceUid,
55 55 std::unique_ptr<IDataProvider> dataProvider) noexcept;
56 56
57 /**
58 * Loads an item (product) as a variable in SciQlop
59 * @param productItem the item to load
60 */
61 void loadProductItem(const DataSourceItem &productItem) noexcept;
62
57 63 public slots:
58 64 /// Manage init/end of the controller
59 65 void initialize();
60 66 void finalize();
61 67
62 68 signals:
63 69 /// Signal emitted when a structure has been set for a data source
64 70 void dataSourceItemSet(DataSourceItem *dataSourceItem);
65 71
66 72 private:
67 73 void waitForFinish();
68 74
69 75 class DataSourceControllerPrivate;
70 76 spimpl::unique_impl_ptr<DataSourceControllerPrivate> impl;
71 77 };
72 78
73 79 #endif // SCIQLOP_DATASOURCECONTROLLER_H
@@ -1,59 +1,73
1 1 #ifndef SCIQLOP_DATASOURCEITEM_H
2 2 #define SCIQLOP_DATASOURCEITEM_H
3 3
4 4 #include <Common/spimpl.h>
5 5
6 6 #include <QVariant>
7 7 #include <QVector>
8 8
9 class DataSourceItemAction;
10
9 11 /**
10 12 * Possible types of an item
11 13 */
12 14 enum class DataSourceItemType { NODE, PRODUCT };
13 15
14 16 /**
15 17 * @brief The DataSourceItem class aims to represent a structure element of a data source.
16 18 * A data source has a tree structure that is made up of a main DataSourceItem object (root)
17 19 * containing other DataSourceItem objects (children).
18 20 * For each DataSourceItem can be associated a set of data representing it.
19 21 */
20 22 class DataSourceItem {
21 23 public:
22 24 explicit DataSourceItem(DataSourceItemType type, QVector<QVariant> data = {});
23 25
26 /// @return the actions of the item as a vector
27 QVector<DataSourceItemAction *> actions() const noexcept;
28
29 /**
30 * Adds an action to the item. The item takes ownership of the action, and the action is
31 * automatically associated to the item
32 * @param action the action to add
33 */
34 void addAction(std::unique_ptr<DataSourceItemAction> action) noexcept;
35
24 36 /**
25 37 * Adds a child to the item. The item takes ownership of the child.
26 38 * @param child the child to add
27 39 */
28 40 void appendChild(std::unique_ptr<DataSourceItem> child) noexcept;
29 41
30 42 /**
31 43 * Returns the item's child associated to an index
32 44 * @param childIndex the index to search
33 45 * @return a pointer to the child if index is valid, nullptr otherwise
34 46 */
35 47 DataSourceItem *child(int childIndex) const noexcept;
36 48
37 49 int childCount() const noexcept;
38 50
39 51 /**
40 52 * Get the data associated to an index
41 53 * @param dataIndex the index to search
42 54 * @return the data found if index is valid, default QVariant otherwise
43 55 */
44 56 QVariant data(int dataIndex) const noexcept;
45 57
58 QString name() const noexcept;
59
46 60 /**
47 61 * Get the item's parent
48 62 * @return a pointer to the parent if it exists, nullptr if the item is a root
49 63 */
50 64 DataSourceItem *parentItem() const noexcept;
51 65
52 66 DataSourceItemType type() const noexcept;
53 67
54 68 private:
55 69 class DataSourceItemPrivate;
56 70 spimpl::unique_impl_ptr<DataSourceItemPrivate> impl;
57 71 };
58 72
59 73 #endif // SCIQLOP_DATASOURCEITEMMODEL_H
@@ -1,95 +1,100
1 1 #include <DataSource/DataSourceController.h>
2 2 #include <DataSource/DataSourceItem.h>
3 3
4 4 #include <Data/IDataProvider.h>
5 5
6 6 #include <QMutex>
7 7 #include <QThread>
8 8
9 9 #include <QDir>
10 10 #include <QStandardPaths>
11 11
12 12 Q_LOGGING_CATEGORY(LOG_DataSourceController, "DataSourceController")
13 13
14 14 class DataSourceController::DataSourceControllerPrivate {
15 15 public:
16 16 QMutex m_WorkingMutex;
17 17 /// Data sources registered
18 18 QHash<QUuid, QString> m_DataSources;
19 19 /// Data sources structures
20 20 std::map<QUuid, std::unique_ptr<DataSourceItem> > m_DataSourceItems;
21 21 /// Data providers registered
22 22 std::map<QUuid, std::unique_ptr<IDataProvider> > m_DataProviders;
23 23 };
24 24
25 25 DataSourceController::DataSourceController(QObject *parent)
26 26 : impl{spimpl::make_unique_impl<DataSourceControllerPrivate>()}
27 27 {
28 28 qCDebug(LOG_DataSourceController()) << tr("DataSourceController construction")
29 29 << QThread::currentThread();
30 30 }
31 31
32 32 DataSourceController::~DataSourceController()
33 33 {
34 34 qCDebug(LOG_DataSourceController()) << tr("DataSourceController destruction")
35 35 << QThread::currentThread();
36 36 this->waitForFinish();
37 37 }
38 38
39 39 QUuid DataSourceController::registerDataSource(const QString &dataSourceName) noexcept
40 40 {
41 41 auto dataSourceUid = QUuid::createUuid();
42 42 impl->m_DataSources.insert(dataSourceUid, dataSourceName);
43 43
44 44 return dataSourceUid;
45 45 }
46 46
47 47 void DataSourceController::setDataSourceItem(
48 48 const QUuid &dataSourceUid, std::unique_ptr<DataSourceItem> dataSourceItem) noexcept
49 49 {
50 50 if (impl->m_DataSources.contains(dataSourceUid)) {
51 51 impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem)));
52 52
53 53 // Retrieves the data source item to emit the signal with it
54 54 auto it = impl->m_DataSourceItems.find(dataSourceUid);
55 55 if (it != impl->m_DataSourceItems.end()) {
56 56 emit dataSourceItemSet(it->second.get());
57 57 }
58 58 }
59 59 else {
60 60 qCWarning(LOG_DataSourceController()) << tr("Can't set data source item for uid %1 : no "
61 61 "data source has been registered with the uid")
62 62 .arg(dataSourceUid.toString());
63 63 }
64 64 }
65 65
66 66 void DataSourceController::setDataProvider(const QUuid &dataSourceUid,
67 67 std::unique_ptr<IDataProvider> dataProvider) noexcept
68 68 {
69 69 if (impl->m_DataSources.contains(dataSourceUid)) {
70 70 impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider)));
71 71 }
72 72 else {
73 73 qCWarning(LOG_DataSourceController()) << tr("Can't set data provider for uid %1 : no data "
74 74 "source has been registered with the uid")
75 75 .arg(dataSourceUid.toString());
76 76 }
77 77 }
78 78
79 void DataSourceController::loadProductItem(const DataSourceItem &productItem) noexcept
80 {
81 /// @todo ALX
82 }
83
79 84 void DataSourceController::initialize()
80 85 {
81 86 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init")
82 87 << QThread::currentThread();
83 88 impl->m_WorkingMutex.lock();
84 89 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END");
85 90 }
86 91
87 92 void DataSourceController::finalize()
88 93 {
89 94 impl->m_WorkingMutex.unlock();
90 95 }
91 96
92 97 void DataSourceController::waitForFinish()
93 98 {
94 99 QMutexLocker locker{&impl->m_WorkingMutex};
95 100 }
@@ -1,56 +1,86
1 1 #include <DataSource/DataSourceItem.h>
2 #include <DataSource/DataSourceItemAction.h>
2 3
3 4 #include <QVector>
4 5
6 namespace {
7
8 /// Index of the 'name' value in the item
9 const auto NAME_INDEX = 0;
10
11 } // namespace
12
5 13 struct DataSourceItem::DataSourceItemPrivate {
6 14 explicit DataSourceItemPrivate(DataSourceItemType type, QVector<QVariant> data)
7 : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}
15 : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{}
8 16 {
9 17 }
10 18
11 19 DataSourceItem *m_Parent;
12 20 std::vector<std::unique_ptr<DataSourceItem> > m_Children;
13 21 DataSourceItemType m_Type;
14 22 QVector<QVariant> m_Data;
23 std::vector<std::unique_ptr<DataSourceItemAction> > m_Actions;
15 24 };
16 25
17 26 DataSourceItem::DataSourceItem(DataSourceItemType type, QVector<QVariant> data)
18 27 : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(type, std::move(data))}
19 28 {
20 29 }
21 30
31 QVector<DataSourceItemAction *> DataSourceItem::actions() const noexcept
32 {
33 QVector<DataSourceItemAction *> result{};
34
35 std::transform(std::cbegin(impl->m_Actions), std::cend(impl->m_Actions),
36 std::back_inserter(result), [](const auto &action) { return action.get(); });
37
38 return result;
39 }
40
41 void DataSourceItem::addAction(std::unique_ptr<DataSourceItemAction> action) noexcept
42 {
43 action->setDataSourceItem(this);
44 impl->m_Actions.push_back(std::move(action));
45 }
46
22 47 void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept
23 48 {
24 49 child->impl->m_Parent = this;
25 50 impl->m_Children.push_back(std::move(child));
26 51 }
27 52
28 53 DataSourceItem *DataSourceItem::child(int childIndex) const noexcept
29 54 {
30 55 if (childIndex < 0 || childIndex >= childCount()) {
31 56 return nullptr;
32 57 }
33 58 else {
34 59 return impl->m_Children.at(childIndex).get();
35 60 }
36 61 }
37 62
38 63 int DataSourceItem::childCount() const noexcept
39 64 {
40 65 return impl->m_Children.size();
41 66 }
42 67
43 68 QVariant DataSourceItem::data(int dataIndex) const noexcept
44 69 {
45 70 return impl->m_Data.value(dataIndex);
46 71 }
47 72
73 QString DataSourceItem::name() const noexcept
74 {
75 return data(NAME_INDEX).toString();
76 }
77
48 78 DataSourceItem *DataSourceItem::parentItem() const noexcept
49 79 {
50 80 return impl->m_Parent;
51 81 }
52 82
53 83 DataSourceItemType DataSourceItem::type() const noexcept
54 84 {
55 85 return impl->m_Type;
56 86 }
@@ -1,10 +1,13
1 1 # On ignore toutes les règles vera++ pour le fichier spimpl
2 2 Common/spimpl\.h:\d+:.*
3 3
4 4 # Ignore false positive relative to two class definitions in a same file
5 5 DataSourceItem\.h:\d+:.*IPSIS_S01.*
6 6
7 7 # Ignore false positive relative to a template class
8 8 ArrayData\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (D)
9 9 ArrayData\.h:\d+:.*IPSIS_S06.*found: (D)
10 10 ArrayData\.h:\d+:.*IPSIS_S06.*found: (Dim)
11
12 # Ignore false positive relative to an alias
13 DataSourceItemAction\.h:\d+:.*IPSIS_S06.*found: (ExecuteFunction)
@@ -1,32 +1,35
1 1 #ifndef SCIQLOP_DATASOURCETREEWIDGETITEM_H
2 2 #define SCIQLOP_DATASOURCETREEWIDGETITEM_H
3 3
4 4 #include <Common/spimpl.h>
5 5
6 6 #include <QLoggingCategory>
7 7 #include <QTreeWidgetItem>
8 8
9 9 Q_DECLARE_LOGGING_CATEGORY(LOG_DataSourceTreeWidgetItem)
10 10
11 11 class DataSourceItem;
12 12
13 13 /**
14 14 * @brief The DataSourceTreeWidgetItem is the graphical representation of a data source item. It is
15 15 * intended to be displayed in a QTreeWidget.
16 16 * @sa DataSourceItem
17 17 */
18 18 class DataSourceTreeWidgetItem : public QTreeWidgetItem {
19 19 public:
20 20 explicit DataSourceTreeWidgetItem(const DataSourceItem *data, int type = Type);
21 21 explicit DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data,
22 22 int type = Type);
23 23
24 24 virtual QVariant data(int column, int role) const override;
25 25 virtual void setData(int column, int role, const QVariant &value) override;
26 26
27 /// @return the actions associated to the item
28 QList<QAction *> actions() const noexcept;
29
27 30 private:
28 31 class DataSourceTreeWidgetItemPrivate;
29 32 spimpl::unique_impl_ptr<DataSourceTreeWidgetItemPrivate> impl;
30 33 };
31 34
32 35 #endif // SCIQLOP_DATASOURCETREEWIDGETITEM_H
@@ -1,34 +1,38
1 1 #ifndef SCIQLOP_DATASOURCEWIDGET_H
2 2 #define SCIQLOP_DATASOURCEWIDGET_H
3 3
4 4 #include <QWidget>
5 5
6 6 namespace Ui {
7 7 class DataSourceWidget;
8 8 } // Ui
9 9
10 10 class DataSourceItem;
11 11
12 12 /**
13 13 * @brief The DataSourceWidget handles the graphical representation (as a tree) of the data sources
14 14 * attached to SciQlop.
15 15 */
16 16 class DataSourceWidget : public QWidget {
17 17 Q_OBJECT
18 18
19 19 public:
20 20 explicit DataSourceWidget(QWidget *parent = 0);
21 21
22 22 public slots:
23 23 /**
24 24 * Adds a data source. An item associated to the data source is created and then added to the
25 25 * representation tree
26 26 * @param dataSource the data source to add. The pointer has to be not null
27 27 */
28 28 void addDataSource(DataSourceItem *dataSource) noexcept;
29 29
30 30 private:
31 31 Ui::DataSourceWidget *ui;
32
33 private slots:
34 /// Slot called when right clicking on an item in the tree (displays a menu)
35 void onTreeMenuRequested(const QPoint &pos) noexcept;
32 36 };
33 37
34 38 #endif // SCIQLOP_DATASOURCEWIDGET_H
@@ -1,75 +1,101
1 1 #include <DataSource/DataSourceItem.h>
2 #include <DataSource/DataSourceItemAction.h>
2 3 #include <DataSource/DataSourceTreeWidgetItem.h>
3 4
4 5 #include <SqpApplication.h>
5 6
7 #include <QAction>
8
6 9 Q_LOGGING_CATEGORY(LOG_DataSourceTreeWidgetItem, "DataSourceTreeWidgetItem")
7 10
8 11 namespace {
9 12
10 13 QIcon itemIcon(const DataSourceItem *dataSource)
11 14 {
12 15 if (dataSource) {
13 16 auto dataSourceType = dataSource->type();
14 17 switch (dataSourceType) {
15 18 case DataSourceItemType::NODE:
16 19 return sqpApp->style()->standardIcon(QStyle::SP_DirIcon);
17 20 case DataSourceItemType::PRODUCT:
18 21 return sqpApp->style()->standardIcon(QStyle::SP_FileIcon);
19 22 default:
20 23 // No action
21 24 break;
22 25 }
23 26
24 27 qCWarning(LOG_DataSourceTreeWidgetItem())
25 28 << QObject::tr("Can't set data source icon : unknown data source type");
26 29 }
27 30 else {
28 31 qCWarning(LOG_DataSourceTreeWidgetItem())
29 32 << QObject::tr("Can't set data source icon : the data source is null");
30 33 }
31 34
32 35 // Default cases
33 36 return QIcon{};
34 37 }
35 38
36 39 } // namespace
37 40
38 41 struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate {
39 42 explicit DataSourceTreeWidgetItemPrivate(const DataSourceItem *data) : m_Data{data} {}
40 43
41 44 /// Model used to retrieve data source information
42 45 const DataSourceItem *m_Data;
46 /// Actions associated to the item. The parent of the item (QTreeWidget) takes the ownership of
47 /// the actions
48 QList<QAction *> m_Actions;
43 49 };
44 50
45 51 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(const DataSourceItem *data, int type)
46 52 : DataSourceTreeWidgetItem{nullptr, data, type}
47 53 {
48 54 }
49 55
50 56 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data,
51 57 int type)
52 58 : QTreeWidgetItem{parent, type},
53 59 impl{spimpl::make_unique_impl<DataSourceTreeWidgetItemPrivate>(data)}
54 60 {
55 61 // Sets the icon depending on the data source
56 62 setIcon(0, itemIcon(impl->m_Data));
63
64 // Generates tree actions based on the item actions
65 auto createTreeAction = [this, &parent](const auto &itemAction) {
66 auto treeAction = new QAction{itemAction->name(), parent};
67
68 // Executes item action when tree action is triggered
69 QObject::connect(treeAction, &QAction::triggered, itemAction,
70 &DataSourceItemAction::execute);
71
72 return treeAction;
73 };
74
75 auto itemActions = impl->m_Data->actions();
76 std::transform(std::cbegin(itemActions), std::cend(itemActions),
77 std::back_inserter(impl->m_Actions), createTreeAction);
57 78 }
58 79
59 80 QVariant DataSourceTreeWidgetItem::data(int column, int role) const
60 81 {
61 82 if (role == Qt::DisplayRole) {
62 83 return (impl->m_Data) ? impl->m_Data->data(column) : QVariant{};
63 84 }
64 85 else {
65 86 return QTreeWidgetItem::data(column, role);
66 87 }
67 88 }
68 89
69 90 void DataSourceTreeWidgetItem::setData(int column, int role, const QVariant &value)
70 91 {
71 92 // Data can't be changed by edition
72 93 if (role != Qt::EditRole) {
73 94 QTreeWidgetItem::setData(column, role, value);
74 95 }
75 96 }
97
98 QList<QAction *> DataSourceTreeWidgetItem::actions() const noexcept
99 {
100 return impl->m_Actions;
101 }
@@ -1,52 +1,72
1 1 #include <DataSource/DataSourceWidget.h>
2 2
3 3 #include <ui_DataSourceWidget.h>
4 4
5 5 #include <DataSource/DataSourceItem.h>
6 6 #include <DataSource/DataSourceTreeWidgetItem.h>
7 7
8 #include <QMenu>
9
8 10 namespace {
9 11
10 12 /// Number of columns displayed in the tree
11 13 const auto TREE_NB_COLUMNS = 1;
12 14
13 15 /// Header labels for the tree
14 16 const auto TREE_HEADER_LABELS = QStringList{QObject::tr("Name")};
15 17
16 18 /**
17 19 * Creates the item associated to a data source
18 20 * @param dataSource the data source for which to create the item
19 21 * @return the new item
20 22 */
21 23 DataSourceTreeWidgetItem *createTreeWidgetItem(DataSourceItem *dataSource)
22 24 {
23 25 // Creates item for the data source
24 26 auto item = new DataSourceTreeWidgetItem{dataSource};
25 27
26 28 // Generates items for the children of the data source
27 29 for (auto i = 0; i < dataSource->childCount(); ++i) {
28 30 item->addChild(createTreeWidgetItem(dataSource->child(i)));
29 31 }
30 32
31 33 return item;
32 34 }
33 35
34 36 } // namespace
35 37
36 38 DataSourceWidget::DataSourceWidget(QWidget *parent) : QWidget{parent}, ui{new Ui::DataSourceWidget}
37 39 {
38 40 ui->setupUi(this);
39 41
40 42 // Set tree properties
41 43 ui->treeWidget->setColumnCount(TREE_NB_COLUMNS);
42 44 ui->treeWidget->setHeaderLabels(TREE_HEADER_LABELS);
45 ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
46
47 // Connection to show a menu when right clicking on the tree
48 connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested, this,
49 &DataSourceWidget::onTreeMenuRequested);
43 50 }
44 51
45 52 void DataSourceWidget::addDataSource(DataSourceItem *dataSource) noexcept
46 53 {
47 54 // Creates the item associated to the source and adds it to the tree widget. The tree widget
48 55 // takes the ownership of the item
49 56 if (dataSource) {
50 57 ui->treeWidget->addTopLevelItem(createTreeWidgetItem(dataSource));
51 58 }
52 59 }
60
61 void DataSourceWidget::onTreeMenuRequested(const QPoint &pos) noexcept
62 {
63 // Retrieves the selected item in the tree, and build the menu from its actions
64 if (auto selectedItem = dynamic_cast<DataSourceTreeWidgetItem *>(ui->treeWidget->itemAt(pos))) {
65 QMenu treeMenu{};
66 treeMenu.addActions(selectedItem->actions());
67
68 if (!treeMenu.isEmpty()) {
69 treeMenu.exec(mapToGlobal(pos));
70 }
71 }
72 }
@@ -1,67 +1,79
1 1 #include "MockPlugin.h"
2 2 #include "CosinusProvider.h"
3 3
4 4 #include <DataSource/DataSourceController.h>
5 5 #include <DataSource/DataSourceItem.h>
6 #include <DataSource/DataSourceItemAction.h>
6 7
7 8 #include <SqpApplication.h>
8 9
9 10 Q_LOGGING_CATEGORY(LOG_MockPlugin, "MockPlugin")
10 11
11 12 namespace {
12 13
13 14 /// Name of the data source
14 15 const auto DATA_SOURCE_NAME = QStringLiteral("MMS");
15 16
16 17 /// Creates the data provider relative to the plugin
17 18 std::unique_ptr<IDataProvider> createDataProvider() noexcept
18 19 {
19 20 return std::make_unique<CosinusProvider>();
20 21 }
21 22
23 std::unique_ptr<DataSourceItem> createProductItem(const QString &productName)
24 {
25 auto result = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT,
26 QVector<QVariant>{productName});
27
28 // Add action to load product from DataSourceController
29 result->addAction(std::make_unique<DataSourceItemAction>(
30 QObject::tr("Load %1 product").arg(productName), [productName](DataSourceItem &item) {
31 if (auto app = sqpApp) {
32 app->dataSourceController().loadProductItem(item);
33 }
34 }));
35
36 return result;
37 }
38
22 39 /// Creates the data source item relative to the plugin
23 40 std::unique_ptr<DataSourceItem> createDataSourceItem() noexcept
24 41 {
25 42 // Magnetic field products
26 auto fgmProduct = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT,
27 QVector<QVariant>{QStringLiteral("FGM")});
28 auto scProduct = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT,
29 QVector<QVariant>{QStringLiteral("SC")});
30
31 43 auto magneticFieldFolder = std::make_unique<DataSourceItem>(
32 44 DataSourceItemType::NODE, QVector<QVariant>{QStringLiteral("Magnetic field")});
33 magneticFieldFolder->appendChild(std::move(fgmProduct));
34 magneticFieldFolder->appendChild(std::move(scProduct));
45 magneticFieldFolder->appendChild(createProductItem(QStringLiteral("FGM")));
46 magneticFieldFolder->appendChild(createProductItem(QStringLiteral("SC")));
35 47
36 48 // Electric field products
37 49 auto electricFieldFolder = std::make_unique<DataSourceItem>(
38 50 DataSourceItemType::NODE, QVector<QVariant>{QStringLiteral("Electric field")});
39 51
40 52 // Root
41 53 auto root = std::make_unique<DataSourceItem>(DataSourceItemType::NODE,
42 54 QVector<QVariant>{DATA_SOURCE_NAME});
43 55 root->appendChild(std::move(magneticFieldFolder));
44 56 root->appendChild(std::move(electricFieldFolder));
45 57
46 58 return root;
47 59 }
48 60
49 61 } // namespace
50 62
51 63 void MockPlugin::initialize()
52 64 {
53 65 if (auto app = sqpApp) {
54 66 // Registers to the data source controller
55 67 auto &dataSourceController = app->dataSourceController();
56 68 auto dataSourceUid = dataSourceController.registerDataSource(DATA_SOURCE_NAME);
57 69
58 70 // Sets data source tree
59 71 dataSourceController.setDataSourceItem(dataSourceUid, createDataSourceItem());
60 72
61 73 // Sets data provider
62 74 dataSourceController.setDataProvider(dataSourceUid, createDataProvider());
63 75 }
64 76 else {
65 77 qCWarning(LOG_MockPlugin()) << tr("Can't access to SciQlop application");
66 78 }
67 79 }
General Comments 0
You need to be logged in to leave comments. Login now