##// END OF EJS Templates
Centralize Qt font selection into a generic utility....
Fernando Perez -
Show More
@@ -1,3 +1,9 b''
1 """A base class for console-type widgets.
2 """
3 #-----------------------------------------------------------------------------
4 # Imports
5 #-----------------------------------------------------------------------------
6
1 # Standard library imports
7 # Standard library imports
2 from os.path import commonprefix
8 from os.path import commonprefix
3 import re
9 import re
@@ -9,11 +15,14 b' from PyQt4 import QtCore, QtGui'
9
15
10 # Local imports
16 # Local imports
11 from IPython.config.configurable import Configurable
17 from IPython.config.configurable import Configurable
12 from IPython.frontend.qt.util import MetaQObjectHasTraits
18 from IPython.frontend.qt.util import MetaQObjectHasTraits, get_font
13 from IPython.utils.traitlets import Bool, Enum, Int
19 from IPython.utils.traitlets import Bool, Enum, Int
14 from ansi_code_processor import QtAnsiCodeProcessor
20 from ansi_code_processor import QtAnsiCodeProcessor
15 from completion_widget import CompletionWidget
21 from completion_widget import CompletionWidget
16
22
23 #-----------------------------------------------------------------------------
24 # Classes
25 #-----------------------------------------------------------------------------
17
26
18 class ConsoleWidget(Configurable, QtGui.QWidget):
27 class ConsoleWidget(Configurable, QtGui.QWidget):
19 """ An abstract base class for console-type widgets. This class has
28 """ An abstract base class for console-type widgets. This class has
@@ -441,16 +450,19 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
441 def reset_font(self):
450 def reset_font(self):
442 """ Sets the font to the default fixed-width font for this platform.
451 """ Sets the font to the default fixed-width font for this platform.
443 """
452 """
444 font = QtGui.QFont()
445 if sys.platform == 'win32':
453 if sys.platform == 'win32':
446 # Prefer Consolas, but fall back to Courier if necessary.
454 # Consolas ships with Vista/Win7, fallback to Courier if needed
447 font.setFamily('Consolas')
455 family, fallback = 'Consolas', 'Courier'
448 if not font.exactMatch():
449 font.setFamily('Courier')
450 elif sys.platform == 'darwin':
456 elif sys.platform == 'darwin':
451 font.setFamily('Monaco')
457 # OSX always has Monaco, no need for a fallback
458 family, fallback = 'Monaco', None
452 else:
459 else:
453 font.setFamily('Monospace')
460 # Consolas isn't too common on linux, but anyone who has it
461 # installed it because they want it for their monospaced apps,
462 # since it's better than anything on linux by default.
463 family, fallback = 'Consolas', 'Monospace'
464
465 font = get_font(family, fallback)
454 font.setPointSize(QtGui.qApp.font().pointSize())
466 font.setPointSize(QtGui.qApp.font().pointSize())
455 font.setStyleHint(QtGui.QFont.TypeWriter)
467 font.setStyleHint(QtGui.QFont.TypeWriter)
456 self._set_font(font)
468 self._set_font(font)
@@ -2,7 +2,7 b''
2 """
2 """
3
3
4 # System library imports.
4 # System library imports.
5 from PyQt4 import QtCore
5 from PyQt4 import QtCore, QtGui
6
6
7 # IPython imports.
7 # IPython imports.
8 from IPython.utils.traitlets import HasTraits
8 from IPython.utils.traitlets import HasTraits
@@ -23,3 +23,30 b' class MetaQObjectHasTraits(MetaQObject, MetaHasTraits):'
23 QObject. See QtKernelManager for an example.
23 QObject. See QtKernelManager for an example.
24 """
24 """
25 pass
25 pass
26
27
28 def get_font(family, fallback=None):
29 """Return a font of the requested family, using fallback as alternative.
30
31 If a fallback is provided, it is used in case the requested family isn't
32 found. If no fallback is given, no alternative is chosen and Qt's internal
33 algorithms may automatically choose a fallback font.
34
35 Parameters
36 ----------
37 family : str
38 A font name.
39 fallback : str
40 A font name.
41
42 Returns
43 -------
44 font : QFont object
45 """
46 font = QtGui.QFont(family)
47 # Check whether we got what we wanted using QFontInfo, since exactMatch()
48 # is overly strict and returns false in too many cases.
49 font_info = QtGui.QFontInfo(font)
50 if fallback is not None and font_info.family() != family:
51 font = QtGui.QFont(fallback)
52 return font
General Comments 0
You need to be logged in to leave comments. Login now