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