From b92faa6e4b21e9660ff9e113cc0be0dcce1e911a 2017-11-28 13:37:12 From: Alexandre Leroux Date: 2017-11-28 13:37:12 Subject: [PATCH] Inits merge method of two items --- diff --git a/core/include/DataSource/DataSourceItem.h b/core/include/DataSource/DataSourceItem.h index 3b03955..84e70c0 100644 --- a/core/include/DataSource/DataSourceItem.h +++ b/core/include/DataSource/DataSourceItem.h @@ -64,6 +64,46 @@ public: /// Gets all data QVariantHash data() const noexcept; + /** + * Merge in the item the source item passed as parameter. + * + * The merge is done by adding as child of the item the complete tree represented by the source + * item. If a part of the tree already exists in the item (based on the name of the nodes), it + * is merged by completing the existing tree by items "leaves" (products, components or nodes + * with no child). + * + * For example, with item representing the tree: + * R (root node) + * - N1 (node) + * -- N11 (node) + * --- P1 (product) + * --- P2 (product) + * - N2 (node) + * + * and the source item representing the tree: + * N1 (root node) + * - N11 (node) + * -- P3 (product) + * - N12 (node) + * + * The leaves of the source item to merge into the item are N1/N11/P3 and N1/N12 => we therefore + * have the following merge result: + * R + * - N1 + * -- N11 + * --- P1 + * --- P2 + * --- P3 (added leaf) + * -- N12 (added leaf) + * + * @param item the source item + * @remarks No control is performed on products or components that are merged into the same tree + * part (two products or components may have the same name) + * @remarks the merge is made by copy (source item is not changed and still exists after the + * operation) + */ + void merge(const DataSourceItem &item); + bool isRoot() const noexcept; QString name() const noexcept; diff --git a/core/include/DataSource/DataSourceItemMergeHelper.h b/core/include/DataSource/DataSourceItemMergeHelper.h new file mode 100644 index 0000000..722da2c --- /dev/null +++ b/core/include/DataSource/DataSourceItemMergeHelper.h @@ -0,0 +1,15 @@ +#ifndef SCIQLOP_DATASOURCEITEMMERGEHELPER_H +#define SCIQLOP_DATASOURCEITEMMERGEHELPER_H + +class DataSourceItem; + +/** + * @brief The DataSourceItemMergeHelper struct is used to merge two data source items + * @sa DataSourceItem::merge() + */ +struct DataSourceItemMergeHelper { + /// Merges source item into dest item + static void merge(const DataSourceItem &source, DataSourceItem &dest); +}; + +#endif // SCIQLOP_DATASOURCEITEMMERGEHELPER_H diff --git a/core/meson.build b/core/meson.build index 11a1fb6..864624b 100644 --- a/core/meson.build +++ b/core/meson.build @@ -31,6 +31,7 @@ core_sources = [ 'src/DataSource/DataSourceController.cpp', 'src/DataSource/DataSourceItem.cpp', 'src/DataSource/DataSourceItemAction.cpp', + 'src/DataSource/DataSourceItemMergeHelper.cpp', 'src/Network/NetworkController.cpp', 'src/Plugin/PluginManager.cpp', 'src/Settings/SqpSettingsDefs.cpp', diff --git a/core/src/DataSource/DataSourceItem.cpp b/core/src/DataSource/DataSourceItem.cpp index 35972b7..a0a80a5 100644 --- a/core/src/DataSource/DataSourceItem.cpp +++ b/core/src/DataSource/DataSourceItem.cpp @@ -1,5 +1,6 @@ #include #include +#include #include @@ -75,6 +76,11 @@ QVariantHash DataSourceItem::data() const noexcept return impl->m_Data; } +void DataSourceItem::merge(const DataSourceItem &item) +{ + DataSourceItemMergeHelper::merge(item, *this); +} + bool DataSourceItem::isRoot() const noexcept { return impl->m_Parent == nullptr; diff --git a/core/src/DataSource/DataSourceItemMergeHelper.cpp b/core/src/DataSource/DataSourceItemMergeHelper.cpp new file mode 100644 index 0000000..36a8192 --- /dev/null +++ b/core/src/DataSource/DataSourceItemMergeHelper.cpp @@ -0,0 +1,8 @@ +#include "DataSource/DataSourceItemMergeHelper.h" + +#include + +void DataSourceItemMergeHelper::merge(const DataSourceItem &source, DataSourceItem &dest) +{ + /// @todo ALX +}