forked from qt-creator/qt-creator
TreeModel: Add some convenience functions
Add functions to remove a single item, to access the last child of an item and a simple way to construct items that only display static data. Change-Id: I89347fbb6bbbac6b77fcfb23fa0b780a13643d6f Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
@@ -44,7 +44,13 @@ namespace Utils {
|
|||||||
// TreeItem
|
// TreeItem
|
||||||
//
|
//
|
||||||
TreeItem::TreeItem()
|
TreeItem::TreeItem()
|
||||||
: m_parent(0), m_lazy(false), m_populated(false),
|
: m_parent(0), m_displays(0), m_lazy(false), m_populated(false),
|
||||||
|
m_flags(Qt::ItemIsEnabled|Qt::ItemIsSelectable)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeItem::TreeItem(const QStringList &displays)
|
||||||
|
: m_parent(0), m_displays(new QStringList(displays)), m_lazy(false), m_populated(false),
|
||||||
m_flags(Qt::ItemIsEnabled|Qt::ItemIsSelectable)
|
m_flags(Qt::ItemIsEnabled|Qt::ItemIsSelectable)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -52,6 +58,7 @@ TreeItem::TreeItem()
|
|||||||
TreeItem::~TreeItem()
|
TreeItem::~TreeItem()
|
||||||
{
|
{
|
||||||
clear();
|
clear();
|
||||||
|
delete m_displays;
|
||||||
}
|
}
|
||||||
|
|
||||||
TreeItem *TreeItem::child(int pos) const
|
TreeItem *TreeItem::child(int pos) const
|
||||||
@@ -69,7 +76,7 @@ bool TreeItem::isLazy() const
|
|||||||
|
|
||||||
int TreeItem::columnCount() const
|
int TreeItem::columnCount() const
|
||||||
{
|
{
|
||||||
return 1;
|
return m_displays ? m_displays->size() : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int TreeItem::rowCount() const
|
int TreeItem::rowCount() const
|
||||||
@@ -84,8 +91,8 @@ void TreeItem::populate()
|
|||||||
|
|
||||||
QVariant TreeItem::data(int column, int role) const
|
QVariant TreeItem::data(int column, int role) const
|
||||||
{
|
{
|
||||||
Q_UNUSED(column);
|
if (role == Qt::DisplayRole && m_displays && column >= 0 && column < m_displays->size())
|
||||||
Q_UNUSED(role);
|
return m_displays->at(column);
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,6 +116,19 @@ void TreeItem::appendChild(TreeItem *item)
|
|||||||
m_children.append(item);
|
m_children.append(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TreeItem *TreeItem::lastChild() const
|
||||||
|
{
|
||||||
|
return m_children.isEmpty() ? 0 : m_children.last();
|
||||||
|
}
|
||||||
|
|
||||||
|
int TreeItem::level() const
|
||||||
|
{
|
||||||
|
int l = 0;
|
||||||
|
for (TreeItem *item = this->parent(); item; item = item->parent())
|
||||||
|
++l;
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
void TreeItem::setLazy(bool on)
|
void TreeItem::setLazy(bool on)
|
||||||
{
|
{
|
||||||
m_lazy = on;
|
m_lazy = on;
|
||||||
@@ -251,6 +271,39 @@ QModelIndex TreeModel::indexFromItem(const TreeItem *item) const
|
|||||||
return indexFromItemHelper(item, m_root, QModelIndex());
|
return indexFromItemHelper(item, m_root, QModelIndex());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TreeModel::appendItem(TreeItem *parent, TreeItem *item)
|
||||||
|
{
|
||||||
|
QTC_ASSERT(item, return);
|
||||||
|
QTC_ASSERT(parent, return);
|
||||||
|
|
||||||
|
QModelIndex idx = indexFromItem(parent);
|
||||||
|
const int n = parent->rowCount();
|
||||||
|
beginInsertRows(idx, n, n);
|
||||||
|
parent->appendChild(item);
|
||||||
|
endInsertRows();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TreeModel::updateItem(TreeItem *item)
|
||||||
|
{
|
||||||
|
QModelIndex idx = indexFromItem(item);
|
||||||
|
dataChanged(idx.sibling(idx.row(), 0), idx.sibling(idx.row(), item->columnCount() - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
void TreeModel::removeItem(TreeItem *item)
|
||||||
|
{
|
||||||
|
QTC_ASSERT(item, return);
|
||||||
|
TreeItem *parent = item->parent();
|
||||||
|
QTC_ASSERT(parent, return);
|
||||||
|
int pos = parent->m_children.indexOf(item);
|
||||||
|
QTC_ASSERT(pos != -1, return);
|
||||||
|
|
||||||
|
QModelIndex idx = indexFromItem(parent);
|
||||||
|
beginRemoveRows(idx, pos, pos);
|
||||||
|
item->m_parent = 0;
|
||||||
|
parent->m_children.removeAt(pos);
|
||||||
|
endRemoveRows();
|
||||||
|
}
|
||||||
|
|
||||||
QModelIndex TreeModel::indexFromItemHelper(const TreeItem *needle,
|
QModelIndex TreeModel::indexFromItemHelper(const TreeItem *needle,
|
||||||
TreeItem *parentItem, const QModelIndex &parentIndex) const
|
TreeItem *parentItem, const QModelIndex &parentIndex) const
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ class QTCREATOR_UTILS_EXPORT TreeItem
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TreeItem();
|
TreeItem();
|
||||||
|
explicit TreeItem(const QStringList &displays);
|
||||||
virtual ~TreeItem();
|
virtual ~TreeItem();
|
||||||
|
|
||||||
virtual TreeItem *parent() const { return m_parent; }
|
virtual TreeItem *parent() const { return m_parent; }
|
||||||
@@ -55,6 +56,8 @@ public:
|
|||||||
|
|
||||||
void prependChild(TreeItem *item);
|
void prependChild(TreeItem *item);
|
||||||
void appendChild(TreeItem *item);
|
void appendChild(TreeItem *item);
|
||||||
|
TreeItem *lastChild() const;
|
||||||
|
int level() const;
|
||||||
|
|
||||||
void setLazy(bool on);
|
void setLazy(bool on);
|
||||||
void setPopulated(bool on);
|
void setPopulated(bool on);
|
||||||
@@ -69,9 +72,12 @@ private:
|
|||||||
|
|
||||||
TreeItem *m_parent; // Not owned.
|
TreeItem *m_parent; // Not owned.
|
||||||
QVector<TreeItem *> m_children; // Owned.
|
QVector<TreeItem *> m_children; // Owned.
|
||||||
|
QStringList *m_displays;
|
||||||
bool m_lazy;
|
bool m_lazy;
|
||||||
mutable bool m_populated;
|
mutable bool m_populated;
|
||||||
Qt::ItemFlags m_flags;
|
Qt::ItemFlags m_flags;
|
||||||
|
|
||||||
|
friend class TreeModel;
|
||||||
};
|
};
|
||||||
|
|
||||||
class QTCREATOR_UTILS_EXPORT TreeModel : public QAbstractItemModel
|
class QTCREATOR_UTILS_EXPORT TreeModel : public QAbstractItemModel
|
||||||
@@ -94,6 +100,10 @@ public:
|
|||||||
TreeItem *itemFromIndex(const QModelIndex &) const;
|
TreeItem *itemFromIndex(const QModelIndex &) const;
|
||||||
QModelIndex indexFromItem(const TreeItem *needle) const;
|
QModelIndex indexFromItem(const TreeItem *needle) const;
|
||||||
|
|
||||||
|
void appendItem(TreeItem *parent, TreeItem *item);
|
||||||
|
void removeItem(TreeItem *item); // item is not destroyed.
|
||||||
|
void updateItem(TreeItem *item); // call to trigger dataChanged
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QModelIndex indexFromItemHelper(const TreeItem *needle,
|
QModelIndex indexFromItemHelper(const TreeItem *needle,
|
||||||
TreeItem *parentItem, const QModelIndex &parentIndex) const;
|
TreeItem *parentItem, const QModelIndex &parentIndex) const;
|
||||||
|
|||||||
Reference in New Issue
Block a user