diff --git a/src/Gui/PythonEditor.cpp b/src/Gui/PythonEditor.cpp index a76c1eeba7..4154cc6a4d 100644 --- a/src/Gui/PythonEditor.cpp +++ b/src/Gui/PythonEditor.cpp @@ -243,27 +243,7 @@ void PythonEditor::onComment() void PythonEditor::onUncomment() { - QTextCursor cursor = textCursor(); - int selStart = cursor.selectionStart(); - int selEnd = cursor.selectionEnd(); - QTextBlock block; - cursor.beginEditBlock(); - for (block = document()->begin(); block.isValid(); block = block.next()) { - int pos = block.position(); - int off = block.length()-1; - // at least one char of the block is part of the selection - if ( pos >= selStart || pos+off >= selStart) { - if ( pos+1 > selEnd ) - break; // end of selection reached - if (block.text().startsWith(QLatin1String("#"))) { - cursor.setPosition(block.position()); - cursor.deleteChar(); - selEnd--; - } - } - } - - cursor.endEditBlock(); + remove(QStringLiteral("#")); } void PythonEditor::onExecuteInConsole() diff --git a/src/Gui/TextEdit.cpp b/src/Gui/TextEdit.cpp index 0ad823dce7..c11d081101 100644 --- a/src/Gui/TextEdit.cpp +++ b/src/Gui/TextEdit.cpp @@ -519,6 +519,34 @@ void PythonTextEditor::prepend(const QString& str) cursor.endEditBlock(); } +void PythonTextEditor::remove(const QString& str) +{ + QTextCursor cursor = textCursor(); + int selStart = cursor.selectionStart(); + int selEnd = cursor.selectionEnd(); + QTextBlock block; + cursor.beginEditBlock(); + for (block = document()->begin(); block.isValid(); block = block.next()) { + int pos = block.position(); + int off = block.length()-1; + // at least one char of the block is part of the selection + if ( pos >= selStart || pos+off >= selStart) { + if ( pos+1 > selEnd ) + break; // end of selection reached + QString text = block.text(); + if (text.startsWith(str)) { + cursor.setPosition(block.position()); + for (int i = 0; i < str.length(); i++) { + cursor.deleteChar(); + selEnd--; + } + } + } + } + + cursor.endEditBlock(); +} + void PythonTextEditor::keyPressEvent (QKeyEvent * e) { if ( e->key() == Qt::Key_Tab ) { @@ -547,40 +575,13 @@ void PythonTextEditor::keyPressEvent (QKeyEvent * e) // If some text is selected we remove a leading tab or // spaces from each selected block ParameterGrp::handle hPrefGrp = getWindowParameter(); + bool space = hPrefGrp->GetBool("Spaces", true); int indent = hPrefGrp->GetInt( "IndentSize", 4 ); + QString ch = space ? QString(indent, QLatin1Char(' ')) + : QStringLiteral("\t"); - int selStart = cursor.selectionStart(); - int selEnd = cursor.selectionEnd(); - QTextBlock block; - cursor.beginEditBlock(); - for (block = document()->begin(); block.isValid(); block = block.next()) { - int pos = block.position(); - int off = block.length()-1; - // at least one char of the block is part of the selection - if ( pos >= selStart || pos+off >= selStart) { - if ( pos+1 > selEnd ) - break; // end of selection reached - // if possible remove one tab or several spaces - QString text = block.text(); - if (text.startsWith(QLatin1String("\t"))) { - cursor.setPosition(block.position()); - cursor.deleteChar(); - selEnd--; - } - else { - cursor.setPosition(block.position()); - for (int i=0; i