2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2008 by Davide Bettio <davide.bettio@kdemail.net>
|
|
|
|
*
|
|
|
|
* This program 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, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "svgwidget.h"
|
|
|
|
|
2012-02-02 10:35:53 +01:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-05-21 20:53:15 +02:00
|
|
|
#include "kdebug.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
#include "svg.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
2010-08-24 22:40:43 +02:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
class SvgWidgetPrivate
|
|
|
|
{
|
2010-08-24 22:40:43 +02:00
|
|
|
public:
|
|
|
|
SvgWidgetPrivate(SvgWidget *widget, Svg *s, const QString &element)
|
|
|
|
: q(widget), svg(s), elementID(element)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void svgChanged()
|
|
|
|
{
|
|
|
|
q->update();
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-08-24 22:40:43 +02:00
|
|
|
SvgWidget *q;
|
|
|
|
Svg *svg;
|
|
|
|
QString elementID;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
SvgWidget::SvgWidget(QGraphicsItem *parent, Qt::WindowFlags wFlags)
|
|
|
|
: QGraphicsWidget(parent, wFlags),
|
2010-08-24 22:40:43 +02:00
|
|
|
d(new SvgWidgetPrivate(this, 0, QString()))
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SvgWidget::SvgWidget(Svg *svg, const QString &elementID, QGraphicsItem *parent, Qt::WindowFlags wFlags)
|
|
|
|
: QGraphicsWidget(parent, wFlags),
|
2010-08-24 22:40:43 +02:00
|
|
|
d(new SvgWidgetPrivate(this, svg, elementID))
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SvgWidget::~SvgWidget()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2010-05-21 20:53:15 +02:00
|
|
|
void SvgWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2010-05-21 20:53:15 +02:00
|
|
|
// we check for receivers so that SvgWidgets that aren't being used for events remain "click
|
|
|
|
// transparent"
|
2010-03-04 23:27:43 +01:00
|
|
|
if (receivers(SIGNAL(clicked(Qt::MouseButton)))) {
|
2008-11-04 00:08:39 +01:00
|
|
|
event->accept();
|
2010-08-10 23:44:55 +02:00
|
|
|
} else {
|
|
|
|
QGraphicsWidget::mousePressEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-21 20:53:15 +02:00
|
|
|
void SvgWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (boundingRect().contains(event->pos())) {
|
|
|
|
emit clicked(event->button());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void SvgWidget::setSvg(Svg *svg)
|
|
|
|
{
|
2010-08-24 22:40:43 +02:00
|
|
|
if (d->svg) {
|
|
|
|
disconnect(d->svg);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
d->svg = svg;
|
2010-08-24 22:40:43 +02:00
|
|
|
|
|
|
|
if (svg) {
|
|
|
|
connect(svg, SIGNAL(repaintNeeded()), this, SLOT(svgChanged()));
|
|
|
|
}
|
2009-04-20 21:07:40 +02:00
|
|
|
update();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Svg *SvgWidget::svg() const
|
|
|
|
{
|
|
|
|
return d->svg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SvgWidget::setElementID(const QString &elementID)
|
|
|
|
{
|
2010-03-06 19:44:07 +01:00
|
|
|
if (d->svg) {
|
|
|
|
d->svg->setContainsMultipleImages(!elementID.isNull());
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
d->elementID = elementID;
|
2009-04-20 21:07:40 +02:00
|
|
|
update();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SvgWidget::elementID() const
|
|
|
|
{
|
|
|
|
return d->elementID;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SvgWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(option);
|
|
|
|
Q_UNUSED(widget);
|
|
|
|
|
|
|
|
if (d->svg){
|
|
|
|
d->svg->paint(painter, boundingRect(), d->elementID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-01 20:36:01 +02:00
|
|
|
QSizeF SvgWidget::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
|
|
|
|
{
|
|
|
|
if (d->svg && which == Qt::PreferredSize) {
|
2009-08-01 20:48:09 +02:00
|
|
|
if (d->elementID.isNull()) {
|
|
|
|
return d->svg->size();
|
|
|
|
} else {
|
|
|
|
return d->svg->elementSize(d->elementID);
|
|
|
|
}
|
2009-08-01 20:36:01 +02:00
|
|
|
} else {
|
|
|
|
return QGraphicsWidget::sizeHint(which, constraint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|
2012-02-08 23:33:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
#include "moc_svgwidget.cpp"
|