File indexing completed on 2024-04-14 14:53:45

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
0003    Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
0004    Copyright (C) 2004-2017 Jarosław Staniek <staniek@kde.org>
0005 
0006    This library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 
0011    This library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this library; see the file COPYING.LIB.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #include "KPropertySetBuffer.h"
0023 #include "KPropertySet_p.h"
0024 #include "KProperty_p.h"
0025 
0026 class Q_DECL_HIDDEN KPropertySetBuffer::Private
0027 {
0028 public:
0029     Private()
0030     {
0031     }
0032 };
0033 
0034 KPropertySetBuffer::KPropertySetBuffer()
0035         : KPropertySet(false)
0036         , d(new Private)
0037 {
0038     connect(this, SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
0039             this, SLOT(intersectedChanged(KPropertySet&,KProperty&)));
0040 
0041     connect(this, SIGNAL(propertyReset(KPropertySet&,KProperty&)),
0042             this, SLOT(intersectedReset(KPropertySet&,KProperty&)));
0043 }
0044 
0045 KPropertySetBuffer::KPropertySetBuffer(const KPropertySet& set)
0046         : KPropertySet(false)
0047         , d(new Private)
0048 {
0049     connect(this, SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
0050             this, SLOT(intersectedChanged(KPropertySet&,KProperty&)));
0051 
0052     connect(this, SIGNAL(propertyReset(KPropertySet&,KProperty&)),
0053             this, SLOT(intersectedReset(KPropertySet&,KProperty&)));
0054 
0055     init(set);
0056 }
0057 
0058 KPropertySetBuffer::~KPropertySetBuffer()
0059 {
0060     delete d;
0061 }
0062 
0063 void KPropertySetBuffer::init(const KPropertySet& set)
0064 {
0065     //deep copy of set
0066     const QList<KProperty*>::ConstIterator itEnd(KPropertySetPrivate::d(&set)->listConstEnd());
0067     for (QList<KProperty*>::ConstIterator it(KPropertySetPrivate::d(&set)->listConstIterator());
0068         it!=itEnd; ++it)
0069     {
0070         KProperty *prop = new KProperty(*(*it));
0071         QByteArray group = KPropertySetPrivate::d(&set)->groupForProperty(*it);
0072         const QString groupCaption = set.groupCaption(group);
0073         setGroupCaption(group, groupCaption);
0074         addProperty(prop, group);
0075         prop->d->addRelatedProperty(*it);
0076     }
0077 }
0078 
0079 void KPropertySetBuffer::intersect(const KPropertySet& set)
0080 {
0081     if (isEmpty()) {
0082         init(set);
0083         return;
0084     }
0085 
0086     const QList<KProperty*>::ConstIterator itEnd(KPropertySetPrivate::d(&set)->listConstEnd());
0087     for (QList<KProperty*>::ConstIterator it(KPropertySetPrivate::d(&set)->listConstIterator());
0088         it!=itEnd; ++it)
0089     {
0090         const QByteArray key( (*it)->name() );
0091         KProperty *property = KPropertySetPrivate::d(&set)->property( key );
0092         if (property) {
0093             blockSignals(true);
0094             (*it)->resetValue();
0095             (*it)->d->addRelatedProperty(property);
0096             blockSignals(false);
0097         } else {
0098             removeProperty(key);
0099         }
0100     }
0101 }
0102 
0103 void KPropertySetBuffer::intersectedChanged(KPropertySet& set, KProperty& prop)
0104 {
0105     Q_UNUSED(set);
0106     if (!contains(prop.name()))
0107         return;
0108 
0109     const QList<KProperty*> *props = prop.d->relatedProperties;
0110     for (QList<KProperty*>::ConstIterator it = props->constBegin(); it != props->constEnd(); ++it) {
0111         (*it)->setValue(prop.value(), KProperty::ValueOption::IgnoreOld);
0112     }
0113 }
0114 
0115 void KPropertySetBuffer::intersectedReset(KPropertySet& set, KProperty& prop)
0116 {
0117     Q_UNUSED(set);
0118     if (!contains(prop.name()))
0119         return;
0120 
0121     const QList<KProperty*> *props = prop.d->relatedProperties;
0122     for (QList<KProperty*>::ConstIterator it = props->constBegin(); it != props->constEnd(); ++it)  {
0123         (*it)->setValue(prop.value(), KProperty::ValueOption::IgnoreOld);
0124     }
0125 }