ItemWidgets: add helper function to retrieve the underlying model

Add a new function to return the underlying model to avoid code
duplication and make the code more readable. Also replace some 0 with
nullptr.

Change-Id: I1ca33de6f26b4e36f46ce7d2eacc45d0799478a3
Reviewed-by: Konstantin Shegunov <kshegunov@gmail.com>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Christian Ehrlicher 2018-11-04 19:42:41 +01:00
parent 97f73e9577
commit 63967313f5
6 changed files with 105 additions and 67 deletions

View File

@ -634,14 +634,14 @@ Qt::DropActions QListModel::supportedDropActions() const
\sa type()
*/
QListWidgetItem::QListWidgetItem(QListWidget *view, int type)
: rtti(type), view(view), d(new QListWidgetItemPrivate(this)),
QListWidgetItem::QListWidgetItem(QListWidget *listview, int type)
: rtti(type), view(listview), d(new QListWidgetItemPrivate(this)),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled)
{
if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0))
if (QListModel *model = listModel())
model->insert(model->rowCount(), this);
}
@ -661,16 +661,18 @@ QListWidgetItem::QListWidgetItem(QListWidget *view, int type)
\sa type()
*/
QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *view, int type)
: rtti(type), view(0), d(new QListWidgetItemPrivate(this)),
QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *listview, int type)
: rtti(type), view(listview), d(new QListWidgetItemPrivate(this)),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled)
{
setData(Qt::DisplayRole, text);
this->view = view;
if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0))
{
QSignalBlocker b(view);
setData(Qt::DisplayRole, text);
}
if (QListModel *model = listModel())
model->insert(model->rowCount(), this);
}
@ -692,17 +694,19 @@ QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *view, int typ
\sa type()
*/
QListWidgetItem::QListWidgetItem(const QIcon &icon,const QString &text,
QListWidget *view, int type)
: rtti(type), view(0), d(new QListWidgetItemPrivate(this)),
QListWidget *listview, int type)
: rtti(type), view(listview), d(new QListWidgetItemPrivate(this)),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled)
{
setData(Qt::DisplayRole, text);
setData(Qt::DecorationRole, icon);
this->view = view;
if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0))
{
QSignalBlocker b(view);
setData(Qt::DisplayRole, text);
setData(Qt::DecorationRole, icon);
}
if (QListModel *model = listModel())
model->insert(model->rowCount(), this);
}
@ -711,7 +715,7 @@ QListWidgetItem::QListWidgetItem(const QIcon &icon,const QString &text,
*/
QListWidgetItem::~QListWidgetItem()
{
if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0))
if (QListModel *model = listModel())
model->remove(this);
delete d;
}
@ -748,7 +752,7 @@ void QListWidgetItem::setData(int role, const QVariant &value)
}
if (!found)
d->values.append(QWidgetItemData(role, value));
if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : nullptr)) {
if (QListModel *model = listModel()) {
const QVector<int> roles((role == Qt::DisplayRole) ?
QVector<int>({Qt::DisplayRole, Qt::EditRole}) :
QVector<int>({role}));
@ -815,7 +819,7 @@ void QListWidgetItem::write(QDataStream &out) const
\sa data(), flags()
*/
QListWidgetItem::QListWidgetItem(const QListWidgetItem &other)
: rtti(Type), view(0),
: rtti(Type), view(nullptr),
d(new QListWidgetItemPrivate(this)),
itemFlags(other.itemFlags)
{
@ -837,6 +841,15 @@ QListWidgetItem &QListWidgetItem::operator=(const QListWidgetItem &other)
return *this;
}
/*!
\internal
returns the QListModel if a view is set
*/
QListModel *QListWidgetItem::listModel() const
{
return (view ? qobject_cast<QListModel*>(view->model()) : nullptr);
}
#ifndef QT_NO_DATASTREAM
/*!
@ -998,7 +1011,7 @@ QDataStream &operator>>(QDataStream &in, QListWidgetItem &item)
void QListWidgetItem::setFlags(Qt::ItemFlags aflags)
{
itemFlags = aflags;
if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0))
if (QListModel *model = listModel())
model->itemChanged(this);
}

View File

@ -61,10 +61,10 @@ class Q_WIDGETS_EXPORT QListWidgetItem
friend class QListWidget;
public:
enum ItemType { Type = 0, UserType = 1000 };
explicit QListWidgetItem(QListWidget *view = nullptr, int type = Type);
explicit QListWidgetItem(const QString &text, QListWidget *view = nullptr, int type = Type);
explicit QListWidgetItem(QListWidget *listview = nullptr, int type = Type);
explicit QListWidgetItem(const QString &text, QListWidget *listview = nullptr, int type = Type);
explicit QListWidgetItem(const QIcon &icon, const QString &text,
QListWidget *view = nullptr, int type = Type);
QListWidget *listview = nullptr, int type = Type);
QListWidgetItem(const QListWidgetItem &other);
virtual ~QListWidgetItem();
@ -165,6 +165,8 @@ public:
inline int type() const { return rtti; }
private:
QListModel *listModel() const;
private:
int rtti;
QVector<void *> dummy;

View File

@ -1148,7 +1148,7 @@ void QTableWidgetItem::setSelected(bool select)
void QTableWidgetItem::setFlags(Qt::ItemFlags aflags)
{
itemFlags = aflags;
if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : 0))
if (QTableModel *model = tableModel())
model->itemChanged(this);
}
@ -1339,7 +1339,7 @@ void QTableWidgetItem::setFlags(Qt::ItemFlags aflags)
\sa type()
*/
QTableWidgetItem::QTableWidgetItem(int type)
: rtti(type), view(0), d(new QTableWidgetItemPrivate(this)),
: rtti(type), view(nullptr), d(new QTableWidgetItemPrivate(this)),
itemFlags(Qt::ItemIsEditable
|Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
@ -1355,7 +1355,7 @@ QTableWidgetItem::QTableWidgetItem(int type)
\sa type()
*/
QTableWidgetItem::QTableWidgetItem(const QString &text, int type)
: rtti(type), view(0), d(new QTableWidgetItemPrivate(this)),
: rtti(type), view(nullptr), d(new QTableWidgetItemPrivate(this)),
itemFlags(Qt::ItemIsEditable
|Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
@ -1372,7 +1372,7 @@ QTableWidgetItem::QTableWidgetItem(const QString &text, int type)
\sa type()
*/
QTableWidgetItem::QTableWidgetItem(const QIcon &icon, const QString &text, int type)
: rtti(type), view(0), d(new QTableWidgetItemPrivate(this)),
: rtti(type), view(nullptr), d(new QTableWidgetItemPrivate(this)),
itemFlags(Qt::ItemIsEditable
|Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
@ -1389,9 +1389,8 @@ QTableWidgetItem::QTableWidgetItem(const QIcon &icon, const QString &text, int t
*/
QTableWidgetItem::~QTableWidgetItem()
{
if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : 0))
if (QTableModel *model = tableModel())
model->removeItem(this);
view = 0;
delete d;
}
@ -1427,7 +1426,7 @@ void QTableWidgetItem::setData(int role, const QVariant &value)
}
if (!found)
values.append(QWidgetItemData(role, value));
if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : nullptr))
if (QTableModel *model = tableModel())
{
const QVector<int> roles((role == Qt::DisplayRole) ?
QVector<int>({Qt::DisplayRole, Qt::EditRole}) :
@ -1481,6 +1480,16 @@ void QTableWidgetItem::write(QDataStream &out) const
out << values;
}
/*!
\internal
returns the QTableModel if a view is set
*/
QTableModel *QTableWidgetItem::tableModel() const
{
return (view ? qobject_cast<QTableModel*>(view->model()) : nullptr);
}
/*!
\relates QTableWidgetItem
@ -1524,7 +1533,7 @@ QDataStream &operator<<(QDataStream &out, const QTableWidgetItem &item)
\sa data(), flags()
*/
QTableWidgetItem::QTableWidgetItem(const QTableWidgetItem &other)
: rtti(Type), values(other.values), view(0),
: rtti(Type), values(other.values), view(nullptr),
d(new QTableWidgetItemPrivate(this)),
itemFlags(other.itemFlags)
{

View File

@ -182,6 +182,9 @@ public:
inline int type() const { return rtti; }
private:
QTableModel *tableModel() const;
private:
int rtti;
QVector<QWidgetItemData> values;

View File

@ -1346,7 +1346,7 @@ bool QTreeWidgetItem::isHidden() const
\sa type()
*/
QTreeWidgetItem::QTreeWidgetItem(int type)
: rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
: rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
@ -1365,7 +1365,7 @@ QTreeWidgetItem::QTreeWidgetItem(int type)
\sa type()
*/
QTreeWidgetItem::QTreeWidgetItem(const QStringList &strings, int type)
: rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
: rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
@ -1385,16 +1385,16 @@ QTreeWidgetItem::QTreeWidgetItem(const QStringList &strings, int type)
\sa type()
*/
QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, int type)
: rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, int type)
: rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled
|Qt::ItemIsDropEnabled)
{
if (view && view->model()) {
QTreeModel *model = qobject_cast<QTreeModel*>(view->model());
// do not set this->view here otherwise insertChild() will fail
if (QTreeModel *model = treeModel(treeview)) {
model->rootItem->addChild(this);
values.reserve(model->headerItem->columnCount());
}
@ -1410,8 +1410,8 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, int type)
\sa type()
*/
QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, const QStringList &strings, int type)
: rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, const QStringList &strings, int type)
: rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
@ -1420,8 +1420,8 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, const QStringList &strings,
{
for (int i = 0; i < strings.count(); ++i)
setText(i, strings.at(i));
if (view && view->model()) {
QTreeModel *model = qobject_cast<QTreeModel*>(view->model());
// do not set this->view here otherwise insertChild() will fail
if (QTreeModel *model = treeModel(treeview)) {
model->rootItem->addChild(this);
values.reserve(model->headerItem->columnCount());
}
@ -1435,21 +1435,19 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, const QStringList &strings,
\sa type()
*/
QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, QTreeWidgetItem *after, int type)
: rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, QTreeWidgetItem *after, int type)
: rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled
|Qt::ItemIsDropEnabled)
{
if (view) {
QTreeModel *model = qobject_cast<QTreeModel*>(view->model());
if (model) {
int i = model->rootItem->children.indexOf(after) + 1;
model->rootItem->insertChild(i, this);
values.reserve(model->headerItem->columnCount());
}
// do not set this->view here otherwise insertChild() will fail
if (QTreeModel *model = treeModel(treeview)) {
int i = model->rootItem->children.indexOf(after) + 1;
model->rootItem->insertChild(i, this);
values.reserve(model->headerItem->columnCount());
}
}
@ -1459,7 +1457,7 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, QTreeWidgetItem *after, int
\sa type()
*/
QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, int type)
: rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
: rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
@ -1477,7 +1475,7 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, int type)
\sa type()
*/
QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &strings, int type)
: rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
: rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
@ -1499,7 +1497,7 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &str
\sa type()
*/
QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *after, int type)
: rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
: rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
@ -1522,7 +1520,7 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *after
QTreeWidgetItem::~QTreeWidgetItem()
{
QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0);
QTreeModel *model = treeModel();
bool wasSkipSort = false;
if (model) {
wasSkipSort = model->skipPendingSort;
@ -1684,16 +1682,17 @@ void QTreeWidgetItem::setFlags(Qt::ItemFlags flags)
void QTreeWidgetItemPrivate::updateHiddenStatus(QTreeWidgetItem *item, bool inserting)
{
QTreeModel *model = (item->view ? qobject_cast<QTreeModel*>(item->view->model()) : 0);
QTreeModel *model = item->treeModel();
if (!model)
return;
QStack<QTreeWidgetItem *> parents;
parents.push(item);
while (!parents.isEmpty()) {
QTreeWidgetItem *parent = parents.pop();
QModelIndex index = model->index(parent, 0);
if (parent->d->hidden)
if (parent->d->hidden) {
const QModelIndex index = model->index(parent, 0);
item->view->setRowHidden(index.row(), index.parent(), inserting);
}
for (int i = 0; i < parent->children.count(); ++i) {
QTreeWidgetItem *child = parent->children.at(i);
parents.push(child);
@ -1758,7 +1757,7 @@ void QTreeWidgetItem::setData(int column, int role, const QVariant &value)
if (column < 0)
return;
QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0);
QTreeModel *model = treeModel();
switch (role) {
case Qt::EditRole:
case Qt::DisplayRole: {
@ -1966,7 +1965,7 @@ void QTreeWidgetItem::insertChild(int index, QTreeWidgetItem *child)
if (index < 0 || index > children.count() || child == 0 || child->view != 0 || child->par != 0)
return;
if (QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0)) {
if (QTreeModel *model = treeModel()) {
const bool wasSkipSort = model->skipPendingSort;
model->skipPendingSort = true;
if (model->rootItem == this)
@ -2017,7 +2016,7 @@ QTreeWidgetItem *QTreeWidgetItem::takeChild(int index)
{
// we move this outside the check of the index to allow executing
// pending sorts from inline functions, using this function (hack)
QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0);
QTreeModel *model = treeModel();
if (model) {
// This will trigger a layoutChanged signal, thus we might want to optimize
// this function by not emitting the rowsRemoved signal etc to the view.
@ -2075,7 +2074,7 @@ void QTreeWidgetItem::insertChildren(int index, const QList<QTreeWidgetItem*> &c
insertChild(index, children.at(n));
return;
}
QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0);
QTreeModel *model = treeModel();
QStack<QTreeWidgetItem*> stack;
QList<QTreeWidgetItem*> itemsToInsert;
for (int n = 0; n < children.count(); ++n) {
@ -2122,7 +2121,7 @@ QList<QTreeWidgetItem*> QTreeWidgetItem::takeChildren()
{
QList<QTreeWidgetItem*> removed;
if (children.count() > 0) {
QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0);
QTreeModel *model = treeModel();
if (model) {
// This will trigger a layoutChanged signal, thus we might want to optimize
// this function by not emitting the rowsRemoved signal etc to the view.
@ -2154,7 +2153,7 @@ QList<QTreeWidgetItem*> QTreeWidgetItem::takeChildren()
void QTreeWidgetItemPrivate::sortChildren(int column, Qt::SortOrder order, bool climb)
{
QTreeModel *model = (q->view ? qobject_cast<QTreeModel*>(q->view->model()) : 0);
QTreeModel *model = q->treeModel();
if (!model)
return;
model->sortItems(&q->children, column, order);
@ -2177,7 +2176,7 @@ void QTreeWidgetItemPrivate::sortChildren(int column, Qt::SortOrder order, bool
*/
void QTreeWidgetItem::sortChildren(int column, Qt::SortOrder order, bool climb)
{
QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0);
QTreeModel *model = treeModel();
if (!model)
return;
if (model->isChanging())
@ -2257,7 +2256,7 @@ void QTreeWidgetItem::emitDataChanged()
*/
void QTreeWidgetItem::itemChanged()
{
if (QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0))
if (QTreeModel *model = treeModel())
model->itemChanged(this);
}
@ -2266,10 +2265,21 @@ void QTreeWidgetItem::itemChanged()
*/
void QTreeWidgetItem::executePendingSort() const
{
if (QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0))
if (QTreeModel *model = treeModel())
model->executePendingSort();
}
/*!
\internal
returns the QTreeModel if a view is set
*/
QTreeModel *QTreeWidgetItem::treeModel(QTreeWidget *v) const
{
if (!v)
v = view;
return (v ? qobject_cast<QTreeModel*>(v->model()) : nullptr);
}
#ifndef QT_NO_DATASTREAM
/*!

View File

@ -66,9 +66,9 @@ public:
enum ItemType { Type = 0, UserType = 1000 };
explicit QTreeWidgetItem(int type = Type);
explicit QTreeWidgetItem(const QStringList &strings, int type = Type);
explicit QTreeWidgetItem(QTreeWidget *view, int type = Type);
QTreeWidgetItem(QTreeWidget *view, const QStringList &strings, int type = Type);
QTreeWidgetItem(QTreeWidget *view, QTreeWidgetItem *after, int type = Type);
explicit QTreeWidgetItem(QTreeWidget *treeview, int type = Type);
QTreeWidgetItem(QTreeWidget *treeview, const QStringList &strings, int type = Type);
QTreeWidgetItem(QTreeWidget *treeview, QTreeWidgetItem *after, int type = Type);
explicit QTreeWidgetItem(QTreeWidgetItem *parent, int type = Type);
QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &strings, int type = Type);
QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *after, int type = Type);
@ -215,6 +215,7 @@ private:
QVariant childrenCheckState(int column) const;
void itemChanged();
void executePendingSort() const;
QTreeModel *treeModel(QTreeWidget *v = nullptr) const;
int rtti;
// One item has a vector of column entries. Each column has a vector of (role, value) pairs.