1
/***************************************************************************
2
* Copyright (C) 2004 by Alexander Dymo *
3
* cloudtemple@mskat.net *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU Library General Public License as *
7
* published by the Free Software Foundation; either version 2 of the *
8
* License, or (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU Library General Public *
16
* License along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
***************************************************************************/
20
#include "propertylist.h"
23
#include "multiproperty.h"
25
namespace PropertyLib{
27
PropertyList::PropertyList()
28
:QObject(0, 0), m_propertyOwner(true)
32
PropertyList::PropertyList(bool propertyOwner)
33
:QObject(0, 0), m_propertyOwner(propertyOwner)
37
PropertyList::~PropertyList()
42
MultiProperty *PropertyList::operator[](const QString &name)
44
if (m_list.contains(name))
47
return new MultiProperty(this);
50
MultiProperty *PropertyList::property( const QString &name )
52
if (m_list.contains(name))
55
return new MultiProperty(this);
58
void PropertyList::addProperty(Property *property)
62
MultiProperty *mp = 0;
63
if ( m_list.contains(property->name()) )
65
mp = m_list[property->name()];
66
mp->addProperty(property);
70
mp = new MultiProperty(this, property);
71
m_list[property->name()] = mp;
76
void PropertyList::addProperty(const QString &group, Property *property)
81
MultiProperty *mp = 0;
82
if (m_list.contains(property->name()))
84
mp = m_list[property->name()];
85
mp->addProperty(property);
89
mp = new MultiProperty(this, property);
90
m_list[property->name()] = mp;
91
addToGroup(group, mp);
95
void PropertyList::removeProperty(Property *property)
101
emit aboutToDeleteProperty(property);
103
MultiProperty *mp = m_list[property->name()];
104
QString group = m_groupOfProperty[mp];
106
QString pname = property->name();
107
mp->removeProperty(property);
110
if (mp->list.count() == 0)
112
// qWarning("rp: removing mp for %s itself", pname.ascii());
113
m_list.remove(pname);
117
addToGroup(group, mp);
120
void PropertyList::removeProperty(const QString &name)
122
if (m_list.contains(name))
124
QString group = m_groupOfProperty[m_list[name]];
125
removeFromGroup(m_list[name]);
127
for (property = m_list[name]->list.first(); property; property = m_list[name]->list.next())
130
emit aboutToDeleteProperty(property);
132
m_list[property->name()]->removeProperty(property);
136
if (m_list[name]->list.count() == 0)
138
// qWarning("rp2: removing mp for %s itself", name.ascii());
144
addToGroup(group, m_list[name]);
149
const QValueList<QPair<QString, QValueList<QString> > >& PropertyList::propertiesOfGroup() const
151
return m_propertiesOfGroup;
154
const QMap<MultiProperty*, QString>& PropertyList::groupOfProperty() const
156
return m_groupOfProperty;
159
void PropertyList::addToGroup(const QString &group, MultiProperty *property)
164
//do not add same property to the group twice
165
if (m_groupOfProperty.contains(property) && (m_groupOfProperty[property] == group))
168
QPair<QString, QValueList<QString> > *groupPair = 0;
169
for(QValueList<QPair<QString, QValueList<QString> > >::iterator it = m_propertiesOfGroup.begin();
170
it != m_propertiesOfGroup.end(); ++it)
172
if ((*it).first == group)
180
groupPair = new QPair<QString, QValueList<QString> >();
181
groupPair->first = group;
182
groupPair->second.append(property->name());
183
m_propertiesOfGroup.append(*groupPair);
184
m_groupOfProperty[property] = group;
187
//check if group already contains property with the same name
188
if (!groupPair->second.contains(property->name()))
189
groupPair->second.append(property->name());
191
m_groupOfProperty[property] = group;
194
void PropertyList::removeFromGroup(MultiProperty *property)
196
QString group = m_groupOfProperty[property];
197
// qWarning("removeFromGroup group=%s", group.ascii());
199
for(QValueList<QPair<QString, QValueList<QString> > >::iterator it = m_propertiesOfGroup.begin();
200
it != m_propertiesOfGroup.end(); ++it)
202
// qWarning("removeFromGroup checking %s", (*it).first.ascii());
203
if ((*it).first == group)
205
// qWarning("removeFromGroup removing %s", property->name().ascii());
206
(*it).second.remove(property->name());
211
m_groupOfProperty.remove(property);
214
void PropertyList::clear( )
216
for (QMap<QString, MultiProperty*>::iterator it = m_list.begin(); it != m_list.end(); ++it)
217
removeProperty(it.key());
220
bool PropertyList::contains( const QString & name )
222
if (m_list.contains(name))
227
QPtrList<Property> PropertyList::properties(const QString &name)
229
if (m_list.contains(name))
230
return m_list[name]->list;
231
return QPtrList<Property>();
234
PropertyList::Iterator PropertyList::begin()
236
return Iterator(this);
239
PropertyList::Iterator PropertyList::end()
241
return Iterator(this, true);
244
//PropertyList::Iterator class
246
PropertyList::Iterator::Iterator(PropertyList *list)
249
current = m_list->m_list.begin();
252
PropertyList::Iterator::Iterator(PropertyList *list, bool // end
256
current = m_list->m_list.end();
259
void PropertyList::Iterator::operator ++()
264
void PropertyList::Iterator::operator ++(int)
269
void PropertyList::Iterator::next()
274
MultiProperty *PropertyList::Iterator::operator *()
279
QString PropertyList::Iterator::key()
281
return current.key();
284
MultiProperty *PropertyList::Iterator::data()
286
return current.data();
289
bool PropertyList::Iterator::operator !=(Iterator it)
291
return current != it.current;
295
// PropertyBuffer class
301
PropertyBuffer::PropertyBuffer( )
306
void PropertyBuffer::intersect(const PropertyList *list)
308
qWarning("PropertyBuffer::intersect");
309
for (QMap<QString, MultiProperty*>::iterator it = m_list.begin(); it != m_list.end(); ++it)
311
// qWarning("intersect:: for mp = %s", it.data()->name().ascii());
312
if (list->m_list.contains(it.key()))
314
/* qWarning("intersect:: list contains %s", it.key().ascii());
315
if ( (*(it.data()) == *(list->m_list[it.key()])))
316
qWarning("intersect:: equal properties");
318
qWarning("intersect:: not equal properties");*/
319
if ( ((*it.data()) == *(list->m_list[it.key()]))
320
&& (list->m_groupOfProperty[list->m_list[it.key()]] == m_groupOfProperty[it.data()]) )
322
// qWarning("intersect:: equal properties, adding");
323
it.data()->addProperty(list->m_list[it.key()]);
327
// qWarning("intersect:: removing %s from intersection", it.key().ascii());
328
removeProperty(it.key());
330
connect(list, SIGNAL(propertyValueChanged(Property*)), this, SLOT(intersectedValueChanged(Property*)));
333
void PropertyBuffer::intersectedValueChanged(Property *property)
335
// qWarning("PropertyBuffer::intersectedValueChanged");
336
QString propertyName = property->name();
337
if (!contains(propertyName))
340
MultiProperty mp(property);
341
if (mp == *m_list[propertyName])
344
QPtrList<Property> props = properties(propertyName);
345
for (prop = props.first(); prop; prop = props.next())
346
emit propertyValueChanged(prop);
350
PropertyBuffer::PropertyBuffer(PropertyList *list)
353
//deep copy of m_list
354
for (QMap<QString, MultiProperty*>::const_iterator it = list->m_list.begin();
355
it != list->m_list.end(); ++it)
357
MultiProperty *mp = new MultiProperty(*it.data());
358
mp->m_propertyList = this;
359
addToGroup(list->m_groupOfProperty[it.data()], mp);
360
m_list[it.key()] = mp;
362
connect(list, SIGNAL(propertyValueChanged(Property*)), this, SLOT(intersectedValueChanged(Property*)));
368
#include "propertylist.moc"