Cleanup SimpleDomModel example

Cleanup the SimpleDomModel example:
 - include own headers first
 - use nullptr
 - use const where possible
 - init members in initialization list

Change-Id: If7029a774793927b9a3a9115ea4a7053402a86a1
Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com>
Reviewed-by: Luca Beldi <v.ronin@yahoo.it>
This commit is contained in:
Christian Ehrlicher 2018-11-23 19:33:22 +01:00
parent 1cfe064632
commit b5e0d854bd
6 changed files with 45 additions and 44 deletions

View File

@ -53,23 +53,20 @@
#include <QtXml>
//! [0]
DomItem::DomItem(QDomNode &node, int row, DomItem *parent)
{
domNode = node;
DomItem::DomItem(const QDomNode &node, int row, DomItem *parent)
: domNode(node),
//! [0]
// Record the item's location within its parent.
// Record the item's location within its parent.
//! [1]
rowNumber = row;
parentItem = parent;
}
parentItem(parent),
rowNumber(row)
{}
//! [1]
//! [2]
DomItem::~DomItem()
{
QHash<int,DomItem*>::iterator it;
for (it = childItems.begin(); it != childItems.end(); ++it)
delete it.value();
qDeleteAll(childItems);
}
//! [2]
@ -90,21 +87,22 @@ DomItem *DomItem::parent()
//! [5]
DomItem *DomItem::child(int i)
{
if (childItems.contains(i))
return childItems[i];
DomItem *childItem = childItems.value(i);
if (childItem)
return childItem;
// if child does not yet exist, create it
if (i >= 0 && i < domNode.childNodes().count()) {
QDomNode childNode = domNode.childNodes().item(i);
DomItem *childItem = new DomItem(childNode, i, this);
childItem = new DomItem(childNode, i, this);
childItems[i] = childItem;
return childItem;
}
return 0;
return childItem;
}
//! [5]
//! [6]
int DomItem::row()
int DomItem::row() const
{
return rowNumber;
}

View File

@ -58,16 +58,16 @@
class DomItem
{
public:
DomItem(QDomNode &node, int row, DomItem *parent = 0);
DomItem(const QDomNode &node, int row, DomItem *parent = nullptr);
~DomItem();
DomItem *child(int i);
DomItem *parent();
QDomNode node() const;
int row();
int row() const;
private:
QDomNode domNode;
QHash<int,DomItem*> childItems;
QHash<int, DomItem *> childItems;
DomItem *parentItem;
int rowNumber;
};

View File

@ -48,16 +48,17 @@
**
****************************************************************************/
#include "domitem.h"
#include "dommodel.h"
#include "domitem.h"
#include <QtXml>
//! [0]
DomModel::DomModel(QDomDocument document, QObject *parent)
: QAbstractItemModel(parent), domDocument(document)
DomModel::DomModel(const QDomDocument &document, QObject *parent)
: QAbstractItemModel(parent),
domDocument(document),
rootItem(new DomItem(domDocument, 0))
{
rootItem = new DomItem(domDocument, 0);
}
//! [0]
@ -69,8 +70,9 @@ DomModel::~DomModel()
//! [1]
//! [2]
int DomModel::columnCount(const QModelIndex &/*parent*/) const
int DomModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;
}
//! [2]
@ -84,28 +86,31 @@ QVariant DomModel::data(const QModelIndex &index, int role) const
if (role != Qt::DisplayRole)
return QVariant();
DomItem *item = static_cast<DomItem*>(index.internalPointer());
const DomItem *item = static_cast<DomItem*>(index.internalPointer());
QDomNode node = item->node();
const QDomNode node = item->node();
//! [3] //! [4]
QStringList attributes;
QDomNamedNodeMap attributeMap = node.attributes();
switch (index.column()) {
case 0:
return node.nodeName();
case 1:
{
const QDomNamedNodeMap attributeMap = node.attributes();
QStringList attributes;
for (int i = 0; i < attributeMap.count(); ++i) {
QDomNode attribute = attributeMap.item(i);
attributes << attribute.nodeName() + "=\""
+attribute.nodeValue() + '"';
+ attribute.nodeValue() + '"';
}
return attributes.join(' ');
}
case 2:
return node.nodeValue().split("\n").join(' ');
return node.nodeValue().split('\n').join(' ');
default:
return QVariant();
break;
}
return QVariant();
}
//! [4]
@ -113,7 +118,7 @@ QVariant DomModel::data(const QModelIndex &index, int role) const
Qt::ItemFlags DomModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::NoItemFlags;
return QAbstractItemModel::flags(index);
}
@ -132,17 +137,15 @@ QVariant DomModel::headerData(int section, Qt::Orientation orientation,
case 2:
return tr("Value");
default:
return QVariant();
break;
}
}
return QVariant();
}
//! [6]
//! [7]
QModelIndex DomModel::index(int row, int column, const QModelIndex &parent)
const
QModelIndex DomModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
@ -159,8 +162,7 @@ QModelIndex DomModel::index(int row, int column, const QModelIndex &parent)
DomItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
return QModelIndex();
}
//! [8]

View File

@ -63,7 +63,7 @@ class DomModel : public QAbstractItemModel
Q_OBJECT
public:
explicit DomModel(QDomDocument document, QObject *parent = 0);
explicit DomModel(const QDomDocument &document, QObject *parent = nullptr);
~DomModel();
QVariant data(const QModelIndex &index, int role) const override;

View File

@ -48,22 +48,23 @@
**
****************************************************************************/
#include "dommodel.h"
#include "mainwindow.h"
#include "dommodel.h"
#include <QDomDocument>
#include <QTreeView>
#include <QMenuBar>
#include <QFileDialog>
MainWindow::MainWindow() : QMainWindow(), model(0)
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
model(new DomModel(QDomDocument(), this)),
view(new QTreeView(this))
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(tr("&Open..."), this, &MainWindow::openFile, QKeySequence::Open);
fileMenu->addAction(tr("E&xit"), this, &QWidget::close, QKeySequence::Quit);
model = new DomModel(QDomDocument(), this);
view = new QTreeView(this);
view->setModel(model);
setCentralWidget(view);

View File

@ -65,7 +65,7 @@ class MainWindow : public QMainWindow
Q_OBJECT
public:
MainWindow();
MainWindow(QWidget *parent = nullptr);
public slots:
void openFile();