source: orange/Orange/OrangeCanvas/config.py @ 11702:fc917b3c9a15

Revision 11702:fc917b3c9a15, 7.9 KB checked in by Ales Erjavec <ales.erjavec@…>, 7 months ago (diff)

Added logging to a file.

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