concept of fixedMargins

sometimes we need to know what the margins are of a framesvg, even if some of them are disabled
This commit is contained in:
Marco Martin 2014-02-21 21:13:12 +01:00
parent 840a77b083
commit c009c7f0cd
6 changed files with 169 additions and 6 deletions

View File

@ -29,7 +29,8 @@ namespace Plasma
FrameSvgItemMargins::FrameSvgItemMargins(Plasma::FrameSvg *frameSvg, QObject *parent)
: QObject(parent),
m_frameSvg(frameSvg)
m_frameSvg(frameSvg),
m_fixed(false)
{
//qDebug() << "margins at: " << left() << top() << right() << bottom();
connect(m_frameSvg, SIGNAL(repaintNeeded()), this, SLOT(update()));
@ -37,22 +38,38 @@ FrameSvgItemMargins::FrameSvgItemMargins(Plasma::FrameSvg *frameSvg, QObject *pa
qreal FrameSvgItemMargins::left() const
{
return m_frameSvg->marginSize(Types::LeftMargin);
if (m_fixed) {
return m_frameSvg->fixedMarginSize(Types::LeftMargin);
} else {
return m_frameSvg->marginSize(Types::LeftMargin);
}
}
qreal FrameSvgItemMargins::top() const
{
return m_frameSvg->marginSize(Types::TopMargin);
if (m_fixed) {
return m_frameSvg->fixedMarginSize(Types::TopMargin);
} else {
return m_frameSvg->marginSize(Types::TopMargin);
}
}
qreal FrameSvgItemMargins::right() const
{
return m_frameSvg->marginSize(Types::RightMargin);
if (m_fixed) {
return m_frameSvg->fixedMarginSize(Types::RightMargin);
} else {
return m_frameSvg->marginSize(Types::RightMargin);
}
}
qreal FrameSvgItemMargins::bottom() const
{
return m_frameSvg->marginSize(Types::BottomMargin);
if (m_fixed) {
return m_frameSvg->fixedMarginSize(Types::BottomMargin);
} else {
return m_frameSvg->marginSize(Types::BottomMargin);
}
}
void FrameSvgItemMargins::update()
@ -60,11 +77,28 @@ void FrameSvgItemMargins::update()
emit marginsChanged();
}
void FrameSvgItemMargins::setFixed(bool fixed)
{
if (fixed == m_fixed) {
return;
}
m_fixed = fixed;
emit marginsChanged();
}
bool FrameSvgItemMargins::isFixed() const
{
return m_fixed;
}
FrameSvgItem::FrameSvgItem(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
m_frameSvg = new Plasma::FrameSvg(this);
m_margins = new FrameSvgItemMargins(m_frameSvg, this);
m_fixedMargins = new FrameSvgItemMargins(m_frameSvg, this);
m_fixedMargins->setFixed(true);
setFlag(ItemHasContents, true);
connect(m_frameSvg, SIGNAL(repaintNeeded()), this, SLOT(doUpdate()));
}
@ -143,6 +177,11 @@ FrameSvgItemMargins *FrameSvgItem::margins() const
return m_margins;
}
FrameSvgItemMargins *FrameSvgItem::fixedMargins() const
{
return m_fixedMargins;
}
void FrameSvgItem::setEnabledBorders(const Plasma::FrameSvg::EnabledBorders borders)
{
if (m_frameSvg->enabledBorders() == borders)

View File

@ -61,6 +61,9 @@ public:
qreal right() const;
qreal bottom() const;
void setFixed(bool fixed);
bool isFixed() const;
public Q_SLOTS:
void update();
@ -69,6 +72,7 @@ Q_SIGNALS:
private:
FrameSvg *m_frameSvg;
bool m_fixed;
};
class FrameSvgItem : public QQuickPaintedItem
@ -93,6 +97,13 @@ class FrameSvgItem : public QQuickPaintedItem
*/
Q_PROPERTY(QObject *margins READ margins CONSTANT)
/**
* The margins of the frame, regardless if they are enabled or not
* read only
* @see FrameSvgItemMargins
*/
Q_PROPERTY(QObject *fixedMargins READ fixedMargins CONSTANT)
Q_FLAGS(Plasma::FrameSvg::EnabledBorders)
/**
* The borders that will be rendered, it's a flag combination of:
@ -128,6 +139,7 @@ public:
Plasma::FrameSvg::EnabledBorders enabledBorders() const;
FrameSvgItemMargins *margins() const;
FrameSvgItemMargins *fixedMargins() const;
void paint(QPainter *painter);
@ -162,6 +174,7 @@ private Q_SLOTS:
private:
Plasma::FrameSvg *m_frameSvg;
FrameSvgItemMargins *m_margins;
FrameSvgItemMargins *m_fixedMargins;
QString m_prefix;
Units m_units;
};

View File

@ -177,7 +177,7 @@ Item {
Item {
parent: delegate
anchors.fill: parent
property alias margins: surface.margins
property alias margins: surface.fixedMargins
property alias hasOverState: shadow.hasOverState
Private.ButtonShadow {
id: shadow

View File

@ -412,6 +412,32 @@ qreal FrameSvg::marginSize(const Plasma::Types::MarginEdge edge) const
}
}
qreal FrameSvg::fixedMarginSize(const Plasma::Types::MarginEdge edge) const
{
if (d->frames[d->prefix]->noBorderPadding) {
return .0;
}
switch (edge) {
case Plasma::Types::TopMargin:
return d->frames[d->prefix]->fixedTopMargin;
break;
case Plasma::Types::LeftMargin:
return d->frames[d->prefix]->fixedLeftMargin;
break;
case Plasma::Types::RightMargin:
return d->frames[d->prefix]->fixedRightMargin;
break;
//Plasma::BottomMargin
default:
return d->frames[d->prefix]->fixedBottomMargin;
break;
}
}
void FrameSvg::getMargins(qreal &left, qreal &top, qreal &right, qreal &bottom) const
{
FrameData *frame = d->frames[d->prefix];
@ -427,6 +453,21 @@ void FrameSvg::getMargins(qreal &left, qreal &top, qreal &right, qreal &bottom)
bottom = frame->bottomMargin;
}
void FrameSvg::getFixedMargins(qreal &left, qreal &top, qreal &right, qreal &bottom) const
{
FrameData *frame = d->frames[d->prefix];
if (frame->noBorderPadding) {
left = top = right = bottom = 0;
return;
}
top = frame->fixedTopMargin;
left = frame->fixedLeftMargin;
right = frame->fixedRightMargin;
bottom = frame->fixedBottomMargin;
}
QRectF FrameSvg::contentsRect() const
{
QSizeF size(frameSize());
@ -975,6 +1016,17 @@ void FrameSvgPrivate::updateSizes() const
q->resize();
frame->cachedBackground = QPixmap();
//This has the same size regardless the border is enabled or not
frame->fixedTopHeight = q->elementSize(prefix % "top").height();
if (q->hasElement(prefix % "hint-top-margin")) {
frame->fixedTopMargin = q->elementSize(prefix % "hint-top-margin").height();
} else {
frame->fixedTopMargin = frame->fixedTopHeight;
}
//The same, but its size depends from the margin being enabled
if (frame->enabledBorders & FrameSvg::TopBorder) {
frame->topHeight = q->elementSize(prefix % "top").height();
@ -987,6 +1039,14 @@ void FrameSvgPrivate::updateSizes() const
frame->topMargin = frame->topHeight = 0;
}
frame->fixedLeftWidth = q->elementSize(prefix % "left").width();
if (q->hasElement(prefix % "hint-left-margin")) {
frame->fixedLeftMargin = q->elementSize(prefix % "hint-left-margin").width();
} else {
frame->fixedLeftMargin = frame->fixedLeftWidth;
}
if (frame->enabledBorders & FrameSvg::LeftBorder) {
frame->leftWidth = q->elementSize(prefix % "left").width();
@ -999,6 +1059,14 @@ void FrameSvgPrivate::updateSizes() const
frame->leftMargin = frame->leftWidth = 0;
}
frame->fixedRightWidth = q->elementSize(prefix % "right").width();
if (q->hasElement(prefix % "hint-right-margin")) {
frame->fixedRightMargin = q->elementSize(prefix % "hint-right-margin").width();
} else {
frame->fixedRightMargin = frame->fixedRightWidth;
}
if (frame->enabledBorders & FrameSvg::RightBorder) {
frame->rightWidth = q->elementSize(prefix % "right").width();
@ -1011,6 +1079,14 @@ void FrameSvgPrivate::updateSizes() const
frame->rightMargin = frame->rightWidth = 0;
}
frame->fixedBottomHeight = q->elementSize(prefix % "bottom").height();
if (q->hasElement(prefix % "hint-bottom-margin")) {
frame->fixedBottomMargin = q->elementSize(prefix % "hint-bottom-margin").height();
} else {
frame->fixedBottomMargin = frame->fixedBottomHeight;
}
if (frame->enabledBorders & FrameSvg::BottomBorder) {
frame->bottomHeight = q->elementSize(prefix % "bottom").height();

View File

@ -137,6 +137,8 @@ class PLASMA_EXPORT FrameSvg : public Svg
/**
* Returns the margin size given the margin edge we want
* If the given margin is disabled, it willreturn 0.
* If you don't care about the margin being on or off, use fixedMarginSize()
* @param edge the margin edge we want, top, bottom, left or right
* @return the margin size
*/
@ -145,6 +147,8 @@ class PLASMA_EXPORT FrameSvg : public Svg
/**
* Convenience method that extracts the size of the four margins
* in the four output parameters
* The disabled margins will be 0.
* If you don't care about the margins being on or off, use getFixedMargins()
* @param left left margin size
* @param top top margin size
* @param right right margin size
@ -152,6 +156,25 @@ class PLASMA_EXPORT FrameSvg : public Svg
*/
Q_INVOKABLE void getMargins(qreal &left, qreal &top, qreal &right, qreal &bottom) const;
/**
* Returns the margin size given the margin edge we want.
* Compared to marginSize(), this doesn't depend whether the margin is enabled or not
* @param edge the margin edge we want, top, bottom, left or right
* @return the margin size
*/
Q_INVOKABLE qreal fixedMarginSize(const Plasma::Types::MarginEdge edge) const;
/**
* Convenience method that extracts the size of the four margins
* in the four output parameters
* Compared to getMargins(), this doesn't depend whether the margins are enabled or not
* @param left left margin size
* @param top top margin size
* @param right right margin size
* @param bottom bottom margin size
*/
Q_INVOKABLE void getFixedMargins(qreal &left, qreal &top, qreal &right, qreal &bottom) const;
/**
* @return the rectangle of the center element, taking the margins into account.
*/

View File

@ -100,6 +100,18 @@ public:
int rightMargin;
int bottomMargin;
//measures
int fixedTopHeight;
int fixedLeftWidth;
int fixedRightWidth;
int fixedBottomHeight;
//margins, are equal to the measures by default
int fixedTopMargin;
int fixedLeftMargin;
int fixedRightMargin;
int fixedBottomMargin;
//size of the svg where the size of the "center"
//element is contentWidth x contentHeight
bool noBorderPadding : 1;