⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qspinbox.cpp

📁 qtopia-phone-2.2.0下公共的控件实现源代码。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************** $Id: qt/src/widgets/qspinbox.cpp   2.3.12   edited 2005-10-27 $**** Implementation of QSpinBox widget class**** Created : 1997**** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.**** This file is part of the widgets module of the Qt GUI Toolkit.**** This file may be distributed under the terms of the Q Public License** as defined by Trolltech AS of Norway and appearing in the file** LICENSE.QPL included in the packaging of this file.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition** licenses may use this file in accordance with the Qt Commercial License** Agreement provided with the Software.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for**   information about Qt Commercial License Agreements.** See http://www.trolltech.com/qpl/ for QPL licensing information.** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "qspinbox.h"#ifndef QT_NO_SPINBOX#include "qspinbox.h"#include "qpushbutton.h"#include "qpainter.h"#include "qbitmap.h"#include "qlineedit.h"#include "qvalidator.h"#include "qpixmapcache.h"#include "qapplication.h"#ifdef QT_KEYPAD_MODEextern bool qt_modalEditingEnabled;#endifstruct QSpinBoxPrivate{    QSpinBoxPrivate(): buttonSymbols( QSpinBox::UpDownArrows ) {}    QSpinBox::ButtonSymbols buttonSymbols;};// REVISED: warwick/*!  \class QSpinBox qspinbox.h  \brief The QSpinBox class provides a spin box widget, sometimes called	  up-down widget, little arrows widget or spin button.  \ingroup basic  QSpinBox allows the user to choose a value, either by  clicking the up/down buttons to increase/decrease the value  currently displayed, or by typing the value directly into the spin  box. Usually the value is an integer.  Every time the value changes, QSpinBox emits the valueChanged()  signal.  The current value can be fetched with value() and set with  setValue().  The spin box clamps the value within a numeric range, see  QRangeControl for details.  Clicking the up/down down buttons (or  using the keyboard accelerators: Up-arrow and Down-arrow) will  increase or decrease the current value in steps of size lineStep().  Most spin boxes are directional, but QSpinBox can also operate as a  circular spin box, i.e. if the range is 0-99 and the current value  is 99, clicking Up will give 0.  Use setWrapping() if you want  circular behavior.  The displayed value can be prepended and/or appended with an  arbitrary string indicating for example the unit of measurement.  See setPrefix() and setSuffix().  Normally, the spin box displays up and down arrows in the buttons.  You can use setButtonSymbols() to change the display to  show + and - symbols, if this is clearer for your intended purpose.  In either case, the up and down arrow keys always work.  It is often desirable to give the user a special, often default,  choice in addition to the range of numeric values.  See  setSpecialValueText() for how to do this with QSpinBox.  The default \l QWidget::focusPolicy() is StrongFocus.  QSpinBox can easily be subclassed to allow the user to input other  things than an integer value, as long as the allowed input can be  mapped down to a range of integers.  This can be done by overriding  the virtual functions mapValueToText() and mapTextToValue() and  setting another, suitable validator using setValidator(). For example,  these function could be changed so that the user provided values  from 0.0 to 10.0 while the range of integers used inside the program  would be 0 to 100:  \code  class MySpinBox : public QSpinBox {  public:    ...    QString	mapValueToText( int value )    {      return QString("%1.%2").arg(value/10).arg(value%10);    }    int		mapTextToValue( bool* ok )    {      return int(text().toFloat()*10);    }  };  \endcode  <img src=qspinbox-m.png> <img src=qspinbox-w.png>  \sa QScrollBar QSlider  <a href="guibooks.html#fowler">GUI Design Handbook: Spin Box</a>*//*!  Constructs a spin box with the default QRangeControl range and step  values.  \sa minValue(), maxValue(), setRange(), lineStep(), setSteps()*/QSpinBox::QSpinBox( QWidget * parent , const char *name )    : QFrame( parent, name ),      QRangeControl(){    initSpinBox();}/*!  Constructs a spin box with range from \a minValue to \a maxValue  inclusive, with step amount \a step.  The value is initially  set to \a minValue.  \sa minValue(), maxValue(), setRange(), lineStep(), setSteps()*/QSpinBox::QSpinBox( int minValue, int maxValue, int step, QWidget* parent,		    const char* name )    : QFrame( parent, name ),      QRangeControl( minValue, maxValue, step, step, minValue ){    initSpinBox();}/*!  \internal Initialization.*/void QSpinBox::initSpinBox(){    d = 0;    wrap = FALSE;    edited = FALSE;    up = new QPushButton( this, "up" );    up->setFocusPolicy( QWidget::NoFocus );    up->setAutoDefault( FALSE );    up->setAutoRepeat( TRUE );    down = new QPushButton( this, "down" );    down->setFocusPolicy( QWidget::NoFocus );    down->setAutoDefault( FALSE );    down->setAutoRepeat( TRUE );    validate = new QIntValidator( minValue(), maxValue(), this, "validator" );    vi = new QLineEdit( this, "line editor" );    vi->setFrame( FALSE );    setFocusProxy( vi );    setFocusPolicy( StrongFocus );    vi->setValidator( validate );    vi->installEventFilter( this );    if ( style() == WindowsStyle )	setFrameStyle( WinPanel | Sunken );    else	setFrameStyle( Panel | Sunken );    setLineWidth( 2 );    setPalettePropagation( AllChildren );    setFontPropagation( AllChildren );    setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) );    updateDisplay();    connect( up, SIGNAL(pressed()), SLOT(stepUp()) );    connect( down, SIGNAL(pressed()), SLOT(stepDown()) );    connect( vi, SIGNAL(textChanged(const QString&)), SLOT(textChanged()) );}/*!  Destroys the spin box, freeing all memory and other resources.*/QSpinBox::~QSpinBox(){    delete d;}/*!  Returns the current text of the spin box, including any prefix() and suffix().  \sa value()*/QString QSpinBox::text() const{    return vi->text();}/*!  Returns a copy of the current text of the spin box with any prefix  and/or suffix and white space at the start and end removed.  \sa text(), setPrefix(), setSuffix()*/QString QSpinBox::cleanText() const{    QString s = QString(text()).stripWhiteSpace();    if ( !prefix().isEmpty() ) {	QString px = QString(prefix()).stripWhiteSpace();	int len = px.length();	if ( len && s.left(len) == px )  // Remove _only_ if it is the prefix	    s.remove( 0, len );    }    if ( !suffix().isEmpty() ) {	QString sx = QString(suffix()).stripWhiteSpace();	int len = sx.length();	if ( len && s.right(len) == sx )  // Remove _only_ if it is the suffix	    s.truncate( s.length() - len );    }    return s.stripWhiteSpace();}/*!  Sets the special-value text to \a text.  If set, the spin box will  display this text instead of a numeric value whenever the current  value is equal to minVal().  Typically used for indicating 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" );    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.  Neither \link setPrefix prefix\endlink nor \link setSuffix  suffix,\endlink if set, are added to the special-value text when  displayed.  To turn off the special-value text display, call this function with  an empty string as parameter. The default is no special-value text,  i.e. the numeric value is shown as usual.  \sa specialValueText()*/void QSpinBox::setSpecialValueText( const QString &text ){    specText = text;    updateDisplay();}/*!  Returns the currently special-value text, or a null string if no  special-value text is currently set.  \sa setSpecialValueText()*/QString QSpinBox::specialValueText() const{    if ( specText.isEmpty() )	return QString::null;    else	return specText;}/*!  Sets the prefix to \a text.  The prefix is prepended to the start of  the displayed value.  Typical use is to indicate the unit of  measurement to the user. eg.  \code    sb->setPrefix("$");  \endcode  To turn off the prefix display, call this function with an empty  string as parameter.  The default is no prefix.  \sa prefix(), setSuffix(), suffix()*/void QSpinBox::setPrefix( const QString &text ){    pfix = text;    updateDisplay();}/*!  Sets the suffix to \a text.  The suffix is appended to the end of the  displayed value.  Typical use is to indicate the unit of measurement  to the user. eg.  \code    sb->setSuffix("cm");  \endcode  To turn off the suffix display, call this function with an empty  string as parameter.  The default is no suffix.  \sa suffix(), setPrefix(), prefix()*/void QSpinBox::setSuffix( const QString &text ){    sfix = text;    updateDisplay();}/*!  Returns the currently set prefix, or a null string if no prefix is  set.  \sa setPrefix(), setSuffix(), suffix()*/QString QSpinBox::prefix() const{    if ( pfix.isEmpty() )	return QString::null;    else	return pfix;}/*!  Returns the currently set suffix, or a null string if no suffix is  set.  \sa setSuffix(), setPrefix(), suffix()*/QString QSpinBox::suffix() const{    if ( sfix.isEmpty() )	return QString::null;    else	return sfix;}/*!  Setting wrapping to TRUE will allow the value to be stepped from the  highest value to the lowest, and vice versa.  By default, wrapping is  turned off.  \sa wrapping(), minValue(), maxValue(), setRange()*/void QSpinBox::setWrapping( bool on ){    wrap = on;    updateDisplay();}/*!  Returns the current setWrapping() value.*/bool QSpinBox::wrapping() const{    return wrap;}/*!\reimp*/QSize QSpinBox::sizeHint() const{    constPolish();    QFontMetrics fm = fontMetrics();    int h = fm.height();    if ( h < 12 ) 	// ensure enough space for the button pixmaps	h = 12;    int w = 35; 	// minimum width for the value    int wx = fm.width( ' ' )*2 * style().pixelMetric(QStyle::SpinBoxHExtraScale);    QString s;    s = prefix() + ( (QSpinBox*)this )->mapValueToText( minValue() ) + suffix();    w = QMAX( w, fm.width( s ) + wx);    s = prefix() + ( (QSpinBox*)this )->mapValueToText( maxValue() ) + suffix();    w = QMAX(w, fm.width( s ) + wx );    if ( !specialValueText().isEmpty() ) {	s = specialValueText();	w = QMAX( w, fm.width( s ) + wx );    }    QSize r( (h*style().pixelMetric(QStyle::SpinBoxButtonScale)) // buttons AND frame both sides - see resizeevent()	     + style().pixelMetric(QStyle::SpinBoxHMargin) // right/left margins	     + w, // widest value	     frameWidth() * 2 // top/bottom frame	     + style().pixelMetric(QStyle::SpinBoxVMargin) // top/bottom margins	     + h // font height	     );    return r.expandedTo( QApplication::globalStrut() );}// Does the layout of the lineedit and the buttonsvoid QSpinBox::arrangeWidgets(){    if ( !up || !down ) // may happen if the application has a pointer error	return;    QSize bs; // no, it's short for 'button size'    if ( style() == WindowsStyle )	bs.setHeight( height()/2 - frameWidth() );    else	bs.setHeight( height()/2 );    if ( bs.height() < 8 )	bs.setHeight( 8 );    bs.setWidth( bs.height() * 8 / 5 ); // 1.6 - approximate golden mean    if ( style() == WindowsStyle )	setFrameRect( QRect( 0, 0, 0, 0 ) );    else	setFrameRect( QRect( 0, 0, width() - bs.width(), height() ) );    if ( up->size() != bs || down->size() != bs ) {	up->resize( bs );	down->resize( bs );	updateButtonSymbols();    }    int y = style() == WindowsStyle ? frameWidth() : 0;    int x = width() - y - bs.width();    up->move( x, y );    down->move( x, height() - y - up->height() );    if ( style() == WindowsStyle )	vi->setGeometry( frameWidth(), frameWidth(),			 x - frameWidth(), height() - 2*frameWidth() );    else	vi->setGeometry( contentsRect() );}/*!\reimp*/QSizePolicy QSpinBox::sizePolicy() const{    //### removeme 3.0    return QWidget::sizePolicy();}/*!  Sets the current value of the spin box to \a value.  This is  QRangeControl::setValue() made available as a slot.*/void QSpinBox::setValue( int value ){    QRangeControl::setValue( value );}/*!  Increases the current value one step, wrapping as necessary.  This is  the same as clicking on the pointing-up button, and can be used for  e.g.  keyboard accelerators.  \sa stepDown(), addLine(), lineStep(), setSteps(), setValue(), value()*/void QSpinBox::stepUp(){    if ( edited )	interpretText();    if ( wrapping() && ( value()+lineStep() > maxValue() ) )	setValue( minValue() );    else	addLine();}/*!  Decreases the current value one step, wrapping as necessary.  This is  the same as clicking on the pointing-down button, and can be used  for e.g.  keyboard accelerators.  \sa stepUp(), subtractLine(), lineStep(), setSteps(), setValue(), value()*/void QSpinBox::stepDown(){    if ( edited )	interpretText();    if ( wrapping() && ( value()-lineStep() < minValue() ) )	setValue( maxValue() );    else	subtractLine();}/*!  \fn void QSpinBox::valueChanged( int value )  This signal is emitted every time the value of the spin box changes  (whatever the cause - by setValue(), by a keyboard accelerator, by  mouse clicks etc.).  Note that it is emitted \e every time, not just for the "final" step  - if the user clicks 'up' three times, this signal is emitted three  times.  \sa value()*//*!  \fn void QSpinBox::valueChanged( const QString& valueText )  This signal is emitted whenever the valueChanged( int ) signal is  emitted, i.e.  every time the value of the spin box changes (whatever  the cause - by setValue(), by a keyboard accelerator, by mouse  clicks etc.).

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -