14
14
Ctrl + Alt + Q, W, F, G, Y, X, C, V, B, N
17
from spyderlib.qt.QtGui import QFont, QFontDatabase
17
from collections import namedtuple
19
from spyderlib.qt.QtGui import QFont, QFontDatabase, QShortcut, QKeySequence
20
from spyderlib.qt.QtCore import Qt
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
28
# To save metadata about widget shortcuts (needed to build our
30
Shortcut = namedtuple('Shortcut', 'data')
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]
35
return [fam for fam in QFontDatabase().families()
36
if to_text_string(fam)==font]
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):
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()
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
62
75
def set_font(font, section, option=None):
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)
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)
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))
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)
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)
89
125
def reset_shortcuts():
90
126
"""Reset keyboard shortcuts to default values"""
91
127
CONF.remove_section('shortcuts')
93
130
def get_color_scheme(name):
94
131
"""Get syntax color scheme"""
97
134
color_scheme[key] = CONF.get("color_schemes", "%s/%s" % (name, key))
98
135
return color_scheme
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))))
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)
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"),