Skip to content

Commit

Permalink
Added plugin loading for external plugins. #1443
Browse files Browse the repository at this point in the history
  • Loading branch information
rozyczko committed Jan 20, 2020
1 parent 9a26a2b commit 3c0b7ba
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/sas/qtgui/Plotting/Plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def __init__(self, parent=None, manager=None, quickplot=False):

parent.geometry()


@property
def data(self):
return self._data
Expand Down Expand Up @@ -229,7 +228,6 @@ def plot(self, data=None, color=None, marker=None, hide_error=False, transform=T
self.plot_lines[data.name] = line

# Now add the legend with some customizations.

if self.showLegend:
width=_legendResize(self.canvas.size().width(), self.parent)
if width is not None:
Expand Down
56 changes: 56 additions & 0 deletions src/sas/qtgui/Utilities/PluginManager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# global
import os
from shutil import copyfile
import logging

from PyQt5 import QtWidgets, QtCore

Expand Down Expand Up @@ -54,6 +55,7 @@ def addSignals(self):
self.cmdOK.clicked.connect(self.accept)
self.cmdDelete.clicked.connect(self.onDelete)
self.cmdAdd.clicked.connect(self.onAdd)
self.cmdAddFile.clicked.connect(self.onAddFile)
self.cmdDuplicate.clicked.connect(self.onDuplicate)
self.cmdEdit.clicked.connect(self.onEdit)
self.cmdHelp.clicked.connect(self.onHelp)
Expand Down Expand Up @@ -100,6 +102,60 @@ def onAdd(self):
self.add_widget = TabbedModelEditor(parent=self.parent)
self.add_widget.show()

def onAddFile(self):
"""
Open system Load FIle dialog, load a plugin and put it in the plugin directory
"""
plugin_file = QtWidgets.QFileDialog.getOpenFileName(
self, "Choose a plugin", "","Python (*.py)")[0]

This comment has been minimized.

Copy link
@pkienzle

pkienzle Jan 24, 2020

Contributor

Maybe some .c files associated with the plugin, but only the .py file needs to be checked that it is a valid plugin model.


if not plugin_file:
return

plugin_dir = models.find_plugins_dir()
file_name = os.path.basename(str(plugin_file))

# check if valid model
try:
model_results = GuiUtils.checkModel(plugin_file)
logging.info(model_results)
# We can't guarantee the type of the exception coming from
# Sasmodels, so need the overreaching general Exception
except Exception as ex:
msg = "Invalid plugin: %s " % file_name
msgbox = QtWidgets.QMessageBox()
msgbox.setIcon(QtWidgets.QMessageBox.Critical)
msgbox.setText(msg)
msgbox.setStandardButtons(QtWidgets.QMessageBox.Ok)
retval = msgbox.exec_()
return

# check if file with the same name exists
if file_name in os.listdir(plugin_dir):
msg = "Plugin " + file_name + " already exists.\n"
msg += "Do you wish to overwrite the file?"
msgbox = QtWidgets.QMessageBox(self)
msgbox.setIcon(QtWidgets.QMessageBox.Warning)
msgbox.setText(msg)
msgbox.setWindowTitle("Plugin Load")
# custom buttons
button_yes = QtWidgets.QPushButton("Yes")
msgbox.addButton(button_yes, QtWidgets.QMessageBox.YesRole)
button_no = QtWidgets.QPushButton("No")
msgbox.addButton(button_no, QtWidgets.QMessageBox.RejectRole)
retval = msgbox.exec_()
if retval == QtWidgets.QMessageBox.RejectRole:
# cancel copy
return

# Copy from origin to ~/.sasview/plugin_models
from shutil import copy
# no check on clash
copy(plugin_file, plugin_dir)
self.parent.communicate.customModelDirectoryChanged.emit()
log_msg = "New plugin added: %s" % file_name
logging.info(log_msg)

def onDuplicate(self):
"""
Creates a copy of the selected model(s)
Expand Down
13 changes: 10 additions & 3 deletions src/sas/qtgui/Utilities/UI/PluginManagerUI.ui
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@
<item>
<widget class="QPushButton" name="cmdAdd">
<property name="text">
<string>Add</string>
<string>Add...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cmdAddFile">
<property name="text">
<string>Add file...</string>
</property>
</widget>
</item>
Expand All @@ -49,7 +56,7 @@
<item>
<widget class="QPushButton" name="cmdEdit">
<property name="text">
<string>Edit</string>
<string>Edit...</string>
</property>
</widget>
</item>
Expand All @@ -75,7 +82,7 @@
</item>
</layout>
</item>
<item row="1" column="0" colspan="2">
<item row="1" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
Expand Down

0 comments on commit 3c0b7ba

Please sign in to comment.