File indexing completed on 2024-05-12 04:35:04

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2015-2016 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "PropertyInterface.h"
0021 #include "Property.h"
0022 #include <QHash>
0023 
0024 namespace tikz {
0025 namespace core {
0026 
0027 class PropertyInterfacePrivate
0028 {
0029 public:
0030     QVector<Property *> properties;
0031     QHash<QString, Property *> propertyMap;
0032 };
0033 
0034 class Property;
0035 class Entity;
0036 
0037 PropertyInterface::PropertyInterface()
0038     : d(new PropertyInterfacePrivate())
0039 {
0040 }
0041 
0042 PropertyInterface::~PropertyInterface()
0043 {
0044     qDeleteAll(d->properties);
0045     d->properties.clear();
0046 
0047     d->propertyMap.clear();
0048 
0049     delete d;
0050 }
0051 
0052 void PropertyInterface::addProperty(Property * property)
0053 {
0054     d->properties.append(property);
0055     d->propertyMap[property->propertyName()] = property;
0056 }
0057 
0058 void PropertyInterface::removeProperty(Property * property)
0059 {
0060     const int index = d->properties.indexOf(property);
0061     if (index >= 0) {
0062         d->properties.remove(index);
0063         d->propertyMap.remove(property->propertyName());
0064     }
0065 }
0066 
0067 Property * PropertyInterface::property(const QString & propertyName,
0068                                                bool fallbackToParents) const
0069 {
0070     const auto it = d->propertyMap.find(propertyName);
0071     if (it != d->propertyMap.end()) {
0072         return *it;
0073     }
0074 
0075     // check parent, if possible
0076     if (fallbackToParents) {
0077         auto parent = parentPropertyInterface();
0078         if (parent) {
0079             return parent->property(propertyName, fallbackToParents);
0080         }
0081     }
0082 
0083     return nullptr;
0084 }
0085 
0086 bool PropertyInterface::hasProperty(const QString & propertyName,
0087                                     bool fallbackToParents)
0088 {
0089     const auto it = d->propertyMap.find(propertyName);
0090     if (it != d->propertyMap.end()) {
0091         return true;
0092     }
0093 
0094     // check parent, if possible
0095     if (fallbackToParents) {
0096         auto parent = parentPropertyInterface();
0097         if (parent) {
0098             return parent->hasProperty(propertyName, fallbackToParents);
0099         }
0100     }
0101 
0102     return false;
0103 }
0104 
0105 QVector<Property *> PropertyInterface::properties() const
0106 {
0107     // FIXME: only properties of this instance (no parent ones)
0108     return d->properties;
0109 }
0110 
0111 void PropertyInterface::notifyPropertyChanged(Property * property)
0112 {
0113     Q_EMIT propertyChanged(this, property);
0114 }
0115 
0116 void PropertyInterface::notifyPropertyAboutToChange(Property * property)
0117 {
0118     Q_UNUSED(property)
0119     // TODO: leave empty for now ?
0120 }
0121 
0122 } //namespace core
0123 } //namespace tikz
0124 
0125 // kate: indent-width 4; replace-tabs on;