Skip to content

Commit f3282f9

Browse files
committed
GUI: add setting options for highlight and focus_change
1 parent 953ce8b commit f3282f9

File tree

8 files changed

+112
-22
lines changed

8 files changed

+112
-22
lines changed

src/gui/mainwindow/MainWindow.ui

+31-1
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,11 @@
126126
</widget>
127127
<widget class="QMenu" name="menuOptions">
128128
<property name="title">
129-
<string>Options</string>
129+
<string>&amp;Options</string>
130130
</property>
131131
<addaction name="actionEditorShowLineNumbers"/>
132+
<addaction name="actionEditorEnableHighlight"/>
133+
<addaction name="actionEditorEnableFocusChange"/>
132134
</widget>
133135
<addaction name="menuFile"/>
134136
<addaction name="menuMachine"/>
@@ -611,6 +613,34 @@
611613
<string>Show line number in the code editor.</string>
612614
</property>
613615
</action>
616+
<action name="actionEditorEnableHighlight">
617+
<property name="checkable">
618+
<bool>true</bool>
619+
</property>
620+
<property name="checked">
621+
<bool>true</bool>
622+
</property>
623+
<property name="text">
624+
<string>Enable &amp;Highlighting</string>
625+
</property>
626+
<property name="toolTip">
627+
<string>Highlight currently executed instruction in internal editor.</string>
628+
</property>
629+
</action>
630+
<action name="actionEditorEnableFocusChange">
631+
<property name="checkable">
632+
<bool>true</bool>
633+
</property>
634+
<property name="checked">
635+
<bool>true</bool>
636+
</property>
637+
<property name="text">
638+
<string>Enable &amp;Focus Change</string>
639+
</property>
640+
<property name="toolTip">
641+
<string>Focus on currently executed instruction in internal editor.</string>
642+
</property>
643+
</action>
614644
</widget>
615645
<layoutdefault spacing="6" margin="11"/>
616646
<resources>

src/gui/mainwindow/mainwindow.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ MainWindow::MainWindow(QSettings *settings, QWidget *parent)
9393
connect(
9494
ui->actionEditorShowLineNumbers, &QAction::triggered, editor_tabs.data(),
9595
&EditorDock::set_show_line_numbers);
96+
connect(
97+
ui->actionEditorEnableHighlight, &QAction::triggered, editor_tabs.data(),
98+
&EditorDock::set_enable_hightlight);
99+
connect(
100+
ui->actionEditorEnableFocusChange, &QAction::triggered, editor_tabs.data(),
101+
&EditorDock::set_enable_focus_change);
96102

97103
bool line_numbers_visible = settings->value("EditorShowLineNumbers", true).toBool();
98104
editor_tabs->set_show_line_numbers(line_numbers_visible);

src/gui/windows/editor/editordock.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,22 @@ void EditorDock::set_show_line_numbers(bool visible) {
161161
}
162162
}
163163

