Skip to content

Commit

Permalink
Basic DPI scale
Browse files Browse the repository at this point in the history
  • Loading branch information
mhtvsSFrpHdE committed Aug 31, 2022
1 parent d982f3a commit dd58187
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
35 changes: 29 additions & 6 deletions qpp/prefetch/Source/Interface/Dpi/dpi.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <boost/math/special_functions/round.hpp>
#include <cmath>
#include <QWidget>

#include "dpi.h"
#include "..\..\Setting\setting.h"
Expand All @@ -9,8 +10,8 @@ using boost::multiprecision::cpp_bin_float_single;
cpp_bin_float_single Dpi::zoomLevel;
const cpp_bin_float_single Dpi::percentage = 100;

cpp_bin_float_single Dpi::dpiMultiplyer;
cpp_bin_float_single Dpi::pointToPixelMultiplyer;
cpp_bin_float_single Dpi::dpiMultiplier;
cpp_bin_float_single Dpi::pointToPixelMultiplier;

const cpp_bin_float_single Dpi::assumeQtPointEachInch = 72;
const cpp_bin_float_single Dpi::assumeOsPointEachInch = 96;
Expand All @@ -32,8 +33,8 @@ void Dpi::init()
}

// Ratio
dpiMultiplyer = zoomLevel / percentage;
pointToPixelMultiplyer = assumeQtPointEachInch / assumeOsPointEachInch;
dpiMultiplier = zoomLevel / percentage;
pointToPixelMultiplier = assumeQtPointEachInch / assumeOsPointEachInch;

// Font size
auto getFontSize = Setting::getInt("Instance", "FontSize", Setting::setting);
Expand All @@ -53,10 +54,32 @@ int Dpi::ptToPx(int pt)
// Parse input to float
cpp_bin_float_single floatPt = pt;
// Convert point to pixel
cpp_bin_float_single floatResult = floatPt / pointToPixelMultiplyer;
cpp_bin_float_single floatResult = floatPt / pointToPixelMultiplier;
// DPI scale
floatResult = floatResult * dpiMultiplyer;
floatResult = floatResult * dpiMultiplier;

int result_floor = std::floor(floatResult.convert_to<float>());
return result_floor;
}

int Dpi::multiply(int number)
{
cpp_bin_float_single floatResult = number;

floatResult = floatResult * dpiMultiplier;

int result_floor = std::floor(floatResult.convert_to<float>());
return result_floor;
};

void Dpi::scale_qWidgetRect(QWidget *widget)
{
auto qRect = widget->geometry();

auto newX = multiply(qRect.x());
auto newY = multiply(qRect.y());
auto newWidth = multiply(qRect.width());
auto newHeight = multiply(qRect.height());

widget->setGeometry(newX, newY, newWidth, newHeight);
}
12 changes: 10 additions & 2 deletions qpp/prefetch/Source/Interface/Dpi/dpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ class Dpi
// https://accessibility.psu.edu/legibility/fontsize
static int ptToPx(int pt);

// Multiply number with dpiMultiplier
static int multiply(int number);

// Try scale object inherted from QWidget and QRect
static void scale_qWidgetRect(QWidget *widget);

// Default font pixel size, convert from point size in config
static int defaultFontSize_pixel;

Expand All @@ -25,10 +31,12 @@ class Dpi
static cpp_bin_float_single zoomLevel;
const static cpp_bin_float_single percentage;

static cpp_bin_float_single dpiMultiplyer;
static cpp_bin_float_single pointToPixelMultiplyer;
static cpp_bin_float_single dpiMultiplier;
static cpp_bin_float_single pointToPixelMultiplier;

// It seems Qt use 1/72 as internal dpi calculation
const static cpp_bin_float_single assumeQtPointEachInch;
// But Windows use 96/1 as internal dpi calculation
const static cpp_bin_float_single assumeOsPointEachInch;

static int defaultFontSize_point;
Expand Down
7 changes: 7 additions & 0 deletions qpp/prefetch/Source/Interface/MainWindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ui_mainwindow.h"
#include "..\..\Global\global.h"
#include "..\..\Setting\setting.h"
#include "..\Dpi\dpi.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::MainWindow)
Expand All @@ -16,6 +17,12 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
startToTray = Setting::getBool("Instance", "StartToTray", Setting::setting);
minimizeToTray = Setting::getBool("Instance", "MinimizeToTray", Setting::setting);

// Zoom
Dpi::scale_qWidgetRect(this);
Dpi::scale_qWidgetRect(ui->stdOut_plainTextEdit);
Dpi::scale_qWidgetRect(ui->command_lineEdit);
Dpi::scale_qWidgetRect(ui->sendCommand_pushButton);

// Log size limit to prevent memory leak
auto getMaximumBlockCount = Setting::getInt("Instance", "MaximumBlockCount", Setting::setting);
if (getMaximumBlockCount.success)
Expand Down

0 comments on commit dd58187

Please sign in to comment.