Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_brush_hud.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "kis_brush_hud.h"
8
9#include <QGuiApplication>
10#include <QVBoxLayout>
11#include <QHBoxLayout>
12#include <QPointer>
13#include <QLabel>
14#include <QPainter>
15#include <QPaintEvent>
16#include <QScrollArea>
17#include <QEvent>
18#include <QToolButton>
19#include <QAction>
20
25#include "kis_paintop_preset.h"
29#include "kis_icon_utils.h"
32#include "kis_elided_label.h"
33
34#include "kis_canvas2.h"
35#include "KisViewManager.h"
36#include "kactioncollection.h"
37
38#include "kis_debug.h"
39
40
58
60 : QWidget(parent, Qt::FramelessWindowHint),
61 m_d(new Private)
62{
63 m_d->provider = provider;
64
65 QVBoxLayout *layout = new QVBoxLayout(this);
66
67 QHBoxLayout *labelLayout = new QHBoxLayout();
68 m_d->lblPresetIcon = new QLabel(this);
69 const QSize iconSize = QSize(22,22);
70 m_d->lblPresetIcon->setMinimumSize(iconSize);
71 m_d->lblPresetIcon->setMaximumSize(iconSize);
72 m_d->lblPresetIcon->setScaledContents(true);
73
74 m_d->lblPresetName = new KisElidedLabel("<Preset Name>", Qt::ElideMiddle, this);
75 m_d->lblPresetName->setSizePolicy(QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred));
76
77 m_d->btnReloadPreset = new QToolButton(this);
78 m_d->btnReloadPreset->setAutoRaise(true);
79 m_d->btnReloadPreset->setToolTip(i18n("Reload Original Preset"));
80
81 m_d->btnConfigure = new QToolButton(this);
82 m_d->btnConfigure->setAutoRaise(true);
83 m_d->btnConfigure->setToolTip(i18n("Configure the on-canvas brush editor"));
84
85 connect(m_d->btnReloadPreset, SIGNAL(clicked()), SLOT(slotReloadPreset()));
86 connect(m_d->btnConfigure, SIGNAL(clicked()), SLOT(slotConfigBrushHud()));
87
88 labelLayout->addWidget(m_d->lblPresetIcon);
89 labelLayout->addWidget(m_d->lblPresetName);
90 labelLayout->addWidget(m_d->btnReloadPreset);
91 labelLayout->addWidget(m_d->btnConfigure);
92
93 layout->addLayout(labelLayout);
94
95 m_d->wdgPropertiesArea = new QScrollArea(this);
96 m_d->wdgPropertiesArea->setAlignment(Qt::AlignLeft | Qt::AlignTop);
97 m_d->wdgPropertiesArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
98
99 m_d->wdgPropertiesArea->setWidgetResizable(true);
100
101 m_d->wdgProperties = new QWidget(this);
102 m_d->propertiesLayout = new QVBoxLayout(m_d->wdgProperties);
103 m_d->propertiesLayout->setSpacing(0);
104 m_d->propertiesLayout->setContentsMargins(0, 0, 22, 0);
105 m_d->propertiesLayout->setSizeConstraint(QLayout::SetMinimumSize);
106
107 // not adding any widgets until explicitly requested
108
109 m_d->wdgPropertiesArea->setWidget(m_d->wdgProperties);
110 layout->addWidget(m_d->wdgPropertiesArea);
111
112 // unfortunately the sizeHint() function of QScrollArea is pretty broken
113 // and it would add another event loop iteration to react to it anyway,
114 // so let's just catch LayoutRequest events from the properties widget directly
115 m_d->wdgProperties->installEventFilter(this);
116
117 updateIcons();
118
119 setCursor(Qt::ArrowCursor);
120
121 // Prevent tablet events from being captured by the canvas
122 setAttribute(Qt::WA_NoMousePropagation, true);
123}
124
125
126
130
132{
133 this->setPalette(qApp->palette());
134 for(int i=0; i<this->children().size(); i++) {
135 QWidget *w = qobject_cast<QWidget*>(this->children().at(i));
136 if (w) {
137 w->setPalette(qApp->palette());
138 }
139 }
140 for(int i=0; i<m_d->wdgProperties->children().size(); i++) {
141 KisUniformPaintOpPropertyWidget *w = qobject_cast<KisUniformPaintOpPropertyWidget*>(m_d->wdgProperties->children().at(i));
142 if (w) {
143 w->slotThemeChanged(qApp->palette());
144 }
145 }
146 m_d->btnReloadPreset->setIcon(KisIconUtils::loadIcon("reload-preset-16"));
147 m_d->btnConfigure->setIcon(KisIconUtils::loadIcon("applications-system"));
148}
149
151{
152 m_d->presetConnections.clear();
155}
156
158{
159 while (m_d->propertiesLayout->count()) {
160 QLayoutItem *item = m_d->propertiesLayout->takeAt(0);
161
162 QWidget *w = item->widget();
163 if (w) {
164 w->deleteLater();
165 }
166
167 delete item;
168 }
169
170 m_d->currentPreset.clear();
171}
172
174{
175 KisPaintOpPresetSP preset = m_d->provider->currentPreset();
176
177 if (preset == m_d->currentPreset) return;
178
179 m_d->presetConnections.clear();
181
182 m_d->currentPreset = preset;
183 m_d->presetConnections.addConnection(
184 m_d->currentPreset->updateProxy(), SIGNAL(sigUniformPropertiesChanged()),
185 this, SLOT(slotReloadProperties()));
186
187 m_d->lblPresetIcon->setPixmap(QPixmap::fromImage(preset->image()));
188 m_d->lblPresetName->setLongText(preset->name());
189
191
192 {
193 QList<KisUniformPaintOpPropertySP> allProperties = preset->uniformProperties();
194 QList<KisUniformPaintOpPropertySP> discardedProperties;
195
197 cfg.filterProperties(preset->paintOp().id(),
198 allProperties,
199 &properties,
200 &discardedProperties);
201 }
202
203 Q_FOREACH(auto property, properties) {
204 QWidget *w = 0;
205
206 if (!property->isVisible()) continue;
207
208 if (property->type() == KisUniformPaintOpProperty::Int) {
209 w = new KisUniformPaintOpPropertyIntSlider(property, m_d->wdgProperties);
210 } else if (property->type() == KisUniformPaintOpProperty::Double) {
211 w = new KisUniformPaintOpPropertyDoubleSlider(property, m_d->wdgProperties);
212 } else if (property->type() == KisUniformPaintOpProperty::Bool) {
213 w = new KisUniformPaintOpPropertyCheckBox(property, m_d->wdgProperties);
214 } else if (property->type() == KisUniformPaintOpProperty::Combo) {
215 w = new KisUniformPaintOpPropertyComboBox(property, m_d->wdgProperties);
216 }
217
218 if (w) {
219 w->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
220 m_d->propertiesLayout->addWidget(w);
221 }
222 }
223
224 m_d->propertiesLayout->addStretch();
225}
226
227void KisBrushHud::showEvent(QShowEvent *event)
228{
229 m_d->connections.clear();
230 m_d->connections.addUniqueConnection(
231 m_d->provider->resourceManager(), SIGNAL(canvasResourceChanged(int,QVariant)),
232 this, SLOT(slotCanvasResourceChanged(int,QVariant)));
233
235
236 QWidget::showEvent(event);
237}
238
239void KisBrushHud::hideEvent(QHideEvent *event)
240{
241 m_d->connections.clear();
242 QWidget::hideEvent(event);
243
245}
246
247void KisBrushHud::slotCanvasResourceChanged(int key, const QVariant &resource)
248{
249 Q_UNUSED(resource);
250
253 }
254}
255
256void KisBrushHud::paintEvent(QPaintEvent *event)
257{
258 QColor bgColor = palette().color(QPalette::Window);
259
260 QPainter painter(this);
261 painter.fillRect(rect() & event->rect(), bgColor);
262 painter.end();
263
264 QWidget::paintEvent(event);
265}
266
267bool KisBrushHud::event(QEvent *event)
268{
269 switch (event->type()) {
270 case QEvent::TabletPress:
271 case QEvent::TabletMove:
272 case QEvent::TabletRelease:
273 // Allow the tablet event to be translated to a mouse event on certain platforms
274 break;
275 case QEvent::MouseButtonPress:
276 case QEvent::MouseMove:
277 case QEvent::MouseButtonRelease:
278 case QEvent::Wheel:
279 event->accept();
280 return true;
281 default:
282 break;
283 }
284
285 return QWidget::event(event);
286}
287
288bool KisBrushHud::eventFilter(QObject *watched, QEvent *event)
289{
290 // LayoutRequest event is sent from a layout to its parent widget
291 // when size requirements have been determined, i.e. sizeHint is available
292 if (watched == m_d->wdgProperties && event->type() == QEvent::LayoutRequest)
293 {
294 int totalMargin = 2 * m_d->wdgPropertiesArea->frameWidth();
295 m_d->wdgPropertiesArea->setMinimumWidth(m_d->wdgProperties->sizeHint().width() + totalMargin);
296 }
297 return false;
298}
299
301{
302 if (!m_d->currentPreset) return;
303
304 KisDlgConfigureBrushHud dlg(m_d->currentPreset);
305 dlg.exec();
306
308}
309
311{
312 KisCanvas2* canvas = dynamic_cast<KisCanvas2*>(m_d->provider->canvas());
314 canvas->viewManager()->actionCollection()->action("reload_preset_action")->trigger();
315}
int iconSize(qreal width, qreal height)
void filterProperties(const QString &paintOpId, const QList< KisUniformPaintOpPropertySP > &allProperties, QList< KisUniformPaintOpPropertySP > *chosenProperties, QList< KisUniformPaintOpPropertySP > *skippedProperties) const
void slotReloadPreset()
void slotCanvasResourceChanged(int key, const QVariant &resource)
KisBrushHud(KisCanvasResourceProvider *provider, QWidget *parent)
const QScopedPointer< Private > m_d
bool eventFilter(QObject *watched, QEvent *event) override
bool event(QEvent *event) override
void paintEvent(QPaintEvent *event) override
void hideEvent(QHideEvent *event) override
void clearProperties() const
~KisBrushHud() override
void updateProperties()
void slotConfigBrushHud()
void slotReloadProperties()
void showEvent(QShowEvent *event) override
KisViewManager * viewManager() const
QAction * action(int index) const
virtual KisKActionCollection * actionCollection() const
#define KIS_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:75
QIcon loadIcon(const QString &name)
QPointer< QWidget > wdgProperties
KisSignalAutoConnectionsStore presetConnections
QPointer< KisElidedLabel > lblPresetName
KisPaintOpPresetSP currentPreset
QPointer< QToolButton > btnReloadPreset
KisCanvasResourceProvider * provider
QPointer< QVBoxLayout > propertiesLayout
QPointer< QLabel > lblPresetIcon
QPointer< QToolButton > btnConfigure
KisSignalAutoConnectionsStore connections
QPointer< QScrollArea > wdgPropertiesArea