📄 qeditor.cpp
字号:
{ setAutoUpdate(true); updateContents(); } else { repaintCell(oldY, 0, false); } if (!mark) turnMark(false); makeVisible();}// THE CORE INSERTION FUNCTIONvoid QEditor::insertAtAux(const QString & txt, int line, int col, bool mark){ dummy = false; stopBlink(); cursorOn = true; //int oldw = contentsRect().width(); line = QMAX(QMIN(line, numLines() - 1), 0); col = QMAX(QMIN(col, lineLength(line)), 0); QString itxt = txt; QEditorRow *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); row->change(); if (mark) { markAnchorX = col; markAnchorY = line; } if (cursorX == col && cursorY == line) { cursorX += itxt.length(); } // BM - QFontMetrics fm(font()); #if 0 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)); repaintCell(line, 0, false); }*/ else { if (line > 0 && !contents->at(line - 1)->newline) rebreakParagraph(line - 1); else rebreakParagraph(line); } #endif wrapLine(line, 0); if (mark) newMark(cursorX, cursorY, false); setNumRowsAndTruncate(); textDirty = true; d->edited = true; #if 0 if (autoUpdate()) { makeVisible(); if (DYNAMIC_WRAP && oldw != contentsRect().width()) { setAutoUpdate(false); rebreakAll(); setAutoUpdate(true); updateContents(); } } #endif startBlink();}/*= 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 QEditor::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 QEditorRow(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 QEditor::removeLine(int line){ CLEAR_UNDO if (line >= numLines()) return; if (cursorY >= line && cursorY > 0) { setY(cursorY - 1); } bool updt = autoUpdate() && rowIsVisible(line); QEditorRow *r = contents->at(line); ASSERT(r); bool recalc = r->w == maxLineWidth(); contents->remove(line); if (contents->count() == 0) { int w = textWidth(QString::fromLatin1("")); contents->append(new QEditorRow(QString::fromLatin1(""), w)); setWidth(w); dummy = true; } if (setNumRowsAndTruncate()) recalc = updt = false; if (recalc) updateCellWidth(); makeVisible(); if (updt) updateContents(); textDirty = true; d->edited = true;}/*= Inserts \a s at the current cursor position.*/void QEditor::insert(const QString & s){ insert(s, false); if (textDirty) emit textChanged();}/*= Inserts \a c at the current cursor position.*/void QEditor::insert(const QString & str, bool mark){ bool nl = str.contains('\n'); dummy = false; bool wasMarkedText = hasMarkedText(); if (wasMarkedText) addUndoCmd(new QBeginCommand); if (wasMarkedText) del(); // ## Will flicker QString *s = getString(cursorY); if (cursorX > (int) s->length()) cursorX = s->length(); else if (overWrite && !wasMarkedText && cursorX < (int) s->length()) del(); // ## Will flicker insertAt(str, cursorY, cursorX, mark); makeVisible(); if (nl) { colorize(cursorY); repaintCell(cursorY, 0, false); } if (wasMarkedText) addUndoCmd(new QEndCommand());}/*= Makes a line break at the current cursor position.*/int QEditor::getIndent(int line, bool &empty){ empty = true; if (line < 0 || line > numLines()) return 0; QEditorRow *r = contents->at(line); int i = 0; for (i = 0; i < (int)r->s.length(); i++) { if (!r->s[i].isSpace()) { empty = false; break; } } return i;}void QEditor::newLine(){ bool empty; QEditorRow *r = contents->at(cursorY); insert("\n" + r->s.left(QMIN(cursorX, getIndent(cursorY, empty))));}void QEditor::tab(bool back, bool noundo /* = false */){ int line1, col1, line2, col2, y; int indent, i; QString ins; QEditorRow *r; //int offset; bool autoupdate; bool empty; if (!getMarkedRegion(&line1, &col1, &line2, &col2)) { if (!back) { ins.fill(' ', tabWidth - (cursorX % tabWidth)); insert(ins); } return; } autoupdate = autoUpdate(); setAutoUpdate(false); stopBlink(); col1 = 0; if (col2 > 0) { if (line2 < (numLines() - 1)) { line2++; col2 = 0; } else col2 = lineLength(line2); } setCursorPosition(line1, col1, false); setCursorPosition(line2, col2, true); y = line1; indent = 65536; for (y = line1; y < line2; y++) { i = getIndent(y, empty); if (!empty) indent = QMIN(indent, i); } if (back && indent <= 0) { setAutoUpdate(autoupdate); startBlink(); return; } if (!back) { ins.fill(' ', tabWidth - (indent % tabWidth)); for (y = line1; y < line2; y++) { //insertAt(ins, y, 0); r = contents->at(y); /* if (!noundo) { offset = positionToOffsetInternal(y, 0); d->undoList.append(new QInsTextCmd(offset, ins)); } */ r->s = ins + r->s; r->change(); colorize(y); } if (!noundo) addUndoCmd(new QInsTabCmd(line1, line2)); } else { indent %= tabWidth; if (indent == 0) indent = tabWidth; ins.fill(' ', indent); for (y = line1; y < line2; y++) { r = contents->at(y); /* if (!noundo) { offset = positionToOffsetInternal(y, 0); d->undoList.append(new QDelTextCmd(offset, r->s.left(indent))); } */ if (r->s.length() < ins.length() || r->s.left(indent) == ins) { r->s = r->s.mid(indent); r->change(); colorize(y); } } if (!noundo) addUndoCmd(new QDelTabCmd(line1, line2)); } setAutoUpdate(autoupdate); if (autoUpdate()) updateContents(); startBlink(); emit textChanged();}void QEditor::indent(bool back){ if (!hasMarkedText()) return; tab(back);}/*= Deletes text from the current cursor position to the end of the line.*/void QEditor::killLineAux(){ deselect(); // -sanders Don't let del() delete marked region QEditorRow *r = contents->at(cursorY); if (cursorX == (int) r->s.length()) { // if (r->newline) // -sanders Only del newlines! del(); return; } else { bool recalc = r->w == maxLineWidth(); r->s.remove(cursorX, r->s.length()); r->w = textWidth(r->s); r->change(); repaintCell(cursorY, 0, false); if (recalc) updateCellWidth(); rebreakParagraph(cursorY); // -sanders textDirty = true; d->edited = true; } curXPos = 0; makeVisible(); turnMark(false);}/*= Moves the cursor one character to the left. If \a mark is true, the text is marked. If \a wrap is true, the cursor moves to the end of the previous line if it is placed at the beginning of the current line. \sa cursorRight() cursorUp() cursorDown()*/void QEditor::cursorLeft(bool mark, bool wrap){ cursorLeft(mark, !mark, wrap);}void QEditor::cursorLeft(bool mark, bool clear_mark, bool wrap){ int oldY; if (cursorX != 0 || cursorY != 0 && wrap) { if (mark && !hasMarkedText()) { markAnchorX = cursorX; markAnchorY = cursorY; } stopBlink(); int ll = lineLength(cursorY); if (cursorX > ll) cursorX = ll; cursorOn = true; cursorX--; oldY = cursorY; if (cursorX < 0) { if (cursorY > 0) { setY(cursorY - 1); cursorX = lineLength(cursorY); if (cursorX > 1 && !isEndOfParagraph(cursorY)) cursorX--; } else { setY(0); //### ? cursorX = 0; } } if (mark) newMark(cursorX, cursorY, false); if (oldY != cursorY) repaintCell(oldY, 0, false); startBlink(); repaintCell(cursorY, 0, false); } curXPos = 0; makeVisible(); if (clear_mark) turnMark(false);}/*= Moves the cursor one character to the right. If \a mark is true, the text is marked. If \a wrap is true, the cursor moves to the beginning of the next line if it is placed at the end of the current line. \sa cursorLeft() cursorUp() cursorDown()*/void QEditor::cursorRight(bool mark, bool wrap){ cursorRight(mark, !mark, wrap);}void QEditor::cursorRight(bool mark, bool clear_mark, bool wrap){ int strl = lineLength(cursorY); int oldY = cursorY; if (strl > 1 && !isEndOfParagraph(cursorY)) strl--; if (cursorX < strl || cursorY < (int) contents->count() - 1 && wrap) { if (mark && !hasMarkedText()) { markAnchorX = cursorX; markAnchorY = cursorY; } stopBlink(); cursorOn = true; cursorX++; if (cursorX > strl) { if (cursorY < (int) contents->count() - 1) { setY(cursorY + 1); cursorX = 0; } else { cursorX = lineLength(cursorY); } } if (mark) newMark(cursorX, cursorY, false); if (oldY != cursorY) repaintCell(oldY, 0, false); repaintCell(cursorY, 0, false); startBlink(); } curXPos = 0; makeVisible(); if (clear_mark) turnMark(false);}/*= Moves the cursor up one line. If \a mark is true, the text is marked. \sa cursorDown() cursorLeft() cursorRight()*/void QEditor::cursorUp(bool mark){ cursorUp(mark, !mark);}void QEditor::cursorUp(bool mark, bool clear_mark){ if (cursorY != 0) { if (mark && !hasMarkedText()) { markAnchorX = cursorX; markAnchorY = cursorY; } if (!curXPos) curXPos = mapToView(cursorX, cursorY); int oldY = cursorY; stopBlink(); cursorOn = true; setY(cursorY - 1); cursorX = mapFromView(curXPos, cursorY); if (mark) newMark(cursorX, cursorY, false); repaintCell(oldY, 0, false); repaintCell(cursorY, 0, false); startBlink(); } makeVisible(); if (clear_mark) turnMark(false);}/*= Moves the cursor one line down. If \a mark is true, the text is marked. \sa cursorUp() cursorLeft() cursorRight()*/void QEditor::cursorDown(bool mark){ cursorDown(mark, !mark);}void QEditor::cursorDown(bool mark, bool clear_mark){ int lastLin = contents->count() - 1; if (cursorY != lastLin) { if (mark && !hasMarkedText()) { markAnchorX = cursorX; markAnchorY = cursorY; } if (!curXPos) curXPos = mapToView(cursorX, cursorY); int oldY = cursorY; stopBlink(); cursorOn = true; setY(cursorY + 1); cursorX = mapFromView(curXPos, cursorY);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -