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:
parent
8cd7f80dea
commit
2c264def9d
123
dialog.cpp
123
dialog.cpp
@ -34,7 +34,7 @@
|
|||||||
#include <KDebug>
|
#include <KDebug>
|
||||||
#include <NETRootInfo>
|
#include <NETRootInfo>
|
||||||
|
|
||||||
#include <plasma/svg.h>
|
#include <plasma/svgpanel.h>
|
||||||
|
|
||||||
#ifdef Q_WS_X11
|
#ifdef Q_WS_X11
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
@ -44,20 +44,31 @@
|
|||||||
namespace Plasma
|
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 )
|
Dialog::Dialog( QWidget * parent, Qt::WindowFlags f )
|
||||||
: QWidget(parent, 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 topHeight = d->background->marginSize(Plasma::TopMargin);
|
||||||
const int leftWidth = m_background->elementSize("left").width();
|
const int leftWidth = d->background->marginSize(Plasma::LeftMargin);
|
||||||
const int rightWidth = m_background->elementSize("right").width();
|
const int rightWidth = d->background->marginSize(Plasma::RightMargin);
|
||||||
const int bottomHeight = m_background->elementSize("bottom").height();
|
const int bottomHeight = d->background->marginSize(Plasma::BottomMargin);
|
||||||
setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
|
setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
|
||||||
|
|
||||||
connect(m_background, SIGNAL(repaintNeeded()), this, SLOT(update()));
|
connect(d->background, SIGNAL(repaintNeeded()), this, SLOT(update()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Dialog::~Dialog()
|
Dialog::~Dialog()
|
||||||
@ -71,102 +82,12 @@ void Dialog::paintEvent(QPaintEvent *e)
|
|||||||
p.setClipRect(e->rect());
|
p.setClipRect(e->rect());
|
||||||
p.setCompositionMode(QPainter::CompositionMode_Source );
|
p.setCompositionMode(QPainter::CompositionMode_Source );
|
||||||
p.fillRect(rect(), Qt::transparent);
|
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()) {
|
d->background->resize(e->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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dialog::position(QGraphicsSceneEvent *event, const QRectF boundingRect, QPointF scenePos)
|
void Dialog::position(QGraphicsSceneEvent *event, const QRectF boundingRect, QPointF scenePos)
|
||||||
|
22
dialog.h
22
dialog.h
@ -28,11 +28,8 @@
|
|||||||
|
|
||||||
#include <plasma/plasma_export.h>
|
#include <plasma/plasma_export.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace Plasma
|
namespace Plasma
|
||||||
{
|
{
|
||||||
class Svg;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @short A dialog that uses the Plasma style
|
* @short A dialog that uses the Plasma style
|
||||||
@ -75,23 +72,12 @@ class PLASMA_EXPORT Dialog : public QWidget
|
|||||||
/**
|
/**
|
||||||
* Reimplemented from QWidget
|
* Reimplemented from QWidget
|
||||||
*/
|
*/
|
||||||
void paintEvent( QPaintEvent *e );
|
void paintEvent(QPaintEvent *e);
|
||||||
|
void resizeEvent(QResizeEvent *e);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
class Private;
|
||||||
* Paints the plasma-themed background
|
Private * const d;
|
||||||
*/
|
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // Plasma namespace
|
} // Plasma namespace
|
||||||
|
Loading…
x
Reference in New Issue
Block a user