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