forked from spyder-ide/spyder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguiconfig.py
167 lines (130 loc) · 5.4 KB
/
guiconfig.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*- coding: utf-8 -*-
#
# Copyright © 2009-2012 The Spyder development team
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)
"""
Spyder GUI-related configuration management
(for non-GUI configuration, see spyderlib/baseconfig.py)
Important note regarding shortcuts:
For compatibility with QWERTZ keyboards, one must avoid using the following
shortcuts:
Ctrl + Alt + Q, W, F, G, Y, X, C, V, B, N
"""
from collections import namedtuple
from spyderlib.qt.QtGui import QFont, QFontDatabase, QShortcut, QKeySequence
from spyderlib.qt.QtCore import Qt
from spyderlib.config import CONF
from spyderlib.userconfig import NoDefault
from spyderlib.widgets.sourcecode import syntaxhighlighters as sh
from spyderlib.py3compat import to_text_string
# To save metadata about widget shortcuts (needed to build our
# preferences page)
Shortcut = namedtuple('Shortcut', 'data')
def font_is_installed(font):
"""Check if font is installed"""
return [fam for fam in QFontDatabase().families()
if to_text_string(fam)==font]
def get_family(families):
"""Return the first installed font family in family list"""
if not isinstance(families, list):
families = [ families ]
for family in families:
if font_is_installed(family):
return family
else:
print("Warning: None of the following fonts is installed: %r" % families)
return QFont().family()
FONT_CACHE = {}
def get_font(section, option=None):
"""Get console font properties depending on OS and user options"""
font = FONT_CACHE.get((section, option))
if font is None:
if option is None:
option = 'font'
else:
option += '/font'
families = CONF.get(section, option+"/family", None)
if families is None:
return QFont()
family = get_family(families)
weight = QFont.Normal
italic = CONF.get(section, option+'/italic', False)
if CONF.get(section, option+'/bold', False):
weight = QFont.Bold
size = CONF.get(section, option+'/size', 9)
font = QFont(family, size, weight)
font.setItalic(italic)
FONT_CACHE[(section, option)] = font
return font
def set_font(font, section, option=None):
"""Set font"""
if option is None:
option = 'font'
else:
option += '/font'
CONF.set(section, option+'/family', to_text_string(font.family()))
CONF.set(section, option+'/size', float(font.pointSize()))
CONF.set(section, option+'/italic', int(font.italic()))
CONF.set(section, option+'/bold', int(font.bold()))
FONT_CACHE[(section, option)] = font
def get_shortcut(context, name, default=NoDefault):
"""Get keyboard shortcut (key sequence string)"""
return CONF.get('shortcuts', '%s/%s' % (context, name), default=default)
def set_shortcut(context, name, keystr):
"""Set keyboard shortcut (key sequence string)"""
CONF.set('shortcuts', '%s/%s' % (context, name), keystr)
def new_shortcut(keystr, parent, action):
"""Define a new shortcut according to a keysequence string"""
sc = QShortcut(QKeySequence(keystr), parent, action)
sc.setContext(Qt.WidgetWithChildrenShortcut)
return sc
def create_shortcut(action, context, name, parent):
"""Creates a Shortcut namedtuple for a widget"""
keystr = get_shortcut(context, name)
qsc = new_shortcut(keystr, parent, action)
sc = Shortcut(data=(qsc, name, keystr))
return sc
def iter_shortcuts():
"""Iterate over keyboard shortcuts"""
for option in CONF.options('shortcuts'):
context, name = option.split("/", 1)
yield context, name, get_shortcut(context, name)
def remove_deprecated_shortcuts(data):
"""Remove deprecated shortcuts (shortcuts in CONF but not registered)"""
section = 'shortcuts'
options = [('%s/%s' % (context, name)).lower() for (context, name) in data]
for option, _ in CONF.items(section, raw=CONF.raw):
if option not in options:
CONF.remove_option(section, option)
if len(CONF.items(section, raw=CONF.raw)) == 0:
CONF.remove_section(section)
def reset_shortcuts():
"""Reset keyboard shortcuts to default values"""
CONF.reset_to_defaults(section='shortcuts')
def get_color_scheme(name):
"""Get syntax color scheme"""
color_scheme = {}
for key in sh.COLOR_SCHEME_KEYS:
color_scheme[key] = CONF.get("color_schemes", "%s/%s" % (name, key))
return color_scheme
def set_color_scheme(name, color_scheme, replace=True):
"""Set syntax color scheme"""
section = "color_schemes"
names = CONF.get("color_schemes", "names", [])
for key in sh.COLOR_SCHEME_KEYS:
option = "%s/%s" % (name, key)
value = CONF.get(section, option, default=None)
if value is None or replace or name not in names:
CONF.set(section, option, color_scheme[key])
names.append(to_text_string(name))
CONF.set(section, "names", sorted(list(set(names))))
def set_default_color_scheme(name, replace=True):
"""Reset color scheme to default values"""
assert name in sh.COLOR_SCHEME_NAMES
set_color_scheme(name, sh.get_color_scheme(name), replace=replace)
for _name in sh.COLOR_SCHEME_NAMES:
set_default_color_scheme(_name, replace=False)
CUSTOM_COLOR_SCHEME_NAME = "Custom"
set_color_scheme(CUSTOM_COLOR_SCHEME_NAME, sh.get_color_scheme("Spyder"),
replace=False)