source: sasview/src/sas/qtgui/GuiUtils.py @ a281ab8

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 a281ab8 was a281ab8, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 8 years ago

Prototype DE↔perspective api based on QStandardItem.

  • Property mode set to 100755
File size: 7.6 KB
Line 
1"""
2    Global defaults and various utility functions usable by the general GUI
3"""
4
5import os
6import sys
7import time
8import imp
9import warnings
10import re
11warnings.simplefilter("ignore")
12import logging
13import traceback
14
15from PyQt4 import QtCore
16from 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
27from sas.sasgui.guiframe.dataFitting import Data1D
28
29
30def get_app_dir():
31    """
32        The application directory is the one where the default custom_config.py
33        file resides.
34
35        :returns: app_path - the path to the applicatin directory
36    """
37    # First, try the directory of the executable we are running
38    app_path = sys.path[0]
39    if os.path.isfile(app_path):
40        app_path = os.path.dirname(app_path)
41    if os.path.isfile(os.path.join(app_path, "custom_config.py")):
42        app_path = os.path.abspath(app_path)
43        logging.info("Using application path: %s", app_path)
44        return app_path
45
46    # Next, try the current working directory
47    if os.path.isfile(os.path.join(os.getcwd(), "custom_config.py")):
48        logging.info("Using application path: %s", os.getcwd())
49        return os.path.abspath(os.getcwd())
50
51    # Finally, try the directory of the sasview module
52    # TODO: gui_manager will have to know about sasview until we
53    # clean all these module variables and put them into a config class
54    # that can be passed by sasview.py.
55    logging.info(sys.executable)
56    logging.info(str(sys.argv))
57    from sas import sasview as sasview
58    app_path = os.path.dirname(sasview.__file__)
59    logging.info("Using application path: %s", app_path)
60    return app_path
61
62def get_user_directory():
63    """
64        Returns the user's home directory
65    """
66    userdir = os.path.join(os.path.expanduser("~"), ".sasview")
67    if not os.path.isdir(userdir):
68        os.makedirs(userdir)
69    return userdir
70
71def _find_local_config(file, path):
72    """
73        Find configuration file for the current application
74    """
75    config_module = None
76    fObj = None
77    try:
78        fObj, path_config, descr = imp.find_module(file, [path])
79        config_module = imp.load_module(file, fObj, path_config, descr)
80    except:
81        logging.error("Error loading %s/%s: %s" % (path, file, sys.exc_value))
82    finally:
83        if fObj is not None:
84            fObj.close()
85    logging.info("GuiManager loaded %s/%s" % (path, file))
86    return config_module
87
88# Get APP folder
89PATH_APP = get_app_dir()
90DATAPATH = PATH_APP
91
92# GUI always starts from the App folder
93#os.chdir(PATH_APP)
94# Read in the local config, which can either be with the main
95# application or in the installation directory
96config = _find_local_config('local_config', PATH_APP)
97if config is None:
98    config = _find_local_config('local_config', os.getcwd())
99    if config is None:
100        # Didn't find local config, load the default
101        import sas.sasgui.guiframe.config as config
102        logging.info("using default local_config")
103    else:
104        logging.info("found local_config in %s" % os.getcwd())
105else:
106    logging.info("found local_config in %s" % PATH_APP)
107
108
109from sas.sasgui.guiframe.customdir  import SetupCustom
110c_conf_dir = SetupCustom().setup_dir(PATH_APP)
111custom_config = _find_local_config('custom_config', c_conf_dir)
112if custom_config is None:
113    custom_config = _find_local_config('custom_config', os.getcwd())
114    if custom_config is None:
115        msgConfig = "Custom_config file was not imported"
116        logging.info(msgConfig)
117    else:
118        logging.info("using custom_config in %s" % os.getcwd())
119else:
120    logging.info("using custom_config from %s" % c_conf_dir)
121
122#read some constants from config
123APPLICATION_STATE_EXTENSION = config.APPLICATION_STATE_EXTENSION
124APPLICATION_NAME = config.__appname__
125SPLASH_SCREEN_PATH = config.SPLASH_SCREEN_PATH
126WELCOME_PANEL_ON = config.WELCOME_PANEL_ON
127SPLASH_SCREEN_WIDTH = config.SPLASH_SCREEN_WIDTH
128SPLASH_SCREEN_HEIGHT = config.SPLASH_SCREEN_HEIGHT
129SS_MAX_DISPLAY_TIME = config.SS_MAX_DISPLAY_TIME
130if not WELCOME_PANEL_ON:
131    WELCOME_PANEL_SHOW = False
132else:
133    WELCOME_PANEL_SHOW = True
134try:
135    DATALOADER_SHOW = custom_config.DATALOADER_SHOW
136    TOOLBAR_SHOW = custom_config.TOOLBAR_SHOW
137    FIXED_PANEL = custom_config.FIXED_PANEL
138    if WELCOME_PANEL_ON:
139        WELCOME_PANEL_SHOW = custom_config.WELCOME_PANEL_SHOW
140    PLOPANEL_WIDTH = custom_config.PLOPANEL_WIDTH
141    DATAPANEL_WIDTH = custom_config.DATAPANEL_WIDTH
142    GUIFRAME_WIDTH = custom_config.GUIFRAME_WIDTH
143    GUIFRAME_HEIGHT = custom_config.GUIFRAME_HEIGHT
144    CONTROL_WIDTH = custom_config.CONTROL_WIDTH
145    CONTROL_HEIGHT = custom_config.CONTROL_HEIGHT
146    DEFAULT_PERSPECTIVE = custom_config.DEFAULT_PERSPECTIVE
147    CLEANUP_PLOT = custom_config.CLEANUP_PLOT
148    # custom open_path
149    open_folder = custom_config.DEFAULT_OPEN_FOLDER
150    if open_folder != None and os.path.isdir(open_folder):
151        DEFAULT_OPEN_FOLDER = os.path.abspath(open_folder)
152    else:
153        DEFAULT_OPEN_FOLDER = PATH_APP
154except:
155    DATALOADER_SHOW = True
156    TOOLBAR_SHOW = True
157    FIXED_PANEL = True
158    WELCOME_PANEL_SHOW = False
159    PLOPANEL_WIDTH = config.PLOPANEL_WIDTH
160    DATAPANEL_WIDTH = config.DATAPANEL_WIDTH
161    GUIFRAME_WIDTH = config.GUIFRAME_WIDTH
162    GUIFRAME_HEIGHT = config.GUIFRAME_HEIGHT
163    CONTROL_WIDTH = -1
164    CONTROL_HEIGHT = -1
165    DEFAULT_PERSPECTIVE = None
166    CLEANUP_PLOT = False
167    DEFAULT_OPEN_FOLDER = PATH_APP
168
169DEFAULT_STYLE = config.DEFAULT_STYLE
170
171PLUGIN_STATE_EXTENSIONS = config.PLUGIN_STATE_EXTENSIONS
172OPEN_SAVE_MENU = config.OPEN_SAVE_PROJECT_MENU
173VIEW_MENU = config.VIEW_MENU
174EDIT_MENU = config.EDIT_MENU
175extension_list = []
176if APPLICATION_STATE_EXTENSION is not None:
177    extension_list.append(APPLICATION_STATE_EXTENSION)
178EXTENSIONS = PLUGIN_STATE_EXTENSIONS + extension_list
179try:
180    PLUGINS_WLIST = '|'.join(config.PLUGINS_WLIST)
181except:
182    PLUGINS_WLIST = ''
183APPLICATION_WLIST = config.APPLICATION_WLIST
184IS_WIN = True
185IS_LINUX = False
186CLOSE_SHOW = True
187TIME_FACTOR = 2
188NOT_SO_GRAPH_LIST = ["BoxSum"]
189
190class Communicate(QtCore.QObject):
191    """
192    Utility class for tracking of the Qt signals
193    """
194    # File got successfully read
195    fileReadSignal = QtCore.pyqtSignal(list)
196
197    # Open File returns "list" of paths
198    fileDataReceivedSignal = QtCore.pyqtSignal(dict)
199
200    # Update Main window status bar with "str"
201    statusBarUpdateSignal = QtCore.pyqtSignal(str)
202
203    # Send data to the current perspective
204    updatePerspectiveWithDataSignal = QtCore.pyqtSignal(list)
205
206    # New data in current perspective
207    updateModelFromPerspectiveSignal = QtCore.pyqtSignal(QtGui.QStandardItem)
208
209def updateModelItem(item, update_data, name=""):
210    """
211    Updates QStandardItem with a checkboxed row named 'name'
212    and containing QVariant 'update_data'
213    """
214    assert type(item) == QtGui.QStandardItem
215    assert type(update_data) == QtCore.QVariant
216
217    checkbox_item = QtGui.QStandardItem(True)
218    checkbox_item.setCheckable(True)
219    checkbox_item.setCheckState(QtCore.Qt.Checked)
220    checkbox_item.setText(name)
221
222    # Add "Info" item
223    info_item = QtGui.QStandardItem("Info")
224
225    # Add the actual Data1D/Data2D object
226    object_item = QtGui.QStandardItem()
227    object_item.setData(update_data)
228
229    checkbox_item.setChild(0, object_item)
230
231    # Set info_item as the only child
232    checkbox_item.setChild(1, info_item)
233
234    # Append the new row to the main item
235    item.appendRow(checkbox_item)
Note: See TracBrowser for help on using the repository browser.