diff --git a/src/plasma/framesvg.h b/src/plasma/framesvg.h --- a/src/plasma/framesvg.h +++ b/src/plasma/framesvg.h @@ -29,6 +29,7 @@ #include #include +class QMarginsF; class QPainter; class QPoint; class QPointF; @@ -156,6 +157,18 @@ */ Q_INVOKABLE void getMargins(qreal &left, qreal &top, qreal &right, qreal &bottom) const; + /** + * Returns margins, taking enabled borders into account + * + * If a border is disabled, corresponding margin size will + * have value of 0. If you don't care about the margins being + * on or off, use fixedMargins(). + * + * @returns the margins + * @see fixedMargins + */ + Q_INVOKABLE QMarginsF margins() 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 @@ -175,6 +188,20 @@ */ Q_INVOKABLE void getFixedMargins(qreal &left, qreal &top, qreal &right, qreal &bottom) const; + /** + * Returns fixed margins + * + * The main difference between fixedMargins() and margins() is that + * margins() takes into account what borders are enabled, while + * fixedMargins() ignores that knowledge. E.g. if fixedMargins() + * returns QMargins(10, 20, 42, 30) and only right border is on, + * margins() will return QMargins(0, 0, 42, 0). + * + * @returns the fixed margins + * @see margins + */ + Q_INVOKABLE QMarginsF fixedMargins() const; + /** * @return the rectangle of the center element, taking the margins into account. */ diff --git a/src/plasma/framesvg.cpp b/src/plasma/framesvg.cpp --- a/src/plasma/framesvg.cpp +++ b/src/plasma/framesvg.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -220,17 +221,17 @@ switch (edge) { case Plasma::Types::TopMargin: - return d->frame->topMargin; + return d->frame->margins.top(); case Plasma::Types::LeftMargin: - return d->frame->leftMargin; + return d->frame->margins.left(); case Plasma::Types::RightMargin: - return d->frame->rightMargin; + return d->frame->margins.right(); //Plasma::BottomMargin default: - return d->frame->bottomMargin; + return d->frame->margins.bottom(); } } @@ -246,17 +247,17 @@ switch (edge) { case Plasma::Types::TopMargin: - return d->frame->fixedTopMargin; + return d->frame->fixedMargins.top(); case Plasma::Types::LeftMargin: - return d->frame->fixedLeftMargin; + return d->frame->fixedMargins.left(); case Plasma::Types::RightMargin: - return d->frame->fixedRightMargin; + return d->frame->fixedMargins.right(); //Plasma::BottomMargin default: - return d->frame->fixedBottomMargin; + return d->frame->fixedMargins.bottom(); } } @@ -267,10 +268,18 @@ return; } - top = d->frame->topMargin; - left = d->frame->leftMargin; - right = d->frame->rightMargin; - bottom = d->frame->bottomMargin; + top = d->frame->margins.top(); + left = d->frame->margins.left(); + right = d->frame->margins.right(); + bottom = d->frame->margins.bottom(); +} + +QMarginsF FrameSvg::margins() const +{ + if (!d->frame || d->frame->noBorderPadding) { + return QMarginsF(); + } + return d->frame->margins; } void FrameSvg::getFixedMargins(qreal &left, qreal &top, qreal &right, qreal &bottom) const @@ -280,20 +289,26 @@ return; } - top = d->frame->fixedTopMargin; - left = d->frame->fixedLeftMargin; - right = d->frame->fixedRightMargin; - bottom = d->frame->fixedBottomMargin; + top = d->frame->fixedMargins.top(); + left = d->frame->fixedMargins.left(); + right = d->frame->fixedMargins.right(); + bottom = d->frame->fixedMargins.bottom(); +} + +QMarginsF FrameSvg::fixedMargins() const +{ + if (!d->frame || d->frame->noBorderPadding) { + return QMarginsF(); + } + return d->frame->fixedMargins; } QRectF FrameSvg::contentsRect() const { - if (d->frame) { - QRectF rect(QPoint(0,0), d->frame->frameSize); - return rect.adjusted(d->frame->leftMargin, d->frame->topMargin, -d->frame->rightMargin, -d->frame->bottomMargin); - } else { + if (!d->frame) { return QRectF(); } + return QRectF(QPointF(0, 0), d->frame->frameSize).marginsRemoved(d->frame->margins); } QPixmap FrameSvg::alphaMask() const @@ -614,31 +629,20 @@ // Sides const int leftHeight = q->elementSize(frame->prefix % QLatin1String("left")).height(); - paintBorder(p, frame, FrameSvg::LeftBorder, QSize(frame->leftWidth, leftHeight) * q->devicePixelRatio(), contentRect); - paintBorder(p, frame, FrameSvg::RightBorder, QSize(frame->rightWidth, leftHeight) * q->devicePixelRatio(), contentRect); + paintBorder(p, frame, FrameSvg::LeftBorder, QSize(frame->measures.left(), leftHeight) * q->devicePixelRatio(), contentRect); + paintBorder(p, frame, FrameSvg::RightBorder, QSize(frame->measures.right(), leftHeight) * q->devicePixelRatio(), contentRect); const int topWidth = q->elementSize(frame->prefix % QLatin1String("top")).width(); - paintBorder(p, frame, FrameSvg::TopBorder, QSize(topWidth, frame->topHeight) * q->devicePixelRatio(), contentRect); - paintBorder(p, frame, FrameSvg::BottomBorder, QSize(topWidth, frame->bottomHeight) * q->devicePixelRatio(), contentRect); + paintBorder(p, frame, FrameSvg::TopBorder, QSize(topWidth, frame->measures.top()) * q->devicePixelRatio(), contentRect); + paintBorder(p, frame, FrameSvg::BottomBorder, QSize(topWidth, frame->measures.bottom()) * q->devicePixelRatio(), contentRect); p.end(); frame->cachedBackground.setDevicePixelRatio(q->devicePixelRatio()); } QRect FrameSvgPrivate::contentGeometry(FrameData* frame, const QSize& size) const { - const QSize contentSize(size.width() - frame->leftWidth * q->devicePixelRatio() - frame->rightWidth * q->devicePixelRatio(), - size.height() - frame->topHeight * q->devicePixelRatio() - frame->bottomHeight * q->devicePixelRatio()); - QRect contentRect(QPoint(0,0), contentSize); - if (frame->enabledBorders & FrameSvg::LeftBorder && q->hasElement(frame->prefix % QLatin1String("left"))) { - contentRect.translate(frame->leftWidth * q->devicePixelRatio(), 0); - } - - // Corners - if (frame->enabledBorders & FrameSvg::TopBorder && q->hasElement(frame->prefix % QLatin1String("top"))) { - contentRect.translate(0, frame->topHeight * q->devicePixelRatio()); - } - return contentRect; + return QRect(QPoint(0, 0), size).marginsRemoved(frame->measures * q->devicePixelRatio()); } void FrameSvgPrivate::updateFrameData(UpdateType updateType) @@ -829,93 +833,73 @@ } //This has the same size regardless the border is enabled or not - frame->fixedTopHeight = q->elementSize(frame->prefix % QLatin1String("top")).height(); + frame->fixedMeasures.setTop(q->elementSize(frame->prefix % QLatin1String("top")).height()); - int hintTopMargin = -1; if (q->hasElement(frame->prefix % QLatin1String("hint-top-margin"))) { - hintTopMargin = q->elementSize(frame->prefix % QLatin1String("hint-top-margin")).height(); - frame->fixedTopMargin = hintTopMargin; + const int hint = q->elementSize(frame->prefix % QLatin1String("hint-top-margin")).height(); + frame->fixedMargins.setTop(hint); } else { - frame->fixedTopMargin = frame->fixedTopHeight; + frame->fixedMargins.setTop(frame->fixedMeasures.top()); } //The same, but its size depends from the margin being enabled if (frame->enabledBorders & FrameSvg::TopBorder) { - frame->topHeight = frame->fixedTopHeight; - - if (hintTopMargin > -1) { - frame->topMargin = hintTopMargin; - } else { - frame->topMargin = frame->topHeight; - } + frame->margins.setTop(frame->fixedMargins.top()); + frame->measures.setTop(frame->fixedMeasures.top()); } else { - frame->topMargin = frame->topHeight = 0; + frame->margins.setTop(0); + frame->measures.setTop(0); } - frame->fixedLeftWidth = q->elementSize(frame->prefix % QLatin1String("left")).width(); + frame->fixedMeasures.setLeft(q->elementSize(frame->prefix % QLatin1String("left")).width()); - int hintLeftMargin = -1; if (q->hasElement(frame->prefix % QLatin1String("hint-left-margin"))) { - hintLeftMargin = q->elementSize(frame->prefix % QLatin1String("hint-left-margin")).width(); - frame->fixedLeftMargin = hintLeftMargin; + const int hint = q->elementSize(frame->prefix % QLatin1String("hint-left-margin")).width(); + frame->fixedMargins.setLeft(hint); } else { - frame->fixedLeftMargin = frame->fixedLeftWidth; + frame->fixedMargins.setLeft(frame->fixedMeasures.left()); } if (frame->enabledBorders & FrameSvg::LeftBorder) { - frame->leftWidth = frame->fixedLeftWidth; - - if (hintLeftMargin > -1) { - frame->leftMargin = hintLeftMargin; - } else { - frame->leftMargin = frame->leftWidth; - } + frame->margins.setLeft(frame->fixedMargins.left()); + frame->measures.setLeft(frame->fixedMeasures.left()); } else { - frame->leftMargin = frame->leftWidth = 0; + frame->margins.setLeft(0); + frame->measures.setLeft(0); } - frame->fixedRightWidth = q->elementSize(frame->prefix % QLatin1String("right")).width(); + frame->fixedMeasures.setRight(q->elementSize(frame->prefix % QLatin1String("right")).width()); - int hintRightMargin = -1; if (q->hasElement(frame->prefix % QLatin1String("hint-right-margin"))) { - hintRightMargin = q->elementSize(frame->prefix % QLatin1String("hint-right-margin")).width(); - frame->fixedRightMargin = hintRightMargin; + const int hint = q->elementSize(frame->prefix % QLatin1String("hint-right-margin")).width(); + frame->fixedMargins.setRight(hint); } else { - frame->fixedRightMargin = frame->fixedRightWidth; + frame->fixedMargins.setRight(frame->fixedMeasures.right()); } if (frame->enabledBorders & FrameSvg::RightBorder) { - frame->rightWidth = frame->fixedRightWidth; - - if (hintRightMargin > -1) { - frame->rightMargin = hintRightMargin; - } else { - frame->rightMargin = frame->rightWidth; - } + frame->margins.setRight(frame->fixedMargins.right()); + frame->measures.setRight(frame->fixedMeasures.right()); } else { - frame->rightMargin = frame->rightWidth = 0; + frame->margins.setRight(0); + frame->measures.setRight(0); } - frame->fixedBottomHeight = q->elementSize(frame->prefix % QLatin1String("bottom")).height(); + frame->fixedMeasures.setBottom(q->elementSize(frame->prefix % QLatin1String("bottom")).height()); - int hintBottomMargin = -1; if (q->hasElement(frame->prefix % QLatin1String("hint-bottom-margin"))) { - hintBottomMargin = q->elementSize(frame->prefix % QLatin1String("hint-bottom-margin")).height(); - frame->fixedBottomMargin = hintBottomMargin; + const int hint = q->elementSize(frame->prefix % QLatin1String("hint-bottom-margin")).height(); + frame->fixedMargins.setBottom(hint); } else { - frame->fixedBottomMargin = frame->fixedBottomHeight; + frame->fixedMargins.setBottom(frame->fixedMeasures.right()); } if (frame->enabledBorders & FrameSvg::BottomBorder) { - frame->bottomHeight = frame->fixedBottomHeight; - - if (hintBottomMargin > -1) { - frame->bottomMargin = hintBottomMargin; - } else { - frame->bottomMargin = frame->bottomHeight; - } + frame->margins.setBottom(frame->fixedMargins.bottom()); + frame->measures.setBottom(frame->fixedMeasures.bottom()); } else { - frame->bottomMargin = frame->bottomHeight = 0; + frame->margins.setBottom(0); + frame->measures.setBottom(0); } frame->composeOverBorder = (q->hasElement(frame->prefix % QLatin1String("hint-compose-over-border")) && diff --git a/src/plasma/private/framesvg_p.h b/src/plasma/private/framesvg_p.h --- a/src/plasma/private/framesvg_p.h +++ b/src/plasma/private/framesvg_p.h @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -40,14 +41,6 @@ prefix(p), enabledBorders(FrameSvg::AllBorders), frameSize(-1, -1), - topHeight(0), - leftWidth(0), - rightWidth(0), - bottomHeight(0), - topMargin(0), - leftMargin(0), - rightMargin(0), - bottomMargin(0), noBorderPadding(false), stretchBorders(false), tileCenter(false), @@ -63,14 +56,6 @@ enabledBorders(other.enabledBorders), cachedMasks(MAX_CACHED_MASKS), frameSize(other.frameSize), - topHeight(0), - leftWidth(0), - rightWidth(0), - bottomHeight(0), - topMargin(0), - leftMargin(0), - rightMargin(0), - bottomMargin(0), devicePixelRatio(svg->devicePixelRatio()), noBorderPadding(false), stretchBorders(false), @@ -99,29 +84,11 @@ QSize frameSize; - //measures - int topHeight; - int leftWidth; - int rightWidth; - int bottomHeight; - - //margins, are equal to the measures by default - int topMargin; - int leftMargin; - 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; + QMargins margins; + QMargins fixedMargins; + + QMargins measures; + QMargins fixedMeasures; qreal devicePixelRatio;