Skip to content

Commit

Permalink
[Sizing] Calc constants on lipstick values
Browse files Browse the repository at this point in the history
  • Loading branch information
neochapay committed Jul 8, 2024
1 parent eaf77f5 commit 4f0a2ec
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 89 deletions.
20 changes: 18 additions & 2 deletions examples/sizeview/glacier-sizeview.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017-2021 Chupligin Sergey <[email protected]>
* Copyright (C) 2017-2024 Chupligin Sergey <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
Expand Down Expand Up @@ -79,9 +79,14 @@ ApplicationWindow {

Column {
id: content
width: parent.width
width: parent.width - Theme.itemSpacingLarge*2
spacing: Theme.itemSpacingLarge

anchors{
left: parent.left
leftMargin: Theme.itemSpacingLarge
}

Label{
text: "DP Scale factor: " + size.dpScaleFactor;
}
Expand All @@ -91,6 +96,17 @@ ApplicationWindow {
text: "MM Scale factor: " + size.mmScaleFactor;
}

Rectangle{
color: Theme.accentColor
width: size.mm(10)
height: size.mm(10)

Label{
anchors.centerIn: parent
text: "1 cm"
}
}

Rectangle{
color: Theme.accentColor
width: Theme.itemWidthLarge
Expand Down
80 changes: 17 additions & 63 deletions src/core/sizing.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2021 Chupligin Sergey <[email protected]>
* Copyright (C) 2018-2024 Chupligin Sergey <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
Expand All @@ -25,50 +25,23 @@
#include <QScreen>
#include <math.h>

const qreal refHeight = 1440; // HD
const qreal refWidth = 720; // HD

Sizing::Sizing(QObject* parent)
: QObject(parent)
, m_physicalScreenSize(QSize(67, 136))
, m_screenSize(QSize(refWidth, refHeight))
, m_mmScaleFactor(10)
, m_dpScaleFactor(1)
, m_screenDPI(0)
, m_densitie(mdpi)
, m_forceDpiScaleFactor(false)
{
qreal physicalScreenHeight = qEnvironmentVariableIntValue("QT_QPA_EGLFS_PHYSICAL_HEIGHT");
qreal physicalScreenWidth = qEnvironmentVariableIntValue("QT_QPA_EGLFS_PHYSICAL_WIDTH");

MGConfItem* dpScaleFactorValue = new MGConfItem(QStringLiteral("/nemo/apps/libglacier/dpScaleFactor"));
if (dpScaleFactorValue->value("0").toFloat() != 0) {
m_forceDpiScaleFactor = true;
m_dpScaleFactor = dpScaleFactorValue->value().toFloat();
}
connect(dpScaleFactorValue, &MGConfItem::valueChanged, this, &Sizing::setDpScaleFactor);

QScreen* primaryScreen = QGuiApplication::primaryScreen();

/*If we dont have everoment of physical size try get it from screen*/
if (physicalScreenHeight == 0 || physicalScreenWidth == 0) {
physicalScreenHeight = primaryScreen->physicalSize().height();
physicalScreenWidth = primaryScreen->physicalSize().width();
//All sizes we get from LipstickSettings::exportScreenProperties()
MGConfItem* physicalDotsPerInchConf = new MGConfItem("/lipstick/screen/primary/physicalDotsPerInch");
connect(physicalDotsPerInchConf, &MGConfItem::valueChanged, this, &Sizing::recalcConstants);
if(physicalDotsPerInchConf->value().isNull()) {
QScreen *primaryScreen = QGuiApplication::primaryScreen();
physicalDotsPerInchConf->set(primaryScreen->physicalDotsPerInch());
physicalDotsPerInchConf->sync();
}

connect(primaryScreen, &QScreen::physicalDotsPerInchChanged, this, &Sizing::physicalDotsPerInchChanged);
connect(primaryScreen, &QScreen::physicalSizeChanged, this, &Sizing::physicalSizeChanged);

if (QGuiApplication::screens().count() == 0) {
qWarning() << "Qt not see any screens. Use defaults values";
}

physicalSizeChanged(QSizeF(physicalScreenHeight, physicalScreenWidth));

/*Calculate dpi bacuse wayland compositor return aways is 100*/
qreal calcDPI = primaryScreen->size().height() * 2.54 / physicalScreenHeight * 10;

physicalDotsPerInchChanged(calcDPI);
recalcConstants();
}

void Sizing::setDpScaleFactor()
Expand All @@ -78,7 +51,6 @@ void Sizing::setDpScaleFactor()

if (value != m_dpScaleFactor && value != 0) {
m_dpScaleFactor = value;
m_forceDpiScaleFactor = true;
emit dpScaleFactorChanged();
}
}
Expand All @@ -98,60 +70,42 @@ float Sizing::dp(float value)

float Sizing::mm(float value)
{
qWarning("Dont use size.mm(value)! Use value*size.mmScaleFactor");
return value * m_mmScaleFactor;
}

void Sizing::physicalDotsPerInchChanged(qreal dpi)
void Sizing::recalcConstants()
{
qreal dpi = MGConfItem("/lipstick/screen/primary/physicalDotsPerInch").value().toReal();

if (dpi == m_screenDPI) {
return;
}

qDebug() << "Screen DPI is: " << dpi;

Densitie densitie;
qreal dpScaleFactor;

if (dpi < 200) {
densitie = ldpi;
dpScaleFactor = 1;
} else if (dpi >= 200 && dpi < 300) {
densitie = hdpi;
dpScaleFactor = 1.5;
} else if (dpi >= 300 && dpi < 450) {
densitie = xhdpi;
dpScaleFactor = 2;
} else if (dpi >= 450 && dpi < 600) {
densitie = xxhdpi;
dpScaleFactor = 2.5;
} else {
densitie = xxxhdpi;
dpScaleFactor = 3;
}

m_screenDPI = dpi;
emit screenDPIChanged();

if (densitie != m_densitie) {
m_densitie = densitie;
emit densitieChanged();
}

if (dpScaleFactor != m_dpScaleFactor && !m_forceDpiScaleFactor) {
if (dpScaleFactor != m_dpScaleFactor) {
m_dpScaleFactor = dpScaleFactor;
emit dpScaleFactorChanged();
}
}

void Sizing::physicalSizeChanged(const QSizeF& size)
{
if (size != m_physicalScreenSize) {
m_physicalScreenSize = size;
float mmScaleFactor = m_screenSize.width() / size.width();

if (mmScaleFactor != m_mmScaleFactor) {
emit mmScaleFactorChanged();
}
float pixelsInMM = dpi/2.45/10;
if(pixelsInMM != m_mmScaleFactor) {
m_mmScaleFactor = pixelsInMM;
emit mmScaleFactorChanged();
}
}
26 changes: 2 additions & 24 deletions src/core/sizing.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2021 Chupligin Sergey <[email protected]>
* Copyright (C) 2018-2024 Chupligin Sergey <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
Expand Down Expand Up @@ -27,29 +27,15 @@
class Sizing : public QObject {
Q_OBJECT

enum Densitie {
ldpi,
mdpi,
hdpi,
xhdpi,
xxhdpi,
xxxhdpi
};
Q_ENUMS(Densities)

Q_PROPERTY(float mmScaleFactor READ mmScaleFactor WRITE setMmScaleFactor NOTIFY mmScaleFactorChanged)
Q_PROPERTY(float dpScaleFactor READ dpScaleFactor NOTIFY dpScaleFactorChanged)

Q_PROPERTY(Densitie densitie READ densitie NOTIFY densitieChanged)

public:
explicit Sizing(QObject* parent = 0);

float mmScaleFactor() { return m_mmScaleFactor; }
float dpScaleFactor() { return m_dpScaleFactor; }

Densitie densitie() { return m_densitie; };

void setMmScaleFactor(float value);
void setDpScaleFactor();

Expand All @@ -59,24 +45,16 @@ class Sizing : public QObject {
signals:
void mmScaleFactorChanged();
void dpScaleFactorChanged();
void densitieChanged();
void screenDPIChanged();

private slots:
void physicalDotsPerInchChanged(qreal dpi);
void physicalSizeChanged(const QSizeF& size);
void recalcConstants();

private:
QSizeF m_physicalScreenSize;
QSize m_screenSize;

float m_mmScaleFactor;
float m_dpScaleFactor;

qreal m_screenDPI;
Densitie m_densitie;

bool m_forceDpiScaleFactor;
};

#endif // SIZING_H

0 comments on commit 4f0a2ec

Please sign in to comment.