void QSAEditor::keyPressEvent(QKeyEvent *e) { QTextCursor cursor = textCursor(); const QChar leftChar = charFromCursor(cursor, QTextCursor::PreviousCharacter); if (e->key() == Qt::Key_Tab) { QString leftText = cursor.block().text().left(cursor.position() - cursor.block().position()); if (cursor.hasSelection() || leftText.simplified().isEmpty()) { indent(document(), cursor); e->accept(); return; } } if ((e->key() == Qt::Key_Period && (cursor.atBlockStart() || leftChar != QLatin1Char('.')) ) || (e->key() == Qt::Key_Greater && !cursor.atBlockStart() && leftChar == QLatin1Char('-')) ) { doObjectCompletion(); } QTextEdit::keyPressEvent(e); }
QString ScriptEditorWidget::textUnderCursor() const { QString szWord; QTextCursor tc = textCursor(); if(tc.atBlockStart()) return QString(); tc.clearSelection(); tc.movePosition(QTextCursor::StartOfWord,QTextCursor::KeepAnchor); if(tc.atBlockStart()) { szWord.append(tc.selectedText()); tc.movePosition(QTextCursor::EndOfWord,QTextCursor::KeepAnchor); szWord.append(tc.selectedText()); if(tc.atBlockEnd()){ return szWord; } tc.movePosition(QTextCursor::NextCharacter,QTextCursor::KeepAnchor); szWord.append(tc.selectedText()); if(szWord.right(1)!=".") szWord.chop(1); return szWord; } tc.movePosition(QTextCursor::PreviousCharacter,QTextCursor::KeepAnchor); szWord=tc.selectedText(); if(szWord.left(1)==".") { tc.movePosition(QTextCursor::StartOfWord); tc.movePosition(QTextCursor::PreviousCharacter); tc.movePosition(QTextCursor::PreviousWord); tc.movePosition(QTextCursor::EndOfWord,QTextCursor::KeepAnchor,1); szWord.prepend(tc.selectedText()); } else szWord.remove(0,1); return szWord; }
void GenericCodeEditor::moveLineUpDown(bool up) { // directly taken from qtcreator // Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). // GNU Lesser General Public License QTextCursor cursor = textCursor(); QTextCursor move = cursor; move.setVisualNavigation(false); // this opens folded items instead of destroying them move.beginEditBlock(); bool hasSelection = cursor.hasSelection(); if (cursor.hasSelection()) { move.setPosition(cursor.selectionStart()); move.movePosition(QTextCursor::StartOfBlock); move.setPosition(cursor.selectionEnd(), QTextCursor::KeepAnchor); move.movePosition(move.atBlockStart() ? QTextCursor::Left: QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); } else { move.movePosition(QTextCursor::StartOfBlock); move.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); } QString text = move.selectedText(); move.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); move.removeSelectedText(); if (up) { move.movePosition(QTextCursor::PreviousBlock); move.insertBlock(); move.movePosition(QTextCursor::Left); } else { move.movePosition(QTextCursor::EndOfBlock); if (move.atBlockStart()) { // empty block move.movePosition(QTextCursor::NextBlock); move.insertBlock(); move.movePosition(QTextCursor::Left); } else { move.insertBlock(); } } int start = move.position(); move.clearSelection(); move.insertText(text); int end = move.position(); if (hasSelection) { move.setPosition(start); move.setPosition(end, QTextCursor::KeepAnchor); } move.endEditBlock(); setTextCursor(move); }
void GenericCodeEditor::copyUpDown(bool up) { // directly taken from qtcreator // Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). // GNU Lesser General Public License QTextCursor cursor = textCursor(); QTextCursor move = cursor; move.beginEditBlock(); bool hasSelection = cursor.hasSelection(); if (hasSelection) { move.setPosition(cursor.selectionStart()); move.movePosition(QTextCursor::StartOfBlock); move.setPosition(cursor.selectionEnd(), QTextCursor::KeepAnchor); move.movePosition(move.atBlockStart() ? QTextCursor::Left: QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); } else { move.movePosition(QTextCursor::StartOfBlock); move.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); } QString text = move.selectedText(); if (up) { move.setPosition(cursor.selectionStart()); move.movePosition(QTextCursor::StartOfBlock); move.insertBlock(); move.movePosition(QTextCursor::Left); } else { move.movePosition(QTextCursor::EndOfBlock); if (move.atBlockStart()) { move.movePosition(QTextCursor::NextBlock); move.insertBlock(); move.movePosition(QTextCursor::Left); } else { move.insertBlock(); } } int start = move.position(); move.clearSelection(); move.insertText(text); int end = move.position(); move.setPosition(start); move.setPosition(end, QTextCursor::KeepAnchor); move.endEditBlock(); setTextCursor(move); }
QString CvsEditorWidget::changeUnderCursor(const QTextCursor &c) const { // Try to match "1.1" strictly: // 1) Annotation: Check for a revision number at the beginning of the line. // Note that "cursor.select(QTextCursor::WordUnderCursor)" will // only select the part up until the dot. // Check if we are at the beginning of a line within a reasonable offset. // 2) Log: check for lines like "revision 1.1", cursor past "revision" switch (contentType()) { case VcsBase::OtherContent: case VcsBase::DiffOutput: break; case VcsBase::AnnotateOutput: { const QTextBlock block = c.block(); if (c.atBlockStart() || (c.position() - block.position() < 3)) { const QString line = block.text(); if (m_revisionAnnotationPattern.exactMatch(line)) return m_revisionAnnotationPattern.cap(1); } } break; case VcsBase::LogOutput: { const QTextBlock block = c.block(); if (c.position() - block.position() > 8 && m_revisionLogPattern.exactMatch(block.text())) return m_revisionLogPattern.cap(1); } break; } return QString(); }
void TextProcessor::textChangeEvent() { if (autoInsertion_) return; QTextCursor c = edit_->textCursor(); if (c.atBlockStart()) { QTextBlock bl = c.block(); QTextBlock prevBl = bl.previous(); if (bl.isValid() && prevBl.isValid()) { // ensure that cursor was moved from the previous row if (lastRow_ - 1 != preLastRow_) return; QString text = bl.text(); QString prevText = prevBl.text(); if (/*text.isEmpty() &&*/ !prevText.isEmpty()) { int lineBeginIndex = prevText.indexOf(QRegExp("[^ \t]")); QString lineBegin = prevText.left(lineBeginIndex); autoInsertion_ = true; while (bl.text().startsWith('\t') || bl.text().startsWith(' ')) { c.deleteChar(); } c.insertText(lineBegin); autoInsertion_ = false; } } } }
void ScCodeEditor::moveToPreviousToken( QTextCursor & cursor, QTextCursor::MoveMode mode ) { if (cursor.atBlockStart()) { cursor.movePosition( QTextCursor::PreviousCharacter, mode ); return; } QTextBlock block( cursor.block() ); QString blockText = block.text(); int positionInBlock = cursor.position() - block.position() - 1; // skip whitespace while (positionInBlock > 0 && blockText[positionInBlock].isSpace()) --positionInBlock; cursor.setPosition(positionInBlock + block.position(), mode); if (positionInBlock == 0) return; // go to beginning of token or beginning of word TokenIterator tokenIt( block, positionInBlock ); if (tokenIt.isValid()) { cursor.setPosition( tokenIt.position(), mode ); } else { int pos = positionInBlock; if (blockText[pos].isLetterOrNumber()) { while (pos > 0 && blockText[pos-1].isLetterOrNumber()) --pos; } cursor.setPosition( pos + block.position(), mode ); } }
static bool isBlockOnlySelection(QTextCursor cursor) { Q_ASSERT(cursor.hasSelection()); QTextCursor begin(cursor); begin.setPosition(begin.anchor()); if (begin.atBlockStart() && (cursor.atBlockStart() || cursor.atBlockEnd())) return true; else return false; }
void Dialog_line::test2(){ QTextCursor cursor = ui->textEdit->textCursor(); cursor.atBlockStart(); cursor.setPosition(ui->textEdit->toPlainText().length()); //cursor.movePosition(ui->textEdit->toPlainText().length()); ui->textEdit->setTextCursor(cursor); string str = ui->textEdit->toPlainText().toStdString(); str.erase(std::remove(str.begin(), str.end(), '\n'), str.end()); (parent_window.*m_func)(str); }
void SyntaxTextEditor::indentText(QTextDocument *doc, QTextCursor cur, bool bIndent) { cur.beginEditBlock(); if (!cur.hasSelection()) { indentCursor(cur,bIndent); } else { QTextBlock block = doc->findBlock(cur.selectionStart()); QTextBlock end = doc->findBlock(cur.selectionEnd()); if (!cur.atBlockStart()) { end = end.next(); } do { indentBlock(block,bIndent); block = block.next(); } while (block.isValid() && block != end); } cur.endEditBlock(); }
QString TikzEditor::textUnderCursor() const { QTextCursor cursor = textCursor(); const int oldPos = cursor.position(); // cursor.select(QTextCursor::WordUnderCursor); // const int newPos = cursor.selectionStart(); int newPos; for (newPos = oldPos; newPos > 0;) // move the cursor to the beginning of the word { cursor.setPosition(--newPos, QTextCursor::KeepAnchor); if (cursor.selectedText().trimmed().isEmpty()) // if the current char is a whitespace, then we have reached the beginning of the word { cursor.clearSelection(); cursor.setPosition(++newPos, QTextCursor::MoveAnchor); break; } else if (cursor.selectedText() == "\\" || cursor.atBlockStart()) // these characters also delimit the beginning of the word (the beginning of a TikZ command) { cursor.clearSelection(); break; } else if (cursor.selectedText() == "[" || cursor.selectedText() == ",") // these characters also delimit the beginning of the word (the beginning of a TikZ option) { cursor.clearSelection(); cursor.setPosition(++newPos, QTextCursor::MoveAnchor); break; } cursor.clearSelection(); } // cursor.setPosition(newPos, QTextCursor::MoveAnchor); cursor.setPosition(oldPos, QTextCursor::KeepAnchor); QString word = cursor.selectedText(); // if (word.right(1) != word.trimmed().right(1)) // word = ""; return word; }
void CodeEditor::keyPressEvent(QKeyEvent* event) { if (event->key() == Qt::Key_Return && event->modifiers() == Qt::SHIFT) return; if (event->key() == Qt::Key_E && event->modifiers() == Qt::CTRL) { QTextBlock block = textCursor().block(); foldUnfold(block); FULLRESIZE; return; } if (event->key() == Qt::Key_Space && event->modifiers() == Qt::CTRL) { performCompletion(); return; } if (event->key() == Qt::Key_Backspace && config->backUnindent) { QTextCursor cursor = textCursor(); if (!cursor.hasSelection() && !cursor.atBlockStart() && cursor.block().text(). \ left(cursor.positionInBlock()). \ trimmed().isEmpty()) { cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, qMin(config->indentSize, cursor.positionInBlock())); cursor.removeSelectedText(); return; } } if (event->key() == Qt::Key_Tab && config->tabIndents) { QTextCursor cursor = textCursor(); if (cursor.hasSelection()) { QTextBlock block = document()->findBlock(cursor.selectionStart()); int end = document()->findBlock(cursor.selectionEnd()).blockNumber(); if (end - block.blockNumber()) { cursor.beginEditBlock(); do { cursor.setPosition(block.position(), QTextCursor::MoveAnchor); cursor.insertText(QString().fill(' ', config->indentSize)); } while ((block = block.next()).isValid() && block.blockNumber() <= end); cursor.endEditBlock(); return; } } else if (textCursor().block().text(). \ left(textCursor().positionInBlock()). \ trimmed().isEmpty()) { textCursor().insertText(QString().fill(' ', config->indentSize)); return; } } if (event->key() == Qt::Key_Tab && config->spaceTabs) { textCursor().insertText(QString().fill(' ', config->tabSize)); return; } if (completer->popup()->isVisible()) { switch (event->key()) { case Qt::Key_Up: case Qt::Key_Down: case Qt::Key_Enter: case Qt::Key_Return: case Qt::Key_Escape: event->ignore(); return; } } QPlainTextEdit::keyPressEvent(event); if (completer->popup()->isVisible()) performCompletion(); if (event->key() == Qt::Key_Return && config->autoIndent) { int state = textCursor().block().userState(); if (!(state & Error) && (state & Nested)) { QString txt = textCursor().block().previous().text(); int i = 0; while (txt[i].isSpace()) ++i; int previousBlockState = textCursor().block().previous().userState(); if (!(previousBlockState & Error) && previousBlockState & Begin) i += config->indentSize; textCursor().insertText(QString().fill(' ', i)); } } }
void SoTextEdit::keyPressEvent(QKeyEvent *input) { QTextCursor cursor = textCursor(); QString text; QStringList list; QTextBlock block; int position = 0; // int position_in_block = 0; int count; if (input->modifiers() != Qt::NoModifier && input->modifiers() != Qt::ShiftModifier) { input->ignore(); return; } switch (input->key()) { case Qt::Key_Shift: case Qt::Key_Control: case Qt::Key_unknown: input->ignore(); return; case Qt::Key_BraceLeft: cursor.insertText(input->text(), colorFormat(default_color)); std::cout << block_stack << std::endl; return; case Qt::Key_BraceRight: cursor.insertText("}"); position = autoIndent(cursor); cursor.setPosition(position); setTextCursor(cursor); return; case Qt::Key_Return: emit returnPressed(); position = cursor.position(); cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); for (count = 0; count <= text.size(); count++) { if (text[count] == QChar('{')) { block_stack++; } else if (text[count] == QChar('}')) { block_stack--; } } cursor.setPosition(position); if (cursor.atBlockEnd()) { cursor.insertText(input->text()); position++; } else { cursor.setPosition(position + 1, QTextCursor::MoveAnchor); cursor.setPosition(position - 1, QTextCursor::KeepAnchor); text = cursor.selectedText(); for (count = 0; count < exception->count(); count++) { if (text[0] == (*exception)[count][0] || text[1] == (*exception)[count][0]) { cursor.setPosition(position); cursor.insertText(input->text()); position = autoIndent(cursor); cursor.setPosition(position); setTextCursor(cursor); return; } } cursor.setPosition(position, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.setPosition(position); cursor.insertText(input->text()); position++; } position = autoIndent(cursor); cursor.setPosition(position); setTextCursor(cursor); return; case Qt::Key_Tab: cout << "tab" << endl; position = autoIndent(cursor); cursor.setPosition(position); setTextCursor(cursor); std::cout << "block_stack" << block_stack << std::endl; return; case Qt::Key_Left: if (cursor.atBlockStart()) { cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor); position = cursor.position(); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack--; } else if (text[count] == QChar('}')) { block_stack++; } count++; } cursor.setPosition(position, QTextCursor::MoveAnchor); } else { cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor); } setTextCursor(cursor); std::cout << block_stack << std::endl; return; case Qt::Key_Right: if (cursor.atEnd()) { return; } else if (cursor.atBlockEnd()) { cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack++; } else if (text[count] == QChar('}')) { block_stack--; } count++; } } cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor); setTextCursor(cursor); std::cout << block_stack << std::endl; return; case Qt::Key_Down: block = cursor.block(); if ((block.blockNumber() + 1 ) == blockCount()) { return; } else { position = cursor.position(); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack++; } else if (text[count] == QChar('}')) { block_stack--; } count++; } cursor.setPosition(position); cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor); setTextCursor(cursor); std::cout << block_stack << std::endl; return; } case Qt::Key_Up: block = cursor.block(); if (block.blockNumber() == 0) { return; } else { cursor.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor); position = cursor.position(); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack--; } else if (text[count] == QChar('}')) { block_stack++; } count++; } cursor.setPosition(position); setTextCursor(cursor); std::cout << block_stack << std::endl; return; } case Qt::Key_Space: if (cursor.atBlockEnd()) { cursor.insertText(input->text()); setTextCursor(cursor); return; } else { position = cursor.position(); cursor.setPosition(position - 1, QTextCursor::MoveAnchor); cursor.setPosition(position + 1, QTextCursor::KeepAnchor); text = cursor.selectedText(); for (count = 0; count < exception->count(); count++) { if (text[0] == (*exception)[count][0] || text[1] == (*exception)[count][0]) { cursor.setPosition(position); cursor.insertText(input->text()); setTextCursor(cursor); return; } } cursor.setPosition(position, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.setPosition(position); cursor.insertText(input->text()); setTextCursor(cursor); return; } case Qt::Key_Delete: position = cursor.position(); cursor.deleteChar(); cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); text = cursor.selectedText(); cursor.setPosition(position); for (count = 0; count < exception->count(); count++) { if (text[0] == (*exception)[count][0]) { cursor.setPosition(position - 1); } } cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); break; case Qt::Key_Backspace: if (cursor.atStart()) { return; } else if (cursor.atBlockStart()) { position = cursor.position() - 1; cursor.deletePreviousChar(); cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); std::cout << "debug_text " << qPrintable(text) << std::endl; count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack--; } else if (text[count] == QChar('}')) { block_stack++; } count++; } cursor.setPosition(position, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); } else { position = cursor.position() - 1; cursor.deletePreviousChar(); cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); text = cursor.selectedText(); for (count = 0; count < exception->count(); count++) { if (text[0] == (*exception)[count][0]) { cursor.setPosition(position - 1); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); break; } } if (count == exception->count()) { cursor.setPosition(position); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); } } std::cout << block_stack <<std::endl; break; default: position = cursor.position() + 1; cursor.insertText(input->text()); cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); } qDebug() << text; if (text.isEmpty()) { return; } switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.setPosition(position); setTextCursor(cursor); return; }