📄 qeditor.cpp
字号:
int markBeginX, markBeginY; int markEndX, markEndY; if (!getMarkedRegion(&markBeginY, &markBeginX, &markEndY, &markEndX)) return QString(); if (markBeginY == markEndY) { //just one line QString *s = getString(markBeginY); return s->mid(markBeginX, markEndX - markBeginX); } else { //multiline QString *firstS, *lastS; firstS = getString(markBeginY); lastS = getString(markEndY); int i; QString tmp; if (firstS) tmp += firstS->mid(markBeginX); if (contents->at(markBeginY)->newline) tmp += '\n'; for (i = markBeginY + 1; i < markEndY; i++) { tmp += *getString(i); if (contents->at(i)->newline) tmp += '\n'; } if (lastS) { tmp += lastS->left(markEndX); } else { tmp.truncate(tmp.length() - 1); } return tmp; }}/*= Returns the text at line number \a line (possibly the empty string), or a \link QString::operator!() null string\endlink if \a line is invalid.*/QString QEditor::textLine(int line) const{ QString *s = getString(line); if (s) { if (s->isNull()) return QString::fromLatin1(""); else return *s; } else return QString::null;}void QEditor::setTextLine(int line, QString & s){ int offset = positionToOffsetInternal(line, 0); addUndoCmd(new QBeginCommand); d->undoList.append(new QDelTextCmd(offset, contents->at(line)->s)); contents->at(line)->s = QString::fromLatin1(""); insertAt(s, line, 0, false); colorize(line); addUndoCmd(new QEndCommand); emit textChanged();}/*= Returns a copy of the whole text. If the multi line edit contains no text, a \link QString::operator!() null string\endlink is returned.*/QString QEditor::text(){ static QString tmp; colorize(cursorY); tmp = ""; for (int i = 0; i < (int) contents->count(); i++) { tmp += *getString(i); if (i + 1 < (int) contents->count() && contents->at(i)->newline) tmp += '\n'; } return tmp;}/*= Selects all text without moving the cursor.*/void QEditor::selectAll(){ markAnchorX = 0; markAnchorY = 0; markDragY = numLines() - 1; markDragX = lineLength(markDragY); turnMark(markDragX != markAnchorX || markDragY != markAnchorY); if (autoUpdate()) updateContents();}/*= Deselects all text (i.e. removes marking) and leaves the cursor at the current position.*/void QEditor::deselect(){ turnMark(false);}/*= Sets the text to \a s, removing old text, if any.*/void QEditor::resetChangedLines(){ for (int i = 0; i < (int) contents->count(); i++) contents->at(i)->changed = false; if (autoUpdate()) updateContents();}void QEditor::setText(const QString & s){ 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()) viewport()->repaint(); setUndoEnabled(oldUndo);}/*= Appends \a s to the text.*/void QEditor::append(const QString & s){ bool oldUndo = isUndoEnabled(); setUndoEnabled(false); insertLine(s, -1); setUndoEnabled(oldUndo); emit textChanged();}/*void QEditor::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 QEditor::keyPressEvent(QKeyEvent * e){ textDirty = false; d->isHandlingEvent = true; int unknown = 0; if (readOnly) { int pageSize = contentsHeight() / 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); cursorUp(false); break; case Key_Down: //setTopCell(topCell() + 1); cursorDown(false); 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)) if (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 ); 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: tab(false); break; case Key_BackTab: tab(true); 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 QEditor::pageDown(bool mark){ bool oldAuto = autoUpdate(); if (cursorY >= (int) contents->count() - 1) { makeVisible(); return; } if (mark) setAutoUpdate(false); if (partiallyInvisible(cursorY)) setY(topCell()); int delta = cursorY - topCell(); int pageSize = viewHeight() / cellHeight(); int newTopCell = QMIN(topCell() + pageSize, numLines() - pageSize - 1); 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()) { setY(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 setY(QMIN(cursorY + pageSize, numLines() - 1)); cursorX = mapFromView(curXPos, cursorY); if (mark) newMark(cursorX, cursorY, false); //makeVisible(); } if (oldAuto) if (mark) { setAutoUpdate(true); updateContents(); } else { repaintCell(oldY, 0, false); } if (!mark) turnMark(false); makeVisible();}/*= Moves the cursor one page up. If \a mark is true, the text is marked.*/void QEditor::pageUp(bool mark){ bool oldAuto = autoUpdate(); if (mark) setAutoUpdate(false); if (partiallyInvisible(cursorY)) setY(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()) { setY(QMIN(newTopCell + delta, numLines() - 1)); if (partial) setY(cursorY - 1); cursorX = mapFromView(curXPos, cursorY); if (mark) newMark(cursorX, cursorY, false); setTopCell(newTopCell); } else { // just move the cursor setY(QMAX(cursorY - pageSize, 0)); cursorX = mapFromView(curXPos, cursorY); if (mark) newMark(cursorX, cursorY, false); } if (oldAuto) if (mark)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -