Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change model function input to multi-line text field #28

Merged
merged 2 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/tailor/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,9 @@ def tab_changed(self, idx):
Args:
idx: an integer index of the now-focused tab.
"""
tab = self.ui.tabWidget.currentWidget()
if type(tab.code) == PlotTab:
tab.code.update_plot()
tab = self.ui.tabWidget.currentWidget().code
if type(tab) == PlotTab:
tab.update_plot()

def add_column(self):
"""Add column to data model and select it."""
Expand Down Expand Up @@ -446,7 +446,7 @@ def rename_plot_variables(self, old_name, new_name):
new_name: the new column name
"""
num_tabs = self.ui.tabWidget.count()
tabs = [self.ui.tabWidget.widget(i) for i in range(num_tabs)]
tabs = [self.ui.tabWidget.widget(i).code for i in range(num_tabs)]
for tab in tabs:
if type(tab) == PlotTab:
needs_info_update = False
Expand All @@ -455,6 +455,8 @@ def rename_plot_variables(self, old_name, new_name):
needs_info_update = True
setattr(tab, var, new_name)
# The following creates problems with partial matches
# For now, the model function is *not* updated
#
# if var == "x_var":
# update model expression and model object
# expr = tab.model_func.text()
Expand Down Expand Up @@ -548,7 +550,7 @@ def clear_all(self):
# close all plot tabs in reverse order, they are no longer valid
self.ui.tabWidget.removeTab(idx)
self._plot_num = 1
self.data_model = DataModel(main_window=self)
self.data_model = DataModel(main_window=self.ui)
self._set_view_and_selection_model()
self.ui.data_view.setCurrentIndex(self.data_model.createIndex(0, 0))
self._set_project_path(None)
Expand Down Expand Up @@ -603,9 +605,9 @@ def save_project(self, filename):

for idx in range(1, self.ui.tabWidget.count()):
# save data for each tab
tab = self.ui.tabWidget.widget(idx)
tab = self.ui.tabWidget.widget(idx).code
tab_data = {"label": self.ui.tabWidget.tabBar().tabText(idx)}
tab.code.save_state_to_obj(tab_data)
tab.save_state_to_obj(tab_data)
save_obj["tabs"].append(tab_data)
except Exception as exc:
self._show_exception(
Expand Down Expand Up @@ -795,7 +797,7 @@ def export_graph(self, suffix):
Args:
suffix: the required suffix of the file.
"""
tab = self.ui.tabWidget.currentWidget()
tab = self.ui.tabWidget.currentWidget().code
if type(tab) == PlotTab:
filename, _ = QtWidgets.QFileDialog.getSaveFileName(
parent=self.ui,
Expand Down
6 changes: 3 additions & 3 deletions src/tailor/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def rename_column(self, col_idx, new_name):
except KeyError:
pass
self.headerDataChanged.emit(QtCore.Qt.Horizontal, col_idx, col_idx)
self.main_window.statusbar.showMessage("Renamed column.", msecs=MSG_TIMEOUT)
self.main_window.statusbar.showMessage("Renamed column.", timeout=MSG_TIMEOUT)

def update_column_expression(self, col_idx, expression):
"""Update a calculated column with a new expression.
Expand All @@ -285,7 +285,7 @@ def update_column_expression(self, col_idx, expression):
bottom_right = self.createIndex(len(self._data), col_idx)
self.dataChanged.emit(top_left, bottom_right)
self.main_window.statusbar.showMessage(
"Updated column values.", msecs=MSG_TIMEOUT
"Updated column values.", timeout=MSG_TIMEOUT
)

def recalculate_column(self, col_name, expression=None):
Expand All @@ -311,7 +311,7 @@ def recalculate_column(self, col_name, expression=None):
for err in aeval.error:
exc, msg = err.get_error()
self.main_window.statusbar.showMessage(
f"ERROR: {exc}: {msg}.", msecs=MSG_TIMEOUT
f"ERROR: {exc}: {msg}.", timeout=MSG_TIMEOUT
)
elif output is not None:
if isinstance(output, pd.Series):
Expand Down
10 changes: 7 additions & 3 deletions src/tailor/plot_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def get_params_and_update_model(self):
Returns:
A set of parameter names.
"""
model_expr = self.ui.model_func.text()
model_expr = self.ui.model_func.toPlainText()
code = compile(model_expr, "<string>", "eval")
params = set(code.co_names) - set([self.x_var]) - self._symbols
if self.y_var in params:
Expand Down Expand Up @@ -698,7 +698,6 @@ def save_state_to_obj(self, save_obj):
{
name: getattr(self.ui, name).text()
for name in [
"model_func",
"xlabel",
"xmin",
"xmax",
Expand All @@ -709,6 +708,9 @@ def save_state_to_obj(self, save_obj):
}
)

# save plaintext strings
save_obj["model_func"] = self.ui.model_func.toPlainText()

save_obj["parameters"] = self.get_parameter_hints()

# save (possibly outdated) fit
Expand Down Expand Up @@ -747,7 +749,6 @@ def load_state_from_obj(self, save_obj):

# load linedit strings
for name in [
"model_func",
"xlabel",
"xmin",
"xmax",
Expand All @@ -759,6 +760,9 @@ def load_state_from_obj(self, save_obj):
widget = getattr(self.ui, name)
widget.setText(text)

# load plaintext strings
self.ui.model_func.setPlainText(save_obj["model_func"])

# load checkbox state
for name in ["use_fit_domain"]:
state = QtCore.Qt.CheckState(save_obj[name])
Expand Down
15 changes: 8 additions & 7 deletions src/tailor/resources/plot_tab.ui
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="model_func"/>
</item>
<item row="1" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
Expand All @@ -91,6 +88,13 @@
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPlainTextEdit" name="model_func">
<property name="lineWrapMode">
<enum>QPlainTextEdit::WidgetWidth</enum>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -241,9 +245,7 @@
<item>
<widget class="QPlainTextEdit" name="result_box">
<property name="font">
<font>
<family>Courier New</family>
</font>
<font/>
</property>
<property name="lineWrapMode">
<enum>QPlainTextEdit::NoWrap</enum>
Expand Down Expand Up @@ -388,7 +390,6 @@
</customwidget>
</customwidgets>
<tabstops>
<tabstop>model_func</tabstop>
<tabstop>show_initial_fit</tabstop>
<tabstop>fit_start_box</tabstop>
<tabstop>fit_end_box</tabstop>
Expand Down