[f721030] | 1 | """ |
---|
| 2 | Global defaults and various utility functions usable by the general GUI |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | import os |
---|
| 6 | import sys |
---|
| 7 | import imp |
---|
| 8 | import warnings |
---|
[f82ab8c] | 9 | import webbrowser |
---|
| 10 | import urlparse |
---|
| 11 | |
---|
[f721030] | 12 | warnings.simplefilter("ignore") |
---|
| 13 | import logging |
---|
| 14 | |
---|
| 15 | from PyQt4 import QtCore |
---|
| 16 | from PyQt4 import QtGui |
---|
| 17 | |
---|
| 18 | # Translate event handlers |
---|
| 19 | #from sas.sasgui.guiframe.events import EVT_CATEGORY |
---|
| 20 | #from sas.sasgui.guiframe.events import EVT_STATUS |
---|
| 21 | #from sas.sasgui.guiframe.events import EVT_APPEND_BOOKMARK |
---|
| 22 | #from sas.sasgui.guiframe.events import EVT_PANEL_ON_FOCUS |
---|
| 23 | #from sas.sasgui.guiframe.events import EVT_NEW_LOAD_DATA |
---|
| 24 | #from sas.sasgui.guiframe.events import EVT_NEW_COLOR |
---|
| 25 | #from sas.sasgui.guiframe.events import StatusEvent |
---|
| 26 | #from sas.sasgui.guiframe.events import NewPlotEvent |
---|
[f82ab8c] | 27 | |
---|
[5032ea68] | 28 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
[1042dba] | 29 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
[f721030] | 30 | |
---|
| 31 | |
---|
| 32 | def get_app_dir(): |
---|
| 33 | """ |
---|
| 34 | The application directory is the one where the default custom_config.py |
---|
| 35 | file resides. |
---|
| 36 | |
---|
| 37 | :returns: app_path - the path to the applicatin directory |
---|
| 38 | """ |
---|
| 39 | # First, try the directory of the executable we are running |
---|
| 40 | app_path = sys.path[0] |
---|
| 41 | if os.path.isfile(app_path): |
---|
| 42 | app_path = os.path.dirname(app_path) |
---|
| 43 | if os.path.isfile(os.path.join(app_path, "custom_config.py")): |
---|
| 44 | app_path = os.path.abspath(app_path) |
---|
| 45 | logging.info("Using application path: %s", app_path) |
---|
| 46 | return app_path |
---|
| 47 | |
---|
| 48 | # Next, try the current working directory |
---|
| 49 | if os.path.isfile(os.path.join(os.getcwd(), "custom_config.py")): |
---|
| 50 | logging.info("Using application path: %s", os.getcwd()) |
---|
| 51 | return os.path.abspath(os.getcwd()) |
---|
| 52 | |
---|
| 53 | # Finally, try the directory of the sasview module |
---|
| 54 | # TODO: gui_manager will have to know about sasview until we |
---|
| 55 | # clean all these module variables and put them into a config class |
---|
| 56 | # that can be passed by sasview.py. |
---|
| 57 | logging.info(sys.executable) |
---|
| 58 | logging.info(str(sys.argv)) |
---|
| 59 | from sas import sasview as sasview |
---|
| 60 | app_path = os.path.dirname(sasview.__file__) |
---|
| 61 | logging.info("Using application path: %s", app_path) |
---|
| 62 | return app_path |
---|
| 63 | |
---|
| 64 | def get_user_directory(): |
---|
| 65 | """ |
---|
| 66 | Returns the user's home directory |
---|
| 67 | """ |
---|
| 68 | userdir = os.path.join(os.path.expanduser("~"), ".sasview") |
---|
| 69 | if not os.path.isdir(userdir): |
---|
| 70 | os.makedirs(userdir) |
---|
| 71 | return userdir |
---|
| 72 | |
---|
[481ff26] | 73 | def _find_local_config(confg_file, path): |
---|
[f721030] | 74 | """ |
---|
| 75 | Find configuration file for the current application |
---|
| 76 | """ |
---|
| 77 | config_module = None |
---|
| 78 | fObj = None |
---|
| 79 | try: |
---|
[481ff26] | 80 | fObj, path_config, descr = imp.find_module(confg_file, [path]) |
---|
| 81 | config_module = imp.load_module(confg_file, fObj, path_config, descr) |
---|
[f721030] | 82 | except: |
---|
[481ff26] | 83 | logging.error("Error loading %s/%s: %s" % (path, confg_file, sys.exc_value)) |
---|
[f721030] | 84 | finally: |
---|
| 85 | if fObj is not None: |
---|
| 86 | fObj.close() |
---|
[481ff26] | 87 | logging.info("GuiManager loaded %s/%s" % (path, confg_file)) |
---|
[f721030] | 88 | return config_module |
---|
| 89 | |
---|
| 90 | # Get APP folder |
---|
| 91 | PATH_APP = get_app_dir() |
---|
| 92 | DATAPATH = PATH_APP |
---|
| 93 | |
---|
| 94 | # GUI always starts from the App folder |
---|
| 95 | #os.chdir(PATH_APP) |
---|
| 96 | # Read in the local config, which can either be with the main |
---|
| 97 | # application or in the installation directory |
---|
| 98 | config = _find_local_config('local_config', PATH_APP) |
---|
| 99 | if config is None: |
---|
| 100 | config = _find_local_config('local_config', os.getcwd()) |
---|
| 101 | if config is None: |
---|
| 102 | # Didn't find local config, load the default |
---|
| 103 | import sas.sasgui.guiframe.config as config |
---|
| 104 | logging.info("using default local_config") |
---|
| 105 | else: |
---|
| 106 | logging.info("found local_config in %s" % os.getcwd()) |
---|
| 107 | else: |
---|
| 108 | logging.info("found local_config in %s" % PATH_APP) |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | from sas.sasgui.guiframe.customdir import SetupCustom |
---|
| 112 | c_conf_dir = SetupCustom().setup_dir(PATH_APP) |
---|
| 113 | custom_config = _find_local_config('custom_config', c_conf_dir) |
---|
| 114 | if custom_config is None: |
---|
| 115 | custom_config = _find_local_config('custom_config', os.getcwd()) |
---|
| 116 | if custom_config is None: |
---|
| 117 | msgConfig = "Custom_config file was not imported" |
---|
| 118 | logging.info(msgConfig) |
---|
| 119 | else: |
---|
| 120 | logging.info("using custom_config in %s" % os.getcwd()) |
---|
| 121 | else: |
---|
| 122 | logging.info("using custom_config from %s" % c_conf_dir) |
---|
| 123 | |
---|
| 124 | #read some constants from config |
---|
| 125 | APPLICATION_STATE_EXTENSION = config.APPLICATION_STATE_EXTENSION |
---|
| 126 | APPLICATION_NAME = config.__appname__ |
---|
| 127 | SPLASH_SCREEN_PATH = config.SPLASH_SCREEN_PATH |
---|
| 128 | WELCOME_PANEL_ON = config.WELCOME_PANEL_ON |
---|
| 129 | SPLASH_SCREEN_WIDTH = config.SPLASH_SCREEN_WIDTH |
---|
| 130 | SPLASH_SCREEN_HEIGHT = config.SPLASH_SCREEN_HEIGHT |
---|
| 131 | SS_MAX_DISPLAY_TIME = config.SS_MAX_DISPLAY_TIME |
---|
| 132 | if not WELCOME_PANEL_ON: |
---|
| 133 | WELCOME_PANEL_SHOW = False |
---|
| 134 | else: |
---|
| 135 | WELCOME_PANEL_SHOW = True |
---|
| 136 | try: |
---|
| 137 | DATALOADER_SHOW = custom_config.DATALOADER_SHOW |
---|
| 138 | TOOLBAR_SHOW = custom_config.TOOLBAR_SHOW |
---|
| 139 | FIXED_PANEL = custom_config.FIXED_PANEL |
---|
| 140 | if WELCOME_PANEL_ON: |
---|
| 141 | WELCOME_PANEL_SHOW = custom_config.WELCOME_PANEL_SHOW |
---|
| 142 | PLOPANEL_WIDTH = custom_config.PLOPANEL_WIDTH |
---|
| 143 | DATAPANEL_WIDTH = custom_config.DATAPANEL_WIDTH |
---|
| 144 | GUIFRAME_WIDTH = custom_config.GUIFRAME_WIDTH |
---|
| 145 | GUIFRAME_HEIGHT = custom_config.GUIFRAME_HEIGHT |
---|
| 146 | CONTROL_WIDTH = custom_config.CONTROL_WIDTH |
---|
| 147 | CONTROL_HEIGHT = custom_config.CONTROL_HEIGHT |
---|
| 148 | DEFAULT_PERSPECTIVE = custom_config.DEFAULT_PERSPECTIVE |
---|
| 149 | CLEANUP_PLOT = custom_config.CLEANUP_PLOT |
---|
| 150 | # custom open_path |
---|
| 151 | open_folder = custom_config.DEFAULT_OPEN_FOLDER |
---|
| 152 | if open_folder != None and os.path.isdir(open_folder): |
---|
| 153 | DEFAULT_OPEN_FOLDER = os.path.abspath(open_folder) |
---|
| 154 | else: |
---|
| 155 | DEFAULT_OPEN_FOLDER = PATH_APP |
---|
| 156 | except: |
---|
| 157 | DATALOADER_SHOW = True |
---|
| 158 | TOOLBAR_SHOW = True |
---|
| 159 | FIXED_PANEL = True |
---|
| 160 | WELCOME_PANEL_SHOW = False |
---|
| 161 | PLOPANEL_WIDTH = config.PLOPANEL_WIDTH |
---|
| 162 | DATAPANEL_WIDTH = config.DATAPANEL_WIDTH |
---|
| 163 | GUIFRAME_WIDTH = config.GUIFRAME_WIDTH |
---|
| 164 | GUIFRAME_HEIGHT = config.GUIFRAME_HEIGHT |
---|
| 165 | CONTROL_WIDTH = -1 |
---|
| 166 | CONTROL_HEIGHT = -1 |
---|
| 167 | DEFAULT_PERSPECTIVE = None |
---|
| 168 | CLEANUP_PLOT = False |
---|
| 169 | DEFAULT_OPEN_FOLDER = PATH_APP |
---|
| 170 | |
---|
| 171 | DEFAULT_STYLE = config.DEFAULT_STYLE |
---|
| 172 | |
---|
| 173 | PLUGIN_STATE_EXTENSIONS = config.PLUGIN_STATE_EXTENSIONS |
---|
| 174 | OPEN_SAVE_MENU = config.OPEN_SAVE_PROJECT_MENU |
---|
| 175 | VIEW_MENU = config.VIEW_MENU |
---|
| 176 | EDIT_MENU = config.EDIT_MENU |
---|
| 177 | extension_list = [] |
---|
| 178 | if APPLICATION_STATE_EXTENSION is not None: |
---|
| 179 | extension_list.append(APPLICATION_STATE_EXTENSION) |
---|
| 180 | EXTENSIONS = PLUGIN_STATE_EXTENSIONS + extension_list |
---|
| 181 | try: |
---|
| 182 | PLUGINS_WLIST = '|'.join(config.PLUGINS_WLIST) |
---|
| 183 | except: |
---|
| 184 | PLUGINS_WLIST = '' |
---|
| 185 | APPLICATION_WLIST = config.APPLICATION_WLIST |
---|
| 186 | IS_WIN = True |
---|
| 187 | IS_LINUX = False |
---|
| 188 | CLOSE_SHOW = True |
---|
| 189 | TIME_FACTOR = 2 |
---|
| 190 | NOT_SO_GRAPH_LIST = ["BoxSum"] |
---|
| 191 | |
---|
| 192 | class Communicate(QtCore.QObject): |
---|
| 193 | """ |
---|
| 194 | Utility class for tracking of the Qt signals |
---|
| 195 | """ |
---|
| 196 | # File got successfully read |
---|
| 197 | fileReadSignal = QtCore.pyqtSignal(list) |
---|
| 198 | |
---|
| 199 | # Open File returns "list" of paths |
---|
| 200 | fileDataReceivedSignal = QtCore.pyqtSignal(dict) |
---|
| 201 | |
---|
| 202 | # Update Main window status bar with "str" |
---|
[f82ab8c] | 203 | # Old "StatusEvent" |
---|
[f721030] | 204 | statusBarUpdateSignal = QtCore.pyqtSignal(str) |
---|
| 205 | |
---|
| 206 | # Send data to the current perspective |
---|
| 207 | updatePerspectiveWithDataSignal = QtCore.pyqtSignal(list) |
---|
[5032ea68] | 208 | |
---|
| 209 | # New data in current perspective |
---|
[a281ab8] | 210 | updateModelFromPerspectiveSignal = QtCore.pyqtSignal(QtGui.QStandardItem) |
---|
| 211 | |
---|
[f82ab8c] | 212 | # New plot requested from the GUI manager |
---|
| 213 | # Old "NewPlotEvent" |
---|
| 214 | plotRequestedSignal = QtCore.pyqtSignal(str) |
---|
| 215 | |
---|
| 216 | |
---|
[a281ab8] | 217 | def updateModelItem(item, update_data, name=""): |
---|
| 218 | """ |
---|
[1042dba] | 219 | Adds a checkboxed row named "name" to QStandardItem |
---|
| 220 | Adds QVariant 'update_data' to that row. |
---|
[a281ab8] | 221 | """ |
---|
| 222 | assert type(item) == QtGui.QStandardItem |
---|
| 223 | assert type(update_data) == QtCore.QVariant |
---|
| 224 | |
---|
| 225 | checkbox_item = QtGui.QStandardItem(True) |
---|
| 226 | checkbox_item.setCheckable(True) |
---|
| 227 | checkbox_item.setCheckState(QtCore.Qt.Checked) |
---|
| 228 | checkbox_item.setText(name) |
---|
| 229 | |
---|
| 230 | # Add "Info" item |
---|
[1042dba] | 231 | py_update_data = update_data.toPyObject() |
---|
| 232 | if type(py_update_data) == (Data1D or Data2D): |
---|
| 233 | # If Data1/2D added - extract Info from it |
---|
| 234 | info_item = infoFromData(py_update_data) |
---|
| 235 | else: |
---|
| 236 | # otherwise just add a naked item |
---|
[481ff26] | 237 | info_item = QtGui.QStandardItem("Info") |
---|
[a281ab8] | 238 | |
---|
| 239 | # Add the actual Data1D/Data2D object |
---|
| 240 | object_item = QtGui.QStandardItem() |
---|
| 241 | object_item.setData(update_data) |
---|
| 242 | |
---|
[1042dba] | 243 | # Set the data object as the first child |
---|
[a281ab8] | 244 | checkbox_item.setChild(0, object_item) |
---|
| 245 | |
---|
[1042dba] | 246 | # Set info_item as the second child |
---|
[a281ab8] | 247 | checkbox_item.setChild(1, info_item) |
---|
| 248 | |
---|
| 249 | # Append the new row to the main item |
---|
| 250 | item.appendRow(checkbox_item) |
---|
[1042dba] | 251 | |
---|
| 252 | def plotsFromCheckedItems(model_item): |
---|
| 253 | """ |
---|
| 254 | Returns the list of plots for items in the model which are checked |
---|
| 255 | """ |
---|
| 256 | assert type(model_item) == QtGui.QStandardItemModel |
---|
| 257 | |
---|
| 258 | checkbox_item = QtGui.QStandardItem(True) |
---|
| 259 | plot_data = [] |
---|
| 260 | |
---|
| 261 | # Iterate over model looking for items with checkboxes |
---|
[9e426c1] | 262 | for index in range(model_item.rowCount()): |
---|
| 263 | item = model_item.item(index) |
---|
| 264 | if item.isCheckable() and item.checkState() == QtCore.Qt.Checked: |
---|
| 265 | # TODO: assure item type is correct (either data1/2D or Plotter) |
---|
| 266 | plot_data.append(item.child(0).data().toPyObject()) |
---|
| 267 | # Going 1 level deeper only |
---|
| 268 | for index_2 in range(item.rowCount()): |
---|
| 269 | item_2 = item.child(index_2) |
---|
| 270 | if item_2 and item_2.isCheckable() and item_2.checkState() == QtCore.Qt.Checked: |
---|
| 271 | # TODO: assure item type is correct (either data1/2D or Plotter) |
---|
| 272 | plot_data.append(item_2.child(0).data().toPyObject()) |
---|
[481ff26] | 273 | |
---|
[9e426c1] | 274 | return plot_data |
---|
| 275 | |
---|
| 276 | def infoFromData(data): |
---|
| 277 | """ |
---|
| 278 | Given Data1D/Data2D object, extract relevant Info elements |
---|
| 279 | and add them to a model item |
---|
| 280 | """ |
---|
| 281 | assert type(data) in [Data1D, Data2D] |
---|
| 282 | |
---|
[1042dba] | 283 | info_item = QtGui.QStandardItem("Info") |
---|
[9e426c1] | 284 | |
---|
[481ff26] | 285 | title_item = QtGui.QStandardItem("Title: " + data.title) |
---|
[1042dba] | 286 | info_item.appendRow(title_item) |
---|
[481ff26] | 287 | run_item = QtGui.QStandardItem("Run: " + str(data.run)) |
---|
[1042dba] | 288 | info_item.appendRow(run_item) |
---|
[481ff26] | 289 | type_item = QtGui.QStandardItem("Type: " + str(data.__class__.__name__)) |
---|
[1042dba] | 290 | info_item.appendRow(type_item) |
---|
| 291 | |
---|
| 292 | if data.path: |
---|
[481ff26] | 293 | path_item = QtGui.QStandardItem("Path: " + data.path) |
---|
[1042dba] | 294 | info_item.appendRow(path_item) |
---|
| 295 | |
---|
| 296 | if data.instrument: |
---|
[481ff26] | 297 | instr_item = QtGui.QStandardItem("Instrument: " + data.instrument) |
---|
[1042dba] | 298 | info_item.appendRow(instr_item) |
---|
| 299 | |
---|
| 300 | process_item = QtGui.QStandardItem("Process") |
---|
| 301 | if isinstance(data.process, list) and data.process: |
---|
| 302 | for process in data.process: |
---|
| 303 | process_date = process.date |
---|
| 304 | process_date_item = QtGui.QStandardItem("Date: " + process_date) |
---|
| 305 | process_item.appendRow(process_date_item) |
---|
| 306 | |
---|
| 307 | process_descr = process.description |
---|
| 308 | process_descr_item = QtGui.QStandardItem("Description: " + process_descr) |
---|
| 309 | process_item.appendRow(process_descr_item) |
---|
| 310 | |
---|
| 311 | process_name = process.name |
---|
| 312 | process_name_item = QtGui.QStandardItem("Name: " + process_name) |
---|
| 313 | process_item.appendRow(process_name_item) |
---|
| 314 | |
---|
| 315 | info_item.appendRow(process_item) |
---|
[9e426c1] | 316 | |
---|
| 317 | return info_item |
---|
| 318 | |
---|
[f82ab8c] | 319 | def openLink(url): |
---|
| 320 | """ |
---|
| 321 | Open a URL in an external browser. |
---|
| 322 | Check the URL first, though. |
---|
| 323 | """ |
---|
| 324 | parsed_url = urlparse.urlparse(url) |
---|
| 325 | if parsed_url.scheme: |
---|
| 326 | webbrowser.open(url) |
---|
| 327 | else: |
---|
| 328 | msg = "Attempt at opening an invalid URL" |
---|
| 329 | raise AttributeError, msg |
---|