164+
void EditorDock::set_enable_hightlight(bool enable) {
165+
enable_hightlight = enable;
166+
settings->setValue("editorEnableHighlisht", enable);
167+
for (int i = 0; i < this->count(); i++) {
168+
get_tab(i)->set_enable_highlight(enable);
169+
}
170+
}
171+
172+
void EditorDock::set_enable_focus_change(bool enable) {
173+
enable_focus_change = enable;
174+
settings->setValue("editorEnableFocusChange", enable);
175+
for (int i = 0; i < this->count(); i++) {
176+
get_tab(i)->set_enable_focus_change(enable);
177+
}
178+
}
179+
164180
void EditorDock::tabCountChanged() {
165181
Super::tabCountChanged();
166182
emit editor_available_changed(count() > 0);

src/gui/windows/editor/editordock.h

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class EditorDock : public HidingTabWidget {
4040

4141
public slots:
4242
void set_show_line_numbers(bool visible);
43+
void set_enable_hightlight(bool enable);
44+
void set_enable_focus_change(bool enable);
4345

4446
void open_file_dialog();
4547
void save_tab(int index);
@@ -59,6 +61,8 @@ public slots:
5961
private:
6062
QSharedPointer<QSettings> settings;
6163
bool line_numbers_visible = true;
64+
bool enable_hightlight = true;
65+
bool enable_focus_change = true;
6266
size_t unknown_editor_counter = 1;
6367
};
6468

src/gui/windows/editor/editortab.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ void EditorTab::set_show_line_number(bool visible) {
3939
editor->setShowLineNumbers(visible);
4040
}
4141

42+
void EditorTab::set_enable_highlight(bool enable) {
43+
editor->setEnableHighlight(enable);
44+
}
45+
46+
void EditorTab::set_enable_focus_change(bool enable) {
47+
editor->setEnableFocusChange(enable);
48+
}
49+
4250
void EditorTab::resizeEvent(QResizeEvent *event) {
4351
QWidget::resizeEvent(event);
4452
elide_file_name();

src/gui/windows/editor/editortab.h

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class EditorTab : public QWidget {
1919

2020
public slots:
2121
void set_show_line_number(bool visible);
22+
void set_enable_highlight(bool enable);
23+
void set_enable_focus_change(bool enable);
2224

2325
protected:
2426
void resizeEvent(QResizeEvent *event) override;

src/gui/windows/editor/srceditor.cpp

+41-21
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,19 @@ void SrcEditor::setShowLineNumbers(bool show) {
283283
updateMargins(0);
284284
}
285285

286+
void SrcEditor::setEnableHighlight(bool enable) {
287+
if (!enable) {
288+
// clear old styles
289+
QList<QTextEdit::ExtraSelection> extra_selections;
290+
setExtraSelections(extra_selections);
291+
}
292+
enable_highlight = enable;
293+
}
294+
295+
void SrcEditor::setEnableFocusChange(bool enable) {
296+
enable_focus_change = enable;
297+
}
298+
286299
void SrcEditor::insertFromMimeData(const QMimeData *source) {
287300
if (source->hasText()) { insertPlainText(source->text()); }
288301
}
@@ -292,27 +305,34 @@ bool SrcEditor::canInsertFromMimeData(const QMimeData *source) const {
292305
}
293306

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

297-
// set hightly style
298-
QTextEdit::ExtraSelection selection;
299-
QColor lineColor = QColor(Qt::yellow).lighter(160);
300-
selection.format.setBackground(lineColor);
301-
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
310+
if (enable_highlight) {
311+
// set hightly style
312+
QList<QTextEdit::ExtraSelection> extra_selections;
313+
QTextEdit::ExtraSelection selection;
314+
QColor lineColor = QColor(Qt::yellow).lighter(160);
315+
selection.format.setBackground(lineColor);
316+
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
317+
318+
// move cursor
319+
selection.cursor = QTextCursor(block);
320+
// select until the end of block
321+
selection.cursor.movePosition(
322+
QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, block.length());
323+
// select an extra \n
324+
selection.cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 1);
325+
extra_selections.append(selection);
326+
setExtraSelections(extra_selections);
327+
}
302328

303-
// select block
304-
QTextBlock block = document()->findBlockByNumber(block_num - 1);
305-
selection.cursor = QTextCursor(block);
306-
selection.cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, block.length());
307-
extra_selections.append(selection);
308-
309-
// calculate viewport line count
310-
int viewport_line_count
311-
= viewport()->height() / QFontMetrics(document()->defaultFont()).height();
312-
// scroll to block and show it in editor middle
313-
QScrollBar *vScrollBar = verticalScrollBar();
314-
vScrollBar->setValue(
315-
vScrollBar->singleStep() * (block.firstLineNumber() - viewport_line_count / 2));
316-
317-
setExtraSelections(extra_selections);
329+
if (enable_focus_change) {
330+
// calculate viewport line count
331+
int viewport_line_count
332+
= viewport()->height() / QFontMetrics(document()->defaultFont()).height();
333+
// scroll to block and show it in editor middle
334+
QScrollBar *vScrollBar = verticalScrollBar();
335+
vScrollBar->setValue(
336+
vScrollBar->singleStep() * (block.firstLineNumber() - viewport_line_count / 2));
337+
}
318338
}

src/gui/windows/editor/srceditor.h

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class SrcEditor : public QPlainTextEdit {
4242

4343
public slots:
4444
void setShowLineNumbers(bool visible);
45+
void setEnableHighlight(bool enable);
46+
void setEnableFocusChange(bool enable);
4547
void highlightBlock(int block_num);
4648

4749
private slots:
@@ -52,6 +54,8 @@ private slots:
5254
::Box<QSyntaxHighlighter> highlighter {};
5355
LineNumberArea *line_number_area;
5456
bool line_numbers_visible = true;
57+
bool enable_highlight = true;
58+
bool enable_focus_change = true;
5559
QString fname;
5660
QString tname;
5761
bool saveAsRequiredFl {};

0 commit comments

Comments
 (0)