Skip to content
Permalink
Browse files

Copy font setting logic from QgsPythonConsoleBase to QgsCodeEditor

  • Loading branch information
nyalldawson committed Oct 5, 2020
1 parent 28245ef commit e2ff63da48bb8ab4e47e8e39a1a75393f8b59fd5
Showing with 43 additions and 15 deletions.
  1. +1 −9 python/console/console_base.py
  2. +21 −5 src/gui/qgscodeeditor.cpp
  3. +21 −1 tests/src/python/test_qgscodeeditor.py
@@ -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))
@@ -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 );

@@ -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()

@@ -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.
You can’t perform that action at this time.