Skip to content

Commit

Permalink
GUI: add setting options for highlight and focus_change
Browse files Browse the repository at this point in the history
  • Loading branch information
trdthg authored and jdupak committed Jun 21, 2024
1 parent 57b7a92 commit c9e1b06
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 22 deletions.
32 changes: 31 additions & 1 deletion src/gui/mainwindow/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@
</widget>
<widget class="QMenu" name="menuOptions">
<property name="title">
<string>Options</string>
<string>&amp;Options</string>
</property>
<addaction name="actionEditorShowLineNumbers"/>
<addaction name="actionEditorEnableHighlight"/>
<addaction name="actionEditorEnableFocusChange"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuMachine"/>
Expand Down Expand Up @@ -630,6 +632,34 @@
<string>Show line number in the code editor.</string>
</property>
</action>
<action name="actionEditorEnableHighlight">
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="text">
<string>Enable &amp;Highlighting</string>
</property>
<property name="toolTip">
<string>Highlight currently executed instruction in internal editor.</string>
</property>
</action>
<action name="actionEditorEnableFocusChange">
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="text">
<string>Enable &amp;Focus Change</string>
</property>
<property name="toolTip">
<string>Focus on currently executed instruction in internal editor.</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
Expand Down
6 changes: 6 additions & 0 deletions src/gui/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ MainWindow::MainWindow(QSettings *settings, QWidget *parent)
connect(
ui->actionEditorShowLineNumbers, &QAction::triggered, editor_tabs.data(),
&EditorDock::set_show_line_numbers);
connect(
ui->actionEditorEnableHighlight, &QAction::triggered, editor_tabs.data(),
&EditorDock::set_enable_hightlight);
connect(
ui->actionEditorEnableFocusChange, &QAction::triggered, editor_tabs.data(),
&EditorDock::set_enable_focus_change);

bool line_numbers_visible = settings->value("EditorShowLineNumbers", true).toBool();
editor_tabs->set_show_line_numbers(line_numbers_visible);
Expand Down
16 changes: 16 additions & 0 deletions src/gui/windows/editor/editordock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ void EditorDock::set_show_line_numbers(bool visible) {
}
}

void EditorDock::set_enable_hightlight(bool enable) {
enable_hightlight = enable;
settings->setValue("editorEnableHighlisht", enable);
for (int i = 0; i < this->count(); i++) {
get_tab(i)->set_enable_highlight(enable);
}
}

void EditorDock::set_enable_focus_change(bool enable) {
enable_focus_change = enable;
settings->setValue("editorEnableFocusChange", enable);
for (int i = 0; i < this->count(); i++) {
get_tab(i)->set_enable_focus_change(enable);
}
}

void EditorDock::tabCountChanged() {
Super::tabCountChanged();
emit editor_available_changed(count() > 0);
Expand Down
4 changes: 4 additions & 0 deletions src/gui/windows/editor/editordock.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class EditorDock : public HidingTabWidget {

public slots:
void set_show_line_numbers(bool visible);
void set_enable_hightlight(bool enable);
void set_enable_focus_change(bool enable);

void open_file_dialog();
void save_tab(int index);
Expand All @@ -59,6 +61,8 @@ public slots:
private:
QSharedPointer<QSettings> settings;
bool line_numbers_visible = true;
bool enable_hightlight = true;
bool enable_focus_change = true;
size_t unknown_editor_counter = 1;
};

Expand Down
8 changes: 8 additions & 0 deletions src/gui/windows/editor/editortab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ void EditorTab::set_show_line_number(bool visible) {
editor->setShowLineNumbers(visible);
}

void EditorTab::set_enable_highlight(bool enable) {
editor->setEnableHighlight(enable);
}

void EditorTab::set_enable_focus_change(bool enable) {
editor->setEnableFocusChange(enable);
}

void EditorTab::resizeEvent(QResizeEvent *event) {
QWidget::resizeEvent(event);
elide_file_name();
Expand Down
2 changes: 2 additions & 0 deletions src/gui/windows/editor/editortab.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class EditorTab : public QWidget {

public slots:
void set_show_line_number(bool visible);
void set_enable_highlight(bool enable);
void set_enable_focus_change(bool enable);

protected:
void resizeEvent(QResizeEvent *event) override;
Expand Down
62 changes: 41 additions & 21 deletions src/gui/windows/editor/srceditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ void SrcEditor::setShowLineNumbers(bool show) {
updateMargins(0);
}

void SrcEditor::setEnableHighlight(bool enable) {
if (!enable) {
// clear old styles
QList<QTextEdit::ExtraSelection> extra_selections;
setExtraSelections(extra_selections);
}
enable_highlight = enable;
}

void SrcEditor::setEnableFocusChange(bool enable) {
enable_focus_change = enable;
}

void SrcEditor::insertFromMimeData(const QMimeData *source) {
if (source->hasText()) { insertPlainText(source->text()); }
}
Expand All @@ -292,27 +305,34 @@ bool SrcEditor::canInsertFromMimeData(const QMimeData *source) const {
}

void SrcEditor::highlightBlock(int block_num) {
QList<QTextEdit::ExtraSelection> extra_selections;
QTextBlock block = document()->findBlockByNumber(block_num - 1);

// set hightly style
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
if (enable_highlight) {
// set hightly style
QList<QTextEdit::ExtraSelection> extra_selections;
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);

// move cursor
selection.cursor = QTextCursor(block);
// select until the end of block
selection.cursor.movePosition(
QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, block.length());
// select an extra \n
selection.cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 1);
extra_selections.append(selection);
setExtraSelections(extra_selections);
}

// select block
QTextBlock block = document()->findBlockByNumber(block_num - 1);
selection.cursor = QTextCursor(block);
selection.cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, block.length());
extra_selections.append(selection);

// calculate viewport line count
int viewport_line_count
= viewport()->height() / QFontMetrics(document()->defaultFont()).height();
// scroll to block and show it in editor middle
QScrollBar *vScrollBar = verticalScrollBar();
vScrollBar->setValue(
vScrollBar->singleStep() * (block.firstLineNumber() - viewport_line_count / 2));

setExtraSelections(extra_selections);
if (enable_focus_change) {
// calculate viewport line count
int viewport_line_count
= viewport()->height() / QFontMetrics(document()->defaultFont()).height();
// scroll to block and show it in editor middle
QScrollBar *vScrollBar = verticalScrollBar();
vScrollBar->setValue(
vScrollBar->singleStep() * (block.firstLineNumber() - viewport_line_count / 2));
}
}
4 changes: 4 additions & 0 deletions src/gui/windows/editor/srceditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class SrcEditor : public QPlainTextEdit {

public slots:
void setShowLineNumbers(bool visible);
void setEnableHighlight(bool enable);
void setEnableFocusChange(bool enable);
void highlightBlock(int block_num);

private slots:
Expand All @@ -52,6 +54,8 @@ private slots:
::Box<QSyntaxHighlighter> highlighter {};
LineNumberArea *line_number_area;
bool line_numbers_visible = true;
bool enable_highlight = true;
bool enable_focus_change = true;
QString fname;
QString tname;
bool saveAsRequiredFl {};
Expand Down

0 comments on commit c9e1b06

Please sign in to comment.