1 /* This file is part of the KDE project
2    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3    Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
4    Copyright (C) 2004-2017 Jarosław Staniek <staniek@kde.org>
5 
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public
8    License as published by the Free Software Foundation; either
9    version 2 of the License, or (at your option) any later version.
10 
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15 
16    You should have received a copy of the GNU Library General Public License
17    along with this library; see the file COPYING.LIB.  If not, write to
18    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20 */
21 
22 #include "KPropertySetBuffer.h"
23 #include "KPropertySet_p.h"
24 #include "KProperty_p.h"
25 
26 class Q_DECL_HIDDEN KPropertySetBuffer::Private
27 {
28 public:
Private()29     Private()
30     {
31     }
32 };
33 
KPropertySetBuffer()34 KPropertySetBuffer::KPropertySetBuffer()
35         : KPropertySet(false)
36         , d(new Private)
37 {
38     connect(this, SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
39             this, SLOT(intersectedChanged(KPropertySet&,KProperty&)));
40 
41     connect(this, SIGNAL(propertyReset(KPropertySet&,KProperty&)),
42             this, SLOT(intersectedReset(KPropertySet&,KProperty&)));
43 }
44 
KPropertySetBuffer(const KPropertySet & set)45 KPropertySetBuffer::KPropertySetBuffer(const KPropertySet& set)
46         : KPropertySet(false)
47         , d(new Private)
48 {
49     connect(this, SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
50             this, SLOT(intersectedChanged(KPropertySet&,KProperty&)));
51 
52     connect(this, SIGNAL(propertyReset(KPropertySet&,KProperty&)),
53             this, SLOT(intersectedReset(KPropertySet&,KProperty&)));
54 
55     init(set);
56 }
57 
~KPropertySetBuffer()58 KPropertySetBuffer::~KPropertySetBuffer()
59 {
60     delete d;
61 }
62 
init(const KPropertySet & set)63 void KPropertySetBuffer::init(const KPropertySet& set)
64 {
65     //deep copy of set
66     const QList<KProperty*>::ConstIterator itEnd(KPropertySetPrivate::d(&set)->listConstEnd());
67     for (QList<KProperty*>::ConstIterator it(KPropertySetPrivate::d(&set)->listConstIterator());
68         it!=itEnd; ++it)
69     {
70         KProperty *prop = new KProperty(*(*it));
71         QByteArray group = KPropertySetPrivate::d(&set)->groupForProperty(*it);
72         const QString groupCaption = set.groupCaption(group);
73         setGroupCaption(group, groupCaption);
74         addProperty(prop, group);
75         prop->d->addRelatedProperty(*it);
76     }
77 }
78 
intersect(const KPropertySet & set)79 void KPropertySetBuffer::intersect(const KPropertySet& set)
80 {
81     if (isEmpty()) {
82         init(set);
83         return;
84     }
85 
86     const QList<KProperty*>::ConstIterator itEnd(KPropertySetPrivate::d(&set)->listConstEnd());
87     for (QList<KProperty*>::ConstIterator it(KPropertySetPrivate::d(&set)->listConstIterator());
88         it!=itEnd; ++it)
89     {
90         const QByteArray key( (*it)->name() );
91         KProperty *property = KPropertySetPrivate::d(&set)->property( key );
92         if (property) {
93             blockSignals(true);
94             (*it)->resetValue();
95             (*it)->d->addRelatedProperty(property);
96             blockSignals(false);
97         } else {
98             removeProperty(key);
99         }
100     }
101 }
102 
intersectedChanged(KPropertySet & set,KProperty & prop)103 void KPropertySetBuffer::intersectedChanged(KPropertySet& set, KProperty& prop)
104 {
105     Q_UNUSED(set);
106     if (!contains(prop.name()))
107         return;
108 
109     const QList<KProperty*> *props = prop.d->relatedProperties;
110     for (QList<KProperty*>::ConstIterator it = props->constBegin(); it != props->constEnd(); ++it) {
111         (*it)->setValue(prop.value(), KProperty::ValueOption::IgnoreOld);
112     }
113 }
114 
intersectedReset(KPropertySet & set,KProperty & prop)115 void KPropertySetBuffer::intersectedReset(KPropertySet& set, KProperty& prop)
116 {
117     Q_UNUSED(set);
118     if (!contains(prop.name()))
119         return;
120 
121     const QList<KProperty*> *props = prop.d->relatedProperties;
122     for (QList<KProperty*>::ConstIterator it = props->constBegin(); it != props->constEnd(); ++it)  {
123         (*it)->setValue(prop.value(), KProperty::ValueOption::IgnoreOld);
124     }
125 }
126