~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to lib/widgets/propeditor/propertylist.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2004 by Alexander Dymo                                  *
3
 
 *   cloudtemple@mskat.net                                                 *
4
 
 *                                                                         *
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.                       *
9
 
 *                                                                         *
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.                          *
14
 
 *                                                                         *
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"
21
 
 
22
 
#include "property.h"
23
 
#include "multiproperty.h"
24
 
 
25
 
namespace PropertyLib{
26
 
 
27
 
PropertyList::PropertyList()
28
 
    :QObject(0, 0), m_propertyOwner(true)
29
 
{
30
 
}
31
 
 
32
 
PropertyList::PropertyList(bool propertyOwner)
33
 
    :QObject(0, 0), m_propertyOwner(propertyOwner)
34
 
{
35
 
}
36
 
 
37
 
PropertyList::~PropertyList()
38
 
{
39
 
    clear();
40
 
}
41
 
 
42
 
MultiProperty *PropertyList::operator[](const QString &name)
43
 
{
44
 
    if (m_list.contains(name))
45
 
        return m_list[name];
46
 
    else
47
 
        return new MultiProperty(this);
48
 
}
49
 
 
50
 
MultiProperty *PropertyList::property( const QString &name )
51
 
{
52
 
        if (m_list.contains(name))
53
 
                return m_list[name];
54
 
        else
55
 
                return new MultiProperty(this);
56
 
}
57
 
 
58
 
void PropertyList::addProperty(Property *property)
59
 
{
60
 
    if (property == 0)
61
 
        return;
62
 
    MultiProperty *mp = 0;
63
 
    if ( m_list.contains(property->name()) )
64
 
    {
65
 
        mp = m_list[property->name()];
66
 
        mp->addProperty(property);
67
 
    }
68
 
    else
69
 
    {
70
 
        mp = new MultiProperty(this, property);
71
 
        m_list[property->name()] = mp;
72
 
        addToGroup("", mp);
73
 
    }
74
 
}
75
 
 
76
 
void PropertyList::addProperty(const QString &group, Property *property)
77
 
{
78
 
    if (property == 0)
79
 
        return;
80
 
 
81
 
    MultiProperty *mp = 0;
82
 
    if (m_list.contains(property->name()))
83
 
    {
84
 
        mp = m_list[property->name()];
85
 
        mp->addProperty(property);
86
 
    }
87
 
    else
88
 
    {
89
 
        mp = new MultiProperty(this, property);
90
 
        m_list[property->name()] = mp;
91
 
        addToGroup(group, mp);
92
 
    }
93
 
}
94
 
 
95
 
void PropertyList::removeProperty(Property *property)
96
 
{
97
 
    if (property == 0)
98
 
        return;
99
 
 
100
 
    if (m_propertyOwner)
101
 
        emit aboutToDeleteProperty(property);
102
 
 
103
 
    MultiProperty *mp = m_list[property->name()];
104
 
    QString group = m_groupOfProperty[mp];
105
 
    removeFromGroup(mp);
106
 
    QString pname = property->name();
107
 
    mp->removeProperty(property);
108
 
    if (m_propertyOwner)
109
 
        delete property;
110
 
    if (mp->list.count() == 0)
111
 
    {
112
 
//        qWarning("rp:            removing mp for %s itself", pname.ascii());
113
 
        m_list.remove(pname);
114
 
        delete mp;
115
 
    }
116
 
    else
117
 
        addToGroup(group, mp);
118
 
}
119
 
 
120
 
void PropertyList::removeProperty(const QString &name)
121
 
{
122
 
    if (m_list.contains(name))
123
 
    {
124
 
        QString group = m_groupOfProperty[m_list[name]];
125
 
        removeFromGroup(m_list[name]);
126
 
        Property *property;
127
 
        for (property = m_list[name]->list.first(); property; property = m_list[name]->list.next())
128
 
        {
129
 
            if (m_propertyOwner)
130
 
                emit aboutToDeleteProperty(property);
131
 
 
132
 
            m_list[property->name()]->removeProperty(property);
133
 
            if (m_propertyOwner)
134
 
                delete property;
135
 
        }
136
 
        if (m_list[name]->list.count() == 0)
137
 
        {
138
 
//            qWarning("rp2:            removing mp for %s itself", name.ascii());
139
 
            delete m_list[name];
140
 
            m_list.remove(name);
141
 
        }
142
 
        else
143
 
        {
144
 
            addToGroup(group, m_list[name]);
145
 
        }
146
 
    }
147
 
}
148
 
 
149
 
const QValueList<QPair<QString, QValueList<QString> > >& PropertyList::propertiesOfGroup() const
150
 
{
151
 
    return m_propertiesOfGroup;
152
 
}
153
 
 
154
 
const QMap<MultiProperty*, QString>& PropertyList::groupOfProperty() const
155
 
{
156
 
    return m_groupOfProperty;
157
 
}
158
 
 
159
 
void PropertyList::addToGroup(const QString &group, MultiProperty *property)
160
 
{
161
 
    if (!property)
162
 
        return;
163
 
 
164
 
    //do not add same property to the group twice
165
 
    if (m_groupOfProperty.contains(property) && (m_groupOfProperty[property] == group))
166
 
        return;
167
 
 
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)
171
 
