File indexing completed on 2024-05-12 16:39:51

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003 Lucijan Busch <lucijan@gmx.at>
0003    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
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 "WidgetInfo.h"
0023 #include "widgetfactory.h"
0024 
0025 #include <KDb>
0026 
0027 #include <KProperty>
0028 
0029 #include <KLocalizedString>
0030 
0031 namespace KFormDesigner {
0032 class Q_DECL_HIDDEN WidgetInfo::Private
0033 {
0034 public:
0035     Private (WidgetFactory *f)
0036         : overriddenAlternateNames(0)
0037         , factory(f)
0038         , propertiesWithDisabledAutoSync(0)
0039         , customTypesForProperty(0)
0040         , inheritedClass(0)
0041     {
0042     }
0043 
0044     ~Private() {
0045         delete overriddenAlternateNames;
0046         delete propertiesWithDisabledAutoSync;
0047         delete customTypesForProperty;
0048     }
0049 
0050     QString iconName;
0051     QByteArray className;
0052     QString name;
0053     QByteArray namePrefix;
0054     QString translatedNamePrefix;
0055     QString desc;
0056     QString include;
0057     QList<QByteArray> alternateNames;
0058     QList<QByteArray> *overriddenAlternateNames;
0059     QList<QByteArray> autoSaveProperties;
0060     QByteArray saveName;
0061     QPointer<WidgetFactory> factory;
0062     QHash<QByteArray, tristate> *propertiesWithDisabledAutoSync;
0063     QHash<QByteArray, int> *customTypesForProperty;
0064     QByteArray parentFactoryName;
0065     QByteArray inheritedClassName; //!< Used for inheriting widgets between factories
0066     WidgetInfo* inheritedClass;
0067     Qt::Alignment supportedAlignmentFlags = Qt::Alignment(Qt::AlignHorizontal_Mask | Qt::AlignVertical_Mask) ^ Qt::AlignAbsolute;
0068 };
0069 }
0070 
0071 //--------------------------------------
0072 
0073 using namespace KFormDesigner;
0074 
0075 WidgetInfo::WidgetInfo(WidgetFactory *f)
0076  : d(new Private(f) )
0077 {
0078 }
0079 
0080 WidgetInfo::~WidgetInfo()
0081 {
0082     delete d;
0083 }
0084 
0085 QString WidgetInfo::iconName() const
0086 {
0087     return d->iconName;
0088 }
0089 
0090 void WidgetInfo::setIconName(const QString &iconName)
0091 {
0092     d->iconName = iconName;
0093 }
0094 
0095 QByteArray WidgetInfo::className() const
0096 {
0097     return d->className;
0098 }
0099 
0100 void WidgetInfo::setClassName(const QByteArray &className)
0101 {
0102     d->className = className;
0103 }
0104 
0105 QByteArray WidgetInfo::inheritedClassName() const
0106 {
0107     return d->inheritedClassName;
0108 }
0109 
0110 void WidgetInfo::setInheritedClassName(const QByteArray& inheritedClassName)
0111 {
0112     d->inheritedClassName = inheritedClassName;
0113     if (d->className.isEmpty())
0114         d->className = inheritedClassName; // the default
0115 }
0116 
0117 void WidgetInfo::setInheritedClass(WidgetInfo *inheritedClass)
0118 {
0119     d->inheritedClass = inheritedClass;
0120 }
0121 
0122 WidgetInfo* WidgetInfo::inheritedClass() const
0123 {
0124     return d->inheritedClass;
0125 }
0126 
0127 QString WidgetInfo::namePrefix() const
0128 {
0129     return QString::fromLatin1(d->namePrefix);
0130 }
0131 
0132 QString WidgetInfo::translatedNamePrefix() const
0133 {
0134     return d->translatedNamePrefix;
0135 }
0136 
0137 void WidgetInfo::setNamePrefix(const char *context, const char *prefix)
0138 {
0139     Q_UNUSED(context)
0140     d->namePrefix = prefix;
0141     if (!KDb::isIdentifier(d->namePrefix)) {
0142         qWarning() << "Invalid untranslated name prefix" << d->namePrefix
0143                    << "for form widgets of class" << className()
0144                    << "has been detected. It is not a valid identifier. \"widget\" prefix"
0145                    << "will be used. Please report the issue to authors of the" << className()
0146                    << "class implementation so they can fix it.";
0147         d->namePrefix = "widget";
0148         d->translatedNamePrefix = d->namePrefix;
0149         return;
0150     }
0151     // Proper prefix, but is it translated properly?
0152     const QString newTranslatedNamePrefix = i18n(d->namePrefix);
0153     if (KDb::isIdentifier(newTranslatedNamePrefix)) {
0154         d->translatedNamePrefix = newTranslatedNamePrefix;
0155     } else {
0156         qWarning() << "Invalid translation" << newTranslatedNamePrefix
0157                    << "of name prefix" << d->namePrefix << "for form widgets of class" << className()
0158                    << "has been detected. It is not a valid identifier. Untranslated prefix"
0159                    << d->namePrefix << "will be used. Please report the issue to authors of"
0160                    << QLocale().name() << "translation so they can fix it.";
0161         d->translatedNamePrefix = d->namePrefix;
0162     }
0163 }
0164 
0165 QString WidgetInfo::name() const
0166 {
0167     return d->name;
0168 }
0169 
0170 void WidgetInfo::setName(const QString &n)
0171 {
0172     d->name = n;
0173 }
0174 
0175 QString WidgetInfo::description() const
0176 {
0177     return d->desc;
0178 }
0179 
0180 
0181 void WidgetInfo::setDescription(const QString &desc)
0182 {
0183     d->desc = desc;
0184 }
0185 
0186 QString WidgetInfo::includeFileName() const
0187 {
0188     return d->include;
0189 }
0190 
0191 void WidgetInfo::setIncludeFileName(const QString &name)
0192 {
0193     d->include = name;
0194 }
0195 
0196 QList<QByteArray> WidgetInfo::alternateClassNames() const
0197 {
0198     return d->alternateNames;
0199 }
0200 
0201 QByteArray WidgetInfo::savingName() const
0202 {
0203     return d->saveName;
0204 }
0205 
0206 WidgetFactory* WidgetInfo::factory() const
0207 {
0208     return d->factory;
0209 }
0210 
0211 void WidgetInfo::setSavingName(const QByteArray& saveName)
0212 {
0213     d->saveName = saveName;
0214 }
0215 
0216 QByteArray WidgetInfo::parentFactoryName() const
0217 {
0218     return d->parentFactoryName;
0219 }
0220 
0221 void WidgetInfo::setParentFactoryName(const QByteArray& parentFactoryName)
0222 {
0223     d->parentFactoryName = parentFactoryName;
0224 }
0225 
0226 void WidgetInfo::addAlternateClassName(const QByteArray& alternateName, bool override)
0227 {
0228     d->alternateNames += alternateName;
0229     if (override) {
0230         if (!d->overriddenAlternateNames)
0231             d->overriddenAlternateNames = new QList<QByteArray>;
0232         d->overriddenAlternateNames->append(alternateName);
0233     } else {
0234         if (d->overriddenAlternateNames)
0235             d->overriddenAlternateNames->removeAll(alternateName);
0236     }
0237 }
0238 
0239 bool WidgetInfo::isOverriddenClassName(const QByteArray& alternateName) const
0240 {
0241     return d->overriddenAlternateNames && d->overriddenAlternateNames->contains(alternateName);
0242 }
0243 
0244 void WidgetInfo::setAutoSyncForProperty(const QByteArray& propertyName, tristate flag)
0245 {
0246     if (!d->propertiesWithDisabledAutoSync) {
0247         if (~flag)
0248             return;
0249         d->propertiesWithDisabledAutoSync = new QHash<QByteArray, tristate>;
0250     }
0251 
0252     if (~flag) {
0253         d->propertiesWithDisabledAutoSync->remove(propertyName);
0254     } else {
0255         d->propertiesWithDisabledAutoSync->insert(propertyName, flag);
0256     }
0257 }
0258 
0259 tristate WidgetInfo::autoSyncForProperty(const QByteArray& propertyName) const
0260 {
0261     if (!d->propertiesWithDisabledAutoSync)
0262         return cancelled;
0263     tristate flag = d->propertiesWithDisabledAutoSync->value(propertyName);
0264     return flag;
0265 }
0266 
0267 void WidgetInfo::setAutoSaveProperties(const QList<QByteArray>& properties)
0268 {
0269     d->autoSaveProperties = properties;
0270 }
0271 
0272 QList<QByteArray> WidgetInfo::autoSaveProperties() const
0273 {
0274     if (!d->inheritedClass)
0275         return d->autoSaveProperties;
0276     return d->inheritedClass->autoSaveProperties() + d->autoSaveProperties;
0277 }
0278 
0279 void WidgetInfo::setCustomTypeForProperty(const QByteArray& propertyName, int type)
0280 {
0281     if (propertyName.isEmpty() || type == int(KProperty::Auto))
0282         return;
0283     if (!d->customTypesForProperty) {
0284         d->customTypesForProperty = new QHash<QByteArray, int>();
0285     }
0286     d->customTypesForProperty->remove(propertyName);
0287     d->customTypesForProperty->insert(propertyName, type);
0288 }
0289 
0290 int WidgetInfo::customTypeForProperty(const QByteArray& propertyName) const
0291 {
0292     if (!d->customTypesForProperty || !d->customTypesForProperty->contains(propertyName))
0293         return KProperty::Auto;
0294     return d->customTypesForProperty->value(propertyName);
0295 }
0296 
0297 QVariant WidgetInfo::internalProperty(const QByteArray& property) const
0298 {
0299     return d->factory->internalProperty(d->className, property);
0300 }
0301 
0302 void WidgetInfo::setInternalProperty(const QByteArray& property, const QVariant& value)
0303 {
0304     InternalPropertyHandlerInterface *iface = static_cast<InternalPropertyHandlerInterface*>(d->factory);
0305     iface->setInternalProperty(d->className, property, value);
0306 }
0307 
0308 Qt::Alignment WidgetInfo::supportedAlignmentFlags() const
0309 {
0310     return d->supportedAlignmentFlags;
0311 }
0312 
0313 void WidgetInfo::setSupportedAlignmentFlags(Qt::Alignment flags)
0314 {
0315     d->supportedAlignmentFlags = flags;
0316 }