1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the examples of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
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.
25 **
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."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 /*
42     treemodel.cpp
43 
44     Provides a simple tree model to show how to create and use hierarchical
45     models.
46 */
47 
48 #include <QtGui>
49 
50 #include "treeitem.h"
51 #include "treemodel.h"
52 
TreeModel(QObject * parent)53 TreeModel::TreeModel(QObject *parent )
54 {
55 	m_rootItem = nullptr;
56 }
57 
58 //! [0]
TreeModel(const QString & data,QObject * parent)59 TreeModel::TreeModel(const QString &data, QObject *parent)
60     : QAbstractItemModel(parent)
61 {
62     QList<QVariant> rootData;
63     rootData << "Title" << "Summary";
64     m_rootItem = new TreeItem(rootData);
65     setupModelData(data.split(QString("\n")), m_rootItem);
66 }
67 //! [0]
68 
69 //! [1]
~TreeModel()70 TreeModel::~TreeModel()
71 {
72     delete m_rootItem;
73 }
74 //! [1]
75 
76 //! [2]
columnCount(const QModelIndex & parent) const77 int TreeModel::columnCount(const QModelIndex &parent) const
78 {
79     if (parent.isValid())
80         return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
81     else
82         return m_rootItem->columnCount();
83 }
84 //! [2]
85 
86 //! [3]
data(const QModelIndex & index,int role) const87 QVariant TreeModel::data(const QModelIndex &index, int role) const
88 {
89     if (!index.isValid())
90         return QVariant();
91 
92     if (role != Qt::DisplayRole)
93         return QVariant();
94 
95     TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
96 
97     return item->data(index.column());
98 }
99 //! [3]
100 
101 //! [4]
flags(const QModelIndex & index) const102 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
103 {
104     if (!index.isValid())
105 		return Qt::ItemFlags();
106 
107     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
108 }
109 //! [4]
110 
111 //! [5]
headerData(int section,Qt::Orientation orientation,int role) const112 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
113                                int role) const
114 {
115     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
116         return m_rootItem->data(section);
117 
118     return QVariant();
119 }
120 //! [5]
121 
122 //! [6]
index(int row,int column,const QModelIndex & parent) const123 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
124             const
125 {
126     if (!hasIndex(row, column, parent))
127         return QModelIndex();
128 
129     TreeItem *parentItem;
130 
131     if (!parent.isValid())
132         parentItem = m_rootItem;
133     else
134         parentItem = static_cast<TreeItem*>(parent.internalPointer());
135 
136     TreeItem *childItem = parentItem->child(row);
137     if (childItem)
138         return createIndex(row, column, childItem);
139     else
140         return QModelIndex();
141 }
142 //! [6]
143 
144 //! [7]
parent(const QModelIndex & index) const145 QModelIndex TreeModel::parent(const QModelIndex &index) const
146 {
147     if (!index.isValid())
148         return QModelIndex();
149 
150     TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
151     TreeItem *parentItem = childItem->parent();
152 
153     if (parentItem == m_rootItem)
154         return QModelIndex();
155 
156     return createIndex(parentItem->row(), 0, parentItem);
157 }
158 //! [7]
159 
160 //! [8]
rowCount(const QModelIndex & parent) const161 int TreeModel::rowCount(const QModelIndex &parent) const
162 {
163     TreeItem *parentItem;
164     if (parent.column() > 0)
165         return 0;
166 
167     if (!parent.isValid())
168         parentItem = m_rootItem;
169     else
170         parentItem = static_cast<TreeItem*>(parent.internalPointer());
171 
172     return parentItem->childCount();
173 }
174 //! [8]
175 
setupModelData(const QStringList & lines,TreeItem * parent)176 void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
177 {
178     QList<TreeItem*> parents;
179     QList<int> indentations;
180     parents << parent;
181     indentations << 0;
182 
183     int number = 0;
184 
185     while (number < lines.count()) {
186         int position = 0;
187         while (position < lines[number].length()) {
188             if (lines[number].mid(position, 1) != " ")
189                 break;
190             position++;
191         }
192 
193         QString lineData = lines[number].mid(position).trimmed();
194 
195         if (!lineData.isEmpty()) {
196             // Read the column data from the rest of the line.
197             QStringList columnStrings = lineData.split("\t", Qt::SkipEmptyParts);
198             QList<QVariant> columnData;
199             for (int column = 0; column < columnStrings.count(); ++column)
200                 columnData << columnStrings[column];
201 
202             if (position > indentations.last()) {
203                 // The last child of the current parent is now the new parent
204                 // unless the current parent has no children.
205 
206                 if (parents.last()->childCount() > 0) {
207                     parents << parents.last()->child(parents.last()->childCount()-1);
208                     indentations << position;
209                 }
210             } else {
211                 while (position < indentations.last() && parents.count() > 0) {
212                     parents.pop_back();
213                     indentations.pop_back();
214                 }
215             }
216 
217             // Append a new item to the current parent's list of children.
218             parents.last()->appendChild(new TreeItem(columnData, parents.last()));
219         }
220 
221         number++;
222     }
223 }
224