[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[2] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QtGui>
|
---|
| 42 | #include <QtXml>
|
---|
| 43 |
|
---|
| 44 | #include "domitem.h"
|
---|
| 45 | #include "dommodel.h"
|
---|
| 46 |
|
---|
| 47 | //! [0]
|
---|
| 48 | DomModel::DomModel(QDomDocument document, QObject *parent)
|
---|
| 49 | : QAbstractItemModel(parent), domDocument(document)
|
---|
| 50 | {
|
---|
| 51 | rootItem = new DomItem(domDocument, 0);
|
---|
| 52 | }
|
---|
| 53 | //! [0]
|
---|
| 54 |
|
---|
| 55 | //! [1]
|
---|
| 56 | DomModel::~DomModel()
|
---|
| 57 | {
|
---|
| 58 | delete rootItem;
|
---|
| 59 | }
|
---|
| 60 | //! [1]
|
---|
| 61 |
|
---|
| 62 | //! [2]
|
---|
| 63 | int DomModel::columnCount(const QModelIndex &/*parent*/) const
|
---|
| 64 | {
|
---|
| 65 | return 3;
|
---|
| 66 | }
|
---|
| 67 | //! [2]
|
---|
| 68 |
|
---|
| 69 | //! [3]
|
---|
| 70 | QVariant DomModel::data(const QModelIndex &index, int role) const
|
---|
| 71 | {
|
---|
| 72 | if (!index.isValid())
|
---|
| 73 | return QVariant();
|
---|
| 74 |
|
---|
| 75 | if (role != Qt::DisplayRole)
|
---|
| 76 | return QVariant();
|
---|
| 77 |
|
---|
| 78 | DomItem *item = static_cast<DomItem*>(index.internalPointer());
|
---|
| 79 |
|
---|
| 80 | QDomNode node = item->node();
|
---|
| 81 | //! [3] //! [4]
|
---|
| 82 | QStringList attributes;
|
---|
| 83 | QDomNamedNodeMap attributeMap = node.attributes();
|
---|
| 84 |
|
---|
| 85 | switch (index.column()) {
|
---|
| 86 | case 0:
|
---|
| 87 | return node.nodeName();
|
---|
| 88 | case 1:
|
---|
| 89 | for (int i = 0; i < attributeMap.count(); ++i) {
|
---|
| 90 | QDomNode attribute = attributeMap.item(i);
|
---|
| 91 | attributes << attribute.nodeName() + "=\""
|
---|
| 92 | +attribute.nodeValue() + "\"";
|
---|
| 93 | }
|
---|
| 94 | return attributes.join(" ");
|
---|
| 95 | case 2:
|
---|
| 96 | return node.nodeValue().split("\n").join(" ");
|
---|
| 97 | default:
|
---|
| 98 | return QVariant();
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | //! [4]
|
---|
| 102 |
|
---|
| 103 | //! [5]
|
---|
| 104 | Qt::ItemFlags DomModel::flags(const QModelIndex &index) const
|
---|
| 105 | {
|
---|
| 106 | if (!index.isValid())
|
---|
| 107 | return 0;
|
---|
| 108 |
|
---|
| 109 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
---|
| 110 | }
|
---|
| 111 | //! [5]
|
---|
| 112 |
|
---|
| 113 | //! [6]
|
---|
| 114 | QVariant DomModel::headerData(int section, Qt::Orientation orientation,
|
---|
| 115 | int role) const
|
---|
| 116 | {
|
---|
| 117 | if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
---|
| 118 | switch (section) {
|
---|
| 119 | case 0:
|
---|
| 120 | return tr("Name");
|
---|
| 121 | case 1:
|
---|
| 122 | return tr("Attributes");
|
---|
| 123 | case 2:
|
---|
| 124 | return tr("Value");
|
---|
| 125 | default:
|
---|
| 126 | return QVariant();
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | return QVariant();
|
---|
| 131 | }
|
---|
| 132 | //! [6]
|
---|
| 133 |
|
---|
| 134 | //! [7]
|
---|
| 135 | QModelIndex DomModel::index(int row, int column, const QModelIndex &parent)
|
---|
| 136 | const
|
---|
| 137 | {
|
---|
| 138 | if (!hasIndex(row, column, parent))
|
---|
| 139 | return QModelIndex();
|
---|
| 140 |
|
---|
| 141 | DomItem *parentItem;
|
---|
| 142 |
|
---|
| 143 | if (!parent.isValid())
|
---|
| 144 | parentItem = rootItem;
|
---|
| 145 | else
|
---|
| 146 | parentItem = static_cast<DomItem*>(parent.internalPointer());
|
---|
| 147 | //! [7]
|
---|
| 148 |
|
---|
| 149 | //! [8]
|
---|
| 150 | DomItem *childItem = parentItem->child(row);
|
---|
| 151 | if (childItem)
|
---|
| 152 | return createIndex(row, column, childItem);
|
---|
| 153 | else
|
---|
| 154 | return QModelIndex();
|
---|
| 155 | }
|
---|
| 156 | //! [8]
|
---|
| 157 |
|
---|
| 158 | //! [9]
|
---|
| 159 | QModelIndex DomModel::parent(const QModelIndex &child) const
|
---|
| 160 | {
|
---|
| 161 | if (!child.isValid())
|
---|
| 162 | return QModelIndex();
|
---|
| 163 |
|
---|
| 164 | DomItem *childItem = static_cast<DomItem*>(child.internalPointer());
|
---|
| 165 | DomItem *parentItem = childItem->parent();
|
---|
| 166 |
|
---|
| 167 | if (!parentItem || parentItem == rootItem)
|
---|
| 168 | return QModelIndex();
|
---|
| 169 |
|
---|
| 170 | return createIndex(parentItem->row(), 0, parentItem);
|
---|
| 171 | }
|
---|
| 172 | //! [9]
|
---|
| 173 |
|
---|
| 174 | //! [10]
|
---|
| 175 | int DomModel::rowCount(const QModelIndex &parent) const
|
---|
| 176 | {
|
---|
| 177 | if (parent.column() > 0)
|
---|
| 178 | return 0;
|
---|
| 179 |
|
---|
| 180 | DomItem *parentItem;
|
---|
| 181 |
|
---|
| 182 | if (!parent.isValid())
|
---|
| 183 | parentItem = rootItem;
|
---|
| 184 | else
|
---|
| 185 | parentItem = static_cast<DomItem*>(parent.internalPointer());
|
---|
| 186 |
|
---|
| 187 | return parentItem->node().childNodes().count();
|
---|
| 188 | }
|
---|
| 189 | //! [10]
|
---|