    {
172
 
        if ((*it).first == group)
173
 
        {
174
 
            groupPair = &(*it);
175
 
            break;
176
 
        }
177
 
    }
178
 
    if (groupPair == 0)
179
 
    {
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;
185
 
        return;
186
 
    }
187
 
    //check if group already contains property with the same name
188
 
    if (!groupPair->second.contains(property->name()))
189
 
        groupPair->second.append(property->name());
190
 
 
191
 
    m_groupOfProperty[property] = group;
192
 
}
193
 
 
194
 
void PropertyList::removeFromGroup(MultiProperty *property)
195
 
{
196
 
    QString group = m_groupOfProperty[property];
197
 
//    qWarning("removeFromGroup group=%s", group.ascii());
198
 
 
199
 
    for(QValueList<QPair<QString, QValueList<QString> > >::iterator it = m_propertiesOfGroup.begin();
200
 
        it != m_propertiesOfGroup.end(); ++it)
201
 
    {
202
 
//        qWarning("removeFromGroup checking %s", (*it).first.ascii());
203
 
        if ((*it).first == group)
204
 
        {
205
 
//            qWarning("removeFromGroup removing %s", property->name().ascii());
206
 
            (*it).second.remove(property->name());
207
 
            break;
208
 
        }
209
 
    }
210
 
 
211
 
    m_groupOfProperty.remove(property);
212
 
}
213
 
 
214
 
void PropertyList::clear( )
215
 
{
216
 
    for (QMap<QString, MultiProperty*>::iterator it = m_list.begin(); it != m_list.end(); ++it)
217
 
        removeProperty(it.key());
218
 
}
219
 
 
220
 
bool PropertyList::contains( const QString & name )
221
 
{
222
 
    if (m_list.contains(name))
223
 
        return true;
224
 
    return false;
225
 
}
226
 
 
227
 
QPtrList<Property> PropertyList::properties(const QString &name)
228
 
{
229
 
    if (m_list.contains(name))
230
 
        return m_list[name]->list;
231
 
    return QPtrList<Property>();
232
 
}
233
 
 
234
 
PropertyList::Iterator PropertyList::begin()
235
 
{
236
 
    return Iterator(this);
237
 
}
238
 
 
239
 
PropertyList::Iterator PropertyList::end()
240
 
{
241
 
    return Iterator(this, true);
242
 
}
243
 
 
244
 
//PropertyList::Iterator class
245
 
 
246
 
PropertyList::Iterator::Iterator(PropertyList *list)
247
 
    :m_list(list)
