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

WIP: Fixed derived components #1488

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
More improvements to derived component editor
  • Loading branch information
astrofrog committed Nov 29, 2017
commit 51870a6106025ffa18fb30d22a28a2be2362f0dc
160 changes: 97 additions & 63 deletions glue/dialogs/component_manager/qt/equation_editor.py
Original file line number Diff line number Diff line change
@@ -136,6 +136,8 @@ def __init__(self, data=None, equation=None, references=None, parent=None):
self._setup_freeform_tab(data=data, equation=equation, references=references)
self._setup_predefined_tab(data=data)

self.ui.tab.currentChanged.connect(self._update_status)

def _setup_predefined_tab(self, data=None):

# Populate category combo
@@ -168,54 +170,78 @@ def is_function(self):
return self.function is not None and type(self.function).__name__ == 'LinkFunction'

def _setup_inputs(self, event=None):

if self.is_function:
self._setup_inputs_function()
label = function_label(self.function)
input_labels = getfullargspec(self.function.function)[0]

else:
self._setup_inputs_helper()
label = helper_label(self.function)
input_labels = self.function.input_labels

def _clear_inputs_layout(self):
while self.ui.layout_inputs.count() > 0:
item = self.ui.layout_inputs.itemAt(0)
self.ui.layout_inputs.removeItem(item)
item.widget().setParent(None)
self.ui.label_info.setText(label)

def _add_input_widget(self, name):
widget = QtWidgets.QWidget()
layout = QtWidgets.QHBoxLayout()
label = QtWidgets.QLabel(name)
combo = QtWidgets.QComboBox()
update_combobox(combo, list(self.references.items()))
layout.addWidget(label)
layout.addWidget(combo)
widget.setLayout(layout)
layout.setContentsMargins(1, 0, 1, 1)
self.ui.layout_inputs.addWidget(widget)
self._clear_input_output_layouts()

def _setup_inputs_function(self):
input_message = "The function above takes the following input(s):"

func = self.function.function
args = getfullargspec(func)[0]
label = function_label(self.function)
self.ui.label_info.setText(label)
if len(input_labels) > 1:
input_message = input_message.replace('(s)', 's')
else:
input_message = input_message.replace('(s)', '')

self._clear_inputs_layout()
self.ui.layout_inout.addWidget(QtWidgets.QLabel(input_message), 0, 1, 1, 3)

for a in args:
self._add_input_widget(a)
spacer1 = QtWidgets.QSpacerItem(10, 5,
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Fixed)
spacer2 = QtWidgets.QSpacerItem(10, 5,
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Fixed)
self.ui.layout_inout.addItem(spacer1, 0, 0)
self.ui.layout_inout.addItem(spacer2, 0, 4)

def _setup_inputs_helper(self):
row = 0
for a in input_labels:
row += 1
self._add_input_widget(a, row)

# Here it looks like helpers need to be clearer which are the inputs from
# one side and the other side (input/output)
output_message = "This function produces the following output(s) - you can set the label(s) here:"

label = helper_label(self.function)
args = self.function.input_labels
self.ui.label_info.setText(label)
if len(self.function.output_labels) > 1:
output_message = output_message.replace('(s)', 's')
else:
output_message = output_message.replace('(s)', '')

row += 1
self.ui.layout_inout.addWidget(QtWidgets.QLabel(output_message), row, 1, 1, 3)

for a in self.function.output_labels:
row += 1
self._add_output_widget(a, row)

def _clear_input_output_layouts(self):

for row in range(self.ui.layout_inout.rowCount()):
for col in range(self.ui.layout_inout.columnCount()):
item = self.ui.layout_inout.itemAtPosition(row, col)
if item is not None:
self.ui.layout_inout.removeItem(item)
if item.widget() is not None:
item.widget().setParent(None)

self._clear_inputs_layout()
def _add_input_widget(self, name, row):
label = QtWidgets.QLabel(name)
combo = QtWidgets.QComboBox()
update_combobox(combo, list(self.references.items()))
self.ui.layout_inout.addWidget(label, row, 1)
self.ui.layout_inout.addWidget(combo, row, 2)

for a in args:
self._add_input_widget(a)
def _add_output_widget(self, name, row):
label = QtWidgets.QLabel(name)
edit = QtWidgets.QLineEdit()
self.ui.layout_inout.addWidget(label, row, 1)
self.ui.layout_inout.addWidget(edit, row, 2)

def _populate_function_combo(self, event=None):
"""
@@ -260,42 +286,50 @@ def _insert_component(self):
label = self.ui.combosel_component.currentText()
self.expression.insertPlainText('{' + label + '}')

def _update_status(self):
def _update_status(self, event=None):

# If the text hasn't changed, no need to check again
if hasattr(self, '_cache') and self._get_raw_command() == self._cache:
return
if self.ui.tab.currentIndex() == 0:

self.ui.label_status.setStyleSheet('color: green')
self.ui.label_status.setText('')
self.ui.button_ok.setEnabled(True)

if self._get_raw_command() == "":
self.ui.label_status.setText("")
self.ui.button_ok.setEnabled(False)
else:
try:
pc = self._get_parsed_command()
result = pc.evaluate_test()
except SyntaxError:
self.ui.label_status.setStyleSheet('color: red')
self.ui.label_status.setText("Incomplete or invalid syntax")
self.ui.button_ok.setEnabled(False)
except InvalidTagError as exc:
self.ui.label_status.setStyleSheet('color: red')
self.ui.label_status.setText("Invalid component: {0}".format(exc.tag))
self.ui.button_ok.setEnabled(False)
except Exception as exc:
self.ui.label_status.setStyleSheet('color: red')
self.ui.label_status.setText(str(exc))

# If the text hasn't changed, no need to check again
if hasattr(self, '_cache') and self._get_raw_command() == self._cache and event is None:
return

if self._get_raw_command() == "":
self.ui.label_status.setText("")
self.ui.button_ok.setEnabled(False)
else:
if result is None:
try:
pc = self._get_parsed_command()
result = pc.evaluate_test()
except SyntaxError:
self.ui.label_status.setStyleSheet('color: red')
self.ui.label_status.setText("Incomplete or invalid syntax")
self.ui.button_ok.setEnabled(False)
except InvalidTagError as exc:
self.ui.label_status.setStyleSheet('color: red')
self.ui.label_status.setText("Expression should not return None")
self.ui.label_status.setText("Invalid component: {0}".format(exc.tag))
self.ui.button_ok.setEnabled(False)
except Exception as exc:
self.ui.label_status.setStyleSheet('color: red')
self.ui.label_status.setText(str(exc))
self.ui.button_ok.setEnabled(False)
else:
self.ui.label_status.setStyleSheet('color: green')
self.ui.label_status.setText("Valid expression")
self.ui.button_ok.setEnabled(True)

self._cache = self._get_raw_command()
if result is None:
self.ui.label_status.setStyleSheet('color: red')
self.ui.label_status.setText("Expression should not return None")
self.ui.button_ok.setEnabled(False)
else:
self.ui.label_status.setStyleSheet('color: green')
self.ui.label_status.setText("Valid expression")
self.ui.button_ok.setEnabled(True)

self._cache = self._get_raw_command()

def _get_raw_command(self):
return str(self.ui.expression.toPlainText())
95 changes: 83 additions & 12 deletions glue/dialogs/component_manager/qt/equation_editor.ui
Original file line number Diff line number Diff line change
@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>748</width>
<height>493</height>
<width>532</width>
<height>402</height>
</rect>
</property>
<property name="windowTitle">
@@ -17,6 +17,9 @@
<property name="spacing">
<number>5</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<property name="leftMargin">
<number>10</number>
</property>
@@ -90,29 +93,63 @@
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_info">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Info</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>The function above takes the following inputs - select a component for each output:</string>
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QVBoxLayout" name="layout_inputs"/>
<layout class="QGridLayout" name="layout_inout"/>
</item>
<item>
<widget class="QLabel" name="label_output">
<property name="text">
<string>Description of which components this will create</string>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
@@ -169,7 +206,7 @@
<item>
<widget class="ColorizedCompletionTextEdit" name="expression">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Maximum">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
@@ -182,6 +219,40 @@
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Derived component name:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit">
<property name="maximumSize">
<size>
<width>100</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</widget>