Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdesigner_utils_p.h
Go to the documentation of this file.
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
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
32QT_BEGIN_NAMESPACE
33
34class QDebug;
35
36namespace qdesigner_internal {
37class QDesignerFormWindowCommand;
38class DesignerIconCache;
39class FormWindowBase;
40
41
43
45
46QDESIGNER_SHARED_EXPORT void designerWarning(const QString &message);
47
48QDESIGNER_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
62template <class IntType>
64{
65public:
67 Qualified }; // Qt pre 6.7 without enum name
68
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
86protected:
87 void appendQualifiedName(const QString &key, SerializationMode sm, QString &target) const;
88
89private:
90 QString m_enumName;
91 QString m_scope;
92 QString m_separator;
93 KeyToValueMap m_keyToValueMap;
94 QStringList m_keys;
95};
96
97template <class IntType>
98MetaEnum<IntType>::MetaEnum(const QString &enumName, const QString &scope, const QString &separator) :
100 m_scope(scope),
102{
103}
104
105template <class IntType>
106void MetaEnum<IntType>::addKey(IntType value, const QString &name)
107{
108 m_keyToValueMap.insert({name, value});
109 m_keys.append(name);
110}
111
112template <class IntType>
113QString 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
127template <class IntType>
128IntType MetaEnum<IntType>::keyToValue(QStringView key, bool *ok) const
129{
130 const auto lastSep = key.lastIndexOf(m_separator);
131 if (lastSep != -1)
132 key = key.sliced(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
140template <class IntType>
141void 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
156{
157public:
159 DesignerMetaEnum() = default;
160
161 QString toString(int value, SerializationMode sm, bool *ok = nullptr) const;
162
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(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
175{
176public:
177 explicit DesignerMetaFlags(const QString &enumName, const QString &scope,
178 const QString &separator);
179 DesignerMetaFlags() = default;
180
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
199
200// -------------- FlagValue: Returned by the property sheet for flags
201
210
211// -------------- PixmapValue: Returned by the property sheet for pixmaps
213{
214public:
217
218 // Check where a pixmap comes from
221
223
224 QString path() const;
225 void setPath(const QString &path); // passing the empty path resets the pixmap
226
227private:
228 friend size_t qHash(const PropertySheetPixmapValue &p, size_t seed = 0) noexcept
229 {
230 return qHash(p.m_path, seed);
231 }
233 const PropertySheetPixmapValue &rhs) noexcept
234 {
235 return lhs.m_path == rhs.m_path;
236 }
238
240};
241
242// -------------- IconValue: Returned by the property sheet for icons
243
245
247{
248 public:
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
266 void setPixmap(QIcon::Mode mode, QIcon::State state, const PropertySheetPixmapValue &path); // passing the empty path resets the pixmap
267
268 uint mask() const;
271
272 // Convenience accessors to get themed/unthemed icons.
275
278
279 const ModeStateToPixmapMap &paths() const;
280
281private:
284 friend size_t qHash(const PropertySheetIconValue &p) noexcept
285 { return qHash(p, 0); }
288 const PropertySheetIconValue &rhs) noexcept;
290
292};
293
295
297{
299public:
302 void clear();
303signals:
304 void reloaded();
305private:
307 friend class FormWindowBase;
308};
309
311{
313public:
316 void clear();
317signals:
318 void reloaded();
319private:
322 friend class FormWindowBase;
323};
324
325// -------------- PropertySheetTranslatableData: Base class for translatable properties.
359
360// -------------- StringValue: Returned by the property sheet for strings
382
383// -------------- StringValue: Returned by the property sheet for string lists
407
408// -------------- StringValue: Returned by the property sheet for strings
441
442} // namespace qdesigner_internal
443
444QT_END_NAMESPACE
445
446
447// NOTE: Do not move this code, needed for GCC 3.3
448Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetEnumValue)
449Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetFlagValue)
450Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetPixmapValue)
451Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetIconValue)
452Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetStringValue)
453Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetStringListValue)
454Q_DECLARE_METATYPE(qdesigner_internal::PropertySheetKeySequenceValue)
455
456
457QT_BEGIN_NAMESPACE
458
459namespace 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)
464
465// Returns preferred task menu action for managed widget
467
468enum class UicLanguage
469{
470 Cpp,
471 Python,
472};
473
474// Convenience to run UIC
475QDESIGNER_SHARED_EXPORT bool runUIC(const QString &fileName, UicLanguage language,
476 QByteArray& ba, QString &errorMessage);
477
478// Find a suitable variable name for a class.
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
487
488public:
490 ~UpdateBlocker();
491
492private:
494 const bool m_enabled;
495};
496
497// QPalette helpers: Mask for a single color role/group
498QDESIGNER_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)
501QDESIGNER_SHARED_EXPORT quint64 paletteResolveMask(QPalette::ColorRole colorRole);
502
503namespace Utils {
504
505inline 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>(value).value;
511 }
512 if (value.canConvert<PropertySheetFlagValue>()) {
513 if (ok)
514 *ok = true;
515 return qvariant_cast<PropertySheetFlagValue>(value).value;
516 }
517 return value.toInt(ok);
518}
519
520inline 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
531inline 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*>(fw->mainContainer())) {
541 return mw->centralWidget() == widget;
542 }
543
544 return false;
545}
546
547} // namespace Utils
548
549} // namespace qdesigner_internal
550
551QT_END_NAMESPACE
552
553#endif // QDESIGNER_UTILS_H
static bool canBeBuddy(QWidget *w, QDesignerFormWindowInterface *form)
static QString buddy(QLabel *label, QDesignerFormEditorInterface *core)
static constexpr auto buddyPropertyC
#define QT_BUDDYEDITOR_EXPORT
The QDesignerMetaDataBaseItemInterface class provides an interface to individual items in \QD's meta ...
friend class QWidget
Definition qpainter.h:421
void init(QWidget *parentWidget, QAction *action, QAction *beforeAction=nullptr, bool update=true)
ActionInsertionCommand(const QString &text, QDesignerFormWindowInterface *formWindow)
void redo() override
Applies a change to the document.
AddMenuActionCommand(QDesignerFormWindowInterface *formWindow)
void undo() override
Reverts a change to the document.
void redo() override
Applies a change to the document.
AddStackedWidgetPageCommand(QDesignerFormWindowInterface *formWindow)
void undo() override
Reverts a change to the document.
void init(QStackedWidget *stackedWidget, InsertionMode mode)
void redo() override
Applies a change to the document.
void undo() override
Reverts a change to the document.
void init(QTabWidget *tabWidget, InsertionMode mode)
AddTabPageCommand(QDesignerFormWindowInterface *formWindow)
void redo() override
Applies a change to the document.
AddToolBarCommand(QDesignerFormWindowInterface *formWindow)
void init(QMainWindow *mainWindow, Qt::ToolBarArea area)
void undo() override
Reverts a change to the document.
AddToolBoxPageCommand(QDesignerFormWindowInterface *formWindow)
void redo() override
Applies a change to the document.
void init(QToolBox *toolBox, InsertionMode mode)
void undo() override
Reverts a change to the document.
void setBackground(QWidget *background) override
QDesignerFormWindowInterface * formWindow() const
void endConnection(QWidget *target, const QPoint &pos) override
void widgetRemoved(QWidget *w) override
QWidget * widgetAt(const QPoint &pos) const override
Connection * createConnection(QWidget *source, QWidget *destination) override
void createContextMenu(QMenu &menu) override
CreateMenuBarCommand(QDesignerFormWindowInterface *formWindow)
void redo() override
Applies a change to the document.
void undo() override
Reverts a change to the document.
void redo() override
Applies a change to the document.
void undo() override
Reverts a change to the document.
CreateStatusBarCommand(QDesignerFormWindowInterface *formWindow)
void undo() override
Reverts a change to the document.
void init(QDesignerMenu *menu, QAction *action, QObject *m_objectToSelect=nullptr)
CreateSubmenuCommand(QDesignerFormWindowInterface *formWindow)
void redo() override
Applies a change to the document.
void restore(QDesignerFormWindowInterface *formWindow) const
void save(const QDesignerFormWindowInterface *formWindow)
void redo() override
Applies a change to the document.
void undo() override
Reverts a change to the document.
DeleteMenuBarCommand(QDesignerFormWindowInterface *formWindow)
DeleteStackedWidgetPageCommand(QDesignerFormWindowInterface *formWindow)
void redo() override
Applies a change to the document.
void undo() override
Reverts a change to the document.
void redo() override
Applies a change to the document.
void undo() override
Reverts a change to the document.
DeleteTabPageCommand(QDesignerFormWindowInterface *formWindow)
void undo() override
Reverts a change to the document.
void redo() override
Applies a change to the document.
DeleteToolBarCommand(QDesignerFormWindowInterface *formWindow)
DeleteToolBoxPageCommand(QDesignerFormWindowInterface *formWindow)
void redo() override
Applies a change to the document.
void undo() override
Reverts a change to the document.
DockWidgetCommand(const QString &description, QDesignerFormWindowInterface *formWindow)
void undo() override
Reverts a change to the document.
InsertActionIntoCommand(QDesignerFormWindowInterface *formWindow)
void redo() override
Applies a change to the document.
void init(QAction *action, QAction *actionBefore, QWidget *associatedWidget, QWidget *objectToSelect)
MenuActionCommand(const QString &text, QDesignerFormWindowInterface *formWindow)
const KeyToValueMap & keyToValueMap() const
const QStringList & keys() const
const QString & scope() const
void addKey(IntType value, const QString &name)
const QString & enumName() const
IntType keyToValue(QStringView key, bool *ok=nullptr) const
void appendQualifiedName(const QString &key, SerializationMode sm, QString &target) const
const QString & separator() const
QString valueToKey(IntType value, bool *ok=nullptr) const
MetaEnum(const QString &enumName, const QString &scope, const QString &separator)
void init(QStackedWidget *stackedWidget, QWidget *page, int newIndex)
MoveStackedWidgetCommand(QDesignerFormWindowInterface *formWindow)
void undo() override
Reverts a change to the document.
void redo() override
Applies a change to the document.
void undo() override
Reverts a change to the document.
MoveTabPageCommand(QDesignerFormWindowInterface *formWindow)
void redo() override
Applies a change to the document.
void init(QTabWidget *tabWidget, QWidget *page, const QIcon &icon, const QString &label, int index, int newIndex)
MoveToolBoxPageCommand(QDesignerFormWindowInterface *formWindow)
void redo() override
Applies a change to the document.
void init(QToolBox *toolBox, QWidget *page, int newIndex)
void undo() override
Reverts a change to the document.
RemoveActionFromCommand(QDesignerFormWindowInterface *formWindow)
void undo() override
Reverts a change to the document.
void redo() override
Applies a change to the document.
RemoveMenuActionCommand(QDesignerFormWindowInterface *formWindow)
void undo() override
Reverts a change to the document.
void redo() override
Applies a change to the document.
StackedWidgetCommand(QDesignerFormWindowInterface *formWindow)
void init(QStackedWidget *stackedWidget)
TabWidgetCommand(QDesignerFormWindowInterface *formWindow)
ToolBoxCommand(QDesignerFormWindowInterface *formWindow)
Combined button and popup list for selecting options.
int valueOf(const QVariant &value, bool *ok=nullptr)
bool isObjectAncestorOf(QObject *ancestor, QObject *child)
bool isCentralWidget(QDesignerFormWindowInterface *fw, QWidget *widget)
Auxiliary methods to store/retrieve settings.
QDESIGNER_SHARED_EXPORT void getFormLayoutItemPosition(const QFormLayout *formLayout, int index, int *rowPtr, int *columnPtr=nullptr, int *rowspanPtr=nullptr, int *colspanPtr=nullptr)
QDESIGNER_SHARED_EXPORT QDebug operator<<(QDebug, const PropertySheetIconValue &)
static QUndoCommand * createBuddyCommand(QDesignerFormWindowInterface *fw, QLabel *label, QWidget *buddy)
QDESIGNER_SHARED_EXPORT void formLayoutAddWidget(QFormLayout *formLayout, QWidget *w, const QRect &r, bool insert)
QDESIGNER_SHARED_EXPORT void designerWarning(const QString &message)
QDESIGNER_SHARED_EXPORT void reloadIconResources(DesignerIconCache *iconCache, QObject *object)
QDESIGNER_SHARED_EXPORT bool runUIC(const QString &fileName, UicLanguage language, QByteArray &ba, QString &errorMessage)
#define QDESIGNER_SHARED_EXPORT