basically, replace
// Reset the parent FIXME: horrible method
parents << parents.first();
for(int p = 0; p < list.size(); p++) {
QList<QVariant> column;
column << list[p];
parents.last()->appendChild(new TreeItem(column, parents.last()));
parents << parents.last()->child(parents.last()->childCount()-1);
}
// Reset the parent FIXME: horrible method
parents << parents.first();
for(int p = 0; p < list.size(); p++) {
QList<QVariant> column;
column << list[p];
parents.last()->appendChild(new TreeItem(column, parents.last()));
parents << parents.last()->child(parents.last()->childCount()-1);
}
To copy to clipboard, switch view to plain text mode
by
// store last parent directory chain in a QStringList
// add at [B]top[/B] of your function
...
// and then replace the code above by
QStringList common
= common_prefix
(lLastDirChain, list
);
// remove the un-common dirs from parents
while (parents.count()>common.count()) parents.pop_back();
// now parents should be the common ones:
for(int p=common.count(); p<list.size(); ++p) {
QList<QVariant> column;
column << list[p];
TreeItem * const item = new TreeItem(column, parents.last());
parents.last()->appendChild(item);
parents << item;
}
lLastDirChain = list;
// store last parent directory chain in a QStringList
// add at [B]top[/B] of your function
QStringList lLastDirChain;
...
// and then replace the code above by
QStringList common = common_prefix(lLastDirChain, list);
// remove the un-common dirs from parents
while (parents.count()>common.count()) parents.pop_back();
// now parents should be the common ones:
for(int p=common.count(); p<list.size(); ++p) {
QList<QVariant> column;
column << list[p];
TreeItem * const item = new TreeItem(column, parents.last());
parents.last()->appendChild(item);
parents << item;
}
lLastDirChain = list;
To copy to clipboard, switch view to plain text mode
untested, probably does not compile. hopefully still helpful.
PS: use ++p instead of p++ (at least when free standing); faster for iterators.
PPS: common_prefix could be implemented as
{
int minlen = qMin(lhs.count(), rhs.count());
for (int i=0; i<minlen; ++i)
{
if (lhs[i]==rhs[i])
res << lhs[i];
else break;
}
return res;
}
static QStringList common_prefix(const QStringList &lhs, const QStringList &rhs)
{
QStringList res;
int minlen = qMin(lhs.count(), rhs.count());
for (int i=0; i<minlen; ++i)
{
if (lhs[i]==rhs[i])
res << lhs[i];
else break;
}
return res;
}
To copy to clipboard, switch view to plain text mode
Bookmarks