summaryrefslogtreecommitdiffstats
path: root/src/gui
diff options
context:
space:
mode:
authorJens Bache-Wiig <jbache@trolltech.com>2009-08-31 16:15:13 (GMT)
committerJens Bache-Wiig <jbache@trolltech.com>2009-08-31 16:20:51 (GMT)
commit758f4735bcae034ac25730e53bb371df3b7d6e8a (patch)
tree7086950b36e5b46ab5a034839c1e489904e51fc1 /src/gui
parente70980b2aacbc758a0cd1e2246633278f7c505ab (diff)
downloadQt-758f4735bcae034ac25730e53bb371df3b7d6e8a.zip
Qt-758f4735bcae034ac25730e53bb371df3b7d6e8a.tar.gz
Qt-758f4735bcae034ac25730e53bb371df3b7d6e8a.tar.bz2
Make QMargins a proper class
Since we need QMargins for other things then the CSS helper functions in drawutil, we have to make it more generic. It is already useful for QWidget::contentsMargins for example. This ensures we have some flexibility on how to use and modify it in the future. Reviewed-by: mbm
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/kernel/qwidget.cpp36
-rw-r--r--src/gui/kernel/qwidget.h4
-rw-r--r--src/gui/painting/qdrawutil.cpp137
-rw-r--r--src/gui/painting/qdrawutil.h20
4 files changed, 98 insertions, 99 deletions
diff --git a/src/gui/kernel/qwidget.cpp b/src/gui/kernel/qwidget.cpp
index ff644b8..37ffa8fb 100644
--- a/src/gui/kernel/qwidget.cpp
+++ b/src/gui/kernel/qwidget.cpp
@@ -6748,7 +6748,27 @@ void QWidget::setContentsMargins(int left, int top, int right, int bottom)
QApplication::sendEvent(this, &e);
}
-/*! Returns the widget's contents margins for \a left, \a top, \a
+/*!
+ \overload
+ \since 4.6
+
+ Sets the margins around the contents of the widget to have the
+ sizes determined by \a margins. The margins are
+ used by the layout system, and may be used by subclasses to
+ specify the area to draw in (e.g. excluding the frame).
+
+ Changing the margins will trigger a resizeEvent().
+
+ \sa contentsRect(), getContentsMargins()
+*/
+void QWidget::setContentsMargins(const QMargins &margins)
+{
+ setContentsMargins(margins.left(), margins.top(),
+ margins.right(), margins.bottom());
+}
+
+/*!
+ Returns the widget's contents margins for \a left, \a top, \a
right, and \a bottom.
\sa setContentsMargins(), contentsRect()
@@ -6767,6 +6787,20 @@ void QWidget::getContentsMargins(int *left, int *top, int *right, int *bottom) c
}
/*!
+ \since 4.6
+
+ Returns the widget's contents margins.
+
+ \sa getContentsMargins(), setContentsMargins(), contentsRect()
+ */
+QMargins QWidget::contentsMargins() const
+{
+ Q_D(const QWidget);
+ return QMargins(d->leftmargin, d->topmargin, d->rightmargin, d->bottommargin);
+}
+
+
+/*!
Returns the area inside the widget's margins.
\sa setContentsMargins(), getContentsMargins()
diff --git a/src/gui/kernel/qwidget.h b/src/gui/kernel/qwidget.h
index 38650f0..f398dbd 100644
--- a/src/gui/kernel/qwidget.h
+++ b/src/gui/kernel/qwidget.h
@@ -44,6 +44,7 @@
#include <QtGui/qwindowdefs.h>
#include <QtCore/qobject.h>
+#include <QtCore/qmargins.h>
#include <QtGui/qpaintdevice.h>
#include <QtGui/qpalette.h>
#include <QtGui/qfont.h>
@@ -527,7 +528,10 @@ public:
QRegion visibleRegion() const;
void setContentsMargins(int left, int top, int right, int bottom);
+ void setContentsMargins(const QMargins &margins);
void getContentsMargins(int *left, int *top, int *right, int *bottom) const;
+ QMargins contentsMargins() const;
+
QRect contentsRect() const;
public:
diff --git a/src/gui/painting/qdrawutil.cpp b/src/gui/painting/qdrawutil.cpp
index 59a5063..2220206 100644
--- a/src/gui/painting/qdrawutil.cpp
+++ b/src/gui/painting/qdrawutil.cpp
@@ -1038,27 +1038,6 @@ void qDrawItem(QPainter *p, Qt::GUIStyle gs,
#endif
/*!
- \class QMargins
- \since 4.6
-
- Holds the borders used to split a pixmap into nine segments in order to
- draw it, similar to \l{http://www.w3.org/TR/css3-background/}
- {CSS3 border-images}.
-
- \sa Qt::TileRule, QTileRules
-*/
-
-/*! \fn QMargins::QMargins(int margin)
- Constructs a QMargins with the top, left, bottom, and
- right margins set to \a margin.
-*/
-
-/*! \fn QMargins::QMargins(int topMargin, int leftMargin, int bottomMargin, int rightMargin)
- Constructs a QMargins with the given \a topMargin, \a leftMargin,
- \a bottomMargin, and \a rightMargin.
- */
-
-/*!
\class QTileRules
\since 4.6
@@ -1195,79 +1174,79 @@ void qDrawBorderPixmap(QPainter *painter, const QRect &targetRect, const QMargin
// source center
const int sourceTop = sourceRect.top();
const int sourceLeft = sourceRect.left();
- const int sourceCenterTop = sourceTop + sourceMargins.top;
- const int sourceCenterLeft = sourceLeft + sourceMargins.left;
- const int sourceCenterBottom = sourceRect.bottom() - sourceMargins.bottom + 1;
- const int sourceCenterRight = sourceRect.right() - sourceMargins.right + 1;
- const int sourceCenterWidth = sourceCenterRight - sourceMargins.left;
- const int sourceCenterHeight = sourceCenterBottom - sourceMargins.top;
+ const int sourceCenterTop = sourceTop + sourceMargins.top();
+ const int sourceCenterLeft = sourceLeft + sourceMargins.left();
+ const int sourceCenterBottom = sourceRect.bottom() - sourceMargins.bottom() + 1;
+ const int sourceCenterRight = sourceRect.right() - sourceMargins.right() + 1;
+ const int sourceCenterWidth = sourceCenterRight - sourceMargins.left();
+ const int sourceCenterHeight = sourceCenterBottom - sourceMargins.top();
// target center
const int targetTop = targetRect.top();
const int targetLeft = targetRect.left();
- const int targetCenterTop = targetTop + targetMargins.top;
- const int targetCenterLeft = targetLeft + targetMargins.left;
- const int targetCenterBottom = targetRect.bottom() - targetMargins.bottom + 1;
- const int targetCenterRight = targetRect.right() - targetMargins.right + 1;
+ const int targetCenterTop = targetTop + targetMargins.top();
+ const int targetCenterLeft = targetLeft + targetMargins.left();
+ const int targetCenterBottom = targetRect.bottom() - targetMargins.bottom() + 1;
+ const int targetCenterRight = targetRect.right() - targetMargins.right() + 1;
const int targetCenterWidth = targetCenterRight - targetCenterLeft;
const int targetCenterHeight = targetCenterBottom - targetCenterTop;
// corners
- if (targetMargins.top > 0 && targetMargins.left > 0 && sourceMargins.top > 0 && sourceMargins.left > 0) { // top left
- const QRect targetTopLeftRect(targetLeft, targetTop, targetMargins.left, targetMargins.top);
- const QRect sourceTopLeftRect(sourceLeft, sourceTop, sourceMargins.left, sourceMargins.top);
+ if (targetMargins.top() > 0 && targetMargins.left() > 0 && sourceMargins.top() > 0 && sourceMargins.left() > 0) { // top left
+ const QRect targetTopLeftRect(targetLeft, targetTop, targetMargins.left(), targetMargins.top());
+ const QRect sourceTopLeftRect(sourceLeft, sourceTop, sourceMargins.left(), sourceMargins.top());
qDrawPixmap(painter, targetTopLeftRect, pixmap, sourceTopLeftRect);
}
- if (targetMargins.top > 0 && targetMargins.right > 0 && sourceMargins.top > 0 && sourceMargins.right > 0) { // top right
- const QRect targetTopRightRect(targetCenterRight, targetTop, targetMargins.right, targetMargins.top);
- const QRect sourceTopRightRect(sourceCenterRight, sourceTop, sourceMargins.right, sourceMargins.top);
+ if (targetMargins.top() > 0 && targetMargins.right() > 0 && sourceMargins.top() > 0 && sourceMargins.right() > 0) { // top right
+ const QRect targetTopRightRect(targetCenterRight, targetTop, targetMargins.right(), targetMargins.top());
+ const QRect sourceTopRightRect(sourceCenterRight, sourceTop, sourceMargins.right(), sourceMargins.top());
qDrawPixmap(painter, targetTopRightRect, pixmap, sourceTopRightRect);
}
- if (targetMargins.bottom > 0 && targetMargins.left > 0 && sourceMargins.bottom > 0 && sourceMargins.left > 0) { // bottom left
- const QRect targetBottomLeftRect(targetLeft, targetCenterBottom, targetMargins.left, targetMargins.bottom);
- const QRect sourceBottomLeftRect(sourceLeft, sourceCenterBottom, sourceMargins.left, sourceMargins.bottom);
+ if (targetMargins.bottom() > 0 && targetMargins.left() > 0 && sourceMargins.bottom() > 0 && sourceMargins.left() > 0) { // bottom left
+ const QRect targetBottomLeftRect(targetLeft, targetCenterBottom, targetMargins.left(), targetMargins.bottom());
+ const QRect sourceBottomLeftRect(sourceLeft, sourceCenterBottom, sourceMargins.left(), sourceMargins.bottom());
qDrawPixmap(painter, targetBottomLeftRect, pixmap, sourceBottomLeftRect);
}
- if (targetMargins.bottom > 0 && targetMargins.right > 0 && sourceMargins.bottom > 0 && sourceMargins.right > 0) { // bottom right
- const QRect targetBottomRightRect(targetCenterRight, targetCenterBottom, targetMargins.right, targetMargins.bottom);
- const QRect sourceBottomRightRect(sourceCenterRight, sourceCenterBottom, sourceMargins.right, sourceMargins.bottom);
+ if (targetMargins.bottom() > 0 && targetMargins.right() > 0 && sourceMargins.bottom() > 0 && sourceMargins.right() > 0) { // bottom right
+ const QRect targetBottomRightRect(targetCenterRight, targetCenterBottom, targetMargins.right(), targetMargins.bottom());
+ const QRect sourceBottomRightRect(sourceCenterRight, sourceCenterBottom, sourceMargins.right(), sourceMargins.bottom());
qDrawPixmap(painter, targetBottomRightRect, pixmap, sourceBottomRightRect);
}
// horizontal edges
switch (rules.horizontal) {
case Qt::Stretch:
- if (targetMargins.top > 0 && sourceMargins.top > 0) { // top
- const QRect targetTopRect(targetCenterLeft, targetTop, targetCenterWidth, targetMargins.top);
- const QRect sourceTopRect(sourceCenterLeft, sourceTop, sourceCenterWidth, sourceMargins.top);
+ if (targetMargins.top() > 0 && sourceMargins.top() > 0) { // top
+ const QRect targetTopRect(targetCenterLeft, targetTop, targetCenterWidth, targetMargins.top());
+ const QRect sourceTopRect(sourceCenterLeft, sourceTop, sourceCenterWidth, sourceMargins.top());
qDrawPixmap(painter, targetTopRect, pixmap, sourceTopRect);
}
- if (targetMargins.bottom > 0 && sourceMargins.bottom > 0) { // bottom
- const QRect targetBottomRect(targetCenterLeft, targetCenterBottom, targetCenterWidth, targetMargins.bottom);
- const QRect sourceBottomRect(sourceCenterLeft, sourceCenterBottom, sourceCenterWidth, sourceMargins.bottom);
+ if (targetMargins.bottom() > 0 && sourceMargins.bottom() > 0) { // bottom
+ const QRect targetBottomRect(targetCenterLeft, targetCenterBottom, targetCenterWidth, targetMargins.bottom());
+ const QRect sourceBottomRect(sourceCenterLeft, sourceCenterBottom, sourceCenterWidth, sourceMargins.bottom());
qDrawPixmap(painter, targetBottomRect, pixmap, sourceBottomRect);
}
break;
case Qt::Repeat:
- if (targetMargins.top > 0 && sourceMargins.top > 0) { // top
- const QRect targetTopRect(targetCenterLeft, targetTop, targetCenterWidth, targetMargins.top);
- const QRect sourceTopRect(sourceCenterLeft, sourceTop, sourceCenterWidth, sourceMargins.top);
+ if (targetMargins.top() > 0 && sourceMargins.top() > 0) { // top
+ const QRect targetTopRect(targetCenterLeft, targetTop, targetCenterWidth, targetMargins.top());
+ const QRect sourceTopRect(sourceCenterLeft, sourceTop, sourceCenterWidth, sourceMargins.top());
qDrawHorizontallyRepeatedPixmap(painter, targetTopRect, pixmap, sourceTopRect);
}
- if (targetMargins.bottom > 0 && sourceMargins.bottom > 0) { // bottom
- const QRect targetBottomRect(targetCenterLeft, targetCenterBottom, targetCenterWidth, targetMargins.bottom);
- const QRect sourceBottomRect(sourceCenterLeft, sourceCenterBottom, sourceCenterWidth, sourceMargins.bottom);
+ if (targetMargins.bottom() > 0 && sourceMargins.bottom() > 0) { // bottom
+ const QRect targetBottomRect(targetCenterLeft, targetCenterBottom, targetCenterWidth, targetMargins.bottom());
+ const QRect sourceBottomRect(sourceCenterLeft, sourceCenterBottom, sourceCenterWidth, sourceMargins.bottom());
qDrawHorizontallyRepeatedPixmap(painter, targetBottomRect, pixmap, sourceBottomRect);
}
break;
case Qt::Round:
- if (targetMargins.top > 0 && sourceMargins.top > 0) { // top
- const QRect targetTopRect(targetCenterLeft, targetTop, targetCenterWidth, targetMargins.top);
- const QRect sourceTopRect(sourceCenterLeft, sourceTop, sourceCenterWidth, sourceMargins.top);
+ if (targetMargins.top() > 0 && sourceMargins.top() > 0) { // top
+ const QRect targetTopRect(targetCenterLeft, targetTop, targetCenterWidth, targetMargins.top());
+ const QRect sourceTopRect(sourceCenterLeft, sourceTop, sourceCenterWidth, sourceMargins.top());
qDrawHorizontallyRoundedPixmap(painter, targetTopRect, pixmap, sourceTopRect);
}
- if (targetMargins.bottom > 0 && sourceMargins.bottom > 0) { // bottom
- const QRect targetBottomRect(targetCenterLeft, targetCenterBottom, targetCenterWidth, targetMargins.bottom);
- const QRect sourceBottomRect(sourceCenterLeft, sourceCenterBottom, sourceCenterWidth, sourceMargins.bottom);
+ if (targetMargins.bottom() > 0 && sourceMargins.bottom() > 0) { // bottom
+ const QRect targetBottomRect(targetCenterLeft, targetCenterBottom, targetCenterWidth, targetMargins.bottom());
+ const QRect sourceBottomRect(sourceCenterLeft, sourceCenterBottom, sourceCenterWidth, sourceMargins.bottom());
qDrawHorizontallyRoundedPixmap(painter, targetBottomRect, pixmap, sourceBottomRect);
}
break;
@@ -1276,38 +1255,38 @@ void qDrawBorderPixmap(QPainter *painter, const QRect &targetRect, const QMargin
// vertical edges
switch (rules.vertical) {
case Qt::Stretch:
- if (targetMargins.left > 0 && sourceMargins.left > 0) { // left
- const QRect targetLeftRect(targetLeft, targetCenterTop, targetMargins.left, targetCenterHeight);
- const QRect sourceLeftRect(sourceLeft, sourceCenterTop, sourceMargins.left, sourceCenterHeight);
+ if (targetMargins.left() > 0 && sourceMargins.left() > 0) { // left
+ const QRect targetLeftRect(targetLeft, targetCenterTop, targetMargins.left(), targetCenterHeight);
+ const QRect sourceLeftRect(sourceLeft, sourceCenterTop, sourceMargins.left(), sourceCenterHeight);
qDrawPixmap(painter, targetLeftRect, pixmap, sourceLeftRect);
}
- if (targetMargins.right > 0 && sourceMargins.right > 0) { // right
- const QRect targetRightRect(targetCenterRight, targetCenterTop, targetMargins.right, targetCenterHeight);
- const QRect sourceRightRect(sourceCenterRight, sourceCenterTop, sourceMargins.right, sourceCenterHeight);
+ if (targetMargins.right() > 0 && sourceMargins.right() > 0) { // right
+ const QRect targetRightRect(targetCenterRight, targetCenterTop, targetMargins.right(), targetCenterHeight);
+ const QRect sourceRightRect(sourceCenterRight, sourceCenterTop, sourceMargins.right(), sourceCenterHeight);
qDrawPixmap(painter, targetRightRect, pixmap, sourceRightRect);
}
break;
case Qt::Repeat:
- if (targetMargins.left > 0 && sourceMargins.left > 0) { // left
- const QRect targetLeftRect(targetLeft, targetCenterTop, targetMargins.left, targetCenterHeight);
- const QRect sourceLeftRect(sourceLeft, sourceCenterTop, sourceMargins.left, sourceCenterHeight);
+ if (targetMargins.left() > 0 && sourceMargins.left() > 0) { // left
+ const QRect targetLeftRect(targetLeft, targetCenterTop, targetMargins.left(), targetCenterHeight);
+ const QRect sourceLeftRect(sourceLeft, sourceCenterTop, sourceMargins.left(), sourceCenterHeight);
qDrawVerticallyRepeatedPixmap(painter, targetLeftRect, pixmap, sourceLeftRect);
}
- if (targetMargins.right > 0 && sourceMargins.right > 0) { // right
- const QRect targetRightRect(targetCenterRight, targetCenterTop, targetMargins.right, targetCenterHeight);
- const QRect sourceRightRect(sourceCenterRight, sourceCenterTop, sourceMargins.right, sourceCenterHeight);
+ if (targetMargins.right() > 0 && sourceMargins.right() > 0) { // right
+ const QRect targetRightRect(targetCenterRight, targetCenterTop, targetMargins.right(), targetCenterHeight);
+ const QRect sourceRightRect(sourceCenterRight, sourceCenterTop, sourceMargins.right(), sourceCenterHeight);
qDrawVerticallyRepeatedPixmap(painter, targetRightRect, pixmap, sourceRightRect);
}
break;
case Qt::Round:
- if (targetMargins.left > 0 && sourceMargins.left > 0) { // left
- const QRect targetLeftRect(targetLeft, targetCenterTop, targetMargins.left, targetCenterHeight);
- const QRect sourceLeftRect(sourceLeft, sourceCenterTop, sourceMargins.left, sourceCenterHeight);
+ if (targetMargins.left() > 0 && sourceMargins.left() > 0) { // left
+ const QRect targetLeftRect(targetLeft, targetCenterTop, targetMargins.left(), targetCenterHeight);
+ const QRect sourceLeftRect(sourceLeft, sourceCenterTop, sourceMargins.left(), sourceCenterHeight);
qDrawVerticallyRoundedPixmap(painter, targetLeftRect, pixmap, sourceLeftRect);
}
- if (targetMargins.right > 0 && sourceMargins.right > 0) { // right
- const QRect targetRightRect(targetCenterRight, targetCenterTop, targetMargins.right, targetCenterHeight);
- const QRect sourceRightRect(sourceCenterRight, sourceCenterTop, sourceMargins.right, sourceCenterHeight);
+ if (targetMargins.right() > 0 && sourceMargins.right() > 0) { // right
+ const QRect targetRightRect(targetCenterRight, targetCenterTop, targetMargins.right(), targetCenterHeight);
+ const QRect sourceRightRect(sourceCenterRight, sourceCenterTop, sourceMargins.right(), sourceCenterHeight);
qDrawVerticallyRoundedPixmap(painter, targetRightRect, pixmap, sourceRightRect);
}
break;
diff --git a/src/gui/painting/qdrawutil.h b/src/gui/painting/qdrawutil.h
index 4135c19..38ef30e 100644
--- a/src/gui/painting/qdrawutil.h
+++ b/src/gui/painting/qdrawutil.h
@@ -44,8 +44,8 @@
#include <QtCore/qnamespace.h>
#include <QtCore/qstring.h> // char*->QString conversion
+#include <QtCore/qmargins.h>
#include <QtGui/qpixmap.h>
-
QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
@@ -133,24 +133,6 @@ Q_GUI_EXPORT QT3_SUPPORT void qDrawArrow(QPainter *p, Qt::ArrowType type, Qt::GU
const QPalette &pal, bool enabled);
#endif
-struct QMargins
-{
- inline QMargins(int margin = 0)
- : top(margin),
- left(margin),
- bottom(margin),
- right(margin) {}
- inline QMargins(int topMargin, int leftMargin, int bottomMargin, int rightMargin)
- : top(topMargin),
- left(leftMargin),
- bottom(bottomMargin),
- right(rightMargin) {}
- int top;
- int left;
- int bottom;
- int right;
-};
-
struct QTileRules
{
inline QTileRules(Qt::TileRule horizontalRule, Qt::TileRule verticalRule = Qt::Stretch)