1 | """ |
---|
2 | Global defaults and various utility functions usable by the general GUI |
---|
3 | """ |
---|
4 | |
---|
5 | import os |
---|
6 | import sys |
---|
7 | import time |
---|
8 | import imp |
---|
9 | import warnings |
---|
10 | import re |
---|
11 | warnings.simplefilter("ignore") |
---|
12 | import logging |
---|
13 | import traceback |
---|
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 |
---|
27 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
28 | |
---|
29 | |
---|
30 | def 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 | |
---|
62 | def 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 | |
---|
71 | def _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 |
---|
89 | PATH_APP = get_app_dir() |
---|
90 | DATAPATH = 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 |
---|
96 | config = _find_local_config('local_config', PATH_APP) |
---|
97 | if 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()) |
---|
105 | else: |
---|
106 | logging.info("found local_config in %s" % PATH_APP) |
---|
107 | |
---|
108 | |
---|
109 | from sas.sasgui.guiframe.customdir import SetupCustom |
---|
110 | c_conf_dir = SetupCustom().setup_dir(PATH_APP) |
---|
111 | custom_config = _find_local_config('custom_config', c_conf_dir) |
---|
112 | if 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()) |
---|
119 | else: |
---|
120 | logging.info("using custom_config from %s" % c_conf_dir) |
---|
121 | |
---|
122 | #read some constants from config |
---|
123 | APPLICATION_STATE_EXTENSION = config.APPLICATION_STATE_EXTENSION |
---|
124 | APPLICATION_NAME = config.__appname__ |
---|
125 | SPLASH_SCREEN_PATH = config.SPLASH_SCREEN_PATH |
---|
126 | WELCOME_PANEL_ON = config.WELCOME_PANEL_ON |
---|
127 | SPLASH_SCREEN_WIDTH = config.SPLASH_SCREEN_WIDTH |
---|
128 | SPLASH_SCREEN_HEIGHT = config.SPLASH_SCREEN_HEIGHT |
---|
129 | SS_MAX_DISPLAY_TIME = config.SS_MAX_DISPLAY_TIME |
---|
130 | if not WELCOME_PANEL_ON: |
---|
131 | WELCOME_PANEL_SHOW = False |
---|
132 | else: |
---|
133 | WELCOME_PANEL_SHOW = True |
---|
134 | try: |
---|
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 |
---|
154 | except: |
---|
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 | |
---|
169 | DEFAULT_STYLE = config.DEFAULT_STYLE |
---|
170 | |
---|
171 | PLUGIN_STATE_EXTENSIONS = config.PLUGIN_STATE_EXTENSIONS |
---|
172 | OPEN_SAVE_MENU = config.OPEN_SAVE_PROJECT_MENU |
---|
173 | VIEW_MENU = config.VIEW_MENU |
---|
174 | EDIT_MENU = config.EDIT_MENU |
---|
175 | extension_list = [] |
---|
176 | if APPLICATION_STATE_EXTENSION is not None: |
---|
177 | extension_list.append(APPLICATION_STATE_EXTENSION) |
---|
178 | EXTENSIONS = PLUGIN_STATE_EXTENSIONS + extension_list |
---|
179 | try: |
---|
180 | PLUGINS_WLIST = '|'.join(config.PLUGINS_WLIST) |
---|
181 | except: |
---|
182 | PLUGINS_WLIST = '' |
---|
183 | APPLICATION_WLIST = config.APPLICATION_WLIST |
---|
184 | IS_WIN = True |
---|
185 | IS_LINUX = False |
---|
186 | CLOSE_SHOW = True |
---|
187 | TIME_FACTOR = 2 |
---|
188 | NOT_SO_GRAPH_LIST = ["BoxSum"] |
---|
189 | |
---|
190 | class 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(Data1D) |
---|