📄 qabstractspinbox.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui 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://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** 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 <qplatformdefs.h>#include <private/qabstractspinbox_p.h>#include <private/qdatetime_p.h>#include <qabstractspinbox.h>#ifndef QT_NO_SPINBOX#include <qapplication.h>#include <qclipboard.h>#include <qdatetime.h>#include <qdatetimeedit.h>#include <qevent.h>#include <qmenu.h>#include <qpainter.h>#include <qdebug.h>#include <qpalette.h>#include <qstyle.h>#if defined(Q_WS_X11)#include <limits.h>#endifstatic const int thresholdTime = 500; // ### make this a stylehint in 4.1//#define QABSTRACTSPINBOX_QSBDEBUG#ifdef QABSTRACTSPINBOX_QSBDEBUG# define QASBDEBUG qDebug#else# define QASBDEBUG if (false) qDebug#endif/*! \class QAbstractSpinBox \brief The QAbstractSpinBox class provides a spinbox and a line edit to display values. \ingroup abstractwidgets The class is designed as a common super class for widgets like QSpinBox, QDoubleSpinBox and QDateTimeEdit Here are the main properties of the class: \list 1 \i \l text: The text that is displayed in the QAbstractSpinBox. \i \l alignment: The alignment of the text in the QAbstractSpinBox. \i \l wrapping: Whether the QAbstractSpinBox wraps from the minimum value to the maximum value and vica versa. \endlist QAbstractSpinBox provides a virtual stepBy() function that is called whenever the user triggers a step. This function takes an integer value to signify how many steps were taken. E.g. Pressing Qt::Key_Down will trigger a call to stepBy(-1). QAbstractSpinBox also provide a virtual function stepEnabled() to determine whether stepping up/down is allowed at any point. This function returns a bitset of StepEnabled.*//*! \enum QAbstractSpinBox::StepEnabledFlag \value StepNone \value StepUpEnabled \value StepDownEnabled*//*! \fn void QAbstractSpinBox::editingFinished() This signal is emitted editing is finished. This happens when the spinbox loses focus and when enter is pressed.*//*! Constructs an abstract spinbox with the given \a parent with default \l wrapping, and \l alignment properties.*/QAbstractSpinBox::QAbstractSpinBox(QWidget *parent) : QWidget(*new QAbstractSpinBoxPrivate, parent, 0){ Q_D(QAbstractSpinBox); d->init();}/*! \internal*/QAbstractSpinBox::QAbstractSpinBox(QAbstractSpinBoxPrivate &dd, QWidget *parent) : QWidget(dd, parent, 0){ Q_D(QAbstractSpinBox); d->init();}/*! Called when the QAbstractSpinBox is destroyed.*/QAbstractSpinBox::~QAbstractSpinBox(){}/*! \enum QAbstractSpinBox::ButtonSymbols This enum type describes the symbols that can be displayed on the buttons in a spin box. \inlineimage qspinbox-updown.png \inlineimage qspinbox-plusminus.png \value UpDownArrows Little arrows in the classic style. \value PlusMinus \bold{+} and \bold{-} symbols. \sa QAbstractSpinBox::buttonSymbols*//*! \property QAbstractSpinBox::buttonSymbols \brief the current button symbol mode The possible values can be either \c UpDownArrows or \c PlusMinus. The default is \c UpDownArrows. \sa ButtonSymbols*/QAbstractSpinBox::ButtonSymbols QAbstractSpinBox::buttonSymbols() const{ Q_D(const QAbstractSpinBox); return d->buttonSymbols;}void QAbstractSpinBox::setButtonSymbols(ButtonSymbols bs){ Q_D(QAbstractSpinBox); if (d->buttonSymbols != (d->buttonSymbols = bs)) update();}/*! \property QAbstractSpinBox::text \brief the spin box's text, including any prefix and suffix There is no default text.*/QString QAbstractSpinBox::text() const{ return lineEdit()->displayText();}/*! \property QAbstractSpinBox::specialValueText \brief the special-value text If set, the spin box will display this text instead of a numeric value whenever the current value is equal to minimum(). Typical use is to indicate that this choice has a special (default) meaning. For example, if your spin box allows the user to choose the margin width in a print dialog and your application is able to automatically choose a good margin width, you can set up the spin box like this: \code QSpinBox marginBox(-1, 20, 1, parent); marginBox.setSuffix(" mm"); marginBox.setSpecialValueText("Auto"); \endcode The user will then be able to choose a margin width from 0-20 millimeters or select "Auto" to leave it to the application to choose. Your code must then interpret the spin box value of -1 as the user requesting automatic margin width. All values are displayed with the prefix and suffix (if set), \e except for the special value, which only shows the special value text. To turn off the special-value text display, call this function with an empty string. The default is no special-value text, i.e. the numeric value is shown as usual. If no special-value text is set, specialValueText() returns an empty string.*/QString QAbstractSpinBox::specialValueText() const{ Q_D(const QAbstractSpinBox); return d->specialValueText;}void QAbstractSpinBox::setSpecialValueText(const QString &s){ Q_D(QAbstractSpinBox); d->specialValueText = s; d->clearCache(); d->updateEdit();}/*! \property QAbstractSpinBox::wrapping \brief whether the spin box is circular. If wrapping is true stepping up from maximum() value will take you to the minimum() value and vica versa. Wrapping only make sense if you have minimum() and maximum() values set. \code QSpinBox *spinBox = new QSpinBox(this); spinBox->setRange(0, 100); spinBox->setWrapping(true); sb->setValue(100); sb->stepBy(1); // value is 0 \endcode By default, wrapping is turned off. \sa QSpinBox::minimum(), QSpinBox::maximum()*/bool QAbstractSpinBox::wrapping() const{ Q_D(const QAbstractSpinBox); return d->wrapping;}void QAbstractSpinBox::setWrapping(bool w){ Q_D(QAbstractSpinBox); d->wrapping = w;}/*! \property QAbstractSpinBox::readOnly \brief whether the spin box is read only. In read-only mode, the user can still copy the text to the clipboard, or drag and drop the text; but cannot edit it. The QLineEdit in the QAbstractSpinBox does not show a cursor in read-only mode. \sa QLineEdit::readOnly*/bool QAbstractSpinBox::isReadOnly() const{ Q_D(const QAbstractSpinBox); return d->readOnly;}void QAbstractSpinBox::setReadOnly(bool enable){ Q_D(QAbstractSpinBox); d->readOnly = enable; d->edit->setReadOnly(enable); d->updateButtons();}/*! \property QAbstractSpinBox::frame \brief whether the spin box draws itself with a frame If enabled (the default) the spin box draws itself inside a frame, otherwise the spin box draws itself without any frame.*/bool QAbstractSpinBox::hasFrame() const{ Q_D(const QAbstractSpinBox); return d->frame;}void QAbstractSpinBox::setFrame(bool enable){ Q_D(QAbstractSpinBox); d->frame = enable; update(); updateGeometry();}/*! \property QAbstractSpinBox::alignment \brief the alignment of the spin box Possible Values are Qt::AlignLeft, Qt::AlignRight, and Qt::AlignHCenter. By default, the alignment is Qt::AlignLeft Attempting to set the alignment to an illegal flag combination does nothing. \sa Qt::Alignment*/Qt::Alignment QAbstractSpinBox::alignment() const{ Q_D(const QAbstractSpinBox); return (Qt::Alignment)d->edit->alignment();}void QAbstractSpinBox::setAlignment(Qt::Alignment flag){ Q_D(QAbstractSpinBox); d->edit->setAlignment(flag);}/*! Selects all the text in the spinbox except the prefix and suffix.*/void QAbstractSpinBox::selectAll(){ Q_D(QAbstractSpinBox); if (!d->specialValue()) { const int tmp = d->edit->displayText().size() - d->suffix.size(); d->edit->setSelection(tmp, -(tmp - d->prefix.size())); } else { d->edit->selectAll(); }}/*! Clears the lineedit of all text but prefix and suffix.*/void QAbstractSpinBox::clear(){ Q_D(QAbstractSpinBox); d->edit->setText(d->prefix + d->suffix); d->edit->setCursorPosition(d->prefix.size());}/*! Virtual function that determines whether stepping up and down is legal at any given time. The up arrow will be painted as disabled unless (stepEnabled() & StepUpEnabled) != 0. The default implementation will return (StepUpEnabled| StepDownEnabled) if wrapping is turned on. Else it will return StepDownEnabled if value is > minimum() or'ed with StepUpEnabled if value < maximum(). If you subclass QAbstractSpinBox you will need to reimplement this function. \sa QSpinBox::minimum(), QSpinBox::maximum(), wrapping()*/QAbstractSpinBox::StepEnabled QAbstractSpinBox::stepEnabled() const{ Q_D(const QAbstractSpinBox); if (d->readOnly || d->type == QVariant::Invalid) return StepNone; if (!style()->styleHint(QStyle::SH_SpinControls_DisableOnBounds) || d->wrapping) return StepEnabled(StepUpEnabled | StepDownEnabled); StepEnabled ret = StepNone; if (d->variantCompare(d->value, d->maximum) < 0) { ret |= StepUpEnabled; } if (d->variantCompare(d->value, d->minimum) > 0) { ret |= StepDownEnabled; } return ret;}/*! This virtual function is called by the QAbstractSpinBox to determine whether \a input is valid. The \a pos parameter indicates the position in the string. Reimplemented in the various subclasses.*/QValidator::State QAbstractSpinBox::validate(QString & /* input */, int & /* pos */) const{ return QValidator::Acceptable;}/*! This virtual function is called by the QAbstractSpinBox if the \a input is not validated to QValidator::Acceptable when Return is pressed or interpretText() is called. It will try to change the text so it is valid. Reimplemented in the various subclasses.*/void QAbstractSpinBox::fixup(QString & /* input */) const{}/*! Steps up by one linestep Calling this slot is analogous to calling stepBy(1); \sa stepBy(), stepDown()*/void QAbstractSpinBox::stepUp(){ stepBy(1);}/*! Steps down by one linestep Calling this slot is analogous to calling stepBy(-1); \sa stepBy(), stepUp()*/void QAbstractSpinBox::stepDown(){ stepBy(-1);}/*! Virtual function that is called whenever the user triggers a step. The \a steps parameter indicates how many steps were taken, e.g.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -