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

📄 qlineedit.cpp

📁 qtopia-phone-2.2.0下公共的控件实现源代码。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    d->preeditCPos = -1;    d->preeditSelLen = 0;    if ( isReadOnly() )	return;    //#########  Validation doesn't work!!!!    //######### textChanged() probably doesn't work!    //######### alignment doesn't work    validateAndSet( test, newcp , newcp, newcp ); //### not a perfect fit, uses tbuf...    update(); // since if doesn't have focus... further on won't repaint.. strange but true.    d->pmDirty = TRUE; //###    blinkOn();    emit textChanged( tbuf );        e->accept();}bool QLineEdit::composeMode() const{    return d->preeditStart >= 0;}#endif //QT_NO_QWS_IM/*! \reimp*/void QLineEdit::mousePressEvent( QMouseEvent *e ){#if defined( _WS_QWS_ ) && !defined( QT_NO_QWS_IM )        if ( composeMode() ) {	int clickPos = xPosToCursorPos( e->pos().x() ) - d->preeditStart;	if ( clickPos >= 0 && clickPos <= d->preeditLength ) 	    QPaintDevice::qwsDisplay()->sendIMMouseEvent( clickPos, TRUE ); 		return;    }#endif#ifdef QT_KEYPAD_MODE    if( qt_modalEditingEnabled ) {	if ( e->button() == LeftButton && !isModalEditing() )	    setModalEditing( TRUE );    }#endif    d->dnd_startpos = e->pos();    d->dnd_primed = FALSE;#ifndef QT_NO_POPUPMENU    if ( e->button() == RightButton ) {	QGuardedPtr<QPopupMenu> popup = new QPopupMenu( this );	int id[ 7 ];	id[ IdUndo ] = popup->insertItem( tr( "Undo" ) );	id[ IdRedo ] = popup->insertItem( tr( "Redo" ) );	popup->insertSeparator();#ifndef QT_NO_CLIPBOARD	id[ IdCut ] = popup->insertItem( tr( "Cut" ) );	id[ IdCopy ] = popup->insertItem( tr( "Copy" ) );	id[ IdPaste ] = popup->insertItem( tr( "Paste" ) );#endif	id[ IdClear ] = popup->insertItem( tr( "Clear" ) );	popup->insertSeparator();	id[ IdSelectAll ] = popup->insertItem( tr( "Select All" ) );	popup->setItemEnabled( id[ IdUndo ],				  !this->d->readonly && !this->d->undoList.isEmpty() );	popup->setItemEnabled( id[ IdRedo ],				  !this->d->readonly && !this->d->redoList.isEmpty() );#ifndef QT_NO_CLIPBOARD	popup->setItemEnabled( id[ IdCut ],				  !this->d->readonly && !this->d->readonly && hasMarkedText() );	popup->setItemEnabled( id[ IdCopy ], hasMarkedText() );	popup->setItemEnabled( id[ IdPaste ],				  !this->d->readonly				  && (bool)QApplication::clipboard()->text().length() );#endif	popup->setItemEnabled( id[ IdClear ],				  !this->d->readonly && (bool)text().length() );	int allSelected = minMark() == 0 && maxMark() == (int)text().length();	popup->setItemEnabled( id[ IdSelectAll ],				  (bool)text().length() && !allSelected );	int r = popup->exec( e->globalPos() );	delete (QPopupMenu *) popup;	if ( r == id[ IdUndo ] )	    undoInternal();	else if ( r == id[ IdRedo ] )	    redoInternal();#ifndef QT_NO_CLIPBOARD	else if ( r == id[ IdCut ] )	    cut();	else if ( r == id[ IdCopy ] )	    copy();	else if ( r == id[ IdPaste ] )	    paste();#endif	else if ( r == id[ IdClear ] )	    clear();	else if ( r == id[ IdSelectAll ] )	    selectAll();	return;    }#endif //QT_NO_POPUPMENU    d->inDoubleClick = FALSE;    int newCP = xPosToCursorPos( e->pos().x() );    int m1 = minMark();    int m2 = maxMark();#ifndef QT_NO_DRAGANDDROP    if ( hasMarkedText() && echoMode() == Normal && !( e->state() & ShiftButton ) &&	 e->button() == LeftButton && m1 < newCP && m2 > newCP ) {	d->dndTimer.start( QApplication::startDragTime(), TRUE );	d->dnd_primed = TRUE;	return;    }#endif    m1 = QMIN( m1, cursorPos );    m2 = QMAX( m2, cursorPos );    dragScrolling = FALSE;    if ( e->state() & ShiftButton ) {	newMark( newCP, FALSE );    } else {	markDrag = newCP;	markAnchor = newCP;	newMark( newCP, FALSE );    }    repaintArea( m1, m2 );    d->mousePressed = TRUE;}#ifndef QT_NO_DRAGANDDROP/*  \internal*/void QLineEdit::doDrag(){    d->dnd_primed = FALSE;    QTextDrag *tdo = new QTextDrag( markedText(), this );    tdo->drag();}#endif // QT_NO_DRAGANDDROP/*!\reimp*/void QLineEdit::mouseMoveEvent( QMouseEvent *e ){#ifndef QT_NO_QWS_IM    if ( composeMode() ) {	return;    }#endif#ifndef QT_NO_DRAGANDDROP    if ( d->dndTimer.isActive() ) {	d->dndTimer.stop();	return;    }    if ( d->dnd_primed ) {	if ( ( d->dnd_startpos - e->pos() ).manhattanLength() > QApplication::startDragDistance() )	    doDrag();	return;    }#endif    if ( !(e->state() & LeftButton) )	return;    int margin = frame() ? frameW()*2 : 2;    if ( e->pos().x() < margin || e->pos().x() > width() - margin ) {	if ( !dragScrolling ) {	    dragScrolling = TRUE;	    scrollingLeft = e->pos().x() < margin;	    if ( scrollingLeft )		newMark( xPosToCursorPos( 0 ), FALSE );	    else		newMark( xPosToCursorPos( width() ), FALSE );	    d->dragTimer.start( scrollTime );	}    } else {	dragScrolling = FALSE;	int mousePos = xPosToCursorPos( e->pos().x() );	int m1 = markDrag;	newMark( mousePos, FALSE );	repaintArea( m1, mousePos );    }}/*!\reimp*/void QLineEdit::mouseReleaseEvent( QMouseEvent * e ){#ifdef _WS_QWS_    if ( qt_lineedit_password_visible_on_focus &&	 echoMode() == Password) {	selectAll();	return;    }#endif#if defined( _WS_QWS_ ) && !defined ( QT_NO_QWS_IM )    if ( composeMode() ) {	int clickPos = xPosToCursorPos( e->pos().x() ) - d->preeditStart;	if ( clickPos >= 0 && clickPos <= d->preeditLength )	    QPaintDevice::qwsDisplay()->sendIMMouseEvent( clickPos, FALSE );		return;    }#endif    dragScrolling = FALSE;    d->dnd_primed = FALSE;    if ( d->dndTimer.isActive() ) {	d->dndTimer.stop();	int ncp = xPosToCursorPos( e->pos().x() );	setSelection( ncp, 0 );	setCursorPosition( ncp );	return;    }    if ( d->inDoubleClick ) {	d->inDoubleClick = FALSE;	return;    }    if ( !d->mousePressed )	return;    d->mousePressed = FALSE;#ifndef QT_NO_CLIPBOARD#if defined(_WS_X11_)    copy();#endif    if ( !d->readonly && e->button() == MidButton ) {#if defined(_WS_X11_)	insert( QApplication::clipboard()->text() );#else	if ( style() == MotifStyle )	    insert( QApplication::clipboard()->text() );#endif	return;    }#endif    if ( e->button() != LeftButton )	return;    int margin = frame() ? frameW()*2 : 2;    if ( !QRect( margin, margin,		 width() - 2*margin,		 height() - 2*margin ).contains( e->pos() ) )	return;    int mousePos = xPosToCursorPos( e->pos().x() );    int m1 = markDrag;    newMark( mousePos, FALSE );    repaintArea( m1, mousePos );}/*!\reimp*/void QLineEdit::mouseDoubleClickEvent( QMouseEvent * ){#ifndef QT_NO_QWS_IM    if ( composeMode() ) {	return;    }#endif    d->inDoubleClick = TRUE;    dragScrolling = FALSE;    if ( echoMode() == Password )	selectAll();    else	markWord( cursorPos );}/*!  Moves the cursor leftwards one or more characters.  \sa cursorRight()*/void QLineEdit::cursorLeft( bool mark, int steps ){    cursorRight( mark, -steps );}/*!  Moves the cursor rightwards one or more characters.  \sa cursorLeft()*/void QLineEdit::cursorRight( bool mark, int steps ){    int cp = cursorPos + steps;    cp = QMAX( cp, 0 );    cp = QMIN( cp, (int)tbuf.length() );    if ( cp == cursorPos ) {	if ( !mark )	    deselect();    } else if ( mark ) {	newMark( cp );	blinkOn();    } else {	setCursorPosition( cp );	setSelection( cp, 0 );    }}/*!  Deletes the character to the left of the text cursor and moves the  cursor one position to the left. If a text has been marked by the user  (e.g. by clicking and dragging) the cursor will be put at the beginning  of the marked text and the marked text will be removed.  \sa del()*/void QLineEdit::backspace(){    if ( hasMarkedText() ) {	del();    } else if ( cursorPos > 0 ) {	if ( d->undo && d->needundo && !d->ignoreUndoWithDel ) {	    if ( d->undoList.isEmpty() || d->undoList.last().str != tbuf ) {		d->undoList += QLineEditUndoItem(tbuf, cursorPos );		d->redoList.clear();	    }	}	cursorLeft( FALSE );	del();    }}/*!  Deletes the character on the right side of the text cursor. If a text  has been marked by the user (e.g. by clicking and dragging) the cursor  will be put at the beginning of the marked text and the marked text will  be removed.  \sa backspace()*/void QLineEdit::del(){    QString test( tbuf);    d->ignoreUndoWithDel = TRUE;    if ( d->undo && ( (d->needundo && !d->ignoreUndoWithDel) || hasMarkedText() ) ) {	if ( d->undoList.isEmpty() || d->undoList.last().str != tbuf ) {	    d->undoList += QLineEditUndoItem(tbuf, cursorPos );	    d->redoList.clear();	}    }    if ( hasMarkedText() ) {	test.remove( minMark(), maxMark() - minMark() );	validateAndSet( test, minMark(), minMark(), minMark() );    } else if ( cursorPos != (int)tbuf.length() ) {	test.remove( cursorPos, 1 );	validateAndSet( test, cursorPos, 0, 0 );    }}/*!  Moves the text cursor to the left end of the line. If mark is TRUE text  will be marked towards the first position, if not any marked text will  be unmarked if the cursor is moved.  \sa end()*/void QLineEdit::home( bool mark ){    cursorRight( mark, -cursorPos );}/*!  Moves the text cursor to the right end of the line. If mark is TRUE text  will be marked towards the last position, if not any marked text will  be unmarked if the cursor is moved.  \sa home()*/void QLineEdit::end( bool mark ){    cursorRight( mark, tbuf.length()-cursorPos );}void QLineEdit::newMark( int pos, bool c ){    if ( markDrag != pos || cursorPos != pos )	d->pmDirty = TRUE;    markDrag = pos;    setCursorPosition( pos );#if !defined(QT_NO_CLIPBOARD) && defined(_WS_X11_)    if ( c )	copy();#else    Q_UNUSED(c);#endif}void QLineEdit::markWord( int pos ){    int i = pos - 1;    while ( i >= 0 && tbuf[i].isPrint() && !tbuf[i].isSpace() )	i--;    i++;    int newAnchor = i;    i = pos;    while ( tbuf[i].isPrint() && !tbuf[i].isSpace() )	i++;    if ( style() != MotifStyle ) {	while( tbuf[i].isSpace() )	    i++;	setCursorPosition( i );    }    int newDrag = i;    setSelection( newAnchor, newDrag - newAnchor );#ifndef QT_NO_CLIPBOARD#if defined(_WS_X11_)    copy();#endif#endif}#ifndef QT_NO_CLIPBOARD/*! Copies the marked text to the clipboard, if there is any and  if echoMode() is Normal.  \sa cut() paste()*/void QLineEdit::copy() const{    QString t = markedText();#ifdef QT_KEYPAD_MODE //QT_KEYPAD_MODE    // In keypad input mode, copy will copy the entire field if there is no    // marked text.    if ( t.isEmpty() )	t = text();#endif    if ( !t.isEmpty() && echoMode() == Normal ) {	disconnect( QApplication::clipboard(), SIGNAL(dataChanged()), this, 0);	QApplication::clipboard()->setText( t );	connect( QApplication::clipboard(), SIGNAL(dataChanged()),		 this, SLOT(clipboardChanged()) );    }}/*!  Inserts the clipboard's text at the cursor position, deleting any  previous marked text.  If the end result is not acceptable for the current validator,  nothing happens.  \sa copy() cut()*/void QLineEdit::paste(){    insert( QApplication::clipboard()->text() );}/*!  Copies the marked text to the clipboard and deletes it, if there is  any.  If the current validator disallows deleting the marked text, cut()  will copy it but not delete it.  \sa copy() paste()*/void QLineEdit::cut(){    QString t = markedText();    if ( !t.isEmpty() ) {	copy();	del();    }}#endif/*!  Sets the alignment of the line edit. Possible Values are Qt::AlignLeft,  Qt::AlignRight and Qt::Align(H)Center - see Qt::AlignmentFlags.  \sa alignment()*/void QLineEdit::setAlignment( int flag ){    if ( flag == alignmentFlag )	return;    if ( flag == Qt::AlignRight ||	 flag == Qt::AlignCenter ||	 flag == Qt::AlignHCenter ||	 flag == Qt::AlignLeft ) {	alignmentFlag = flag;	updateOffset();	update();    }}/*!  Returns the alignment of the line edit. Possible Values  are Qt::AlignLeft, Qt::AlignRight and Qt::Align(H)Center.  \sa setAlignment(), Qt::AlignmentFlags*/int QLineEdit::alignment() const{    return alignmentFlag;}/*!  This private slot is activated when this line edit owns the clipboard and  some other widget/application takes over the clipboard. (X11 only)*/void QLineEdit::clipboardChanged(){#if defined(_WS_X11_)    disconnect( QApplication::clipboard(), SIGNAL(dataChanged()),		this, SLOT(clipboardChanged()) );    deselect();#endif}int QLineEdit::lastCharVisible() const{    int tDispWidth = width() - (frameW()*2 + 4);    return xPosToCursorPos( tDispWidth );}int QLineEdit::minMark() const{    return markAnchor < markDrag ? markAnchor : markDrag;}int QLineEdit::maxMark() const{    return markAnchor > markDrag ? markAnchor : markDrag;}/*!  Sets the line edit to draw itself inside a frame if \a  enable is TRUE, and to draw itself without any frame if \a enable is  FALSE.  The default is TRUE.  \sa frame()*/void QLineEdit::setFrame( bool enable ){    if ( d->frame == enable )	return;    d->frame = enable;    d->pmDirty = TRUE;    updateOffset();    update();}/*!  Returns TRUE if the line edit draws itself inside a frame, FALSE  if it draws itself without any frame.  The default is to use a frame.  \sa setFrame()*/bool QLineEdit::frame() const{    return d ? d->frame : TRUE;}int QLineEdit::frameW() const{    return frame() ? style().defaultFrameWidth() : 0;}/*!  Sets the echo mode of the line edit widget.  The echo modes available are:  <ul>  <li> \c Normal - display characters as they are entered.  This is	the default.  <li> \c NoEcho - do not display anything. This may be appropriate	for passwords where even the length of the password should	be kept secret.  <li> \c Password - display asterisks instead of the characters	actually entered.  </ul>  The widget's display, and the ability to copy or drag the  text is affected by this setting.  \sa echoMode() EchoMode displayText()*/void QLineEdit::setEchoMode( EchoMode mode ){    if ( d->mode == mode )	return;    d->mode = mode;    d->pmDirty = TRUE;    updateOffset();    update();}/*!  Returns the echo mode of the line edit.  \sa setEchoMode() EchoMode*/QLineEdit::EchoMode QLineEdit::echoMode() const{    return d->mode;}/*!  Enables or disables read-only mode, where the user can cut-and-paste  or drag-and-drop the text, but cannot edit it.  They never see a cursor in this case.  \sa setEnabled(), isReadOnly()*/void QLineEdit::setReadOnly( bool enable )

⌨️ 快捷键说明

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