1 /* This file is part of the KDE project
2    Copyright (C) 2003 Lucijan Busch <lucijan@gmx.at>
3    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
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 "WidgetInfo.h"
23 #include "widgetfactory.h"
24 
25 #include <KDb>
26 
27 #include <KProperty>
28 
29 #include <KLocalizedString>
30 
31 namespace KFormDesigner {
32 class Q_DECL_HIDDEN WidgetInfo::Private
33 {
34 public:
35     Private (WidgetFactory *f)
36         : overriddenAlternateNames(0)
37         , factory(f)
38         , propertiesWithDisabledAutoSync(0)
39         , customTypesForProperty(0)
40         , inheritedClass(0)
41     {
42     }
43 
44     ~Private() {
45         delete overriddenAlternateNames;
Reader(llvm::StringRef Data)46         delete propertiesWithDisabledAutoSync;
47         delete customTypesForProperty;
48     }
err() const49 
50     QString iconName;
eof() const51     QByteArray className;
52     QString name;
rest() const53     QByteArray namePrefix;
54     QString translatedNamePrefix;
consume8()55     QString desc;
56     QString include;
57     QList<QByteArray> alternateNames;
58     QList<QByteArray> *overriddenAlternateNames;
59     QList<QByteArray> autoSaveProperties;
60     QByteArray saveName;
61     QPointer<WidgetFactory> factory;
62     QHash<QByteArray, tristate> *propertiesWithDisabledAutoSync;
consume32()63     QHash<QByteArray, int> *customTypesForProperty;
64     QByteArray parentFactoryName;
65     QByteArray inheritedClassName; //!< Used for inheriting widgets between factories
66     WidgetInfo* inheritedClass;
67     Qt::Alignment supportedAlignmentFlags = Qt::Alignment(Qt::AlignHorizontal_Mask | Qt::AlignVertical_Mask) ^ Qt::AlignAbsolute;
68 };
69 }
70 
71 //--------------------------------------
72 
consume(int N)73 using namespace KFormDesigner;
74 
75 WidgetInfo::WidgetInfo(WidgetFactory *f)
76  : d(new Private(f) )
77 {
78 }
79 
80 WidgetInfo::~WidgetInfo()
81 {
82     delete d;
consumeVar()83 }
84 
85 QString WidgetInfo::iconName() const
86 {
87     return d->iconName;
88 }
89 
90 void WidgetInfo::setIconName(const QString &iconName)
91 {
92     d->iconName = iconName;
93 }
94 
95 QByteArray WidgetInfo::className() const
96 {
97     return d->className;
98 }
99 
100 void WidgetInfo::setClassName(const QByteArray &className)
consumeString(llvm::ArrayRef<llvm::StringRef> Strings)101 {
102     d->className = className;
103 }
104 
105 QByteArray WidgetInfo::inheritedClassName() const
106 {
107     return d->inheritedClassName;
108 }
109 
consumeID()110 void WidgetInfo::setInheritedClassName(const QByteArray& inheritedClassName)
111 {
112     d->inheritedClassName = inheritedClassName;
113     if (d->className.isEmpty())
114         d->className = inheritedClassName; // the default
115 }
116 
117 void WidgetInfo::setInheritedClass(WidgetInfo *inheritedClass)
consumeSize(T & Container)118 {
119     d->inheritedClass = inheritedClass;
120 }
121 
122 WidgetInfo* WidgetInfo::inheritedClass() const
123 {
124     return d->inheritedClass;
125 }
126 
127 QString WidgetInfo::namePrefix() const
128 {
129     return QString::fromLatin1(d->namePrefix);
write32(uint32_t I,llvm::raw_ostream & OS)130 }
131 
132 QString WidgetInfo::translatedNamePrefix() const
133 {
134     return d->translatedNamePrefix;
135 }
writeVar(uint32_t I,llvm::raw_ostream & OS)136 
137 void WidgetInfo::setNamePrefix(const char *context, const char *prefix)
138 {
139     Q_UNUSED(context)
140     d->namePrefix = prefix;
141     if (!KDb::isIdentifier(d->namePrefix)) {
142         qWarning() << "Invalid untranslated name prefix" << d->namePrefix
143                    << "for form widgets of class" << className()
144                    << "has been detected. It is not a valid identifier. \"widget\" prefix"
145                    << "will be used. Please report the issue to authors of the" << className()
146                    << "class implementation so they can fix it.";
147         d->namePrefix = "widget";
148         d->translatedNamePrefix = d->namePrefix;
149         return;
150     }
151     // Proper prefix, but is it translated properly?
152     const QString newTranslatedNamePrefix = i18n(d->namePrefix);
153     if (KDb::isIdentifier(newTranslatedNamePrefix)) {
154         d->translatedNamePrefix = newTranslatedNamePrefix;
155     } else {
156         qWarning() << "Invalid translation" << newTranslatedNamePrefix
157                    << "of name prefix" << d->namePrefix << "for form widgets of class" << className()
158                    << "has been detected. It is not a valid identifier. Untranslated prefix"
159                    << d->namePrefix << "will be used. Please report the issue to authors of"
160                    << QLocale().name() << "translation so they can fix it.";
161         d->translatedNamePrefix = d->namePrefix;
162     }
163 }
164 
165 QString WidgetInfo::name() const
166 {
167     return d->name;
168 }
169 
170 void WidgetInfo::setName(const QString &n)
171 {
172     d->name = n;
StringTableOut()173 }
174 
175 QString WidgetInfo::description() const
176 {
177     return d->desc;
178 }
intern(llvm::StringRef & S)179 
180 
181 void WidgetInfo::setDescription(const QString &desc)
182 {
183     d->desc = desc;
184 }
185 
186 QString WidgetInfo::includeFileName() const
187 {
188     return d->include;
189 }
190 
191 void WidgetInfo::setIncludeFileName(const QString &name)
192 {
193     d->include = name;
194 }
195 
196 QList<QByteArray> WidgetInfo::alternateClassNames() const
197 {
198     return d->alternateNames;
199 }
200 
201 QByteArray WidgetInfo::savingName() const
202 {
index(llvm::StringRef S) const203     return d->saveName;
204 }
205 
206 WidgetFactory* WidgetInfo::factory() const
207 {
208     return d->factory;
209 }
210 
211 void WidgetInfo::setSavingName(const QByteArray& saveName)
212 {
213     d->saveName = saveName;
214 }
readStringTable(llvm::StringRef Data)215 
216 QByteArray WidgetInfo::parentFactoryName() const
217 {
218     return d->parentFactoryName;
219 }
220 
221 void WidgetInfo::setParentFactoryName(const QByteArray& parentFactoryName)
222 {
223     d->parentFactoryName = parentFactoryName;
224 }
225 
226 void WidgetInfo::addAlternateClassName(const QByteArray& alternateName, bool override)
227 {
228     d->alternateNames += alternateName;
229     if (override) {
230         if (!d->overriddenAlternateNames)
231             d->overriddenAlternateNames = new QList<QByteArray>;
232         d->overriddenAlternateNames->append(alternateName);
233     } else {
234         if (d->overriddenAlternateNames)
235             d->overriddenAlternateNames->removeAll(alternateName);
236     }
237 }
238 
239 bool WidgetInfo::isOverriddenClassName(const QByteArray& alternateName) const
240 {
241     return d->overriddenAlternateNames && d->overriddenAlternateNames->contains(alternateName);
242 }
243 
244 void WidgetInfo::setAutoSyncForProperty(const QByteArray& propertyName, tristate flag)
245 {
246     if (!d->propertiesWithDisabledAutoSync) {
247         if (~flag)
248             return;
249         d->propertiesWithDisabledAutoSync = new QHash<QByteArray, tristate>;
250     }
251 
252     if (~flag) {
253         d->propertiesWithDisabledAutoSync->remove(propertyName);
254     } else {
255         d->propertiesWithDisabledAutoSync->insert(propertyName, flag);
256     }
257 }
258 
259 tristate WidgetInfo::autoSyncForProperty(const QByteArray& propertyName) const
260 {
261     if (!d->propertiesWithDisabledAutoSync)
262         return cancelled;
writeLocation(const SymbolLocation & Loc,const StringTableOut & Strings,llvm::raw_ostream & OS)263     tristate flag = d->propertiesWithDisabledAutoSync->value(propertyName);
264     return flag;
265 }
266 
267 void WidgetInfo::setAutoSaveProperties(const QList<QByteArray>& properties)
268 {
269     d->autoSaveProperties = properties;
270 }
271 
readLocation(Reader & Data,llvm::ArrayRef<llvm::StringRef> Strings)272 QList<QByteArray> WidgetInfo::autoSaveProperties() const
273 {
274     if (!d->inheritedClass)
275         return d->autoSaveProperties;
276     return d->inheritedClass->autoSaveProperties() + d->autoSaveProperties;
277 }
278 
279 void WidgetInfo::setCustomTypeForProperty(const QByteArray& propertyName, int type)
280 {
281     if (propertyName.isEmpty() || type == int(KProperty::Auto))
282         return;
readIncludeGraphNode(Reader & Data,llvm::ArrayRef<llvm::StringRef> Strings)283     if (!d->customTypesForProperty) {
284         d->customTypesForProperty = new QHash<QByteArray, int>();
285     }
286     d->customTypesForProperty->remove(propertyName);
287     d->customTypesForProperty->insert(propertyName, type);
288 }
289 
290 int WidgetInfo::customTypeForProperty(const QByteArray& propertyName) const
291 {
292     if (!d->customTypesForProperty || !d->customTypesForProperty->contains(propertyName))
293         return KProperty::Auto;
294     return d->customTypesForProperty->value(propertyName);
295 }
296 
writeIncludeGraphNode(const IncludeGraphNode & IGN,const StringTableOut & Strings,llvm::raw_ostream & OS)297 QVariant WidgetInfo::internalProperty(const QByteArray& property) const
298 {
299     return d->factory->internalProperty(d->className, property);
300 }
301 
302 void WidgetInfo::setInternalProperty(const QByteArray& property, const QVariant& value)
303 {
304     InternalPropertyHandlerInterface *iface = static_cast<InternalPropertyHandlerInterface*>(d->factory);
305     iface->setInternalProperty(d->className, property, value);
306 }
307 
308 Qt::Alignment WidgetInfo::supportedAlignmentFlags() const
309 {
writeSymbol(const Symbol & Sym,const StringTableOut & Strings,llvm::raw_ostream & OS)310     return d->supportedAlignmentFlags;
311 }
312 
313 void WidgetInfo::setSupportedAlignmentFlags(Qt::Alignment flags)
314 {
315     d->supportedAlignmentFlags = flags;
316 }
317