~ubuntu-branches/debian/experimental/spyder/experimental

« back to all changes in this revision

Viewing changes to spyderlib/guiconfig.py

  • Committer: Package Import Robot
  • Author(s): Picca Frédéric-Emmanuel
  • Date: 2014-05-29 09:06:26 UTC
  • mfrom: (1.1.21) (18.1.6 sid)
  • Revision ID: package-import@ubuntu.com-20140529090626-f58t82g0n5iewaxu
Tags: 2.3.0~rc+dfsg-1~experimental2
* Add spyder-common binary package for all the python2,3 common files
* debian/path
  - 0001-fix-documentation-installation.patch (deleted)
  + 0001-fix-spyderlib-path.patch (new)

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
        Ctrl + Alt + Q, W, F, G, Y, X, C, V, B, N
15
15
"""
16
16
 
17
 
from spyderlib.qt.QtGui import QFont, QFontDatabase
 
17
from collections import namedtuple
 
18
 
 
19
from spyderlib.qt.QtGui import QFont, QFontDatabase, QShortcut, QKeySequence
 
20
from spyderlib.qt.QtCore import Qt
18
21
 
19
22
from spyderlib.config import CONF
20
23
from spyderlib.userconfig import NoDefault
21
24
from spyderlib.widgets.sourcecode import syntaxhighlighters as sh
 
25
from spyderlib.py3compat import to_text_string
 
26
 
 
27
 
 
28
# To save metadata about widget shortcuts (needed to build our
 
29
# preferences page)
 
30
Shortcut = namedtuple('Shortcut', 'data')
22
31
 
23
32
 
24
33
def font_is_installed(font):
25
34
    """Check if font is installed"""
26
 
    return [fam for fam in QFontDatabase().families() if unicode(fam)==font]
27
 
    
 
35
    return [fam for fam in QFontDatabase().families()
 
36
            if to_text_string(fam)==font]
 
37
 
 
38
 
28
39
def get_family(families):
29
40
    """Return the first installed font family in family list"""
30
41
    if not isinstance(families, list):
33
44
        if font_is_installed(family):
34
45
            return family
35
46
    else:
36
 
        print "Warning: None of the following fonts is installed: %r" % families
 
47
        print("Warning: None of the following fonts is installed: %r" % families)
37
48
        return QFont().family()
38
 
    
 
49
 
 
50
 
39
51
FONT_CACHE = {}
40
52
def get_font(section, option=None):
41
53
    """Get console font properties depending on OS and user options"""
59
71
        FONT_CACHE[(section, option)] = font
60
72
    return font
61
73
 
 
74
 
62
75
def set_font(font, section, option=None):
63
76
    """Set font"""
64
77
    if option is None:
65
78
        option = 'font'
66
79
    else:
67
80
        option += '/font'
68
 
    CONF.set(section, option+'/family', unicode(font.family()))
 
81
    CONF.set(section, option+'/family', to_text_string(font.family()))
69
82
    CONF.set(section, option+'/size', float(font.pointSize()))
70
83
    CONF.set(section, option+'/italic', int(font.italic()))
71
84
    CONF.set(section, option+'/bold', int(font.bold()))
76
89
    """Get keyboard shortcut (key sequence string)"""
77
90
    return CONF.get('shortcuts', '%s/%s' % (context, name), default=default)
78
91
 
 
92
 
79
93
def set_shortcut(context, name, keystr):
80
94
    """Set keyboard shortcut (key sequence string)"""
81
95
    CONF.set('shortcuts', '%s/%s' % (context, name), keystr)
82
 
    
 
96
 
 
97
 
 
98
def create_shortcut(action, context, name, parent):
 
99
    """Creates a QShortcut for a widget and returns its associated data"""
 
100
    keystr = get_shortcut(context, name)
 
101
    qsc = QShortcut(QKeySequence(keystr), parent, action)
 
102
    qsc.setContext(Qt.WidgetWithChildrenShortcut)
 
103
    sc = Shortcut(data=(qsc, name, keystr))
 
104
    return sc
 
105
 
 
106
 
83
107
def iter_shortcuts():
84
108
    """Iterate over keyboard shortcuts"""
85
109
    for option in CONF.options('shortcuts'):
86
110
        context, name = option.split("/", 1)
87
111
        yield context, name, get_shortcut(context, name)
88
 
        
 
112
 
 
113
 
 
114
def remove_deprecated_shortcuts(data):
 
115
    """Remove deprecated shortcuts (shortcuts in CONF but not registered)"""
 
116
    section = 'shortcuts'
 
117
    options = [('%s/%s' % (context, name)).lower() for (context, name) in data]
 
118
    for option, _ in CONF.items(section, raw=CONF.raw):
 
119
        if option not in options:
 
120
            CONF.remove_option(section, option)
 
121
            if len(CONF.items(section, raw=CONF.raw)) == 0:
 
122
                CONF.remove_section(section)
 
123
 
 
124
 
89
125
def reset_shortcuts():
90
126
    """Reset keyboard shortcuts to default values"""
91
127
    CONF.remove_section('shortcuts')
92
128
 
 
129
 
93
130
def get_color_scheme(name):
94
131
    """Get syntax color scheme"""
95
132
    color_scheme = {}
97
134
        color_scheme[key] = CONF.get("color_schemes", "%s/%s" % (name, key))
98
135
    return color_scheme
99
136
 
 
137
 
100
138
def set_color_scheme(name, color_scheme, replace=True):
101
139
    """Set syntax color scheme"""
102
140
    section = "color_schemes"
106
144
        value = CONF.get(section, option, default=None)
107
145
        if value is None or replace or name not in names:
108
146
            CONF.set(section, option, color_scheme[key])
109
 
    names.append(unicode(name))
 
147
    names.append(to_text_string(name))
110
148
    CONF.set(section, "names", sorted(list(set(names))))
111
149
 
 
150
 
112
151
def set_default_color_scheme(name, replace=True):
113
152
    """Reset color scheme to default values"""
114
153
    assert name in sh.COLOR_SCHEME_NAMES
115
 
    set_color_scheme(name, sh.COLORS[name], replace=replace)
 
154
    set_color_scheme(name, sh.get_color_scheme(name), replace=replace)
 
155
 
116
156
 
117
157
for _name in sh.COLOR_SCHEME_NAMES:
118
158
    set_default_color_scheme(_name, replace=False)
119
159
CUSTOM_COLOR_SCHEME_NAME = "Custom"
120
 
set_color_scheme(CUSTOM_COLOR_SCHEME_NAME,
121
 
                 sh.COLORS["Spyder"], replace=False)
 
160
set_color_scheme(CUSTOM_COLOR_SCHEME_NAME, sh.get_color_scheme("Spyder"),
 
161
                 replace=False)