aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewposition.cpp
blob: 52e197b1ed34564f857feea95521fb9e27607ff9 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QML preview debug service.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qqmlpreviewposition.h"

#include <QtGui/qwindow.h>
#include <QtGui/qscreen.h>
#include <QtGui/qguiapplication.h>
#include <private/qhighdpiscaling_p.h>

QT_BEGIN_NAMESPACE

static QVector<QQmlPreviewPosition::ScreenData> initScreensData()
{
    QVector<QQmlPreviewPosition::ScreenData> screensData;

    for (QScreen *screen : QGuiApplication::screens()) {
        QQmlPreviewPosition::ScreenData sd{screen->name(), screen->geometry()};
        screensData.append(sd);
    }
    return screensData;
}

static QScreen *findScreen(const QString &nameOfScreen)
{
    for (QScreen *screen : QGuiApplication::screens()) {
        if (screen->name() == nameOfScreen)
            return screen;
    }
    return nullptr;
}

static QDataStream &operator<<(QDataStream &out, const QQmlPreviewPosition::ScreenData &screenData)
{
    out << screenData.name;
    out << screenData.rect;
    return out;
}

static QDataStream &operator>>(QDataStream &in, QQmlPreviewPosition::ScreenData &screenData)
{
    in >> screenData.name;
    in >> screenData.rect;
    return in;
}

bool QQmlPreviewPosition::ScreenData::operator==(const QQmlPreviewPosition::ScreenData &other) const
{
    return other.rect == rect && other.name == name;
}

QQmlPreviewPosition::QQmlPreviewPosition()
    : m_settings("QtProject", "QtQmlPreview")
{
    m_savePositionTimer.setSingleShot(true);
    m_savePositionTimer.setInterval(500);
    QObject::connect(&m_savePositionTimer, &QTimer::timeout, [this]() {
        saveWindowPosition();
    });
}

QQmlPreviewPosition::~QQmlPreviewPosition()
{
    saveWindowPosition();
}

void QQmlPreviewPosition::takePosition(QWindow *window, InitializeState state)
{
    Q_ASSERT(window);
    // only save the position if we already tried to get the last saved position
    if (m_initializeState == PositionInitialized) {
        m_hasPosition = true;
        auto screen = window->screen();
        auto nativePosition = QHighDpiScaling::mapPositionToNative(window->framePosition(),
                                                                   screen->handle());
        m_lastWindowPosition = {screen->name(), nativePosition};

        m_savePositionTimer.start();
    }
    if (state == InitializePosition)
        m_initializeState = InitializePosition;
}

void QQmlPreviewPosition::saveWindowPosition()
{
    if (m_hasPosition) {
        const QByteArray positionAsByteArray = fromPositionToByteArray(m_lastWindowPosition);
        if (!m_settingsKey.isNull())
            m_settings.setValue(m_settingsKey, positionAsByteArray);

        m_settings.setValue(QLatin1String("global_lastpostion"), positionAsByteArray);
    }
}

void QQmlPreviewPosition::loadWindowPositionSettings(const QUrl &url)
{
    m_settingsKey = url.toString(QUrl::PreferLocalFile) + QLatin1String("_lastpostion");

    if (m_settings.contains(m_settingsKey)) {
        m_hasPosition = true;
        readLastPositionFromByteArray(m_settings.value(m_settingsKey).toByteArray());
    }
}

void QQmlPreviewPosition::initLastSavedWindowPosition(QWindow *window)
{
    Q_ASSERT(window);
    m_initializeState = PositionInitialized;
    if (m_currentInitScreensData.isEmpty())
        m_currentInitScreensData = initScreensData();
    // if it is the first time we just use the fall back from a last shown qml file
    if (!m_hasPosition) {
        if (!m_settings.contains(QLatin1String("global_lastpostion")))
            return;
        readLastPositionFromByteArray(m_settings.value(QLatin1String("global_lastpostion"))
                                              .toByteArray());
    }
    setPosition(m_lastWindowPosition, window);
}

QByteArray QQmlPreviewPosition::fromPositionToByteArray(
        const QQmlPreviewPosition::Position &position)
{
    QByteArray array;
    QDataStream stream(&array, QIODevice::WriteOnly);
    stream.setVersion(QDataStream::Qt_5_12);

    const quint16 majorVersion = 1;
    const quint16 minorVersion = 0;

    stream << majorVersion
           << minorVersion
           << m_currentInitScreensData
           << position.screenName
           << position.nativePosition;
    return array;
}

void QQmlPreviewPosition::readLastPositionFromByteArray(const QByteArray &array)
{
    QDataStream stream(array);
    stream.setVersion(QDataStream::Qt_5_12);

    // no version check for 1.0
    //const quint16 currentMajorVersion = 1;
    quint16 majorVersion = 0;
    quint16 minorVersion = 0;

    stream >> majorVersion >> minorVersion;

    QVector<ScreenData> initScreensData;
    stream >> initScreensData;

    if (m_currentInitScreensData != initScreensData)
        return;

    QString nameOfScreen;
    stream >> nameOfScreen;

    QScreen *screen = findScreen(nameOfScreen);
    if (!screen)
        return;

    QPoint nativePosition;
    stream >> nativePosition;
    if (nativePosition.isNull())
        return;
    m_lastWindowPosition = {nameOfScreen, nativePosition};
}

void QQmlPreviewPosition::setPosition(const QQmlPreviewPosition::Position &position,
                                      QWindow *window)
{
    if (position.nativePosition.isNull())
        return;
    if (QScreen *screen = findScreen(position.screenName)) {
        window->setScreen(screen);
        const auto point = QHighDpiScaling::mapPositionFromNative(position.nativePosition,
                                                                  screen->handle());
        const QRect geometry(point, window->size());
        if (screen->virtualGeometry().contains(geometry))
            window->setFramePosition(point);
        else
            qWarning("preview position is out of screen");
    }
}

QT_END_NAMESPACE