QTextDocument *Exporter::prepareTextDoc(QTextDocument *textDoc) { QTextDocument *textDocument = textDoc->clone(this); // textDocument->setDefaultStyleSheet("p, li { white-space: pre-wrap; } p{line-height: 2em; font-family:'Liberation Serif'; font-size:19pt;margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:72px;}"); //cut blank spaces at the begining and end : QTextCursor *tCursor = new QTextCursor(textDocument); tCursor->movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1); tCursor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,1); while(tCursor->selectedText() == " "){ tCursor->deleteChar(); tCursor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,1); } tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor,1); while(tCursor->selectedText() == " "){ tCursor->deleteChar(); tCursor->movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor,1); } //set text config : QTextBlockFormat blockFormat; // blockFormat.setBottomMargin(0); // blockFormat.setTopMargin(0); // blockFormat.setTextIndent(72); blockFormat.setLineHeight(200, QTextBlockFormat::ProportionalHeight); blockFormat.setAlignment(Qt::AlignJustify); // QTextCharFormat charFormat; // charFormat.setFontPointSize(12); // charFormat.setFontFamily("Courrier"); tCursor->select(QTextCursor::Document); tCursor->mergeBlockFormat(blockFormat); // tCursor->mergeBlockCharFormat(charFormat); QRegExp reg("-qt-paragraph-type:.*;|margin-top:.*;|margin-bottom:.*;|margin-left:.*;|margin-right:.*;|-qt-block-indent:.*;|text-indent:.*;|font-family:.*;|font-size:.*;"); reg.setMinimal(true); textDocument->setHtml(textDocument->toHtml().remove(reg)); return textDocument; }
void CodeEditor::deleteLine(){ QTextCursor selection = getSelectedLines(); selection.beginEditBlock(); selection.removeSelectedText(); selection.deleteChar(); selection.endEditBlock(); }
bool AutoCompleter::autoBackspace(QTextCursor &cursor) { m_allowSkippingOfBlockEnd = false; if (!m_autoParenthesesEnabled) return false; int pos = cursor.position(); if (pos == 0) return false; QTextCursor c = cursor; c.setPosition(pos - 1); QTextDocument *doc = cursor.document(); const QChar lookAhead = doc->characterAt(pos); const QChar lookBehind = doc->characterAt(pos - 1); const QChar lookFurtherBehind = doc->characterAt(pos - 2); const QChar character = lookBehind; if (character == QLatin1Char('(') || character == QLatin1Char('[')) { QTextCursor tmp = cursor; TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp); int blockStart = tmp.isNull() ? 0 : tmp.position(); tmp = cursor; TextBlockUserData::findNextBlockClosingParenthesis(&tmp); int blockEnd = tmp.isNull() ? (cursor.document()->characterCount()-1) : tmp.position(); QChar openChar = character; QChar closeChar = (character == QLatin1Char('(')) ? QLatin1Char(')') : QLatin1Char(']'); int errors = 0; int stillopen = 0; countBrackets(cursor, blockStart, blockEnd, openChar, closeChar, &errors, &stillopen); int errorsBeforeDeletion = errors + stillopen; errors = 0; stillopen = 0; countBrackets(cursor, blockStart, pos - 1, openChar, closeChar, &errors, &stillopen); countBrackets(cursor, pos, blockEnd, openChar, closeChar, &errors, &stillopen); int errorsAfterDeletion = errors + stillopen; if (errorsAfterDeletion < errorsBeforeDeletion) return false; // insertion fixes parentheses or bracket errors, do not auto complete } // ### this code needs to be generalized if ((lookBehind == QLatin1Char('(') && lookAhead == QLatin1Char(')')) || (lookBehind == QLatin1Char('[') && lookAhead == QLatin1Char(']')) || (lookBehind == QLatin1Char('"') && lookAhead == QLatin1Char('"') && lookFurtherBehind != QLatin1Char('\\')) || (lookBehind == QLatin1Char('\'') && lookAhead == QLatin1Char('\'') && lookFurtherBehind != QLatin1Char('\\'))) { if (! isInComment(c)) { cursor.beginEditBlock(); cursor.deleteChar(); cursor.deletePreviousChar(); cursor.endEditBlock(); return true; } } return false; }
bool ScCodeEditor::removeMatchingTokens() { QTextCursor cursor = textCursor(); QTextDocument *document = cursor.document(); int cursorPosition = cursor.position(); if (cursorPosition == 0) return false; QChar previousChar = document->characterAt(cursorPosition-1); QChar nextChar; if (previousChar == '{') nextChar = '}'; else if (previousChar == '[') nextChar = ']'; else if (previousChar == '(') nextChar = ')'; else if (previousChar == '\'' || previousChar == '"') nextChar = previousChar; else return false; if (document->characterAt(cursorPosition) != nextChar) return false; cursor.beginEditBlock(); cursor.deletePreviousChar(); cursor.deleteChar(); cursor.endEditBlock(); setTextCursor(cursor); return true; }
void CodeEditor::insertCompletion(const QString& completion){ if (completer_->widget() != this){ return; } QTextCursor tc = textCursor(); //Delete the characters that the user has entered as it might be in //wrong case, and insert entire completion word. tc.movePosition(QTextCursor::StartOfWord); int toDelete = completer_->completionPrefix().length(); for(int i=0;i<toDelete;i++){ tc.deleteChar(); } tc.insertText(completion); setTextCursor(tc); /* //original code, insert only missing characters and keep the characters //the user has already entered. int extra = completion.length() - completer_->completionPrefix().length(); tc.movePosition(QTextCursor::Left); tc.movePosition(QTextCursor::EndOfWord); tc.insertText(completion.right(extra)); setTextCursor(tc); */ }
void QsvTextOperationsWidget::on_replaceOldText_returnPressed() { if (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) || QApplication::keyboardModifiers().testFlag(Qt::AltModifier) || QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier) ) { on_replaceAll_clicked(); showReplace(); return; } QTextCursor c = m_searchCursor; QTextDocument *doc = getTextDocument(); if (!doc){ qDebug("%s:%d - no document found, using a wrong class? wrong parent?", __FILE__,__LINE__); return; } c = doc->find( replaceFormUi->findText->text(), c, getReplaceFlags() ); if (c.isNull()) return; int start = c.selectionStart(); int end = c.selectionEnd(); c.beginEditBlock(); c.deleteChar(); c.insertText( replaceFormUi->replaceText->text() ); c.setPosition(start,QTextCursor::KeepAnchor); c.setPosition(end ,QTextCursor::MoveAnchor); c.endEditBlock(); setTextCursor( c ); // is there any other apperance of this text? m_searchCursor = c; updateReplaceInput(); }
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; } } } }
QTextDocument *Exporter::prepareNoteDoc(QTextDocument *noteDoc) { QTextDocument *textDocument = noteDoc->clone(this); //cut blank spaces at the begining and end : QTextCursor *tCursor = new QTextCursor(textDocument); tCursor->movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1); tCursor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,1); while(tCursor->selectedText() == " "){ tCursor->deleteChar(); tCursor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,1); } tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor,1); while(tCursor->selectedText() == " "){ tCursor->deleteChar(); tCursor->movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor,1); } //set text config : QTextBlockFormat blockFormat; blockFormat.setBottomMargin(0); blockFormat.setTopMargin(0); blockFormat.setTextIndent(72); blockFormat.setLineHeight(200, QTextBlockFormat::ProportionalHeight); blockFormat.setAlignment(Qt::AlignJustify); QTextCharFormat charFormat; charFormat.setFontPointSize(12); charFormat.setFontFamily("Courrier"); tCursor->select(QTextCursor::Document); tCursor->mergeBlockCharFormat(charFormat); tCursor->mergeBlockFormat(blockFormat); return textDocument; }
void SimpleRichTextEdit::deleteText() { QTextCursor cursor = textCursor(); if(cursor.hasSelection()) cursor.removeSelectedText(); else cursor.deleteChar(); }
QTextCursor CodeEditor::deleteCurrentLine() { QTextCursor currentPos = textCursor(); textCursor().beginEditBlock(); currentPos.select(QTextCursor::BlockUnderCursor); if (currentPos.selectedText() == ""){ if (currentPos.selectionStart() == 0){ currentPos.deleteChar(); }else{ currentPos.deletePreviousChar(); } currentPos.removeSelectedText(); } else{ currentPos.removeSelectedText(); if (currentPos.selectionStart() == 0) currentPos.deleteChar(); } return currentPos; }
void CCodeEditor::_DeleteCurrentLine() { QTextCursor currentCursor = this->textCursor(); currentCursor.select(QTextCursor::BlockUnderCursor); currentCursor.removeSelectedText(); ++m_textChangeTime; if (currentCursor.atStart()) { currentCursor.deleteChar(); ++m_textChangeTime; } }
void XTextEdit::sCorrectWord() { QAction *action = qobject_cast<QAction *>(sender()); if (action) { QString replacement = action->text(); QTextCursor cursor = cursorForPosition(_lastPos); cursor.select(QTextCursor::WordUnderCursor); cursor.deleteChar(); cursor.insertText(replacement); _highlighter->rehighlight(); } }
void ChatMessageArea::setHtml(const QString& html) { // Create format with updated line height QTextBlockFormat format; format.setLineHeight(CHAT_MESSAGE_LINE_HEIGHT, QTextBlockFormat::ProportionalHeight); // Possibly a bug in QT, the format won't take effect if `insertHtml` is used first. Inserting a space and deleting // it after ensures the format is applied. QTextCursor cursor = textCursor(); cursor.setBlockFormat(format); cursor.insertText(" "); cursor.insertHtml(html); cursor.setPosition(0); cursor.deleteChar(); }
/** * Deletes the current line */ void HaiQTextEdit::delete_current_line() { QTextCursor cursor = textCursor(); cursor.beginEditBlock(); cursor.clearSelection(); cursor.movePosition(QTextCursor::StartOfLine); int pos(cursor.position()); cursor.select(QTextCursor::LineUnderCursor); // remove line (and line feed char) cursor.removeSelectedText(); cursor.deleteChar(); // goto start of next line cursor.setPosition(pos); cursor.endEditBlock(); }
int QUimTextUtil::deleteSelectionTextInQTextEdit( enum UTextOrigin origin, int former_req_len, int latter_req_len ) { QTextEdit *edit = static_cast<QTextEdit *>( mWidget ); QTextCursor cursor = edit->textCursor(); if ( ! cursor.hasSelection() ) return -1; bool cursor_at_beginning = false; int current = cursor.position(); int start = cursor.selectionStart(); if ( current == start ) cursor_at_beginning = true; QString text = cursor.selectedText(); int len = text.length(); int end = start + len; if ( origin == UTextOrigin_Beginning || ( origin == UTextOrigin_Cursor && cursor_at_beginning ) ) { if ( latter_req_len >= 0 ) { if ( len > latter_req_len ) end = start + latter_req_len; } else { if (! ( ~latter_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) return -1; } } else if ( origin == UTextOrigin_End || ( origin == UTextOrigin_Cursor && !cursor_at_beginning ) ) { if ( former_req_len >= 0 ) { if ( len > former_req_len ) start = end - former_req_len; } else { if (! ( ~former_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) return -1; } } else { return -1; } cursor.setPosition( start ); cursor.setPosition( end, QTextCursor::KeepAnchor ); edit->setTextCursor( cursor ); cursor.deleteChar(); return 0; }
void CodeEditor::moveLineUp(){ QTextCursor selectionCursor = getSelectedLines(); QString selectedLines = selectionCursor.selection().toPlainText(); selectionCursor.beginEditBlock(); selectionCursor.removeSelectedText(); selectionCursor.deleteChar(); selectionCursor.movePosition(QTextCursor::Up); selectionCursor.insertText(QString(selectedLines + "\n")); selectionCursor.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor, selectedLines.split("\n").size()); selectionCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, selectedLines.size()); selectionCursor.endEditBlock(); setTextCursor(selectionCursor); if(codeCompleter->popup()->isVisible()) codeCompleter->popup()->hide(); }
void CodeEditor::moveLineDown(){ QTextCursor selectionCursor = getSelectedLines(); QString selectedLines = selectionCursor.selection().toPlainText(); selectionCursor.beginEditBlock(); selectionCursor.removeSelectedText(); selectionCursor.deleteChar(); selectionCursor.movePosition(QTextCursor::EndOfLine); selectionCursor.insertText(QString("\n" + selectedLines)); selectionCursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, selectedLines.size()); selectionCursor.endEditBlock(); setTextCursor(selectionCursor); if(codeCompleter->popup()->isVisible()) codeCompleter->popup()->hide(); }
void ChatTextEdit::limitText() { QTextCursor cursor = this->textCursor(); cursor.movePosition(QTextCursor::End); while(cursor.position() > 0x20000) { cursor.movePosition(QTextCursor::Start); cursor.select(QTextCursor::LineUnderCursor); if(cursor.hasSelection()) cursor.removeSelectedText(); else cursor.deleteChar(); cursor.movePosition(QTextCursor::End); } }
void LiteEditorWidget::insertFromMimeData(const QMimeData *source) { if (isReadOnly()) return; if (source->hasFormat(QLatin1String(kVerticalTextBlockMimeType))) { QString text = QString::fromUtf8(source->data(QLatin1String(kVerticalTextBlockMimeType))); if (text.isEmpty()) return; QStringList lines = text.split(QLatin1Char('\n')); QTextCursor cursor = textCursor(); cursor.beginEditBlock(); const TextEditor::TabSettings &ts = this->tabSettings(); int initialCursorPosition = cursor.position(); int column = ts.columnAt(cursor.block().text(), cursor.positionInBlock()); cursor.insertText(lines.first()); for (int i = 1; i < lines.count(); ++i) { QTextBlock next = cursor.block().next(); if (next.isValid()) { cursor.setPosition(next.position()); } else { cursor.movePosition(QTextCursor::EndOfBlock); cursor.insertBlock(); } int offset = 0; int position = ts.positionAtColumn(cursor.block().text(), column, &offset); cursor.setPosition(cursor.block().position() + position); if (offset < 0) { cursor.deleteChar(); cursor.insertText(QString(-offset, QLatin1Char(' '))); } else { cursor.insertText(QString(offset, QLatin1Char(' '))); } cursor.insertText(lines.at(i)); } cursor.setPosition(initialCursorPosition); cursor.endEditBlock(); setTextCursor(cursor); ensureCursorVisible(); return; } QPlainTextEdit::insertFromMimeData(source); }
void QsvTextOperationsWidget::on_replaceAll_clicked() { // WHY NOT HIDING THE WIDGET? // it seems that if you hide the widget, when the replace all action // is triggered by pressing control+enter on the replace widget // eventually an "enter event" is sent to the text eidtor. // the work around is to update the transparency of the widget, to let the user // see the text bellow the widget //showReplaceWidget(); m_replace->hide(); int replaceCount = 0; // replaceWidget->setWidgetTransparency( 0.2 ); QTextCursor c = getTextCursor(); c = getTextDocument()->find( replaceFormUi->replaceText->text(), c, getReplaceFlags() ); while (!c.isNull()) { setTextCursor( c ); QMessageBox::StandardButton button = QMessageBox::question( qobject_cast<QWidget*>(parent()), tr("Replace all"), tr("Replace this text?"), QMessageBox::Yes | QMessageBox::Ignore | QMessageBox::Cancel ); if (button == QMessageBox::Cancel) { break; } else if (button == QMessageBox::Yes) { c.beginEditBlock(); c.deleteChar(); c.insertText( replaceFormUi->replaceText->text() ); c.endEditBlock(); setTextCursor( c ); replaceCount++; } c = getTextDocument()->find( replaceFormUi->replaceText->text(), c, getReplaceFlags() ); } // replaceWidget->setWidgetTransparency( 0.8 ); m_replace->show(); QMessageBox::information( 0, tr("Replace all"), tr("%1 replacement(s) made").arg(replaceCount) ); }
void OpenedFile::removeLine(const int line_number) { printf("WARNING: OpenedFile::removeLine(const int line_number) never tested!!!!\n"); QTextCursor parsingCursor = textCursor(); parsingCursor.setPosition(0); while(parsingCursor.blockNumber() != line_number) { parsingCursor.movePosition(QTextCursor::Down); } parsingCursor.movePosition(QTextCursor::StartOfLine); parsingCursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); parsingCursor.deleteChar(); }
void CodeEditor::toggleComments(){ QTextCursor selectionCursor = getSelectedLines(); QStringList selectedLines = selectionCursor.selection().toPlainText().split("\n"); selectionCursor.beginEditBlock(); selectionCursor.removeSelectedText(); selectionCursor.deleteChar(); for (int i = 0; i < selectedLines.size(); i++){ if (selectedLines.at(i).trimmed().size() > 0){ if(!selectedLines.at(i).startsWith("#")) selectedLines[i] = "#" + selectedLines[i]; else selectedLines[i].remove(0, 1); } } int contentCount = 0; foreach(QString line, selectedLines){ selectionCursor.insertText(line + "\n"); contentCount += line.size() + 1; }
void QLabelPrivate::ensureTextPopulated() const { if (!textDirty) return; if (control) { QTextDocument *doc = control->document(); if (textDirty) { #ifndef QT_NO_TEXTHTMLPARSER if (isRichText) doc->setHtml(text); else doc->setPlainText(text); #else doc->setPlainText(text); #endif doc->setUndoRedoEnabled(false); #ifndef QT_NO_SHORTCUT if (hasShortcut) { // Underline the first character that follows an ampersand (and remove the others ampersands) int from = 0; bool found = false; QTextCursor cursor; while (!(cursor = control->document()->find((QLatin1String("&")), from)).isNull()) { cursor.deleteChar(); // remove the ampersand cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); from = cursor.position(); if (!found && cursor.selectedText() != QLatin1String("&")) { //not a second & found = true; shortcutCursor = cursor; } } } #endif } } textDirty = false; }
void removeString(char *msg) { char *posstring = (char*)malloc(sizeof(char)*10); char *anchorstring = (char*)malloc(sizeof(char)*10); int pos; int anchor; int possize = strchr(msg,'|') - msg; strncpy(posstring,msg,possize); strcpy(anchorstring,msg+possize+1); pos=atoi(posstring); anchor=atoi(anchorstring); if(*cursor_locked){ exit(-1); //I don't think that this thread should ever spawn race conditions, but you never know. } *cursor_locked=1; QTextCursor oldcursor = editor->textCursor(); QTextCursor tempcursor = editor->textCursor(); tempcursor.setPosition(anchor,QTextCursor::MoveAnchor); tempcursor.setPosition(pos,QTextCursor::KeepAnchor); editor->setTextCursor(tempcursor); tempcursor.deleteChar(); editor->setTextCursor(oldcursor); *cursor_locked=0; }
void CodeEditor::keyPressEvent(QKeyEvent * e) { if (e->key() == Qt::Key_Tab || e->key() == Qt::Key_Backtab) { if (AutoCompleter->popup()->isVisible()||TagAutoCompleter->popup()->isVisible()) { e->ignore(); return; } textCursor().beginEditBlock(); bool shiftIsPressed = e->key() == Qt::Key_Backtab; QTextCursor editingTextCursor = textCursor(); QTextCursor endingTextCursor = textCursor(); editingTextCursor.setPosition(textCursor().selectionStart()); endingTextCursor.setPosition(textCursor().selectionEnd()); endingTextCursor.movePosition( QTextCursor::Down ); endingTextCursor.movePosition( QTextCursor::StartOfLine ); do{ editingTextCursor.movePosition( QTextCursor::StartOfLine ); if (!shiftIsPressed) { for(int j=0; j<Settings::getTabSize(); ++j) { editingTextCursor.insertText(" "); } } else { for(int j=0; j<Settings::getTabSize(); ++j) { if (toPlainText()[editingTextCursor.position()] == ' ') { editingTextCursor.deleteChar(); } } } editingTextCursor.movePosition( QTextCursor::Down ); editingTextCursor.movePosition( QTextCursor::StartOfLine ); } while(editingTextCursor.position() != endingTextCursor.position()); textCursor().endEditBlock(); } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) { QStringList lineList = toPlainText().split("\n"); int currentPosition = 0; int lineStartIndex = 0; int nextLineStartIndex; int spaceCount; if (AutoCompleter->popup()->isVisible()||TagAutoCompleter->popup()->isVisible()) { e->ignore(); return; } textCursor().beginEditBlock(); // TODO: refactor using same kind of functions as the tabbing system currentPosition = textCursor().selectionStart(); for(int i=0; i<lineList.size(); ++i){ nextLineStartIndex = lineStartIndex + lineList[i].size() + 1; if ((lineStartIndex >= currentPosition || (currentPosition >= lineStartIndex && currentPosition < nextLineStartIndex))) { for( spaceCount = 0;lineList[i][spaceCount].isSpace(); spaceCount++ ); textCursor().insertText("\n"); for( ;spaceCount>0; spaceCount-- ){ textCursor().insertText(" "); } break; } lineStartIndex = nextLineStartIndex; } textCursor().endEditBlock(); } else if (e->key() == Qt::Key_Left) { if(textCursor().hasSelection() && (e->modifiers() & Qt::ShiftModifier) == 0) { textCursor().beginEditBlock(); QTextCursor newCursor = textCursor(); newCursor.setPosition(textCursor().selectionStart()); setTextCursor(newCursor); document()->setModified(false); textCursor().endEditBlock(); } else { QPlainTextEdit::keyPressEvent(e); } } else if (e->key() == Qt::Key_Right) { if(textCursor().hasSelection() && (e->modifiers() & Qt::ShiftModifier) == 0) { textCursor().beginEditBlock(); QTextCursor newCursor = textCursor(); newCursor.setPosition(textCursor().selectionEnd()); setTextCursor(newCursor); document()->setModified(false); textCursor().endEditBlock(); } else if(toPlainText()[textCursor().position()] == '\n') { textCursor().beginEditBlock(); QTextCursor editingTextCursor = textCursor(); do { editingTextCursor.movePosition( QTextCursor::Right, (e->modifiers() & Qt::ShiftModifier) == 0 ? QTextCursor::MoveAnchor : QTextCursor::KeepAnchor ); } while (toPlainText()[editingTextCursor.position()] == ' '); setTextCursor(editingTextCursor); textCursor().endEditBlock(); } else { QPlainTextEdit::keyPressEvent(e); } } else if (e->key() == Qt::Key_Delete) { if(textCursor().hasSelection()) { QPlainTextEdit::keyPressEvent(e); return; } if(toPlainText()[textCursor().position()] == '\n') { textCursor().beginEditBlock(); textCursor().deleteChar(); QTextCursor editingTextCursor = textCursor(); do { editingTextCursor.movePosition( QTextCursor::Right, QTextCursor::KeepAnchor ); } while (toPlainText()[editingTextCursor.position()] == ' '); editingTextCursor.removeSelectedText(); textCursor().endEditBlock(); } else if(toPlainText()[textCursor().position()] == ' ') { QTextCursor editingTextCursor = textCursor(); textCursor().beginEditBlock(); for(int i = 0; i < Settings::getTabSize(); i++) { if( toPlainText()[editingTextCursor.position()] == ' ' ) { editingTextCursor.movePosition( QTextCursor::Right, QTextCursor::KeepAnchor ); } else { break; } } editingTextCursor.removeSelectedText(); textCursor().endEditBlock(); } else { QPlainTextEdit::keyPressEvent(e); } } else if (e->key() == Qt::Key_Backspace) { if(textCursor().position()>0 && toPlainText()[textCursor().position()-1] == ' ' && !textCursor().hasSelection() ) { QTextCursor editingTextCursor = textCursor(); textCursor().beginEditBlock(); for(int i = 0; i < Settings::getTabSize(); i++) { if( textCursor().position()>0 && toPlainText()[editingTextCursor.position()-1] == ' ' ) { editingTextCursor.movePosition( QTextCursor::Left, QTextCursor::KeepAnchor ); } else { break; } } editingTextCursor.removeSelectedText(); textCursor().endEditBlock(); } else { QPlainTextEdit::keyPressEvent(e); } } else if (e->key() == Qt::Key_Home) { textCursor().beginEditBlock(); QTextCursor editingTextCursor = textCursor(); editingTextCursor.movePosition( QTextCursor::StartOfLine, (e->modifiers() & Qt::ShiftModifier) == 0 ? QTextCursor::MoveAnchor : QTextCursor::KeepAnchor ); while (toPlainText()[editingTextCursor.position()] == ' ') { editingTextCursor.movePosition( QTextCursor::Right, (e->modifiers() & Qt::ShiftModifier) == 0 ? QTextCursor::MoveAnchor : QTextCursor::KeepAnchor ); } setTextCursor(editingTextCursor); document()->setModified(false); textCursor().endEditBlock(); } else if (e->key() == Qt::Key_Space && (e->modifiers() & Qt::ControlModifier) != 0) { int character_position = 0; QTextCursor editingTextCursor = textCursor(); if (AutoCompleter->popup()->isVisible()) { AutoCompleter->popup()->hide(); return; } if (TagAutoCompleter->popup()->isVisible()) { TagAutoCompleter->popup()->hide(); return; } editingTextCursor.setPosition(textCursor().selectionStart()); editingTextCursor.movePosition( QTextCursor::StartOfWord, QTextCursor::KeepAnchor ); if(editingTextCursor.position()-1 >=0 ) { character_position = editingTextCursor.position() - 1; } QRect cursor_rect = cursorRect(); cursor_rect.setWidth(150); if (toPlainText()[character_position] == '<') { TagAutoCompleter->setCompletionPrefix( editingTextCursor.selectedText().trimmed() ); TagAutoCompleter->complete(cursor_rect); } else { AutoCompleter->setCompletionPrefix( editingTextCursor.selectedText().trimmed() ); AutoCompleter->complete(cursor_rect); } } else { QPlainTextEdit::keyPressEvent(e); } Rockete::getInstance().checkTextChanged(-1); // end of event is dedicated to auto completion pop up/out. Do not place anything important under this comment if(e->key()==Qt::Key_Space||e->key()==Qt::Key_Control||(e->modifiers() & Qt::ControlModifier) != 0||(e->modifiers() & Qt::AltModifier) != 0) return; if (!AutoCompleter->popup()->isVisible()&&!TagAutoCompleter->popup()->isVisible()) { if(e->key()==Qt::Key_Left||e->key()==Qt::Key_Right||e->key()==Qt::Key_Up||e->key()==Qt::Key_Down) return; } QTextCursor editingTextCursor = textCursor(); QRect cursor_rect = cursorRect(); cursor_rect.setWidth(150); editingTextCursor.setPosition(textCursor().selectionEnd()); editingTextCursor.movePosition(QTextCursor::PreviousWord, QTextCursor::KeepAnchor); editingTextCursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor); if(editingTextCursor.selectedText().contains('-')) { editingTextCursor.movePosition(QTextCursor::PreviousWord, QTextCursor::KeepAnchor); } if(!editingTextCursor.selectedText().trimmed().isEmpty()) { if(editingTextCursor.selectedText().trimmed().startsWith('<')) { editingTextCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); TagAutoCompleter->setCompletionPrefix( editingTextCursor.selectedText().trimmed() ); if(TagAutoCompleter->completionCount()>0) { TagAutoCompleter->complete(cursor_rect); if (AutoCompleter->popup()->isVisible()) { AutoCompleter->popup()->hide(); } } else { if (TagAutoCompleter->popup()->isVisible()) { TagAutoCompleter->popup()->hide(); } } } else { AutoCompleter->setCompletionPrefix(editingTextCursor.selectedText().trimmed()); if(AutoCompleter->completionCount()>0) { AutoCompleter->complete(cursor_rect); if (TagAutoCompleter->popup()->isVisible()) { TagAutoCompleter->popup()->hide(); } } else { if (AutoCompleter->popup()->isVisible()) { AutoCompleter->popup()->hide(); } } } } else { if (AutoCompleter->popup()->isVisible()) { AutoCompleter->popup()->hide(); } if (TagAutoCompleter->popup()->isVisible()) { TagAutoCompleter->popup()->hide(); } } // end of event is dedicated to auto completion pop up/out. Do not place anything important under this comment }
int QUimTextUtil::deletePrimaryTextInQTextEdit( enum UTextOrigin origin, int former_req_len, int latter_req_len ) { QTextEdit *edit = static_cast<QTextEdit *>( mWidget ); QString text = edit->toPlainText(); // excluding preedit string int len = text.length(); int preedit_len = mIc->getPreeditString().length(); QTextCursor cursor = edit->textCursor(); int precedence_len = cursor.position(); int following_len = len - precedence_len; int former_del_start; int latter_del_end; switch ( origin ) { case UTextOrigin_Cursor: former_del_start = 0; if ( former_req_len >= 0 ) { if ( precedence_len > former_req_len ) former_del_start = precedence_len - former_req_len; } else { if (! ( ~former_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) return -1; } latter_del_end = len + preedit_len; if ( latter_req_len >= 0 ) { if ( following_len > latter_req_len ) latter_del_end = precedence_len + preedit_len + latter_req_len; } else { if (! ( ~latter_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) return -1; } break; case UTextOrigin_Beginning: former_del_start = 0; latter_del_end = precedence_len + preedit_len; if ( latter_req_len >= 0 ) { if ( precedence_len < latter_req_len ) { if ( following_len >= ( latter_req_len - precedence_len ) ) latter_del_end = preedit_len + latter_req_len; else latter_del_end = len + preedit_len; } } else { if (! ( ~latter_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) return -1; latter_del_end = len + preedit_len; } break; case UTextOrigin_End: former_del_start = precedence_len; latter_del_end = len + preedit_len; if ( former_req_len < 0 ) { if (! ( ~former_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) return -1; former_del_start = 0; } break; case UTextOrigin_Unspecified: default: return -1; } // don't call setText() to avoid flicker unlike QLineEdit int end_index = latter_del_end - preedit_len; if ( precedence_len != end_index ) { cursor.setPosition( precedence_len ); cursor.setPosition( end_index, QTextCursor::KeepAnchor ); edit->setTextCursor( cursor ); cursor.deleteChar(); } if ( precedence_len != former_del_start ) { cursor.setPosition( precedence_len ); cursor.setPosition( former_del_start, QTextCursor::KeepAnchor ); edit->setTextCursor( cursor ); cursor.deleteChar(); } return 0; }
void Editor::del() { QTextCursor curs = EditTex->textCursor(); curs.deleteChar(); EditTex->setTextCursor(curs); }
void QFCompleterTextEditWidget::keyPressEvent(QKeyEvent *event) { if (c && c->popup()->isVisible()) { // The following keys are forwarded by the completer to the widget switch (event->key()) { case Qt::Key_Enter: case Qt::Key_Return: case Qt::Key_Escape: case Qt::Key_Tab: case Qt::Key_Backtab: event->ignore(); return; // completer widgets handles these keys! default: break; } } else { // if the completer is not visible, we may treat some special // key press events (convert TAB to spaces ... if (event->key()==Qt::Key_Tab) { // convert TAB to <tabwidth> spaces processTab(event); return; // let the completer do default behavior } else if (event->key()==Qt::Key_Backtab || (event->key()==Qt::Key_Tab && event->modifiers()==Qt::ShiftModifier)) { indentDec(); return; // let the completer do default behavior } else if (event->key()==Qt::Key_Return || event->key()==Qt::Key_Enter) { // indent when last non-space character in current line is '{' QTextCursor tc = textCursor(); // if text has been selected, the user wants to overwrite this text // with a linebreak, so we first delete the text if (tc.hasSelection()) tc.deleteChar(); //tc.select(QTextCursor::LineUnderCursor); tc.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); QString line=tc.selectedText(); // count leading spaces int lspaces=0; QChar* data=line.data(); while (data->isSpace()) { ++data; lspaces++; } // get right-most non-space character int indx=line.size()-1; data=line.data(); if (indx>=0) while (data[indx].isSpace() && (indx>0)) { indx--; } QChar last=data[indx]; if (last=='{') { QTextCursor c(textCursor()); c.insertText("\n"+QString(lspaces, QChar(' '))+tabToSpaces()); setTextCursor(c); } else if (last=='}' && line.indexOf('}')==lspaces) { // '}' is the first non-space character in this line QTextCursor c(textCursor()); QTextCursor c1(textCursor()); c1.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor); QString left=c1.selectedText(); if (lspaces>0 && left=="}") { long clspaces=lspaces; // the number of leading spaces in the current line long indention=qMax((long)0, (long)lspaces-(long)tabwidth); // new number of leading spaces // here we try to find the matching '{' and reduce the indention to the // indention of the matching '{'. If the matching '{' was not found, // the indention will not be reduced QTextCursor cc(textCursor()); int cnt=1; int pos=cc.selectionStart()-2; while ((cnt>0) && (pos>=0)) { cc.setPosition(pos); cc.setPosition(pos+1, QTextCursor::KeepAnchor); QString curChar=cc.selectedText(); if (curChar=="{") cnt--; if (curChar=="}") cnt++; //std::cout<<"'"<<(char*)curChar.toLatin1().data()<<"' cnt="<<cnt<<" pos="<<pos<<std::endl; pos--; } // here we found the matching '{' and count its leading spaces if (pos>=0){ cc.select(QTextCursor::LineUnderCursor); lspaces=0; QChar* data=cc.selectedText().data(); while (data->isSpace()) { ++data; lspaces++; } indention=lspaces; } //std::cout<<"indention="<<indention<<" clspaces="<<clspaces<<" lspaces="<<lspaces<<std::endl; c=textCursor(); c.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); c.insertText(QString(indention, QChar(' '))+line.right(qMax((long)1,(long)line.size()-clspaces))+"\n"+QString(indention, QChar(' '))); //c.movePosition(QTextCursor::Left); //c.insertText("\n"+QString(lspaces, QChar(' '))); } else { c=textCursor(); c.insertText("\n"); } setTextCursor(c); } else { QTextCursor c(textCursor()); c.insertText("\n"+QString(lspaces, QChar(' '))); setTextCursor(c); } return; } } bool isShortcut = ((event->modifiers() & Qt::ControlModifier) && event->key() == Qt::Key_E); // CTRL+E if (!c || !isShortcut) // dont process the shortcut when we have a completer QTextEdit::keyPressEvent(event); const bool ctrlOrShift = event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier); if (!c || (ctrlOrShift && event->text().isEmpty())) return; static QString eow("~!@#$%^&*()+{}|:\"<>?,./;'[]\\-="); // end of word bool hasModifier = (event->modifiers() != Qt::NoModifier) && !ctrlOrShift; QString completionPrefix = textUnderCursor(); /*QMessageBox::information(this, "", tr("completionPrefix=%1\nisShortcut=%2\nctrlOrShif=%3\nhasModifier=%4") .arg(completionPrefix) .arg(isShortcut) .arg(ctrlOrShift) .arg(hasModifier) );*/ if (!isShortcut && (hasModifier || event->text().isEmpty()|| completionPrefix.length() < 2 || eow.contains(event->text().right(1)))) { c->popup()->hide(); return; } /*QMessageBox::information(this, "", tr("c->completionPrefix=%1\ncompletionPrefix=%2") .arg(c->completionPrefix()) .arg(completionPrefix) );*/ if (completionPrefix != c->completionPrefix()) { c->setCompletionPrefix(completionPrefix); /*QMessageBox::information(this, "", tr("c->completionModel()->rowCount()=%1") .arg(c->completionModel()->rowCount()) );*/ c->popup()->setCurrentIndex(c->completionModel()->index(0, 0)); } QRect cr = cursorRect(); cr.setWidth(c->popup()->sizeHintForColumn(0) + c->popup()->verticalScrollBar()->sizeHint().width()); c->complete(cr); // popup it up! /*QMessageBox::information(this, "", tr("cr.width=%1\ncr.height=%2\ncr.x=%3\ncr.y=%4") .arg(cr.width()) .arg(cr.height()) .arg(cr.x()) .arg(cr.y()) );*/ }
void CCodeEdit::del() { QTextCursor cursor = textCursor(); cursor.deleteChar(); }
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; }