WidgetScale.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 #ifndef SV_WIDGET_SCALE_H
16 #define SV_WIDGET_SCALE_H
17 
18 #include <QFont>
19 #include <QFontMetrics>
20 
21 #include "base/Debug.h"
22 
24 {
25 public:
31  static int scalePixelSize(int pixels) {
32 
33  static double ratio = 0.0;
34  if (ratio == 0.0) {
35  double baseEm;
36 #ifdef Q_OS_MAC
37  baseEm = 17.0;
38 #else
39  baseEm = 15.0;
40 #endif
41  double em = QFontMetrics(QFont()).height();
42  ratio = em / baseEm;
43  SVDEBUG << "WidgetScale::scalePixelSize: baseEm = " << baseEm
44  << ", platform default font height = " << em
45  << ", resulting scale factor = " << ratio << endl;
46  if (ratio < 1.0) {
47  SVDEBUG << "WidgetScale::scalePixelSize: rounding up to 1.0"
48  << endl;
49  ratio = 1.0;
50  }
51  }
52 
53  int scaled = int(pixels * ratio + 0.5);
54  if (pixels != 0 && scaled == 0) scaled = 1;
55  return scaled;
56  }
57 
58  static QSize scaleQSize(QSize size) {
59  return QSize(scalePixelSize(size.width()),
60  scalePixelSize(size.height()));
61  }
62 };
63 
64 #endif
static QSize scaleQSize(QSize size)
Definition: WidgetScale.h:58
static int scalePixelSize(int pixels)
Take a "design pixel" size and scale it for the actual display.
Definition: WidgetScale.h:31