QString ChatEdit::textEditToPlainText() { QTextDocument *doc = document(); QString result; result.reserve(doc->characterCount()); QTextCursor begin(doc); QTextCursor end; QString specialChar = QChar(QChar::ObjectReplacementCharacter); bool first = true; while (!begin.atEnd()) { end = doc->find(specialChar, begin, QTextDocument::FindCaseSensitively); QString postValue; bool atEnd = end.isNull(); if (atEnd) { end = QTextCursor(doc); QTextBlock block = doc->lastBlock(); end.setPosition(block.position() + block.length() - 1); } else { postValue = end.charFormat().toolTip(); } begin.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, end.position() - begin.position() - (atEnd ? 0 : 1)); QString selectionText = begin.selection().toPlainText(); if (!first) result += selectionText.midRef(1); else result += selectionText; result += postValue; begin = end; end.clearSelection(); first = false; } return result; }
QTextBlock QTextDocumentProto::lastBlock() const { QTextDocument *item = qscriptvalue_cast<QTextDocument*>(thisObject()); if (item) return item->lastBlock(); return QTextBlock(); }
void UBTGAdaptableText::setCursorToTheEnd() { QTextDocument* doc = document(); if(NULL != doc){ QTextBlock block = doc->lastBlock(); QTextCursor cursor(doc); cursor.setPosition(block.position() + block.length() - 1); setTextCursor(cursor); } }
int AutoCompleter::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor, const TabSettings &tabSettings) { if (!m_autoParenthesesEnabled) return 0; QTextDocument *doc = cursor.document(); if (doc->characterAt(cursor.position() - 1) != QLatin1Char('{')) return 0; if (!contextAllowsAutoParentheses(cursor)) return 0; // verify that we indeed do have an extra opening brace in the document QTextBlock block = cursor.block(); const QString textFromCusror = block.text().mid(cursor.positionInBlock()).trimmed(); int braceDepth = TextDocumentLayout::braceDepth(doc->lastBlock()); if (braceDepth <= 0 && (textFromCusror.isEmpty() || textFromCusror.at(0) != QLatin1Char('}'))) return 0; // braces are all balanced or worse, no need to do anything and separator inserted not between '{' and '}' // we have an extra brace , let's see if we should close it /* verify that the next block is not further intended compared to the current block. This covers the following case: if (condition) {| statement; */ int indentation = tabSettings.indentationColumn(block.text()); if (block.next().isValid()) { // not the last block block = block.next(); //skip all empty blocks while (block.isValid() && tabSettings.onlySpace(block.text())) block = block.next(); if (block.isValid() && tabSettings.indentationColumn(block.text()) > indentation) return 0; } const QString &textToInsert = insertParagraphSeparator(cursor); int pos = cursor.position(); cursor.insertBlock(); cursor.insertText(textToInsert); cursor.setPosition(pos); // if we actually insert a separator, allow it to be overwritten if // user types it if (!textToInsert.isEmpty()) m_allowSkippingOfBlockEnd = true; return 1; }
QPair<int, int> getFirstAndLastVisiblePosition(Editor *editor) { QTextCursor cursor = editor->textCursor(); QTextDocument *doc = editor->document(); int currentLine = doc->findBlock(cursor.position()).blockNumber(); int cursorHeight = editor->cursorRect().height(); int lineCountToFirstVisibleLine = editor->cursorRect().top() / cursorHeight; int firstVisibleLineNum = currentLine - lineCountToFirstVisibleLine; if (firstVisibleLineNum < 0) { firstVisibleLineNum = 0; } int maxLineNumOnScreen = (editor->viewport()->height() / cursorHeight); if (maxLineNumOnScreen < 1) { maxLineNumOnScreen = 1; } int firstPos = doc->findBlockByNumber(firstVisibleLineNum).position(); int lastVisibleLineNum = firstVisibleLineNum + maxLineNumOnScreen - 1; QTextBlock lastVisibleTextBlock = doc->findBlockByNumber(lastVisibleLineNum); if (!lastVisibleTextBlock.isValid()) { lastVisibleTextBlock = doc->lastBlock(); } int lastPos = lastVisibleTextBlock.position() + lastVisibleTextBlock.length() - 1; return QPair<int, int>(firstPos, lastPos); }