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

📄 qlineedit.cpp

📁 qtopia-phone-2.2.0下公共的控件实现源代码。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
{    d->readonly = enable;}/*!  Returns whether the line-edit is read-only.  \sa setReadOnly()*/bool QLineEdit::isReadOnly() const{    return d->readonly;}/*!  Returns a recommended size for the widget.  The width returned is enough for a few characters, typically 15 to 20.*/QSize QLineEdit::sizeHint() const{    constPolish();    QFontMetrics fm( font() );    int h = fm.height();    int w = fm.width( 'x' ) * 17; // "some"    int hm = style().pixelMetric(QStyle::LineEditTextHMargin); // 2    int vm = style().pixelMetric(QStyle::LineEditTextVMargin); // 2    int il = style().pixelMetric(QStyle::IdealHeightLimit); // 25    int ih = style().pixelMetric(QStyle::IdealHeight); // 22    if ( frame() ) {	h += vm*2 + frameW()*2;	if ( style() == WindowsStyle && h <= il )	    h = ih;	return QSize( w + hm*2 + frameW()*2, h ).expandedTo( QApplication::globalStrut() );    } else {	return QSize( w + hm*2, h + vm*2 ).expandedTo( QApplication::globalStrut() );    }}/*!  Returns a minimum size for the line edit.  The width returned is enough for at least one character.*/QSize QLineEdit::minimumSizeHint() const{    constPolish();    QFontMetrics fm( font() );    int h = fm.height();    int w = fm.maxWidth();    int il = style().pixelMetric(QStyle::IdealHeightLimit); // 25    int ih = style().pixelMetric(QStyle::IdealHeight); // 22    if ( frame() ) {	h += 4 + frameW()*2;	if ( style() == WindowsStyle && h <= il )	    h = ih;	return QSize( w + 4 + frameW()*2, h );    } else {	return QSize( w + 4, h + 4 );    }}/*!\reimp*/QSizePolicy QLineEdit::sizePolicy() const{    //### removeme 3.0    return QWidget::sizePolicy();}/*!  Sets this line edit to accept input only as accepted by \a v,  allowing arbitrary constraints on the text which the user can edit.  If \a v == 0, remove the current input validator.  The default  is no input validator (ie. any input is accepted up to maxLength()).  \sa validator() QValidator*/void QLineEdit::setValidator( const QValidator * v ){    d->validator = v;}/*!  Returns a pointer to the current input validator, or 0 if no  validator has been set.  \sa setValidator()*/const QValidator * QLineEdit::validator() const{    return d ? d->validator : 0;}/*!  This slot is equivalent to setValidator( 0 ). */void QLineEdit::clearValidator(){    setValidator( 0 );}#ifndef QT_NO_DRAGANDDROP/*! \reimp*/void QLineEdit::dragEnterEvent( QDragEnterEvent *e ){    if ( !d->readonly && QTextDrag::canDecode(e) )	e->accept( rect() );}/*!\reimp*/void QLineEdit::dropEvent( QDropEvent *e ){    QString str;    QCString plain = "plain";    // try text/plain    bool decoded = QTextDrag::decode(e, str, plain);    // otherwise we'll accept any kind of text (like text/uri-list)    if (! decoded) decoded = QTextDrag::decode(e, str);    if ( !d->readonly && decoded) {	if ( e->source() == this && hasMarkedText() )	    del();	if ( !hasMarkedText() )	    setCursorPosition( xPosToCursorPos(e->pos().x()) );	insert( str );	e->accept();    } else {	e->ignore();    }}#endif // QT_NO_DRAGANDDROP/*!  This private slot handles cursor blinking. */void QLineEdit::blinkSlot(){    if ( hasFocus() || cursorOn ) {	cursorOn = !cursorOn;	if ( d->pm && !d->pmDirty && d->cursorRepaintRect.isValid() )	    repaint( d->cursorRepaintRect, FALSE );	else	    repaint( FALSE );    }    if ( hasFocus() )	d->blinkTimer.start( QApplication::cursorFlashTime()/2, TRUE );    else	d->blinkTimer.stop();}/*!  This private slot handles drag-scrolling. */void QLineEdit::dragScrollSlot(){    if ( !hasFocus() || !dragScrolling )	d->dragTimer.stop();    else if ( scrollingLeft )	cursorLeft( TRUE );    else	cursorRight( TRUE );}/*!  Validates and perhaps sets this line edit to contain \a newText  with the cursor at position newPos, with marked text from \a  newMarkAnchor to \a newMarkDrag.  Returns TRUE if it changes the line  edit and FALSE if it doesn't.  Linebreaks in \a newText are converted to spaces, and it is  truncated to maxLength() before testing its validity.  Repaints and emits textChanged() if appropriate.*/bool QLineEdit::validateAndSet( const QString &newText, int newPos,				int newMarkAnchor, int newMarkDrag ){#ifndef QT_NO_QWS_IM    forceIMEnd();#endif    QString t( newText );    for ( uint i=0; i<t.length(); i++ ) {	if ( t[(int)i] < ' ' )  // unprintable/linefeed becomes space	    t[(int)i] = ' ';    }    t.truncate( maxLength() );#ifndef QT_NO_VALIDATOR    const QValidator * v = validator();    if ( v && v->validate( t, newPos ) == QValidator::Invalid &&	 v->validate( tbuf, cursorPos ) != QValidator::Invalid ) {	return FALSE;    }#endif    bool tc = ( t != tbuf );    // okay, it succeeded    if ( newMarkDrag != markDrag ||	 newMarkAnchor != markAnchor ||	 newPos != cursorPos ||	 tc ) {	int minP = QMIN( cursorPos, minMark() );	int maxP = QMAX( cursorPos, maxMark() );	cursorPos = newPos;	markAnchor = newMarkAnchor;	markDrag = newMarkDrag;	minP = QMIN( minP, QMIN( cursorPos, minMark() ) );	int i = 0;	while( i < minP && t[i] == tbuf[i] )	    i++;	minP = i;	maxP = QMAX( maxP, QMAX( cursorPos, maxMark() ) );	if ( fontMetrics().width( t ) < fontMetrics().width( tbuf ) )	    maxP = t.length();	tbuf = t;	if ( cursorPos < (int)text().length() && maxP < (int)text().length() )	    maxP = text().length();	repaintArea( minP, maxP );    }    if ( tc ) {	ed = TRUE;	emit textChanged( tbuf );    }    return TRUE;}/*!  Removes any selected text, inserts \a newText,  validates the result and if it is valid, sets it as the new contents  of the line edit.*/void QLineEdit::insert( const QString &newText ){#ifndef QT_NO_QWS_IM    forceIMEnd();#endif    QString t( newText );    if ( t.isEmpty() && !hasMarkedText() )	return;    for ( int i=0; i<(int)t.length(); i++ )	if ( t[i] < ' ' )  // unprintable/linefeed becomes space	    t[i] = ' ';    QString test( tbuf );    int cp = cursorPos;    if ( d->undo && ( d->needundo || hasMarkedText() ) ) {	if ( d->undoList.isEmpty() || d->undoList.last().str != tbuf ) {	    d->undoList += QLineEditUndoItem(tbuf, cursorPos );	    d->redoList.clear();	    d->needundo = FALSE;	}    }    if ( hasMarkedText() ) {	test.remove( minMark(), maxMark() - minMark() );	cp = minMark();    }    test.insert( cp, t );    int ncp = QMIN( cp+t.length(), (uint)maxLength() );    validateAndSet( test, ncp, ncp, ncp );    blinkOn();}/*!  Repaints all characters from \a from to \a to.  If cursorPos is  between from and to, ensures that cursorPos is visible.  */void QLineEdit::repaintArea( int from, int to ){    QString buf = displayText();    int a, b;    if ( from < to ) {	a = from;	b = to;    } else {	a = to;	b = from;    }    d->pmDirty = TRUE;    int old = offset;    if ( d->offsetDirty || cursorPos >= a && cursorPos <= b )	updateOffset();    if ( !d->pmDirty || !isVisible() ) {	return;    } else if ( old != offset ) {	repaint( FALSE );	return;    }    QFontMetrics fm = fontMetrics();    int x = fm.width( buf.left( a ) ) + offset - 2 + frameW();    QRect r( x, 0, fm.width( buf.mid( a, b-a ) ) + 5, height() );    r = r.intersect( rect() );    if ( !r.isValid() )	return;    if ( b >= (int)buf.length() )	r.setRight( width() );    repaint( r, FALSE );}/*!  \reimp */void QLineEdit::setEnabled( bool e ){    d->pmDirty = TRUE;    QWidget::setEnabled( e );}/*! \reimp */void QLineEdit::setFont( const QFont & f ){    d->pmDirty     = TRUE;    d->offsetDirty = TRUE;    QWidget::setFont( f );}/*!  Syntactic sugar for setText( "" ), provided to match no-argument  signals.*/void QLineEdit::clear(){    setText( QString::fromLatin1("") );}/*!  Sets the marked area of this line edit to start at \a start and  be \a length characters long. */void QLineEdit::setSelection( int start, int length ){    int b, e;    b = QMIN( markAnchor, markDrag );    e = QMAX( markAnchor, markDrag );    b = QMIN( b, start );    e = QMAX( e, start + length );    markAnchor = start;    markDrag = start + length;    repaintArea( b, e );}/*!  Sets the cursor position for this line edit to \a newPos and  repaints accordingly.  \sa cursorPosition() */void QLineEdit::setCursorPosition( int newPos ){    if ( newPos == cursorPos )	return;    newPos = QMIN( newPos, (int)tbuf.length() );    newPos = QMAX( newPos, 0 );    int b, e;    b = QMIN( newPos, cursorPos );    e = QMAX( newPos, cursorPos );    cursorPos = newPos;    blinkOn();    repaintArea( b, e );}/*!  Returns the current cursor position for this line edit.  \sa  setCursorPosition() */int QLineEdit::cursorPosition() const{    return cursorPos;}/*! \reimp */void QLineEdit::setPalette( const QPalette & p ){    d->pmDirty = TRUE;    QWidget::setPalette( p );}/*!  Sets the edited flag of this line edit to \a on.  The edited flagis never read by QLineEdit, and is changed to TRUE whenever the userchanges its contents.This is useful e.g. for things that need to provide a default value,but cannot find the default at once.  Just open the line edit withoutthe best default and when the default is known, check the edited()return value and set the line edit's contents if the user has notstarted editing the line edit.\sa edited()*/void QLineEdit::setEdited( bool on ){    ed = on;}/*!  Returns the edited flag of the line edit.  If this returns FALSE,the line edit's contents have not been changed since the constructionof the QLineEdit (or the last call to either setText() or setEdited( FALSE ),if any).  If it returns true, the contents have been edited, orsetEdited( TRUE ) has been called.\sa setEdited()*/bool QLineEdit::edited() const{    return ed;}/*!  Moves the cursor one word to the right.  If \a mark is TRUE, the text  is marked.  \sa cursorWordBackward()*/void QLineEdit::cursorWordForward( bool mark ){    int i = cursorPos;    while ( i < (int) tbuf.length() && !tbuf[i].isSpace() )	++i;    while ( i < (int) tbuf.length() && tbuf[i].isSpace() )	++i;    cursorRight( mark, i - cursorPos );}/*!  Moves the cursor one word to the left.  If \a mark is TRUE, the text  is marked.  \sa cursorWordForward()*/void QLineEdit::cursorWordBackward( bool mark ){    int i = cursorPos;    while ( i > 0 && tbuf[i-1].isSpace() )	--i;    while ( i > 0 && !tbuf[i-1].isSpace() )	--i;    cursorLeft( mark, cursorPos - i );}void QLineEdit::updateOffset(){ // must not call repaint() - paintEvent() calls this    if ( !isVisible() ) {	d->offsetDirty = TRUE;	return;    }    d->offsetDirty = FALSE;    makePixmap();    QFontMetrics fm = fontMetrics();    int textWidth = fm.width( displayText() )+4;    int w = d->pm->width();    int old = offset;    if ( textWidth > w ) {	static const int rightDist = 2*fm.width("x");	static const int leftMargin = 2;	// may need to scroll.	QString dt = displayText();	int leftPos = fm.width( dt.left( QMAX( cursorPos-leftMargin, 0 ) ) );	int rightPos;#if !defined(QT_NO_QWS_IM)	if ( d->preeditSelLen )	    rightPos = fm.width( dt.left( cursorPos+d->preeditSelLen+1 ) );	else#endif	    rightPos = fm.width( dt.left( cursorPos ) ) + rightDist;		if ( rightPos + offset > w )	    offset = w - rightPos;	if ( leftPos + offset < 0 )	    offset = -leftPos;		//make sure we show as much as possible of the text:	if ( textWidth + offset < w )	    offset = w - textWidth;    } else {	if ( textWidth < 5 ) {	    // nothing is to be drawn.  okay.	    textWidth = QMIN( 5, w );	}	if ( alignmentFlag == Qt::AlignRight ) {	    // right-aligned text, space for all of it	    offset = w - textWidth;	} else if ( alignmentFlag == Qt::AlignCenter || alignmentFlag == Qt::AlignHCenter ) {	    // center-aligned text, space for all of it	    offset = (w - textWidth)/2;	} else {	    // default: left-aligned, space for all of it	    offset = 0;	}    }    if ( old == offset && !d->pmDirty )	return;    d->pmDirty = TRUE;}/*! Returns the index of the character to whose left edge \a goalx is  closest.*/int QLineEdit::xPosToCursorPos( int goalx ) const{    int x1, x2;    x1 = offset;    int i = 0;    QFontMetrics fm = fontMetrics();    QString s = displayText();    goalx -= (frameW() + 2);    while( i < (int) s.length() ) {	x2 = x1 + fm.width( s[i] );	if ( QABS( x1 - goalx ) < QABS( x2 - goalx ) )	    return i;	i++;	x1 = x2;    }    return i;}/*!  Starts the thing blinking, or makes sure it's displayed at once. */void QLineEdit::blinkOn(){    if ( !hasFocus() )	return;    d->blinkTimer.start( cursorOn?QApplication::cursorFlashTime() / 2 : 0, TRUE );    inBlinkOn = TRUE;    blinkSlot();    inBlinkOn = FALSE;}void QLineEdit::makePixmap() const{    if ( d->pm )	return;    QSize s( width() - frameW()*2, height() - frameW()*2 );    if ( s.width() < 0 )	s.setWidth( 0 );    if ( s.height() < 0 )	s.setHeight( 0 );    d->pm = new QPixmap( s );    d->pmDirty = TRUE;}void QLineEdit::undoInternal(){    if ( d->undoList.isEmpty() )	return;    d->undo = FALSE;    d->redoList += QLineEditUndoItem(tbuf, cursorPos );    setText( d->undoList.last().str );    setCursorPosition( d->undoList.last().pos );    markAnchor = cursorPos;    d->undoList.remove( d->undoList.fromLast() );    if ( d->undoList.count() > 10 )	d->undoList.remove( d->undoList.begin() );    d->undo = TRUE;    d->needundo = TRUE;}void QLineEdit::redoInternal(){    if ( d->redoList.isEmpty() )	return;    d->undo = FALSE;    d->undoList += QLineEditUndoItem(tbuf, cursorPos );    setText( d->redoList.last().str );    setCursorPosition( d->redoList.last().pos );    markAnchor = cursorPos;    d->redoList.remove( d->redoList.fromLast() );    d->undo = TRUE;    d->needundo = TRUE;}#endif#if defined(Q_INCOMPATIBLE_3_0_ADDONS)bool QLineEdit::getSelection( int *start, int *end ){    if( !hasMarkedText() )	return false;    *start = minMark();    *end = maxMark();    return true;}void QLineEdit::setPasswordChar( QChar c ){    d->passwordChar = c;}QChar QLineEdit::passwordChar() const{    return d->passwordChar;}#endif

⌨️ 快捷键说明

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