QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
ScreenTools.qml
Go to the documentation of this file.
1pragma Singleton
2
3import QtQuick
4import QtQuick.Controls
5import QtQuick.Window
6
7import QGroundControl
8
9/*!
10 The ScreenTools Singleton provides information on QGC's standard font metrics. It also provides information on screen
11 size which can be used to adjust user interface for varying available screen real estate.
12
13 QGC has four standard font sizes: default, small, medium and large. The QGC controls use the default font for display and you should use this font
14 for most text within the system that is drawn using something other than a standard QGC control. The small font is smaller than the default font.
15 The medium and large fonts are larger than the default font.
16
17 Usage:
18
19 import QGroundControl.Controls
20 Rectangle {
21 anchors.fill: parent
22 anchors.margins: ScreenTools.defaultFontPixelWidth
23 ...
24 }
25*/
26Item {
27 id: _screenTools
28
29 //-- The point and pixel font size values are computed at runtime
30
31 property real defaultFontPointSize: 10
32 property real platformFontPointSize: 10
33
34 readonly property real smallFontPointRatio: 0.75
35 readonly property real mediumFontPointRatio: 1.25
36 readonly property real largeFontPointRatio: 1.5
37
38 /// You can use these properties to position ui elements in a screen resolution independent manner. Using fixed positioning values should not
39 /// be done. All positioning should be done using anchors or a ratio of the defaultFontPixelHeight and defaultFontPixelWidth values. This way
40 /// your ui elements will reposition themselves appropriately on varying screen sizes and resolutions.
41 property real defaultFontPixelHeight: 10
42 property real largeFontPixelHeight: defaultFontPixelHeight * largeFontPointRatio
43 property real mediumFontPixelHeight: defaultFontPixelHeight * mediumFontPointRatio
44 property real smallFontPixelHeight: defaultFontPixelHeight * smallFontPointRatio
45
46 /// You can use these properties to position ui elements in a screen resolution independent manner. Using fixed positioning values should not
47 /// be done. All positioning should be done using anchors or a ratio of the defaultFontPixelHeight and defaultFontPixelWidth values. This way
48 /// your ui elements will reposition themselves appropriately on varying screen sizes and resolutions.
49 property real defaultFontPixelWidth: 10
50 property real largeFontPixelWidth: defaultFontPixelWidth * largeFontPointRatio
51 property real mediumFontPixelWidth: defaultFontPixelWidth * mediumFontPointRatio
52 property real smallFontPixelWidth: defaultFontPixelWidth * smallFontPointRatio
53
54 /// QFontMetrics::descent for default font at default point size
55 property real defaultFontDescent: 0
56
57 /// The default amount of space in between controls in a dialog
58 property real defaultDialogControlSpacing: defaultFontPixelHeight / 2
59
60 property real smallFontPointSize: 10
61 property real mediumFontPointSize: 10
62 property real largeFontPointSize: 10
63
64 property real toolbarHeight: 0
65
66 property real realPixelDensity: {
67 //-- If a plugin defines it, just use what it tells us
68 if(QGroundControl.corePlugin.options.devicePixelDensity != 0) {
69 return QGroundControl.corePlugin.options.devicePixelDensity
70 }
71 //-- Android is rather unreliable
72 if(isAndroid) {
73 // Lets assume it's unlikely you have a tablet over 300mm wide
74 if((Screen.width / Screen.pixelDensity) > 300) {
75 return Screen.pixelDensity * 2
76 }
77 }
78 //-- Let's use what the system tells us
79 return Screen.pixelDensity
80 }
81
82 // These properties allow us to create simulated mobile sizing for a desktop build.
83 // This makes testing the UI for smaller mobile sizing much easier.
84 // The 731x411 size is the size of the Herelink screen which is our target lower bound
85 property real screenWidth: ScreenToolsController.fakeMobile ? 731 : Screen.width
86 property real screenHeight: ScreenToolsController.fakeMobile ? 411 : Screen.height
87
88 property bool isAndroid: ScreenToolsController.isAndroid
89 property bool isiOS: ScreenToolsController.isiOS
90 property bool isMobile: ScreenToolsController.isMobile
91 property bool isFakeMobile: ScreenToolsController.fakeMobile
92 property bool isWindows: ScreenToolsController.isWindows
93 property bool isDebug: ScreenToolsController.isDebug
94 property bool isMac: ScreenToolsController.isMacOS
95 property bool isLinux: ScreenToolsController.isLinux
96 property bool isTinyScreen: (Screen.width / realPixelDensity) < 120 // 120mm
97 property bool isShortScreen: ((Screen.height / realPixelDensity) < 120) || (ScreenToolsController.isMobile && ((Screen.height / Screen.width) < 0.6))
98 property bool isHugeScreen: (Screen.width / realPixelDensity) >= (23.5 * 25.4) // 27" monitor
99 property bool isSerialAvailable: ScreenToolsController.isSerialAvailable
100
101 readonly property real minTouchMillimeters: 5 ///< Minimum touch size in millimeters
102 property real minTouchPixels: 0 ///< Minimum touch size in pixels (calculatedd from minTouchMillimeters and realPixelDensity)
103
104 // The implicit heights/widths for our custom control set
105 property real implicitButtonWidth: Math.round(defaultFontPixelWidth * 5.0)
106 property real implicitButtonHeight: Math.round(defaultFontPixelHeight * 1.6)
107 property real implicitCheckBoxHeight: Math.round(defaultFontPixelHeight * 1.0)
108 property real implicitRadioButtonHeight: implicitCheckBoxHeight
109 property real implicitTextFieldWidth: defaultFontPixelWidth * 10
110 property real implicitTextFieldHeight: implicitButtonHeight
111 property real implicitComboBoxHeight: implicitButtonHeight
112 property real implicitComboBoxWidth: implicitButtonWidth
113 property real comboBoxPadding: defaultFontPixelWidth
114 property real implicitSliderHeight: defaultFontPixelHeight
115 property real defaultBorderRadius: defaultFontPixelWidth / 2
116
117 // It's not possible to centralize an even number of pixels, checkBoxIndicatorSize should be an odd number to allow centralization
118 property real checkBoxIndicatorSize: 2 * Math.floor(defaultFontPixelHeight / 2) + 1
119 property real radioButtonIndicatorSize: checkBoxIndicatorSize
120
121 readonly property string normalFontFamily: ScreenToolsController.normalFontFamily
122 readonly property string fixedFontFamily: ScreenToolsController.fixedFontFamily
123 /* This mostly works but for some reason, reflowWidths() in SetupView doesn't change size.
124 I've disabled (in release builds) until I figure out why. Changes require a restart for now.
125 */
126 Connections {
127 target: QGroundControl.settingsManager.appSettings.appFontPointSize
128 function onValueChanged() {
129 _setBasePointSize(QGroundControl.settingsManager.appSettings.appFontPointSize.value)
130 }
131 }
132
133 onRealPixelDensityChanged: {
134 _setBasePointSize(defaultFontPointSize)
135 }
136
137 function printScreenStats() {
138 console.log('ScreenTools: Screen.width: ' + Screen.width + ' Screen.height: ' + Screen.height + ' Screen.pixelDensity: ' + Screen.pixelDensity)
139 }
140
141 /// Returns the current x position of the mouse in global screen coordinates.
142 function mouseX() {
143 return ScreenToolsController.mouseX()
144 }
145
146 /// Returns the current y position of the mouse in global screen coordinates.
147 function mouseY() {
148 return ScreenToolsController.mouseY()
149 }
150
151 /// \private
152 function _setBasePointSize(pointSize) {
153 _textMeasure.font.pointSize = pointSize
154 defaultFontPointSize = pointSize
155 defaultFontPixelHeight = Math.round(_textMeasure.fontHeight/2.0)*2
156 defaultFontPixelWidth = Math.round(_textMeasure.fontWidth/2.0)*2
157 defaultFontDescent = ScreenToolsController.defaultFontDescent(defaultFontPointSize)
158 smallFontPointSize = defaultFontPointSize * _screenTools.smallFontPointRatio
159 mediumFontPointSize = defaultFontPointSize * _screenTools.mediumFontPointRatio
160 largeFontPointSize = defaultFontPointSize * _screenTools.largeFontPointRatio
161 minTouchPixels = Math.round(minTouchMillimeters * realPixelDensity)
162 if (minTouchPixels / Screen.height > 0.15) {
163 // If using physical sizing takes up too much of the vertical real estate fall back to font based sizing
164 minTouchPixels = defaultFontPixelHeight * 3
165 }
166 toolbarHeight = defaultFontPixelHeight * 3
167 toolbarHeight = toolbarHeight * QGroundControl.corePlugin.options.toolbarHeightMultiplier
168 }
169
170 Text {
171 id: _defaultFont
172 text: "X"
173 }
174
175 Text {
176 id: _textMeasure
177 text: "X"
178 font.family: normalFontFamily
179 property real fontWidth: contentWidth
180 property real fontHeight: contentHeight
181 Component.onCompleted: {
182 //-- First, compute platform, default size
183 if(ScreenToolsController.isMobile) {
184 //-- Check iOS really tiny screens (iPhone 4s/5/5s)
185 if(ScreenToolsController.isiOS) {
186 if(ScreenToolsController.isiOS && Screen.width < 570) {
187 // For iPhone 4s size we don't fit with additional tweaks to fit screen,
188 // we will just drop point size to make things fit. Correct size not yet determined.
189 platformFontPointSize = 12; // This will be lowered in a future pull
190 } else {
191 platformFontPointSize = 14;
192 }
193 } else if((Screen.width / realPixelDensity) < 120) {
194 platformFontPointSize = 11;
195 // Other Android
196 } else {
197 platformFontPointSize = 14;
198 }
199 } else {
200 platformFontPointSize = _defaultFont.font.pointSize;
201 }
202 //-- See if we are using a custom size
203 var _appFontPointSizeFact = QGroundControl.settingsManager.appSettings.appFontPointSize
204 var baseSize = _appFontPointSizeFact.value
205 //-- Sanity check
206 if(baseSize < _appFontPointSizeFact.min || baseSize > _appFontPointSizeFact.max) {
207 baseSize = platformFontPointSize;
208 _appFontPointSizeFact.value = baseSize
209 }
210 //-- Set size saved in settings
211 _screenTools._setBasePointSize(baseSize);
212 }
213 }
214}