diff view/View.cpp @ 1401:28075cc658c9

Scale pen for retina display on macOS as well as for hidpi display elsewhere
author Chris Cannam
date Thu, 15 Nov 2018 14:04:32 +0000
parents 694004228ab7
children b0eeec95ab5b
line wrap: on
line diff
--- a/view/View.cpp	Thu Nov 15 13:06:38 2018 +0000
+++ b/view/View.cpp	Thu Nov 15 14:04:32 2018 +0000
@@ -238,7 +238,7 @@
         }
     }
 
-    int y = ViewManager::scalePixelSize(15) + paint.fontMetrics().ascent();
+    int y = scaleSize(15) + paint.fontMetrics().ascent();
 
     for (std::map<int, Layer *>::const_iterator i = sortedLayers.begin();
          i != sortedLayers.end(); ++i) {
@@ -674,7 +674,7 @@
     QPushButton *cancel = new QPushButton(this);
     cancel->setIcon(IconLoader().load("cancel"));
     cancel->setFlat(true);
-    int scaled20 = ViewManager::scalePixelSize(20);
+    int scaled20 = scaleSize(20);
     cancel->setFixedSize(QSize(scaled20, scaled20));
     connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
     
@@ -1593,6 +1593,49 @@
     return n;
 }
 
+double
+View::scaleSize(double size) const
+{
+    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 << "View::scaleSize: ratio is " << ratio
+                << " (em = " << em << ")" << endl;
+
+        if (ratio < 1.0) {
+            SVDEBUG << "View::scaleSize: rounding ratio up to 1.0" << endl;
+            ratio = 1.0;
+        }
+    }
+
+    return size * ratio;
+}
+
+double
+View::scalePenWidth(double width) const 
+{
+    if (width <= 0) { // zero-width pen, produce a scaled one-pixel pen
+        width = 1;
+    }
+    double ratio = scaleSize(1.0);
+    return width * sqrt(ratio);
+}
+
+QPen
+View::scalePen(QPen pen) const 
+{
+    return QPen(pen.color(), scalePenWidth(pen.width()));
+}
+
 bool
 View::areLayerColoursSignificant() const
 {
@@ -1749,7 +1792,7 @@
                     timer->start();
                 }
 
-                int scaled20 = ViewManager::scalePixelSize(20);
+                int scaled20 = scaleSize(20);
 
                 cancel->move(0, ph - pb->height()/2 - scaled20/2);
                 cancel->show();
@@ -2170,7 +2213,7 @@
             (illuminateFrame >= 0 && i->contains(illuminateFrame));
 
         double h = height();
-        double penWidth = PaintAssistant::scalePenWidth(1.0);
+        double penWidth = scalePenWidth(1.0);
         double half = penWidth/2.0;
 
         paint.setPen(QPen(QColor(150, 150, 255), penWidth));
@@ -2187,7 +2230,7 @@
 
         if (illuminateThis) {
             paint.save();
-            penWidth = PaintAssistant::scalePenWidth(2.0);
+            penWidth = scalePenWidth(2.0);
             half = penWidth/2.0;
             paint.setPen(QPen(getForeground(), penWidth));
             if (closeToLeft) {