Skip to content

Commit

Permalink
Improve editor
Browse files Browse the repository at this point in the history
* Better colors
* Monospaced font
* Address issue with loading files
  • Loading branch information
tpeulen committed Feb 3, 2025
1 parent ce5b8a7 commit 785debb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 55 deletions.
112 changes: 63 additions & 49 deletions chisurf/gui/tools/code_editor/qsci_editor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import pathlib
import sys

from PyQt5.Qsci import QsciScintilla
Expand Down Expand Up @@ -70,7 +71,6 @@ def __init__(
# Set the default font
font = QtGui.QFont()
font.setFamily(font_family)
font.setFixedPitch(True)
font.setPointSize(int(font_point_size))

self.setFont(font)
Expand Down Expand Up @@ -104,7 +104,7 @@ def __init__(
# Set Python lexer
# Set style for Python comments (style number 1) to a fixed-width
# courier.
if lexers_available:
if lexers_available and isinstance(language, str):
if language.lower() == "python":
lexer = QsciLexerPython()
elif language.lower() == "json":
Expand Down Expand Up @@ -142,43 +142,53 @@ def on_margin_clicked(self, nmargin, nline, modifiers):

class CodeEditor(QtWidgets.QWidget):

def load_file_event(self, event, filename: str = None, **kwargs):
self.load_file(filename)

def load_file(self, filename: str = None, **kwargs):
if filename is None:
filename = chisurf.gui.widgets.get_filename()
self.filename = filename
"""Load a file into the editor."""
filename = filename or chisurf.gui.widgets.get_filename()
if not filename:
return
try:
logging.log(0, f"Loading file: {filename}")
text = ""
with open(filename) as fp:
text = fp.read()
self.editor.setText(text)
self.line_edit.setText(
str(filename)
)
except IOError:
logging.log(1, f"CodeEditor, not a valid filename: {filename}")

def run_macro(self):
with open(filename, encoding="utf-8") as file:
self.editor.setText(file.read())
self.line_edit.setText(str(filename))
self.filename = filename
except IOError as e:
logging.log(1, f"Error loading file {filename}: {e}")

def run_macro(self, event):
"""Execute the currently loaded Python script."""
if not self.filename:
logging.log(1, "No file to run.")
return
self.save_text()
chisurf.console.run_macro(filename=self.filename)

def save_text(self):
if self.filename is None or self.filename == '':
self.filename = chisurf.gui.widgets.save_file(file_type='Python script (*.py)')
with io.zipped.open_maybe_zipped(self.filename, 'w') as fp:
text = str(self.editor.text())
fp.write(text)
self.line_edit.setText(str(self.filename.as_posix))
def save_text(self, event = None):
"""Save the current text to a file."""
if not self.filename:
self.filename = chisurf.gui.widgets.save_file(file_type="Python script (*.py)")
if not self.filename:
return
try:
with io.zipped.open_maybe_zipped(self.filename, "w") as file:
file.write(self.editor.text())
self.line_edit.setText(str(pathlib.Path(self.filename).as_posix()))
except IOError as e:
logging.log(1, f"Error saving file {self.filename}: {e}")

def __init__(
self,
*args,
filename: str = None,
language: str = 'Python',
can_load: bool = True,
**kwargs
self,
*args,
filename: str = None,
language: str = "Python",
can_load: bool = True,
**kwargs
):
super(CodeEditor, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

layout = QtWidgets.QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
Expand All @@ -192,29 +202,33 @@ def __init__(
language=language
)
layout.addWidget(self.editor)
h_layout = QtWidgets.QHBoxLayout()

load_button = QtWidgets.QPushButton('load')
save_button = QtWidgets.QPushButton('save')
run_button = QtWidgets.QPushButton('run')

h_layout.addWidget(self.line_edit)
h_layout.addWidget(load_button)
h_layout.addWidget(save_button)
h_layout.addWidget(run_button)
layout.addLayout(h_layout)
# Button layout
button_layout = QtWidgets.QHBoxLayout()
self.load_button = QtWidgets.QPushButton("Load")
self.save_button = QtWidgets.QPushButton("Save")
self.run_button = QtWidgets.QPushButton("Run")

button_layout.addWidget(self.line_edit)
button_layout.addWidget(self.load_button)
button_layout.addWidget(self.save_button)
button_layout.addWidget(self.run_button)
layout.addLayout(button_layout)

# Connect buttons to actions
self.save_button.clicked.connect(self.save_text)
self.load_button.clicked.connect(self.load_file_event)
self.run_button.clicked.connect(self.run_macro)

# Handle initial file loading
if isinstance(filename, str) and pathlib.Path(filename).is_file():
self.load_file(filename=filename)

save_button.clicked.connect(self.save_text)
load_button.clicked.connect(self.load_file)
run_button.clicked.connect(self.run_macro)
if isinstance(language, str) and language.lower() != "python":
self.run_button.hide()

# Load the file
if filename is not None and filename != '':
self.load_file(filename=filename)
if language != 'Python':
run_button.hide()
if not can_load:
load_button.hide()
self.load_button.hide()


if __name__ == "__main__":
Expand Down
12 changes: 6 additions & 6 deletions chisurf/settings/settings_chisurf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ parameter:
hide_label: false
gui:
editor:
margins_background_color: "#a1a1a1"
marker_background_color: "#d6d6d6"
caret_line_background_color: "#747677"
paper_color: "#747677"
default_color: "#e1e1e1"
margins_background_color: "#808080"
marker_background_color: "#f0f0f0"
caret_line_background_color: "#afafaf"
paper_color: "#cfcfcf"
default_color: "#000006"
font_size: 9
font_family: Sans-Serif
font_family: Courier New
language: Python
style_sheet: dark.css
RubberBandResize: true
Expand Down

0 comments on commit 785debb

Please sign in to comment.