Cleanup EditableItemModel example
Cleanup the EditableItemModel example: - include own headers first - use nullptr - add sanity checks - use for loop instead foreach - use const where possible Change-Id: Ib36e5710c07979576b48a905ee50908a64dcb697 Reviewed-by: Paul Wicking <paul.wicking@qt.io>bb10
parent
866112973d
commit
e0e22d56ac
|
|
@ -48,10 +48,10 @@
|
|||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Q_INIT_RESOURCE(editabletreemodel);
|
||||
|
|
|
|||
|
|
@ -58,8 +58,7 @@ MainWindow::MainWindow(QWidget *parent)
|
|||
{
|
||||
setupUi(this);
|
||||
|
||||
QStringList headers;
|
||||
headers << tr("Title") << tr("Description");
|
||||
const QStringList headers({tr("Title"), tr("Description")});
|
||||
|
||||
QFile file(":/default.txt");
|
||||
file.open(QIODevice::ReadOnly);
|
||||
|
|
@ -87,7 +86,7 @@ MainWindow::MainWindow(QWidget *parent)
|
|||
|
||||
void MainWindow::insertChild()
|
||||
{
|
||||
QModelIndex index = view->selectionModel()->currentIndex();
|
||||
const QModelIndex index = view->selectionModel()->currentIndex();
|
||||
QAbstractItemModel *model = view->model();
|
||||
|
||||
if (model->columnCount(index) == 0) {
|
||||
|
|
@ -99,10 +98,10 @@ void MainWindow::insertChild()
|
|||
return;
|
||||
|
||||
for (int column = 0; column < model->columnCount(index); ++column) {
|
||||
QModelIndex child = model->index(0, column, index);
|
||||
model->setData(child, QVariant("[No data]"), Qt::EditRole);
|
||||
const QModelIndex child = model->index(0, column, index);
|
||||
model->setData(child, QVariant(tr("[No data]")), Qt::EditRole);
|
||||
if (!model->headerData(column, Qt::Horizontal).isValid())
|
||||
model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"), Qt::EditRole);
|
||||
model->setHeaderData(column, Qt::Horizontal, QVariant(tr("[No header]")), Qt::EditRole);
|
||||
}
|
||||
|
||||
view->selectionModel()->setCurrentIndex(model->index(0, 0, index),
|
||||
|
|
@ -127,7 +126,7 @@ bool MainWindow::insertColumn()
|
|||
|
||||
void MainWindow::insertRow()
|
||||
{
|
||||
QModelIndex index = view->selectionModel()->currentIndex();
|
||||
const QModelIndex index = view->selectionModel()->currentIndex();
|
||||
QAbstractItemModel *model = view->model();
|
||||
|
||||
if (!model->insertRow(index.row()+1, index.parent()))
|
||||
|
|
@ -136,19 +135,18 @@ void MainWindow::insertRow()
|
|||
updateActions();
|
||||
|
||||
for (int column = 0; column < model->columnCount(index.parent()); ++column) {
|
||||
QModelIndex child = model->index(index.row()+1, column, index.parent());
|
||||
model->setData(child, QVariant("[No data]"), Qt::EditRole);
|
||||
const QModelIndex child = model->index(index.row() + 1, column, index.parent());
|
||||
model->setData(child, QVariant(tr("[No data]")), Qt::EditRole);
|
||||
}
|
||||
}
|
||||
|
||||
bool MainWindow::removeColumn()
|
||||
{
|
||||
QAbstractItemModel *model = view->model();
|
||||
int column = view->selectionModel()->currentIndex().column();
|
||||
const int column = view->selectionModel()->currentIndex().column();
|
||||
|
||||
// Insert columns in each child of the parent item.
|
||||
bool changed = model->removeColumn(column);
|
||||
|
||||
const bool changed = model->removeColumn(column);
|
||||
if (changed)
|
||||
updateActions();
|
||||
|
||||
|
|
@ -157,7 +155,7 @@ bool MainWindow::removeColumn()
|
|||
|
||||
void MainWindow::removeRow()
|
||||
{
|
||||
QModelIndex index = view->selectionModel()->currentIndex();
|
||||
const QModelIndex index = view->selectionModel()->currentIndex();
|
||||
QAbstractItemModel *model = view->model();
|
||||
if (model->removeRow(index.row(), index.parent()))
|
||||
updateActions();
|
||||
|
|
@ -165,19 +163,19 @@ void MainWindow::removeRow()
|
|||
|
||||
void MainWindow::updateActions()
|
||||
{
|
||||
bool hasSelection = !view->selectionModel()->selection().isEmpty();
|
||||
const bool hasSelection = !view->selectionModel()->selection().isEmpty();
|
||||
removeRowAction->setEnabled(hasSelection);
|
||||
removeColumnAction->setEnabled(hasSelection);
|
||||
|
||||
bool hasCurrent = view->selectionModel()->currentIndex().isValid();
|
||||
const bool hasCurrent = view->selectionModel()->currentIndex().isValid();
|
||||
insertRowAction->setEnabled(hasCurrent);
|
||||
insertColumnAction->setEnabled(hasCurrent);
|
||||
|
||||
if (hasCurrent) {
|
||||
view->closePersistentEditor(view->selectionModel()->currentIndex());
|
||||
|
||||
int row = view->selectionModel()->currentIndex().row();
|
||||
int column = view->selectionModel()->currentIndex().column();
|
||||
const int row = view->selectionModel()->currentIndex().row();
|
||||
const int column = view->selectionModel()->currentIndex().column();
|
||||
if (view->selectionModel()->currentIndex().parent().isValid())
|
||||
statusBar()->showMessage(tr("Position: (%1,%2)").arg(row).arg(column));
|
||||
else
|
||||
|
|
|
|||
|
|
@ -54,14 +54,13 @@
|
|||
#include "ui_mainwindow.h"
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QModelIndex>
|
||||
|
||||
class MainWindow : public QMainWindow, private Ui::MainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWindow(QWidget *parent = 0);
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
|
||||
public slots:
|
||||
void updateActions();
|
||||
|
|
|
|||
|
|
@ -56,14 +56,11 @@
|
|||
|
||||
#include "treeitem.h"
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
//! [0]
|
||||
TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent)
|
||||
{
|
||||
parentItem = parent;
|
||||
itemData = data;
|
||||
}
|
||||
: itemData(data),
|
||||
parentItem(parent)
|
||||
{}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
|
|
@ -76,7 +73,9 @@ TreeItem::~TreeItem()
|
|||
//! [2]
|
||||
TreeItem *TreeItem::child(int number)
|
||||
{
|
||||
return childItems.value(number);
|
||||
if (number < 0 || number >= childItems.size())
|
||||
return nullptr;
|
||||
return childItems.at(number);
|
||||
}
|
||||
//! [2]
|
||||
|
||||
|
|
@ -92,7 +91,6 @@ int TreeItem::childNumber() const
|
|||
{
|
||||
if (parentItem)
|
||||
return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
|
||||
|
||||
return 0;
|
||||
}
|
||||
//! [4]
|
||||
|
|
@ -107,7 +105,9 @@ int TreeItem::columnCount() const
|
|||
//! [6]
|
||||
QVariant TreeItem::data(int column) const
|
||||
{
|
||||
return itemData.value(column);
|
||||
if (column < 0 || column >= itemData.size())
|
||||
return QVariant();
|
||||
return itemData.at(column);
|
||||
}
|
||||
//! [6]
|
||||
|
||||
|
|
@ -136,7 +136,7 @@ bool TreeItem::insertColumns(int position, int columns)
|
|||
for (int column = 0; column < columns; ++column)
|
||||
itemData.insert(position, QVariant());
|
||||
|
||||
foreach (TreeItem *child, childItems)
|
||||
for (TreeItem *child : qAsConst(childItems))
|
||||
child->insertColumns(position, columns);
|
||||
|
||||
return true;
|
||||
|
|
@ -171,7 +171,7 @@ bool TreeItem::removeColumns(int position, int columns)
|
|||
for (int column = 0; column < columns; ++column)
|
||||
itemData.remove(position);
|
||||
|
||||
foreach (TreeItem *child, childItems)
|
||||
for (TreeItem *child : qAsConst(childItems))
|
||||
child->removeColumns(position, columns);
|
||||
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -51,7 +51,6 @@
|
|||
#ifndef TREEITEM_H
|
||||
#define TREEITEM_H
|
||||
|
||||
#include <QList>
|
||||
#include <QVariant>
|
||||
#include <QVector>
|
||||
|
||||
|
|
@ -59,7 +58,7 @@
|
|||
class TreeItem
|
||||
{
|
||||
public:
|
||||
explicit TreeItem(const QVector<QVariant> &data, TreeItem *parent = 0);
|
||||
explicit TreeItem(const QVector<QVariant> &data, TreeItem *parent = nullptr);
|
||||
~TreeItem();
|
||||
|
||||
TreeItem *child(int number);
|
||||
|
|
@ -75,7 +74,7 @@ public:
|
|||
bool setData(int column, const QVariant &value);
|
||||
|
||||
private:
|
||||
QList<TreeItem*> childItems;
|
||||
QVector<TreeItem*> childItems;
|
||||
QVector<QVariant> itemData;
|
||||
TreeItem *parentItem;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -48,21 +48,21 @@
|
|||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "treeitem.h"
|
||||
#include "treemodel.h"
|
||||
#include "treeitem.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
//! [0]
|
||||
TreeModel::TreeModel(const QStringList &headers, const QString &data, QObject *parent)
|
||||
: QAbstractItemModel(parent)
|
||||
{
|
||||
QVector<QVariant> rootData;
|
||||
foreach (QString header, headers)
|
||||
for (const QString &header : headers)
|
||||
rootData << header;
|
||||
|
||||
rootItem = new TreeItem(rootData);
|
||||
setupModelData(data.split(QString("\n")), rootItem);
|
||||
setupModelData(data.split('\n'), rootItem);
|
||||
}
|
||||
//! [0]
|
||||
|
||||
|
|
@ -74,8 +74,9 @@ TreeModel::~TreeModel()
|
|||
//! [1]
|
||||
|
||||
//! [2]
|
||||
int TreeModel::columnCount(const QModelIndex & /* parent */) const
|
||||
int TreeModel::columnCount(const QModelIndex &parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
return rootItem->columnCount();
|
||||
}
|
||||
//! [2]
|
||||
|
|
@ -97,7 +98,7 @@ QVariant TreeModel::data(const QModelIndex &index, int role) const
|
|||
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return 0;
|
||||
return Qt::NoItemFlags;
|
||||
|
||||
return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
|
||||
}
|
||||
|
|
@ -133,21 +134,20 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) con
|
|||
|
||||
//! [6]
|
||||
TreeItem *parentItem = getItem(parent);
|
||||
if (!parentItem)
|
||||
return QModelIndex();
|
||||
|
||||
TreeItem *childItem = parentItem->child(row);
|
||||
if (childItem)
|
||||
return createIndex(row, column, childItem);
|
||||
else
|
||||
return QModelIndex();
|
||||
return QModelIndex();
|
||||
}
|
||||
//! [6]
|
||||
|
||||
bool TreeModel::insertColumns(int position, int columns, const QModelIndex &parent)
|
||||
{
|
||||
bool success;
|
||||
|
||||
beginInsertColumns(parent, position, position + columns - 1);
|
||||
success = rootItem->insertColumns(position, columns);
|
||||
const bool success = rootItem->insertColumns(position, columns);
|
||||
endInsertColumns();
|
||||
|
||||
return success;
|
||||
|
|
@ -156,10 +156,13 @@ bool TreeModel::insertColumns(int position, int columns, const QModelIndex &pare
|
|||
bool TreeModel::insertRows(int position, int rows, const QModelIndex &parent)
|
||||
{
|
||||
TreeItem *parentItem = getItem(parent);
|
||||
bool success;
|
||||
if (!parentItem)
|
||||
return false;
|
||||
|
||||
beginInsertRows(parent, position, position + rows - 1);
|
||||
success = parentItem->insertChildren(position, rows, rootItem->columnCount());
|
||||
const bool success = parentItem->insertChildren(position,
|
||||
rows,
|
||||
rootItem->columnCount());
|
||||
endInsertRows();
|
||||
|
||||
return success;
|
||||
|
|
@ -172,9 +175,9 @@ QModelIndex TreeModel::parent(const QModelIndex &index) const
|
|||
return QModelIndex();
|
||||
|
||||
TreeItem *childItem = getItem(index);
|
||||
TreeItem *parentItem = childItem->parent();
|
||||
TreeItem *parentItem = childItem ? childItem->parent() : nullptr;
|
||||
|
||||
if (parentItem == rootItem)
|
||||
if (parentItem == rootItem || !parentItem)
|
||||
return QModelIndex();
|
||||
|
||||
return createIndex(parentItem->childNumber(), 0, parentItem);
|
||||
|
|
@ -183,10 +186,8 @@ QModelIndex TreeModel::parent(const QModelIndex &index) const
|
|||
|
||||
bool TreeModel::removeColumns(int position, int columns, const QModelIndex &parent)
|
||||
{
|
||||
bool success;
|
||||
|
||||
beginRemoveColumns(parent, position, position + columns - 1);
|
||||
success = rootItem->removeColumns(position, columns);
|
||||
const bool success = rootItem->removeColumns(position, columns);
|
||||
endRemoveColumns();
|
||||
|
||||
if (rootItem->columnCount() == 0)
|
||||
|
|
@ -198,10 +199,11 @@ bool TreeModel::removeColumns(int position, int columns, const QModelIndex &pare
|
|||
bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent)
|
||||
{
|
||||
TreeItem *parentItem = getItem(parent);
|
||||
bool success = true;
|
||||
if (!parentItem)
|
||||
return false;
|
||||
|
||||
beginRemoveRows(parent, position, position + rows - 1);
|
||||
success = parentItem->removeChildren(position, rows);
|
||||
const bool success = parentItem->removeChildren(position, rows);
|
||||
endRemoveRows();
|
||||
|
||||
return success;
|
||||
|
|
@ -210,9 +212,9 @@ bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent)
|
|||
//! [8]
|
||||
int TreeModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
TreeItem *parentItem = getItem(parent);
|
||||
const TreeItem *parentItem = getItem(parent);
|
||||
|
||||
return parentItem->childCount();
|
||||
return parentItem ? parentItem->childCount() : 0;
|
||||
}
|
||||
//! [8]
|
||||
|
||||
|
|
@ -225,7 +227,7 @@ bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int rol
|
|||
bool result = item->setData(index.column(), value);
|
||||
|
||||
if (result)
|
||||
emit dataChanged(index, index, {role});
|
||||
emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
@ -236,7 +238,7 @@ bool TreeModel::setHeaderData(int section, Qt::Orientation orientation,
|
|||
if (role != Qt::EditRole || orientation != Qt::Horizontal)
|
||||
return false;
|
||||
|
||||
bool result = rootItem->setData(section, value);
|
||||
const bool result = rootItem->setData(section, value);
|
||||
|
||||
if (result)
|
||||
emit headerDataChanged(orientation, section, section);
|
||||
|
|
@ -246,8 +248,8 @@ bool TreeModel::setHeaderData(int section, Qt::Orientation orientation,
|
|||
|
||||
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
|
||||
{
|
||||
QList<TreeItem*> parents;
|
||||
QList<int> indentations;
|
||||
QVector<TreeItem*> parents;
|
||||
QVector<int> indentations;
|
||||
parents << parent;
|
||||
indentations << 0;
|
||||
|
||||
|
|
@ -261,14 +263,15 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
|
|||
++position;
|
||||
}
|
||||
|
||||
QString lineData = lines[number].mid(position).trimmed();
|
||||
const QString lineData = lines[number].mid(position).trimmed();
|
||||
|
||||
if (!lineData.isEmpty()) {
|
||||
// Read the column data from the rest of the line.
|
||||
QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
|
||||
const QStringList columnStrings = lineData.split('\t', QString::SkipEmptyParts);
|
||||
QVector<QVariant> columnData;
|
||||
for (int column = 0; column < columnStrings.count(); ++column)
|
||||
columnData << columnStrings[column];
|
||||
columnData.reserve(columnStrings.size());
|
||||
for (const QString &columnString : columnStrings)
|
||||
columnData << columnString;
|
||||
|
||||
if (position > indentations.last()) {
|
||||
// The last child of the current parent is now the new parent
|
||||
|
|
@ -291,7 +294,6 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
|
|||
for (int column = 0; column < columnData.size(); ++column)
|
||||
parent->child(parent->childCount() - 1)->setData(column, columnData[column]);
|
||||
}
|
||||
|
||||
++number;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ class TreeModel : public QAbstractItemModel
|
|||
|
||||
public:
|
||||
TreeModel(const QStringList &headers, const QString &data,
|
||||
QObject *parent = 0);
|
||||
QObject *parent = nullptr);
|
||||
~TreeModel();
|
||||
//! [0] //! [1]
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue