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