From d65fa2bdef7ab59b1d4ebb0c39f76294490966ce Mon Sep 17 00:00:00 2001
From: thomahl <thomahl@stud.ntnu.no>
Date: Tue, 21 Apr 2020 15:33:28 +0200
Subject: [PATCH] =?UTF-8?q?#125=20WIP=20l=C3=B8sning=20til=20oppl=C3=B8sni?=
 =?UTF-8?q?ngstr=C3=B8bbel=20ved=20=C3=A5=20bruke=20px=20ikke=20pt=20i=20f?=
 =?UTF-8?q?ont?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Som indirekte vist her: https://stackoverflow.com/questions/42141354/convert-pixel-size-to-point-size-for-fonts-on-multiple-platforms/42141980 punktstørrelser på fonter resulterer i ulik pikselstørrelse fra skjerm til skjerm. Dette er hele poenget med punktstørrelser. Men til vårt bruk trenger vi at tekster inne i SOI har lik pikselstørrelse fra skjerm til skjerm, så denne endringen begynner å flytte fonter brukt i SOI fra punkt til piksel.

For å teste: Lag en enkel SOI og skriv ut til PDF. Endre navn på utskrevet PDF slik at denne ikke overskrives senere. Dermed endre skjermoppløsning, åpne programmet og lag den samme SOIen. Skriv ut denne. Ved å sammenligne de to PDFene kan du nå se at de er like. Om du gjør det samme på master vil de være helt ulike..
---
 soitool/inline_editable_soi_view.py           | 19 +++++++------
 soitool/modules/code_table_base.py            |  2 ++
 .../modules/module_authentication_board.py    |  4 +--
 soitool/modules/module_base.py                | 28 ++++++++++++++++---
 soitool/modules/module_freetext.py            |  3 +-
 soitool/modules/module_subtractorcodes.py     |  4 +--
 soitool/modules/module_table.py               |  2 ++
 7 files changed, 44 insertions(+), 18 deletions(-)

diff --git a/soitool/inline_editable_soi_view.py b/soitool/inline_editable_soi_view.py
index 5bde3a8..564e7f1 100644
--- a/soitool/inline_editable_soi_view.py
+++ b/soitool/inline_editable_soi_view.py
@@ -22,6 +22,7 @@ from PySide2.QtPrintSupport import QPrinter
 from soitool.soi import ModuleLargerThanBinError
 from soitool.dialog_wrappers import exec_warning_dialog
 from soitool.serialize_export_import_soi import generate_soi_filename
+from soitool.modules.module_base import qfont_with_pixel_size
 
 # How attachment pages should be numbered. The first page should be numbered
 # by the value of ATTACHMENT_NUMBERING_SCHEME[0], the second page
@@ -419,7 +420,7 @@ class InlineEditableSOIView(QScrollArea):
         label_title = QLabel(self.soi.title)
         label_title.move(x, y + self.soi.HEADER_HEIGHT)
         label_title.setStyleSheet("background-color: rgba(0,0,0,0%)")
-        label_title.setFont(QFont("Times New Roman", 35))
+        label_title.setFont(qfont_with_pixel_size("Times New Roman", 60))
         proxy = self.scene.addWidget(label_title)
         proxy.setRotation(-90)
 
@@ -427,7 +428,7 @@ class InlineEditableSOIView(QScrollArea):
         label_description = QLabel(self.soi.description)
         label_description.move(x + 57, y + self.soi.HEADER_HEIGHT)
         label_description.setStyleSheet("background-color: rgba(0,0,0,0%)")
-        label_description.setFont(QFont("Times New Roman", 15))
+        label_description.setFont(qfont_with_pixel_size("Times New Roman", 25))
         proxy = self.scene.addWidget(label_description)
         proxy.setRotation(-90)
 
@@ -435,7 +436,7 @@ class InlineEditableSOIView(QScrollArea):
         creation_date = soi_date_string_to_user_friendly_string(self.soi.date)
         label_creation_date = QLabel("Opprettet: {}".format(creation_date))
         label_creation_date.setStyleSheet("background-color: rgba(0,0,0,0%)")
-        label_creation_date.setFont(QFont("Times New Roman", 15))
+        label_creation_date.setFont(qfont_with_pixel_size("Times New Roman", 25))
         # Source: https://stackoverflow.com/a/8638114/3545896
         # CAUTION: does not work if font is set through stylesheet
         label_width = (
@@ -470,7 +471,7 @@ class InlineEditableSOIView(QScrollArea):
             "1 av N", f"{self.copy_current} av {self.copy_total}"
         )
         proxy.label.setStyleSheet("background-color: rgba(0,0,0,0%)")
-        proxy.label.setFont(QFont("Times New Roman", 40))
+        proxy.label.setFont(qfont_with_pixel_size("Times New Roman", 70))
 
         # Need to call this when label of proxy changes. See function docstring
         proxy.update_bounding_rect()
