Commit 2b19081c authored by hjk's avatar hjk

TreeModel: More API cleanup

- introduce topLevelItemCount (similar to QTreeWidget)
- squash untypedTreeLevelItems()
- rename removeItems() to clear()
- rename removeItem() to takeItem()
- rename treeLevelItems<> to itemsAtLevel<>

Change-Id: I0f1bb4110f7687b20da3d92e3d943858645a9fa2
Reviewed-by: default avatarEike Ziller <eike.ziller@theqtcompany.com>
parent b30b69bb
......@@ -392,7 +392,7 @@ PluginSpec *PluginView::pluginForIndex(const QModelIndex &index) const
void PluginView::updatePlugins()
{
// Model.
m_model->removeItems();
m_model->clear();
PluginCollection *defaultCollection = 0;
QList<CollectionItem *> collections;
......
......@@ -984,6 +984,11 @@ TreeItem *TreeModel::rootItem() const
return m_root;
}
int TreeModel::topLevelItemCount() const
{
return m_root->childCount();
}
void TreeModel::setRootItem(TreeItem *item)
{
delete m_root;
......@@ -1030,25 +1035,22 @@ QModelIndex TreeModel::indexForItem(const TreeItem *item) const
return createIndex(row, 0, mitem);
}
void TreeModel::removeItems()
/*!
Destroys all items in them model except the invisible root item.
*/
void TreeModel::clear()
{
if (m_root)
m_root->removeChildren();
}
UntypedTreeLevelItems TreeModel::untypedLevelItems(int level, TreeItem *start) const
{
if (start == 0)
start = m_root;
return UntypedTreeLevelItems(start, level);
}
/*!
Removes the specified item from the model.
UntypedTreeLevelItems TreeModel::untypedLevelItems(TreeItem *start) const
{
return UntypedTreeLevelItems(start, 1);
}
\note The item is not destroyed, ownership is effectively passed to the caller.
*/
void TreeModel::removeItem(TreeItem *item)
void TreeModel::takeItem(TreeItem *item)
{
#if USE_MODEL_TEST
(void) new ModelTest(this, this);
......
......@@ -99,6 +99,8 @@ public:
void setLazy(bool on);
void setPopulated(bool on);
void setFlags(Qt::ItemFlags flags);
int childCount() const { return m_children.size(); }
TreeItem *childAt(int index) const { return m_children.at(index); }
QVector<TreeItem *> children() const { return m_children; }
QModelIndex index() const;
......@@ -258,8 +260,17 @@ class QTCREATOR_UTILS_EXPORT TreeModel : public QAbstractItemModel
public:
explicit TreeModel(QObject *parent = 0);
explicit TreeModel(TreeItem *root, QObject *parent = 0);
virtual ~TreeModel();
~TreeModel();
void setHeader(const QStringList &displays);
void clear();
TreeItem *rootItem() const;
void setRootItem(TreeItem *item);
TreeItem *itemForIndex(const QModelIndex &) const;
QModelIndex indexForItem(const TreeItem *needle) const;
int topLevelItemCount() const;
int rowCount(const QModelIndex &idx = QModelIndex()) const;
int columnCount(const QModelIndex &idx) const;
......@@ -274,36 +285,19 @@ public:
bool canFetchMore(const QModelIndex &idx) const;
void fetchMore(const QModelIndex &idx);
TreeItem *rootItem() const;
void setRootItem(TreeItem *item);
TreeItem *itemForIndex(const QModelIndex &) const;
QModelIndex indexForItem(const TreeItem *needle) const;
void removeItems();
void setHeader(const QStringList &displays);
UntypedTreeLevelItems untypedLevelItems(int level = 0, TreeItem *start = 0) const;
UntypedTreeLevelItems untypedLevelItems(TreeItem *start) const;
template <class T>
TreeLevelItems<T> treeLevelItems(int level, TreeItem *start = 0) const
{
return TreeLevelItems<T>(untypedLevelItems(level, start));
}
template <class T>
TreeLevelItems<T> treeLevelItems(TreeItem *start) const
TreeLevelItems<T> itemsAtLevel(int level, TreeItem *start = 0) const
{
return TreeLevelItems<T>(untypedLevelItems(start));
return TreeLevelItems<T>(UntypedTreeLevelItems(start ? start : m_root, level));
}
template <class T>
T findItemAtLevel(int level, std::function<bool(T)> f, TreeItem *start = 0) const
{
return Utils::findOrDefault(treeLevelItems<T>(level, start), f);
return Utils::findOrDefault(itemsAtLevel<T>(level, start), f);
}
void removeItem(TreeItem *item); // item is not destroyed.
void takeItem(TreeItem *item); // item is not destroyed.
signals:
void requestExpansion(QModelIndex);
......
......@@ -237,7 +237,7 @@ void CMakeToolItemModel::removeCMakeTool(const Core::Id &id)
CMakeToolTreeItem *treeItem = cmakeToolItem(id);
QTC_ASSERT(treeItem, return);
removeItem(treeItem);
takeItem(treeItem);
m_removedItems.append(id);
delete treeItem;
......@@ -248,7 +248,7 @@ void CMakeToolItemModel::apply()
foreach (const Core::Id &id, m_removedItems)
CMakeToolManager::deregisterCMakeTool(id);
foreach (auto item, treeLevelItems<CMakeToolTreeItem *>(2)) {
foreach (auto item, itemsAtLevel<CMakeToolTreeItem *>(2)) {
item->m_changed = false;
bool isNew = false;
......@@ -300,7 +300,7 @@ void CMakeToolItemModel::setDefaultItemId(const Core::Id &id)
QString CMakeToolItemModel::uniqueDisplayName(const QString &base) const
{
QStringList names;
foreach (CMakeToolTreeItem *item, treeLevelItems<CMakeToolTreeItem *>(2))
foreach (CMakeToolTreeItem *item, itemsAtLevel<CMakeToolTreeItem *>(2))
names << item->m_name;
return ProjectExplorer::Project::makeUnique(base, names);
......
......@@ -401,7 +401,7 @@ void BreakHandler::deletionHelper(BreakpointModelId id)
{
Breakpoint b = breakpointById(id);
QTC_ASSERT(b, return);
removeItem(b.b);
takeItem(b.b);
delete b.b;
}
......@@ -1132,7 +1132,7 @@ void BreakHandler::saveSessionData()
void BreakHandler::loadSessionData()
{
removeItems();
clear();
loadBreakpoints();
}
......@@ -1216,7 +1216,7 @@ void BreakHandler::changeLineNumberFromMarkerHelper(BreakpointModelId id)
Breakpoint b = breakpointById(id);
QTC_ASSERT(b, return);
BreakpointParameters params = b.parameters();
removeItem(b.b);
takeItem(b.b);
delete b.b;
appendBreakpoint(params);
}
......
......@@ -1875,7 +1875,7 @@ void CdbEngine::handleLocals(const CdbResponse &response, bool newFrame)
QSet<QByteArray> toDelete;
if (newFrame) {
foreach (WatchItem *item, handler->model()->treeLevelItems<WatchItem *>(2))
foreach (WatchItem *item, handler->model()->itemsAtLevel<WatchItem *>(2))
toDelete.insert(item->iname);
}
......
......@@ -1944,7 +1944,7 @@ void DebuggerEngine::updateLocalsView(const GdbMi &all)
QSet<QByteArray> toDelete;
if (!partial) {
foreach (WatchItem *item, handler->model()->treeLevelItems<WatchItem *>(2))
foreach (WatchItem *item, handler->model()->itemsAtLevel<WatchItem *>(2))
toDelete.insert(item->iname);
}
......
......@@ -165,7 +165,7 @@ void DebuggerItemModel::removeCurrentDebugger()
QVariant id = m_currentTreeItem->m_item.id();
DebuggerTreeItem *treeItem = m_currentTreeItem;
m_currentTreeItem = 0;
removeItem(treeItem);
takeItem(treeItem);
delete treeItem;
m_removedItems.append(id);
}
......@@ -175,7 +175,7 @@ void DebuggerItemModel::apply()
foreach (const QVariant &id, m_removedItems)
DebuggerItemManager::deregisterDebugger(id);
foreach (auto item, treeLevelItems<DebuggerTreeItem *>(2)) {
foreach (auto item, itemsAtLevel<DebuggerTreeItem *>(2)) {
item->m_changed = false;
DebuggerItemManager::updateOrAddDebugger(item->m_item);
}
......
......@@ -199,7 +199,7 @@ public:
ToolTipWatchItem(WatchItem *item);
bool hasChildren() const { return expandable; }
bool canFetchMore() const { return children().isEmpty() && expandable && model(); }
bool canFetchMore() const { return childCount() == 0 && expandable && model(); }
void fetchMore() {}
QVariant data(int column, int role) const;
......
......@@ -197,7 +197,7 @@ QAbstractItemModel *ModulesHandler::model() const
void ModulesHandler::removeAll()
{
m_model->removeItems();
m_model->clear();
}
Modules ModulesHandler::modules() const
......@@ -212,7 +212,7 @@ Modules ModulesHandler::modules() const
void ModulesHandler::removeModule(const QString &modulePath)
{
if (ModuleItem *item = moduleFromPath(m_model->rootItem(), modulePath))
m_model->removeItem(item);
m_model->takeItem(item);
}
void ModulesHandler::updateModule(const Module &module)
......@@ -254,7 +254,7 @@ void ModulesHandler::endUpdateAll()
for (int i = root->rowCount(); --i >= 0; ) {
auto item = static_cast<ModuleItem *>(root->child(i));
if (!item->updated)
m_model->removeItem(item);
m_model->takeItem(item);
}
}
......
......@@ -572,7 +572,7 @@ void PdbEngine::refreshLocals(const GdbMi &vars)
handler->resetValueCache();
QSet<QByteArray> toDelete;
foreach (WatchItem *item, handler->model()->treeLevelItems<WatchItem *>(2))
foreach (WatchItem *item, handler->model()->itemsAtLevel<WatchItem *>(2))
toDelete.insert(item->iname);
foreach (const GdbMi &child, vars.children()) {
......
......@@ -1241,7 +1241,7 @@ void WatchModel::insertItem(WatchItem *item)
{
WatchItem *existing = findItem(item->iname);
if (existing)
removeItem(existing);
takeItem(existing);
//item->walkTree([item](TreeItem *sub) { sub->sortChildren(&watchItemSorter); });
item->sortChildren(&watchItemSorter);
......@@ -1308,7 +1308,7 @@ void WatchHandler::purgeOutdatedItems(const QSet<QByteArray> &inames)
{
foreach (const QByteArray &iname, inames) {
WatchItem *item = findItem(iname);
m_model->removeItem(item);
m_model->takeItem(item);
}
m_model->layoutChanged();
......@@ -1326,7 +1326,7 @@ void WatchHandler::removeItemByIName(const QByteArray &iname)
theWatcherNames.remove(item->exp);
saveWatchers();
}
m_model->removeItem(item);
m_model->takeItem(item);
delete item;
updateWatchersWindow();
}
......@@ -1492,7 +1492,7 @@ void WatchHandler::updateWatchersWindow()
// Force show/hide of watchers and return view.
int showWatch = !theWatcherNames.isEmpty();
int showReturn = !m_model->m_returnRoot->children().isEmpty();
int showReturn = m_model->m_returnRoot->childCount() != 0;
Internal::updateWatchersWindow(showWatch, showReturn);
}
......
......@@ -192,8 +192,8 @@ void KitModel::isAutoDetectedChanged()
if (oldParent && oldParent != newParent) {
beginMoveRows(indexForItem(oldParent), idx, idx, indexForItem(newParent), newParent->children().size());
TreeItem *n = oldParent->children().at(idx);
removeItem(n);
TreeItem *n = oldParent->childAt(idx);
takeItem(n);
newParent->appendChild(n);
endMoveRows();
}
......@@ -202,7 +202,7 @@ void KitModel::isAutoDetectedChanged()
void KitModel::validateKitNames()
{
QHash<QString, int> nameHash;
foreach (KitNode *n, treeLevelItems<KitNode *>(2)) {
foreach (KitNode *n, itemsAtLevel<KitNode *>(2)) {
const QString displayName = n->widget->displayName();
if (nameHash.contains(displayName))
++nameHash[displayName];
......@@ -210,7 +210,7 @@ void KitModel::validateKitNames()
nameHash.insert(displayName, 1);
}
foreach (KitNode *n, treeLevelItems<KitNode *>(2)) {
foreach (KitNode *n, itemsAtLevel<KitNode *>(2)) {
const QString displayName = n->widget->displayName();
n->widget->setHasUniqueName(nameHash.value(displayName) == 1);
}
......@@ -223,7 +223,7 @@ void KitModel::apply()
n->widget->removeKit();
// Update kits:
foreach (KitNode *n, treeLevelItems<KitNode *>(2)) {
foreach (KitNode *n, itemsAtLevel<KitNode *>(2)) {
if (n->widget->isDirty()) {
n->widget->apply();
n->update();
......@@ -249,7 +249,7 @@ void KitModel::markForRemoval(Kit *k)
if (node == m_defaultNode)
setDefaultNode(findItemAtLevel<KitNode *>(2, [node](KitNode *kn) { return kn != node; }));
removeItem(node);
takeItem(node);
if (node->widget->configures(0))
delete node;
else
......@@ -279,7 +279,7 @@ Kit *KitModel::markForAddition(Kit *baseKit)
KitNode *KitModel::findWorkingCopy(Kit *k) const
{
foreach (KitNode *n, treeLevelItems<KitNode *>(2)) {
foreach (KitNode *n, itemsAtLevel<KitNode *>(2)) {
if (n->widget->workingCopy() == k)
return n;
}
......@@ -349,7 +349,7 @@ void KitModel::removeKit(Kit *k)
}
KitNode *node = 0;
foreach (KitNode *n, treeLevelItems<KitNode *>(2)) {
foreach (KitNode *n, itemsAtLevel<KitNode *>(2)) {
if (n->widget->configures(k)) {
node = n;
break;
......@@ -359,7 +359,7 @@ void KitModel::removeKit(Kit *k)
if (node == m_defaultNode)
setDefaultNode(findItemAtLevel<KitNode *>(2, [node](KitNode *kn) { return kn != node; }));
removeItem(node);
takeItem(node);
delete node;
validateKitNames();
......@@ -369,7 +369,7 @@ void KitModel::removeKit(Kit *k)
void KitModel::changeDefaultKit()
{
Kit *defaultKit = KitManager::defaultKit();
foreach (KitNode *n, treeLevelItems<KitNode *>(2)) {
foreach (KitNode *n, itemsAtLevel<KitNode *>(2)) {
if (n->widget->configures(defaultKit)) {
setDefaultNode(n);
return;
......
......@@ -219,7 +219,7 @@ public:
void ToolChainOptionsWidget::markForRemoval(ToolChainTreeItem *item)
{
m_model.removeItem(item);
m_model.takeItem(item);
if (m_toAddList.contains(item)) {
delete item->toolChain;
item->toolChain = 0;
......@@ -257,9 +257,9 @@ void ToolChainOptionsWidget::removeToolChain(ToolChain *tc)
}
TreeItem *parent = m_model.rootItem()->child(tc->isAutoDetected() ? 0 : 1);
foreach (ToolChainTreeItem *item, m_model.treeLevelItems<ToolChainTreeItem *>(1, parent)) {
foreach (ToolChainTreeItem *item, m_model.itemsAtLevel<ToolChainTreeItem *>(1, parent)) {
if (item->toolChain == tc) {
m_model.removeItem(item);
m_model.takeItem(item);
delete item;
break;
}
......@@ -292,7 +292,7 @@ void ToolChainOptionsWidget::apply()
Q_ASSERT(m_toRemoveList.isEmpty());
// Update tool chains:
foreach (ToolChainTreeItem *item, m_model.treeLevelItems<ToolChainTreeItem *>(1, m_manualRoot)) {
foreach (ToolChainTreeItem *item, m_model.itemsAtLevel<ToolChainTreeItem *>(1, m_manualRoot)) {
if (item->changed) {
Q_ASSERT(item->toolChain);
if (item->widget)
......
......@@ -47,7 +47,7 @@ private slots:
static int countLevelItems(TreeItem *base, int level)
{
int n = 0;
foreach (TreeItem *item, base->model()->untypedLevelItems(level, base)) {
foreach (TreeItem *item, base->model()->untypedItemsAtLevel(level, base)) {
Q_UNUSED(item);
++n;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment