1 /****************************************************************************
2 **
3 ** Modifications Copyright (C) 2016-2021 Kevin B, Hendricks, Stratford, ON, Canada
4 **
5 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
6 ** Contact: http://www.qt-project.org/legal
7 **
8 ** This file is part of the examples of the Qt Toolkit.
9 **
10 ** $QT_BEGIN_LICENSE:BSD$
11 ** You may use this file under the terms of the BSD license as follows:
12 **
13 ** "Redistribution and use in source and binary forms, with or without
14 ** modification, are permitted provided that the following conditions are
15 ** met:
16 **   * Redistributions of source code must retain the above copyright
17 **     notice, this list of conditions and the following disclaimer.
18 **   * Redistributions in binary form must reproduce the above copyright
19 **     notice, this list of conditions and the following disclaimer in
20 **     the documentation and/or other materials provided with the
21 **     distribution.
22 **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
23 **     of its contributors may be used to endorse or promote products derived
24 **     from this software without specific prior written permission.
25 **
26 **
27 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
38 **
39 ** $QT_END_LICENSE$
40 **
41 ****************************************************************************/
42 
43 /*
44     TreeItem.cpp
45 
46     A container for items of data supplied by the simple tree model.
47 */
48 
49 #include "Dialogs/TreeItem.h"
50 
51 #include <QStringList>
52 
TreeItem(const QVector<QVariant> & data,TreeItem * parent)53 TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent)
54 {
55     parentItem = parent;
56     itemData = data;
57     itemTips = data;
58 }
59 
~TreeItem()60 TreeItem::~TreeItem()
61 {
62     qDeleteAll(childItems);
63 }
64 
child(int number)65 TreeItem *TreeItem::child(int number)
66 {
67     return childItems.value(number);
68 }
69 
childCount() const70 int TreeItem::childCount() const
71 {
72     return childItems.count();
73 }
74 
childNumber() const75 int TreeItem::childNumber() const
76 {
77     if (parentItem)
78         return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
79 
80     return 0;
81 }
82 
columnCount() const83 int TreeItem::columnCount() const
84 {
85     return itemData.count();
86 }
87 
tip(int column) const88 QVariant TreeItem::tip(int column) const
89 {
90     return itemTips.value(column);
91 }
92 
data(int column) const93 QVariant TreeItem::data(int column) const
94 {
95     return itemData.value(column);
96 }
97 
insertChildren(int position,int count,int columns)98 bool TreeItem::insertChildren(int position, int count, int columns)
99 {
100     if (position < 0 || position > childItems.size())
101         return false;
102 
103     for (int row = 0; row < count; ++row) {
104         QVector<QVariant> data(columns);
105         TreeItem *item = new TreeItem(data, this);
106         childItems.insert(position, item);
107     }
108 
109     return true;
110 }
111 
112 
parent()113 TreeItem *TreeItem::parent()
114 {
115     return parentItem;
116 }
117 
removeChildren(int position,int count)118 bool TreeItem::removeChildren(int position, int count)
119 {
120     if (position < 0 || position + count > childItems.size())
121         return false;
122 
123     for (int row = 0; row < count; ++row)
124         delete childItems.takeAt(position);
125 
126     return true;
127 }
128 
moveChildUp(int position)129 bool TreeItem::moveChildUp(int position)
130 {
131     if (position <=0 || position > childItems.size() - 1)
132         return false;
133 
134     childItems.swap(position, position-1);
135     return true;
136 }
137 
moveChildDown(int position)138 bool TreeItem::moveChildDown(int position)
139 {
140     if (position < 0 || position >= childItems.size() - 1)
141         return false;
142 
143     childItems.swap(position, position+1);
144     return true;
145 }
146 
setData(int column,const QVariant & value)147 bool TreeItem::setData(int column, const QVariant &value)
148 {
149     if (column < 0 || column >= itemData.size())
150         return false;
151 
152     itemData[column] = value;
153     return true;
154 }
155 
setTips(int column,const QVariant & value)156 bool TreeItem::setTips(int column, const QVariant &value)
157 {
158     if (column < 0 || column >= itemTips.size())
159         return false;
160 
161     itemTips[column] = value;
162     return true;
163 }
164 
165