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;
|
return m_flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool TreeItem::canFetchMore() const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void TreeItem::prependChild(TreeItem *item)
|
void TreeItem::prependChild(TreeItem *item)
|
||||||
{
|
{
|
||||||
QTC_CHECK(!item->parent());
|
insertChild(0, item);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TreeItem::appendChild(TreeItem *item)
|
void TreeItem::appendChild(TreeItem *item)
|
||||||
|
{
|
||||||
|
insertChild(m_children.size(), item);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TreeItem::insertChild(int pos, TreeItem *item)
|
||||||
{
|
{
|
||||||
QTC_CHECK(!item->parent());
|
QTC_CHECK(!item->parent());
|
||||||
|
QTC_ASSERT(0 <= pos && pos <= m_children.size(), return); // '<= size' is intentional.
|
||||||
|
|
||||||
if (m_model && !m_lazy) {
|
if (m_model && !m_lazy) {
|
||||||
const int n = rowCount();
|
|
||||||
QModelIndex idx = index();
|
QModelIndex idx = index();
|
||||||
item->propagateModel(m_model);
|
m_model->beginInsertRows(idx, pos, pos);
|
||||||
m_model->beginInsertRows(idx, n, n);
|
|
||||||
item->m_parent = this;
|
item->m_parent = this;
|
||||||
item->m_model = m_model;
|
item->m_model = m_model;
|
||||||
m_children.append(item);
|
m_children.insert(m_children.begin() + pos, item);
|
||||||
m_model->endInsertRows();
|
m_model->endInsertRows();
|
||||||
} else {
|
} else {
|
||||||
item->m_parent = this;
|
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);
|
: (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
|
TreeItem *TreeModel::rootItem() const
|
||||||
{
|
{
|
||||||
return m_root;
|
return m_root;
|
||||||
|
@@ -62,8 +62,12 @@ public:
|
|||||||
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;
|
||||||
|
|
||||||
|
virtual bool canFetchMore() const;
|
||||||
|
virtual void fetchMore() {}
|
||||||
|
|
||||||
void prependChild(TreeItem *item);
|
void prependChild(TreeItem *item);
|
||||||
void appendChild(TreeItem *item);
|
void appendChild(TreeItem *item);
|
||||||
|
void insertChild(int pos, TreeItem *item);
|
||||||
void removeChildren();
|
void removeChildren();
|
||||||
void update();
|
void update();
|
||||||
void expand();
|
void expand();
|
||||||
@@ -242,6 +246,9 @@ public:
|
|||||||
Qt::ItemFlags flags(const QModelIndex &idx) const;
|
Qt::ItemFlags flags(const QModelIndex &idx) const;
|
||||||
QVariant headerData(int section, Qt::Orientation orientation, int role) 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 *rootItem() const;
|
||||||
TreeItem *itemFromIndex(const QModelIndex &) const;
|
TreeItem *itemFromIndex(const QModelIndex &) const;
|
||||||
QModelIndex indexFromItem(const TreeItem *needle) const;
|
QModelIndex indexFromItem(const TreeItem *needle) const;
|
||||||
|
Reference in New Issue
Block a user