| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | // |
| 5 | // W A R N I N G |
| 6 | // ------------- |
| 7 | // |
| 8 | // This file is not part of the Qt API. It exists for the convenience |
| 9 | // of Qt Designer. This header |
| 10 | // file may change from version to version without notice, or even be removed. |
| 11 | // |
| 12 | // We mean it. |
| 13 | // |
| 14 | |
| 15 | #ifndef QDESIGNER_UTILS_H |
| 16 | #define QDESIGNER_UTILS_H |
| 17 | |
| 18 | #include "shared_global_p.h" |
| 19 | |
| 20 | #include <QtDesigner/abstractformwindow.h> |
| 21 | |
| 22 | #include <QtCore/qcompare.h> |
| 23 | #include <QtCore/qhash.h> |
| 24 | #include <QtCore/qvariant.h> |
| 25 | #include <QtCore/qshareddata.h> |
| 26 | #include <QtWidgets/qmainwindow.h> |
| 27 | #include <QtGui/qicon.h> |
| 28 | #include <QtGui/qpixmap.h> |
| 29 | |
| 30 | #include <map> |
| 31 | |
| 32 | QT_BEGIN_NAMESPACE |
| 33 | |
| 34 | class QDebug; |
| 35 | |
| 36 | namespace qdesigner_internal { |
| 37 | class QDesignerFormWindowCommand; |
| 38 | class DesignerIconCache; |
| 39 | class FormWindowBase; |
| 40 | |
| 41 | |
| 42 | QDESIGNER_SHARED_EXPORT QString dataDirectory(); |
| 43 | |
| 44 | QDESIGNER_SHARED_EXPORT QString legacyDataDirectory(); |
| 45 | |
| 46 | QDESIGNER_SHARED_EXPORT void designerWarning(const QString &message); |
| 47 | |
| 48 | QDESIGNER_SHARED_EXPORT void reloadIconResources(DesignerIconCache *iconCache, QObject *object); |
| 49 | |
| 50 | /* Flag/Enumeration helpers for the property sheet: Enumeration or flag values are returned by the property sheet |
| 51 | * as a pair of meta type and integer value. |
| 52 | * The meta type carries all the information required for the property editor and serialization |
| 53 | * by the form builders (names, etc). |
| 54 | * Note that the property editor uses unqualified names ("Cancel") while the form builder serialization (uic) |
| 55 | * requires the whole string |
| 56 | * ("QDialogButtonBox::Cancel" or "org.qt-project.qt.gui.QDialogButtonBox.StandardButton.Cancel").*/ |
| 57 | |
| 58 | /* --------- MetaEnum: Base class representing a QMetaEnum with lookup functions |
| 59 | * in both ways. Template of int type since unsigned is more suitable for flags. |
| 60 | * The keyToValue() is ignorant of scopes, it can handle fully qualified or unqualified names. */ |
| 61 | |
| 62 | template <class IntType> |
| 63 | class MetaEnum |
| 64 | { |
| 65 | public: |
| 66 | enum SerializationMode { FullyQualified, |
| 67 | Qualified }; // Qt pre 6.7 without enum name |
| 68 | |
| 69 | using KeyToValueMap = std::map<QString, IntType, std::less<>>; |
| 70 | |
| 71 | MetaEnum(const QString &enumName, const QString &scope, const QString &separator); |
| 72 | MetaEnum() = default; |
| 73 | void addKey(IntType value, const QString &name); |
| 74 | |
| 75 | QString valueToKey(IntType value, bool *ok = nullptr) const; |
| 76 | // Ignorant of scopes. |
| 77 | IntType keyToValue(QStringView key, bool *ok = nullptr) const; |
| 78 | |
| 79 | const QString &enumName() const { return m_enumName; } |
| 80 | const QString &scope() const { return m_scope; } |
| 81 | const QString &separator() const { return m_separator; } |
| 82 | |
| 83 | const QStringList &keys() const { return m_keys; } |
| 84 | const KeyToValueMap &keyToValueMap() const { return m_keyToValueMap; } |
| 85 | |
| 86 | protected: |
| 87 | void appendQualifiedName(const QString &key, SerializationMode sm, QString &target) const; |
| 88 | |
| 89 | private: |
| 90 | QString m_enumName; |
| 91 | QString m_scope; |
| 92 | QString m_separator; |
| 93 | KeyToValueMap m_keyToValueMap; |
| 94 | QStringList m_keys; |
| 95 | }; |
| 96 | |
| 97 | template <class IntType> |
| 98 | MetaEnum<IntType>::MetaEnum(const QString &enumName, const QString &scope, const QString &separator) : |
| 99 | m_enumName(enumName), |
| 100 | m_scope(scope), |
| 101 | m_separator(separator) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | template <class IntType> |
| 106 | void MetaEnum<IntType>::addKey(IntType value, const QString &name) |
| 107 | { |
| 108 | m_keyToValueMap.insert({name, value}); |
| 109 | m_keys.append(t: name); |
| 110 | } |
| 111 | |
| 112 | template <class IntType> |
| 113 | QString MetaEnum<IntType>::valueToKey(IntType value, bool *ok) const |
| 114 | { |
| 115 | QString rc; |
| 116 | for (auto it = m_keyToValueMap.begin(), end = m_keyToValueMap.end(); it != end; ++it) { |
| 117 | if (it->second == value) { |
| 118 | rc = it->first; |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | if (ok) |
| 123 | *ok = !rc.isEmpty(); |
| 124 | return rc; |
| 125 | } |
| 126 | |
| 127 | template <class IntType> |
| 128 | IntType MetaEnum<IntType>::keyToValue(QStringView key, bool *ok) const |
| 129 | { |
| 130 | const auto lastSep = key.lastIndexOf(s: m_separator); |
| 131 | if (lastSep != -1) |
| 132 | key = key.sliced(pos: lastSep + m_separator.size()); |
| 133 | const auto it = m_keyToValueMap.find(key); |
| 134 | const bool found = it != m_keyToValueMap.end(); |
| 135 | if (ok) |
| 136 | *ok = found; |
| 137 | return found ? it->second : IntType(0); |
| 138 | } |
| 139 | |
| 140 | template <class IntType> |
| 141 | void MetaEnum<IntType>::appendQualifiedName(const QString &key, SerializationMode sm, |
| 142 | QString &target) const |
| 143 | { |
| 144 | if (!m_scope.isEmpty()) { |
| 145 | target += m_scope; |
| 146 | target += m_separator; |
| 147 | } |
| 148 | if (sm == FullyQualified) |
| 149 | target += m_enumName + m_separator; |
| 150 | target += key; |
| 151 | } |
| 152 | |
| 153 | // -------------- DesignerMetaEnum: Meta type for enumerations |
| 154 | |
| 155 | class QDESIGNER_SHARED_EXPORT DesignerMetaEnum : public MetaEnum<int> |
| 156 | { |
| 157 | public: |
| 158 | DesignerMetaEnum(const QString &name, const QString &scope, const QString &separator); |
| 159 | DesignerMetaEnum() = default; |
| 160 | |
| 161 | QString toString(int value, SerializationMode sm, bool *ok = nullptr) const; |
| 162 | |
| 163 | QString messageToStringFailed(int value) const; |
| 164 | QString messageParseFailed(const QString &s) const; |
| 165 | |
| 166 | // parse a string (ignorant of scopes) |
| 167 | int parseEnum(const QString &s, bool *ok = nullptr) const { return keyToValue(key: s, ok); } |
| 168 | }; |
| 169 | |
| 170 | // -------------- DesignerMetaFlags: Meta type for flags. |
| 171 | // Note that while the handling of flags is done using unsigned integers, the actual values returned |
| 172 | // by the property system are integers. |
| 173 | |
| 174 | class QDESIGNER_SHARED_EXPORT DesignerMetaFlags : public MetaEnum<uint> |
| 175 | { |
| 176 | public: |
| 177 | explicit DesignerMetaFlags(const QString &enumName, const QString &scope, |
| 178 | const QString &separator); |
| 179 | DesignerMetaFlags() = default; |
| 180 | |
| 181 | QString toString(int value, SerializationMode sm) const; |
| 182 | QStringList flags(int value) const; |
| 183 | |
| 184 | QString messageParseFailed(const QString &s) const; |
| 185 | // parse a string (ignorant of scopes) |
| 186 | int parseFlags(const QString &s, bool *ok = nullptr) const; |
| 187 | }; |
| 188 | |
| 189 | // -------------- EnumValue: Returned by the property sheet for enumerations |
| 190 | |
| 191 | struct QDESIGNER_SHARED_EXPORT PropertySheetEnumValue |
| 192 | { |
| 193 | PropertySheetEnumValue(int v, const DesignerMetaEnum &me); |
| 194 | PropertySheetEnumValue(); |
| 195 | |
| 196 | int value{0}; |
| 197 | DesignerMetaEnum metaEnum; |
| 198 | }; |
| 199 | |
| 200 | // -------------- FlagValue: Returned by the property sheet for flags |
| 201 | |
| 202 | struct QDESIGNER_SHARED_EXPORT PropertySheetFlagValue |
| 203 | { |
| 204 | PropertySheetFlagValue(int v, const DesignerMetaFlags &mf); |
| 205 | PropertySheetFlagValue(); |
| 206 | |
| 207 | int value{0}; |
| 208 | DesignerMetaFlags metaFlags; |
| 209 | }; |
| 210 | |
| 211 | // -------------- PixmapValue: Returned by the property sheet for pixmaps |
| 212 | class QDESIGNER_SHARED_EXPORT PropertySheetPixmapValue |
| 213 | { |
| 214 | public: |
| 215 | PropertySheetPixmapValue(const QString &path); |
| 216 | PropertySheetPixmapValue(); |
| 217 | |
| 218 | // Check where a pixmap comes from |
| 219 | enum PixmapSource { LanguageResourcePixmap , ResourcePixmap, FilePixmap }; |
| 220 | static PixmapSource getPixmapSource(QDesignerFormEditorInterface *core, const QString & path); |
| 221 | |
| 222 | PixmapSource pixmapSource(QDesignerFormEditorInterface *core) const { return getPixmapSource(core, path: m_path); } |
| 223 | |
| 224 | QString path() const; |
| 225 | void setPath(const QString &path); // passing the empty path resets the pixmap |
| 226 | |
| 227 | private: |
| 228 | friend size_t qHash(const PropertySheetPixmapValue &p, size_t seed = 0) noexcept |
| 229 | { |
| 230 | return qHash(key: p.m_path, seed); |
| 231 | } |
| 232 | friend bool comparesEqual(const PropertySheetPixmapValue &lhs, |
| 233 | const PropertySheetPixmapValue &rhs) noexcept |
| 234 | { |
| 235 | return lhs.m_path == rhs.m_path; |
| 236 | } |
| 237 | Q_DECLARE_EQUALITY_COMPARABLE(PropertySheetPixmapValue) |
| 238 | |
| 239 | QString m_path; |
| 240 | }; |
| 241 | |
| 242 | // -------------- IconValue: Returned by the property sheet for icons |
| 243 | |
| 244 | class PropertySheetIconValueData; |
| 245 | |
| 246 | class QDESIGNER_SHARED_EXPORT PropertySheetIconValue |
| 247 | { |
| 248 | public: |
| 249 | explicit PropertySheetIconValue(const PropertySheetPixmapValue &pixmap); |
| 250 | PropertySheetIconValue(); |
| 251 | ~PropertySheetIconValue(); |
| 252 | PropertySheetIconValue(const PropertySheetIconValue &) noexcept; |
| 253 | PropertySheetIconValue &operator=(const PropertySheetIconValue &); |
| 254 | PropertySheetIconValue(PropertySheetIconValue &&) noexcept; |
| 255 | PropertySheetIconValue &operator=(PropertySheetIconValue &&) noexcept; |
| 256 | |
| 257 | bool isEmpty() const; |
| 258 | |
| 259 | QString theme() const; |
| 260 | void setTheme(const QString &); |
| 261 | |
| 262 | int themeEnum() const; |
| 263 | void setThemeEnum(int e); |
| 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 | // Convenience accessors to get themed/unthemed icons. |
| 273 | PropertySheetIconValue themed() const; |
| 274 | PropertySheetIconValue unthemed() const; |
| 275 | |
| 276 | using ModeStateKey = std::pair<QIcon::Mode, QIcon::State>; |
| 277 | using ModeStateToPixmapMap = QMap<ModeStateKey, PropertySheetPixmapValue>; |
| 278 | |
| 279 | const ModeStateToPixmapMap &paths() const; |
| 280 | |
| 281 | private: |
| 282 | friend QDESIGNER_SHARED_EXPORT |
| 283 | size_t qHash(const PropertySheetIconValue &p, size_t seed) noexcept; |
| 284 | friend size_t qHash(const PropertySheetIconValue &p) noexcept |
| 285 | { return qHash(p, seed: 0); } |
| 286 | friend QDESIGNER_SHARED_EXPORT |
| 287 | bool comparesEqual(const PropertySheetIconValue &lhs, |
| 288 | const PropertySheetIconValue &rhs) noexcept; |
| 289 | Q_DECLARE_EQUALITY_COMPARABLE(PropertySheetIconValue) |
| 290 | |
| 291 | QSharedDataPointer<PropertySheetIconValueData> m_data; |
| 292 | }; |
| 293 | |
| 294 | QDESIGNER_SHARED_EXPORT QDebug operator<<(QDebug, const PropertySheetIconValue &); |
| 295 | |
| 296 | class QDESIGNER_SHARED_EXPORT DesignerPixmapCache : public QObject |
| 297 | { |
| 298 | Q_OBJECT |
| 299 | public: |
| 300 | DesignerPixmapCache(QObject *parent = nullptr); |
| 301 | QPixmap pixmap(const PropertySheetPixmapValue &value) const; |
| 302 | void clear(); |
| 303 | signals: |
| 304 | void reloaded(); |
| 305 | private: |
| 306 | mutable QHash<PropertySheetPixmapValue, QPixmap> m_cache; |
| 307 | friend class FormWindowBase; |
| 308 | }; |
| 309 | |
| 310 | class QDESIGNER_SHARED_EXPORT DesignerIconCache : public QObject |
| 311 | { |
| 312 | Q_OBJECT |
| 313 | public: |
| 314 | explicit DesignerIconCache(DesignerPixmapCache *pixmapCache, QObject *parent = nullptr); |
| 315 | QIcon icon(const PropertySheetIconValue &value) const; |
| 316 | void clear(); |
| 317 | signals: |
| 318 | void reloaded(); |
| 319 | private: |
| 320 | mutable QHash<PropertySheetIconValue, QIcon> m_cache; |
| 321 | DesignerPixmapCache *m_pixmapCache; |
| 322 | friend class FormWindowBase; |
| 323 | }; |
| 324 | |
| 325 | // -------------- PropertySheetTranslatableData: Base class for translatable properties. |
| 326 | class QDESIGNER_SHARED_EXPORT PropertySheetTranslatableData |
| 327 | { |
| 328 | protected: |
| 329 | PropertySheetTranslatableData(bool translatable = true, |
| 330 | const QString &disambiguation = QString(), |
| 331 | const QString & = QString()); |
| 332 | |
| 333 | public: |
| 334 | bool translatable() const { return m_translatable; } |
| 335 | void setTranslatable(bool translatable) { m_translatable = translatable; } |
| 336 | QString disambiguation() const { return m_disambiguation; } |
| 337 | void setDisambiguation(const QString &d) { m_disambiguation = d; } |
| 338 | QString () const { return m_comment; } |
| 339 | void (const QString &) { m_comment = comment; } |
| 340 | QString id() const { return m_id; } |
| 341 | void setId(const QString &id) { m_id = id; } |
| 342 | |
| 343 | private: |
| 344 | friend bool comparesEqual(const PropertySheetTranslatableData &lhs, |
| 345 | const PropertySheetTranslatableData &rhs) noexcept |
| 346 | { |
| 347 | return lhs.m_translatable == rhs.m_translatable |
| 348 | && lhs.m_disambiguation == rhs.m_disambiguation |
| 349 | && lhs.m_comment == rhs.m_comment |
| 350 | && lhs.m_id == rhs.m_id; |
| 351 | } |
| 352 | Q_DECLARE_EQUALITY_COMPARABLE(PropertySheetTranslatableData) |
| 353 | |
| 354 | bool m_translatable; |
| 355 | QString m_disambiguation; |
| 356 | QString ; |
| 357 | QString m_id; |
| 358 | }; |
| 359 | |
| 360 | // -------------- StringValue: Returned by the property sheet for strings |
| 361 | class QDESIGNER_SHARED_EXPORT PropertySheetStringValue : public PropertySheetTranslatableData |
| 362 | { |
| 363 | public: |
| 364 | PropertySheetStringValue(const QString &value = QString(), bool translatable = true, |
| 365 | const QString &disambiguation = QString(), const QString & = QString()); |
| 366 | |
| 367 | QString value() const; |
| 368 | void setValue(const QString &value); |
| 369 | |
| 370 | private: |
| 371 | friend bool comparesEqual(const PropertySheetStringValue &lhs, |
| 372 | const PropertySheetStringValue &rhs) noexcept |
| 373 | { |
| 374 | const PropertySheetTranslatableData &upLhs = lhs; |
| 375 | const PropertySheetTranslatableData &upRhs = rhs; |
| 376 | return lhs.m_value == rhs.m_value && upLhs == upRhs; |
| 377 | } |
| 378 | Q_DECLARE_EQUALITY_COMPARABLE(PropertySheetStringValue) |
| 379 | |
| 380 | QString m_value; |
| 381 | }; |
| 382 | |
| 383 | // -------------- StringValue: Returned by the property sheet for string lists |
| 384 | class QDESIGNER_SHARED_EXPORT PropertySheetStringListValue : public PropertySheetTranslatableData |
| 385 | { |
| 386 | public: |
| 387 | PropertySheetStringListValue(const QStringList &value = QStringList(), |
| 388 | bool translatable = true, |
| 389 | const QString &disambiguation = QString(), |
| 390 | const QString & = QString()); |
| 391 | |
| 392 | QStringList value() const; |
| 393 | void setValue(const QStringList &value); |
| 394 | |
| 395 | private: |
| 396 | friend bool comparesEqual(const PropertySheetStringListValue &lhs, |
| 397 | const PropertySheetStringListValue &rhs) noexcept |
| 398 | { |
| 399 | const PropertySheetTranslatableData &upLhs = lhs; |
| 400 | const PropertySheetTranslatableData &upRhs = rhs; |
| 401 | return lhs.m_value == rhs.m_value && upLhs == upRhs; |
| 402 | } |
| 403 | Q_DECLARE_EQUALITY_COMPARABLE(PropertySheetStringListValue) |
| 404 | |
| 405 | QStringList m_value; |
| 406 | }; |
| 407 | |
| 408 | // -------------- StringValue: Returned by the property sheet for strings |
| 409 | class QDESIGNER_SHARED_EXPORT PropertySheetKeySequenceValue : public PropertySheetTranslatableData |
| 410 | { |
| 411 | public: |
| 412 | PropertySheetKeySequenceValue(const QKeySequence &value = QKeySequence(), |
| 413 | bool translatable = true, |
| 414 | const QString &disambiguation = QString(), |
| 415 | const QString & = QString()); |
| 416 | PropertySheetKeySequenceValue(const QKeySequence::StandardKey &standardKey, |
| 417 | bool translatable = true, |
| 418 | const QString &disambiguation = QString(), |
| 419 | const QString & = QString()); |
| 420 | |
| 421 | QKeySequence value() const; |
| 422 | void setValue(const QKeySequence &value); |
| 423 | QKeySequence::StandardKey standardKey() const; |
| 424 | void setStandardKey(const QKeySequence::StandardKey &standardKey); |
| 425 | bool isStandardKey() const; |
| 426 | |
| 427 | private: |
| 428 | friend bool comparesEqual(const PropertySheetKeySequenceValue &lhs, |
| 429 | const PropertySheetKeySequenceValue &rhs) noexcept |
| 430 | { |
| 431 | const PropertySheetTranslatableData &upLhs = lhs; |
| 432 | const PropertySheetTranslatableData &upRhs = rhs; |
| 433 | return lhs.m_value == rhs.m_value && lhs.m_standardKey == rhs.m_standardKey |
| 434 | && upLhs == upRhs; |
| 435 | } |
| 436 | Q_DECLARE_EQUALITY_COMPARABLE(PropertySheetKeySequenceValue) |
| 437 | |
| 438 | QKeySequence m_value; |
| 439 | QKeySequence::StandardKey m_standardKey; |
| 440 | }; |
| 441 | |
| 442 | } // namespace qdesigner_internal |
| 443 | |
| 444 | QT_END_NAMESPACE |
| 445 | |
| 446 | |
| 447 | // NOTE: Do not move this code, needed for GCC 3.3 |
| 448 | Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetEnumValue) |
| 449 | Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetFlagValue) |
| 450 | Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetPixmapValue) |
| 451 | Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetIconValue) |
| 452 | Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetStringValue) |
| 453 | Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetStringListValue) |
| 454 | Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetKeySequenceValue) |
| 455 | |
| 456 | |
| 457 | QT_BEGIN_NAMESPACE |
| 458 | |
| 459 | namespace qdesigner_internal { |
| 460 | |
| 461 | |
| 462 | // Create a command to change a text property (that is, create a reset property command if the text is empty) |
| 463 | QDESIGNER_SHARED_EXPORT QDesignerFormWindowCommand *createTextPropertyCommand(const QString &propertyName, const QString &text, QObject *object, QDesignerFormWindowInterface *fw); |
| 464 | |
| 465 | // Returns preferred task menu action for managed widget |
| 466 | QDESIGNER_SHARED_EXPORT QAction *preferredEditAction(QDesignerFormEditorInterface *core, QWidget *managedWidget); |
| 467 | |
| 468 | enum class UicLanguage |
| 469 | { |
| 470 | Cpp, |
| 471 | Python, |
| 472 | }; |
| 473 | |
| 474 | // Convenience to run UIC |
| 475 | QDESIGNER_SHARED_EXPORT bool runUIC(const QString &fileName, UicLanguage language, |
| 476 | QByteArray& ba, QString &errorMessage); |
| 477 | |
| 478 | // Find a suitable variable name for a class. |
| 479 | QDESIGNER_SHARED_EXPORT QString qtify(const QString &name); |
| 480 | |
| 481 | /* UpdateBlocker: Blocks the updates of the widget passed on while in scope. |
| 482 | * Does nothing if the incoming widget already has updatesEnabled==false |
| 483 | * which is important to avoid side-effects when putting it into QStackedLayout. */ |
| 484 | |
| 485 | class QDESIGNER_SHARED_EXPORT UpdateBlocker { |
| 486 | Q_DISABLE_COPY_MOVE(UpdateBlocker) |
| 487 | |
| 488 | public: |
| 489 | UpdateBlocker(QWidget *w); |
| 490 | ~UpdateBlocker(); |
| 491 | |
| 492 | private: |
| 493 | QWidget *m_widget; |
| 494 | const bool m_enabled; |
| 495 | }; |
| 496 | |
| 497 | // QPalette helpers: Mask for a single color role/group |
| 498 | QDESIGNER_SHARED_EXPORT quint64 paletteResolveMask(QPalette::ColorGroup colorGroup, |
| 499 | QPalette::ColorRole colorRole); |
| 500 | // Mask for the colors of a role in all groups (Active/Inactive/Disabled) |
| 501 | QDESIGNER_SHARED_EXPORT quint64 paletteResolveMask(QPalette::ColorRole colorRole); |
| 502 | |
| 503 | namespace Utils { |
| 504 | |
| 505 | inline int valueOf(const QVariant &value, bool *ok = nullptr) |
| 506 | { |
| 507 | if (value.canConvert<PropertySheetEnumValue>()) { |
| 508 | if (ok) |
| 509 | *ok = true; |
| 510 | return qvariant_cast<PropertySheetEnumValue>(v: value).value; |
| 511 | } |
| 512 | if (value.canConvert<PropertySheetFlagValue>()) { |
| 513 | if (ok) |
| 514 | *ok = true; |
| 515 | return qvariant_cast<PropertySheetFlagValue>(v: value).value; |
| 516 | } |
| 517 | return value.toInt(ok); |
| 518 | } |
| 519 | |
| 520 | inline bool isObjectAncestorOf(QObject *ancestor, QObject *child) |
| 521 | { |
| 522 | QObject *obj = child; |
| 523 | while (obj != nullptr) { |
| 524 | if (obj == ancestor) |
| 525 | return true; |
| 526 | obj = obj->parent(); |
| 527 | } |
| 528 | return false; |
| 529 | } |
| 530 | |
| 531 | inline bool isCentralWidget(QDesignerFormWindowInterface *fw, QWidget *widget) |
| 532 | { |
| 533 | if (! fw || ! widget) |
| 534 | return false; |
| 535 | |
| 536 | if (widget == fw->mainContainer()) |
| 537 | return true; |
| 538 | |
| 539 | // ### generalize for other containers |
| 540 | if (QMainWindow *mw = qobject_cast<QMainWindow*>(object: fw->mainContainer())) { |
| 541 | return mw->centralWidget() == widget; |
| 542 | } |
| 543 | |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | } // namespace Utils |
| 548 | |
| 549 | } // namespace qdesigner_internal |
| 550 | |
| 551 | QT_END_NAMESPACE |
| 552 | |
| 553 | #endif // QDESIGNER_UTILS_H |
| 554 | |