Qt 4.8
qframe.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qframe.h"
43 #include "qbitmap.h"
44 #include "qdrawutil.h"
45 #include "qevent.h"
46 #include "qpainter.h"
47 #include "qstyle.h"
48 #include "qstyleoption.h"
49 #include "qapplication.h"
50 
51 #include "qframe_p.h"
52 
54 
56  : frect(QRect(0, 0, 0, 0)),
57  frameStyle(QFrame::NoFrame | QFrame::Plain),
58  lineWidth(1),
59  midLineWidth(0),
60  frameWidth(0),
61  leftFrameWidth(0), rightFrameWidth(0),
62  topFrameWidth(0), bottomFrameWidth(0)
63 {
64 }
65 
66 inline void QFramePrivate::init()
67 {
69 }
70 
219 QFrame::QFrame(QWidget* parent, Qt::WindowFlags f)
220  : QWidget(*new QFramePrivate, parent, f)
221 {
222  Q_D(QFrame);
223  d->init();
224 }
225 
227 QFrame::QFrame(QFramePrivate &dd, QWidget* parent, Qt::WindowFlags f)
228  : QWidget(dd, parent, f)
229 {
230  Q_D(QFrame);
231  d->init();
232 }
233 
234 #ifdef QT3_SUPPORT
235 
239 QFrame::QFrame(QWidget *parent, const char *name, Qt::WindowFlags f)
240  : QWidget(*new QFramePrivate, parent, f)
241 {
242  Q_D(QFrame);
244  d->init();
245 }
246 #endif
247 
252 {
253 }
254 
263 {
264  Q_D(const QFrame);
265  return d->frameStyle;
266 }
267 
279 {
280  Q_D(const QFrame);
281  return (Shape) (d->frameStyle & Shape_Mask);
282 }
283 
285 {
286  Q_D(QFrame);
287  setFrameStyle((d->frameStyle & Shadow_Mask) | s);
288 }
289 
290 
301 {
302  Q_D(const QFrame);
303  return (Shadow) (d->frameStyle & Shadow_Mask);
304 }
305 
307 {
308  Q_D(QFrame);
309  setFrameStyle((d->frameStyle & Shape_Mask) | s);
310 }
311 
330 {
331  Q_D(QFrame);
333  QSizePolicy sp;
334 
335  switch (style & Shape_Mask) {
336  case HLine:
338  break;
339  case VLine:
341  break;
342  default:
344  }
345  setSizePolicy(sp);
347  }
348  d->frameStyle = (short)style;
349  update();
350  d->updateFrameWidth();
351 }
352 
369 {
370  Q_D(QFrame);
371  if (short(w) == d->lineWidth)
372  return;
373  d->lineWidth = short(w);
374  d->updateFrameWidth();
375 }
376 
377 int QFrame::lineWidth() const
378 {
379  Q_D(const QFrame);
380  return d->lineWidth;
381 }
382 
396 {
397  Q_D(QFrame);
398  if (short(w) == d->midLineWidth)
399  return;
400  d->midLineWidth = short(w);
401  d->updateFrameWidth();
402 }
403 
404 int QFrame::midLineWidth() const
405 {
406  Q_D(const QFrame);
407  return d->midLineWidth;
408 }
409 
418 {
419  Q_Q(const QFrame);
421  opt.initFrom(q);
422  opt.lineWidth = lineWidth;
425 
426  QRect cr = q->style()->subElementRect(QStyle::SE_ShapedFrameContents, &opt, q);
427  leftFrameWidth = cr.left() - opt.rect.left();
428  topFrameWidth = cr.top() - opt.rect.top();
429  rightFrameWidth = opt.rect.right() - cr.right(),
430  bottomFrameWidth = opt.rect.bottom() - cr.bottom();
431  frameWidth = qMax(qMax(leftFrameWidth, rightFrameWidth),
432  qMax(topFrameWidth, bottomFrameWidth));
433 }
434 
444 {
445  Q_Q(QFrame);
446  QRect fr = q->frameRect();
447  updateStyledFrameWidths();
448  q->setFrameRect(fr);
450 }
451 
466 int QFrame::frameWidth() const
467 {
468  Q_D(const QFrame);
469  return d->frameWidth;
470 }
471 
472 
490 QRect QFrame::frameRect() const
491 {
492  Q_D(const QFrame);
493  QRect fr = contentsRect();
494  fr.adjust(-d->leftFrameWidth, -d->topFrameWidth, d->rightFrameWidth, d->bottomFrameWidth);
495  return fr;
496 }
497 
499 {
500  Q_D(QFrame);
501  QRect cr = r.isValid() ? r : rect();
502  cr.adjust(d->leftFrameWidth, d->topFrameWidth, -d->rightFrameWidth, -d->bottomFrameWidth);
503  setContentsMargins(cr.left(), cr.top(), rect().right() - cr.right(), rect().bottom() - cr.bottom());
504 }
505 
509 {
510  Q_D(const QFrame);
511  // Returns a size hint for the frame - for HLine and VLine
512  // shapes, this is stretchable one way and 3 pixels wide the
513  // other. For other shapes, QWidget::sizeHint() is used.
514  switch (d->frameStyle & Shape_Mask) {
515  case HLine:
516  return QSize(-1,3);
517  case VLine:
518  return QSize(3,-1);
519  default:
520  return QWidget::sizeHint();
521  }
522 }
523 
528 {
529  QPainter paint(this);
530  drawFrame(&paint);
531 }
532 
539 {
540  Q_D(QFrame);
542  opt.init(this);
543  int frameShape = d->frameStyle & QFrame::Shape_Mask;
544  int frameShadow = d->frameStyle & QFrame::Shadow_Mask;
545  opt.frameShape = Shape(int(opt.frameShape) | frameShape);
546  opt.rect = frameRect();
547  switch (frameShape) {
548  case QFrame::Box:
549  case QFrame::HLine:
550  case QFrame::VLine:
551  case QFrame::StyledPanel:
552  case QFrame::Panel:
553  opt.lineWidth = d->lineWidth;
554  opt.midLineWidth = d->midLineWidth;
555  break;
556  default:
557  // most frame styles do not handle customized line and midline widths
558  // (see updateFrameWidth()).
559  opt.lineWidth = d->frameWidth;
560  break;
561  }
562 
563  if (frameShadow == Sunken)
565  else if (frameShadow == Raised)
567 
568  style()->drawControl(QStyle::CE_ShapedFrame, &opt, p, this);
569 }
570 
571 
575 {
576  Q_D(QFrame);
577  if (ev->type() == QEvent::StyleChange
578 #ifdef Q_WS_MAC
579  || ev->type() == QEvent::MacSizeChange
580 #endif
581  )
582  d->updateFrameWidth();
584 }
585 
588 {
589  if (e->type() == QEvent::ParentChange)
590  d_func()->updateFrameWidth();
591  bool result = QWidget::event(e);
592  //this has to be done after the widget has been polished
593  if (e->type() == QEvent::Polish)
594  d_func()->updateFrameWidth();
595  return result;
596 }
597 
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
double d
Definition: qnumeric_p.h:62
void paintEvent(QPaintEvent *)
This event handler can be reimplemented in a subclass to receive paint events passed in event...
Definition: qframe.cpp:527
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
QRect contentsRect() const
Returns the area inside the widget's margins.
Definition: qwidget.cpp:7544
int lineWidth() const
QFrame::Shape frameShape
This property holds the frame shape value of the frame.
Definition: qstyleoption.h:161
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
int left() const
Returns the x-coordinate of the rectangle's left edge.
Definition: qrect.h:240
void init(const QWidget *w)
Use initFrom(widget) instead.
~QFrame()
Destroys the frame.
Definition: qframe.cpp:251
void updateFrameWidth()
Updated the frameWidth parameter.
Definition: qframe.cpp:443
void setMidLineWidth(int)
Definition: qframe.cpp:395
int frameWidth() const
int frameStyle() const
Returns the frame style.
Definition: qframe.cpp:262
int bottom() const
Returns the y-coordinate of the rectangle's bottom edge.
Definition: qrect.h:249
void setLineWidth(int)
Definition: qframe.cpp:368
void setLayoutItemMargins(int left, int top, int right, int bottom)
Definition: qwidget.cpp:12860
#define Q_D(Class)
Definition: qglobal.h:2482
Shape
This enum type defines the shapes of frame available.
Definition: qframe.h:78
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
virtual QSize sizeHint() const
QStyle * style() const
Definition: qwidget.cpp:2742
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
#define Q_Q(Class)
Definition: qglobal.h:2483
QFrame(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a frame widget with frame style NoFrame and a 1-pixel frame width.
Definition: qframe.cpp:219
void update()
Updates the widget unless updates are disabled or the widget is hidden.
Definition: qwidget.cpp:10883
int lineWidth
the line width for drawing the frame
Definition: qstyleoption.h:124
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void changeEvent(QEvent *)
This event handler can be reimplemented to handle state changes.
Definition: qframe.cpp:574
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
QWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a widget which is a child of parent, with widget flags set to f.
Definition: qwidget.cpp:1189
Shadow
This enum type defines the types of shadow that are used to give a 3D effect to frames.
Definition: qframe.h:96
void setContentsMargins(int left, int top, int right, int bottom)
Sets the margins around the contents of the widget to have the sizes left, top, right, and bottom.
Definition: qwidget.cpp:7449
void initFrom(const QWidget *w)
Definition: qstyleoption.h:99
const char * name
virtual void changeEvent(QEvent *)
This event handler can be reimplemented to handle state changes.
Definition: qwidget.cpp:9170
void adjust(int x1, int y1, int x2, int y2)
Adds dx1, dy1, dx2 and dy2 respectively to the existing coordinates of the rectangle.
Definition: qrect.h:434
void drawFrame(QPainter *)
em>Reimplemented Function
Definition: qframe.cpp:538
void setFrameShape(Shape)
Definition: qframe.cpp:284
Shadow frameShadow() const
Shape frameShape() const
QRect rect() const
void setFrameShadow(Shadow)
Definition: qframe.cpp:306
int top() const
Returns the y-coordinate of the rectangle's top edge.
Definition: qrect.h:243
int right() const
Returns the x-coordinate of the rectangle's right edge.
Definition: qrect.h:246
QRect frameRect() const
int midLineWidth
the mid-line width for drawing the frame
Definition: qstyleoption.h:125
void init()
Definition: qframe.cpp:66
The QStyleOptionFrameV3 class is used to describe the parameters necessary for drawing a frame in Qt ...
Definition: qstyleoption.h:157
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void setFrameStyle(int)
Sets the frame style to style.
Definition: qframe.cpp:329
bool event(QEvent *e)
Reimplemented Function
Definition: qframe.cpp:587
virtual void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w=0) const =0
Draws the given element with the provided painter with the style options specified by option...
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
void setFrameRect(const QRect &)
em>Reimplemented Function
Definition: qframe.cpp:498
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QObject * parent
Definition: qobject.h:92
QSize sizeHint() const
em>Reimplemented Function
Definition: qframe.cpp:508
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
int midLineWidth() const
bool event(QEvent *)
This is the main event handler; it handles event event.
Definition: qwidget.cpp:8636
bool isValid() const
Returns true if the rectangle is valid, otherwise returns false.
Definition: qrect.h:237
The QPaintEvent class contains event parameters for paint events.
Definition: qevent.h:298
void setSizePolicy(QSizePolicy)
Definition: qwidget.cpp:10198
static void setLayoutItemMargins(int left, int top, int right, int bottom, QRect *rect, Qt::LayoutDirection dir)
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
The QFrame class is the base class of widgets that can have a frame.
Definition: qframe.h:55
QRect rect
the area that should be used for various calculations and painting
Definition: qstyleoption.h:90
void updateStyledFrameWidths()
Updates the frame widths from the style.
Definition: qframe.cpp:417