ScreenTools.h 4.69 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1
/*=====================================================================
2

Don Gagne's avatar
Don Gagne committed
3
 QGroundControl Open Source Ground Control Station
4

Don Gagne's avatar
Don Gagne committed
5
 (c) 2009, 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
6

Don Gagne's avatar
Don Gagne committed
7
 This file is part of the QGROUNDCONTROL project
8

Don Gagne's avatar
Don Gagne committed
9 10 11 12
 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.
13

Don Gagne's avatar
Don Gagne committed
14 15 16 17
 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.
18

Don Gagne's avatar
Don Gagne committed
19 20
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
21

Don Gagne's avatar
Don Gagne committed
22 23 24
 ======================================================================*/

/// @file
25
///     @author Gus Grubba <mavlink@grubba.com>
Don Gagne's avatar
Don Gagne committed
26

27 28
#ifndef SCREENTOOLS_H
#define SCREENTOOLS_H
Don Gagne's avatar
Don Gagne committed
29 30 31 32

#include <QObject>
#include <QCursor>

33 34 35 36 37 38 39 40 41 42 43 44 45
/*!
    @brief Screen helper tools for QML widgets
    To use its functions, you need to import the module with the following line:
    @code
    import QGroundControl.ScreenTools 1.0
    @endcode
    @remark As for the screen density functions, QtQuick provides the \c Screen type (defined in QtQuick.Window)
    but as of Qt 5.4 (QtQuick.Window 2.2), this only works if the main window is QtQuick. As QGC is primarily
    a Qt application and only some of its UI elements are QLM widgets, this does not work. Hence, these function
    defined here.
    @sa <a href="http://doc.qt.io/qt-5/qml-qtquick-window-screen.html">Screen QML Type</a>
*/

46 47
/// This Qml control is used to return screen parameters
class ScreenTools : public QObject
Don Gagne's avatar
Don Gagne committed
48 49 50
{
    Q_OBJECT
public:
51 52
    ScreenTools();

53
    //! Returns the screen density in Dots Per Inch
54
    Q_PROPERTY(double   screenDPI           READ screenDPI CONSTANT)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    //! Returns a factor used to calculate the font point size to use
    /*!
      When defining fonts in point size, as in:
      @code
      Text {
        text: "Foo Bar"
        font.pointSize: 14
      }
      @endcode
      The size is device dependent. If you define this based on a screen set to 72dpi (Mac OS), once
      this is displayed on a different screen with a different pixel density, such as 96dpi (Windows),
      the text will be displayed in the wrong size.
      Use \c dpiFactor to accomodate for these differences. All font point sizes are given in 72dpi
      and \c dpiFactor returns a factor to use for adjusting it to the current target screen.
      @code
      import QGroundControl.ScreenTools 1.0
      property ScreenTools screenTools: ScreenTools { }
      Text {
        text: "Foo Bar"
        font.pointSize: 14 * screenTools.dpiFactor
      }
      @endcode
     */
78
    Q_PROPERTY(double   dpiFactor           READ dpiFactor CONSTANT)
79
    //! Returns the global mouse X position
80
    Q_PROPERTY(int      mouseX              READ mouseX)
81
    //! Returns the global mouse Y position
82
    Q_PROPERTY(int      mouseY              READ mouseY)
83 84 85 86 87
    //! 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.
dogmaphobic's avatar
dogmaphobic committed
88
      If you use a \c Canvas element inside some QML widget, you can use this code to handle repaint:
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      @code
      import QGroundControl.ScreenTools 1.0
      ...
      property ScreenTools screenTools: ScreenTools { }
      ...
        Canvas {
            id: myCanvas
            height: 40
            width:  40
            Connections {
                target: screenTools
                onRepaintRequestedChanged: {
                    myCanvas.requestPaint();
                }
            }
            onPaint: {
                var context = getContext("2d");
                ...
            }
        }
      @endcode
     */
111 112
    Q_PROPERTY(bool     repaintRequested    READ repaintRequested   NOTIFY repaintRequestedChanged)

113 114 115 116 117 118
    //! Utility for adjusting font point size.
    /*!
      @sa dpiFactor
     */
    Q_INVOKABLE qreal   dpiAdjustedPointSize(qreal pointSize);

119 120 121 122 123 124 125 126 127 128 129
    double  screenDPI           () { return _dotsPerInch; }
    double  dpiFactor           () { return _dpiFactor; }
    int     mouseX              () { return QCursor::pos().x(); }
    int     mouseY              () { return QCursor::pos().y(); }
    bool    repaintRequested    () { return true; }

signals:
    void repaintRequestedChanged();

private slots:
    void _updateCanvas();
130 131 132 133 134

private:
    double _dotsPerInch;
    double _dpiFactor;

Don Gagne's avatar
Don Gagne committed
135 136 137
};

#endif