From 50f1f534e9f55672b5a089bcf23c455de6537550 Mon Sep 17 00:00:00 2001
From: "alexander.mruchok" <alexander.mruchok@demlabs.net>
Date: Fri, 24 Jan 2020 10:13:10 +0200
Subject: [PATCH] [*] minor changes in convertPointsToPixels [*] added
 subproject auxiliary [*] changed screensize to one size in points [+] Added
 scaling module

---
 DapUiScreen.cpp                               |  8 +++---
 DapUiScreen.h                                 |  7 ++---
 .../AppStyleSheetHandler.cpp                  | 28 +++++++++++++++++--
 .../AppStyleSheetHandler.h                    |  1 +
 auxiliary/UiScaling.cpp                       | 23 +++++++++++++++
 auxiliary/UiScaling.h                         | 18 ++++++++++++
 Utils.cpp => auxiliary/Utils.cpp              |  0
 Utils.h => auxiliary/Utils.h                  |  0
 auxiliary/auxiliary.pri                       | 11 ++++++++
 controls/controls.pri                         |  2 --
 libdap-qt-ui.pri                              | 12 ++++----
 11 files changed, 93 insertions(+), 17 deletions(-)
 rename {controls => auxiliary}/AppStyleSheetHandler.cpp (88%)
 rename {controls => auxiliary}/AppStyleSheetHandler.h (96%)
 create mode 100644 auxiliary/UiScaling.cpp
 create mode 100644 auxiliary/UiScaling.h
 rename Utils.cpp => auxiliary/Utils.cpp (100%)
 rename Utils.h => auxiliary/Utils.h (100%)
 create mode 100644 auxiliary/auxiliary.pri

diff --git a/DapUiScreen.cpp b/DapUiScreen.cpp
index 9eb3bb3..5d328a5 100755
--- a/DapUiScreen.cpp
+++ b/DapUiScreen.cpp
@@ -19,7 +19,7 @@ DapUiScreen::DapUiScreen(QObject *parent, QStackedWidget * a_sw)
 
 // Set up default rotations, childs could change that by their own
     m_rotations.insert(Hor);
-#ifdef DAP_UI_TYPE_MOBILE
+#ifdef DAP_PLATFORM_MOBILE
     m_rotations.insert(Ver);
 #endif
 
@@ -31,13 +31,13 @@ DapUiScreen::~DapUiScreen()
 //        delete l_page;
 }
 
