Improvements to the user defined template function preference editor:

- Support filtering to show only user defined functions or all functions
- Show the function type in the function name combo box
- Minor improvement in when the delete button is enabled or not
This commit is contained in:
Charles Haley 2022-07-09 15:39:45 +01:00
parent e66c896d25
commit 6a3dafd8a8
2 changed files with 57 additions and 16 deletions

View File

@ -168,6 +168,8 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.db.prefs['user_template_functions'] = []
raise AbortInitialize()
self.show_only_user_defined.setChecked(True)
self.show_only_user_defined.stateChanged.connect(self.show_only_user_defined_changed)
self.build_function_names_box()
self.function_name.currentIndexChanged.connect(self.function_index_changed)
self.function_name.editTextChanged.connect(self.function_name_edited)
@ -222,6 +224,9 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
# Python function tab
def show_only_user_defined_changed(self, state):
self.build_function_names_box()
def enable_replace_button(self):
self.replace_button.setEnabled(self.delete_button.isEnabled())
@ -233,23 +238,35 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.create_button.setEnabled(False)
self.delete_button.setEnabled(False)
def function_type_string(self, name):
if name in self.builtins:
return ' -- ' + _('Built-in function')
else:
return ' -- ' + _('User function')
def build_function_names_box(self, scroll_to=''):
self.function_name.blockSignals(True)
if self.show_only_user_defined.isChecked():
func_names = sorted([k for k in self.funcs if k not in self.builtins])
else:
func_names = sorted(self.funcs)
self.function_name.clear()
self.function_name.addItem('')
self.function_name.addItems(func_names)
scroll_to_index = 0
for idx,n in enumerate(func_names):
self.function_name.addItem(n + self.function_type_string(n))
self.function_name.setItemData(idx+1, n)
if scroll_to and n == scroll_to:
scroll_to_index = idx+1
self.function_name.setCurrentIndex(0)
self.function_name.blockSignals(False)
if scroll_to:
idx = self.function_name.findText(scroll_to)
if idx >= 0:
self.function_name.setCurrentIndex(idx)
if scroll_to_index:
self.function_name.setCurrentIndex(scroll_to_index)
if scroll_to not in self.builtins:
self.delete_button.setEnabled(True)
def delete_button_clicked(self):
name = str(self.function_name.currentText())
name = str(self.function_name.itemData(self.function_name.currentIndex()))
if name in self.builtins:
error_dialog(self.gui, _('Template functions'),
_('You cannot delete a built-in function'), show=True)
@ -267,6 +284,11 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
def create_button_clicked(self, use_name=None):
self.changed_signal.emit()
name = use_name if use_name else str(self.function_name.currentText())
name = name.split(' -- ')[0]
if not name:
error_dialog(self.gui, _('Template functions'),
_('Name cannot be empty'), show=True)
return
if name in self.funcs:
error_dialog(self.gui, _('Template functions'),
_('Name %s already used')%(name,), show=True)
@ -299,14 +321,20 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
det_msg=traceback.format_exc())
def function_name_edited(self, txt):
txt = txt.split(' -- ')[0]
if txt not in self.funcs:
self.function_name.blockSignals(True)
self.function_name.setEditText(txt)
self.function_name.blockSignals(False)
self.documentation.setReadOnly(False)
self.argument_count.setReadOnly(False)
self.create_button.setEnabled(True)
self.replace_button.setEnabled(False)
self.delete_button.setEnabled(False)
self.program.setReadOnly(False)
def function_index_changed(self, idx):
txt = self.function_name.currentText()
txt = self.function_name.itemData(idx)
self.create_button.setEnabled(False)
if not txt:
self.argument_count.clear()
@ -335,7 +363,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.replace_button.setEnabled(False)
def replace_button_clicked(self):
name = str(self.function_name.currentText())
name = str(self.function_name.itemData(self.function_name.currentIndex()))
self.delete_button_clicked()
self.create_button_clicked(use_name=name)

View File

@ -125,6 +125,19 @@
<item row="0" column="1" colspan="2" rowspan="2">
<widget class="QTextBrowser" name="textBrowser"/>
</item>
<item row="3" column="1">
<widget class="QCheckBox" name="show_only_user_defined">
<property name="text">
<string>Show &amp;only user defined template functions</string>
</property>
<property name="toolTip">
<string>&lt;p&gt;If checked the 'Function' box will show only user defined
template functions. If unchecked the box will show both system and user defined
functions. Unchecking can be useful for copying source of a system function to create
a new related user defined function.&lt;/p&gt;</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
@ -145,7 +158,7 @@
</property>
</widget>
</item>
<item row="3" column="0">
<item row="4" column="0">
<widget class="QLabel" name="label_3">
<property name="toolTip">
<string/>
@ -158,7 +171,7 @@
</property>
</widget>
</item>
<item row="3" column="1">
<item row="4" column="1">
<widget class="QSpinBox" name="argument_count">
<property name="toolTip">
<string>Set this to -1 if the function takes a variable number of arguments</string>
@ -168,10 +181,10 @@
</property>
</widget>
</item>
<item row="4" column="1">
<item row="5" column="1">
<widget class="QTextEdit" name="documentation"/>
</item>
<item row="4" column="0">
<item row="5" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>D&amp;ocumentation:</string>
@ -184,7 +197,7 @@
</property>
</widget>
</item>
<item row="5" column="1">
<item row="6" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="clear_button">