Skip to content

Commit

Permalink
Copy font setting logic from QgsPythonConsoleBase to QgsCodeEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 5, 2020
1 parent 28245ef commit e2ff63d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
10 changes: 1 addition & 9 deletions python/console/console_base.py
Expand Up @@ -77,15 +77,7 @@ def setLexers(self):
self.lexer.setFoldComments(True)
self.lexer.setFoldQuotes(True)

font = QFontDatabase.systemFont(QFontDatabase.FixedFont)

# output and console
loadFont = self.settings.value("pythonConsole/fontfamilytext")
if loadFont:
font.setFamily(loadFont)
fontSize = self.settings.value("pythonConsole/fontsize", type=int)
if fontSize:
font.setPointSize(fontSize)
font = self.getMonospaceFont()

self.lexer.setDefaultFont(font)
self.lexer.setDefaultColor(self.color(QgsCodeEditor.ColorRole.Default))
Expand Down
26 changes: 21 additions & 5 deletions src/gui/qgscodeeditor.cpp
Expand Up @@ -430,13 +430,29 @@ bool QgsCodeEditor::isFixedPitch( const QFont &font )
QFont QgsCodeEditor::getMonospaceFont()
{
QFont font = QFontDatabase::systemFont( QFontDatabase::FixedFont );

QgsSettings settings;
if ( !settings.value( QStringLiteral( "codeEditor/fontfamily" ), QString(), QgsSettings::Gui ).toString().isEmpty() )
font.setFamily( settings.value( QStringLiteral( "codeEditor/fontfamily" ), QString(), QgsSettings::Gui ).toString() );

const int fontSize = settings.value( QStringLiteral( "codeEditor/fontsize" ), 0, QgsSettings::Gui ).toInt();

#ifdef Q_OS_MAC
// The font size gotten from getMonospaceFont() is too small on Mac
font.setPointSize( QLabel().font().pointSize() );
if ( fontSize > 0 )
font.setPointSize( fontSize );
else
{
// The font size gotten from getMonospaceFont() is too small on Mac
font.setPointSize( QLabel().font().pointSize() );
}
#else
QgsSettings settings;
int fontSize = settings.value( QStringLiteral( "qgis/stylesheet/fontPointSize" ), 10 ).toInt();
font.setPointSize( fontSize );
if ( fontSize > 0 )
font.setPointSize( fontSize );
else
{
int fontSize = settings.value( QStringLiteral( "qgis/stylesheet/fontPointSize" ), 10 ).toInt();
font.setPointSize( fontSize );
}
#endif
font.setBold( false );

Expand Down
22 changes: 21 additions & 1 deletion tests/src/python/test_qgscodeeditor.py
Expand Up @@ -12,12 +12,15 @@

import qgis # NOQA

import sys

from qgis.core import QgsSettings, QgsApplication
from qgis.gui import QgsCodeEditor

from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtGui import QColor
from qgis.PyQt.QtGui import QColor, QFont
from qgis.testing import start_app, unittest
from utilities import getTestFont

start_app()

Expand Down Expand Up @@ -61,6 +64,23 @@ def testColors(self):
QgsCodeEditor.setColor(QgsCodeEditor.ColorRole.Keyword, QColor('#cc11bb'))
self.assertEqual(QgsCodeEditor.color(QgsCodeEditor.ColorRole.Keyword).name(), '#cc11bb')

def testFontFamily(self):
f = QgsCodeEditor().getMonospaceFont()
self.assertTrue(f.styleHint() & QFont.Monospace)

QgsSettings().setValue('codeEditor/fontfamily', getTestFont().family(), QgsSettings.Gui)
f = QgsCodeEditor().getMonospaceFont()
self.assertEqual(f.family(), 'QGIS Vera Sans')

@unittest.skipIf(sys.platform == 'darwin', 'MacOS has different font logic')
def testFontSize(self):
f = QgsCodeEditor().getMonospaceFont()
self.assertEqual(f.pointSize(), 10)

QgsSettings().setValue('codeEditor/fontsize', 14, QgsSettings.Gui)
f = QgsCodeEditor().getMonospaceFont()
self.assertEqual(f.pointSize(), 14)


if __name__ == '__main__':
unittest.main()

0 comments on commit e2ff63d

Please sign in to comment.