Qt 4.8
Functions
qcssutil_p.h File Reference
#include "QtCore/qglobal.h"
#include "private/qcssparser_p.h"
#include "QtCore/qsize.h"

Go to the source code of this file.

Functions

void qDrawBorder (QPainter *p, const QRect &rect, const QCss::BorderStyle *styles, const int *borders, const QBrush *colors, const QSize *radii)
 
void qDrawEdge (QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2, qreal dw1, qreal dw2, QCss::Edge edge, QCss::BorderStyle style, QBrush c)
 
void qDrawRoundedCorners (QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2, const QSizeF &r1, const QSizeF &r2, QCss::Edge edge, QCss::BorderStyle s, QBrush c)
 
void qNormalizeRadii (const QRect &br, const QSize *radii, QSize *tlr, QSize *trr, QSize *blr, QSize *brr)
 

Function Documentation

◆ qDrawBorder()

void qDrawBorder ( QPainter p,
const QRect rect,
const QCss::BorderStyle styles,
const int *  borders,
const QBrush colors,
const QSize radii 
)

Definition at line 348 of file qcssutil.cpp.

Referenced by QRenderRule::drawBorder(), and QRenderRule::drawOutline().

350 {
351  const QRectF br(rect);
352  QSize tlr, trr, blr, brr;
353  qNormalizeRadii(rect, radii, &tlr, &trr, &blr, &brr);
354 
355  // Drawn in increasing order of precendence
356  if (styles[BottomEdge] != BorderStyle_None && borders[BottomEdge] > 0) {
357  qreal dw1 = (blr.width() || paintsOver(styles, colors, BottomEdge, LeftEdge)) ? 0 : borders[LeftEdge];
358  qreal dw2 = (brr.width() || paintsOver(styles, colors, BottomEdge, RightEdge)) ? 0 : borders[RightEdge];
359  qreal x1 = br.x() + blr.width();
360  qreal y1 = br.y() + br.height() - borders[BottomEdge];
361  qreal x2 = br.x() + br.width() - brr.width();
362  qreal y2 = br.y() + br.height() ;
363 
364  qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, BottomEdge, styles[BottomEdge], colors[BottomEdge]);
365  if (blr.width() || brr.width())
366  qDrawRoundedCorners(p, x1, y1, x2, y2, blr, brr, BottomEdge, styles[BottomEdge], colors[BottomEdge]);
367  }
368  if (styles[RightEdge] != BorderStyle_None && borders[RightEdge] > 0) {
369  qreal dw1 = (trr.height() || paintsOver(styles, colors, RightEdge, TopEdge)) ? 0 : borders[TopEdge];
370  qreal dw2 = (brr.height() || paintsOver(styles, colors, RightEdge, BottomEdge)) ? 0 : borders[BottomEdge];
371  qreal x1 = br.x() + br.width() - borders[RightEdge];
372  qreal y1 = br.y() + trr.height();
373  qreal x2 = br.x() + br.width();
374  qreal y2 = br.y() + br.height() - brr.height();
375 
376  qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, RightEdge, styles[RightEdge], colors[RightEdge]);
377  if (trr.height() || brr.height())
378  qDrawRoundedCorners(p, x1, y1, x2, y2, trr, brr, RightEdge, styles[RightEdge], colors[RightEdge]);
379  }
380  if (styles[LeftEdge] != BorderStyle_None && borders[LeftEdge] > 0) {
381  qreal dw1 = (tlr.height() || paintsOver(styles, colors, LeftEdge, TopEdge)) ? 0 : borders[TopEdge];
382  qreal dw2 = (blr.height() || paintsOver(styles, colors, LeftEdge, BottomEdge)) ? 0 : borders[BottomEdge];
383  qreal x1 = br.x();
384  qreal y1 = br.y() + tlr.height();
385  qreal x2 = br.x() + borders[LeftEdge];
386  qreal y2 = br.y() + br.height() - blr.height();
387 
388  qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, LeftEdge, styles[LeftEdge], colors[LeftEdge]);
389  if (tlr.height() || blr.height())
390  qDrawRoundedCorners(p, x1, y1, x2, y2, tlr, blr, LeftEdge, styles[LeftEdge], colors[LeftEdge]);
391  }
392  if (styles[TopEdge] != BorderStyle_None && borders[TopEdge] > 0) {
393  qreal dw1 = (tlr.width() || paintsOver(styles, colors, TopEdge, LeftEdge)) ? 0 : borders[LeftEdge];
394  qreal dw2 = (trr.width() || paintsOver(styles, colors, TopEdge, RightEdge)) ? 0 : borders[RightEdge];
395  qreal x1 = br.x() + tlr.width();
396  qreal y1 = br.y();
397  qreal x2 = br.left() + br.width() - trr.width();
398  qreal y2 = br.y() + borders[TopEdge];
399 
400  qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, TopEdge, styles[TopEdge], colors[TopEdge]);
401  if (tlr.width() || trr.width())
402  qDrawRoundedCorners(p, x1, y1, x2, y2, tlr, trr, TopEdge, styles[TopEdge], colors[TopEdge]);
403  }
404 }
double qreal
Definition: qglobal.h:1193
void qNormalizeRadii(const QRect &br, const QSize *radii, QSize *tlr, QSize *trr, QSize *blr, QSize *brr)
Definition: qcssutil.cpp:316
void qDrawEdge(QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2, qreal dw1, qreal dw2, QCss::Edge edge, QCss::BorderStyle style, QBrush c)
Definition: qcssutil.cpp:187
int width() const
Returns the width.
Definition: qsize.h:126
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
void qDrawRoundedCorners(QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2, const QSizeF &r1, const QSizeF &r2, Edge edge, BorderStyle s, QBrush c)
Definition: qcssutil.cpp:82
int height() const
Returns the height.
Definition: qsize.h:129
static bool paintsOver(const QCss::BorderStyle *styles, const QBrush *colors, QCss::Edge e1, QCss::Edge e2)
Definition: qcssutil.cpp:334
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53

