ScreenToolsController.h 4.4 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
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
/*=====================================================================

 QGroundControl Open Source Ground Control Station

 (c) 2009, 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

 This file is part of the QGROUNDCONTROL project

 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

 ======================================================================*/

/// @file
///     @author Gus Grubba <mavlink@grubba.com>

#ifndef ScreenToolsController_H
#define ScreenToolsController_H

#include "QGCApplication.h"

#include <QQuickItem>
#include <QCursor>

/*!
    @brief Screen helper tools for QML widgets
    To use its functions, you need to import the module with the following line:
    @code
39

Don Gagne's avatar
Don Gagne committed
40 41 42 43 44 45 46 47 48 49
    @endcode
*/

/// This Qml control is used to return screen parameters
class ScreenToolsController : public QQuickItem
{
    Q_OBJECT
public:
    ScreenToolsController();

50 51 52 53
    Q_PROPERTY(bool     isAndroid           READ isAndroid      CONSTANT)
    Q_PROPERTY(bool     isiOS               READ isiOS          CONSTANT)
    Q_PROPERTY(bool     isMobile            READ isMobile       CONSTANT)
    Q_PROPERTY(bool     testHighDPI         READ testHighDPI    CONSTANT)
54
    Q_PROPERTY(bool     isDebug             READ isDebug        CONSTANT)
Don Gagne's avatar
Don Gagne committed
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

    //! Used to trigger a \c Canvas element repaint.
    /*!
      There is a bug as of Qt 5.4 where a Canvas element defined within a QQuickWidget does not receive
      repaint events. QGC's main window will emit these signals when a \c Canvas element needs to be
      repainted.
      If you use a \c Canvas element inside some QML widget, you can use this code to handle repaint:
      @code
      import QGroundControl.ScreenToolsController 1.0
      ...
        Canvas {
            id: myCanvas
            height: 40
            width:  40
            Connections {
                target: ScreenToolsController
                onRepaintRequestedChanged: {
                    myCanvas.requestPaint();
                }
            }
            onPaint: {
                var context = getContext("2d");
                ...
            }
        }
      @endcode
     */

    // Returns current mouse position
    Q_INVOKABLE int mouseX(void) { return QCursor::pos().x(); }
    Q_INVOKABLE int mouseY(void) { return QCursor::pos().y(); }
Don Gagne's avatar
Don Gagne committed
86

87 88 89 90
    // Used to adjust default font size on an OS basis
    Q_PROPERTY(double defaultFontPixelSizeRatio   MEMBER _defaultFontPixelSizeRatio     CONSTANT)

    // Used to calculate font sizes based on default font size
Don Gagne's avatar
Don Gagne committed
91 92 93
    Q_PROPERTY(double smallFontPixelSizeRatio   MEMBER _smallFontPixelSizeRatio     CONSTANT)
    Q_PROPERTY(double mediumFontPixelSizeRatio  MEMBER _mediumFontPixelSizeRatio    CONSTANT)
    Q_PROPERTY(double largeFontPixelSizeRatio   MEMBER _largeFontPixelSizeRatio     CONSTANT)
94

Don Gagne's avatar
Don Gagne committed
95 96 97 98 99 100 101 102 103 104 105
#if defined (__android__)
    bool    isAndroid           () { return true;  }
    bool    isiOS               () { return false; }
    bool    isMobile            () { return true;  }
#elif defined(__ios__)
    bool    isAndroid           () { return false; }
    bool    isiOS               () { return true; }
    bool    isMobile            () { return true; }
#else
    bool    isAndroid           () { return false; }
    bool    isiOS               () { return false; }
106
    bool    isMobile            () { return qgcApp()->fakeMobile(); }
Don Gagne's avatar
Don Gagne committed
107
#endif
108

109
#ifdef QT_DEBUG
110 111
    bool testHighDPI            () { return qgcApp()->testHighDPI(); }
    bool isDebug                () { return true; }
112
#else
113 114
    bool isDebug                () { return false; }
    bool testHighDPI            () { return false; }
115
#endif
Don Gagne's avatar
Don Gagne committed
116 117

signals:
dogmaphobic's avatar
dogmaphobic committed
118
    void repaintRequested();
Don Gagne's avatar
Don Gagne committed
119 120 121 122 123

private slots:
    void _updateCanvas();

private:
124 125
    static const double _defaultFontPixelSizeRatio;
    static const double _smallFontPixelSizeRatio;
Don Gagne's avatar
Don Gagne committed
126 127 128 129 130
    static const double _mediumFontPixelSizeRatio;
    static const double _largeFontPixelSizeRatio;
};

#endif