-QSize DapUiScreen::getScreenResolution(ScreenSize screenSize)
+QSize DapUiScreen::getWindowSizeInPoints(ScreenSize screenSize = DapUiScreen::ScreenSize::Medium)
 {
     switch (screenSize) {
     case DapUiScreen::ScreenSize::Small:
         return QSize(640, 400);
     case DapUiScreen::ScreenSize::Medium:
-        return QSize(1280, 800);
+        return QSize(1024, 768);
     case DapUiScreen::ScreenSize::Big:
         return QSize(1664, 1040);
     default:
@@ -83,7 +83,7 @@ DapUiScreen::ScreenSize DapUiScreen::getScreenSize()
 
         screenSize = Small;
         for (auto curSize: {Big, Medium}) {
-            QSize screenResolution (getScreenResolution(curSize) += WINDOW_BORDER_MAX_SIZE_PX);
+            QSize screenResolution (getWindowSizeInPoints(curSize) += WINDOW_BORDER_MAX_SIZE_PX);
 
             if (avHeight >= screenResolution.height()
                     && avWidth >= screenResolution.width()) {
diff --git a/DapUiScreen.h b/DapUiScreen.h
index 286a608..77094cd 100755
--- a/DapUiScreen.h
+++ b/DapUiScreen.h
@@ -24,8 +24,7 @@ public:
     enum ScreenRotation{Hor,Ver,HorInv,VerInv};
     enum ScreenSize{Small=0,Medium=10,Big=20};
 
-    static QSize getScreenResolution(ScreenSize screenSize);
-
+    static QSize getWindowSizeInPoints(ScreenSize screenSize);
 
     Q_ENUM(ScreenRotation)
     Q_ENUM(ScreenSize)
@@ -127,8 +126,8 @@ public:
             sw()->setCurrentIndex(0);
     }
 #endif
-    virtual void show();
-    virtual void update();
+    void show();
+    void update();
     void rotate(ScreenRotation a_rot);
     ScreenRotation rotation(){ return m_rotation; }
     static ScreenSize getScreenSize();
diff --git a/controls/AppStyleSheetHandler.cpp b/auxiliary/AppStyleSheetHandler.cpp
similarity index 88%
rename from controls/AppStyleSheetHandler.cpp
rename to auxiliary/AppStyleSheetHandler.cpp
index 736a4a7..9e62ce6 100644
--- a/controls/AppStyleSheetHandler.cpp
+++ b/auxiliary/AppStyleSheetHandler.cpp
@@ -4,7 +4,9 @@
 #include <QFileInfo>
 #include <QWidget>
 
-/// Read stylesheet from file
+#include "UiScaling.h"
+
+/// Read stylesheet from file and convert points to pixels
 /// @details Check for existing and available to open
 /// @param a_filePath file path
 /// @return The read stylesheet
@@ -26,7 +28,8 @@ QString AppStyleSheetHandler::readStyleSheetFromFile(const QString &a_filePath)
 
     QString styleSheet(styleSheetFile.readAll());
     styleSheetFile.close();
-    return styleSheet;
+
+    return convertPointsToPixels(styleSheet);
 }
 
 /// Get widget stylesheet by searching parameters
@@ -151,3 +154,24 @@ QApplication *AppStyleSheetHandler::appInstance()
 {
     return qobject_cast<QApplication*>(QCoreApplication::instance());
 }
+
+QString AppStyleSheetHandler::convertPointsToPixels(const QString a_stylesheet)
+{
+    const QRegExp regExp("([\\d.])+pt");
+    QStringList matches;
+    auto data = a_stylesheet;
+    auto pos = 0;
+
+    while ((pos = regExp.indexIn(data, pos)) != -1) {
+        matches << regExp.cap(0);
+        pos += regExp.matchedLength();
+    }
+
+    for (auto match : matches) {
+        float value = match.replace("pt", "").toDouble();
+        int pixelValue = UiScaling::pointsToPixels(value);
+        data.replace(match + "pt", QString::number(pixelValue) + "px");
+    }
+
+    return data;
+}
diff --git a/controls/AppStyleSheetHandler.h b/auxiliary/AppStyleSheetHandler.h
similarity index 96%
rename from controls/AppStyleSheetHandler.h
rename to auxiliary/AppStyleSheetHandler.h
index df6335c..96a3ea0 100644
--- a/controls/AppStyleSheetHandler.h
+++ b/auxiliary/AppStyleSheetHandler.h
@@ -59,6 +59,7 @@ public:
 
 private:
     static QApplication* appInstance();
+    static QString convertPointsToPixels(const QString a_stylesheet);
 
 };
 
diff --git a/auxiliary/UiScaling.cpp b/auxiliary/UiScaling.cpp
new file mode 100644
index 0000000..c61c9a6
--- /dev/null
+++ b/auxiliary/UiScaling.cpp
@@ -0,0 +1,23 @@
+#include "UiScaling.h"
+
+#include <QGuiApplication>
+#include <QScreen>
+
+
+inline int UiScaling::pointsToPixels(float a_points)
+{
+    static auto dpi(QGuiApplication::primaryScreen()->physicalDotsPerInch());
+    static auto pixelsInPoint(devicePointSize() * dpi);
+    return qRound(a_points * pixelsInPoint);
+}
+
+QSize UiScaling::pointsToPixels(const QSize &a_pointsSize)
+{
+    return QSize(UiScaling::pointsToPixels(a_pointsSize.width()),
+                 UiScaling::pointsToPixels(a_pointsSize.height()));
+}
+
+inline float UiScaling::devicePointSize()
+{
+    return 1/160.f;
+}
diff --git a/auxiliary/UiScaling.h b/auxiliary/UiScaling.h
new file mode 100644
index 0000000..e785164
--- /dev/null
+++ b/auxiliary/UiScaling.h
@@ -0,0 +1,18 @@
+#ifndef UISCALING_H
+#define UISCALING_H
+
+#include <QSize>
+
+class UiScaling
+{
+    UiScaling();             ///< private! Can't be created. Only for static methods
+
+public:
+
+    inline static int pointsToPixels(float a_points);
+    static QSize pointsToPixels(const QSize& a_pointsSize);
+
+    inline static float devicePointSize();
+};
+
+#endif // UISCALING_H
diff --git a/Utils.cpp b/auxiliary/Utils.cpp
similarity index 100%
rename from Utils.cpp
rename to auxiliary/Utils.cpp
diff --git a/Utils.h b/auxiliary/Utils.h
similarity index 100%
rename from Utils.h
rename to auxiliary/Utils.h
diff --git a/auxiliary/auxiliary.pri b/auxiliary/auxiliary.pri
new file mode 100644
index 0000000..6bdd7c8
--- /dev/null
+++ b/auxiliary/auxiliary.pri
@@ -0,0 +1,11 @@
+HEADERS += \
+    $$PWD/AppStyleSheetHandler.h \
+    $$PWD/UiScaling.h \
+    $$PWD/Utils.h
+
+SOURCES += \
+    $$PWD/AppStyleSheetHandler.cpp \
+    $$PWD/UiScaling.cpp \
+    $$PWD/Utils.cpp
+
+INCLUDEPATH += $$PWD
diff --git a/controls/controls.pri b/controls/controls.pri
index 32521cc..457e390 100644
--- a/controls/controls.pri
+++ b/controls/controls.pri
@@ -1,6 +1,5 @@
 SOURCES += \
     $$PWD/AnimationChangingWidget.cpp \
-    $$PWD/AppStyleSheetHandler.cpp \
     $$PWD/ComboBoxDelegate.cpp \
     $$PWD/CustomPlacementButton.cpp \
     $$PWD/ServersComboBox.cpp \
@@ -11,7 +10,6 @@ SOURCES += \
 
 HEADERS += \
     $$PWD/AnimationChangingWidget.h \
-    $$PWD/AppStyleSheetHandler.h \
     $$PWD/ComboBoxDelegate.h \
     $$PWD/CustomPlacementButton.h \
     $$PWD/ServersComboBox.h \
diff --git a/libdap-qt-ui.pri b/libdap-qt-ui.pri
index 33069bd..098220b 100644
--- a/libdap-qt-ui.pri
+++ b/libdap-qt-ui.pri
@@ -12,8 +12,7 @@ SOURCES += \
     $$PWD/DapUiScreen.cpp\
     $$PWD/schedule.cpp\
     $$PWD/schedules.cpp\
-    $$PWD/SideBar.cpp\
-    $$PWD/Utils.cpp
+    $$PWD/SideBar.cpp
 
 
 HEADERS  += \
@@ -22,11 +21,14 @@ HEADERS  += \
     $$PWD/DapUiDialog.h\
     $$PWD/DapUiMainWindow.h\
     $$PWD/DapUiScreen.h\
+    $$PWD/UiScaling.h \
     $$PWD/schedule.h\
     $$PWD/schedules.h\
-    $$PWD/SideBar.h\
-    $$PWD/Utils.h
+    $$PWD/SideBar.h
 
-include( controls/controls.pri)
+include(auxiliary/auxiliary.pri)
+include(controls/controls.pri)
 
 INCLUDEPATH += $$PWD
+
+DISTFILES +=
-- 
GitLab