forked from realmahdi/screen-form-factor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenformfactor.cpp
More file actions
146 lines (123 loc) · 3.25 KB
/
screenformfactor.cpp
File metadata and controls
146 lines (123 loc) · 3.25 KB
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
136
137
138
139
140
141
142
143
144
145
146
#include "screenformfactor.h"
screenformfactor::screenformfactor(QQuickItem *parent): QQuickItem(parent)
{
connect(this, &screenformfactor::parentChanged, this, &screenformfactor::parent_changed);
}
void screenformfactor::register_screenformfactor()
{
qmlRegisterType<screenformfactor>("formfactor", 1, 0, "FormFactorHandler");
}
bool screenformfactor::is_phone() const
{
return m_is_phone;
}
bool screenformfactor::is_phablet() const
{
return m_is_phablet;
}
bool screenformfactor::is_tablet() const
{
return m_is_tablet;
}
bool screenformfactor::is_desktop() const
{
return m_is_desktop;
}
bool screenformfactor::is_tv() const
{
return m_is_tv;
}
void screenformfactor::set_is_tv(bool is_tv)
{
if (is_tv == m_is_tv)
return;
m_is_tv = is_tv;
emit is_tv_changed();
}
void screenformfactor::force_detect()
{
if (!this->parentItem())
return;
this->m_is_tv = false;
this->m_is_desktop = false;
this->m_is_phablet = false;
this->m_is_tablet = false;
this->m_is_phone = false;
this->m_form_factor = e_form_factor::NONE;
set_form_factor(true);
}
void screenformfactor::set_is_desktop(bool is_desktop)
{
if (is_desktop == m_is_desktop)
return;
m_is_desktop = is_desktop;
emit is_desktop_changed();
}
void screenformfactor::set_is_tablet(bool is_tablet)
{
if (is_tablet == m_is_tablet)
return;
m_is_tablet = is_tablet;
emit is_tablet_changed();
}
void screenformfactor::set_is_phablet(bool is_phablet)
{
if (is_phablet == m_is_phablet)
return;
m_is_phablet = is_phablet;
emit is_phablet_changed();
}
void screenformfactor::set_is_phone(bool is_phone)
{
if (is_phone == m_is_phone)
return;
m_is_phone = is_phone;
emit is_phone_changed();
}
screenformfactor::e_form_factor screenformfactor::get_form_factor()
{
return m_form_factor;
}
void screenformfactor::parent_changed(QQuickItem *item)
{
if (!item)
return;
connect(item, SIGNAL(widthChanged()), this, SLOT(set_form_factor()));
}
void screenformfactor::set_form_factor(bool force)
{
if (!force && qFabs(old_width - parentItem()->width()) < 15)
return;
old_width = parentItem()->width();
qreal _width = parentItem()->width();
e_form_factor new_form_factor = e_form_factor::NONE;
if (_width >= 0 && _width < 599 )
{
new_form_factor = screenformfactor::PHONE;
}
else if (_width >= 600 && _width < 719)
{
new_form_factor = screenformfactor::PHABLET;
}
else if (_width >= 720 && _width < 959)
{
new_form_factor = screenformfactor::TABLET;
}
else if (_width >= 960 && _width < 1919)
{
new_form_factor = screenformfactor::DESKTOP;
}
else if (_width >= 1920)
{
new_form_factor = screenformfactor::TV;
}
if (new_form_factor == m_form_factor)
return;
m_form_factor = new_form_factor;
emit form_factor_changed(m_form_factor);
set_is_phone((m_form_factor == screenformfactor::PHONE));
set_is_phablet((m_form_factor == screenformfactor::PHABLET));
set_is_tablet((m_form_factor == screenformfactor::TABLET));
set_is_desktop((m_form_factor == screenformfactor::DESKTOP));
set_is_tv((m_form_factor == screenformfactor::TV));
}