248
 
{
249
 
    current = m_list->m_list.begin();
250
 
}
251
 
 
252
 
PropertyList::Iterator::Iterator(PropertyList *list, bool // end
253
 
                                 )
254
 
    :m_list(list)
255
 
{
256
 
    current = m_list->m_list.end();
257
 
}
258
 
 
259
 
void PropertyList::Iterator::operator ++()
260
 
{
261
 
    next();
262
 
}
263
 
 
264
 
void PropertyList::Iterator::operator ++(int)
265
 
{
266
 
    next();
267
 
}
268
 
 
269
 
void PropertyList::Iterator::next()
270
 
{
271
 
    ++current;
272
 
}
273
 
 
274
 
MultiProperty *PropertyList::Iterator::operator *()
275
 
{
276
 
    return data();
277
 
}
278
 
 
279
 
QString PropertyList::Iterator::key()
280
 
{
281
 
    return current.key();
282
 
}
283
 
 
284
 
MultiProperty *PropertyList::Iterator::data()
285
 
{
286
 
    return current.data();
287
 
}
288
 
 
289
 
bool PropertyList::Iterator::operator !=(Iterator it)
290
 
{
291
 
    return current != it.current;
292
 
}
293
 
 
294
 
 
295
 
// PropertyBuffer class
296
 
 
297
 
 
298
 
 
299
 
 
300
 
 
301
 
PropertyBuffer::PropertyBuffer( )
302
 
    :PropertyList(false)
303
 
{
304
 
}
305
 
 
306
 
void PropertyBuffer::intersect(const PropertyList *list)
307
 
{
308
 
    qWarning("PropertyBuffer::intersect");
309
 
    for (QMap<QString, MultiProperty*>::iterator it = m_list.begin(); it != m_list.end(); ++it)
310
 
    {
311
 
//        qWarning("intersect:: for mp = %s", it.data()->name().ascii());
312
 
        if (list->m_list.contains(it.key()))
313
 
        {
314
 
/*            qWarning("intersect::     list contains %s", it.key().ascii());
315
 
            if ( (*(it.data()) == *(list->m_list[it.key()])))
316
 
                qWarning("intersect::     equal properties");
317
 
            else
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()]) )
321
 
            {
322
 
//                qWarning("intersect::     equal properties, adding");
323
 
                it.data()->addProperty(list->m_list[it.key()]);
324
 
                continue;
325
 
            }
326
 
        }
327
 
//        qWarning("intersect::     removing %s from intersection", it.key().ascii());
328
 
        removeProperty(it.key());
329
 
    }
330
 
    connect(list, SIGNAL(propertyValueChanged(Property*)), this, SLOT(intersectedValueChanged(Property*)));
331
 
}
332
 
 
333
 
void PropertyBuffer::intersectedValueChanged(Property *property)
334
 
{
335
 
//     qWarning("PropertyBuffer::intersectedValueChanged");
336
 
    QString propertyName = property->name();
337
 
    if (!contains(propertyName))
338
 
        return;
339
 
 
340
 
    MultiProperty mp(property);
341
 
    if (mp == *m_list[propertyName])
342
 
    {
343
 
        Property *prop;
344
 
        QPtrList<Property> props = properties(propertyName);
345
 
        for (prop = props.first(); prop; prop = props.next())
346
 
            emit propertyValueChanged(prop);
347
 
    }
348
 
}
349
 
 
350
 
PropertyBuffer::PropertyBuffer(PropertyList *list)
351
 
    :PropertyList(false)
352
 
{
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)
356
 
    {
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;
361
 
    }
362
 
    connect(list, SIGNAL(propertyValueChanged(Property*)), this, SLOT(intersectedValueChanged(Property*)));
363
 
}
364
 
 
365
 
}
366
 
 
367
 
#ifndef PURE_QT
368
 
#include "propertylist.moc"
369
 
#endif