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

dogmaphobic's avatar
dogmaphobic committed
3
import QtQuick 2.4
Don Gagne's avatar
Don Gagne committed
4
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 10
import QGroundControl.ScreenToolsController 1.0

Item {
11 12
    id: _screenTools

Don Gagne's avatar
Don Gagne committed
13 14
    signal repaintRequested

15
    property real availableHeight:          0
16

dogmaphobic's avatar
dogmaphobic committed
17 18 19 20 21 22 23
    //-- These are computed at runtime
    property real defaultFontPointSize:     10
    property real defaultFontPixelHeight:   10
    property real defaultFontPixelWidth:    10
    property real smallFontPointSize:       10
    property real mediumFontPointSize:      10
    property real largeFontPointSize:       10
dogmaphobic's avatar
dogmaphobic committed
24

25 26 27
    readonly property real smallFontPointRatio:      0.75
    readonly property real mediumFontPointRatio:     1.25
    readonly property real largeFontPointRatio:      1.5
dogmaphobic's avatar
dogmaphobic committed
28

Don Gagne's avatar
Don Gagne committed
29 30 31
    property bool isAndroid:        ScreenToolsController.isAndroid
    property bool isiOS:            ScreenToolsController.isiOS
    property bool isMobile:         ScreenToolsController.isMobile
32
    property bool isDebug:          ScreenToolsController.isDebug
33
    property bool isTinyScreen:     (Screen.width / Screen.pixelDensity) < 120 // 120mm
34
    property bool isShortScreen:    ScreenToolsController.isMobile && ((Screen.height / Screen.width) < 0.6) // Nexus 7 for example
Don Gagne's avatar
Don Gagne committed
35

36 37 38
    readonly property string normalFontFamily:      "opensans"
    readonly property string demiboldFontFamily:    "opensans-demibold"

39 40 41 42 43 44 45 46 47 48 49
    /* 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 {
        target: QGroundControl
        onBaseFontPointSizeChanged: {
            if(ScreenToolsController.isDebug)
                setBasePointSize(QGroundControl.baseFontPointSize)
        }
    }

Don Gagne's avatar
Don Gagne committed
50 51 52 53 54 55 56 57
    function mouseX() {
        return ScreenToolsController.mouseX()
    }

    function mouseY() {
        return ScreenToolsController.mouseY()
    }

58 59 60 61 62 63 64 65 66 67
    function setBasePointSize(pointSize) {
        _textMeasure.font.pointSize = pointSize
        defaultFontPointSize    = pointSize
        defaultFontPixelHeight  = _textMeasure.fontHeight
        defaultFontPixelWidth   = _textMeasure.fontWidth
        smallFontPointSize      = defaultFontPointSize  * _screenTools.smallFontPointRatio
        mediumFontPointSize     = defaultFontPointSize  * _screenTools.mediumFontPointRatio
        largeFontPointSize      = defaultFontPointSize  * _screenTools.largeFontPointRatio
    }

68 69 70 71 72
    Text {
        id:     _defaultFont
        text:   "X"
    }

Don Gagne's avatar
Don Gagne committed
73
    Text {
74 75
        id:     _textMeasure
        text:   "X"
76
        font.family:    normalFontFamily
77 78 79 80 81 82 83 84
        property real   fontWidth:    contentWidth
        property real   fontHeight:   contentHeight
        Component.onCompleted: {
            var baseSize = QGroundControl.baseFontPointSize;
            //-- If this is the first time (not saved in settings)
            if(baseSize < 6 || baseSize > 48) {
                //-- Init base size base on the platform
                if(ScreenToolsController.isMobile) {
85
                    //-- Check iOS really tiny screens (iPhone 4s/5/5s)
86 87 88 89 90 91 92 93 94
                    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 {
                            baseSize = 12;
                        }
                    } else if((Screen.width / Screen.pixelDensity) < 120) {
95 96
                        baseSize = 11;
                    // Other Android
97
                    } else {
98
                        baseSize = 14;
99
                    }
100
                } else {
101 102 103 104 105 106 107 108 109
                    //-- Mac OS
                    if(ScreenToolsController.isMacOS)
                        baseSize = _defaultFont.font.pointSize;
                    //-- Linux
                    else if(ScreenToolsController.isLinux)
                        baseSize = _defaultFont.font.pointSize - 3.25;
                    //-- Windows
                    else
                        baseSize = _defaultFont.font.pointSize;
110
                }
111 112 113 114
                QGroundControl.baseFontPointSize = baseSize
                //-- Release build doesn't get signal
                if(!ScreenToolsController.isDebug)
                    _screenTools.setBasePointSize(baseSize);
115
            } else {
116 117
                //-- Set size saved in settings
                _screenTools.setBasePointSize(baseSize);
118
            }
119
        }
Don Gagne's avatar
Don Gagne committed
120 121
    }
}