forked from nemomobile/qtquickcontrols-nemo
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Sizing] Calc constants on lipstick values
- Loading branch information
Showing
3 changed files
with
37 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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; | ||
} | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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() | ||
|
@@ -78,7 +51,6 @@ void Sizing::setDpScaleFactor() | |
|
||
if (value != m_dpScaleFactor && value != 0) { | ||
m_dpScaleFactor = value; | ||
m_forceDpiScaleFactor = true; | ||
emit dpScaleFactorChanged(); | ||
} | ||
} | ||
|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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(); | ||
|
||
|
@@ -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 |