| 100 | } |
| 101 | |
| 102 | void PythonEditor::keyPressEvent(QKeyEvent* e) |
| 103 | { |
| 104 | /** When the user presses enter the next line should match the current |
| 105 | * indentation unless the line ends in a colon, where the next line |
| 106 | * should have an additional indentation. Shift+Enter should dedent |
| 107 | * the next block 1 indentation from what it would have been, if possible. |
| 108 | */ |
| 109 | if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) { |
| 110 | bool shiftPressed = e->modifiers() & Qt::ShiftModifier; |
| 111 | ParameterGrp::handle hPrefGrp = getWindowParameter(); |
| 112 | int indent = hPrefGrp->GetInt("IndentSize", 4); |
| 113 | bool space = hPrefGrp->GetBool("Spaces", true); |
| 114 | QString ch = space ? QStringLiteral(" ") : QStringLiteral("\t"); |
| 115 | |
| 116 | QTextCursor cursor = textCursor(); |
| 117 | QString currentLineText = cursor.block().text(); |
| 118 | bool endsWithColon = currentLineText.endsWith(QLatin1Char(':')); |
| 119 | int currentIndentation = 0; |
| 120 | // count spaces/tabs at start of current line |
| 121 | for (auto c : currentLineText) { |
| 122 | if (c == ch) { |
| 123 | currentIndentation++; |
| 124 | } |
| 125 | else { |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | cursor.insertBlock(); // new line |
| 130 | cursor.movePosition(QTextCursor::StartOfBlock); // carriage return |
| 131 | // Shift+Enter means dedent, but ensure we are not at column 0 |
| 132 | if (shiftPressed && currentIndentation >= indent) { |
| 133 | currentIndentation -= indent; |
| 134 | } |
| 135 | // insert appropriate number of spaces/tabs to match current indentation |
| 136 | cursor.insertText(QString(currentIndentation, ch[0])); |
| 137 | // if the line ended in a colon, then we need to add another tab or multiple spaces |
| 138 | if (endsWithColon) { |
| 139 | if (space) { |
| 140 | cursor.insertText(QString(indent, ch[0])); // 4 more spaces by default |
| 141 | } |
| 142 | else { |
| 143 | cursor.insertText(ch); // 1 more tab |
| 144 | } |
| 145 | } |
| 146 | setTextCursor(cursor); |
| 147 | return; // skip default handler |
| 148 | } |
| 149 | PythonTextEditor::keyPressEvent(e); // wasn't enter key, so let base class handle it |
| 150 | } |
| 151 | |
| 152 | void PythonEditor::onComment() |
| 153 | { |