source: orange/Orange/OrangeCanvas/config.py @ 11532:68ae91b09f62

Revision 11532:68ae91b09f62, 7.5 KB checked in by Ales Erjavec <ales.erjavec@…>, 11 months ago (diff)

Set the application version from the package info and display it in the splash screen.

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