##// END OF EJS Templates
Implements merge method (1)
Alexandre Leroux -
r1072:bf587f8540dd
parent child
Show More
@@ -2,7 +2,54
2 2
3 3 #include <DataSource/DataSourceItem.h>
4 4
5 namespace {
6
7 /**
8 * Finds in a tree an item similar to the item passed in parameter
9 * @param item the item for which to find a similar item
10 * @param root the root item of the tree
11 * @return the similar item if found, nullptr otherwise
12 */
13 DataSourceItem *findSimilarItem(const DataSourceItem &item, const DataSourceItem &root)
14 {
15 // An item is considered similar to the another item if:
16 // - the items are both nodes AND
17 // - the names of the items are identical
18
19 if (item.type() != DataSourceItemType::NODE) {
20 return nullptr;
21 }
22
23 DataSourceItem *result{nullptr};
24 bool found{false};
25 for (auto i = 0, count = root.childCount(); i < count && !found; ++i) {
26 auto child = root.child(i);
27
28 found = child->type() == DataSourceItemType::NODE
29 && QString::compare(child->name(), item.name(), Qt::CaseInsensitive) == 0;
30 if (found) {
31 result = child;
32 }
33 }
34
35 return result;
36 }
37
38 } // namespace
39
5 40 void DataSourceItemMergeHelper::merge(const DataSourceItem &source, DataSourceItem &dest)
6 41 {
7 /// @todo ALX
42 // Checks if the source item can be merged into the destination item (i.e. there is a child item
43 // similar to the source item)
44 if (auto subItem = findSimilarItem(source, dest)) {
45 // If there is an item similar to the source item, applies the merge recursively
46 for (auto i = 0, count = source.childCount(); i < count; ++i) {
47 merge(*source.child(i), *subItem);
48 }
49 }
50 else {
51 // If no item is similar to the source item, the item is copied as the child of the
52 // destination item
53 dest.appendChild(source.clone());
54 }
8 55 }
General Comments 0
You need to be logged in to leave comments. Login now