Commit e48451b0 authored by hjk's avatar hjk Committed by hjk

TreeModel: Consolidate child()/childAt() and rowCount()/childCount()

We never used the possibility to overload the virtual child() and
rowCount() functions, it's unlikely to be needed in future.

Change-Id: I7ebdf4dfc70bb0bcadea9ef3fb88f16632a95696
Reviewed-by: Christian Stenger's avatarChristian Stenger <christian.stenger@qt.io>
parent 8667877b
......@@ -617,17 +617,12 @@ TreeItem::~TreeItem()
removeChildren();
}
TreeItem *TreeItem::child(int pos) const
TreeItem *TreeItem::childAt(int pos) const
{
QTC_ASSERT(pos >= 0, return 0);
return pos < m_children.size() ? m_children.at(pos) : 0;
}
int TreeItem::rowCount() const
{
return m_children.size();
}
QVariant TreeItem::data(int column, int role) const
{
Q_UNUSED(column);
......@@ -651,7 +646,7 @@ Qt::ItemFlags TreeItem::flags(int column) const
bool TreeItem::hasChildren() const
{
return canFetchMore() || rowCount() > 0;
return canFetchMore() || childCount() > 0;
}
bool TreeItem::canFetchMore() const
......@@ -692,11 +687,11 @@ void TreeItem::insertChild(int pos, TreeItem *item)
void TreeItem::removeChildren()
{
if (rowCount() == 0)
if (childCount() == 0)
return;
if (m_model) {
QModelIndex idx = index();
m_model->beginRemoveRows(idx, 0, rowCount() - 1);
m_model->beginRemoveRows(idx, 0, childCount() - 1);
clear();
m_model->endRemoveRows();
} else {
......@@ -707,7 +702,7 @@ void TreeItem::removeChildren()
void TreeItem::sortChildren(const std::function<bool(const TreeItem *, const TreeItem *)> &cmp)
{
if (m_model) {
if (const int n = rowCount()) {
if (const int n = childCount()) {
QVector<TreeItem *> tmp = m_children;
std::sort(tmp.begin(), tmp.end(), cmp);
if (tmp == m_children) {
......@@ -850,8 +845,8 @@ QModelIndex TreeModel::parent(const QModelIndex &idx) const
if (!grandparent)
return QModelIndex();
for (int i = 0, n = grandparent->rowCount(); i < n; ++i)
if (grandparent->child(i) == parent)
for (int i = 0, n = grandparent->childCount(); i < n; ++i)
if (grandparent->childAt(i) == parent)
return createIndex(i, 0, (void*) parent);
return QModelIndex();
......@@ -861,12 +856,12 @@ int TreeModel::rowCount(const QModelIndex &idx) const
{
CHECK_INDEX(idx);
if (!idx.isValid())
return m_root->rowCount();
return m_root->childCount();
if (idx.column() > 0)
return 0;
const TreeItem *item = itemForIndex(idx);
QTC_ASSERT(item, return 0);
return item->rowCount();
return item->childCount();
}
int TreeModel::columnCount(const QModelIndex &idx) const
......@@ -985,9 +980,9 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) con
const TreeItem *item = itemForIndex(parent);
QTC_ASSERT(item, return QModelIndex());
if (row >= item->rowCount())
if (row >= item->childCount())
return QModelIndex();
return createIndex(row, column, (void*)(item->child(row)));
return createIndex(row, column, (void*)(item->childAt(row)));
}
TreeItem *TreeModel::itemForIndex(const QModelIndex &idx) const
......
......@@ -41,10 +41,6 @@ public:
TreeItem();
virtual ~TreeItem();
TreeItem *parent() const { return m_parent; }
virtual TreeItem *child(int pos) const;
virtual int rowCount() const;
virtual QVariant data(int column, int role) const;
virtual bool setData(int column, const QVariant &data, int role);
virtual Qt::ItemFlags flags(int column) const;
......@@ -53,6 +49,8 @@ public:
virtual bool canFetchMore() const;
virtual void fetchMore() {}
TreeItem *parent() const { return m_parent; }
void prependChild(TreeItem *item);
void appendChild(TreeItem *item);
void insertChild(int pos, TreeItem *item);
......@@ -67,7 +65,7 @@ public:
void setFlags(Qt::ItemFlags flags);
int childCount() const { return m_children.size(); }
TreeItem *childAt(int index) const { return m_children.at(index); }
TreeItem *childAt(int index) const;
QVector<TreeItem *> children() const { return m_children; }
QModelIndex index() const;
......
......@@ -338,7 +338,7 @@ void TestResultsPane::goToNext()
if (!rootItem || !rootItem->childCount())
return;
nextCurrentIndex = m_filterModel->mapFromSource(m_model->indexForItem(rootItem->child(0)));
nextCurrentIndex = m_filterModel->mapFromSource(m_model->indexForItem(rootItem->childAt(0)));
}
m_treeView->setCurrentIndex(nextCurrentIndex);
......
......@@ -217,7 +217,7 @@ TestTreeItem *TestTreeItem::parentItem() const
TestTreeItem *TestTreeItem::childItem(int row) const
{
return static_cast<TestTreeItem *>(child(row));
return static_cast<TestTreeItem *>(childAt(row));
}
TestTreeItem *TestTreeItem::findChildByName(const QString &name)
......
......@@ -273,7 +273,7 @@ QVariant ExplainingStepItem::data(int column, int role) const
return QVariant::fromValue(static_cast<DiagnosticItem *>(parent())->diagnostic());
case Qt::DisplayRole: {
const int row = parent()->children().indexOf(const_cast<ExplainingStepItem *>(this)) + 1;
const int padding = static_cast<int>(std::log10(parent()->rowCount()))
const int padding = static_cast<int>(std::log10(parent()->childCount()))
- static_cast<int>(std::log10(row));
return QString::fromLatin1("%1%2: %3")
.arg(QString(padding, QLatin1Char(' ')))
......
......@@ -189,12 +189,12 @@ void CMakeToolItemModel::addCMakeTool(const CMakeTool *item, bool changed)
TreeItem *CMakeToolItemModel::autoGroupItem() const
{
return rootItem()->child(0);
return rootItem()->childAt(0);
}
TreeItem *CMakeToolItemModel::manualGroupItem() const
{
return rootItem()->child(1);
return rootItem()->childAt(1);
}
void CMakeToolItemModel::reevaluateChangedFlag(CMakeToolTreeItem *item) const
......
......@@ -80,7 +80,7 @@ int ConsoleItemModel::sizeOfFile(const QFont &font)
lastReadOnlyRow -= 2; // skip editable row
if (lastReadOnlyRow < 0)
return 0;
QString filename = static_cast<ConsoleItem *>(rootItem()->child(lastReadOnlyRow))->file();
QString filename = static_cast<ConsoleItem *>(rootItem()->childAt(lastReadOnlyRow))->file();
const int pos = filename.lastIndexOf(QLatin1Char('/'));
if (pos != -1)
filename = filename.mid(pos + 1);
......
......@@ -157,8 +157,8 @@ QVariant ModuleItem::data(int column, int role) const
static ModuleItem *moduleFromPath(TreeItem *root, const QString &modulePath)
{
// Recent modules are more likely to be unloaded first.
for (int i = root->rowCount(); --i >= 0; ) {
auto item = static_cast<ModuleItem *>(root->child(i));
for (int i = root->childCount(); --i >= 0; ) {
auto item = static_cast<ModuleItem *>(root->childAt(i));
if (item->module.modulePath == modulePath)
return item;
}
......@@ -199,8 +199,8 @@ Modules ModulesHandler::modules() const
{
Modules mods;
TreeItem *root = m_model->rootItem();
for (int i = root->rowCount(); --i >= 0; )
mods.append(static_cast<ModuleItem *>(root->child(i))->module);
for (int i = root->childCount(); --i >= 0; )
mods.append(static_cast<ModuleItem *>(root->childAt(i))->module);
return mods;
}
......@@ -239,15 +239,15 @@ void ModulesHandler::updateModule(const Module &module)
void ModulesHandler::beginUpdateAll()
{
TreeItem *root = m_model->rootItem();
for (int i = root->rowCount(); --i >= 0; )
static_cast<ModuleItem *>(root->child(i))->updated = false;
for (int i = root->childCount(); --i >= 0; )
static_cast<ModuleItem *>(root->childAt(i))->updated = false;
}
void ModulesHandler::endUpdateAll()
{
TreeItem *root = m_model->rootItem();
for (int i = root->rowCount(); --i >= 0; ) {
auto item = static_cast<ModuleItem *>(root->child(i));
for (int i = root->childCount(); --i >= 0; ) {
auto item = static_cast<ModuleItem *>(root->childAt(i));
if (!item->updated)
m_model->destroyItem(item);
}
......
......@@ -1162,7 +1162,7 @@ bool WatchModel::hasChildren(const QModelIndex &idx) const
const WatchItem *item = nonRootItemForIndex(idx);
if (!item)
return true;
if (item->rowCount() > 0)
if (item->childCount() > 0)
return true;
// "Can fetch more", see above.
......
......@@ -236,7 +236,7 @@ void ToolChainOptionsWidget::addToolChain(ToolChain *tc)
}
}
TreeItem *parent = m_model.rootItem()->child(tc->isAutoDetected() ? 0 : 1);
TreeItem *parent = m_model.rootItem()->childAt(tc->isAutoDetected() ? 0 : 1);
parent->appendChild(new ToolChainTreeItem(tc, false));
updateState();
......
......@@ -279,7 +279,7 @@ QVariant FrameItem::data(int column, int role) const
return QVariant::fromValue(getErrorItem()->error());
case Qt::DisplayRole: {
const int row = parent()->children().indexOf(const_cast<FrameItem *>(this)) + 1;
const int padding = static_cast<int>(std::log10(parent()->rowCount()))
const int padding = static_cast<int>(std::log10(parent()->childCount()))
- static_cast<int>(std::log10(row));
return QString::fromLatin1("%1%2: %3")
.arg(QString(padding, QLatin1Char(' ')))
......
......@@ -79,7 +79,7 @@ void tst_TreeModel::testIteration()
group2->appendChild(item21);
group2->appendChild(item22);
QCOMPARE(r->rowCount(), 3);
QCOMPARE(r->childCount(), 3);
QCOMPARE(countLevelItems(r, 1), 3);
QCOMPARE(countLevelItems(r, 2), 6);
QCOMPARE(countLevelItems(r, 3), 0);
......
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