source: orange/Orange/OrangeCanvas/config.py @ 11510:dc1af66e0839

Revision 11510:dc1af66e0839, 6.6 KB checked in by Ales Erjavec <ales.erjavec@…>, 12 months ago (diff)

Changed default settings/initialization.

Line 
1"""
2Orange Canvas Configuration
3
4"""
5
6import os
7import logging
8import cPickle as pickle
9import itertools
10
11import pkg_resources
12
13from PyQt4.QtGui import QDesktopServices
14from PyQt4.QtCore import QCoreApplication
15
16from .utils.settings import Settings, config_slot
17
18# Import QSettings from qtcompat module (compatibility with PyQt < 4.8.3
19from .utils.qtcompat import QSettings
20
21log = logging.getLogger(__name__)
22
23
24def init():
25    """Initialize the QCoreApplication.organizationDomain, applicationName,
26    applicationVersion and the default settings format. Will only run once.
27
28    .. note:: This should not be run before QApplication has been initialized.
29              Otherwise it can break Qt's plugin search paths.
30
31    """
32    # Set application name, version and org. domain
33    QCoreApplication.setOrganizationDomain("biolab.si")
34    QCoreApplication.setApplicationName("Orange Canvas")
35    QCoreApplication.setApplicationVersion("2.6")
36    QSettings.setDefaultFormat(QSettings.IniFormat)
37
38    # Make it a null op.
39    global init
40    init = lambda: None
41
42rc = {}
43
44
45spec = \
46    [("startup/show-splash-screen", bool, True,
47      "Show splash screen at startup"),
48
49     ("startup/show-welcome-screen", bool, True,
50      "Show Welcome screen at startup"),
51
52     ("stylesheet", unicode, "orange",
53      "QSS stylesheet to use"),
54
55     ("schemeinfo/show-at-new-scheme", bool, True,
56      "Show Scheme Properties when creating a new Scheme"),
57
58     ("mainwindow/scheme-margins-enabled", bool, False,
59      "Show margins around the scheme view"),
60
61     ("mainwindow/show-scheme-shadow", bool, True,
62      "Show shadow around the scheme view"),
63
64     ("mainwindow/toolbox-dock-exclusive", bool, True,
65      "Should the toolbox show only one expanded category at the time"),
66
67     ("mainwindow/toolbox-dock-floatable", bool, False,
68      "Is the canvas toolbox floatable (detachable from the main window)"),
69
70     ("mainwindow/toolbox-dock-movable", bool, True,
71      "Is the canvas toolbox movable (between left and right edge)"),
72
73     ("mainwindow/toolbox-dock-use-popover-menu", bool, True,
74      "Use a popover menu to select a widget when clicking on a category "
75      "button"),
76
77     ("mainwindow/number-of-recent-schemes", int, 7,
78      "Number of recent schemes to keep in history"),
79
80     ("schemeedit/show-channel-names", bool, True,
81      "Show channel names"),
82
83     ("schemeedit/show-link-state", bool, True,
84      "Show link state hints."),
85
86     ("schemeedit/enable-node-animations", bool, True,
87      "Enable node animations."),
88
89     ("schemeedit/freeze-on-load", bool, False,
90      "Freeze signal propagation when loading a scheme."),
91
92     ("quickmenu/trigger-on-double-click", bool, True,
93      "Show quick menu on double click."),
94
95     ("quickmenu/trigger-on-right-click", bool, True,
96      "Show quick menu on right click."),
97
98     ("quickmenu/trigger-on-space-key", bool, True,
99      "Show quick menu on space key press."),
100
101     ("quickmenu/trigger-on-any-key", bool, False,
102      "Show quick menu on double click."),
103
104     ("logging/level", int, 1, "Logging level"),
105
106     ("logging/show-on-error", bool, True, "Show log window on error"),
107
108     ("logging/dockable", bool, True, "Allow log window to be docked"),
109
110     ("output/redirect-stderr", bool, True,
111      "Redirect and display standard error output"),
112
113     ("output/redirect-stdout", bool, True,
114      "Redirect and display standard output"),
115
116     ("output/stay-on-top", bool, True, ""),
117
118     ("output/show-on-error", bool, True, "Show output window on error"),
119
120     ("output/dockable", bool, True, "Allow output window to be docked"),
121
122     ("help/stay-on-top", bool, True, ""),
123
124     ("help/dockable", bool, True, "Allow help window to be docked"),
125
126     ("help/open-in-external-browser", bool, False,
127      "Open help in an external browser")
128     ]
129
130spec = [config_slot(*t) for t in spec]
131
132
133def settings():
134    init()
135    store = QSettings()
136    settings = Settings(defaults=spec, store=store)
137    return settings
138
139
140def data_dir():
141    """Return the application data directory. If the directory path
142    does not yet exists then create it.
143
144    """
145    init()
146    datadir = QDesktopServices.storageLocation(QDesktopServices.DataLocation)
147    datadir = unicode(datadir)
148    if not os.path.exists(datadir):
149        os.makedirs(datadir)
150    return datadir
151
152
153def cache_dir():
154    """Return the application cache directory. If the directory path
155    does not yet exists then create it.
156
157    """
158    init()
159    cachedir = QDesktopServices.storageLocation(QDesktopServices.CacheLocation)
160    cachedir = unicode(cachedir)
161    if not os.path.exists(cachedir):
162        os.makedirs(cachedir)
163    return cachedir
164
165
166def open_config():
167    global rc
168    app_dir = data_dir()
169    filename = os.path.join(app_dir, "canvas-rc.pck")
170    if os.path.exists(filename):
171        with open(os.path.join(app_dir, "canvas-rc.pck"), "rb") as f:
172            rc.update(pickle.load(f))
173
174
175def save_config():
176    app_dir = data_dir()
177    with open(os.path.join(app_dir, "canvas-rc.pck"), "wb") as f:
178        pickle.dump(rc, f)
179
180
181def recent_schemes():
182    """Return a list of recently accessed schemes.
183    """
184    app_dir = data_dir()
185    recent_filename = os.path.join(app_dir, "recent.pck")
186    recent = []
187    if os.path.isdir(app_dir) and os.path.isfile(recent_filename):
188        with open(recent_filename, "rb") as f:
189            recent = pickle.load(f)
190
191    # Filter out files not found on the file system
192    recent = [(title, path) for title, path in recent \
193              if os.path.exists(path)]
194    return recent
195
196
197def save_recent_scheme_list(scheme_list):
198    """Save the list of recently accessed schemes
199    """
200    app_dir = data_dir()
201    recent_filename = os.path.join(app_dir, "recent.pck")
202
203    if os.path.isdir(app_dir):
204        with open(recent_filename, "wb") as f:
205            pickle.dump(scheme_list, f)
206
207
208WIDGETS_ENTRY = "orange.widgets"
209
210
211# This could also be achieved by declaring the entry point in
212# Orange's setup.py, but that would not guaranty this entry point
213# is the first in a list.
214
215def default_entry_point():
216    """
217    Return a default orange.widgets entry point for loading
218    default Orange Widgets.
219
220    """
221    dist = pkg_resources.get_distribution("Orange")
222    ep = pkg_resources.EntryPoint("Orange Widgets", "Orange.OrangeWidgets",
223                                  dist=dist)
224    return ep
225
226
227def widgets_entry_points():
228    """
229    Return an `EntryPoint` iterator for all 'orange.widget' entry
230    points plus the default Orange Widgets.
231
232    """
233    ep_iter = pkg_resources.iter_entry_points(WIDGETS_ENTRY)
234    chain = [[default_entry_point()],
235             ep_iter
236             ]
237    return itertools.chain(*chain)
Note: See TracBrowser for help on using the repository browser.