Api changes to PanelSvg finished:

-now it's a Plasma::Svg
-DrawTopBorder DrawLeftBorder etc -> TopBorder LeftBorder etc
-contentAtOrigin -> out of flags
-setFile() -> setImagePath()
-setBorderFlags() -> setEnabledBorders()
-added resize(qreal,qreal)
-setPrefix() -> setElementsPrefix()
-updateSizes() -> Q_PRIVATE_SLOT
-pos() setPos() removed, now pos is a parameter of paint
-setLocation() -> setElementPrefix(enum)
-location() removed


svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=797736
This commit is contained in:
Marco Martin 2008-04-16 20:17:36 +00:00
parent 3e5f70f8bc
commit 6024f48475
4 changed files with 118 additions and 139 deletions

View File

@ -726,7 +726,7 @@ void Applet::setDrawStandardBackground(bool drawBackground)
if (drawBackground) {
if (!d->background) {
d->background = new Plasma::PanelSvg("widgets/background");
d->background->setBorderFlags(Plasma::PanelSvg::DrawAllBorders);
d->background->setEnabledBorders(Plasma::PanelSvg::AllBorders);
int left, top, right, bottom;
d->getBorderSize(left, top, right, bottom);
QSizeF fitSize(left + right, top + bottom);
@ -1006,7 +1006,7 @@ void Applet::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QW
formFactor() != Plasma::Vertical &&
formFactor() != Plasma::Horizontal) {
//kDebug() << "option rect is" << option->rect;
d->background->paint(painter,option->rect);
d->background->paint(painter, option->rect, QPointF(0,0));
}
if (!d->failed) {
if (widget && isContainment()) {

View File

@ -61,7 +61,7 @@ Dialog::Dialog( QWidget * parent, Qt::WindowFlags f )
d(new Private)
{
d->background = new PanelSvg("dialogs/background", this);
d->background->setBorderFlags(PanelSvg::DrawAllBorders);
d->background->setEnabledBorders(PanelSvg::AllBorders);
d->background->resize(size());
connect(d->background, SIGNAL(repaintNeeded()), this, SLOT(update()));

View File

@ -32,9 +32,11 @@ namespace Plasma
class PanelSvg::Private
{
public:
Private()
: bFlags(DrawAllBorders|ContentAtOrigin),
cachedBackground(0), pos(0,0)
Private(PanelSvg *psvg)
: q(psvg),
enabledBorders(AllBorders),
cachedBackground(0),
contentAtOrigin(false)
{
}
@ -44,12 +46,14 @@ public:
}
void generateBackground();
void updateSizes();
BorderFlags bFlags;
PanelSvg *q;
EnabledBorders enabledBorders;
QPixmap *cachedBackground;
Svg *background;
QSizeF panelSize;
QPointF pos;
Location location;
QString prefix;
@ -65,16 +69,17 @@ public:
bool noBorderPadding : 1;
bool stretchBorders : 1;
bool tileCenter : 1;
bool contentAtOrigin : 1;
};
PanelSvg::PanelSvg(const QString& imagePath, QObject* parent)
: QObject(parent),
d(new Private)
: Svg(imagePath, parent),
d(new Private(this))
{
d->background = new Svg(imagePath, this);
connect(d->background, SIGNAL(repaintNeeded()), this, SLOT(updateSizes()));
updateSizes();
d->updateSizes();
d->panelSize = d->background->size();
}
@ -83,74 +88,59 @@ PanelSvg::~PanelSvg()
delete d;
}
void PanelSvg::setFile(const QString& imagePath)
void PanelSvg::setImagePath(const QString& imagePath)
{
if (imagePath == d->background->file()) {
return;
}
d->background->setFile(imagePath);
setPrefix(prefix());
setElementPrefix(prefix());
}
QString PanelSvg::file() const
QString PanelSvg::imagePath() const
{
return d->background->file();
}
void PanelSvg::setBorderFlags(const BorderFlags flags)
void PanelSvg::setEnabledBorders(const EnabledBorders borders)
{
if (flags == d->bFlags) {
if (borders == d->enabledBorders) {
return;
}
d->bFlags = flags;
updateSizes();
d->enabledBorders = borders;
d->updateSizes();
}
PanelSvg::BorderFlags PanelSvg::borderFlags() const
PanelSvg::EnabledBorders PanelSvg::enabledBorders() const
{
return d->bFlags;
return d->enabledBorders;
}
void PanelSvg::setPos(const QPointF& pos)
{
d->pos = pos;
}
QPointF PanelSvg::pos() const
{
return d->pos;
}
void PanelSvg::setLocation(Plasma::Location location)
void PanelSvg::setElementPrefix(Plasma::Location location)
{
switch (location) {
case TopEdge:
setPrefix("north");
setElementPrefix("north");
break;
case BottomEdge:
setPrefix("south");
setElementPrefix("south");
break;
case LeftEdge:
setPrefix("west");
setElementPrefix("west");
break;
case RightEdge:
setPrefix("east");
setElementPrefix("east");
break;
default:
setPrefix(QString());
setElementPrefix(QString());
break;
}
d->location = location;
}
Plasma::Location PanelSvg::location() const
{
return d->location;
}
void PanelSvg::setPrefix(const QString & prefix)
void PanelSvg::setElementPrefix(const QString & prefix)
{
if (!d->background->hasElement(prefix + "-center")) {
d->prefix.clear();
@ -164,7 +154,7 @@ void PanelSvg::setPrefix(const QString & prefix)
d->location = Floating;
if (d->cachedBackground) {
updateSizes();
d->updateSizes();
}
}
@ -183,7 +173,12 @@ void PanelSvg::resize(const QSizeF& size)
}
d->panelSize = size;
updateSizes();
d->updateSizes();
}
void PanelSvg::resize(qreal width, qreal height)
{
resize(QSize(width, height));
}
qreal PanelSvg::marginSize(const Plasma::MarginEdge edge) const
@ -221,9 +216,23 @@ QBitmap PanelSvg::mask() const
return d->cachedBackground->alphaChannel().createMaskFromColor(Qt::black);
}
void PanelSvg::paint(QPainter* painter, const QRectF& rect, const QPointF& pos)
{
if (!d->cachedBackground) {
d->generateBackground();
}
//FIXME: this is redundant with generatebackground for now
bool origined = d->contentAtOrigin;
const int topOffset = origined ? 0 - d->topHeight : 0;
const int leftOffset = origined ? 0 - d->leftWidth : 0;
painter->drawPixmap(rect, *d->cachedBackground, rect.translated(-pos.x()-leftOffset,-pos.y()-topOffset));
}
void PanelSvg::Private::generateBackground()
{
bool origined = bFlags & ContentAtOrigin;
bool origined = contentAtOrigin;
const int topWidth = background->elementSize(prefix + "top").width();
const int leftHeight = background->elementSize(prefix + "left").height();
const int topOffset = origined ? 0 - topHeight : 0;
@ -292,13 +301,13 @@ void PanelSvg::Private::generateBackground()
// Corners
if (bFlags & DrawTopBorder) {
if (enabledBorders & TopBorder) {
if (!origined) {
contentTop = topHeight;
bottomOffset += topHeight;
}
if (bFlags & DrawLeftBorder) {
if (enabledBorders & LeftBorder) {
background->paint(&p, QRect(leftOffset, topOffset, leftWidth, topHeight), prefix + "topleft");
if (!origined) {
@ -307,13 +316,13 @@ void PanelSvg::Private::generateBackground()
}
}
if (bFlags & DrawRightBorder) {
if (enabledBorders & RightBorder) {
background->paint(&p, QRect(rightOffset, topOffset, rightWidth, topHeight), prefix + "topright");
}
}
if (bFlags & DrawBottomBorder) {
if (bFlags & DrawLeftBorder) {
if (enabledBorders & BottomBorder) {
if (enabledBorders & LeftBorder) {
background->paint(&p, QRect(leftOffset, bottomOffset, leftWidth, bottomHeight), prefix + "bottomleft");
if (!origined) {
@ -322,30 +331,30 @@ void PanelSvg::Private::generateBackground()
}
}
if (bFlags & DrawRightBorder) {
if (enabledBorders & RightBorder) {
background->paint(&p, QRect(rightOffset, bottomOffset, rightWidth, bottomHeight), prefix + "bottomright");
}
}
// Sides
if (stretchBorders) {
if (bFlags & DrawLeftBorder) {
if (enabledBorders & LeftBorder) {
background->paint(&p, QRect(leftOffset, contentTop, leftWidth, contentHeight), prefix + "left");
}
if (bFlags & DrawRightBorder) {
if (enabledBorders & RightBorder) {
background->paint(&p, QRect(rightOffset, contentTop, rightWidth, contentHeight), prefix + "right");
}
if (bFlags & DrawTopBorder) {
if (enabledBorders & TopBorder) {
background->paint(&p, QRect(contentLeft, topOffset, contentWidth, topHeight), prefix + "top");
}
if (bFlags & DrawBottomBorder) {
if (enabledBorders & BottomBorder) {
background->paint(&p, QRect(contentLeft, bottomOffset, contentWidth, bottomHeight), prefix + "bottom");
}
} else {
if (bFlags & DrawLeftBorder) {
if (enabledBorders & LeftBorder) {
QPixmap left(leftWidth, leftHeight);
left.fill(Qt::transparent);
@ -358,7 +367,7 @@ void PanelSvg::Private::generateBackground()
p.drawTiledPixmap(QRect(leftOffset, contentTop, leftWidth, contentHeight), left);
}
if (bFlags & DrawRightBorder) {
if (enabledBorders & RightBorder) {
QPixmap right(rightWidth, leftHeight);
right.fill(Qt::transparent);
@ -371,7 +380,7 @@ void PanelSvg::Private::generateBackground()
p.drawTiledPixmap(QRect(rightOffset, contentTop, rightWidth, contentHeight), right);
}
if (bFlags & DrawTopBorder) {
if (enabledBorders & TopBorder) {
QPixmap top(topWidth, topHeight);
top.fill(Qt::transparent);
@ -384,7 +393,7 @@ void PanelSvg::Private::generateBackground()
p.drawTiledPixmap(QRect(contentLeft, topOffset, contentWidth, topHeight), top);
}
if (bFlags & DrawBottomBorder) {
if (enabledBorders & BottomBorder) {
QPixmap bottom(topWidth, bottomHeight);
bottom.fill(Qt::transparent);
@ -404,57 +413,41 @@ void PanelSvg::Private::generateBackground()
}
}
void PanelSvg::paint(QPainter* painter, const QRectF& rect)
void PanelSvg::Private::updateSizes()
{
if (!d->cachedBackground) {
d->generateBackground();
}
delete cachedBackground;
cachedBackground = 0;
//FIXME: this is redundant with generatebackground for now
bool origined = d->bFlags & ContentAtOrigin;
const int topWidth = d->background->elementSize(d->prefix + "top").width();
const int leftHeight = d->background->elementSize(d->prefix + "left").height();
const int topOffset = origined ? 0 - d->topHeight : 0;
const int leftOffset = origined ? 0 - d->leftWidth : 0;
painter->drawPixmap(rect, *d->cachedBackground, rect.translated(-d->pos.x()-leftOffset,-d->pos.y()-topOffset));
}
void PanelSvg::updateSizes()
{
delete d->cachedBackground;
d->cachedBackground = 0;
d->background->resize();
if (d->bFlags & DrawTopBorder) {
d->topHeight = d->background->elementSize(d->prefix + "top").height();
background->resize();
if (enabledBorders & TopBorder) {
topHeight = background->elementSize(prefix + "top").height();
} else {
d->topHeight = 0;
topHeight = 0;
}
if (d->bFlags & DrawLeftBorder) {
d->leftWidth = d->background->elementSize(d->prefix + "left").width();
if (enabledBorders & LeftBorder) {
leftWidth = background->elementSize(prefix + "left").width();
} else {
d->leftWidth = 0;
leftWidth = 0;
}
if (d->bFlags & DrawRightBorder) {
d->rightWidth = d->background->elementSize(d->prefix + "right").width();
if (enabledBorders & RightBorder) {
rightWidth = background->elementSize(prefix + "right").width();
} else {
d->rightWidth = 0;
rightWidth = 0;
}
if (d->bFlags & DrawBottomBorder) {
d->bottomHeight = d->background->elementSize(d->prefix + "bottom").height();
if (enabledBorders & BottomBorder) {
bottomHeight = background->elementSize(prefix + "bottom").height();
} else {
d->bottomHeight = 0;
bottomHeight = 0;
}
//since it's rectangular, topWidth and bottomWidth must be the same
d->tileCenter = d->background->hasElement("hint-tile-center");
d->noBorderPadding = d->background->hasElement("hint-no-border-padding");
d->stretchBorders = d->background->hasElement("hint-stretch-borders");
emit repaintNeeded();
tileCenter = background->hasElement("hint-tile-center");
noBorderPadding = background->hasElement("hint-no-border-padding");
stretchBorders = background->hasElement("hint-stretch-borders");
emit q->repaintNeeded();
}
} // Plasma namespace

View File

@ -41,22 +41,21 @@ class QMatrix;
namespace Plasma
{
class PLASMA_EXPORT PanelSvg : public QObject
class PLASMA_EXPORT PanelSvg : public Svg
{
Q_OBJECT
public:
/**
* These flags represents what borders should be drawn
*/
enum BorderFlag { DrawTopBorder = 1,
DrawBottomBorder = 2,
DrawLeftBorder = 4,
DrawRightBorder = 8,
ContentAtOrigin = 16,
DrawAllBorders = DrawTopBorder | DrawBottomBorder |
DrawLeftBorder | DrawRightBorder
enum EnabledBorder { TopBorder = 1,
BottomBorder = 2,
LeftBorder = 4,
RightBorder = 8,
AllBorders = TopBorder | BottomBorder |
LeftBorder | RightBorder
};
Q_DECLARE_FLAGS(BorderFlags, BorderFlag)
Q_DECLARE_FLAGS(EnabledBorders, EnabledBorder)
/**
* Constructs a new PanelSvg that paints the proper named subelements
@ -70,32 +69,32 @@ class PLASMA_EXPORT PanelSvg : public QObject
*
* @related Plasma::Theme
*/
explicit PanelSvg(const QString& imagePath = QString(), QObject* parent = 0);
explicit PanelSvg(const QString& imagePath, QObject* parent = 0);
~PanelSvg();
/**
* Loads a new Svg
* @arg imagePath the new file
*/
void setFile(const QString& imagePath);
void setImagePath(const QString& imagePath);
/**
* Convenience method to get the svg filepath and name of svg.
* @return the svg's filepath including name of the svg.
*/
QString file() const;
QString imagePath() const;
/**
* Sets what borders should be painted
* @arg flags borders we want to paint
*/
void setBorderFlags(const BorderFlags flags);
void setEnabledBorders(const EnabledBorders borders);
/**
* Convenience method to get the border flags
* Convenience method to get the enabled borders
* @return what borders are painted
*/
BorderFlags borderFlags() const;
EnabledBorders enabledBorders() const;
/**
* Resize the panel maintaining the same border size
@ -103,6 +102,14 @@ class PLASMA_EXPORT PanelSvg : public QObject
*/
void resize(const QSizeF& size);
/**
* Resize the panel maintaining the same border size
* This is an overloaded function provided for convenience
* @arg width the new width
* @arg height the new height
**/
void resize(qreal width, qreal height);
/**
* Returns the margin size given the margin edge we want
* @arg edge the margin edge we want, top, bottom, left or right
@ -111,31 +118,12 @@ class PLASMA_EXPORT PanelSvg : public QObject
qreal marginSize(const Plasma::MarginEdge edge) const;
/**
* Sets the position of the PanelSvg
* @arg pos where it should be positioned at
*/
void setPos( const QPointF& pos );
/**
* Returns the position of the PanelSvg
* @return the position
*/
QPointF pos() const;
/**
* Sets the prefix (@see setPrefix) to 'north', 'south', 'west' and 'east'
* Sets the prefix (@see setElementPrefix) to 'north', 'south', 'west' and 'east'
* when the location is TopEdge, BottomEdge, LeftEdge and RightEdge,
* respectively. Clears the prefix in other cases.
* @arg location location
*/
void setLocation(Plasma::Location location);
/**
* Returns the set location for the PanelSvg. Returns 0 if no location is set
* or a custom prefix is set (@see setPrefix)
* @return the location
*/
Plasma::Location location() const;
void setElementPrefix(Plasma::Location location);
/**
* Sets the prefix for the SVG elements to be used for painting. For example,
@ -150,7 +138,7 @@ class PLASMA_EXPORT PanelSvg : public QObject
* If the
* @arg prefix prefix for the SVG element names
*/
void setPrefix(const QString & prefix);
void setElementPrefix(const QString & prefix);
/**
* Returns the prefix for SVG elements of the PanelSvg
@ -169,22 +157,20 @@ class PLASMA_EXPORT PanelSvg : public QObject
* @arg painter the QPainter to use
* @arg rect the exposed rect to draw into
*/
Q_INVOKABLE void paint(QPainter* painter, const QRectF& rect);
Q_INVOKABLE void paint(QPainter* painter, const QRectF& rect, const QPointF& pos = QPointF(0, 0));
Q_SIGNALS:
void repaintNeeded();
private Q_SLOTS:
//update sizes of the svg elements
void updateSizes();
private:
class Private;
Private * const d;
Q_PRIVATE_SLOT(d, void updateSizes());
};
} // Plasma namespace
Q_DECLARE_OPERATORS_FOR_FLAGS(Plasma::PanelSvg::BorderFlags)
Q_DECLARE_OPERATORS_FOR_FLAGS(Plasma::PanelSvg::EnabledBorders)
#endif // multiple inclusion guard