source: orange/Orange/OrangeCanvas/config.py @ 11250:3b499a088cfc

Revision 11250:3b499a088cfc, 5.4 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Added user settings/preferences dialog.

Line 
1"""
2Orange Canvas Configuration
3
4"""
5
6import os
7import logging
8import cPickle as pickle
9
10log = logging.getLogger(__name__)
11
12from PyQt4.QtGui import QDesktopServices
13from PyQt4.QtCore import QCoreApplication, QSettings
14
15from .utils.settings import Settings, config_slot
16
17
18def init():
19    """Initialize the QCoreApplication.organizationDomain, applicationName,
20    applicationVersion and the default settings format. Will only run once.
21
22    .. note:: This should not be run before QApplication has been initialized.
23              Otherwise it can brake Qt's plugin search paths.
24
25    """
26    # Set application name, version and org. domain
27    QCoreApplication.setOrganizationDomain("biolab.si")
28    QCoreApplication.setApplicationName("Orange Canvas")
29    QCoreApplication.setApplicationVersion("2.6")
30    QSettings.setDefaultFormat(QSettings.IniFormat)
31
32    # Make it a null op.
33    global init
34    init = lambda: None
35
36rc = {}
37
38
39spec = \
40    [("startup/show-splash-screen", bool, True,
41      "Show splash screen at startup"),
42
43     ("startup/show-welcome-screen", bool, True,
44      "Show Welcome screen at startup"),
45
46     ("stylesheet", unicode, "orange",
47      "QSS stylesheet to use"),
48
49     ("schemeinfo/show-at-new-scheme", bool, True,
50      "Show Scheme Properties when creating a new Scheme"),
51
52     ("mainwindow/scheme-margins-enabled", bool, True,
53      "Show margins around the scheme view"),
54
55     ("mainwindow/show-scheme-shadow", bool, True,
56      "Show shadow around the scheme view"),
57
58     ("mainwindow/toolbox-dock-exclusive", bool, False,
59      "Should the toolbox show only one expanded category at the time"),
60
61     ("mainwindow/toolbox-dock-floatable", bool, False,
62      "Is the canvas toolbox floatable (detachable from the main window)"),
63
64     ("mainwindow/toolbox-dock-movable", bool, True,
65      "Is the canvas toolbox movable (between left and right edge)"),
66
67     ("mainwindow/number-of-recent-schemes", int, 7,
68      "Number of recent schemes to keep in history"),
69
70     ("schemeedit/show-channel-names", bool, True,
71      "Show channel names"),
72
73     ("schemeedit/show-link-state", bool, True,
74      "Show link state hints."),
75
76     ("schemeedit/freeze-on-load", bool, False,
77      "Freeze signal propagation when loading a scheme."),
78
79     ("quickmenu/trigger-on-double-click", bool, True,
80      "Show quick menu on double click."),
81
82     ("quickmenu/trigger-on-left-click", bool, False,
83      "Show quick menu on left click."),
84
85     ("quickmenu/trigger-on-space-key", bool, True,
86      "Show quick menu on space key press."),
87
88     ("quickmenu/trigger-on-any-key", bool, False,
89      "Show quick menu on double click."),
90
91     ("logging/level", int, 1, "Logging level"),
92
93     ("logging/show-on-error", bool, True, "Show log window on error"),
94
95     ("logging/dockable", bool, True, "Allow log window to be docked"),
96
97     ("output/redirect-stderr", bool, True,
98      "Redirect and display standard error output"),
99
100     ("output/redirect-stdout", bool, True,
101      "Redirect and display standard output"),
102
103     ("output/stay-on-top", bool, True, ""),
104
105     ("output/show-on-error", bool, True, "Show output window on error"),
106
107     ("output/dockable", bool, True, "Allow output window to be docked"),
108
109     ("help/stay-on-top", bool, True, ""),
110
111     ("help/dockable", bool, True, "Allow help window to be docked"),
112
113     ("help/open-in-external-browser", bool, False,
114      "Open help in an external browser")
115     ]
116
117spec = [config_slot(*t) for t in spec]
118
119
120def settings():
121    init()
122    store = QSettings()
123    settings = Settings(defaults=spec, store=store)
124    return settings
125
126
127def data_dir():
128    """Return the application data directory. If the directory path
129    does not yet exists then create it.
130
131    """
132    init()
133    datadir = QDesktopServices.storageLocation(QDesktopServices.DataLocation)
134    datadir = unicode(datadir)
135    if not os.path.exists(datadir):
136        os.makedirs(datadir)
137    return datadir
138
139
140def cache_dir():
141    """Return the application cache 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 open_config():
154    global rc
155    app_dir = data_dir()
156    filename = os.path.join(app_dir, "canvas-rc.pck")
157    if os.path.exists(filename):
158        with open(os.path.join(app_dir, "canvas-rc.pck"), "rb") as f:
159            rc.update(pickle.load(f))
160
161
162def save_config():
163    app_dir = data_dir()
164    with open(os.path.join(app_dir, "canvas-rc.pck"), "wb") as f:
165        pickle.dump(rc, f)
166
167
168def recent_schemes():
169    """Return a list of recently accessed schemes.
170    """
171    app_dir = data_dir()
172    recent_filename = os.path.join(app_dir, "recent.pck")
173    recent = []
174    if os.path.isdir(app_dir) and os.path.isfile(recent_filename):
175        with open(recent_filename, "rb") as f:
176            recent = pickle.load(f)
177
178    # Filter out files not found on the file system
179    recent = [(title, path) for title, path in recent \
180              if os.path.exists(path)]
181    return recent
182
183
184def save_recent_scheme_list(scheme_list):
185    """Save the list of recently accessed schemes
186    """
187    app_dir = data_dir()
188    recent_filename = os.path.join(app_dir, "recent.pck")
189
190    if os.path.isdir(app_dir):
191        with open(recent_filename, "wb") as f:
192            pickle.dump(scheme_list, f)
Note: See TracBrowser for help on using the repository browser.