# # # patch "src/model/Branches.cpp" # from [9270c6f1e10826443f3421a872cfe5061be6a4ff] # to [1eaf050a54f1f9cd2586212c4a0054fd28ac6b22] # # patch "src/util/TreeBuilder.cpp" # from [c4d4da986645b777ab83d135c3673dc042993d58] # to [974da1b2402a50df3b002069411b67b0915f870b] # # patch "src/util/TreeBuilder.h" # from [1996056b8130b1e46a5ad587ae50e83e956b7cc4] # to [a4acd39e265420a666b119b38d2f32b926ab85a9] # ============================================================ --- src/model/Branches.cpp 9270c6f1e10826443f3421a872cfe5061be6a4ff +++ src/model/Branches.cpp 1eaf050a54f1f9cd2586212c4a0054fd28ac6b22 @@ -52,10 +52,10 @@ void Branches::processTaskResult(const M } branches.setHorizontalHeaderItem(0, new QStandardItem(tr("Branches"))); - if(tree) + if (tree) { - QStandardItem * root = new QStandardItem("*"); - root->setData("*", Qt::ToolTipRole); + QStandardItem * root = new QStandardItem(); + root->setData("*", Qt::DisplayRole); branches.appendRow(root); builder = new TreeBuilder(root, this); @@ -63,18 +63,17 @@ void Branches::processTaskResult(const M } else { - QStringList branchList = task.getOutputUtf8().split("\n"); - QStringListIterator iterator(branchList); - while(iterator.hasNext()) + QStringList branchList = task.getOutputUtf8() + .split("\n", QString::SkipEmptyParts); + foreach (QString branch, branchList) { - QString it = iterator.next(); - QStandardItem * item = new QStandardItem(it); - item->setData(it, Qt::ToolTipRole); + QStandardItem * item = new QStandardItem(); + item->setData(branch, Qt::DisplayRole); branches.appendRow(item); } } - reset(); + reset(); emit branchesRead(); } @@ -132,13 +131,13 @@ QModelIndex Branches::parent(const QMode return QModelIndex(); } -QStandardItem * Branches::getBranchItem(int row, int column, const QModelIndex & index) const +QStandardItem * Branches::getBranchItem(int row, int column, const QModelIndex & parent) const { - if (index.isValid()) + if (parent.isValid()) { - return static_cast(index.internalPointer())->child(row, column); + return static_cast(parent.internalPointer())->child(row, column); } - return 0; + return branches.item(row, column); } QStandardItem * Branches::branchFromIndex(const QModelIndex & index) const ============================================================ --- src/util/TreeBuilder.cpp c4d4da986645b777ab83d135c3673dc042993d58 +++ src/util/TreeBuilder.cpp 974da1b2402a50df3b002069411b67b0915f870b @@ -22,132 +22,138 @@ #include #include +TreeBuilder::TreeBuilder(QStandardItem * r, QObject * parent) + : QObject(parent), root(r), sep('.') {} -TreeBuilder::TreeBuilder(QStandardItem *root, QObject *parent) : QObject(parent) -{ - TreeBuilder::root = root; - sep = '.'; -} +TreeBuilder::~TreeBuilder() {}; -QStandardItem* TreeBuilder::add(const QString &branch) +QStandardItem * TreeBuilder::add(const QString & branch) { - if(branch != "") - return add(branch, root); - else - return NULL; + if (branch != "") + { + return add(branch, root); + } + return NULL; } -void TreeBuilder::addData(QStandardItem *child) +void TreeBuilder::addData(QStandardItem * child) { - QString branch; - QStandardItem* item = child; - QString text = child->data().toString(); - if(text.right(1) == QString(sep)) - text = text.left(text.length() - 1) + "*"; - child->setText(text); + QString branch; + QStandardItem * item = child; + QString text = child->data().toString(); - while(item) - { - branch = item->data().toString() + branch; - item = item->parent(); - } + if (text.right(1) == QString(sep)) + { + text = text.left(text.length() - 1) + "*"; + } + child->setText(text); - if(branch.right(1) == QString(sep)) - branch = branch.left(branch.length() - 1) + "*"; - child->setData(branch, Qt::ToolTipRole); + while (item) + { + branch = item->data().toString() + branch; + item = item->parent(); + } + + if (branch.right(1) == QString(sep)) + { + branch = branch.left(branch.length() - 1) + "*"; + } + + child->setData(branch, Qt::ToolTipRole); } -QStandardItem* TreeBuilder::add(const QString &branch, QStandardItem *parent) +QStandardItem * TreeBuilder::add(const QString & branch, QStandardItem * parent) { - if(branch == "") return NULL; + if (branch == "") + { + return NULL; + } - int len = parent->rowCount(); - for(int index = 0; index < len; index++) - { - int pos = overlap(parent->child(index)->data().toString(), branch) + 1; - if(pos) - { - // Store the child data as we need multiple times - QString tmp = parent->child(index)->data().toString(); + int len = parent->rowCount(); + for(int index = 0; index < len; index++) + { + int pos = overlap(parent->child(index)->data().toString(), branch) + 1; + if (pos) + { + // Store the child data as we need multiple times + QString tmp = parent->child(index)->data().toString(); - if(pos == tmp.length()) - { - // The child contains the branch. - return add(branch.mid(pos), parent->child(index)); - } - else - { - // We found a splitting point. Going to splitt the tree. + if (pos == tmp.length()) + { + // The child contains the branch. + return add(branch.mid(pos), parent->child(index)); + } + else + { + // We found a splitting point. Going to splitt the tree. - // Get the old childs rows - QList row = parent->child(index)->takeColumn(0); + // Get the old childs rows + QList row = parent->child(index)->takeColumn(0); - // Delete the old child - parent->removeRow(index); + // Delete the old child + parent->removeRow(index); - // Create a new parent to replace the old child - QStandardItem *newparent = new QStandardItem(); - newparent->setData(branch.left(pos)); + // Create a new parent to replace the old child + QStandardItem * newparent = new QStandardItem(); + newparent->setData(branch.left(pos)); - // Add the new parent to the parent - parent->appendRow(newparent); - addData(newparent); + // Add the new parent to the parent + parent->appendRow(newparent); + addData(newparent); - // Add the old child to the new parent - QStandardItem* ret; - ret = add(tmp.mid(pos), newparent); + // Add the old child to the new parent + QStandardItem * ret; + ret = add(tmp.mid(pos), newparent); - // Append the old childs rows to the newly added old child - QListIterator iterator(row); - while(iterator.hasNext()) - ret->appendRow(iterator.next()); + // Append the old childs rows to the newly added old child + QListIterator iterator(row); + while (iterator.hasNext()) + { + ret->appendRow(iterator.next()); + } - // Add the new child to the new parent - add(branch.mid(pos), newparent); - - return newparent; - } - } - } + // Add the new child to the new parent + add(branch.mid(pos), newparent); - QStandardItem *it = new QStandardItem(); - it->setData(branch); - parent->appendRow(it); - addData(it); - return it; -} + return newparent; + } + } + } -int TreeBuilder::overlap(const QString &a, const QString &b) -{ - const QChar *ac = a.data(); - const QChar *bc = b.data(); - int len = 0; - int max = qMax(a.length(), b.length()); - while(len < max && ac[len] == bc[len]) - len++; - // Important: It only is a splitting point if both a and b have a sep at - // this point! - while(len >= 0 && (ac[len] != sep || bc[len] != sep)) - len--; - return len; + QStandardItem * it = new QStandardItem(); + it->setData(branch); + parent->appendRow(it); + addData(it); + return it; } -void TreeBuilder::addList(const QString &branches) +int TreeBuilder::overlap(const QString & a, const QString & b) { - QStringList branchList = branches.split("\n"); + const QChar * ac = a.data(); + const QChar * bc = b.data(); + int len = 0; + int max = qMax(a.length(), b.length()); - //Code for testing the algorithm - srand( (unsigned)time( NULL ) ); + while (len < max && ac[len] == bc[len]) + { + len++; + } - while(int c = branchList.count()) - add(branchList.takeAt(rand() % c)); + // Important: It only is a splitting point if both a and b have a sep at + // this point! + while (len >= 0 && (ac[len] != sep || bc[len] != sep)) + { + len--; + } + return len; +} - // FIXME: Replace the code above with the code below once it is clear that the algorithm - // is working. Don't forget to remove the #includes. - - /*QStringListIterator iterator(branchList); - while(iterator.hasNext()) - add(iterator.next()); - */ - root->sortChildren(0); +void TreeBuilder::addList(const QString & branches) +{ + QStringList branchList = branches.split("\n", QString::SkipEmptyParts); + + foreach (QString branch, branchList) + { + add(branch); + } } ============================================================ --- src/util/TreeBuilder.h 1996056b8130b1e46a5ad587ae50e83e956b7cc4 +++ src/util/TreeBuilder.h a4acd39e265420a666b119b38d2f32b926ab85a9 @@ -25,19 +25,22 @@ class TreeBuilder : public QObject class TreeBuilder : public QObject { - Q_OBJECT + Q_OBJECT public: - TreeBuilder(QStandardItem *root, QObject *parent = 0); - QStandardItem* add(const QString &branch); - QStandardItem* add(const QString &branch, QStandardItem *parent); - void addList(const QString &branches); + TreeBuilder(QStandardItem *, QObject * parent = 0); + ~TreeBuilder(); + QStandardItem * add(const QString &); + QStandardItem * add(const QString &, QStandardItem *); + void addList(const QString &); + private: - void addData(QStandardItem *item); - int overlap(const QString &a, const QString &b); - QStandardItem *root; - QChar sep; + void addData(QStandardItem *); + int overlap(const QString &, const QString &); + + QStandardItem * root; + QChar sep; }; #endif //TREEBUILDER_H