Skip to content

Commit

Permalink
qt_dotgraph: setting a device dependend font size in pixel instead of…
Browse files Browse the repository at this point in the history
… points results in correctly sized text in the graph
  • Loading branch information
DorianScholz authored and dirk-thomas committed Jan 23, 2017
1 parent e0b0d0d commit bc1163b
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 additions & 21 deletions qt_dotgraph/src/qt_dotgraph/graph_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# POSSIBILITY OF SUCH DAMAGE.

from python_qt_binding.QtGui import QColor, QFont
from python_qt_binding.QtWidgets import QGraphicsItemGroup, QGraphicsSimpleTextItem
from python_qt_binding.QtWidgets import QGraphicsItemGroup


class GraphItem(QGraphicsItemGroup):
Expand All @@ -41,27 +41,10 @@ class GraphItem(QGraphicsItemGroup):
_COLOR_RED = QColor(255, 0, 0)
_COLOR_TEAL = QColor(0, 170, 170)

_FONT_SIZE = 12.0
_FONT_SIZE_IS_ADJUSTED = False
_FONT_SIZE_REFERENCE_TEXT = ''.join(map(chr, range(0x20, 0x7f)))
_FONT_SIZE_REFERENCE_WIDTH = 650

_LABEL_FONT = QFont('sans', pointSize=_FONT_SIZE, weight=QFont.Light)
_LABEL_FONT = QFont('sans', weight=QFont.Light)

def __init__(self, highlight_level, parent=None):
super(GraphItem, self).__init__(parent)
self._highlight_level = highlight_level
if not GraphItem._FONT_SIZE_IS_ADJUSTED:
GraphItem._adjust_font_size()

@staticmethod
def _adjust_font_size():
GraphItem._FONT_SIZE_IS_ADJUSTED = True

text_item = QGraphicsSimpleTextItem(GraphItem._FONT_SIZE_REFERENCE_TEXT)
text_item.setFont(GraphItem._LABEL_FONT)
# reduce font size on high resolution displays until text_item in smaller than reference width
while text_item.sceneBoundingRect().width() > GraphItem._FONT_SIZE_REFERENCE_WIDTH:
GraphItem._FONT_SIZE *= 0.95
GraphItem._LABEL_FONT.setPointSizeF(GraphItem._FONT_SIZE)
text_item.setFont(GraphItem._LABEL_FONT)
# use device depended font size to produce
GraphItem._LABEL_FONT.setPixelSize(11)

0 comments on commit bc1163b

Please sign in to comment.