@@ -493,7 +494,7 @@ class InlineEditableSOIView(QScrollArea):
         label_copy_number_header.setStyleSheet(
             "background-color: rgba(0,0,0,0%)"
         )
-        label_copy_number_header.setFont(QFont("Times New Roman", 15))
+        label_copy_number_header.setFont(qfont_with_pixel_size("Times New Roman", 25))
         # Source: https://stackoverflow.com/a/8638114/3545896
         # CAUTION: does not work if font is set through stylesheet
         label_width = (
@@ -519,7 +520,7 @@ class InlineEditableSOIView(QScrollArea):
                 )
             )
         page_number.setStyleSheet("background-color: rgba(0,0,0,0%)")
-        page_number.setFont(QFont("Times New Roman", 15))
+        page_number.setFont(qfont_with_pixel_size("Times New Roman", 25))
         # Source: https://stackoverflow.com/a/8638114/3545896
         # CAUTION: does not work if font is set through stylesheet
         label_width = (
@@ -534,7 +535,7 @@ class InlineEditableSOIView(QScrollArea):
         classification.setStyleSheet(
             "background-color: rgba(0,0,0,0%); " "color: red"
         )
-        classification.setFont(QFont("Times New Roman", 35))
+        classification.setFont(qfont_with_pixel_size("Times New Roman", 60))
         # Source: https://stackoverflow.com/a/8638114/3545896
         # CAUTION: does not work if font is set through stylesheet
         label_width = (
@@ -552,7 +553,7 @@ class InlineEditableSOIView(QScrollArea):
         )
         label_valid_from = QLabel("Gyldig fra: {}".format(valid_from))
         label_valid_from.setStyleSheet("background-color: rgba(0,0,0,0%)")
-        label_valid_from.setFont(QFont("Times New Roman", 15))
+        label_valid_from.setFont(qfont_with_pixel_size("Times New Roman", 25))
         # Source: https://stackoverflow.com/a/8638114/3545896
         # CAUTION: does not work if font is set through stylesheet
         label_width = (
@@ -568,7 +569,7 @@ class InlineEditableSOIView(QScrollArea):
         valid_to = soi_date_string_to_user_friendly_string(self.soi.valid_to)
         label_valid_to = QLabel("Gyldig til: {}".format(valid_to))
         label_valid_to.setStyleSheet("background-color: rgba(0,0,0,0%)")
-        label_valid_to.setFont(QFont("Times New Roman", 15))
+        label_valid_to.setFont(qfont_with_pixel_size("Times New Roman", 25))
         # Source: https://stackoverflow.com/a/8638114/3545896
         # CAUTION: does not work if font is set through stylesheet
         label_width = (
diff --git a/soitool/modules/code_table_base.py b/soitool/modules/code_table_base.py
index 9cdff1e..478bc64 100644
--- a/soitool/modules/code_table_base.py
+++ b/soitool/modules/code_table_base.py
@@ -11,6 +11,7 @@ from soitool.modules.module_base import (
     get_table_size,
     resize_table,
     HEADLINE_FONT,
+    TABLE_CELL_DEFAULT_FONT,
 )
 
 AUTHENTICATIONBOARD_MODULE = "AuthenticationBoardModule"
@@ -37,6 +38,7 @@ class CodeTableBase(ModuleBase, QTableWidget, metaclass=Meta):
         self.verticalHeader().hide()
         self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
         self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
+        self.setFont(TABLE_CELL_DEFAULT_FONT)
 
         # Resize table when headline changes
         self.cellChanged.connect(
diff --git a/soitool/modules/module_authentication_board.py b/soitool/modules/module_authentication_board.py
index ca46c80..e1f5912 100644
--- a/soitool/modules/module_authentication_board.py
+++ b/soitool/modules/module_authentication_board.py
@@ -4,7 +4,7 @@ from secrets import choice
 from PySide2.QtWidgets import QTableWidgetItem
 from PySide2 import QtGui
 from PySide2.QtCore import Qt
-from soitool.modules.module_base import resize_table
+from soitool.modules.module_base import resize_table, qfont_with_pixel_size, TABLE_CELL_DEFAULT_FONT
 from soitool.modules.code_table_base import CodeTableBase
 
 # Characters for first column,
@@ -21,7 +21,7 @@ CODE_LENGTH = 25
 CODE_CHARACTER_TYPE = "ascii"
 
 # Font for authentication codes, should be a monospaced font
-CODE_FONT = QtGui.QFont("Consolas", 10, QtGui.QFont.SansSerif)
+CODE_FONT = qfont_with_pixel_size("Consolas", TABLE_CELL_DEFAULT_FONT.pixelSize(), QtGui.QFont.SansSerif)
 
 # Adds space between sets of characters, 0 => no spaces
 # If code is 123456 and interval is 2, code will be 12 34 56
diff --git a/soitool/modules/module_base.py b/soitool/modules/module_base.py
index 1d6f055..4a6e1e1 100644
--- a/soitool/modules/module_base.py
+++ b/soitool/modules/module_base.py
@@ -2,12 +2,32 @@
 from abc import ABC
 from PySide2 import QtGui
 
+def qfont_with_pixel_size(font_family, pixel_size, weight=None):
+    """Provide a QFont with given family and pixel size.
+
+    Created because QFont does not have a constructor with pixel size as a
+    parameter.
+
+    Parameters
+    ----------
+    font_family : str
+        Name of font family. Sent to https://doc.qt.io/qt-5/qfont.html#QFont-1
+    pixel_size : int
+        Pixel size. Sent to https://doc.qt.io/qt-5/qfont.html#setPixelSize
+    weight : QFont.Weight 
+        Weight of font. Sent to https://doc.qt.io/qt-5/qfont.html#QFont-1
+    """
+    if weight is not None:
+        font = QtGui.QFont(font_family, weight=weight)
+    else:
+        font = QtGui.QFont(font_family)
+    font.setPixelSize(pixel_size)
+    return font
+
 # Font for module headline
-HEADLINE_FONT = QtGui.QFont()
-HEADLINE_FONT.setFamily("Arial")
-HEADLINE_FONT.setPointSize(12)
-HEADLINE_FONT.setWeight(100)
+HEADLINE_FONT = qfont_with_pixel_size("Arial", 24, 100)
 
+TABLE_CELL_DEFAULT_FONT = qfont_with_pixel_size("Arial", 16)
 
 class ModuleBase(ABC):
     """Interface for SOI-modules."""
diff --git a/soitool/modules/module_freetext.py b/soitool/modules/module_freetext.py
index 95066bc..1d1f47d 100644
--- a/soitool/modules/module_freetext.py
+++ b/soitool/modules/module_freetext.py
@@ -13,7 +13,7 @@ from PySide2.QtWidgets import (
 )
 from PySide2.QtCore import QSize, Qt
 from PySide2.QtGui import QIcon, QFontMetricsF
-from soitool.modules.module_base import ModuleBase, HEADLINE_FONT
+from soitool.modules.module_base import ModuleBase, HEADLINE_FONT, qfont_with_pixel_size
 
 
 class TextEditWithSizeOfContent(QTextEdit):
@@ -113,6 +113,7 @@ class FreeTextModule(ModuleBase, QWidget, metaclass=Meta):
         self.line_edit_header = LineEditWithSizeOfContent()
         self.line_edit_header.setFont(HEADLINE_FONT)
         self.text_edit_body = TextEditWithSizeOfContent()
+        self.text_edit_body.setFont(qfont_with_pixel_size("Arial", HEADLINE_FONT.pixelSize()))
 
         # When the contents of these widgets change we need to manually trigger
         # adjust of size, even on self. Without adjust of size on self the
diff --git a/soitool/modules/module_subtractorcodes.py b/soitool/modules/module_subtractorcodes.py
index e56c0d4..9ded65c 100644
--- a/soitool/modules/module_subtractorcodes.py
+++ b/soitool/modules/module_subtractorcodes.py
@@ -3,7 +3,7 @@ import string
 from PySide2.QtWidgets import QTableWidgetItem
 from PySide2 import QtGui
 from PySide2.QtCore import Qt
-from soitool.modules.module_base import resize_table
+from soitool.modules.module_base import resize_table, qfont_with_pixel_size, TABLE_CELL_DEFAULT_FONT
 from soitool.modules.code_table_base import CodeTableBase
 
 # Characters for first and second column
@@ -21,7 +21,7 @@ SPACE_INTERVAL = 4
 SPACE_AMOUNT = 3
 
 # Font for subtractorcodes
-CODE_FONT = QtGui.QFont("Arial", 10, QtGui.QFont.SansSerif)
+CODE_FONT = qfont_with_pixel_size("Arial", TABLE_CELL_DEFAULT_FONT.pixelSize(), QtGui.QFont.SansSerif)
 
 HEADLINE_TEXT = "Subtraktorkoder"
 
diff --git a/soitool/modules/module_table.py b/soitool/modules/module_table.py
index 43bef7f..fff51d7 100644
--- a/soitool/modules/module_table.py
+++ b/soitool/modules/module_table.py
@@ -6,6 +6,7 @@ from soitool.modules.module_base import (
     resize_table,
     get_table_size,
     HEADLINE_FONT,
+    TABLE_CELL_DEFAULT_FONT,
 )
 
 START_ROWS = 2
@@ -42,6 +43,7 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
         self.verticalHeader().hide()
         self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
         self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
+        self.setFont(TABLE_CELL_DEFAULT_FONT)
 
         # If parameters are None, start as empty table.
         if size is None and data is None:
-- 
GitLab