Created
September 20, 2017 17:47
-
-
Save anonymous/d08e0e79b05237a765e23849bbe2a78d to your computer and use it in GitHub Desktop.
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
#include <QDebug> | |
#include <QScreen> | |
#include <QWindow> | |
#include <QWidget> | |
#include <QApplication> | |
#include <QPainter> | |
#include <qpa/qplatformscreen.h> | |
class QCoordinate | |
{ | |
public: | |
enum Type { | |
PixelType, | |
PointType | |
}; | |
QCoordinate() {} | |
explicit QCoordinate(int value, Type t) | |
{ | |
(t == PixelType ? m_pixels : m_points) = value; | |
} | |
int toPixels(QScreen* screen) | |
{ | |
return m_pixels + (m_points * pointsToPixelFactor(screen)); | |
} | |
int toPoints(QScreen* screen) | |
{ | |
return m_points + (m_pixels / pointsToPixelFactor(screen)); | |
} | |
private: | |
// Should take QPaintDevice instead? | |
qreal pointsToPixelFactor(QScreen* screen) | |
{ | |
qreal dpr = screen->devicePixelRatio(); | |
qreal pdpi = screen->handle()->logicalDpi().first; | |
return pdpi / (dpr * 96.0); | |
} | |
private: | |
int m_pixels = 0; | |
int m_points = 0; | |
friend QCoordinate operator+(QCoordinate const& lhs, QCoordinate const& rhs); | |
friend QCoordinate operator-(QCoordinate const& lhs, QCoordinate const& rhs); | |
}; | |
QCoordinate operator+(QCoordinate const& lhs, QCoordinate const& rhs) | |
{ | |
QCoordinate result; | |
result.m_pixels = lhs.m_pixels + rhs.m_pixels; | |
result.m_points = lhs.m_points + rhs.m_points; | |
return result; | |
} | |
QCoordinate operator-(QCoordinate const& lhs, QCoordinate const& rhs) | |
{ | |
QCoordinate result; | |
result.m_pixels = lhs.m_pixels - rhs.m_pixels; | |
result.m_points = lhs.m_points - rhs.m_points; | |
return result; | |
} | |
// TODO: Other operators | |
QCoordinate operator "" _pt(unsigned long long int value) | |
{ | |
return QCoordinate(value, QCoordinate::PointType); | |
} | |
QCoordinate operator "" _px(unsigned long long int value) | |
{ | |
return QCoordinate(value, QCoordinate::PixelType); | |
} | |
// Would this be the Qt 6 QPoint? | |
class QCoordinatePoint | |
{ | |
public: | |
explicit QCoordinatePoint(QCoordinate x, QCoordinate y) | |
: m_x(x), m_y(y) | |
{ | |
} | |
/// Compat only | |
[[deprecated]] explicit QCoordinatePoint(int x, int y) | |
: m_x(x, QCoordinate::PixelType), m_y(y, QCoordinate::PixelType) | |
{ | |
} | |
private: | |
QCoordinate m_x; | |
QCoordinate m_y; | |
}; | |
class QCoordinateSize | |
{ | |
public: | |
QCoordinateSize() {} | |
QCoordinateSize(QCoordinate const& coordHeight, QCoordinate const& coordWidth) | |
{ | |
m_coordHeight = coordHeight; | |
m_coordWidth = coordWidth; | |
} | |
QCoordinate height() const { return m_coordHeight; } | |
QCoordinate width() const { return m_coordWidth; } | |
private: | |
QCoordinate m_coordHeight; | |
QCoordinate m_coordWidth; | |
}; | |
// Inherit Qt 5 QWidget to make it work. | |
class Q6Widget : public QWidget | |
{ | |
public: | |
Q6Widget() | |
{ | |
} | |
void setFixedSize(QCoordinateSize const& size) | |
{ | |
// Remove 4px to account for 2px margin used in paint | |
m_fixedSize = QCoordinateSize(size.height() - 4_px, size.width() - 4_px); | |
} | |
void paintEvent(QPaintEvent* event) override | |
{ | |
QPainter painter(this); | |
auto screen = windowHandle()->screen(); | |
{ | |
QRect r; | |
r.setX(10); | |
r.setY(10); | |
r.setWidth(m_fixedSize.height().toPixels(screen)); | |
r.setHeight(m_fixedSize.width().toPixels(screen)); | |
painter.drawRect(r); | |
r.adjust(2, 2, -2, -2); | |
painter.drawRect(r); | |
} | |
} | |
private: | |
QCoordinateSize m_fixedSize; | |
}; | |
int main(int argc, char** argv) | |
{ | |
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); | |
QApplication app(argc, argv); | |
Q6Widget w; | |
w.setFixedSize(QCoordinateSize(120_pt, 260_pt)); | |
w.show(); | |
return app.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment