annotate widgets/WidgetScale.h @ 1278:b6fc0970cd66 horizontal-scale

Tweaks to horizontal scale
author Chris Cannam
date Wed, 02 May 2018 14:25:44 +0100
parents be5e43e2180c
children e7c9650e74a7
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;
Chris@1174 46 }
Chris@1174 47
Chris@1174 48 int scaled = int(pixels * ratio + 0.5);
Chris@1174 49 if (pixels != 0 && scaled == 0) scaled = 1;
Chris@1174 50 return scaled;
Chris@1174 51 }
Chris@1174 52
Chris@1174 53 static QSize scaleQSize(QSize size) {
Chris@1174 54 return QSize(scalePixelSize(size.width()),
Chris@1174 55 scalePixelSize(size.height()));
Chris@1174 56 }
Chris@1174 57 };
Chris@1174 58
Chris@1174 59 #endif