📄 q3groupbox.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "q3groupbox.h"#include "qlayout.h"#include "qpainter.h"#include "qbitmap.h"#include "q3accel.h"#include "qradiobutton.h"#include "qdrawutil.h"#include "qapplication.h"#include "qstyle.h"#include "qcheckbox.h"#include "qaccessible.h"#include "qstyleoption.h"#include "qdebug.h"/*! \class Q3GroupBox \brief The Q3GroupBox widget provides a group box frame with a title. \compat A group box provides a frame, a title and a keyboard shortcut, and displays various other widgets inside itself. The title is on top, the keyboard shortcut moves keyboard focus to one of the group box's child widgets, and the child widgets are usually laid out horizontally (or vertically) inside the frame. The simplest way to use it is to create a group box with the desired number of columns (or rows) and orientation, and then just create widgets with the group box as parent. It is also possible to change the orientation() and number of columns() after construction, or to ignore all the automatic layout support and manage the layout yourself. You can add 'empty' spaces to the group box with addSpace(). Q3GroupBox also lets you set the title() (normally set in the constructor) and the title's alignment(). You can change the spacing used by the group box with setInsideMargin() and setInsideSpacing(). To minimize space consumption, you can remove the right, left and bottom edges of the frame with setFlat(). \sa QButtonGroup*/class QCheckBox;class Q3GroupBoxPrivate{public: Q3GroupBoxPrivate(Q3GroupBox *w): q(w), vbox(0), grid(0), row(0), col(0), nRows(0), nCols(0), dir(Qt::Horizontal), spac(5), marg(11), checkbox(0), frameStyle(Q3GroupBox::GroupBoxPanel | Q3GroupBox::Sunken), lineWidth(1), midLineWidth(0), frameWidth(0), leftFrameWidth(0), rightFrameWidth(0), topFrameWidth(0), bottomFrameWidth(0) {} void updateFrameWidth(); void updateStyledFrameWidths(); Q3GroupBox *q; QVBoxLayout *vbox; QGridLayout *grid; int row; int col; int nRows, nCols; Qt::Orientation dir; int spac, marg; QCheckBox *checkbox; int frameStyle; int oldFrameStyle; short lineWidth, //line width midLineWidth; //midline width int frameWidth; short leftFrameWidth, rightFrameWidth, topFrameWidth, bottomFrameWidth;};/*! \internal Updates the frame widths from the style.*/void Q3GroupBoxPrivate::updateStyledFrameWidths(){ QStyleOptionFrameV2 opt; opt.initFrom(q); QRect cr = q->style()->subElementRect(QStyle::SE_FrameContents, &opt, q); leftFrameWidth = cr.left() - opt.rect.left(); topFrameWidth = cr.top() - opt.rect.top(); rightFrameWidth = opt.rect.right() - cr.right(), bottomFrameWidth = opt.rect.bottom() - cr.bottom(); frameWidth = qMax(qMax(leftFrameWidth, rightFrameWidth), qMax(topFrameWidth, bottomFrameWidth));}/*! \internal Updated the frameWidth parameter.*/void Q3GroupBoxPrivate::updateFrameWidth(){ QRect fr = q->frameRect(); int frameShape = frameStyle & QFrame::Shape_Mask; int frameShadow = frameStyle & QFrame::Shadow_Mask; frameWidth = -1; switch (frameShape) { case QFrame::NoFrame: frameWidth = 0; break; case QFrame::Box: case QFrame::HLine: case QFrame::VLine: switch (frameShadow) { case QFrame::Plain: frameWidth = lineWidth; break; case QFrame::Raised: case QFrame::Sunken: frameWidth = (short)(lineWidth*2 + midLineWidth); break; } break; case QFrame::StyledPanel: updateStyledFrameWidths(); break; case QFrame::WinPanel: frameWidth = 2; break; case QFrame::Panel: switch (frameShadow) { case QFrame::Plain: case QFrame::Raised: case QFrame::Sunken: frameWidth = lineWidth; break; } break; } if (frameWidth == -1) // invalid style frameWidth = 0; q->setFrameRect(fr);}/*! Constructs a group box widget with no title. The \a parent and \a name arguments are passed to the QWidget constructor. This constructor does not do automatic layout.*/Q3GroupBox::Q3GroupBox(QWidget *parent, const char *name) : QGroupBox(parent, name){ init();}/*! Constructs a group box with the title \a title. The \a parent and \a name arguments are passed to the QWidget constructor. This constructor does not do automatic layout.*/Q3GroupBox::Q3GroupBox(const QString &title, QWidget *parent, const char *name) : QGroupBox(parent, name){ init(); setTitle(title);}/*! Constructs a group box with no title. Child widgets will be arranged in \a strips rows or columns (depending on \a orientation). The \a parent and \a name arguments are passed to the QWidget constructor.*/Q3GroupBox::Q3GroupBox(int strips, Qt::Orientation orientation, QWidget *parent, const char *name) : QGroupBox(parent, name){ init(); setColumnLayout(strips, orientation);}/*! Constructs a group box titled \a title. Child widgets will be arranged in \a strips rows or columns (depending on \a orientation). The \a parent and \a name arguments are passed to the QWidget constructor.*/Q3GroupBox::Q3GroupBox(int strips, Qt::Orientation orientation, const QString &title, QWidget *parent, const char *name) : QGroupBox(parent, name){ init(); setTitle(title); setColumnLayout(strips, orientation);}/*! Destroys the group box.*/Q3GroupBox::~Q3GroupBox(){ delete d;}void Q3GroupBox::init(){ d = new Q3GroupBoxPrivate(this);}/*! \reimp*/void Q3GroupBox::resizeEvent(QResizeEvent *e){ QGroupBox::resizeEvent(e);}/*! Adds an empty cell at the next free position. If \a size is greater than 0, the empty cell takes \a size to be its fixed width (if orientation() is \c Horizontal) or height (if orientation() is \c Vertical). Use this method to separate the widgets in the group box or to skip the next free cell. For performance reasons, call this method after calling setColumnLayout() or by changing the \l Q3GroupBox::columns or \l Q3GroupBox::orientation properties. It is generally a good idea to call these methods first (if needed at all), and insert the widgets and spaces afterwards.*/void Q3GroupBox::addSpace(int size){ QApplication::sendPostedEvents(this, QEvent::ChildInserted); if (d->nCols <= 0 || d->nRows <= 0) return; if (d->row >= d->nRows || d->col >= d->nCols) d->grid->expand(d->row+1, d->col+1); if (size > 0) { QSpacerItem *spacer = new QSpacerItem((d->dir == Qt::Horizontal) ? 0 : size, (d->dir == Qt::Vertical) ? 0 : size, QSizePolicy::Fixed, QSizePolicy::Fixed); d->grid->addItem(spacer, d->row, d->col); } skip();}/*! \property Q3GroupBox::columns \brief the number of columns or rows (depending on \l Q3GroupBox::orientation) in the group box Usually it is not a good idea to set this property because it is slow (it does a complete layout). It is best to set the number of columns directly in the constructor.*/int Q3GroupBox::columns() const{ if (d->dir == Qt::Horizontal) return d->nCols; return d->nRows;}void Q3GroupBox::setColumns(int c){ setColumnLayout(c, d->dir);}/*! Returns the width of the empty space between the items in the group and the frame of the group. Only applies if the group box has a defined orientation. The default is usually 11, by may vary depending on the platform and style. \sa setInsideMargin(), orientation*/int Q3GroupBox::insideMargin() const{ return d->marg;}/*! Returns the width of the empty space between each of the items in the group. Only applies if the group box has a defined orientation. The default is usually 5, by may vary depending on the platform and style. \sa setInsideSpacing(), orientation*/int Q3GroupBox::insideSpacing() const{ return d->spac;}/*! Sets the the width of the inside margin to \a m pixels. \sa insideMargin()*/void Q3GroupBox::setInsideMargin(int m){ d->marg = m; setColumnLayout(columns(), d->dir);}/*! Sets the width of the empty space between each of the items in the group to \a s pixels. \sa insideSpacing()*/void Q3GroupBox::setInsideSpacing(int s){ d->spac = s; setColumnLayout(columns(), d->dir);}/*! \property Q3GroupBox::orientation \brief the group box's orientation A horizontal group box arranges its children in columns, while a vertical group box arranges them in rows. Usually it is not a good idea to set this property because it is slow (it does a complete layout). It is better to set the orientation directly in the constructor.*/void Q3GroupBox::setOrientation(Qt::Orientation o){ setColumnLayout(columns(), o);}Qt::Orientation Q3GroupBox::orientation() const{ return d->dir;}/*! Changes the layout of the group box. This function is only useful in combination with the default constructor that does not take any layout information. This function will put all existing children in the new layout. It is not good Qt programming style to call this function after children have been inserted. Sets the number of columns or rows to be \a strips, depending on \a direction. \sa orientation columns*/void Q3GroupBox::setColumnLayout(int strips, Qt::Orientation direction){ if (layout()) delete layout(); d->vbox = 0; d->grid = 0; if (strips < 0) // if 0, we create the d->vbox but not the d->grid. See below. return; d->vbox = new QVBoxLayout(this, d->marg, 0); d->nCols = 0; d->nRows = 0; d->dir = direction; // Send all child events and ignore them. Otherwise we will end up // with doubled insertion. This won't do anything because d->nCols == // d->nRows == 0. QApplication::sendPostedEvents(this, QEvent::ChildInserted); // if 0 or smaller , create a vbox-layout but no grid. This allows // the designer to handle its own grid layout in a group box. if (strips <= 0) return; d->dir = direction; if (d->dir == Qt::Horizontal) { d->nCols = strips; d->nRows = 1; } else { d->nCols = 1; d->nRows = strips; } d->grid = new QGridLayout(d->nRows, d->nCols, d->spac); d->row = d->col = 0; d->grid->setAlignment(Qt::AlignTop); d->vbox->addLayout(d->grid); // Add all children QObjectList childList = children(); if (!childList.isEmpty()) { for (int i = 0; i < childList.size(); ++i) { QObject *o = childList.at(i); if (o->isWidgetType() && o != d->checkbox) insertWid(static_cast<QWidget *>(o));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -