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 | /* |
52 | treeitem.cpp |
53 | |
54 | A container for items of data supplied by the simple tree model. |
55 | */ |
56 | |
57 | #include "treeitem.h" |
58 | |
59 | //! [0] |
60 | TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent) |
61 | : itemData(data), parentItem(parent) |
62 | {} |
63 | //! [0] |
64 | |
65 | //! [1] |
66 | TreeItem::~TreeItem() |
67 | { |
68 | qDeleteAll(childItems); |
69 | } |
70 | //! [1] |
71 | |
72 | //! [2] |
73 | TreeItem *TreeItem::child(int number) |
74 | { |
75 | if (number < 0 || number >= childItems.size()) |
76 | return nullptr; |
77 | return childItems.at(number); |
78 | } |
79 | //! [2] |
80 | |
81 | //! [3] |
82 | int TreeItem::childCount() const |
83 | { |
84 | return childItems.count(); |
85 | } |
86 | //! [3] |
87 | |
88 | //! [4] |
89 | int TreeItem::childNumber() const |
90 | { |
91 | if (parentItem) |
92 | return parentItem->childItems.indexOf(const_cast<TreeItem*>(this)); |
93 | return 0; |
94 | } |
95 | //! [4] |
96 | |
97 | //! [5] |
98 | int TreeItem::columnCount() const |
99 | { |
100 | return itemData.count(); |
101 | } |
102 | //! [5] |
103 | |
104 | //! [6] |
105 | QVariant TreeItem::data(int column) const |
106 | { |
107 | if (column < 0 || column >= itemData.size()) |
108 | return QVariant(); |
109 | return itemData.at(column); |
110 | } |
111 | //! [6] |
112 | |
113 | //! [7] |
114 | bool TreeItem::insertChildren(int position, int count, int columns) |
115 | { |
116 | if (position < 0 || position > childItems.size()) |
117 | return false; |
118 | |
119 | for (int row = 0; row < count; ++row) { |
120 | QList<QVariant> data(columns); |
121 | TreeItem *item = new TreeItem(data, this); |
122 | childItems.insert(position, item); |
123 | } |
124 | |
125 | return true; |
126 | } |
127 | //! [7] |
128 | |
129 | //! [8] |
130 | bool TreeItem::insertColumns(int position, int columns) |
131 | { |
132 | if (position < 0 || position > itemData.size()) |
133 | return false; |
134 | |
135 | for (int column = 0; column < columns; ++column) |
136 | itemData.insert(position, QVariant()); |
137 | |
138 | for (TreeItem *child : qAsConst(childItems)) |
139 | child->insertColumns(position, columns); |
140 | |
141 | return true; |
142 | } |
143 | //! [8] |
144 | |
145 | //! [9] |
146 | TreeItem *TreeItem::parent() |
147 | { |
148 | return parentItem; |
149 | } |
150 | //! [9] |
151 | |
152 | //! [10] |
153 | bool TreeItem::removeChildren(int position, int count) |
154 | { |
155 | if (position < 0 || position + count > childItems.size()) |
156 | return false; |
157 | |
158 | for (int row = 0; row < count; ++row) |
159 | delete childItems.takeAt(position); |
160 | |
161 | return true; |
162 | } |
163 | //! [10] |
164 | |
165 | bool TreeItem::removeColumns(int position, int columns) |
166 | { |
167 | if (position < 0 || position + columns > itemData.size()) |
168 | return false; |
169 | |
170 | for (int column = 0; column < columns; ++column) |
171 | itemData.remove(position); |
172 | |
173 | for (TreeItem *child : qAsConst(childItems)) |
174 | child->removeColumns(position, columns); |
175 | |
176 | return true; |
177 | } |
178 | |
179 | //! [11] |
180 | bool TreeItem::setData(int column, const QVariant &value) |
181 | { |
182 | if (column < 0 || column >= itemData.size()) |
183 | return false; |
184 | |
185 | itemData[column] = value; |
186 | return true; |
187 | } |
188 | //! [11] |
189 | |