use SvgPanel. nice way to get rid of 80+ lines of duplicate code

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=771744
This commit is contained in:
Aaron J. Seigo 2008-02-06 21:07:16 +00:00
parent 8cd7f80dea
commit 2c264def9d
2 changed files with 26 additions and 119 deletions

View File

@ -34,7 +34,7 @@
#include <KDebug>
#include <NETRootInfo>
#include <plasma/svg.h>
#include <plasma/svgpanel.h>
#ifdef Q_WS_X11
#include <X11/Xlib.h>
@ -44,20 +44,31 @@
namespace Plasma
{
class Dialog::Private
{
public:
/**
* Holds the background SVG, to be re-rendered when the cache is invalidated,
* for example by resizing the dialogue.
*/
Plasma::SvgPanel *background;
};
Dialog::Dialog( QWidget * parent, Qt::WindowFlags f )
: QWidget(parent, f),
m_cachedBackground(0)
d(new Private)
{
m_background = new Plasma::Svg("dialogs/background", this);
d->background = new SvgPanel("dialogs/background", this);
d->background->setBorderFlags(SvgPanel::DrawAllBorders);
d->background->resize(size());
const int topHeight = m_background->elementSize("top").height();
const int leftWidth = m_background->elementSize("left").width();
const int rightWidth = m_background->elementSize("right").width();
const int bottomHeight = m_background->elementSize("bottom").height();
const int topHeight = d->background->marginSize(Plasma::TopMargin);
const int leftWidth = d->background->marginSize(Plasma::LeftMargin);
const int rightWidth = d->background->marginSize(Plasma::RightMargin);
const int bottomHeight = d->background->marginSize(Plasma::BottomMargin);
setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
connect(m_background, SIGNAL(repaintNeeded()), this, SLOT(update()));
connect(d->background, SIGNAL(repaintNeeded()), this, SLOT(update()));
}
Dialog::~Dialog()
@ -71,102 +82,12 @@ void Dialog::paintEvent(QPaintEvent *e)
p.setClipRect(e->rect());
p.setCompositionMode(QPainter::CompositionMode_Source );
p.fillRect(rect(), Qt::transparent);
paintBackground(&p, e->rect());
d->background->paint(&p, e->rect());
}
void Dialog::paintBackground(QPainter* painter, const QRect &exposedRect)
void Dialog::resizeEvent(QResizeEvent *e)
{
if (!m_cachedBackground || m_cachedBackground->size() != rect().size()) {
const int contentWidth = rect().width();
const int contentHeight = rect().height();
m_background->resize();
const int topHeight = m_background->elementSize("top").height();
const int topWidth = m_background->elementSize("top").width();
const int leftWidth = m_background->elementSize("left").width();
const int leftHeight = m_background->elementSize("left").height();
const int rightHeight = m_background->elementSize("right").height();
const int rightWidth = m_background->elementSize("right").width();
const int bottomHeight = m_background->elementSize("bottom").height();
const int bottomWidth = m_background->elementSize("bottom").width();
const int topOffset = 0;
const int leftOffset = 0;
const int rightOffset = contentWidth - rightWidth;
const int bottomOffset = contentHeight - bottomHeight;
const int contentTop = topHeight;
const int contentLeft = leftWidth;
delete m_cachedBackground;
m_cachedBackground = new QPixmap(contentWidth, contentHeight);
m_cachedBackground->fill(Qt::transparent);
QPainter p(m_cachedBackground);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.setRenderHint(QPainter::SmoothPixmapTransform);
//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
// svn image by 2 all around didn't resolve the issue. For now it resizes based on the border size.
m_background->resize(contentWidth, contentHeight);
m_background->paint(&p, QRect(leftOffset, topOffset, contentWidth, contentHeight), "center");
m_background->resize();
m_background->paint(&p, QRect(leftOffset, topOffset, leftWidth, topHeight), "topleft");
m_background->paint(&p, QRect(rightOffset, topOffset, rightWidth, topHeight), "topright");
m_background->paint(&p, QRect(leftOffset, bottomOffset, leftWidth, bottomHeight), "bottomleft");
m_background->paint(&p, QRect(rightOffset, bottomOffset, rightWidth, bottomHeight), "bottomright");
if (m_background->elementExists("hint-stretch-borders")) {
m_background->paint(&p, QRect(leftOffset, contentTop, leftWidth, contentHeight), "left");
m_background->paint(&p, QRect(rightOffset, contentTop, rightWidth, contentHeight), "right");
m_background->paint(&p, QRect(contentLeft, topOffset, contentWidth, topHeight), "top");
m_background->paint(&p, QRect(contentLeft, bottomOffset, contentWidth, bottomHeight), "bottom");
} else {
QPixmap left(leftWidth, leftHeight);
left.fill(Qt::transparent);
{
QPainter sidePainter(&left);
sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
m_background->paint(&sidePainter, QPoint(0, 0), "left");
}
p.drawTiledPixmap(QRect(leftOffset, contentTop, leftWidth, contentHeight - topHeight - bottomHeight), left);
QPixmap right(rightWidth, rightHeight);
right.fill(Qt::transparent);
{
QPainter sidePainter(&right);
sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
m_background->paint(&sidePainter, QPoint(0, 0), "right");
}
p.drawTiledPixmap(QRect(rightOffset, contentTop, rightWidth, contentHeight - topHeight - bottomHeight), right);
QPixmap top(topWidth, topHeight);
top.fill(Qt::transparent);
{
QPainter sidePainter(&top);
sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
m_background->paint(&sidePainter, QPoint(0, 0), "top");
}
p.drawTiledPixmap(QRect(contentLeft, topOffset, contentWidth - rightWidth - leftWidth, topHeight), top);
QPixmap bottom(bottomWidth, bottomHeight);
bottom.fill(Qt::transparent);
{
QPainter sidePainter(&bottom);
sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
m_background->paint(&sidePainter, QPoint(0, 0), "bottom");
}
p.drawTiledPixmap(QRect(contentLeft, bottomOffset, contentWidth - rightWidth - leftWidth, bottomHeight), bottom);
}
// re-enable this once Qt's svg rendering is un-buggered
//background->resize(contentWidth, contentHeight);
//background->paint(&p, QRect(contentLeft, contentTop, contentWidth, contentHeight), "center");
}
painter->drawPixmap(exposedRect, *m_cachedBackground, exposedRect);
d->background->resize(e->size());
}
void Dialog::position(QGraphicsSceneEvent *event, const QRectF boundingRect, QPointF scenePos)

View File

@ -28,11 +28,8 @@
#include <plasma/plasma_export.h>
namespace Plasma
{
class Svg;
/**
* @short A dialog that uses the Plasma style
@ -75,23 +72,12 @@ class PLASMA_EXPORT Dialog : public QWidget
/**
* Reimplemented from QWidget
*/
void paintEvent( QPaintEvent *e );
void paintEvent(QPaintEvent *e);
void resizeEvent(QResizeEvent *e);
private:
/**
* Paints the plasma-themed background
*/
void paintBackground(QPainter* painter, const QRect &exposedRect);
/**
* Holds the background SVG, to be re-rendered when the cache is invalidated,
* for example by resizing the dialogue.
*/
Plasma::Svg *m_background;
/**
* Holds a pixmap of the rendered SVG background so we don't need to re-render
* it when not necessary.
*/
QPixmap *m_cachedBackground;
class Private;
Private * const d;
};
} // Plasma namespace