📄 qmultilineedit.cpp
字号:
{ bool oldUndo = isUndoEnabled(); setUndoEnabled( FALSE ); bool oldAuto = autoUpdate(); setAutoUpdate( FALSE ); bool b = signalsBlocked(); blockSignals( TRUE ); clear(); CLEAR_UNDO blockSignals( b ); insertLine( s, -1 ); emit textChanged(); setAutoUpdate(oldAuto); if ( autoUpdate() ) update(); setUndoEnabled( oldUndo );}/*! Appends \a s to the text.*/void QMultiLineEdit::append( const QString &s ){ bool oldUndo = isUndoEnabled(); setUndoEnabled( FALSE ); insertLine( s, -1 ); setUndoEnabled( oldUndo ); emit textChanged();}/*! \reimpPasses wheel events to the vertical scrollbar.*/void QMultiLineEdit::wheelEvent( QWheelEvent *e ){ QApplication::sendEvent( verticalScrollBar(), e);}/*! The key press event handler converts a key press to some line editor action. Here are the default key bindings when isReadOnly() is FALSE: <ul> <li><i> Left Arrow </i> Move the cursor one character leftwards <li><i> Right Arrow </i> Move the cursor one character rightwards <li><i> Up Arrow </i> Move the cursor one line upwards <li><i> Down Arrow </i> Move the cursor one line downwards <li><i> Page Up </i> Move the cursor one page upwards <li><i> Page Down </i> Move the cursor one page downwards <li><i> Backspace </i> Delete the character to the left of the cursor <li><i> Home </i> Move the cursor to the beginning of the line <li><i> End </i> Move the cursor to the end of the line <li><i> Delete </i> Delete the character to the right of the cursor <li><i> Shift - Left Arrow </i> Mark text one character leftwards <li><i> Shift - Right Arrow </i> Mark text one character rightwards <li><i> Control-A </i> Move the cursor to the beginning of the line <li><i> Control-B </i> Move the cursor one character leftwards <li><i> Control-C </i> Copy the marked text to the clipboard <li><i> Control-D </i> Delete the character to the right of the cursor <li><i> Control-E </i> Move the cursor to the end of the line <li><i> Control-F </i> Move the cursor one character rightwards <li><i> Control-H </i> Delete the character to the left of the cursor <li><i> Control-K </i> Delete to end of line <li><i> Control-N </i> Move the cursor one line downwards <li><i> Control-P </i> Move the cursor one line upwards <li><i> Control-V </i> Paste the clipboard text into line edit <li><i> Control-X </i> Cut the marked text, copy to clipboard <li><i> Control-Z </i> Undo the last operation <li><i> Control-Y </i> Redo the last operation <li><i> Control - Left Arrow </i> Move the cursor one word leftwards <li><i> Control - Right Arrow </i> Move the cursor one word rightwards <li><i> Control - Up Arrow </i> Move the cursor one word upwards <li><i> Control - Down Arrow </i> Move the cursor one word downwards <li><i> Control - Home Arrow </i> Move the cursor to the beginning of the text <li><i> Control - End Arrow </i> Move the cursor to the end of the text </ul> In addition, the following key bindings are used on Windows: <ul> <li><i> Shift - Delete </i> Cut the marked text, copy to clipboard <li><i> Shift - Insert </i> Paste the clipboard text into line edit <li><i> Control - Insert </i> Copy the marked text to the clipboard </ul> All other keys with valid ASCII codes insert themselves into the line. Here are the default key bindings when isReadOnly() is TRUE: <ul> <li><i> Left Arrow </i> Scrolls the table rightwards <li><i> Right Arrow </i> Scrolls the table rightwards <li><i> Up Arrow </i> Scrolls the table one line downwards <li><i> Down Arrow </i> Scrolls the table one line upwards <li><i> Page Up </i> Scrolls the table one page downwards <li><i> Page Down </i> Scrolls the table one page upwards <li><i> Control-C </i> Copy the marked text to the clipboard </ul>*/void QMultiLineEdit::keyPressEvent( QKeyEvent *e ){ textDirty = FALSE; d->isHandlingEvent = TRUE; int unknown = 0; if ( readOnly ) { int pageSize = viewHeight() / cellHeight(); switch ( e->key() ) { case Key_Left: setXOffset( xOffset() - viewWidth()/10 ); break; case Key_Right: setXOffset( xOffset() + viewWidth()/10 ); break; case Key_Up: setTopCell( topCell() - 1 ); break; case Key_Down: setTopCell( topCell() + 1 ); break; case Key_Home: setCursorPosition(0,0, e->state() & ShiftButton ); break; case Key_End: setCursorPosition( numLines()-1, lineLength( numLines()-1 ), e->state() & ShiftButton ); break; case Key_Next: setTopCell( topCell() + pageSize ); break; case Key_Prior: setTopCell( QMAX( topCell() - pageSize, 0 ) ); break;#ifndef QT_NO_CLIPBOARD case Key_C: if ( echoMode() == Normal && (e->state()&ControlButton) ) copy(); else unknown++; break; case Key_F16: // Copy key on Sun keyboards if ( echoMode() == Normal ) copy(); else unknown++; break;#endif default: unknown++; } if ( unknown ) e->ignore(); d->isHandlingEvent = FALSE; return; } if ( e->text().length() && e->key() != Key_Return && e->key() != Key_Enter && e->key() != Key_Delete && e->key() != Key_Backspace && (!e->ascii() || e->ascii()>=32) ) { insert( e->text() ); //QApplication::sendPostedEvents( this, QEvent::Paint ); if ( textDirty ) emit textChanged(); d->isHandlingEvent = FALSE; return; } if ( e->state() & ControlButton ) { switch ( e->key() ) { case Key_A: home( e->state() & ShiftButton ); break; case Key_B: cursorLeft( e->state() & ShiftButton ); break;#ifndef QT_NO_CLIPBOARD case Key_C: if ( echoMode() == Normal ) copy(); break;#endif case Key_D: del(); break; case Key_E: end( e->state() & ShiftButton ); break; case Key_Left: cursorWordBackward( e->state() & ShiftButton ); break; case Key_Right: cursorWordForward( e->state() & ShiftButton ); break; case Key_Up: cursorUp( e->state() & ShiftButton ); break; case Key_Down: cursorDown( e->state() & ShiftButton ); break; case Key_Home: setCursorPosition(0,0, e->state() & ShiftButton ); break; case Key_End: setCursorPosition( numLines()-1, lineLength( numLines()-1 ), e->state() & ShiftButton ); break; case Key_F: cursorRight( e->state() & ShiftButton ); break; case Key_H: backspace(); break; case Key_K: killLine(); break; case Key_N: cursorDown( e->state() & ShiftButton ); break; case Key_P: cursorUp( e->state() & ShiftButton ); break;#ifndef QT_NO_CLIPBOARD case Key_V: paste(); break; case Key_X: cut(); break;#endif case Key_Z: undo(); break; case Key_Y: redo(); break;#if defined (_WS_WIN_) case Key_Insert: copy();#endif default: unknown++; } } else { switch ( e->key() ) { case Key_Left: cursorLeft( e->state() & ShiftButton ); break; case Key_Right: cursorRight( e->state() & ShiftButton ); break; case Key_Up: cursorUp( e->state() & ShiftButton ); break; case Key_Down: cursorDown( e->state() & ShiftButton ); break; case Key_Backspace: backspace(); break; case Key_Home: home( e->state() & ShiftButton ); break; case Key_End: end( e->state() & ShiftButton ); break; case Key_Delete:#if defined (_WS_WIN_) if ( e->state() & ShiftButton ) { cut(); break; }#endif del(); break; case Key_Next: pageDown( e->state() & ShiftButton ); break; case Key_Prior: pageUp( e->state() & ShiftButton ); break; case Key_Enter: case Key_Return: newLine(); emit returnPressed(); break; case Key_Tab: insert( e->text() ); break;#if defined (_WS_WIN_) case Key_Insert: if ( e->state() & ShiftButton ) paste(); else unknown++; break;#endif case Key_F14: // Undo key on Sun keyboards undo(); break;#ifndef QT_NO_CLIPBOARD case Key_F16: // Copy key on Sun keyboards if ( echoMode() == Normal ) copy(); break; case Key_F18: // Paste key on Sun keyboards paste(); break; case Key_F20: // Paste key on Sun keyboards cut(); break;#endif default: unknown++; } } if ( textDirty ) emit textChanged(); if ( unknown ) // unknown key e->ignore(); d->isHandlingEvent = FALSE;}/*! Moves the cursor one page down. If \a mark is TRUE, the text is marked.*/void QMultiLineEdit::pageDown( bool mark ){ bool oldAuto = autoUpdate(); if ( mark ) setAutoUpdate( FALSE ); if ( partiallyInvisible( cursorY ) ) cursorY = topCell(); int delta = cursorY - topCell(); int pageSize = viewHeight() / cellHeight(); int newTopCell = QMIN( topCell() + pageSize, numLines() - 1 - pageSize ); if ( pageSize >= numLines() ) { // quick fix to handle small texts newTopCell = topCell(); } if ( !curXPos ) curXPos = mapToView( cursorX, cursorY ); int oldY = cursorY; if ( mark && !hasMarkedText() ) { markAnchorX = cursorX; markAnchorY = cursorY; } if ( newTopCell != topCell() ) { cursorY = newTopCell + delta; cursorX = mapFromView( curXPos, cursorY ); if ( mark ) newMark( cursorX, cursorY, FALSE ); setTopCell( newTopCell ); } else if ( cursorY != (int)contents->count() - 1) { // just move the cursor cursorY = QMIN( cursorY + pageSize, numLines() - 1); cursorX = mapFromView( curXPos, cursorY ); if ( mark ) newMark( cursorX, cursorY, FALSE ); makeVisible(); } if ( oldAuto ) if ( mark ) { setAutoUpdate( TRUE ); update(); } else { updateCell( oldY, 0, FALSE ); } if ( !mark ) turnMark( FALSE );}/*! Moves the cursor one page up. If \a mark is TRUE, the text is marked.*/void QMultiLineEdit::pageUp( bool mark ){ bool oldAuto = autoUpdate(); if ( mark ) setAutoUpdate( FALSE ); if ( partiallyInvisible( cursorY ) ) cursorY = topCell(); int delta = cursorY - topCell(); int pageSize = viewHeight() / cellHeight(); bool partial = delta == pageSize && viewHeight() != pageSize * cellHeight(); int newTopCell = QMAX( topCell() - pageSize, 0 ); if ( pageSize > numLines() ) { // quick fix to handle small texts newTopCell = 0; delta = 0; } if ( mark && !hasMarkedText() ) { markAnchorX = cursorX; markAnchorY = cursorY; } if ( !curXPos ) curXPos = mapToView( cursorX, cursorY ); int oldY = cursorY; if ( newTopCell != topCell() ) { cursorY = QMIN( newTopCell + delta, numLines() - 1 ); if ( partial ) cursorY--; cursorX = mapFromView( curXPos, cursorY ); if ( mark ) newMark( cursorX, cursorY, FALSE ); setTopCell( newTopCell ); } else { // just move the cursor cursorY = QMAX( cursorY - pageSize, 0 ); cursorX = mapFromView( curXPos, cursorY ); if ( mark ) newMark( cursorX, cursorY, FALSE ); } if ( oldAuto ) if ( mark ) { setAutoUpdate( TRUE ); update(); } else { updateCell( oldY, 0, FALSE ); } if ( !mark ) turnMark( FALSE );}// THE CORE INSERTION FUNCTIONvoid QMultiLineEdit::insertAtAux( const QString &txt, int line, int col, bool mark ){ dummy = FALSE; d->blinkTimer->stop(); cursorOn = TRUE; int oldw = contentsRect().width(); line = QMAX( QMIN( line, numLines() - 1), 0 ); col = QMAX( QMIN( col, lineLength( line )), 0 ); QString itxt = txt; QMultiLineEditRow *row = contents->at( line ); if ( d->maxlen >= 0 && length() + int(txt.length()) > d->maxlen ) itxt.truncate( d->maxlen - length() ); row->s.insert( uint(col), itxt ); if ( mark ) { markAnchorX = col; markAnchorY = line; } if ( cursorX == col && cursorY == line ) { cursorX += itxt.length(); } QFontMetrics fm( font() ); if ( !WORD_WRAP || ( col == 0 && itxt.contains('\n') == int(itxt.length())) ) wrapLine( line, 0 ); else if ( WORD_WRAP && itxt.find('\n')<0 && itxt.find('\t')<0 && ( ( DYNAMIC_WRAP && fm.width( itxt ) + row->w < contentsRect().width() - 2*d->lr_marg - d->marg_extra ) || ( FIXED_WIDTH_WRAP && ( d->wrapcol < 0 || fm.width( itxt ) + row->w < d->wrapcol ) ) || ( FIXED_COLUMN_WRAP && ( d->wrapcol < 0 || int(row->s.length()) < d->wrapcol ) ) ) && ( itxt.find(' ') < 0 || row->s.find(' ') >= 0 && row->s.find(' ') < col ) ){ row->w = textWidth( row->s ); setWidth( QMAX( maxLineWidth(), row->w) ); updateCell( line, 0, FALSE ); } else { if ( line > 0 && !contents->at( line-1)->newline ) rebreakParagraph( line-1 ); else rebreakParagraph( line ); } if ( mark ) newMark( cursorX, cursorY, FALSE ); setNumRowsAndTruncate(); textDirty = TRUE; d->edited = TRUE; if ( autoUpdate() ) { makeVisible(); d->blinkTimer->start( QApplication::cursorFlashTime() / 2, FALSE ); if ( DYNAMIC_WRAP && oldw != contentsRect().width() ) { setAutoUpdate( FALSE ); rebreakAll(); setAutoUpdate( TRUE ); update(); } }}/*! Inserts \a txt at line number \a line. If \a line is less than zero, or larger than the number of rows, the new text is put at the end. If \a txt contains newline characters, several lines are inserted. The cursor position is not changed.*/void QMultiLineEdit::insertLine( const QString &txt, int line ){ QString s = txt; int oldXPos = cursorX; int oldYPos = cursorY; if ( line < 0 || line >= int( contents->count() ) ) { if ( !dummy ) contents->append( new QMultiLineEditRow(QString::fromLatin1(""), 0) ); insertAt( s, numLines()-1, 0 ); } else { s.append('\n'); insertAt( s, line, 0 ); } cursorX = oldXPos; cursorY = oldYPos;}/*! Deletes the line at line number \a line. If \a line is less than zero, or larger than the number of lines, no line is deleted.*/void QMultiLineEdit::removeLine( int line ){ CLEAR_UNDO if ( line >= numLines() ) return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -