dev #297
@ -2,136 +2,25 @@ from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from .usermodeleditor import QUsermodelEditor
|
||||
from ..Qt import QtWidgets, QtCore, QtGui
|
||||
from .codeeditor import EditorWidget
|
||||
|
||||
|
||||
class QEditor(QtWidgets.QMainWindow):
|
||||
class QEditor(QUsermodelEditor):
|
||||
runSignal = QtCore.pyqtSignal(str)
|
||||
|
||||
def __init__(self, path: str | Path = None, parent=None):
|
||||
super().__init__(parent=parent)
|
||||
super().__init__(path, parent=parent)
|
||||
|
||||
self._init_gui()
|
||||
self.add_run_button()
|
||||
|
||||
self.fname = None
|
||||
self._dir = None
|
||||
if path is not None:
|
||||
self.read_file(path)
|
||||
def add_run_button(self):
|
||||
self.run_button = QtWidgets.QPushButton("Run")
|
||||
self.centralwidget.layout().addWidget(self.run_button)
|
||||
|
||||
def _init_gui(self):
|
||||
self.centralwidget = QtWidgets.QWidget(self)
|
||||
|
||||
layout = QtWidgets.QVBoxLayout(self.centralwidget)
|
||||
layout.setContentsMargins(3, 3, 3, 3)
|
||||
layout.setSpacing(3)
|
||||
|
||||
self.edit_field = EditorWidget(self.centralwidget)
|
||||
font = QtGui.QFont('default')
|
||||
font.setStyleHint(font.Monospace)
|
||||
font.setPointSize(10)
|
||||
self.edit_field.setFont(font)
|
||||
|
||||
layout.addWidget(self.edit_field)
|
||||
self.setCentralWidget(self.centralwidget)
|
||||
|
||||
self.statusbar = QtWidgets.QStatusBar(self)
|
||||
self.setStatusBar(self.statusbar)
|
||||
|
||||
self.menubar = self.menuBar()
|
||||
|
||||
self.menuFile = QtWidgets.QMenu('File', self.menubar)
|
||||
self.menubar.addMenu(self.menuFile)
|
||||
|
||||
self.menuFile.addAction('Open...', self.open_file, QtGui.QKeySequence.Open)
|
||||
self.menuFile.addAction('Save', self.overwrite_file, QtGui.QKeySequence.Save)
|
||||
self.menuFile.addAction('Save as...', self.save_file, QtGui.QKeySequence('Ctrl+Shift+S'))
|
||||
self.menuFile.addSeparator()
|
||||
self.menuFile.addAction('Close', self.close, QtGui.QKeySequence.Quit)
|
||||
|
||||
self.resize(800, 600)
|
||||
self.setGeometry(
|
||||
QtWidgets.QStyle.alignedRect(
|
||||
QtCore.Qt.LayoutDirection.LeftToRight,
|
||||
QtCore.Qt.AlignmentFlag.AlignCenter,
|
||||
self.size(),
|
||||
QtWidgets.qApp.desktop().availableGeometry()
|
||||
)
|
||||
)
|
||||
|
||||
def is_modified(self):
|
||||
return self.edit_field.editor.document().isModified()
|
||||
|
||||
def set_modified(self, val: bool):
|
||||
self.edit_field.editor.document().setModified(val)
|
||||
self.run_button.clicked.connect(self.start_script)
|
||||
|
||||
@QtCore.pyqtSlot()
|
||||
def open_file(self):
|
||||
overwrite = self.changes_saved
|
||||
|
||||
if overwrite:
|
||||
fname, _ = QtWidgets.QFileDialog.getOpenFileName(directory=str(self._dir))
|
||||
if fname:
|
||||
self.read_file(fname)
|
||||
|
||||
def read_file(self, fname: str | Path):
|
||||
self.set_fname_opts(fname)
|
||||
|
||||
if self.fname is not None:
|
||||
with self.fname.open('r') as f:
|
||||
self.edit_field.setPlainText(f.read())
|
||||
|
||||
def set_fname_opts(self, fname: str | Path):
|
||||
fname = Path(fname)
|
||||
if fname.is_file():
|
||||
self.fname = Path(fname)
|
||||
self._dir = self.fname.parent
|
||||
elif fname.is_dir():
|
||||
self._dir = fname
|
||||
self.setWindowTitle('Edit ' + str(fname))
|
||||
|
||||
def changes_saved(self) -> bool:
|
||||
if not self.is_modified():
|
||||
return True
|
||||
|
||||
ret = QtWidgets.QMessageBox.question(self, 'Time to think',
|
||||
'<h4><p>The document was modified.</p>\n'
|
||||
'<p>Do you want to save changes?</p></h4>',
|
||||
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No |
|
||||
QtWidgets.QMessageBox.Cancel)
|
||||
if ret == QtWidgets.QMessageBox.Yes:
|
||||
self.save_file()
|
||||
|
||||
if ret == QtWidgets.QMessageBox.No:
|
||||
self.set_modified(False)
|
||||
|
||||
return not self.is_modified()
|
||||
|
||||
@QtCore.pyqtSlot()
|
||||
def save_file(self):
|
||||
outfile, _ = QtWidgets.QFileDialog().getSaveFileName(parent=self, caption='Save file', directory=str(self._dir))
|
||||
|
||||
if outfile:
|
||||
with open(outfile, 'w') as f:
|
||||
f.write(self.edit_field.toPlainText())
|
||||
|
||||
self.set_fname_opts(outfile)
|
||||
|
||||
self.set_modified(False)
|
||||
|
||||
return self.is_modified()
|
||||
|
||||
@QtCore.pyqtSlot()
|
||||
def overwrite_file(self):
|
||||
if self.fname is not None:
|
||||
with self.fname.open('w') as f:
|
||||
f.write(self.edit_field.toPlainText())
|
||||
|
||||
self.modelsChanged.emit()
|
||||
|
||||
self.set_modified(False)
|
||||
|
||||
def closeEvent(self, evt: QtGui.QCloseEvent):
|
||||
def start_script(self):
|
||||
self.runSignal.emit(self.edit_field.toPlainText())
|
||||
|
||||
super().closeEvent(evt)
|
||||
|
@ -77,13 +77,19 @@ class QUsermodelEditor(QtWidgets.QMainWindow):
|
||||
def read_file(self, fname: str | Path):
|
||||
self.set_fname_opts(fname)
|
||||
|
||||
with self.fname.open('r') as f:
|
||||
self.edit_field.setPlainText(f.read())
|
||||
if self.fname is not None:
|
||||
with self.fname.open('r') as f:
|
||||
self.edit_field.setPlainText(f.read())
|
||||
|
||||
def set_fname_opts(self, fname: str | Path):
|
||||
self.fname = Path(fname)
|
||||
self._dir = self.fname.parent
|
||||
self.setWindowTitle('Edit ' + str(fname))
|
||||
fname = Path(fname)
|
||||
if fname.is_file():
|
||||
self.fname = Path(fname)
|
||||
self._dir = self.fname.parent
|
||||
self.setWindowTitle('Edit ' + str(fname))
|
||||
elif fname.is_dir():
|
||||
self._dir = fname
|
||||
|
||||
|
||||
def changes_saved(self) -> bool:
|
||||
if not self.is_modified():
|
||||
|
@ -88,7 +88,6 @@ class QFCReader(QtWidgets.QDialog, Ui_FCEval_dialog):
|
||||
|
||||
def accept(self):
|
||||
items = [self.listWidget.item(i).text() for i in range(self.listWidget.count())]
|
||||
print(items)
|
||||
if items:
|
||||
with busy_cursor():
|
||||
self.read(items)
|
||||
|
@ -4,6 +4,8 @@ from collections import namedtuple
|
||||
|
||||
import numpy as np
|
||||
|
||||
import nmreval
|
||||
|
||||
from nmreval import models
|
||||
from nmreval.configs import config_paths
|
||||
from nmreval.lib.importer import find_models, import_
|
||||
@ -28,6 +30,7 @@ class Namespace:
|
||||
'y_err': (None, 'y error values'),
|
||||
'fit': (None, 'dictionary of fit parameter', 'fit["PIKA"]'),
|
||||
'np': (np, 'numpy module'),
|
||||
'nmreval': (nmreval, 'built-in classes and stuff')
|
||||
},
|
||||
parents=('Basic', 'General'),
|
||||
)
|
||||
|
@ -1033,7 +1033,7 @@ class UpperManagement(QtCore.QObject):
|
||||
else:
|
||||
data = self.data[sets[0]]
|
||||
if isinstance(data.data, new_type):
|
||||
error_list.append(f'{data.name} is alreade of type {new_type.__name__}')
|
||||
error_list.append(f'{data.name} is already of type {new_type.__name__}')
|
||||
continue
|
||||
|
||||
new_data = new_type(data.x, np.zeros(data.x.size))
|
||||
@ -1114,7 +1114,8 @@ class UpperManagement(QtCore.QObject):
|
||||
ns = self.namespace.flatten()
|
||||
ns['return_list'] = []
|
||||
|
||||
exec(text, globals(), ns)
|
||||
# custom namespace must be available in global namespace of exec, otherwise imports do not work in functions
|
||||
exec(text, ns, ns)
|
||||
|
||||
new_sets = []
|
||||
for new_data in ns['return_list']:
|
||||
|
Loading…
Reference in New Issue
Block a user