source: sasview/src/sas/qtgui/Utilities/PluginManager.py @ 3b3b40b

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 3b3b40b was 3b3b40b, checked in by Piotr Rozyczko <rozyczko@…>, 5 years ago

Merging feature branches

  • Property mode set to 100644
File size: 4.7 KB
Line 
1# global
2import sys
3import os
4import logging 
5from shutil import copyfile
6
7from PyQt5 import QtCore
8from PyQt5 import QtGui
9from PyQt5 import QtWidgets
10
11from sas.sascalc.fit import models
12from sas.qtgui.Perspectives.Fitting import ModelUtilities
13from sas.qtgui.Utilities.TabbedModelEditor import TabbedModelEditor
14import sas.qtgui.Utilities.GuiUtils as GuiUtils
15
16from sas.qtgui.Utilities.UI.PluginManagerUI import Ui_PluginManagerUI
17
18
19class PluginManager(QtWidgets.QDialog, Ui_PluginManagerUI):
20    """
21    Class describing the model plugin manager.
22    This is a simple list widget allowing for viewing/adding/deleting custom models.
23    """
24    def __init__(self, parent=None):
25        super(PluginManager, self).__init__()
26        self.setupUi(self)
27
28        self.parent = parent
29        self.cmdDelete.setEnabled(False)
30        self.cmdEdit.setEnabled(False)
31        self.cmdDuplicate.setEnabled(False)
32
33        # globals
34        self.readModels()
35
36        # internal representation of the parameter list
37        # {<row>: (<parameter>, <value>)}
38        self.plugin_dict = {}
39
40        # Initialize signals
41        self.addSignals()
42
43    def readModels(self):
44        """
45        Read in custom models from the default location
46        """
47        self.lstModels.clear()
48        plugins = ModelUtilities._find_models()
49        models = list(plugins.keys())
50        self.lstModels.addItems(models)
51
52    def addSignals(self):
53        """
54        Define slots for widget signals
55        """
56        self.cmdOK.clicked.connect(self.accept)
57        self.cmdDelete.clicked.connect(self.onDelete)
58        self.cmdAdd.clicked.connect(self.onAdd)
59        self.cmdDuplicate.clicked.connect(self.onDuplicate)
60        self.cmdEdit.clicked.connect(self.onEdit)
61        self.cmdHelp.clicked.connect(self.onHelp)
62        self.lstModels.selectionModel().selectionChanged.connect(self.onSelectionChanged)
63        self.parent.communicate.customModelDirectoryChanged.connect(self.readModels)
64
65    def onSelectionChanged(self):
66        """
67        Respond to row selection
68        """
69        rows = len(self.lstModels.selectionModel().selectedRows())
70        self.cmdDelete.setEnabled(rows>0)
71        self.cmdEdit.setEnabled(rows==1)
72        self.cmdDuplicate.setEnabled(rows>0)
73
74    def onDelete(self):
75        """
76        Remove the file containing the selected plugin
77        """
78        plugins_to_delete = [s.data() for s in self.lstModels.selectionModel().selectedRows()]
79
80        delete_msg = "Are you sure you want to remove the selected plugins?"
81        reply = QtWidgets.QMessageBox.question(
82            self,
83            'Warning',
84            delete_msg,
85            QtWidgets.QMessageBox.Yes,
86            QtWidgets.QMessageBox.No)
87
88        # Exit if no
89        if reply == QtWidgets.QMessageBox.No:
90            return
91
92        for plugin in plugins_to_delete:
93            name = os.path.join(ModelUtilities.find_plugins_dir(), plugin + ".py")
94            os.remove(name)
95
96        self.parent.communicate.customModelDirectoryChanged.emit()
97
98    def onAdd(self):
99        """
100        Show the add new model dialog
101        """
102        self.add_widget = TabbedModelEditor(parent=self.parent)
103        self.add_widget.show()
104
105    def onDuplicate(self):
106        """
107        Creates a copy of the selected model(s)
108        """
109
110        plugins_to_copy = [s.data() for s in self.lstModels.selectionModel().selectedRows()]
111        plugin_dir = ModelUtilities.find_plugins_dir()
112        for plugin in plugins_to_copy:
113            src_filename = plugin + ".py"
114            src_file = os.path.join(plugin_dir, src_filename)
115            dst_filename = GuiUtils.findNextFilename(src_filename, plugin_dir)
116            if not dst_filename:
117                logging.error("Could not find appropriate filename for "+src_file)
118            dst_file = os.path.join(plugin_dir, dst_filename)
119            copyfile(src_file, dst_file)
120            self.parent.communicate.customModelDirectoryChanged.emit()
121
122    def onEdit(self):
123        """
124        Show the edit existing model dialog
125        """
126        plugin_location = models.find_plugins_dir()
127        # GUI assured only one row selected. Pick up the only element in list.
128        try:
129            model_to_edit = self.lstModels.selectionModel().selectedRows()[0].data()
130        except Exception:
131            # Something wrong with model, return
132            return
133        name = os.path.join(plugin_location, model_to_edit + ".py")
134        self.edit_widget = TabbedModelEditor(parent=self.parent, edit_only=True)
135        self.edit_widget.loadFile(name)
136        self.edit_widget.show()
137
138    def onHelp(self):
139        """
140        Show the help page in the default browser
141        """
142        location = "/user/sasgui/perspectives/fitting/fitting_help.html#new-plugin-model"
143        self.parent.showHelp(location)
144               
Note: See TracBrowser for help on using the repository browser.