Commit 941e583d authored by Jan Hambrecht's avatar Jan Hambrecht

implemented support for shape opacity/transparency

BUG:201688
CCBUG:202178


svn path=/trunk/koffice/; revision=1059492
parent 4d114b8f
......@@ -142,6 +142,7 @@ set(flake_SRCS
commands/KoEventActionRemoveCommand.cpp
commands/KoShapeRenameCommand.cpp
commands/KoPathPointMergeCommand.cpp
commands/KoShapeTransparencyCommand.cpp
tools/KoCreatePathTool.cpp
tools/KoCreatePathToolFactory.cpp
tools/KoCreateShapeStrategy.cpp
......
......@@ -78,6 +78,7 @@ KoShapePrivate::KoShapePrivate(KoShape *shape)
q(shape),
shadow(0),
filterEffectStack(0),
transparency(0.0),
zIndex(0),
visible(true),
printable(true),
......@@ -552,7 +553,25 @@ bool KoShape::hasTransparency()
if (! d->fill)
return true;
else
return d->fill->hasTransparency();
return d->fill->hasTransparency() || d->transparency > 0.0;
}
void KoShape::setTransparency(qreal transparency)
{
Q_D(KoShape);
d->transparency = qBound(0.0, transparency, 1.0);
}
qreal KoShape::transparency(bool recursive) const
{
Q_D(const KoShape);
if (!recursive || !parent()) {
return d->transparency;
} else {
const qreal parentOpacity = 1.0-parent()->transparency(recursive);
const qreal childOpacity = 1.0-d->transparency;
return 1.0-(parentOpacity*childOpacity);
}
}
KoInsets KoShape::borderInsets() const
......
......@@ -350,6 +350,18 @@ public:
*/
virtual bool hasTransparency();
/**
* Sets shape level transparency.
* @param transparency the new shape level transparency
*/
void setTransparency(qreal transparency);
/**
* Returns the shape level transparency.
* @param recursive when true takes the parents transparency into account
*/
qreal transparency(bool recursive=false) const;
/**
* Retrieve the z-coordinate of this shape.
* The zIndex property is used to determine which shape lies on top of other objects.
......
......@@ -299,6 +299,12 @@ void KoShapeManager::paint(QPainter &painter, const KoViewConverter &converter,
void KoShapeManager::paintShape(KoShape * shape, QPainter &painter, const KoViewConverter &converter, bool forPrint)
{
painter.save();
qreal transparency = shape->transparency(true);
if (transparency > 0.0) {
painter.setOpacity(1.0-transparency);
}
if (shape->shadow()) {
painter.save();
shape->shadow()->paint(shape, painter, converter);
......@@ -404,6 +410,7 @@ void KoShapeManager::paintShape(KoShape * shape, QPainter &painter, const KoView
shape->paintDecorations(painter, converter, d->canvas);
painter.restore();
}
painter.restore();
}
......
......@@ -56,7 +56,8 @@ public:
QMap<QByteArray, QString> additionalStyleAttributes;
QList<KoEventAction *> eventActions; ///< list of event actions the shape has
KoFilterEffectStack *filterEffectStack; ///< stack of filter effects applied to the shape
qreal transparency; ///< the shapes tranparency
static const int MaxZIndex = 32767;
int zIndex : 16; // keep maxZIndex in sync!
int visible : 1;
......
/* This file is part of the KDE project
* Copyright (C) 2009 Jan Hambrecht <jaham@gmx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KoShapeTransparencyCommand.h"
#include "KoShape.h"
#include "KoShapeBackground.h"
#include <klocale.h>
class KoShapeTransparencyCommand::Private
{
public:
Private() {
}
~Private() {
}
QList<KoShape*> shapes; ///< the shapes to set background for
QList<qreal> oldTransparencies; ///< the old transparencies
QList<qreal> newTransparencies; ///< the new transparencies
};
KoShapeTransparencyCommand::KoShapeTransparencyCommand(const QList<KoShape*> &shapes, qreal transparency, QUndoCommand *parent)
: QUndoCommand(parent)
, d(new Private())
{
d->shapes = shapes;
foreach(KoShape *shape, d->shapes) {
d->oldTransparencies.append(shape->transparency());
d->newTransparencies.append(transparency);
}
setText(i18n("Set opacity"));
}
KoShapeTransparencyCommand::KoShapeTransparencyCommand(KoShape * shape, qreal transparency, QUndoCommand *parent)
: QUndoCommand(parent)
, d(new Private())
{
d->shapes.append(shape);
d->oldTransparencies.append(shape->transparency());
d->newTransparencies.append(transparency);
setText(i18n("Set opacity"));
}
KoShapeTransparencyCommand::KoShapeTransparencyCommand(const QList<KoShape*> &shapes, const QList<qreal> &transparencies, QUndoCommand *parent)
: QUndoCommand(parent)
, d(new Private())
{
d->shapes = shapes;
foreach(KoShape *shape, d->shapes) {
d->oldTransparencies.append(shape->transparency());
}
d->newTransparencies = transparencies;
setText(i18n("Set opacity"));
}
KoShapeTransparencyCommand::~KoShapeTransparencyCommand()
{
delete d;
}
void KoShapeTransparencyCommand::redo()
{
QUndoCommand::redo();
QList<qreal>::iterator transparencyIt = d->newTransparencies.begin();
foreach(KoShape *shape, d->shapes) {
shape->setTransparency(*transparencyIt);
shape->update();
transparencyIt++;
}
}
void KoShapeTransparencyCommand::undo()
{
QUndoCommand::undo();
QList<qreal>::iterator transparencyIt = d->oldTransparencies.begin();
foreach(KoShape *shape, d->shapes) {
shape->setTransparency(*transparencyIt);
shape->update();
transparencyIt++;
}
}
/* This file is part of the KDE project
* Copyright (C) 2009 Jan Hambrecht <jaham@gmx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KOSHAPETRANSPARENCYCOMMAND_H
#define KOSHAPETRANSPARENCYCOMMAND_H
#include "flake_export.h"
#include <QUndoCommand>
#include <QList>
#include <QBrush>
class KoShape;
class KoShapeBackground;
/// The undo / redo command for setting the shape transparency
class FLAKE_EXPORT KoShapeTransparencyCommand : public QUndoCommand
{
public:
/**
* Command to set a new shape transparency.
* @param shapes a set of all the shapes that should get the new background.
* @param transparency the new shape transparency
* @param parent the parent command used for macro commands
*/
KoShapeTransparencyCommand(const QList<KoShape*> &shapes, qreal transparency, QUndoCommand *parent = 0);
/**
* Command to set a new shape transparency.
* @param shape a single shape that should get the new transparency.
* @param transparency the new shape transparency
* @param parent the parent command used for macro commands
*/
KoShapeTransparencyCommand(KoShape * shape, qreal transparency, QUndoCommand *parent = 0);
/**
* Command to set new shape transparencies.
* @param shapes a set of all the shapes that should get a new transparency.
* @param fills the new transparencies, one for each shape
* @param parent the parent command used for macro commands
*/
KoShapeTransparencyCommand(const QList<KoShape*> &shapes, const QList<qreal> &transparencies, QUndoCommand *parent = 0);
virtual ~KoShapeTransparencyCommand();
/// redo the command
void redo();
/// revert the actions done in redo
void undo();
private:
class Private;
Private * const d;
};
#endif // KOSHAPETRANSPARENCYCOMMAND_H
/* This file is part of the KDE project
* Copyright (C) 2007-2008 Jan Hambrecht <jaham@gmx.net>
* Copyright (C) 2007-2009 Jan Hambrecht <jaham@gmx.net>
* Copyright (C) 2008-2009 Thorsten Zachmann <zachmann@kde.org>
*
* This library is free software; you can redistribute it and/or
......@@ -35,6 +35,7 @@
#include <KoShapeBorderCommand.h>
#include <KoShapeBackgroundCommand.h>
#include <KoPathFillRuleCommand.h>
#include <KoShapeTransparencyCommand.h>
#include <KoPathShape.h>
#include <KoColorSpaceRegistry.h>
#include <KoColorBackground.h>
......@@ -47,10 +48,12 @@
#include <KoColorPopupAction.h>
#include <klocale.h>
#include <KNumInput>
#include <QtGui/QGridLayout>
#include <QtGui/QStackedWidget>
#include <QtGui/QToolButton>
#include <QtGui/QLabel>
const int MsecsThresholdForMergingCommands = 2000;
......@@ -81,6 +84,14 @@ StyleDocker::StyleDocker(QWidget * parent)
m_stack->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
m_layout->addWidget(m_stack, 1, 1);
m_layout->addWidget(new QLabel(i18n("Opacity:")), 2, 0);
m_opacity = new KDoubleNumInput(mainWidget);
m_opacity->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
m_opacity->setRange(0.0, 1.0, 0.05, true);
m_opacity->setValue(1.0);
m_layout->addWidget(m_opacity, 2, 1);
m_spacer = new QSpacerItem(0, 0, QSizePolicy::Fixed, QSizePolicy::Fixed);
m_layout->addItem(m_spacer, 2, 2);
......@@ -123,7 +134,8 @@ StyleDocker::StyleDocker(QWidget * parent)
this, SLOT(updatePattern(KoResource*)));
connect(this, SIGNAL(dockLocationChanged(Qt::DockWidgetArea)),
this, SLOT(locationChanged(Qt::DockWidgetArea)));
connect(m_opacity, SIGNAL(valueChanged(double)), this, SLOT(updateOpacity(double)));
setWidget(mainWidget);
}
......@@ -185,11 +197,17 @@ void StyleDocker::updateStyle()
if (! m_canvas)
return;
m_opacity->blockSignals(true);
KoShape * shape = m_canvas->shapeManager()->selection()->firstSelectedShape();
if (shape)
if (shape) {
updateStyle(shape->border(), shape->background());
else
m_opacity->setValue(1.0-shape->transparency());
}
else {
updateStyle(0, 0);
m_opacity->setValue(1.0);
}
m_opacity->blockSignals(false);
}
void StyleDocker::updateStyle(KoShapeBorderModel * stroke, KoShapeBackground * fill)
......@@ -506,6 +524,22 @@ void StyleDocker::updateFillRule(Qt::FillRule fillRule)
m_canvas->addCommand(new KoPathFillRuleCommand(pathsToChange, fillRule));
}
void StyleDocker::updateOpacity(double opacity)
{
if (! m_canvas)
return;
KoSelection *selection = m_canvas->shapeManager()->selection();
if (! selection || ! selection->count())
return;
QList<KoShape*> selectedShapes = selection->selectedShapes(KoFlake::TopLevelSelection);
if (!selectedShapes.count())
return;
m_canvas->addCommand(new KoShapeTransparencyCommand(selectedShapes, 1.0-opacity));
}
QList<KoPathShape*> StyleDocker::selectedPathShapes()
{
QList<KoPathShape*> pathShapes;
......
/* This file is part of the KDE project
* Copyright (C) 2007-2008 Jan Hambrecht <jaham@gmx.net>
* Copyright (C) 2007-2009 Jan Hambrecht <jaham@gmx.net>
* Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org>
*
* This library is free software; you can redistribute it and/or
......@@ -42,6 +42,7 @@ class KoColorPopupAction;
class KoPathShape;
class QSpacerItem;
class QGridLayout;
class KDoubleNumInput;
class StyleDocker : public QDockWidget, public KoCanvasObserver
{
......@@ -64,6 +65,7 @@ private slots:
void updateGradient(KoResource * item);
void updatePattern(KoResource * item);
void updateFillRule(Qt::FillRule fillRule);
void updateOpacity(double opacity);
/// Called when the docker changes area
void locationChanged(Qt::DockWidgetArea area);
......@@ -92,7 +94,8 @@ private:
KoColorPopupAction *m_actionColor;
QSpacerItem *m_spacer;
QGridLayout *m_layout;
KDoubleNumInput *m_opacity;
QTime m_lastColorChange;
KoShapeBackgroundCommand * m_lastFillCommand;
KoShapeBorderCommand * m_lastStrokeCommand;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment