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