1 /* massXpert - the true massist's program.
2 	 --------------------------------------
3 	 Copyright(C) 2006,2007 Filippo Rusconi
4 
5 http://www.massxpert.org
6 
7 This file is part of the massXpert project.
8 
9 The massxpert project is the successor to the "GNU polyxmass"
10 project that is an official GNU project package(see
11 www.gnu.org). The massXpert project is not endorsed by the GNU
12 project, although it is released ---in its entirety--- under the
13 GNU General Public License. A huge part of the code in massXpert
14 is actually a C++ rewrite of code in GNU polyxmass. As such
15 massXpert was started at the Centre National de la Recherche
16 Scientifique(FRANCE), that granted me the formal authorization to
17 publish it under this Free Software License.
18 
19 This software is free software; you can redistribute it and/or
20 modify it under the terms of the GNU  General Public
21 License version 3, as published by the Free Software Foundation.
22 
23 
24 This software is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27 General Public License for more details.
28 
29 You should have received a copy of the GNU General Public License
30 along with this software; if not, write to the
31 
32 Free Software Foundation, Inc.,
33 
34 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
35 */
36 
37 
38 /////////////////////// Qt includes
39 
40 
41 /////////////////////// Local includes
42 #include "propListHolder.hpp"
43 
44 
45 namespace massXpert
46 {
47 
PropListHolder()48 	PropListHolder::PropListHolder()
49 	{
50 	}
51 
52 
PropListHolder(const PropListHolder & other)53 	PropListHolder::PropListHolder(const PropListHolder &other)
54 	{
55 		for (int iter = 0; iter < other.m_propList.size(); ++iter)
56 		{
57 			Prop *prop = other.m_propList.at(iter);
58 			Q_ASSERT(prop);
59 
60 			void *newProp = static_cast<void *>(prop->clone());
61 			Q_ASSERT(newProp);
62 
63 			appendProp(static_cast<Prop *>(newProp));
64 		}
65 	}
66 
67 
~PropListHolder()68 	PropListHolder::~PropListHolder()
69 	{
70 		while(!m_propList.isEmpty())
71 			delete(m_propList.takeFirst());
72 	}
73 
74 
75 	PropListHolder *
clone() const76 		PropListHolder::clone() const
77 		{
78 			PropListHolder *other = new PropListHolder(*this);
79 
80 			return other;
81 		}
82 
83 
84 	void
clone(PropListHolder * other) const85 		PropListHolder::clone(PropListHolder *other) const
86 		{
87 			Q_ASSERT(other);
88 
89 			while(!other->m_propList.isEmpty())
90 				delete(other->m_propList.takeFirst());
91 
92 			for (int iter = 0; iter < m_propList.size(); ++iter)
93 			{
94 				Prop *prop = m_propList.at(iter);
95 				Q_ASSERT(prop);
96 
97 				void *newProp = static_cast<void *>(prop->clone());
98 				Q_ASSERT(newProp);
99 
100 				other->appendProp(static_cast<Prop *>(newProp));
101 			}
102 		}
103 
104 
105 	void
mold(const PropListHolder & other)106 		PropListHolder::mold(const PropListHolder &other)
107 		{
108 
109 
110 			while(!m_propList.isEmpty())
111 				delete(m_propList.takeFirst());
112 
113 			for (int iter = 0; iter < other.m_propList.size(); ++iter)
114 			{
115 				Prop *prop = other.m_propList.at(iter);
116 				Q_ASSERT(prop);
117 
118 				void *newProp = static_cast<void *>(prop->clone());
119 				Q_ASSERT(newProp);
120 
121 				appendProp(static_cast<Prop *>(newProp));
122 			}
123 		}
124 
125 
126 	PropListHolder &
operator =(const PropListHolder & other)127 		PropListHolder::operator =(const PropListHolder &other)
128 		{
129 			if (&other != this)
130 				mold(other);
131 
132 			return *this;
133 		}
134 
135 
136 	const QList<Prop *> &
propList() const137 		PropListHolder::propList() const
138 		{
139 			return  m_propList;
140 		}
141 
142 
143 	QList<Prop *> &
propList()144 		PropListHolder::propList()
145 		{
146 			return m_propList;
147 		}
148 
149 
150 	Prop *
prop(const QString & name,int * index)151 		PropListHolder::prop(const QString &name, int *index)
152 		{
153 			for (int iter = 0; iter < m_propList.size(); ++iter)
154 			{
155 				Prop *localProp = m_propList.at(iter);
156 				Q_ASSERT(localProp);
157 
158 				if(localProp->name() == name)
159 				{
160 					if (index)
161 						*index = iter;
162 
163 					return localProp;
164 				}
165 			}
166 
167 			return 0;
168 		}
169 
170 
171 	int
propIndex(const QString & name,Prop * prop)172 		PropListHolder::propIndex(const QString &name, Prop *prop)
173 		{
174 			for (int iter = 0; iter < m_propList.size(); ++iter)
175 			{
176 				Prop *localProp = m_propList.at(iter);
177 				Q_ASSERT(localProp);
178 
179 				if(localProp->name() == name)
180 				{
181 					if (prop)
182 						prop = localProp;
183 
184 					return iter;
185 				}
186 			}
187 
188 			return -1;
189 		}
190 
191 
192 	bool
appendProp(Prop * prop)193 		PropListHolder::appendProp(Prop *prop)
194 		{
195 			m_propList.append(prop);
196 
197 			return true;
198 		}
199 
200 
201 	bool
removeProp(Prop * prop)202 		PropListHolder::removeProp(Prop *prop)
203 		{
204 			if (m_propList.removeAll(prop))
205 				return true;
206 
207 			return false;
208 		}
209 
210 
211 	bool
removeProp(const QString & name)212 		PropListHolder::removeProp(const QString &name)
213 		{
214 			for (int iter = 0; iter < m_propList.size(); ++iter)
215 			{
216 				if(m_propList.at(iter)->name() == name)
217 				{
218 					m_propList.removeAt(iter);
219 
220 					return true;
221 				}
222 			}
223 
224 			return false;
225 		}
226 
227 } // namespace massXpert
228