2008-02-06 18:16:45 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2008 by Aaron Seigo <aseigo@kde.org>
|
|
|
|
* Copyright 2008 Marco Martin <notmart@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 "svgpanel.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QSize>
|
|
|
|
|
|
|
|
#include <KDebug>
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2008-02-06 19:49:14 +01:00
|
|
|
class SvgPanel::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
2008-02-06 20:24:48 +01:00
|
|
|
: bFlags(DrawAllBorders|ContentAtOrigin),
|
2008-02-06 19:49:14 +01:00
|
|
|
cachedBackground(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Private()
|
|
|
|
{
|
|
|
|
delete cachedBackground;
|
|
|
|
}
|
|
|
|
|
|
|
|
BorderFlags bFlags;
|
|
|
|
QPixmap *cachedBackground;
|
|
|
|
Svg *background;
|
|
|
|
QSizeF panelSize;
|
|
|
|
|
|
|
|
//measures
|
|
|
|
int topHeight;
|
|
|
|
int leftWidth;
|
|
|
|
int rightWidth;
|
|
|
|
int bottomHeight;
|
|
|
|
|
|
|
|
//size of the svg where the size of the "center"
|
|
|
|
//element is contentWidth x contentHeight
|
|
|
|
bool noBorderPadding : 1;
|
|
|
|
bool stretchBorders : 1;
|
|
|
|
};
|
|
|
|
|
2008-02-06 18:16:45 +01:00
|
|
|
SvgPanel::SvgPanel(const QString& imagePath, QObject* parent)
|
|
|
|
: QObject(parent),
|
2008-02-06 19:49:14 +01:00
|
|
|
d(new Private)
|
2008-02-06 18:16:45 +01:00
|
|
|
{
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background = new Svg(imagePath, this);
|
|
|
|
connect(d->background, SIGNAL(repaintNeeded()), this, SLOT(updateSizes()));
|
2008-02-06 18:16:45 +01:00
|
|
|
|
|
|
|
updateSizes();
|
2008-02-06 19:49:14 +01:00
|
|
|
d->panelSize = d->background->size();
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SvgPanel::~SvgPanel()
|
|
|
|
{
|
2008-02-06 19:49:14 +01:00
|
|
|
delete d;
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SvgPanel::setFile(const QString& imagePath)
|
|
|
|
{
|
2008-02-06 19:49:14 +01:00
|
|
|
if (imagePath == d->background->file()) {
|
2008-02-06 18:16:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->setFile(imagePath);
|
2008-02-06 18:16:45 +01:00
|
|
|
updateSizes();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SvgPanel::file() const
|
|
|
|
{
|
2008-02-06 19:49:14 +01:00
|
|
|
return d->background->file();
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SvgPanel::setBorderFlags(const BorderFlags flags)
|
|
|
|
{
|
2008-02-06 19:49:14 +01:00
|
|
|
if (flags == d->bFlags) {
|
|
|
|
return;
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 19:49:14 +01:00
|
|
|
d->bFlags = flags;
|
2008-02-06 18:16:45 +01:00
|
|
|
updateSizes();
|
|
|
|
}
|
|
|
|
|
|
|
|
SvgPanel::BorderFlags SvgPanel::borderFlags() const
|
|
|
|
{
|
2008-02-06 19:49:14 +01:00
|
|
|
return d->bFlags;
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SvgPanel::resize(const QSizeF& size)
|
|
|
|
{
|
2008-02-06 19:49:14 +01:00
|
|
|
if (!size.isValid() || size.width() < 1 || size.height() < 1 || size == d->panelSize) {
|
2008-02-06 18:16:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-06 19:49:14 +01:00
|
|
|
d->panelSize = size;
|
2008-02-06 18:16:45 +01:00
|
|
|
updateSizes();
|
|
|
|
}
|
|
|
|
|
2008-02-06 20:12:21 +01:00
|
|
|
qreal SvgPanel::marginSize(const Plasma::MarginEdge edge) const
|
2008-02-06 18:16:45 +01:00
|
|
|
{
|
2008-02-06 19:49:14 +01:00
|
|
|
if (d->noBorderPadding) {
|
2008-02-06 18:16:45 +01:00
|
|
|
return .0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (edge) {
|
2008-02-06 20:12:21 +01:00
|
|
|
case Plasma::TopMargin:
|
2008-02-06 19:49:14 +01:00
|
|
|
return d->topHeight;
|
2008-02-06 18:16:45 +01:00
|
|
|
break;
|
|
|
|
|
2008-02-06 20:12:21 +01:00
|
|
|
case Plasma::LeftMargin:
|
2008-02-06 19:49:14 +01:00
|
|
|
return d->leftWidth;
|
2008-02-06 18:16:45 +01:00
|
|
|
break;
|
|
|
|
|
2008-02-06 20:12:21 +01:00
|
|
|
case Plasma::RightMargin:
|
2008-02-06 19:49:14 +01:00
|
|
|
return d->rightWidth;
|
2008-02-06 18:16:45 +01:00
|
|
|
break;
|
|
|
|
|
2008-02-06 20:12:21 +01:00
|
|
|
//Plasma::BottomMargin
|
2008-02-06 18:16:45 +01:00
|
|
|
default:
|
2008-02-06 19:49:14 +01:00
|
|
|
return d->bottomHeight;
|
2008-02-06 18:16:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SvgPanel::paint(QPainter* painter, const QRectF& rect)
|
|
|
|
{
|
2008-02-06 21:53:14 +01:00
|
|
|
bool origined = d->bFlags & ContentAtOrigin;
|
2008-02-06 19:49:14 +01:00
|
|
|
const int topWidth = d->background->elementSize("top").width();
|
|
|
|
const int leftHeight = d->background->elementSize("left").height();
|
2008-02-06 21:53:14 +01:00
|
|
|
const int topOffset = origined ? 0 - d->topHeight : 0;
|
|
|
|
const int leftOffset = origined ? 0 - d->leftWidth : 0;
|
2008-02-06 18:16:45 +01:00
|
|
|
|
2008-02-06 19:49:14 +01:00
|
|
|
if (!d->cachedBackground) {
|
|
|
|
const int contentWidth = d->panelSize.width() - d->leftWidth - d->rightWidth;
|
|
|
|
const int contentHeight = d->panelSize.height() - d->topHeight - d->bottomHeight;
|
2008-02-06 21:53:14 +01:00
|
|
|
int contentTop = 0;
|
|
|
|
int contentLeft = 0;
|
|
|
|
int rightOffset = contentWidth;
|
|
|
|
int bottomOffset = contentHeight;
|
2008-02-06 18:16:45 +01:00
|
|
|
|
2008-02-06 19:49:14 +01:00
|
|
|
delete d->cachedBackground;
|
|
|
|
d->cachedBackground = new QPixmap(d->leftWidth + contentWidth + d->rightWidth,
|
|
|
|
d->topHeight + contentHeight + d->bottomHeight);
|
|
|
|
d->cachedBackground->fill(Qt::transparent);
|
|
|
|
QPainter p(d->cachedBackground);
|
2008-02-06 21:53:14 +01:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
p.setRenderHint(QPainter::SmoothPixmapTransform);
|
2008-02-06 20:46:36 +01:00
|
|
|
|
2008-02-06 21:53:14 +01:00
|
|
|
if (origined) {
|
2008-02-06 20:46:36 +01:00
|
|
|
p.translate(d->leftWidth, d->topHeight);
|
|
|
|
}
|
|
|
|
|
2008-02-06 18:16:45 +01:00
|
|
|
//FIXME: This is a hack to fix a drawing problems with svg files where a thin transparent border is drawn around the svg image.
|
|
|
|
// the transparent border around the svg seems to vary in size depending on the size of the svg and as a result increasing the
|
|
|
|
// svg image by 2 all around didn't resolve the issue. For now it resizes based on the border size.
|
|
|
|
|
|
|
|
|
|
|
|
//CENTER
|
|
|
|
if (contentHeight > 0 && contentWidth > 0) {
|
2008-02-06 20:46:36 +01:00
|
|
|
QSizeF scaledSize = QSizeF(d->panelSize.width() -
|
|
|
|
(d->leftWidth + d->rightWidth) +
|
|
|
|
d->panelSize.width()*(((qreal)(d->leftWidth + d->rightWidth)) / d->panelSize.width()),
|
|
|
|
d->panelSize.height() -
|
|
|
|
(d->topHeight + d->bottomHeight) +
|
|
|
|
d->panelSize.height()*(((qreal)(d->topHeight + d->bottomHeight)) / d->panelSize.height()));
|
|
|
|
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->resize(scaledSize.width(), scaledSize.height());
|
|
|
|
d->background->paint(&p, QRect(contentLeft - d->leftWidth, contentTop - d->topHeight,
|
|
|
|
contentWidth + d->leftWidth*2, contentHeight + d->topHeight*2),
|
2008-02-06 18:16:45 +01:00
|
|
|
"center");
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->resize();
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 21:53:14 +01:00
|
|
|
// Corners
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawTopBorder) {
|
2008-02-06 21:53:14 +01:00
|
|
|
if (!origined) {
|
|
|
|
contentTop = d->topHeight;
|
|
|
|
bottomOffset += d->topHeight;
|
|
|
|
}
|
|
|
|
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawLeftBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->paint(&p, QRect(leftOffset, topOffset, d->leftWidth, d->topHeight), "topleft");
|
2008-02-06 21:53:14 +01:00
|
|
|
|
|
|
|
if (!origined) {
|
|
|
|
contentLeft = d->leftWidth;
|
|
|
|
rightOffset = contentWidth + d->leftWidth;
|
|
|
|
}
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawRightBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->paint(&p, QRect(rightOffset, topOffset, d->rightWidth, d->topHeight), "topright");
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawBottomBorder) {
|
|
|
|
if (d->bFlags & DrawLeftBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->paint(&p, QRect(leftOffset, bottomOffset, d->leftWidth, d->bottomHeight), "bottomleft");
|
2008-02-06 21:53:14 +01:00
|
|
|
|
|
|
|
if (!origined) {
|
|
|
|
contentLeft = d->leftWidth;
|
|
|
|
rightOffset = contentWidth + d->leftWidth;
|
|
|
|
}
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawRightBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->paint(&p, QRect(rightOffset, bottomOffset, d->rightWidth, d->bottomHeight), "bottomright");
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-06 21:53:14 +01:00
|
|
|
// Sides
|
2008-02-06 19:49:14 +01:00
|
|
|
if (d->stretchBorders) {
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawLeftBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->paint(&p, QRect(leftOffset, contentTop, d->leftWidth, contentHeight), "left");
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawRightBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->paint(&p, QRect(rightOffset, contentTop, d->rightWidth, contentHeight), "right");
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawTopBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->paint(&p, QRect(contentLeft, topOffset, contentWidth, d->topHeight), "top");
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawBottomBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->paint(&p, QRect(contentLeft, bottomOffset, contentWidth, d->bottomHeight), "bottom");
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
} else {
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawLeftBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
QPixmap left(d->leftWidth, leftHeight);
|
2008-02-06 18:16:45 +01:00
|
|
|
left.fill(Qt::transparent);
|
|
|
|
|
|
|
|
{
|
|
|
|
QPainter sidePainter(&left);
|
|
|
|
sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->paint(&sidePainter, QPoint(0, 0), "left");
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 19:49:14 +01:00
|
|
|
p.drawTiledPixmap(QRect(leftOffset, contentTop, d->leftWidth, contentHeight), left);
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawRightBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
QPixmap right(d->rightWidth, leftHeight);
|
2008-02-06 18:16:45 +01:00
|
|
|
right.fill(Qt::transparent);
|
|
|
|
|
|
|
|
{
|
|
|
|
QPainter sidePainter(&right);
|
|
|
|
sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->paint(&sidePainter, QPoint(0, 0), "right");
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 19:49:14 +01:00
|
|
|
p.drawTiledPixmap(QRect(rightOffset, contentTop, d->rightWidth, contentHeight), right);
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawTopBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
QPixmap top(topWidth, d->topHeight);
|
2008-02-06 18:16:45 +01:00
|
|
|
top.fill(Qt::transparent);
|
|
|
|
|
|
|
|
{
|
|
|
|
QPainter sidePainter(&top);
|
|
|
|
sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->paint(&sidePainter, QPoint(0, 0), "top");
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 19:49:14 +01:00
|
|
|
p.drawTiledPixmap(QRect(contentLeft, topOffset, contentWidth, d->topHeight), top);
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawBottomBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
QPixmap bottom(topWidth, d->bottomHeight);
|
2008-02-06 18:16:45 +01:00
|
|
|
bottom.fill(Qt::transparent);
|
|
|
|
|
|
|
|
{
|
|
|
|
QPainter sidePainter(&bottom);
|
|
|
|
sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->paint(&sidePainter, QPoint(0, 0), "bottom");
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 19:49:14 +01:00
|
|
|
p.drawTiledPixmap(QRect(contentLeft, bottomOffset, contentWidth, d->bottomHeight), bottom);
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-enable this once Qt's svg rendering is un-buggered
|
|
|
|
//resize(contentWidth, contentHeight);
|
|
|
|
//paint(&p, QRect(contentLeft, contentTop, contentWidth, contentHeight), "center");
|
|
|
|
}
|
|
|
|
|
|
|
|
//p2->drawPixmap(paintRect, *cachedBackground, paintRect.translated(-leftOffset,-topOffset));
|
2008-02-06 19:49:14 +01:00
|
|
|
painter->drawPixmap(rect, *d->cachedBackground, rect.translated(-leftOffset, -topOffset));
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SvgPanel::updateSizes()
|
|
|
|
{
|
2008-02-07 05:33:02 +01:00
|
|
|
delete d->cachedBackground;
|
|
|
|
d->cachedBackground = 0;
|
2008-02-06 19:49:14 +01:00
|
|
|
d->background->resize();
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawTopBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->topHeight = d->background->elementSize("top").height();
|
2008-02-06 18:16:45 +01:00
|
|
|
} else {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->topHeight = 0;
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawLeftBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->leftWidth = d->background->elementSize("left").width();
|
2008-02-06 18:16:45 +01:00
|
|
|
} else {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->leftWidth = 0;
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawRightBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->rightWidth = d->background->elementSize("right").width();
|
2008-02-06 18:16:45 +01:00
|
|
|
} else {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->rightWidth = 0;
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 20:24:48 +01:00
|
|
|
if (d->bFlags & DrawBottomBorder) {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->bottomHeight = d->background->elementSize("bottom").height();
|
2008-02-06 18:16:45 +01:00
|
|
|
} else {
|
2008-02-06 19:49:14 +01:00
|
|
|
d->bottomHeight = 0;
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//since it's rectangular, topWidth and bottomWidth must be the same
|
2008-02-06 19:49:14 +01:00
|
|
|
d->noBorderPadding = d->background->elementExists("hint-no-border-padding");
|
|
|
|
d->stretchBorders = d->background->elementExists("hint-stretch-borders");
|
2008-02-06 21:53:14 +01:00
|
|
|
emit repaintNeeded();
|
2008-02-06 18:16:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "svgpanel.moc"
|