source: trunk/src/qt3support/widgets/q3groupbox.cpp@ 219

Last change on this file since 219 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 24.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the Qt3Support module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "q3groupbox.h"
43
44#include "qlayout.h"
45#include "qpainter.h"
46#include "qbitmap.h"
47#include "q3accel.h"
48#include "qradiobutton.h"
49#include "qdrawutil.h"
50#include "qapplication.h"
51#include "qstyle.h"
52#include "qcheckbox.h"
53#include "qaccessible.h"
54#include "qstyleoption.h"
55#include "qdebug.h"
56
57QT_BEGIN_NAMESPACE
58
59/*!
60 \class Q3GroupBox
61 \brief The Q3GroupBox widget provides a group box frame with a title.
62
63 \compat
64
65 A group box provides a frame, a title and a keyboard shortcut, and
66 displays various other widgets inside itself. The title is on top,
67 the keyboard shortcut moves keyboard focus to one of the group
68 box's child widgets, and the child widgets are usually laid out
69 horizontally (or vertically) inside the frame.
70
71 The simplest way to use it is to create a group box with the
72 desired number of columns (or rows) and orientation, and then just
73 create widgets with the group box as parent.
74
75 It is also possible to change the orientation() and number of
76 columns() after construction, or to ignore all the automatic
77 layout support and manage the layout yourself. You can add 'empty'
78 spaces to the group box with addSpace().
79
80 Q3GroupBox also lets you set the title() (normally set in the
81 constructor) and the title's alignment().
82
83 You can change the spacing used by the group box with
84 setInsideMargin() and setInsideSpacing(). To minimize space
85 consumption, you can remove the right, left and bottom edges of
86 the frame with setFlat().
87
88 \sa QButtonGroup
89*/
90
91class QCheckBox;
92
93class Q3GroupBoxPrivate
94{
95public:
96 Q3GroupBoxPrivate(Q3GroupBox *w):
97 q(w), vbox(0), grid(0), row(0), col(0), nRows(0), nCols(0), dir(Qt::Horizontal),
98 spac(5), marg(11),
99 checkbox(0),
100 frameStyle(Q3GroupBox::GroupBoxPanel | Q3GroupBox::Sunken),
101 lineWidth(1), midLineWidth(0), frameWidth(0),
102 leftFrameWidth(0), rightFrameWidth(0),
103 topFrameWidth(0), bottomFrameWidth(0) {}
104
105 void updateFrameWidth();
106 void updateStyledFrameWidths();
107
108 Q3GroupBox *q;
109 QVBoxLayout *vbox;
110 QGridLayout *grid;
111 int row;
112 int col;
113 int nRows, nCols;
114 Qt::Orientation dir;
115 int spac, marg;
116
117 QCheckBox *checkbox;
118
119 int frameStyle;
120 int oldFrameStyle;
121 short lineWidth, //line width
122 midLineWidth; //midline width
123 int frameWidth;
124 short leftFrameWidth, rightFrameWidth,
125 topFrameWidth, bottomFrameWidth;
126};
127
128/*!
129 \internal
130 Updates the frame widths from the style.
131*/
132void Q3GroupBoxPrivate::updateStyledFrameWidths()
133{
134 QStyleOptionFrameV2 opt;
135 opt.initFrom(q);
136 QRect cr = q->style()->subElementRect(QStyle::SE_FrameContents, &opt, q);
137 leftFrameWidth = cr.left() - opt.rect.left();
138 topFrameWidth = cr.top() - opt.rect.top();
139 rightFrameWidth = opt.rect.right() - cr.right(),
140 bottomFrameWidth = opt.rect.bottom() - cr.bottom();
141 frameWidth = qMax(qMax(leftFrameWidth, rightFrameWidth),
142 qMax(topFrameWidth, bottomFrameWidth));
143}
144
145/*!
146 \internal
147 Updated the frameWidth parameter.
148*/
149
150void Q3GroupBoxPrivate::updateFrameWidth()
151{
152 QRect fr = q->frameRect();
153
154 int frameShape = frameStyle & QFrame::Shape_Mask;
155 int frameShadow = frameStyle & QFrame::Shadow_Mask;
156
157 frameWidth = -1;
158
159 switch (frameShape) {
160
161 case QFrame::NoFrame:
162 frameWidth = 0;
163 break;
164
165 case QFrame::Box:
166 case QFrame::HLine:
167 case QFrame::VLine:
168 switch (frameShadow) {
169 case QFrame::Plain:
170 frameWidth = lineWidth;
171 break;
172 case QFrame::Raised:
173 case QFrame::Sunken:
174 frameWidth = (short)(lineWidth*2 + midLineWidth);
175 break;
176 }
177 break;
178
179 case QFrame::StyledPanel:
180 updateStyledFrameWidths();
181 break;
182
183 case QFrame::WinPanel:
184 frameWidth = 2;
185 break;
186
187
188 case QFrame::Panel:
189 switch (frameShadow) {
190 case QFrame::Plain:
191 case QFrame::Raised:
192 case QFrame::Sunken:
193 frameWidth = lineWidth;
194 break;
195 }
196 break;
197 }
198
199 if (frameWidth == -1) // invalid style
200 frameWidth = 0;
201
202 q->setFrameRect(fr);
203}
204
205
206
207
208
209/*!
210 Constructs a group box widget with no title.
211
212 The \a parent and \a name arguments are passed to the QWidget
213 constructor.
214
215 This constructor does not do automatic layout.
216*/
217
218Q3GroupBox::Q3GroupBox(QWidget *parent, const char *name)
219 : QGroupBox(parent, name)
220{
221 init();
222}
223
224/*!
225 Constructs a group box with the title \a title.
226
227 The \a parent and \a name arguments are passed to the QWidget
228 constructor.
229
230 This constructor does not do automatic layout.
231*/
232
233Q3GroupBox::Q3GroupBox(const QString &title, QWidget *parent, const char *name)
234 : QGroupBox(parent, name)
235{
236 init();
237 setTitle(title);
238}
239
240/*!
241 Constructs a group box with no title. Child widgets will be
242 arranged in \a strips rows or columns (depending on \a
243 orientation).
244
245 The \a parent and \a name arguments are passed to the QWidget
246 constructor.
247*/
248
249Q3GroupBox::Q3GroupBox(int strips, Qt::Orientation orientation,
250 QWidget *parent, const char *name)
251 : QGroupBox(parent, name)
252{
253 init();
254 setColumnLayout(strips, orientation);
255}
256
257/*!
258 Constructs a group box titled \a title. Child widgets will be
259 arranged in \a strips rows or columns (depending on \a
260 orientation).
261
262 The \a parent and \a name arguments are passed to the QWidget
263 constructor.
264*/
265
266Q3GroupBox::Q3GroupBox(int strips, Qt::Orientation orientation,
267 const QString &title, QWidget *parent,
268 const char *name)
269 : QGroupBox(parent, name)
270{
271 init();
272 setTitle(title);
273 setColumnLayout(strips, orientation);
274}
275
276/*!
277 Destroys the group box.
278*/
279Q3GroupBox::~Q3GroupBox()
280{
281 delete d;
282}
283
284void Q3GroupBox::init()
285{
286 d = new Q3GroupBoxPrivate(this);
287}
288
289
290/*! \reimp
291*/
292void Q3GroupBox::resizeEvent(QResizeEvent *e)
293{
294 QGroupBox::resizeEvent(e);
295}
296
297
298/*!
299 Adds an empty cell at the next free position. If \a size is
300 greater than 0, the empty cell takes \a size to be its fixed width
301 (if orientation() is \c Horizontal) or height (if orientation() is
302 \c Vertical).
303
304 Use this method to separate the widgets in the group box or to
305 skip the next free cell. For performance reasons, call this method
306 after calling setColumnLayout() or by changing the \l
307 Q3GroupBox::columns or \l Q3GroupBox::orientation properties. It is
308 generally a good idea to call these methods first (if needed at
309 all), and insert the widgets and spaces afterwards.
310*/
311void Q3GroupBox::addSpace(int size)
312{
313 QApplication::sendPostedEvents(this, QEvent::ChildInserted);
314
315 if (d->nCols <= 0 || d->nRows <= 0)
316 return;
317
318 if (d->row >= d->nRows || d->col >= d->nCols)
319 d->grid->expand(d->row+1, d->col+1);
320
321 if (size > 0) {
322 QSpacerItem *spacer
323 = new QSpacerItem((d->dir == Qt::Horizontal) ? 0 : size,
324 (d->dir == Qt::Vertical) ? 0 : size,
325 QSizePolicy::Fixed, QSizePolicy::Fixed);
326 d->grid->addItem(spacer, d->row, d->col);
327 }
328
329 skip();
330}
331
332/*!
333 \property Q3GroupBox::columns
334 \brief the number of columns or rows (depending on \l Q3GroupBox::orientation) in the group box
335
336 Usually it is not a good idea to set this property because it is
337 slow (it does a complete layout). It is best to set the number
338 of columns directly in the constructor.
339*/
340int Q3GroupBox::columns() const
341{
342 if (d->dir == Qt::Horizontal)
343 return d->nCols;
344 return d->nRows;
345}
346
347void Q3GroupBox::setColumns(int c)
348{
349 setColumnLayout(c, d->dir);
350}
351
352/*!
353 Returns the width of the empty space between the items in the
354 group and the frame of the group.
355
356 Only applies if the group box has a defined orientation.
357
358 The default is usually 11, by may vary depending on the platform
359 and style.
360
361 \sa setInsideMargin(), orientation
362*/
363int Q3GroupBox::insideMargin() const
364{
365 return d->marg;
366}
367
368/*!
369 Returns the width of the empty space between each of the items
370 in the group.
371
372 Only applies if the group box has a defined orientation.
373
374 The default is usually 5, by may vary depending on the platform
375 and style.
376
377 \sa setInsideSpacing(), orientation
378*/
379int Q3GroupBox::insideSpacing() const
380{
381 return d->spac;
382}
383
384/*!
385 Sets the the width of the inside margin to \a m pixels.
386
387 \sa insideMargin()
388*/
389void Q3GroupBox::setInsideMargin(int m)
390{
391 d->marg = m;
392 setColumnLayout(columns(), d->dir);
393}
394
395/*!
396 Sets the width of the empty space between each of the items in
397 the group to \a s pixels.
398
399 \sa insideSpacing()
400*/
401void Q3GroupBox::setInsideSpacing(int s)
402{
403 d->spac = s;
404 setColumnLayout(columns(), d->dir);
405}
406
407/*!
408 \property Q3GroupBox::orientation
409 \brief the group box's orientation