Skip to content
ScreenTools.qml 4.6 KiB
Newer Older
Don Gagne's avatar
Don Gagne committed
pragma Singleton

dogmaphobic's avatar
dogmaphobic committed
import QtQuick 2.4
Don Gagne's avatar
Don Gagne committed
import QtQuick.Controls 1.2
dogmaphobic's avatar
dogmaphobic committed
import QtQuick.Window 2.2
Don Gagne's avatar
Don Gagne committed

import QGroundControl                       1.0
Don Gagne's avatar
Don Gagne committed
import QGroundControl.ScreenToolsController 1.0

Item {
dogmaphobic's avatar
dogmaphobic committed
    id: _screenTools

Don Gagne's avatar
Don Gagne committed
    signal repaintRequested

    property real availableHeight:          0
dogmaphobic's avatar
dogmaphobic committed
    //-- 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

dogmaphobic's avatar
dogmaphobic committed
    readonly property real smallFontPointRatio:      0.75
    readonly property real mediumFontPointRatio:     1.25
    readonly property real largeFontPointRatio:      1.5
dogmaphobic's avatar
dogmaphobic committed

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

    readonly property string normalFontFamily:      "opensans"
    readonly property string demiboldFontFamily:    "opensans-demibold"

    /* 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
    function mouseX() {
        return ScreenToolsController.mouseX()
    }

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

    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
    }

dogmaphobic's avatar
dogmaphobic committed
    Text {
        id:     _defaultFont
        text:   "X"
    }

Don Gagne's avatar
Don Gagne committed
    Text {
        id:     _textMeasure
        text:   "X"
        font.family:    normalFontFamily
        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) {
                    //-- Check iOS really tiny screens (iPhone 4s/5/5s)
                    if(ScreenToolsController.isiOS && Screen.width < 570)
                        baseSize = 9;
                    //-- iPhone 6/6s)
                    else if(ScreenToolsController.isiOS && Screen.width < 670)
                        baseSize = 10;
                    // Larger iOS (6/6s Plus or iPad)
                    else if(ScreenToolsController.isiOS)
                        baseSize = 13;
                    // Small Android Devices
                    else if((Screen.width / Screen.pixelDensity) < 120)
                        baseSize = 11;
                    // Other Android
                    else
                        baseSize = 14;
dogmaphobic's avatar
dogmaphobic committed
                } else {
                    //-- 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;
                QGroundControl.baseFontPointSize = baseSize
                //-- Release build doesn't get signal
                if(!ScreenToolsController.isDebug)
                    _screenTools.setBasePointSize(baseSize);
dogmaphobic's avatar
dogmaphobic committed
            } else {
                //-- Set size saved in settings
                _screenTools.setBasePointSize(baseSize);
Don Gagne's avatar
Don Gagne committed
    }
}