forked from qt-creator/qt-creator
TreeModel: Add more item functions
canFetchMore/fetchMore, and insertChild (at given position). Make prepend/appendChild use the latter. Change-Id: I4162fe6e64f37d26de209aa81894c9730957694b Reviewed-by: Christian Stenger <christian.stenger@theqtcompany.com> Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
This commit is contained in:
@@ -671,39 +671,36 @@ Qt::ItemFlags TreeItem::flags(int column) const
|
||||
return m_flags;
|
||||
}
|
||||
|
||||
bool TreeItem::canFetchMore() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void TreeItem::prependChild(TreeItem *item)
|
||||
{
|
||||
QTC_CHECK(!item->parent());
|
||||
|
||||
if (m_model && !m_lazy) {
|
||||
QModelIndex idx = index();
|
||||
item->propagateModel(m_model);
|
||||
m_model->beginInsertRows(idx, 0, 0);
|
||||
item->m_parent = this;
|
||||
item->m_model = m_model;
|
||||
m_children.prepend(item);
|
||||
m_model->endInsertRows();
|
||||
} else {
|
||||
m_children.prepend(item);
|
||||
}
|
||||
insertChild(0, item);
|
||||
}
|
||||
|
||||
void TreeItem::appendChild(TreeItem *item)
|
||||
{
|
||||
insertChild(m_children.size(), item);
|
||||
}
|
||||
|
||||
void TreeItem::insertChild(int pos, TreeItem *item)
|
||||
{
|
||||
QTC_CHECK(!item->parent());
|
||||
QTC_ASSERT(0 <= pos && pos <= m_children.size(), return); // '<= size' is intentional.
|
||||
|
||||
if (m_model && !m_lazy) {
|
||||
const int n = rowCount();
|
||||
QModelIndex idx = index();
|
||||
item->propagateModel(m_model);
|
||||
m_model->beginInsertRows(idx, n, n);
|
||||
m_model->beginInsertRows(idx, pos, pos);
|
||||
item->m_parent = this;
|
||||
item->m_model = m_model;
|
||||
m_children.append(item);
|
||||
m_children.insert(m_children.begin() + pos, item);
|
||||
m_model->endInsertRows();
|
||||
} else {
|
||||
item->m_parent = this;
|
||||
m_children.append(item);
|
||||
m_children.insert(m_children.begin() + pos, item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -910,6 +907,23 @@ Qt::ItemFlags TreeModel::flags(const QModelIndex &idx) const
|
||||
: (Qt::ItemIsEnabled|Qt::ItemIsSelectable);
|
||||
}
|
||||
|
||||
bool TreeModel::canFetchMore(const QModelIndex &idx) const
|
||||
{
|
||||
if (!idx.isValid())
|
||||
return false;
|
||||
TreeItem *item = itemFromIndex(idx);
|
||||
return item ? item->canFetchMore() : false;
|
||||
}
|
||||
|
||||
void TreeModel::fetchMore(const QModelIndex &idx)
|
||||
{
|
||||
if (!idx.isValid())
|
||||
return;
|
||||
TreeItem *item = itemFromIndex(idx);
|
||||
if (item)
|
||||
item->fetchMore();
|
||||
}
|
||||
|
||||
TreeItem *TreeModel::rootItem() const
|
||||
{
|
||||
return m_root;
|
||||
|
@@ -62,8 +62,12 @@ public:
|
||||
virtual bool setData(int column, const QVariant &data, int role);
|
||||
virtual Qt::ItemFlags flags(int column) const;
|
||||
|
||||
virtual bool canFetchMore() const;
|
||||
virtual void fetchMore() {}
|
||||
|
||||
void prependChild(TreeItem *item);
|
||||
void appendChild(TreeItem *item);
|
||||
void insertChild(int pos, TreeItem *item);
|
||||
void removeChildren();
|
||||
void update();
|
||||
void expand();
|
||||
@@ -242,6 +246,9 @@ public:
|
||||
Qt::ItemFlags flags(const QModelIndex &idx) const;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
|
||||
|
||||
bool canFetchMore(const QModelIndex &idx) const;
|
||||
void fetchMore(const QModelIndex &idx);
|
||||
|
||||
TreeItem *rootItem() const;
|
||||
TreeItem *itemFromIndex(const QModelIndex &) const;
|
||||
QModelIndex indexFromItem(const TreeItem *needle) const;
|
||||
|
Reference in New Issue
Block a user