1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the widgets module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include "qdrawutil.h"
35
#include "qstyleoption.h"
39
QFramePrivate::QFramePrivate()
40
: frect(QRect(0, 0, 0, 0)),
41
frameStyle(QFrame::NoFrame | QFrame::Plain),
50
\brief The QFrame class is the base class of widgets that can have a frame.
52
\ingroup abstractwidgets
55
QMenu uses this to "raise" the menu above the surrounding
56
screen. QProgressBar has a "sunken" look. QLabel has a flat look.
57
The frames of widgets like these can be changed.
61
label.setFrameStyle(QFrame::Panel | QFrame::Raised);
62
label.setLineWidth(2);
64
QProgressBar pbar(...);
65
label.setFrameStyle(QFrame::NoFrame);
68
The QFrame class can also be used directly for creating simple
69
frames without any contents, although usually you would use a
70
QHBox or QVBox because they automatically lay out the widgets you
73
A frame widget has four attributes: frameStyle(), lineWidth(),
74
midLineWidth(), and margin().
76
The frame style is specified by a \link QFrame::Shape frame
77
shape\endlink and a \link QFrame::Shadow shadow style\endlink. The
78
frame shapes are \c NoFrame, \c Box, \c Panel, \c StyledPanel,
79
HLine and \c VLine; the shadow styles are \c Plain, \c Raised and
82
The line width is the width of the frame border.
84
The mid-line width specifies the width of an extra line in the
85
middle of the frame, which uses a third color to obtain a special
86
3D effect. Notice that a mid-line is only drawn for \c Box, \c
87
HLine and \c VLine frames that are raised or sunken.
89
The margin is the gap between the frame and the contents of the
93
This table shows the most useful combinations of styles and widths
94
(and some rather useless ones):
96
\img frames.png Table of frame styles
103
This enum type defines the shapes of a QFrame's frame.
105
\value NoFrame QFrame draws nothing
106
\value Box QFrame draws a box around its contents
107
\value Panel QFrame draws a panel to make the contents appear
109
\value StyledPanel draws a rectangular panel with a look that
110
depends on the current GUI style. It can be raised or sunken.
111
\value HLine QFrame draws a horizontal line that frames nothing
112
(useful as separator)
113
\value VLine QFrame draws a vertical line that frames nothing
114
(useful as separator)
115
\value WinPanel draws a rectangular panel that can be raised or
116
sunken like those in Windows 95. Specifying this shape sets
117
the line width to 2 pixels. WinPanel is provided for compatibility.
118
For GUI style independence we recommend using StyledPanel instead.
120
\omitvalue GroupBoxPanel
121
\omitvalue ToolBarPanel
122
\omitvalue MenuBarPanel
123
\omitvalue PopupPanel
124
\omitvalue LineEditPanel
125
\omitvalue TabWidgetPanel
127
When it does not call QStyle, Shape interacts with QFrame::Shadow,
128
the lineWidth() and the midLineWidth() to create the total result.
129
See the picture of the frames in the main class documentation.
131
\sa QFrame::Shadow QFrame::style() QStyle::drawPrimitive()
138
This enum type defines the 3D effect used for QFrame's frame.
140
\value Plain the frame and contents appear level with the
141
surroundings; draws using the palette foreground color (without
143
\value Raised the frame and contents appear raised; draws a 3D
144
raised line using the light and dark colors of the current color
146
\value Sunken the frame and contents appear sunken; draws a 3D
147
sunken line using the light and dark colors of the current color
150
Shadow interacts with QFrame::Shape, the lineWidth() and the
151
midLineWidth(). See the picture of the frames in the main class
154
\sa QFrame::Shape lineWidth() midLineWidth()
158
\variable QFrame::Shadow_Mask
160
The mask of the QFrame's shadow.
162
\sa QFrame::frameShadow
166
\variable QFrame::Shape_Mask
168
The mask of the QFrame's shape.
170
\sa QFrame::frameShape
174
Constructs a frame widget with frame style \c NoFrame and a
177
The \a parent and \a f arguments are passed to the QWidget
181
QFrame::QFrame(QWidget* parent, Qt::WFlags f)
182
: QWidget(*new QFramePrivate, parent, f)
187
QFrame::QFrame(QFramePrivate &dd, QWidget* parent, Qt::WFlags f)
188
: QWidget(dd, parent, f)
194
Use one of the constructors that doesn't take the \a name
195
argument and then use setObjectName() instead.
197
QFrame::QFrame(QWidget *parent, const char *name, Qt::WFlags f)
198
: QWidget(*new QFramePrivate, parent, f)
212
Returns the frame style.
214
The default value is QFrame::NoFrame.
216
\sa setFrameStyle(), frameShape(), frameShadow()
218
int QFrame::frameStyle() const
221
return d->frameStyle;
225
\property QFrame::frameShape
226
\brief the frame shape value from the frame style
228
\sa frameStyle(), frameShadow()
231
QFrame::Shape QFrame::frameShape() const
234
return (Shape) (d->frameStyle & Shape_Mask);
237
void QFrame::setFrameShape(QFrame::Shape s)
240
setFrameStyle((d->frameStyle & Shadow_Mask) | s);
245
\property QFrame::frameShadow
246
\brief the frame shadow value from the frame style
248
\sa frameStyle(), frameShape()
250
QFrame::Shadow QFrame::frameShadow() const
253
return (Shadow) (d->frameStyle & Shadow_Mask);
256
void QFrame::setFrameShadow(QFrame::Shadow s)
259
setFrameStyle((d->frameStyle & Shape_Mask) | s);
263
Sets the frame style to \a style.
265
The \a style is the bitwise OR between a frame shape and a frame
266
shadow style. See the picture of the frames in the main class
269
The frame shapes are given in \l{QFrame::Shape} and the shadow
270
styles in \l{QFrame::Shadow}.
272
If a mid-line width greater than 0 is specified, an additional
273
line is drawn for \c Raised or \c Sunken \c Box, \c HLine, and \c
274
VLine frames. The mid-color of the current color group is used for
275
drawing middle lines.
280
void QFrame::setFrameStyle(int style)
283
if (!testAttribute(Qt::WA_WState_OwnSizePolicy)) {
284
switch (style & Shape_Mask) {
286
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
289
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
292
if ((d->frameStyle & Shape_Mask) == HLine || (d->frameStyle & Shape_Mask) == VLine)
293
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
295
setAttribute(Qt::WA_WState_OwnSizePolicy, false);
297
d->frameStyle = (short)style;
299
d->updateFrameWidth();
303
\property QFrame::lineWidth
304
\brief the line width
306
Note that the \e total line width for \c HLine and \c VLine is
307
specified by frameWidth(), not lineWidth().
309
The default value is 1.
311
\sa midLineWidth(), frameWidth()
314
void QFrame::setLineWidth(int w)
317
d->lineWidth = (short)w;
318
d->updateFrameWidth();
321
int QFrame::lineWidth() const
328
\property QFrame::midLineWidth
329
\brief the width of the mid-line
331
The default value is 0.
333
\sa lineWidth(), frameWidth()
336
void QFrame::setMidLineWidth(int w)
339
d->midLineWidth = (short)w;
340
d->updateFrameWidth();
343
int QFrame::midLineWidth() const
346
return d->midLineWidth;
352
Updated the frameWidth parameter.
355
void QFramePrivate::updateFrameWidth()
358
QRect fr = q->frameRect();
360
int frameShape = frameStyle & QFrame::Shape_Mask;
361
int frameShadow = frameStyle & QFrame::Shadow_Mask;
365
switch (frameShape) {
367
case QFrame::NoFrame:
374
switch (frameShadow) {
376
frameWidth = lineWidth;
380
frameWidth = (short)(lineWidth*2 + midLineWidth);
385
case QFrame::StyledPanel:
386
frameWidth = q->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, q);
389
case QFrame::WinPanel:
395
switch (frameShadow) {
399
frameWidth = lineWidth;
405
if (frameWidth == -1) // invalid style
413
\property QFrame::frameWidth
414
\brief the width of the frame that is drawn.
416
Note that the frame width depends on the \link
417
QFrame::setFrameStyle() frame style \endlink, not only the line
418
width and the mid-line width. For example, the style \c NoFrame
419
always has a frame width of 0, whereas the style \c Panel has a
420
frame width equivalent to the line width.
422
\sa lineWidth(), midLineWidth(), frameStyle()
424
int QFrame::frameWidth() const
427
return d->frameWidth;
432
\property QFrame::frameRect
433
\brief the frame's rectangle
435
The frame's rectangle is the rectangle the frame is drawn in. By
436
default, this is the entire widget. Setting the rectangle does
437
does \e not cause a widget update. The frame rectangle is
438
automatically adjusted when the widget changes size.
440
If you set the rectangle to a null rectangle (for example
441
\c{QRect(0, 0, 0, 0)}), then the resulting frame rectangle is
442
equivalent to the \link QWidget::rect() widget rectangle\endlink.
445
QRect QFrame::frameRect() const
448
QRect fr = contentsRect();
449
fr.adjust(-d->frameWidth, -d->frameWidth, d->frameWidth, d->frameWidth);
453
void QFrame::setFrameRect(const QRect &r)
456
QRect cr = r.isValid() ? r : rect();
457
cr.adjust(d->frameWidth, d->frameWidth, -d->frameWidth, -d->frameWidth);
458
setContentsMargins(cr.left(), cr.top(), rect().right() - cr.right(), rect().bottom() - cr.bottom());
463
QSize QFrame::sizeHint() const
466
// Returns a size hint for the frame - for HLine and VLine
467
// shapes, this is stretchable one way and 3 pixels wide the
468
// other. For other shapes, QWidget::sizeHint() is used.
469
switch (d->frameStyle & Shape_Mask) {
475
return QWidget::sizeHint();
482
void QFrame::paintEvent(QPaintEvent *)
484
QPainter paint(this);
491
Mostly for the sake of Q3Frame
493
void QFrame::drawFrame(QPainter *p)
497
QStyleOptionFrame opt;
499
int frameShape = d->frameStyle & QFrame::Shape_Mask;
500
int frameShadow = d->frameStyle & QFrame::Shadow_Mask;
504
opt.rect = frameRect();
505
switch (frameShape) {
509
case QFrame::StyledPanel:
511
mlw = d->midLineWidth;
514
// most frame styles do not handle customized line and midline widths
515
// (see updateFrameWidth()).
520
opt.midLineWidth = mlw;
521
if (frameShadow == Sunken)
522
opt.state |= QStyle::State_Sunken;
523
else if (frameShadow == Raised)
524
opt.state |= QStyle::State_Raised;
526
switch (frameShape) {
528
if (frameShadow == Plain)
529
qDrawPlainRect(p, opt.rect, opt.palette.foreground().color(), lw);
531
qDrawShadeRect(p, opt.rect, opt.palette, frameShadow == Sunken, lw, mlw);
536
style()->drawPrimitive(QStyle::PE_Frame, &opt, p, this);
540
if (frameShadow == Plain)
541
qDrawPlainRect(p, opt.rect, opt.palette.foreground().color(), lw);
543
qDrawShadePanel(p, opt.rect, opt.palette, frameShadow == Sunken, lw);
547
if (frameShadow == Plain)
548
qDrawPlainRect(p, opt.rect, opt.palette.foreground().color(), lw);
550
qDrawWinPanel(p, opt.rect, opt.palette, frameShadow == Sunken);
554
if (frameShape == HLine) {
555
p1 = QPoint(opt.rect.x(), opt.rect.height() / 2);
556
p2 = QPoint(opt.rect.x() + opt.rect.width(), p1.y());
558
p1 = QPoint(opt.rect.x()+opt.rect.width() / 2, 0);
559
p2 = QPoint(p1.x(), opt.rect.height());
561
if (frameShadow == Plain) {
562
QPen oldPen = p->pen();
563
p->setPen(QPen(opt.palette.foreground().color(), lw));
567
qDrawShadeLine(p, p1, p2, opt.palette, frameShadow == Sunken, lw, mlw);
576
void QFrame::changeEvent(QEvent *ev)
579
if(ev->type() == QEvent::StyleChange)
580
d->updateFrameWidth();
581
QWidget::changeEvent(ev);