changeset 1228:dc6457ac4d07

Add method to scale pen widths for hi-res screens
author Chris Cannam
date Thu, 26 Jan 2017 21:17:19 +0000
parents a8e126fe6a53
children 491dd600570c
files layer/PaintAssistant.cpp layer/PaintAssistant.h
diffstat 2 files changed, 55 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/layer/PaintAssistant.cpp	Thu Jan 26 21:16:46 2017 +0000
+++ b/layer/PaintAssistant.cpp	Thu Jan 26 21:17:19 2017 +0000
@@ -19,6 +19,7 @@
 
 #include "base/AudioLevel.h"
 #include "base/Strings.h"
+#include "base/Debug.h"
 
 #include <QPaintDevice>
 #include <QPainter>
@@ -262,3 +263,42 @@
         std::cerr << "ERROR: PaintAssistant::drawVisibleText: Boxed style not yet implemented!" << std::endl;
     }
 }
+
+double
+PaintAssistant::scalePenWidth(double width)
+{
+    static double ratio = 0.0;
+    if (ratio == 0.0) {
+        double baseEm;
+#ifdef Q_OS_MAC
+        baseEm = 17.0;
+#else
+        baseEm = 15.0;
+#endif
+        double em = QFontMetrics(QFont()).height();
+        ratio = em / baseEm;
+
+        SVDEBUG << "PaintAssistant::scalePenWidth: ratio is " << ratio
+                << " (em = " << em << ")" << endl;
+    }
+
+    if (ratio <= 1.0) {
+        // we only ever scale up in this method
+        return width;
+    }
+
+    if (width <= 0) {
+        // zero-width pen, produce a scaled one-pixel pen
+        return ratio;
+    }
+
+    return width * ratio;
+}
+
+QPen
+PaintAssistant::scalePen(QPen pen)
+{
+    return QPen(pen.color(), scalePenWidth(pen.width()));
+}
+
+
--- a/layer/PaintAssistant.h	Thu Jan 26 21:16:46 2017 +0000
+++ b/layer/PaintAssistant.h	Thu Jan 26 21:17:19 2017 +0000
@@ -13,10 +13,11 @@
     COPYING included with this distribution for more information.
 */
 
-#ifndef PAINT_ASSISTANT_H
-#define PAINT_ASSISTANT_H
+#ifndef SV_PAINT_ASSISTANT_H
+#define SV_PAINT_ASSISTANT_H
 
 #include <QRect>
+#include <QPen>
 #include <vector>
 
 class QPainter;
@@ -46,6 +47,18 @@
     static void drawVisibleText(const LayerGeometryProvider *,
                                 QPainter &p, int x, int y,
                                 QString text, TextStyle style);
+
+    /**
+     * Scale up pen width for a hi-dpi display without pixel doubling.
+     * Very similar to ViewManager::scalePixelSize, but a bit more
+     * conservative.
+     */
+    static double scalePenWidth(double width);
+
+    /**
+     * Apply scalePenWidth to a pen.
+     */
+    static QPen scalePen(QPen pen);
 };
 
 #endif