◆ qDrawEdge()

void qDrawEdge ( QPainter p,
qreal  x1,
qreal  y1,
qreal  x2,
qreal  y2,
qreal  dw1,
qreal  dw2,
QCss::Edge  edge,
QCss::BorderStyle  style,
QBrush  c 
)

Definition at line 187 of file qcssutil.cpp.

Referenced by QTextDocumentLayoutPrivate::drawBorder(), qDrawBorder(), and qDrawEdge().

189 {
190  p->save();
191  const qreal width = (edge == TopEdge || edge == BottomEdge) ? (y2-y1) : (x2-x1);
192 
193  if (width <= 2 && style == BorderStyle_Double)
194  style = BorderStyle_Solid;
195 
196  switch (style) {
197  case BorderStyle_Inset:
198  case BorderStyle_Outset:
199  if ((style == BorderStyle_Outset && (edge == TopEdge || edge == LeftEdge))
200  || (style == BorderStyle_Inset && (edge == BottomEdge || edge == RightEdge)))
201  c = c.color().lighter();
202  // fall through!
203  case BorderStyle_Solid: {
204  p->setPen(Qt::NoPen);
205  p->setBrush(c);
206  if (width == 1 || (dw1 == 0 && dw2 == 0)) {
207  p->drawRect(QRectF(x1, y1, x2-x1, y2-y1));
208  } else { // draw trapezoid
209  QPolygonF quad;
210  switch (edge) {
211  case TopEdge:
212  quad << QPointF(x1, y1) << QPointF(x1 + dw1, y2)
213  << QPointF(x2 - dw2, y2) << QPointF(x2, y1);
214  break;
215  case BottomEdge:
216  quad << QPointF(x1 + dw1, y1) << QPointF(x1, y2)
217  << QPointF(x2, y2) << QPointF(x2 - dw2, y1);
218  break;
219  case LeftEdge:
220  quad << QPointF(x1, y1) << QPointF(x1, y2)
221  << QPointF(x2, y2 - dw2) << QPointF(x2, y1 + dw1);
222  break;
223  case RightEdge:
224  quad << QPointF(x1, y1 + dw1) << QPointF(x1, y2 - dw2)
225  << QPointF(x2, y2) << QPointF(x2, y1);
226  break;
227  default:
228  break;
229  }
230  p->drawConvexPolygon(quad);
231  }
232  break;
233  }
234  case BorderStyle_Dotted:
235  case BorderStyle_Dashed:
236  case BorderStyle_DotDash:
238  p->setPen(qPenFromStyle(c, width, style));
239  if (width == 1)
240  p->drawLine(QLineF(x1, y1, x2 - 1, y2 - 1));
241  else if (edge == TopEdge || edge == BottomEdge)
242  p->drawLine(QLineF(x1 + width/2, (y1 + y2)/2, x2 - width/2, (y1 + y2)/2));
243  else
244  p->drawLine(QLineF((x1+x2)/2, y1 + width/2, (x1+x2)/2, y2 - width/2));
245  break;
246 
247  case BorderStyle_Double: {
248  int wby3 = qRound(width/3);
249  int dw1by3 = qRound(dw1/3);
250  int dw2by3 = qRound(dw2/3);
251  switch (edge) {
252  case TopEdge:
253  qDrawEdge(p, x1, y1, x2, y1 + wby3, dw1by3, dw2by3, TopEdge, BorderStyle_Solid, c);
254  qDrawEdge(p, x1 + dw1 - dw1by3, y2 - wby3, x2 - dw2 + dw1by3, y2,
255  dw1by3, dw2by3, TopEdge, BorderStyle_Solid, c);
256  break;
257  case LeftEdge:
258  qDrawEdge(p, x1, y1, x1 + wby3, y2, dw1by3, dw2by3, LeftEdge, BorderStyle_Solid, c);
259  qDrawEdge(p, x2 - wby3, y1 + dw1 - dw1by3, x2, y2 - dw2 + dw2by3, dw1by3, dw2by3,
261  break;
262  case BottomEdge:
263  qDrawEdge(p, x1 + dw1 - dw1by3, y1, x2 - dw2 + dw2by3, y1 + wby3, dw1by3, dw2by3,
265  qDrawEdge(p, x1, y2 - wby3, x2, y2, dw1by3, dw2by3, BottomEdge, BorderStyle_Solid, c);
266  break;
267  case RightEdge:
268  qDrawEdge(p, x2 - wby3, y1, x2, y2, dw1by3, dw2by3, RightEdge, BorderStyle_Solid, c);
269  qDrawEdge(p, x1, y1 + dw1 - dw1by3, x1 + wby3, y2 - dw2 + dw2by3, dw1by3, dw2by3,
271  break;
272  default:
273  break;
274  }
275  break;
276  }
277  case BorderStyle_Ridge:
278  case BorderStyle_Groove: {
279  BorderStyle s1, s2;
280  if (style == BorderStyle_Groove) {
281  s1 = BorderStyle_Inset;
282  s2 = BorderStyle_Outset;
283  } else {
284  s1 = BorderStyle_Outset;
285  s2 = BorderStyle_Inset;
286  }
287  int dw1by2 = qFloor(dw1/2), dw2by2 = qFloor(dw2/2);
288  int wby2 = qRound(width/2);
289  switch (edge) {
290  case TopEdge:
291  qDrawEdge(p, x1, y1, x2, y1 + wby2, dw1by2, dw2by2, TopEdge, s1, c);
292  qDrawEdge(p, x1 + dw1by2, y1 + wby2, x2 - dw2by2, y2, dw1by2, dw2by2, TopEdge, s2, c);
293  break;
294  case BottomEdge:
295  qDrawEdge(p, x1, y1 + wby2, x2, y2, dw1by2, dw2by2, BottomEdge, s1, c);
296  qDrawEdge(p, x1 + dw1by2, y1, x2 - dw2by2, y1 + wby2, dw1by2, dw2by2, BottomEdge, s2, c);
297  break;
298  case LeftEdge:
299  qDrawEdge(p, x1, y1, x1 + wby2, y2, dw1by2, dw2by2, LeftEdge, s1, c);
300  qDrawEdge(p, x1 + wby2, y1 + dw1by2, x2, y2 - dw2by2, dw1by2, dw2by2, LeftEdge, s2, c);
301  break;
302  case RightEdge:
303  qDrawEdge(p, x1 + wby2, y1, x2, y2, dw1by2, dw2by2, RightEdge, s1, c);
304  qDrawEdge(p, x1, y1 + dw1by2, x1 + wby2, y2 - dw2by2, dw1by2, dw2by2, RightEdge, s2, c);
305  break;
306  default:
307  break;
308  }
309  }
310  default:
311  break;
312  }
313  p->restore();
314 }
double qreal
Definition: qglobal.h:1193
const QColor & color() const
Returns the brush color.
Definition: qbrush.h:183
int qFloor(qreal v)
Definition: qmath.h:73
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
void restore()
Restores the current painter state (pops a saved state off the stack).
Definition: qpainter.cpp:1620
static QPen qPenFromStyle(const QBrush &b, qreal width, BorderStyle s)
Definition: qcssutil.cpp:53
void drawLine(const QLineF &line)
Draws a line defined by line.
Definition: qpainter.h:573
void save()
Saves the current painter state (pushes the state onto a stack).
Definition: qpainter.cpp:1590
The QLineF class provides a two-dimensional vector using floating point precision.
Definition: qline.h:212
void qDrawEdge(QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2, qreal dw1, qreal dw2, QCss::Edge edge, QCss::BorderStyle style, QBrush c)
Definition: qcssutil.cpp:187
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
void drawConvexPolygon(const QPointF *points, int pointCount)
Draws the convex polygon defined by the first pointCount points in the array points using the current...
Definition: qpainter.cpp:5484
The QPolygonF class provides a vector of points using floating point precision.
Definition: qpolygon.h:134
void setBrush(const QBrush &brush)
Sets the painter&#39;s brush to the given brush.
Definition: qpainter.cpp:4171
void setPen(const QColor &color)
Sets the painter&#39;s pen to have style Qt::SolidLine, width 0 and the specified color.
Definition: qpainter.cpp:4047
void drawRect(const QRectF &rect)
Draws the current rectangle with the current pen and brush.
Definition: qpainter.h:650
QColor lighter(int f=150) const
Returns a lighter (or darker) color, but does not change this object.
Definition: qcolor.h:298
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203

◆ qDrawRoundedCorners()

void qDrawRoundedCorners ( QPainter p,
qreal  x1,
qreal  y1,
qreal  x2,
qreal  y2,
const QSizeF r1,
const QSizeF r2,
QCss::Edge  edge,
QCss::BorderStyle  s,
QBrush  c 
)

◆ qNormalizeRadii()

void qNormalizeRadii ( const QRect br,
const QSize radii,
QSize tlr,
QSize trr,
QSize blr,
QSize brr 
)

Definition at line 316 of file qcssutil.cpp.

Referenced by QRenderRule::borderClip(), and qDrawBorder().

318 {
319  *tlr = radii[0].expandedTo(QSize(0, 0));
320  *trr = radii[1].expandedTo(QSize(0, 0));
321  *blr = radii[2].expandedTo(QSize(0, 0));
322  *brr = radii[3].expandedTo(QSize(0, 0));
323  if (tlr->width() + trr->width() > br.width())
324  *tlr = *trr = QSize(0, 0);
325  if (blr->width() + brr->width() > br.width())
326  *blr = *brr = QSize(0, 0);
327  if (tlr->height() + blr->height() > br.height())
328  *tlr = *blr = QSize(0, 0);
329  if (trr->height() + brr->height() > br.height())
330  *trr = *brr = QSize(0, 0);
331 }
QSize expandedTo(const QSize &) const
Returns a size holding the maximum width and height of this size and the given otherSize.
Definition: qsize.h:187
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
int width() const
Returns the width.
Definition: qsize.h:126
int height() const
Returns the height.
Definition: qsize.h:129
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53