annotate widgets/WidgetScale.h @ 1590:e660a00bc3b9

That didn't work well on a machine with a higher scaled-pixel ratio, try this
author Chris Cannam
date Fri, 03 Apr 2020 11:35:36 +0100
parents e7c9650e74a7
children
rev   line source
Chris@1174 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1174 2
Chris@1174 3 /*
Chris@1174 4 Sonic Visualiser
Chris@1174 5 An audio file viewer and annotation editor.
Chris@1174 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1174 7
Chris@1174 8 This program is free software; you can redistribute it and/or
Chris@1174 9 modify it under the terms of the GNU General Public License as
Chris@1174 10 published by the Free Software Foundation; either version 2 of the
Chris@1174 11 License, or (at your option) any later version. See the file
Chris@1174 12 COPYING included with this distribution for more information.
Chris@1174 13 */
Chris@1174 14
Chris@1174 15 #ifndef SV_WIDGET_SCALE_H
Chris@1174 16 #define SV_WIDGET_SCALE_H
Chris@1174 17
Chris@1174 18 #include <QFont>
Chris@1174 19 #include <QFontMetrics>
Chris@1174 20
Chris@1194 21 #include "base/Debug.h"
Chris@1194 22
Chris@1174 23 class WidgetScale
Chris@1174 24 {
Chris@1174 25 public:
Chris@1174 26 /**
Chris@1174 27 * Take a "design pixel" size and scale it for the actual
Chris@1174 28 * display. This is relevant to hi-dpi systems that do not do
Chris@1174 29 * pixel doubling (i.e. Windows and Linux rather than OS/X).
Chris@1174 30 */
Chris@1174 31 static int scalePixelSize(int pixels) {
Chris@1174 32
Chris@1174 33 static double ratio = 0.0;
Chris@1174 34 if (ratio == 0.0) {
Chris@1174 35 double baseEm;
Chris@1174 36 #ifdef Q_OS_MAC
Chris@1174 37 baseEm = 17.0;
Chris@1174 38 #else
Chris@1174 39 baseEm = 15.0;
Chris@1174 40 #endif
Chris@1174 41 double em = QFontMetrics(QFont()).height();
Chris@1174 42 ratio = em / baseEm;
Chris@1194 43 SVDEBUG << "WidgetScale::scalePixelSize: baseEm = " << baseEm
Chris@1194 44 << ", platform default font height = " << em
Chris@1194 45 << ", resulting scale factor = " << ratio << endl;
cannam@1308 46 if (ratio < 1.0) {
cannam@1308 47 SVDEBUG << "WidgetScale::scalePixelSize: rounding up to 1.0"
cannam@1308 48 << endl;
cannam@1308 49 ratio = 1.0;
cannam@1308 50 }
Chris@1174 51 }
Chris@1174 52
Chris@1174 53 int scaled = int(pixels * ratio + 0.5);
Chris@1174 54 if (pixels != 0 && scaled == 0) scaled = 1;
Chris@1174 55 return scaled;
Chris@1174 56 }
Chris@1174 57
Chris@1174 58 static QSize scaleQSize(QSize size) {
Chris@1174 59 return QSize(scalePixelSize(size.width()),
Chris@1174 60 scalePixelSize(size.height()));
Chris@1174 61 }
Chris@1174 62 };
Chris@1174 63
Chris@1174 64 #endif