ScreenTools.qml 8.49 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2
pragma Singleton

3 4
import QtQuick 2.3
import QtQuick.Controls 1.2
dogmaphobic's avatar
dogmaphobic committed
5
import QtQuick.Window 2.2
Don Gagne's avatar
Don Gagne committed
6

7
import QGroundControl                       1.0
Don Gagne's avatar
Don Gagne committed
8 9
import QGroundControl.ScreenToolsController 1.0

Don Gagne's avatar
Don Gagne committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*!
 The ScreenTools Singleton provides information on QGC's standard font metrics. It also provides information on screen
 size which can be used to adjust user interface for varying available screen real estate.

 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
 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.
 The medium and large fonts are larger than the default font.

 Usage:

        import QGroundControl.ScreenTools 1.0

        Rectangle {
            anchors.fill:       parent
            anchors.margins:    ScreenTools.defaultFontPixelWidth
            ...
        }
*/
Don Gagne's avatar
Don Gagne committed
28
Item {
29 30
    id: _screenTools

Don Gagne's avatar
Don Gagne committed
31
    //-- The point and pixel font size values are computed at runtime
32

dogmaphobic's avatar
dogmaphobic committed
33
    property real defaultFontPointSize:     10
Don Gagne's avatar
Don Gagne committed
34

Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
35
    /// You can use this property to position ui elements in a screen resolution independent manner. Using fixed positioning values should not
Don Gagne's avatar
Don Gagne committed
36 37
    /// be done. All positioning should be done using anchors or a ratio of the defaultFontPixelHeight and defaultFontPixelWidth values. This way
    /// your ui elements will reposition themselves appropriately on varying screen sizes and resolutions.
dogmaphobic's avatar
dogmaphobic committed
38
    property real defaultFontPixelHeight:   10
Don Gagne's avatar
Don Gagne committed
39

Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
40
    /// You can use this property to position ui elements in a screen resolution independent manner. Using fixed positioning values should not
Don Gagne's avatar
Don Gagne committed
41 42
    /// be done. All positioning should be done using anchors or a ratio of the defaultFontPixelHeight and defaultFontPixelWidth values. This way
    /// your ui elements will reposition themselves appropriately on varying screen sizes and resolutions.
dogmaphobic's avatar
dogmaphobic committed
43
    property real defaultFontPixelWidth:    10
Don Gagne's avatar
Don Gagne committed
44

dogmaphobic's avatar
dogmaphobic committed
45 46 47
    property real smallFontPointSize:       10
    property real mediumFontPointSize:      10
    property real largeFontPointSize:       10
dogmaphobic's avatar
dogmaphobic committed
48

Don Gagne's avatar
Don Gagne committed
49
    property real availableHeight:          0
50
    property real toolbarHeight:            0
Don Gagne's avatar
Don Gagne committed
51

52 53 54
    readonly property real smallFontPointRatio:      0.75
    readonly property real mediumFontPointRatio:     1.25
    readonly property real largeFontPointRatio:      1.5
dogmaphobic's avatar
dogmaphobic committed
55

Don Gagne's avatar
Don Gagne committed
56 57 58
    property bool isAndroid:        ScreenToolsController.isAndroid
    property bool isiOS:            ScreenToolsController.isiOS
    property bool isMobile:         ScreenToolsController.isMobile
Gus Grubba's avatar
Gus Grubba committed
59
    property bool isWindows:        ScreenToolsController.isWindows
60
    property bool isDebug:          ScreenToolsController.isDebug
61
    property bool isTinyScreen:     (Screen.width / Screen.pixelDensity) < 120 // 120mm
62
    property bool isShortScreen:    ScreenToolsController.isMobile && ((Screen.height / Screen.width) < 0.6) // Nexus 7 for example
63
    property bool isHugeScreen:     Screen.width >= 1920*2
Don Gagne's avatar
Don Gagne committed
64

65 66 67
    readonly property real minTouchMillimeters: 10      ///< Minimum touch size in millimeters
    property real minTouchPixels:               0       ///< Minimum touch size in pixels

68
    // The implicit heights/widths for our custom control set
69 70 71 72 73 74 75 76 77 78
    property real implicitButtonWidth:              Math.round(defaultFontPixelWidth *  (isMobile ? 7.0 : 5.0))
    property real implicitButtonHeight:             Math.round(defaultFontPixelHeight * (isMobile ? 2.0 : 1.6))
    property real implicitCheckBoxHeight:           Math.round(defaultFontPixelHeight * (isMobile ? 2.0 : 1.0))
    property real implicitRadioButtonHeight:        implicitCheckBoxHeight
    property real implicitTextFieldHeight:          Math.round(defaultFontPixelHeight * (isMobile ? 2.0 : 1.6))
    property real implicitComboBoxHeight:           Math.round(defaultFontPixelHeight * (isMobile ? 2.0 : 1.6))
    property real implicitComboBoxWidth:            Math.round(defaultFontPixelWidth *  (isMobile ? 7.0 : 5.0))
    property real implicitSliderHeight:             isMobile ? Math.max(defaultFontPixelHeight, minTouchPixels) : defaultFontPixelHeight
    property real checkBoxIndicatorSize:            Math.round(defaultFontPixelHeight * (isMobile ? 1.5 : 1.0))
    property real radioButtonIndicatorSize:         checkBoxIndicatorSize
79

80 81
    readonly property string normalFontFamily:      "opensans"
    readonly property string demiboldFontFamily:    "opensans-demibold"
82
    readonly property string fixedFontFamily:       ScreenToolsController.fixedFontFamily
83 84 85 86
    /* This mostly works but for some reason, reflowWidths() in SetupView doesn't change size.
       I've disabled (in release builds) until I figure out why. Changes require a restart for now.
    */
    Connections {
87 88
        target: QGroundControl.settingsManager.appSettings.appFontPointSize
        onValueChanged: {
89
            if(ScreenToolsController.isDebug)
90
                _setBasePointSize(QGroundControl.settingsManager.appSettings.appFontPointSize.value)
91 92 93
        }
    }

94 95 96 97
    function printScreenStats() {
        console.log('ScreenTools: Screen.width: ' + Screen.width + ' Screen.height: ' + Screen.height + ' Screen.pixelDensity: ' + Screen.pixelDensity)
    }

Don Gagne's avatar
Don Gagne committed
98
    /// Returns the current x position of the mouse in global screen coordinates.
Don Gagne's avatar
Don Gagne committed
99 100 101 102
    function mouseX() {
        return ScreenToolsController.mouseX()
    }

Don Gagne's avatar
Don Gagne committed
103
    /// Returns the current y position of the mouse in global screen coordinates.
Don Gagne's avatar
Don Gagne committed
104 105 106 107
    function mouseY() {
        return ScreenToolsController.mouseY()
    }

Don Gagne's avatar
Don Gagne committed
108 109
    /// \private
    function _setBasePointSize(pointSize) {
110 111
        _textMeasure.font.pointSize = pointSize
        defaultFontPointSize    = pointSize
112 113
        defaultFontPixelHeight  = Math.round(_textMeasure.fontHeight/2.0)*2
        defaultFontPixelWidth   = Math.round(_textMeasure.fontWidth/2.0)*2
114 115 116
        smallFontPointSize      = defaultFontPointSize  * _screenTools.smallFontPointRatio
        mediumFontPointSize     = defaultFontPointSize  * _screenTools.mediumFontPointRatio
        largeFontPointSize      = defaultFontPointSize  * _screenTools.largeFontPointRatio
117
        minTouchPixels          = Math.round(minTouchMillimeters * Screen.pixelDensity)
118 119
        if (minTouchPixels / Screen.height > 0.15) {
            // If using physical sizing takes up too much o fthe vertical real estate fall back to font based sizing
120 121 122
            minTouchPixels      = defaultFontPixelHeight * 3
        }

123
        //console.log(minTouchPixels / Screen.height)
124
        toolbarHeight           = isMobile ? minTouchPixels : defaultFontPixelHeight * 3
125 126
    }

127 128 129 130 131
    Text {
        id:     _defaultFont
        text:   "X"
    }

Don Gagne's avatar
Don Gagne committed
132
    Text {
133 134
        id:     _textMeasure
        text:   "X"
135
        font.family:    normalFontFamily
136 137 138
        property real   fontWidth:    contentWidth
        property real   fontHeight:   contentHeight
        Component.onCompleted: {
139 140
            var _appFontPointSizeFact = QGroundControl.settingsManager.appSettings.appFontPointSize
            var baseSize = _appFontPointSizeFact.value
141
            //-- If this is the first time (not saved in settings)
142
            if(baseSize < _appFontPointSizeFact.min || baseSize > _appFontPointSizeFact.max) {
143 144
                //-- Init base size base on the platform
                if(ScreenToolsController.isMobile) {
145
                    //-- Check iOS really tiny screens (iPhone 4s/5/5s)
146 147 148 149 150 151
                    if(ScreenToolsController.isiOS) {
                        if(ScreenToolsController.isiOS && Screen.width < 570) {
                            // For iPhone 4s size we don't fit with additional tweaks to fit screen,
                            // we will just drop point size to make things fit. Correct size not yet determined.
                            baseSize = 12;  // This will be lowered in a future pull
                        } else {
152
                            baseSize = 14;
153 154
                        }
                    } else if((Screen.width / Screen.pixelDensity) < 120) {
155 156
                        baseSize = 11;
                    // Other Android
157
                    } else {
158
                        baseSize = 14;
159
                    }
160
                } else {
161
                    baseSize = _defaultFont.font.pointSize;
162
                }
163
                _appFontPointSizeFact.value = baseSize
164 165
                //-- Release build doesn't get signal
                if(!ScreenToolsController.isDebug)
Don Gagne's avatar
Don Gagne committed
166
                    _screenTools._setBasePointSize(baseSize);
167
            } else {
168
                //-- Set size saved in settings
Don Gagne's avatar
Don Gagne committed
169
                _screenTools._setBasePointSize(baseSize);
170
            }
171
        }
Don Gagne's avatar
Don Gagne committed
172 173
    }
}