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

📄 qlineedit.cpp

📁 qtopia-phone-2.2.0下公共的控件实现源代码。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	    }	    break;	case Key_E:	    end( e->state() & ShiftButton );	    break;	case Key_F:	    cursorRight( e->state() & ShiftButton );	    break;	case Key_H:	    if ( !d->readonly ) {		d->ignoreUndoWithDel = ignoreUndoWithDel;		backspace();	    }	    break;	case Key_K:	    if ( !d->readonly && cursorPos < (int)tbuf.length() ) {		QString t( tbuf );		if ( d->undoList.isEmpty() || d->undoList.last().str != tbuf ) {		    d->undoList += QLineEditUndoItem(tbuf, cursorPos );		    d->redoList.clear();		}		t.truncate( cursorPos );		validateAndSet( t, cursorPos, cursorPos, cursorPos );	    }	    break;#ifndef QT_NO_CLIPBOARD	case Key_V:	    if ( !d->readonly )		insert( QApplication::clipboard()->text() );	    break;	case Key_X:	    if ( !d->readonly && hasMarkedText() && echoMode() == Normal ) {		copy();		del();	    }	    break;#if defined (_WS_WIN_)	case Key_Insert:	    copy();	    break;#endif#endif	case Key_Right:	    if ( echoMode() == Normal )		cursorWordForward( e->state() & ShiftButton );	    else		end( e->state() & ShiftButton );	    break;	case Key_Left:	    if ( echoMode() == Normal )		cursorWordBackward( e->state() & ShiftButton );	    else		home( e->state() & ShiftButton );	    break;	case Key_Z:	    if ( !d->readonly )		undoInternal();	    break;	case Key_Y:	    if ( !d->readonly )		redoInternal();	    break;	default:	    unknown++;	}    } else {	switch ( e->key() ) {	case Key_Left:	    cursorLeft( e->state() & ShiftButton );	    break;	case Key_Right:	    cursorRight( e->state() & ShiftButton );	    break;#ifdef QT_KEYPAD_MODE	case Key_No:	    if( qt_modalEditingEnabled ) {		setText(d->orgTxt);		setModalEditing( FALSE );	    } else {		++unknown;	    }	    break;	case Key_Back:	    if( qt_modalEditingEnabled ) {		if (!e->isAutoRepeat()) {		    if (isReadOnly()) {			setModalEditing( FALSE );		    } else if (tbuf.length() == 0) {			setText(d->orgTxt);			setModalEditing( FALSE );		    } else if ( d->deleteAllTimerId < 0) {			d->deleteAllTimerId = startTimer(750);		    } else {			++unknown;		    }		}	    } else {		++unknown;	    }	    break;#endif	case Key_Backspace:	    if ( !d->readonly ) {		d->ignoreUndoWithDel = ignoreUndoWithDel;		backspace();	    }	    break;	case Key_Home:	    home( e->state() & ShiftButton );	    break;	case Key_End:	    end( e->state() & ShiftButton );	    break;	case Key_Delete:	    if ( !d->readonly ) {#if defined (_WS_WIN_)		if ( e->state() & ShiftButton ) {		    cut();		    break;		}#endif		d->ignoreUndoWithDel = ignoreUndoWithDel;		del();	    }	    break;#if defined (_WS_WIN_)	case Key_Insert:	    if ( !d->readonly && e->state() & ShiftButton )		paste();	    else		unknown++;	    break;#endif	case Key_F14: // Undo key on Sun keyboards	    if ( !d->readonly )		undoInternal();	    break;#ifndef QT_NO_CLIPBOARD	case Key_F16: // Copy key on Sun keyboards	    copy();	    break;	case Key_F18: // Paste key on Sun keyboards	    if ( !d->readonly )		insert( QApplication::clipboard()->text() );	    break;	case Key_F20: // Cut key on Sun keyboards	    if ( !d->readonly && hasMarkedText() && echoMode() == Normal ) {		copy();		del();	    }	    break;#endif	default:	    unknown++;	}    }    if ( unknown ) {				// unknown key	d->needundo = needundo;	e->ignore();	return;    }}/*!\reimp*/void QLineEdit::focusInEvent( QFocusEvent * e){    d->pmDirty = TRUE;    cursorOn = FALSE;    blinkOn();#ifdef QT_KEYPAD_MODE    if( !qt_modalEditingEnabled )#endif    if ( e->reason() == QFocusEvent::Tab )	selectAll();    d->pmDirty = TRUE;    repaint( FALSE );}/*!\reimp*/void QLineEdit::focusOutEvent( QFocusEvent * e ){#ifdef QT_KEYPAD_MODE    if( qt_modalEditingEnabled ) {	if ( isModalEditing() ) // && e->reason() != QFocusEvent::Popup )	    setModalEditing( FALSE );    }#endif    if ( e->reason() != QFocusEvent::ActiveWindow	 && e->reason() != QFocusEvent::Popup )	deselect();    d->dragTimer.stop();    if ( cursorOn )	blinkSlot();    d->pmDirty = TRUE;#ifdef _WS_QWS_    if ( qt_lineedit_password_visible_on_focus &&	 d->resumePassword ) {	d->resumePassword = false;	setEchoMode( Password );    }#endif    repaint( FALSE );}/*!\reimp*/void QLineEdit::leaveEvent( QEvent * ){}extern QPoint qt_backgroundOffset(const QWidget *w);/*!\reimp*/void QLineEdit::paintEvent( QPaintEvent *e ){    if ( d->offsetDirty )	updateOffset();    if ( !d->pm || d->pmDirty ) {	makePixmap();	if ( d->pm->isNull() ) {	    delete d->pm;	    d->pm = 0;	    return;	}	QPainter p( d->pm, this );	const QColorGroup & g = colorGroup();	QBrush bg = g.brush((isEnabled()) ? QColorGroup::Base :			    QColorGroup::Background);#ifdef QT_KEYPAD_MODE	if( qt_modalEditingEnabled ) {	    if ( !isModalEditing() ) {		bg = g.brush(QColorGroup::Background);		QPoint off;		if (parentWidget())		    off = qt_backgroundOffset(parentWidget()) + pos();		p.setBrushOrigin(-(off.x()+frameW()), -(off.y()+frameW()));	    }	}#endif	QFontMetrics fm = fontMetrics();	int markBegin = minMark();	int markEnd = maxMark();#ifndef QT_NO_QWS_IM	if ( d->preeditLength > 0 && d->preeditSelLen > 0 ) {	    markBegin = d->preeditStart + d->preeditCPos;	    markEnd = markBegin + d->preeditSelLen;	}#endif	p.fillRect( 0, 0, width(), height(), bg );	QString display = displayText();	QString before = display.mid( 0, markBegin );	QString marked = display.mid( markBegin, markEnd - markBegin );	QString after = display.mid( markEnd, display.length() );	int y = (d->pm->height() + fm.height())/2 - fm.descent() - 1 ;	int x = offset + 2;	int w;	w = fm.width( before );#ifndef QT_NO_QWS_IM	//We're lazy and also fill in the about-to-be-selected part	if ( composeMode() ) {	    QString beforePre = display.mid( 0, d->preeditStart );	    QString preed = display.mid( d->preeditStart, d->preeditLength );	    int bw = fm.width( beforePre );	    int pw = fm.width( preed );	    if ( !qt_im_compose_background ) {		p.setPen( g.text() );		p.drawLine( x+bw, y+2, x+bw+pw, y+2 ); 	    } else {		p.fillRect( x+bw, 0, pw, height(), *qt_im_compose_background );	    }	}#endif	if ( x < d->pm->width() && x + w >= 0 ) {	    p.setPen( g.text() );	    p.drawText( x, y, before );	}	x += w;	w = fm.width( marked );	if ( x < d->pm->width() && x + w >= 0 ) {	    p.fillRect( x, y-fm.ascent()-1, w, fm.height()+2,			g.brush( QColorGroup::Highlight ) );	    p.setPen( g.highlightedText() );	    p.drawText( x, y, marked );	}	x += w;	w = fm.width( after );	if ( x < d->pm->width() && x + w >= 0 ) {	    p.setPen( g.text() );	    p.drawText( x, y, after );	}	// ... x += w;	p.setPen( g.text() );	d->cursorRepaintRect.setTop( y + frameW() - fm.ascent() );	d->cursorRepaintRect.setHeight( fm.height() );	d->pmDirty = FALSE;    }    QPainter p( this );    if ( frame() ) {	style().drawPanel( &p, 0, 0, width(), height(), colorGroup(),			   TRUE, style().defaultFrameWidth() );	p.drawPixmap( frameW(), frameW(), *d->pm );    } else {	p.drawPixmap( 0, 0, *d->pm );    }    if ( hasFocus() ) {	d->cursorRepaintRect	    = QRect( offset + frameW() +		     fontMetrics().width( displayText().left( cursorPos ) ),		     d->cursorRepaintRect.top(),		     5, d->cursorRepaintRect.height() );	int curYTop = d->cursorRepaintRect.y();	int curYBot = d->cursorRepaintRect.bottom();	int curXPos = d->cursorRepaintRect.x() + 2;	if ( !d->readonly && cursorOn &&#ifdef QT_KEYPAD_MODE	     (!qt_modalEditingEnabled || isModalEditing()) &&#endif	     d->cursorRepaintRect.intersects( e->rect() ) ) {	    p.setPen( colorGroup().text() );	    p.drawLine( curXPos, curYTop, curXPos, curYBot );	    if ( style() != WindowsStyle ) {		p.drawLine( curXPos - 2, curYTop, curXPos + 2, curYTop );		p.drawLine( curXPos - 2, curYBot, curXPos + 2, curYBot );	    }	}#ifdef QT_KEYPAD_MODE	if( qt_modalEditingEnabled ) {	    if(!isModalEditing()) {		style().drawFocusRect( &p, QRect(0, 0, width(), height()), 			colorGroup(), &colorGroup().highlight(), frame());	    }	}#endif	// Now is the optimal time to set this - all the repaint-minimization	// then also reduces the number of calls to setMicroFocusHint().	if ( inBlinkOn )	    setMicroFocusHint( curXPos, curYTop, 1, curYBot-curYTop+1 );    } else {	delete d->pm;	d->pm = 0;    }}/*!\reimp*/void QLineEdit::resizeEvent( QResizeEvent * ){    delete d->pm;    d->pm = 0;    offset = 0;    updateOffset();}/*! \reimp*/bool QLineEdit::event( QEvent * e ){    switch ( e->type() ) {    case QEvent::AccelOverride:	if (!d->readonly ) {	    QKeyEvent* ke = (QKeyEvent*) e;	    if ( ke->state() == NoButton ) {		if ( ke->key() < Key_Escape ) {		    ke->accept();		} else {		    switch ( ke->key() ) {		    case Key_Delete:		    case Key_Home:		    case Key_End:		    case Key_Backspace:			ke->accept();		    default:			break;		    }		}	    } else if ( ke->state() & ControlButton ) {		switch ( ke->key() ) {		    // Those are too frequently used for application functionality		    /*	    case Key_A:				    case Key_B:			    case Key_D:			    case Key_E:			    case Key_F:			    case Key_H:			    case Key_K:		    */		case Key_C:		case Key_V:		case Key_X:	    		case Key_Y:		case Key_Z:	    		case Key_Left:		case Key_Right:#if defined (_WS_WIN_)		case Key_Insert:#endif		    ke->accept();		default:		    break;		}	    }	}	break;#ifndef QT_NO_QWS_IM    case QEvent::IMStart: {#ifdef QT_KEYPAD_MODE	if( qt_modalEditingEnabled ) {	    if ( !isModalEditing() ) {		setModalEditing( TRUE );		clear();	    }	}#ifdef _WS_QWS_	if ( qt_lineedit_password_visible_on_focus &&	     echoMode() == Password ) {	    clear();	    setEchoMode( Normal );	    d->resumePassword = TRUE;	}#endif	#endif // QT_KEYPAD_MODE	QIMEvent *i = (QIMEvent *) e;	imStartEvent(i);	if (! i->isAccepted())	    return FALSE;    }    break;    case QEvent::IMCompose: {	QIMEvent *i = (QIMEvent *) e;	imComposeEvent(i);	if (! i->isAccepted())	    return FALSE;    }    break;    case QEvent::IMEnd: {	QIMEvent *i = (QIMEvent *) e;	imEndEvent(i);	if (! i->isAccepted())	    return FALSE;    }    break;#endif //QT_NO_QWS_IM#ifdef QT_KEYPAD_MODE    case QEvent::EnterModalEdit:	if( qt_modalEditingEnabled ) {	    end( FALSE );	    d->orgTxt = tbuf;	    d->pmDirty = TRUE;#ifdef _WS_QWS_	    if ( qt_lineedit_password_visible_on_focus &&		 echoMode() == Password )		selectAll();#endif	    repaint(FALSE);	}	break;    case QEvent::LeaveModalEdit:	if( qt_modalEditingEnabled ) {#ifdef _WS_QWS_	    if ( qt_lineedit_password_visible_on_focus &&		 d->resumePassword ) {		d->resumePassword = false;		setEchoMode( Password );	    }#endif	    home( FALSE );	    d->pmDirty = TRUE;	    repaint(FALSE);	}	break;    case QEvent::KeyRelease:	if( qt_modalEditingEnabled ) {	    QKeyEvent *ke = (QKeyEvent *)e;	    if ( !ke->isAutoRepeat() && !d->readonly &&		 ke->key() == Key_Back &&		 d->deleteAllTimerId > 0) {		killTimer(d->deleteAllTimerId);		d->deleteAllTimerId = -1;		backspace();	    }	}	break;    case QEvent::Timer: 	if( qt_modalEditingEnabled ) {	    QTimerEvent *te = (QTimerEvent *)e;	    if ( te->timerId() == d->deleteAllTimerId ) {		killTimer(d->deleteAllTimerId);		d->deleteAllTimerId = -1;		clear();	    }	}	break;#endif    default:	break;    }     return QWidget::event( e );}#ifndef QT_NO_QWS_IM//### ??? Undo/Redo for IM events void QLineEdit::imStartEvent( QIMEvent *e ){    /*    setMicroFocusHint( d->cursor->x() - d->offset, d->cursor->y(), 0,	d->cursor->paragraph()->rect().height(), TRUE );    */    //remove selected text:    insert(""); //####    d->preeditStart = cursorPos;    d->preeditLength = 0;    e->accept();//    qDebug( "%p QLineEdit::imStartEvent txt %s cp %d sl %d cursor %d", this,//	      e->text().latin1(), //	      e->cursorPos(), e->selectionLength(), cursorPos );    d->mousePressed = FALSE; //### stop any dragging}void QLineEdit::imComposeEvent( QIMEvent *e ){    if ( isReadOnly() )	return;//    qDebug( "QLineEdit::imComposeEvent txt %s cp %d sl %d", //    e->text().latin1(), //	      e->cursorPos(), e->selectionLength() );    QString test( tbuf);        test.remove( d->preeditStart, d->preeditLength );	    d->preeditLength = e->text().length();    test.insert( d->preeditStart, e->text() );    d->preeditCPos = e->cursorPos();    d->preeditSelLen = e->selectionLength();    cursorPos = d->preeditStart + e->cursorPos();    tbuf = test;    d->pmDirty = TRUE;    d->offsetDirty = TRUE;    blinkOn();    e->accept();#ifdef _WS_QWS_    if ( qt_lineedit_password_visible_on_focus &&	 echoMode() == Password ) {	setEchoMode( Normal );	d->resumePassword = TRUE;    }#endif    ed = TRUE;    emit textChanged( tbuf );}void QLineEdit::forceIMEnd(){    if (composeMode()) {	// force an end.	d->preeditStart = -1;	d->preeditLength = 0;	d->preeditCPos = -1;	d->preeditSelLen = 0;	// possible the below should generate an event to result in an imEnd	// event, but that just seems too complicated.#ifdef _WS_QWS_	QInputContext::notifyWidgetForcedIMEnd(this);#endif    }}/*! \reimp */void QLineEdit::imEndEvent( QIMEvent *e ){//    qDebug( "%p QLineEdit::imEndEvent txt %s cp %d sl %d", this,//	      e->text().latin1(), //	      e->cursorPos(), e->selectionLength() );    QString test( tbuf);#if 0    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();	}    }#endif        test.remove( d->preeditStart, d->preeditLength );    test.insert( d->preeditStart, e->text() );    int newcp = d->preeditStart+e->text().length();    d->preeditStart = -1;    d->preeditLength = 0;

⌨️ 快捷键说明

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