-
Notifications
You must be signed in to change notification settings - Fork 1
/
window.cpp
136 lines (111 loc) · 2.9 KB
/
window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <unordered_map>
#include <cmath>
#include <QtCore/QUrl>
#include <QtGui/QGuiApplication>
#include <QtGui/QScreen>
#include <QtQml/QQmlContext>
#include <QtQuick/QQuickItem>
#include "make_unique.hpp"
#include "window.hpp"
#define DEFAULT_WIDTH 480.0;
#define DEFAULT_HEIGHT 768.0;
using namespace std;
using namespace SMSDB;
class Window::Pimpl {
private:
Window *m_this;
public:
unordered_map<Window::DisplayRatio,
float, Window::Hasher<Window::DisplayRatio>> DisplayRatios {
{Window::DisplayRatio::Horz_16_10, 1.6f},
{Window::DisplayRatio::Horz_16_9, 1.777777777777778f},
{Window::DisplayRatio::Horz_4_3, 1.333333333333333f},
{Window::DisplayRatio::Vert_10_16, 0.625f},
{Window::DisplayRatio::Vert_9_16, 0.5625f},
{Window::DisplayRatio::Vert_3_4, 0.75f}
};
public:
Pimpl(Window *parent);
public:
void ReLocate();
};
Window::Window(QWindow *parent, Qt::WindowType flags) :
QtQuick2ApplicationViewer(parent),
m_pimpl(std::make_unique<Window::Pimpl>(this))
{
this->setFlags(flags);
this->rootContext()->setContextProperty("cppWindow", this);
}
Window::~Window()
{
}
double Window::getScreenWidth() const
{
#if defined(Q_OS_ANDROID) || defined(Q_OS_BLACKBERRY)
return QGuiApplication::primaryScreen()->size().width();
#else
return DEFAULT_WIDTH;
#endif
}
double Window::getScreenHeight() const
{
#if defined(Q_OS_ANDROID) || defined(Q_OS_BLACKBERRY)
return QGuiApplication::primaryScreen()->size().height();
#else
return DEFAULT_HEIGHT;
#endif
}
void Window::SetQML(const QString &url)
{
this->setSource(QUrl(url));
m_pimpl->ReLocate();
}
Window::DisplayRatio Window::GetDisplayRatio()
{
Window::DisplayRatio result;
float actualRatio = getScreenWidth() / getScreenHeight();
float minDiff = 10.0f;
for (auto const &kv : m_pimpl->DisplayRatios) {
float diff = abs(actualRatio - kv.second);
if (diff < minDiff) {
minDiff = diff;
result = kv.first;
}
}
return result;
}
void Window::Show()
{
this->showExpanded();
emit signal_Shown();
}
void Window::Close()
{
// QML has a bug on Android,
// so emit the signal first
emit signal_Closed();
this->close();
}
Window::Pimpl::Pimpl(Window *parent) :
m_this(parent)
{
}
void Window::Pimpl::ReLocate()
{
QSize screenSize = QGuiApplication::primaryScreen()->size();
m_this->setX((screenSize.width() - m_this->width()) / 2.0);
m_this->setY((screenSize.height() - m_this->height()) / 2.0);
}
void Window::keyPressEvent(QKeyEvent *e)
{
if (e->key() != Qt::Key_MediaPrevious
&& e->key() != Qt::Key_Backspace) {
QtQuick2ApplicationViewer::keyPressEvent(e);
} else {
this->Close();
}
}
void Window::resizeEvent(QResizeEvent *e) {
// ToDo: Handle screen-rotation here
QtQuick2ApplicationViewer::resizeEvent(e);
}