Krita Source Code Documentation
Loading...
Searching...
No Matches
KoShadowConfigWidget.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2012 C. Boemann <cbo@boemann.dk>
3 * SPDX-FileCopyrightText: 2008 Jan Hambrecht <jaham@gmx.net>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
9#include "ui_KoShadowConfigWidget.h"
10
11#include <KoIcon.h>
12#include <KoUnit.h>
13#include <KoColorPopupAction.h>
14#include <KoCanvasBase.h>
16#include <KoSelection.h>
17#include <KoShapeShadow.h>
20
21#include <klocalizedstring.h>
22
23#include <QCheckBox>
24
25#include <math.h>
26
27class Q_DECL_HIDDEN KoShadowConfigWidget::Private
28{
29public:
31 {
32 }
33 Ui_KoShadowConfigWidget widget;
34 KoColorPopupAction *actionShadowColor {nullptr};
35 KoCanvasBase *canvas {nullptr};
36};
37
39 : QWidget(parent)
40 , d(new Private())
41{
42 d->widget.setupUi(this);
43 d->widget.shadowOffset->setValue(8.0);
44 d->widget.shadowBlur->setValue(8.0);
45 d->widget.shadowBlur->setMinimum(0.0);
46 d->widget.shadowAngle->setValue(315.0);
47 d->widget.shadowAngle->setMinimum(0.0);
48 d->widget.shadowAngle->setMaximum(360.0);
49 d->widget.shadowVisible->setChecked(false);
51
52 d->actionShadowColor = new KoColorPopupAction(this);
53 d->actionShadowColor->setCurrentColor(QColor(0, 0, 0, 192)); // some reasonable default for shadow
54 d->actionShadowColor->setIcon(koIcon("format-stroke-color"));
55 d->actionShadowColor->setToolTip(i18n("Change the color of the shadow"));
56 d->widget.shadowColor->setDefaultAction(d->actionShadowColor);
57
58 connect(d->widget.shadowVisible, SIGNAL(toggled(bool)), this, SLOT(applyChanges()));
59 connect(d->widget.shadowVisible, SIGNAL(toggled(bool)), this, SLOT(visibilityChanged()));
60 connect(d->actionShadowColor, SIGNAL(colorChanged(KoColor)), this, SLOT(applyChanges()));
61 connect(d->widget.shadowAngle, SIGNAL(valueChanged(int)), this, SLOT(applyChanges()));
62 connect(d->widget.shadowOffset, SIGNAL(valueChangedPt(qreal)), this, SLOT(applyChanges()));
63 connect(d->widget.shadowBlur, SIGNAL(valueChangedPt(qreal)), this, SLOT(applyChanges()));
64}
65
70
71void KoShadowConfigWidget::setShadowColor(const QColor &color)
72{
73 d->widget.shadowColor->blockSignals(true);
74 d->actionShadowColor->blockSignals(true);
75
76 d->actionShadowColor->setCurrentColor( color );
77
78 d->actionShadowColor->blockSignals(false);
79 d->widget.shadowColor->blockSignals(false);
80}
81
83{
84 return d->actionShadowColor->currentColor();
85}
86
87void KoShadowConfigWidget::setShadowOffset(const QPointF &offset)
88{
89 qreal length = sqrt(offset.x()*offset.x() + offset.y()*offset.y());
90 qreal angle = atan2(-offset.y(), offset.x());
91 if (angle < 0.0) {
92 angle += 2*M_PI;
93 }
94
95 d->widget.shadowAngle->blockSignals(true);
96 d->widget.shadowAngle->setValue(-90 - angle * 180.0 / M_PI);
97 d->widget.shadowAngle->blockSignals(false);
98
99 d->widget.shadowOffset->blockSignals(true);
100 d->widget.shadowOffset->changeValue(length);
101 d->widget.shadowOffset->blockSignals(false);
102}
103
105{
106 QPointF offset(d->widget.shadowOffset->value(), 0);
107 QTransform m;
108 m.rotate(d->widget.shadowAngle->value() + 90);
109 return m.map(offset);
110}
111
113{
114 d->widget.shadowBlur->blockSignals(true);
115 d->widget.shadowBlur->changeValue(blur);
116 d->widget.shadowBlur->blockSignals(false);
117}
118
120{
121 return d->widget.shadowBlur->value();
122}
123
125{
126 d->widget.shadowVisible->blockSignals(true);
127 d->widget.shadowVisible->setChecked(visible);
128 d->widget.shadowVisible->blockSignals(false);
130}
131
133{
134 return d->widget.shadowVisible->isChecked();
135}
136
138{
139 d->widget.shadowAngle->setEnabled( d->widget.shadowVisible->isChecked() );
140 d->widget.shadowBlur->setEnabled( d->widget.shadowVisible->isChecked() );
141 d->widget.shadowColor->setEnabled( d->widget.shadowVisible->isChecked() );
142 d->widget.shadowOffset->setEnabled( d->widget.shadowVisible->isChecked() );
143}
144
146{
147 if (d->canvas) {
149 KoShape * shape = selection->firstSelectedShape();
150 if (! shape) {
151 return;
152 }
153
154 KoShapeShadow *newShadow = new KoShapeShadow();
155 newShadow->setVisible(shadowVisible());
156 newShadow->setColor(shadowColor());
157 newShadow->setOffset(shadowOffset());
158 newShadow->setBlur(shadowBlur());
159 d->canvas->addCommand(new KoShapeShadowCommand(selection->selectedShapes(), newShadow));
160 }
161}
162
164{
165 if (! d->canvas) {
166 return;
167 }
168
170 KoShape * shape = selection->firstSelectedShape();
171
172 setEnabled(shape != 0);
173
174 if (! shape) {
175 setShadowVisible(false);
176 return;
177 }
178
179 KoShapeShadow * shadow = shape->shadow();
180 if (! shadow) {
181 setShadowVisible(false);
182 return;
183 }
184
185 setShadowVisible(shadow->isVisible());
186 setShadowOffset(shadow->offset());
187 setShadowColor(shadow->color());
188 setShadowBlur(shadow->blur());
189}
190
192{
193 d->canvas = canvas;
195 connect(canvas->selectedShapesProxy(), SIGNAL(selectionContentChanged()), this, SLOT(selectionChanged()));
196
197 setUnit(canvas->unit());
198
199 connect( d->canvas->resourceManager(), SIGNAL(canvasResourceChanged(int,QVariant)),
200 this, SLOT(resourceChanged(int,QVariant)) );
201}
202
204{
205 d->widget.shadowOffset->blockSignals(true);
206 d->widget.shadowBlur->blockSignals(true);
207 d->widget.shadowOffset->setUnitManager(managerOffset);
208 d->widget.shadowBlur->setUnitManager(managerBlur);
209 d->widget.shadowOffset->blockSignals(false);
210 d->widget.shadowBlur->blockSignals(false);
211}
212
214{
215 d->widget.shadowOffset->blockSignals(true);
216 d->widget.shadowBlur->blockSignals(true);
217 d->widget.shadowOffset->setUnit(unit);
218 d->widget.shadowBlur->setUnit(unit);
219 d->widget.shadowOffset->blockSignals(false);
220 d->widget.shadowBlur->blockSignals(false);
221}
222
223void KoShadowConfigWidget::resourceChanged( int key, const QVariant & res )
224{
225 if( key == KoCanvasResource::Unit ) {
226 setUnit(res.value<KoUnit>());
227 }
228}
qreal length(const QPointF &vec)
Definition Ellipse.cc:82
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
KisSelectedShapesProxy selectedShapesProxy
void addCommand(KUndo2Command *command) override
KoSelection * selection() override
The KisSpinBoxUnitManager class is an abstract interface for the unitspinboxes classes to manage diff...
virtual KoUnit unit() const =0
QPointer< KoCanvasResourceProvider > resourceManager
virtual KoSelectedShapesProxy * selectedShapesProxy() const =0
selectedShapesProxy() is a special interface for keeping a persistent connections to selectionChanged...
KoShape * firstSelectedShape() const
const QList< KoShape * > selectedShapes() const
A widget for configuring the shadow of a shape.
void setCanvas(KoCanvasBase *canvas)
void setUnitManagers(KisSpinBoxUnitManager *managerBlur, KisSpinBoxUnitManager *managerOffset)
void setShadowVisible(bool visible)
Sets if the shadow is visible.
void setShadowOffset(const QPointF &offset)
Sets the shadow offset.
QPointF shadowOffset() const
Returns the shadow offset.
KoShadowConfigWidget(QWidget *parent)
Ui_KoShadowConfigWidget widget
void resourceChanged(int key, const QVariant &res)
QColor shadowColor() const
Returns the shadow color.
qreal shadowBlur() const
Returns the shadow blur radius.
void setShadowBlur(const qreal &blur)
Sets the shadow blur radius.
bool shadowVisible() const
Returns if shadow is visible.
void setUnit(const KoUnit &unit)
void setShadowColor(const QColor &color)
Sets the shadow color.
The undo / redo command for setting the shape shadow.
bool isVisible() const
Returns if shadow is visible.
void setOffset(const QPointF &offset)
void setBlur(qreal blur)
void setColor(const QColor &color)
void setVisible(bool visible)
Sets the shadow visibility.
KoShapeShadow * shadow() const
Returns the currently set shadow or 0 if there is no shadow set.
Definition KoShape.cpp:1116
#define M_PI
Definition kis_global.h:111
#define koIcon(name)
Use these macros for icons without any issues.
Definition kis_icon.h:25
@ Unit
The unit of this canvas.
KisCanvas2 * canvas