9 #include <QApplication>
10 #include <QDesktopWidget>
11 #include <QTextDocumentFragment>
12 #include <QVBoxLayout>
23 mBaseFileModified =
false;
26 void PythonCodeEditor::keyPressEvent(
QKeyEvent* e)
48 void PythonCodeEditor::handleShiftTabKeyPressed()
50 PythonCodeEditor::indentSelection(
false);
53 void PythonCodeEditor::handleTabKeyPressed()
55 PythonCodeEditor::indentSelection(
true);
58 void PythonCodeEditor::handleReturnKeyPressed()
64 current_line =
cursor.selectedText();
67 for (
const auto& c : current_line)
76 auto trimmed = current_line.trimmed();
77 if (!trimmed.isEmpty() && trimmed.at(trimmed.size() - 1) ==
':')
82 num_spaces -= num_spaces % 4;
84 for (
u32 i = 0; i < num_spaces / 4; ++i)
91 void PythonCodeEditor::handleBackspaceKeyPressed(
QKeyEvent* e)
97 current_line =
cursor.selectedText();
105 cursor.removeSelectedText();
112 void PythonCodeEditor::handleDeleteKeyPressed(
QKeyEvent* e)
115 if (
cursor.positionInBlock() % 4 == 0)
118 if (
cursor.selectedText() ==
" ")
120 cursor.removeSelectedText();
128 void PythonCodeEditor::handleInsertKeyPressed()
133 void PythonCodeEditor::handleRedoRequested()
138 void PythonCodeEditor::handleAutocomplete()
144 auto text =
cursor.selectedText().replace(
QChar(0x2029),
'\n');
147 if (candidates.size() == 1)
151 else if (candidates.size() > 1)
153 auto dialog =
new PythonEditorCodeCompletionDialog(
this, candidates);
155 auto menu_width = dialog->width();
156 auto menu_height = dialog->height();
159 auto desk_width = desk_rect.
width();
160 auto desk_height = desk_rect.height();
165 if (anchor_global.x() + menu_width > desk_width)
167 anchor.setX(anchor.x() - menu_width);
171 if (anchor_global.y() + menu_height > desk_height)
173 anchor.setY(
cursorRect().topRight().
y() - menu_height);
176 dialog->move(anchor_global);
181 int PythonCodeEditor::nextIndent(
bool indentUnindent,
int current_indent)
186 nextIndent = 4 - (current_indent % 4);
190 nextIndent = current_indent % 4;
199 void PythonCodeEditor::indentSelection(
bool indent)
202 bool preSelected =
cursor.hasSelection();
203 int start =
cursor.selectionStart();
204 int end =
cursor.selectionEnd();
216 QString line_start_to_cursor =
cursor.selection().toPlainText();
217 bool onlySpaces =
true;
218 for (
const auto& c : line_start_to_cursor)
226 if (indent && !onlySpaces)
230 handleAutocomplete();
247 for (
const auto& c : selected_lines[0])
258 const int constant_indent = nextIndent(indent, n_spaces);
261 const int size = selected_lines.size();
266 padding.
fill(
' ', constant_indent);
269 for (
int i = 0; i <
size; i++)
273 cursor.insertText(padding);
274 end+=constant_indent;
278 QString current_line = selected_lines[i];
281 while(spaces < constant_indent)
283 if (current_line[spaces] !=
' ')
291 cursor.removeSelectedText();
303 void PythonCodeEditor::performCodeCompletion(std::tuple<std::string, std::string> completion)
325 if (
name.startsWith(projectLoc))
331 mBaseFileModified = base_file_modified;
336 return mBaseFileModified;
A plain text edit widget that is intended for editing code.
static ProjectManager * instance()
bool isBaseFileModified()
void setFilename(const QString &name)
QString getAbsFilename() const
void keyPressed(QKeyEvent *e)
void setBaseFileModified(bool base_file_modified)
PythonCodeEditor(QWidget *parent=nullptr)
QString getRelFilename() const
void completionSelected(std::tuple< std::string, std::string > selected)
PythonContext * gPythonContext
QDesktopWidget * desktop()
QString absoluteFilePath(const QString &fileName) const const
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString tr(const char *sourceText, const char *disambiguation, int n)
QRect cursorRect() const const
void ensureCursorVisible()
void insertPlainText(const QString &text)
virtual void keyPressEvent(QKeyEvent *e) override
void setOverwriteMode(bool overwrite)
QTextCursor textCursor() const const
QPoint bottomRight() const const
QString & fill(QChar ch, int size)
QString fromStdString(const std::string &str)
bool isEmpty() const const
QString & remove(int position, int n)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString trimmed() const const
QTextStream & left(QTextStream &stream)
void insertText(const QString &text)