|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Designer of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 // |
|
43 // W A R N I N G |
|
44 // ------------- |
|
45 // |
|
46 // This file is not part of the Qt API. It exists for the convenience |
|
47 // of Qt Designer. This header |
|
48 // file may change from version to version without notice, or even be removed. |
|
49 // |
|
50 // We mean it. |
|
51 // |
|
52 |
|
53 #ifndef QDESIGNER_UTILS_H |
|
54 #define QDESIGNER_UTILS_H |
|
55 |
|
56 #include "shared_global_p.h" |
|
57 |
|
58 #include <QtDesigner/QDesignerFormWindowInterface> |
|
59 |
|
60 #include <QtCore/QVariant> |
|
61 #include <QtCore/QMap> |
|
62 #include <QtGui/QMainWindow> |
|
63 #include <QtGui/QIcon> |
|
64 #include <QtGui/QPixmap> |
|
65 |
|
66 QT_BEGIN_NAMESPACE |
|
67 |
|
68 namespace qdesigner_internal { |
|
69 class QDesignerFormWindowCommand; |
|
70 class DesignerIconCache; |
|
71 class FormWindowBase; |
|
72 |
|
73 |
|
74 QDESIGNER_SHARED_EXPORT void designerWarning(const QString &message); |
|
75 |
|
76 QDESIGNER_SHARED_EXPORT void reloadIconResources(DesignerIconCache *iconCache, QObject *object); |
|
77 |
|
78 /* Flag/Enumeration helpers for the property sheet: Enumeration or flag values are returned by the property sheet |
|
79 * as a pair of meta type and integer value. |
|
80 * The meta type carries all the information required for the property editor and serialization |
|
81 * by the form builders (names, etc). |
|
82 * Note that the property editor uses unqualified names ("Cancel") while the form builder serialization (uic) |
|
83 * requires the whole string |
|
84 * ("QDialogButtonBox::Cancel" or "com.trolltech.qt.gui.QDialogButtonBox.StandardButton.Cancel").*/ |
|
85 |
|
86 /* --------- MetaEnum: Base class representing a QMetaEnum with lookup functions |
|
87 * in both ways. Template of int type since unsigned is more suitable for flags. |
|
88 * The keyToValue() is ignorant of scopes, it can handle fully qualified or unqualified names. */ |
|
89 |
|
90 template <class IntType> |
|
91 class MetaEnum |
|
92 { |
|
93 public: |
|
94 typedef QMap<QString, IntType> KeyToValueMap; |
|
95 |
|
96 MetaEnum(const QString &name, const QString &scope, const QString &separator); |
|
97 MetaEnum() {} |
|
98 void addKey(IntType value, const QString &name); |
|
99 |
|
100 QString valueToKey(IntType value, bool *ok = 0) const; |
|
101 // Ignorant of scopes. |
|
102 IntType keyToValue(QString key, bool *ok = 0) const; |
|
103 |
|
104 const QString &name() const { return m_name; } |
|
105 const QString &scope() const { return m_scope; } |
|
106 const QString &separator() const { return m_separator; } |
|
107 |
|
108 const QStringList &keys() const { return m_keys; } |
|
109 const KeyToValueMap &keyToValueMap() const { return m_keyToValueMap; } |
|
110 |
|
111 protected: |
|
112 void appendQualifiedName(const QString &key, QString &target) const; |
|
113 |
|
114 private: |
|
115 QString m_name; |
|
116 QString m_scope; |
|
117 QString m_separator; |
|
118 KeyToValueMap m_keyToValueMap; |
|
119 QStringList m_keys; |
|
120 }; |
|
121 |
|
122 template <class IntType> |
|
123 MetaEnum<IntType>::MetaEnum(const QString &name, const QString &scope, const QString &separator) : |
|
124 m_name(name), |
|
125 m_scope(scope), |
|
126 m_separator(separator) |
|
127 { |
|
128 } |
|
129 |
|
130 template <class IntType> |
|
131 void MetaEnum<IntType>::addKey(IntType value, const QString &name) |
|
132 { |
|
133 m_keyToValueMap.insert(name, value); |
|
134 m_keys.append(name); |
|
135 } |
|
136 |
|
137 template <class IntType> |
|
138 QString MetaEnum<IntType>::valueToKey(IntType value, bool *ok) const |
|
139 { |
|
140 const QString rc = m_keyToValueMap.key(value); |
|
141 if (ok) |
|
142 *ok = !rc.isEmpty(); |
|
143 return rc; |
|
144 } |
|
145 |
|
146 template <class IntType> |
|
147 IntType MetaEnum<IntType>::keyToValue(QString key, bool *ok) const |
|
148 { |
|
149 if (!m_scope.isEmpty() && key.startsWith(m_scope)) |
|
150 key.remove(0, m_scope.size() + m_separator.size()); |
|
151 const Q_TYPENAME KeyToValueMap::const_iterator it = m_keyToValueMap.find(key); |
|
152 const bool found = it != m_keyToValueMap.constEnd(); |
|
153 if (ok) |
|
154 *ok = found; |
|
155 return found ? it.value() : IntType(0); |
|
156 } |
|
157 |
|
158 template <class IntType> |
|
159 void MetaEnum<IntType>::appendQualifiedName(const QString &key, QString &target) const |
|
160 { |
|
161 if (!m_scope.isEmpty()) { |
|
162 target += m_scope; |
|
163 target += m_separator; |
|
164 } |
|
165 target += key; |
|
166 } |
|
167 |
|
168 // -------------- DesignerMetaEnum: Meta type for enumerations |
|
169 |
|
170 class QDESIGNER_SHARED_EXPORT DesignerMetaEnum : public MetaEnum<int> |
|
171 { |
|
172 public: |
|
173 DesignerMetaEnum(const QString &name, const QString &scope, const QString &separator); |
|
174 DesignerMetaEnum() {} |
|
175 |
|
176 enum SerializationMode { FullyQualified, NameOnly }; |
|
177 QString toString(int value, SerializationMode sm, bool *ok = 0) const; |
|
178 |
|
179 QString messageToStringFailed(int value) const; |
|
180 QString messageParseFailed(const QString &s) const; |
|
181 |
|
182 // parse a string (ignorant of scopes) |
|
183 int parseEnum(const QString &s, bool *ok = 0) const { return keyToValue(s, ok); } |
|
184 }; |
|
185 |
|
186 // -------------- DesignerMetaFlags: Meta type for flags. |
|
187 // Note that while the handling of flags is done using unsigned integers, the actual values returned |
|
188 // by the property system are integers. |
|
189 |
|
190 class QDESIGNER_SHARED_EXPORT DesignerMetaFlags : public MetaEnum<uint> |
|
191 { |
|
192 public: |
|
193 DesignerMetaFlags(const QString &name, const QString &scope, const QString &separator); |
|
194 DesignerMetaFlags() {} |
|
195 |
|
196 enum SerializationMode { FullyQualified, NameOnly }; |
|
197 QString toString(int value, SerializationMode sm) const; |
|
198 QStringList flags(int value) const; |
|
199 |
|
200 QString messageParseFailed(const QString &s) const; |
|
201 // parse a string (ignorant of scopes) |
|
202 int parseFlags(const QString &s, bool *ok = 0) const; |
|
203 }; |
|
204 |
|
205 // -------------- EnumValue: Returned by the property sheet for enumerations |
|
206 |
|
207 struct QDESIGNER_SHARED_EXPORT PropertySheetEnumValue |
|
208 { |
|
209 PropertySheetEnumValue(int v, const DesignerMetaEnum &me); |
|
210 PropertySheetEnumValue(); |
|
211 |
|
212 int value; |
|
213 DesignerMetaEnum metaEnum; |
|
214 }; |
|
215 |
|
216 // -------------- FlagValue: Returned by the property sheet for flags |
|
217 |
|
218 struct QDESIGNER_SHARED_EXPORT PropertySheetFlagValue |
|
219 { |
|
220 PropertySheetFlagValue(int v, const DesignerMetaFlags &mf); |
|
221 PropertySheetFlagValue(); |
|
222 |
|
223 int value; |
|
224 DesignerMetaFlags metaFlags; |
|
225 }; |
|
226 |
|
227 // -------------- PixmapValue: Returned by the property sheet for pixmaps |
|
228 class QDESIGNER_SHARED_EXPORT PropertySheetPixmapValue |
|
229 { |
|
230 public: |
|
231 PropertySheetPixmapValue(const QString &path); |
|
232 PropertySheetPixmapValue(); |
|
233 |
|
234 bool operator==(const PropertySheetPixmapValue &other) const { return compare(other) == 0; } |
|
235 bool operator!=(const PropertySheetPixmapValue &other) const { return compare(other) != 0; } |
|
236 bool operator<(const PropertySheetPixmapValue &other) const { return compare(other) < 0; } |
|
237 |
|
238 // Check where a pixmap comes from |
|
239 enum PixmapSource { LanguageResourcePixmap , ResourcePixmap, FilePixmap }; |
|
240 static PixmapSource getPixmapSource(QDesignerFormEditorInterface *core, const QString & path); |
|
241 |
|
242 PixmapSource pixmapSource(QDesignerFormEditorInterface *core) const { return getPixmapSource(core, m_path); } |
|
243 |
|
244 QString path() const; |
|
245 void setPath(const QString &path); // passing the empty path resets the pixmap |
|
246 |
|
247 int compare(const PropertySheetPixmapValue &other) const; |
|
248 |
|
249 private: |
|
250 QString m_path; |
|
251 }; |
|
252 |
|
253 // -------------- IconValue: Returned by the property sheet for icons |
|
254 |
|
255 class QDESIGNER_SHARED_EXPORT PropertySheetIconValue |
|
256 { |
|
257 public: |
|
258 PropertySheetIconValue(const PropertySheetPixmapValue &pixmap); |
|
259 PropertySheetIconValue(); |
|
260 |
|
261 bool operator==(const PropertySheetIconValue &other) const { return equals(other); } |
|
262 bool operator!=(const PropertySheetIconValue &other) const { return !equals(other); } |
|
263 bool operator<(const PropertySheetIconValue &other) const; |
|
264 |
|
265 PropertySheetPixmapValue pixmap(QIcon::Mode mode, QIcon::State state) const; |
|
266 void setPixmap(QIcon::Mode mode, QIcon::State state, const PropertySheetPixmapValue &path); // passing the empty path resets the pixmap |
|
267 |
|
268 uint mask() const; |
|
269 uint compare(const PropertySheetIconValue &other) const; |
|
270 void assign(const PropertySheetIconValue &other, uint mask); |
|
271 |
|
272 typedef QPair<QIcon::Mode, QIcon::State> ModeStateKey; |
|
273 typedef QMap<ModeStateKey, PropertySheetPixmapValue> ModeStateToPixmapMap; |
|
274 |
|
275 ModeStateToPixmapMap paths() const; |
|
276 |
|
277 private: |
|
278 bool equals(const PropertySheetIconValue &rhs) const; |
|
279 |
|
280 ModeStateToPixmapMap m_paths; |
|
281 }; |
|
282 |
|
283 class QDESIGNER_SHARED_EXPORT DesignerPixmapCache : public QObject |
|
284 { |
|
285 Q_OBJECT |
|
286 public: |
|
287 DesignerPixmapCache(QObject *parent = 0); |
|
288 QPixmap pixmap(const PropertySheetPixmapValue &value) const; |
|
289 void clear(); |
|
290 signals: |
|
291 void reloaded(); |
|
292 private: |
|
293 mutable QMap<PropertySheetPixmapValue, QPixmap> m_cache; |
|
294 friend class FormWindowBase; |
|
295 }; |
|
296 |
|
297 class QDESIGNER_SHARED_EXPORT DesignerIconCache : public QObject |
|
298 { |
|
299 Q_OBJECT |
|
300 public: |
|
301 DesignerIconCache(DesignerPixmapCache *pixmapCache, QObject *parent = 0); |
|
302 QIcon icon(const PropertySheetIconValue &value) const; |
|
303 void clear(); |
|
304 signals: |
|
305 void reloaded(); |
|
306 private: |
|
307 mutable QMap<PropertySheetIconValue, QIcon> m_cache; |
|
308 DesignerPixmapCache *m_pixmapCache; |
|
309 friend class FormWindowBase; |
|
310 }; |
|
311 |
|
312 // -------------- StringValue: Returned by the property sheet for strings |
|
313 class QDESIGNER_SHARED_EXPORT PropertySheetStringValue |
|
314 { |
|
315 public: |
|
316 PropertySheetStringValue(const QString &value = QString(), |
|
317 bool translatable = true, |
|
318 const QString &disambiguation = QString(), |
|
319 const QString &comment = QString()); |
|
320 |
|
321 bool operator==(const PropertySheetStringValue &other) const { return equals(other); } |
|
322 bool operator!=(const PropertySheetStringValue &other) const { return !equals(other); } |
|
323 |
|
324 QString value() const; |
|
325 void setValue(const QString &value); |
|
326 bool translatable() const; |
|
327 void setTranslatable(bool translatable); |
|
328 QString disambiguation() const; |
|
329 void setDisambiguation(const QString &disambiguation); |
|
330 QString comment() const; |
|
331 void setComment(const QString &comment); |
|
332 |
|
333 private: |
|
334 bool equals(const PropertySheetStringValue &rhs) const; |
|
335 |
|
336 QString m_value; |
|
337 bool m_translatable; |
|
338 QString m_disambiguation; |
|
339 QString m_comment; |
|
340 }; |
|
341 |
|
342 |
|
343 |
|
344 // -------------- StringValue: Returned by the property sheet for strings |
|
345 class QDESIGNER_SHARED_EXPORT PropertySheetKeySequenceValue |
|
346 { |
|
347 public: |
|
348 PropertySheetKeySequenceValue(const QKeySequence &value = QKeySequence(), |
|
349 bool translatable = true, |
|
350 const QString &disambiguation = QString(), |
|
351 const QString &comment = QString()); |
|
352 PropertySheetKeySequenceValue(const QKeySequence::StandardKey &standardKey, |
|
353 bool translatable = true, |
|
354 const QString &disambiguation = QString(), |
|
355 const QString &comment = QString()); |
|
356 |
|
357 bool operator==(const PropertySheetKeySequenceValue &other) const { return equals(other); } |
|
358 bool operator!=(const PropertySheetKeySequenceValue &other) const { return !equals(other); } |
|
359 |
|
360 QKeySequence value() const; |
|
361 void setValue(const QKeySequence &value); |
|
362 QKeySequence::StandardKey standardKey() const; |
|
363 void setStandardKey(const QKeySequence::StandardKey &standardKey); |
|
364 bool isStandardKey() const; |
|
365 |
|
366 bool translatable() const; |
|
367 void setTranslatable(bool translatable); |
|
368 QString disambiguation() const; |
|
369 void setDisambiguation(const QString &disambiguation); |
|
370 QString comment() const; |
|
371 void setComment(const QString &comment); |
|
372 |
|
373 private: |
|
374 bool equals(const PropertySheetKeySequenceValue &rhs) const; |
|
375 |
|
376 QKeySequence m_value; |
|
377 QKeySequence::StandardKey m_standardKey; |
|
378 bool m_translatable; |
|
379 QString m_disambiguation; |
|
380 QString m_comment; |
|
381 }; |
|
382 |
|
383 } // namespace qdesigner_internal |
|
384 |
|
385 QT_END_NAMESPACE |
|
386 |
|
387 |
|
388 // NOTE: Do not move this code, needed for GCC 3.3 |
|
389 Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetEnumValue) |
|
390 Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetFlagValue) |
|
391 Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetPixmapValue) |
|
392 Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetIconValue) |
|
393 Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetStringValue) |
|
394 Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetKeySequenceValue) |
|
395 |
|
396 |
|
397 QT_BEGIN_NAMESPACE |
|
398 |
|
399 namespace qdesigner_internal { |
|
400 |
|
401 |
|
402 // Create a command to change a text property (that is, create a reset property command if the text is empty) |
|
403 QDESIGNER_SHARED_EXPORT QDesignerFormWindowCommand *createTextPropertyCommand(const QString &propertyName, const QString &text, QObject *object, QDesignerFormWindowInterface *fw); |
|
404 |
|
405 // Returns preferred task menu action for managed widget |
|
406 QDESIGNER_SHARED_EXPORT QAction *preferredEditAction(QDesignerFormEditorInterface *core, QWidget *managedWidget); |
|
407 |
|
408 // Convenience to run UIC |
|
409 enum UIC_Mode { UIC_GenerateCode, UIC_ConvertV3 }; |
|
410 QDESIGNER_SHARED_EXPORT bool runUIC(const QString &fileName, UIC_Mode mode, QByteArray& ba, QString &errorMessage); |
|
411 |
|
412 // Find a suitable variable name for a class. |
|
413 QDESIGNER_SHARED_EXPORT QString qtify(const QString &name); |
|
414 |
|
415 /* UpdateBlocker: Blocks the updates of the widget passed on while in scope. |
|
416 * Does nothing if the incoming widget already has updatesEnabled==false |
|
417 * which is important to avoid side-effects when putting it into QStackedLayout. */ |
|
418 |
|
419 class QDESIGNER_SHARED_EXPORT UpdateBlocker { |
|
420 Q_DISABLE_COPY(UpdateBlocker) |
|
421 |
|
422 public: |
|
423 UpdateBlocker(QWidget *w); |
|
424 ~UpdateBlocker(); |
|
425 |
|
426 private: |
|
427 QWidget *m_widget; |
|
428 const bool m_enabled; |
|
429 }; |
|
430 |
|
431 namespace Utils { |
|
432 |
|
433 inline int valueOf(const QVariant &value, bool *ok = 0) |
|
434 { |
|
435 if (qVariantCanConvert<PropertySheetEnumValue>(value)) { |
|
436 if (ok) |
|
437 *ok = true; |
|
438 return qVariantValue<PropertySheetEnumValue>(value).value; |
|
439 } |
|
440 else if (qVariantCanConvert<PropertySheetFlagValue>(value)) { |
|
441 if (ok) |
|
442 *ok = true; |
|
443 return qVariantValue<PropertySheetFlagValue>(value).value; |
|
444 } |
|
445 return value.toInt(ok); |
|
446 } |
|
447 |
|
448 inline bool isObjectAncestorOf(QObject *ancestor, QObject *child) |
|
449 { |
|
450 QObject *obj = child; |
|
451 while (obj != 0) { |
|
452 if (obj == ancestor) |
|
453 return true; |
|
454 obj = obj->parent(); |
|
455 } |
|
456 return false; |
|
457 } |
|
458 |
|
459 inline bool isCentralWidget(QDesignerFormWindowInterface *fw, QWidget *widget) |
|
460 { |
|
461 if (! fw || ! widget) |
|
462 return false; |
|
463 |
|
464 if (widget == fw->mainContainer()) |
|
465 return true; |
|
466 |
|
467 // ### generalize for other containers |
|
468 if (QMainWindow *mw = qobject_cast<QMainWindow*>(fw->mainContainer())) { |
|
469 return mw->centralWidget() == widget; |
|
470 } |
|
471 |
|
472 return false; |
|
473 } |
|
474 |
|
475 } // namespace Utils |
|
476 |
|
477 } // namespace qdesigner_internal |
|
478 |
|
479 QT_END_NAMESPACE |
|
480 |
|
481 #endif // QDESIGNER_UTILS_H |