File: elementcollectionitem.cpp

package info (click to toggle)
qelectrotech 1%3A0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 91,680 kB
  • sloc: cpp: 62,335; xml: 750; sh: 264; perl: 258; makefile: 6
file content (246 lines) | stat: -rw-r--r-- 6,597 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
	Copyright 2006-2019 The QElectroTech Team
	This file is part of QElectroTech.

	QElectroTech is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	QElectroTech is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/

#include "elementcollectionitem.h"

/**
 * @brief ElementCollectionItem::ElementCollectionItem
 * Constructor
 */
ElementCollectionItem::ElementCollectionItem()
{}

/**
 * @brief ElementCollectionItem::clearData
 * Reset the data
 */
void ElementCollectionItem::clearData()
{
	setText(QString());
	setToolTip(QString());
	setIcon(QIcon());
	setData(QString());
}

/**
 * @brief ElementCollectionItem::lastItemForPath
 * Return the last existing item in this ElementCollectionItem hierarchy according to the given path.
 * Next_item is the first non existing item in this hierarchy according to the given path.
 * @param path : The path to find last item. The path must be in form : path/otherPath/.../.../myElement.elmt.
 * @param no_found_path : The first item that not exist in this hierarchy
 * @return : The last item that exist in this hierarchy, or nullptr can't find (an error was occurred, or path already exist)
 */
ElementCollectionItem *ElementCollectionItem::lastItemForPath(const QString &path, QString &no_found_path)
{
	QStringList str_list = path.split("/");
	if (str_list.isEmpty()) return nullptr;

	ElementCollectionItem *return_eci = this;
	foreach (QString str, str_list)
	{
		ElementCollectionItem *eci = return_eci->childWithCollectionName(str);
		if (!eci)
		{
			no_found_path = str;
			return return_eci;
		}
		else
			return_eci = eci;
	}

	return nullptr;
}

/**
 * @brief ElementCollectionItem::childWithCollectionName
 * Return the child with the collection name @name, else return nullptr
 * @param name
 * @return
 */
ElementCollectionItem *ElementCollectionItem::childWithCollectionName(const QString& name) const
{
	rowCount();
	foreach (QStandardItem *qsi, directChilds()) {
		ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsi);
		if (eci->name() == name)
			return eci;
	}

	return nullptr;
}

/**
 * @brief ElementCollectionItem::directChilds
 * Return the direct child of this item
 * @return
 */
QList<QStandardItem *> ElementCollectionItem::directChilds() const
{
	QList <QStandardItem *> item_list;

	for (int i=0 ; i<rowCount() ; i++)
		item_list.append(child(i));

	return item_list;
}

/**
 * @brief ElementCollectionItem::rowForInsertItem
 * Return the row for insert a new child item to this item with name @collection_name.
 * If row can't be found (collection_name is null, or already exist) return -1;
 * @param collection_name
 * @return
 */
int ElementCollectionItem::rowForInsertItem(const QString &name)
{
	if (name.isEmpty())
		return -1;

	QList <ElementCollectionItem *> child;
		//The item to insert is an element we search from element child
	if (name.endsWith(".elmt"))
	{
		child = elementsDirectChild();
			//There isn't element, we insert at last position
		if (child.isEmpty())
			return rowCount();
	}
		//The item is a directory, we search from directory child
	else
	{
		child = directoriesDirectChild();
		//There isn't directory, we insert at first position
		if(child.isEmpty())
			return 0;
	}

	foreach (ElementCollectionItem *eci, child)
		if (eci->name() > name)
			return model()->indexFromItem(eci).row();

	return (model()->indexFromItem(child.last()).row() + 1);
}

/**
 * @brief ElementCollectionItem::itemAtPath
 * @param path
 * @return the item at path or nullptr if doesn't exist
 */
ElementCollectionItem *ElementCollectionItem::itemAtPath(const QString &path)
{
	QStringList str_list = path.split("/");
	if (str_list.isEmpty())
		return nullptr;

	ElementCollectionItem *match_eci = this;
	foreach (QString str, str_list) {
		ElementCollectionItem *eci = match_eci->childWithCollectionName(str);
		if (!eci)
			return nullptr;
		else
			match_eci = eci;
	}

	return match_eci;
}

/**
 * @brief ElementCollectionItem::elementsDirectChild
 * @return The direct element child of this item
 */
QList<ElementCollectionItem *> ElementCollectionItem::elementsDirectChild() const
{
	QList <ElementCollectionItem *> element_child;

	foreach (QStandardItem *qsi, directChilds()) {
		ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsi);
		if (eci->isElement())
			element_child.append(eci);
	}

	return element_child;
}

/**
 * @brief ElementCollectionItem::directoriesDirectChild
 * @return the direct directory child of this item
 */
QList<ElementCollectionItem *> ElementCollectionItem::directoriesDirectChild() const
{
	QList <ElementCollectionItem *> dir_child;

	foreach (QStandardItem *qsi, directChilds()) {
		ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsi);
		if (eci->isDir())
			dir_child.append(eci);
	}

	return dir_child;
}

/**
 * @brief ElementCollectionItem::elementsChild
 * @return Every elements child (direct and indirect) of this item
 */
QList<ElementCollectionItem *> ElementCollectionItem::elementsChild() const
{
	QList <ElementCollectionItem *> list = elementsDirectChild();

	foreach (ElementCollectionItem *eci, directoriesChild())
		list.append(eci->elementsDirectChild());

	return list;
}

/**
 * @brief ElementCollectionItem::directoriesChild
 * @return Every directories child (direct and indirect) of this item
 */
QList<ElementCollectionItem *> ElementCollectionItem::directoriesChild() const
{
	QList<ElementCollectionItem *> list = directoriesDirectChild();
	QList<ElementCollectionItem *> child_list;
	foreach (ElementCollectionItem *eci, list) {
		child_list.append(eci->directoriesChild());
	}

	list.append(child_list);
	return list;
}

/**
 * @brief ElementCollectionItem::items
 * @return every childs of this item (direct and indirect childs)
 */
QList<ElementCollectionItem *> ElementCollectionItem::items() const
{
	QList <ElementCollectionItem *> list;

	for (int i=0 ; i<rowCount() ; i++) {
		ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(child(i));
		list.append(eci);
		list.append(eci->items());
	}

	return list;
}

void setUpData(ElementCollectionItem *eci)
{
	eci->setUpData();
}