Warning, file /office/calligra/libs/widgets/KoShadowConfigWidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2012 C. Boemann <cbo@boemann.dk>
0003  * Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "KoShadowConfigWidget.h"
0022 #include "ui_KoShadowConfigWidget.h"
0023 
0024 #include <KoIcon.h>
0025 #include <KoUnit.h>
0026 #include <KoColorPopupAction.h>
0027 #include <KoCanvasBase.h>
0028 #include <KoCanvasResourceManager.h>
0029 #include <KoSelection.h>
0030 #include <KoShapeShadow.h>
0031 #include <KoShapeShadowCommand.h>
0032 #include <KoShapeManager.h>
0033 
0034 #include <klocalizedstring.h>
0035 
0036 #include <QCheckBox>
0037 
0038 #include <math.h>
0039 
0040 class Q_DECL_HIDDEN KoShadowConfigWidget::Private
0041 {
0042 public:
0043     Private()
0044     {
0045     }
0046     Ui_KoShadowConfigWidget widget;
0047     KoColorPopupAction *actionShadowColor;
0048     KoCanvasBase *canvas;
0049 };
0050 
0051 KoShadowConfigWidget::KoShadowConfigWidget(QWidget *parent)
0052     : QWidget(parent)
0053     , d(new Private())
0054 {
0055     d->widget.setupUi(this);
0056     d->widget.shadowOffset->setValue(8.0);
0057     d->widget.shadowBlur->setValue(8.0);
0058     d->widget.shadowBlur->setMinimum(0.0);
0059     d->widget.shadowAngle->setValue(315.0);
0060     d->widget.shadowAngle->setMinimum(0.0);
0061     d->widget.shadowAngle->setMaximum(360.0);
0062     d->widget.shadowVisible->setChecked(false);
0063     visibilityChanged();
0064 
0065     d->actionShadowColor = new KoColorPopupAction(this);
0066     d->actionShadowColor->setCurrentColor(QColor(0, 0, 0, 192)); // some reasonable default for shadow
0067     d->actionShadowColor->setIcon(koIcon("format-stroke-color"));
0068     d->actionShadowColor->setToolTip(i18n("Change the color of the shadow"));
0069     d->widget.shadowColor->setDefaultAction(d->actionShadowColor);
0070 
0071     connect(d->widget.shadowVisible, SIGNAL(toggled(bool)), this, SLOT(applyChanges()));
0072     connect(d->widget.shadowVisible, SIGNAL(toggled(bool)), this, SLOT(visibilityChanged()));
0073     connect(d->actionShadowColor, SIGNAL(colorChanged(KoColor)), this, SLOT(applyChanges()));
0074     connect(d->widget.shadowAngle, SIGNAL(valueChanged(int)), this, SLOT(applyChanges()));
0075     connect(d->widget.shadowOffset, SIGNAL(valueChangedPt(qreal)), this, SLOT(applyChanges()));
0076     connect(d->widget.shadowBlur, SIGNAL(valueChangedPt(qreal)), this, SLOT(applyChanges()));
0077 }
0078 
0079 KoShadowConfigWidget::~KoShadowConfigWidget()
0080 {
0081     delete d;
0082 }
0083 
0084 void KoShadowConfigWidget::setShadowColor(const QColor &color)
0085 {
0086     d->widget.shadowColor->blockSignals(true);
0087     d->actionShadowColor->blockSignals(true);
0088 
0089     d->actionShadowColor->setCurrentColor( color );
0090 
0091     d->actionShadowColor->blockSignals(false);
0092     d->widget.shadowColor->blockSignals(false);
0093 }
0094 
0095 QColor KoShadowConfigWidget::shadowColor() const
0096 {
0097     return d->actionShadowColor->currentColor();
0098 }
0099 
0100 void KoShadowConfigWidget::setShadowOffset(const QPointF &offset)
0101 {
0102     qreal length = sqrt(offset.x()*offset.x() + offset.y()*offset.y());
0103     qreal angle = atan2(-offset.y(), offset.x());
0104     if (angle < 0.0) {
0105         angle += 2*M_PI;
0106     }
0107 
0108     d->widget.shadowAngle->blockSignals(true);
0109     d->widget.shadowAngle->setValue(-90 - angle * 180.0 / M_PI);
0110     d->widget.shadowAngle->blockSignals(false);
0111 
0112     d->widget.shadowOffset->blockSignals(true);
0113     d->widget.shadowOffset->changeValue(length);
0114     d->widget.shadowOffset->blockSignals(false);
0115 }
0116 
0117 QPointF KoShadowConfigWidget::shadowOffset() const
0118 {
0119     QPointF offset(d->widget.shadowOffset->value(), 0);
0120     QTransform m;
0121     m.rotate(d->widget.shadowAngle->value() + 90);
0122     return m.map(offset);
0123 }
0124 
0125 void KoShadowConfigWidget::setShadowBlur(const qreal &blur)
0126 {
0127     d->widget.shadowBlur->blockSignals(true);
0128     d->widget.shadowBlur->changeValue(blur);
0129     d->widget.shadowBlur->blockSignals(false);
0130 }
0131 
0132 qreal KoShadowConfigWidget::shadowBlur() const
0133 {
0134     return d->widget.shadowBlur->value();
0135 }
0136 
0137 void KoShadowConfigWidget::setShadowVisible(bool visible)
0138 {
0139     d->widget.shadowVisible->blockSignals(true);
0140     d->widget.shadowVisible->setChecked(visible);
0141     d->widget.shadowVisible->blockSignals(false);
0142     visibilityChanged();
0143 }
0144 
0145 bool KoShadowConfigWidget::shadowVisible() const
0146 {
0147     return d->widget.shadowVisible->isChecked();
0148 }
0149 
0150 void KoShadowConfigWidget::visibilityChanged()
0151 {
0152     d->widget.shadowAngle->setEnabled( d->widget.shadowVisible->isChecked() );
0153     d->widget.shadowBlur->setEnabled( d->widget.shadowVisible->isChecked() );
0154     d->widget.shadowColor->setEnabled( d->widget.shadowVisible->isChecked() );
0155     d->widget.shadowOffset->setEnabled( d->widget.shadowVisible->isChecked() );
0156 }
0157 
0158 void KoShadowConfigWidget::applyChanges()
0159 {
0160     if (d->canvas) {
0161         KoSelection *selection = d->canvas->shapeManager()->selection();
0162         KoShape * shape = selection->firstSelectedShape(KoFlake::TopLevelSelection);
0163         if (! shape) {
0164             return;
0165         }
0166 
0167         KoShapeShadow *newShadow = new KoShapeShadow();
0168         newShadow->setVisible(shadowVisible());
0169         newShadow->setColor(shadowColor());
0170         newShadow->setOffset(shadowOffset());
0171         newShadow->setBlur(shadowBlur());
0172         d->canvas->addCommand(new KoShapeShadowCommand(selection->selectedShapes(KoFlake::TopLevelSelection), newShadow));
0173     }
0174 }
0175 
0176 void KoShadowConfigWidget::selectionChanged()
0177 {
0178     if (! d->canvas) {
0179         return;
0180     }
0181 
0182     KoSelection *selection = d->canvas->shapeManager()->selection();
0183     KoShape * shape = selection->firstSelectedShape(KoFlake::TopLevelSelection);
0184 
0185     setEnabled(shape != nullptr);
0186 
0187     if (! shape) {
0188         setShadowVisible(false);
0189         return;
0190     }
0191 
0192     KoShapeShadow * shadow = shape->shadow();
0193     if (! shadow) {
0194         setShadowVisible(false);
0195         return;
0196     }
0197 
0198     setShadowVisible(shadow->isVisible());
0199     setShadowOffset(shadow->offset());
0200     setShadowColor(shadow->color());
0201     setShadowBlur(shadow->blur());
0202 }
0203 
0204 void KoShadowConfigWidget::setCanvas(KoCanvasBase *canvas)
0205 {
0206     d->canvas = canvas;
0207     connect(canvas->shapeManager(), SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
0208     connect(canvas->shapeManager(), SIGNAL(selectionContentChanged()), this, SLOT(selectionChanged()));
0209 
0210     setUnit(canvas->unit());
0211 
0212     connect( d->canvas->resourceManager(), SIGNAL(canvasResourceChanged(int,QVariant)),
0213              this, SLOT(resourceChanged(int,QVariant)) );
0214 }
0215 
0216 void KoShadowConfigWidget::setUnit(const KoUnit &unit)
0217 {
0218     d->widget.shadowOffset->blockSignals(true);
0219     d->widget.shadowBlur->blockSignals(true);
0220     d->widget.shadowOffset->setUnit(unit);
0221     d->widget.shadowBlur->setUnit(unit);
0222     d->widget.shadowOffset->blockSignals(false);
0223     d->widget.shadowBlur->blockSignals(false);
0224 }
0225 
0226 void KoShadowConfigWidget::resourceChanged( int key, const QVariant & res )
0227 {
0228     if( key == KoCanvasResourceManager::Unit ) {
0229         setUnit(res.value<KoUnit>());
0230     }
0231 }