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
resetdecorator.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
7
8#include <qdesigner_utils_p.h>
9#include <iconloader_p.h>
10#include <formwindowbase_p.h>
11#include <formwindowcursor.h>
12#include <formwindowmanager.h>
13#include <formwindow.h>
14
15#include <QtDesigner/abstractformeditor.h>
16#include <QtDesigner/qextensionmanager.h>
17#include <QtDesigner/propertysheet.h>
18
19#include <QtWidgets/qboxlayout.h>
20#include <QtWidgets/qlabel.h>
21#include <QtWidgets/qtoolbutton.h>
22
24
25namespace qdesigner_internal {
26
27using namespace Qt::StringLiterals;
28
29ResetWidget::ResetWidget(QtProperty *property, QWidget *parent) :
30 QWidget(parent),
31 m_property(property),
32 m_textLabel(new QLabel(this)),
33 m_iconLabel(new QLabel(this)),
34 m_button(new QToolButton(this))
35{
36 m_textLabel->setSizePolicy(QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed));
37 m_iconLabel->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
38 m_button->setToolButtonStyle(Qt::ToolButtonIconOnly);
39 m_button->setIcon(createIconSet("resetproperty.png"_L1));
40 m_button->setIconSize(QSize(8,8));
41 m_button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));
42 connect(m_button, &QAbstractButton::clicked, this, &ResetWidget::slotClicked);
43 QLayout *layout = new QHBoxLayout(this);
44 layout->setContentsMargins(QMargins());
45 layout->setSpacing(m_spacing);
46 layout->addWidget(m_iconLabel);
47 layout->addWidget(m_textLabel);
48 layout->addWidget(m_button);
49 setFocusProxy(m_textLabel);
50 setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
51}
52
53void ResetWidget::setSpacing(int spacing)
54{
55 m_spacing = spacing;
56 layout()->setSpacing(m_spacing);
57}
58
60{
61 if (m_textLabel) {
62 delete m_textLabel;
63 m_textLabel = nullptr;
64 }
65 if (m_iconLabel) {
66 delete m_iconLabel;
67 m_iconLabel = nullptr;
68 }
69 delete layout();
70 QLayout *layout = new QHBoxLayout(this);
71 layout->setContentsMargins(QMargins());
72 layout->setSpacing(m_spacing);
73 layout->addWidget(widget);
74 layout->addWidget(m_button);
75 setFocusProxy(widget);
76}
77
78void ResetWidget::setResetEnabled(bool enabled)
79{
80 m_button->setEnabled(enabled);
81}
82
83void ResetWidget::setValueText(const QString &text)
84{
85 if (m_textLabel)
86 m_textLabel->setText(text);
87}
88
89void ResetWidget::setValueIcon(const QIcon &icon)
90{
91 QPixmap pix = icon.pixmap(QtPropertyBrowserUtils::itemViewIconSize, devicePixelRatioF());
92 if (m_iconLabel) {
93 m_iconLabel->setVisible(!pix.isNull());
94 m_iconLabel->setPixmap(pix);
95 }
96}
97
98void ResetWidget::slotClicked()
99{
100 emit resetProperty(m_property);
101}
102
103ResetDecorator::ResetDecorator(const QDesignerFormEditorInterface *core, QObject *parent)
104 : QObject(parent)
105 , m_core(core)
106{
107}
108
110{
111 const auto editors = m_resetWidgetToProperty.keys();
112 qDeleteAll(editors);
113}
114
116{
118 this, &ResetDecorator::slotPropertyChanged);
119}
120
122{
124 this, &ResetDecorator::slotPropertyChanged);
125}
126
127void ResetDecorator::setSpacing(int spacing)
128{
129 m_spacing = spacing;
130}
131
132static inline bool isModifiedInMultiSelection(const QDesignerFormEditorInterface *core,
133 const QString &propertyName)
134{
135 const QDesignerFormWindowInterface *form = core->formWindowManager()->activeFormWindow();
136 if (!form)
137 return false;
138 const QDesignerFormWindowCursorInterface *cursor = form->cursor();
139 const int selectionSize = cursor->selectedWidgetCount();
140 if (selectionSize < 2)
141 return false;
142 for (int i = 0; i < selectionSize; ++i) {
143 const QDesignerPropertySheetExtension *sheet =
144 qt_extension<QDesignerPropertySheetExtension*>(core->extensionManager(),
145 cursor->selectedWidget(i));
146 const int index = sheet->indexOf(propertyName);
147 if (index >= 0 && sheet->isChanged(index))
148 return true;
149 }
150 return false;
151}
152
153QWidget *ResetDecorator::editor(QWidget *subEditor, bool resettable, QtAbstractPropertyManager *manager, QtProperty *property,
154 QWidget *parent)
155{
156 Q_UNUSED(manager);
157
158 ResetWidget *resetWidget = nullptr;
159 if (resettable) {
160 resetWidget = new ResetWidget(property, parent);
161 resetWidget->setSpacing(m_spacing);
162 resetWidget->setResetEnabled(property->isModified() || isModifiedInMultiSelection(m_core, property->propertyName()));
163 resetWidget->setValueText(property->valueText());
164 resetWidget->setValueIcon(property->valueIcon());
165 resetWidget->setAutoFillBackground(true);
166 connect(resetWidget, &QObject::destroyed, this, &ResetDecorator::slotEditorDestroyed);
167 connect(resetWidget, &ResetWidget::resetProperty, this, &ResetDecorator::resetProperty);
168 m_createdResetWidgets[property].append(resetWidget);
169 m_resetWidgetToProperty[resetWidget] = property;
170 }
171 if (subEditor) {
172 if (resetWidget) {
173 subEditor->setParent(resetWidget);
174 resetWidget->setWidget(subEditor);
175 }
176 }
177 if (resetWidget)
178 return resetWidget;
179 return subEditor;
180}
181
182void ResetDecorator::slotPropertyChanged(QtProperty *property)
183{
184 const auto prIt = m_createdResetWidgets.constFind(property);
185 if (prIt == m_createdResetWidgets.constEnd())
186 return;
187
188 for (ResetWidget *widget : prIt.value()) {
189 widget->setResetEnabled(property->isModified()
190 || isModifiedInMultiSelection(m_core, property->propertyName()));
191 widget->setValueText(property->valueText());
192 widget->setValueIcon(property->valueIcon());
193 }
194}
195
196void ResetDecorator::slotEditorDestroyed(QObject *object)
197{
198 for (auto itEditor = m_resetWidgetToProperty.cbegin(), cend = m_resetWidgetToProperty.cend(); itEditor != cend; ++itEditor) {
199 if (itEditor.key() == object) {
200 ResetWidget *editor = itEditor.key();
201 QtProperty *property = itEditor.value();
202 m_resetWidgetToProperty.remove(editor);
203 m_createdResetWidgets[property].removeAll(editor);
204 if (m_createdResetWidgets[property].isEmpty())
205 m_createdResetWidgets.remove(property);
206 return;
207 }
208 }
209}
210
211} // namespace qdesigner_internal
212
213QT_END_NAMESPACE
friend class QWidget
Definition qpainter.h:431
The QtAbstractPropertyManager provides an interface for property managers.
void propertyChanged(QtProperty *property)
This signal is emitted whenever a property's data changes, passing a pointer to the property as param...
The QtProperty class encapsulates an instance of a property.
bool isModified() const
Returns whether the property is modified.
void connectPropertyManager(QtAbstractPropertyManager *manager)
void disconnectPropertyManager(QtAbstractPropertyManager *manager)
QWidget * editor(QWidget *subEditor, bool resettable, QtAbstractPropertyManager *manager, QtProperty *property, QWidget *parent)
void setValueIcon(const QIcon &icon)
void setValueText(const QString &text)
void setWidget(QWidget *widget)
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.
static bool isModifiedInMultiSelection(const QDesignerFormEditorInterface *core, const QString &propertyName)