on 5b58aba help sign in
352 lines 7.9 kB history raw
1/*
2 * Copyright (C) 2015 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "MirSurface.h"
19#include <QDebug>
20#include <QQmlEngine>
22MirSurface::MirSurface(const QString& name,
23 Mir::Type type,
24 Mir::State state,
25 const QUrl& screenshot,
26 const QUrl &qmlFilePath,
27 QObject *parent)
28 : unity::shell::application::MirSurfaceInterface(parent)
29 , m_name(name)
30 , m_type(type)
31 , m_state(state)
32 , m_orientationAngle(Mir::Angle0)
33 , m_screenshotUrl(screenshot)
34 , m_qmlFilePath(qmlFilePath)
35 , m_live(true)
36 , m_visible(true)
37 , m_activeFocus(false)
38 , m_width(-1)
39 , m_height(-1)
40 , m_slowToResize(false)
41 , m_shellChrome(Mir::NormalChrome)
43// qDebug() << "MirSurface::MirSurface() " << name;
44 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
46 m_delayedResizeTimer.setInterval(600);
47 m_delayedResizeTimer.setSingleShot(true);
48 connect(&m_delayedResizeTimer, &QTimer::timeout, this, &MirSurface::applyDelayedResize);
51MirSurface::~MirSurface()
53// qDebug() << "MirSurface::~MirSurface() " << name();
56QString MirSurface::name() const
58 return m_name;
61Mir::Type MirSurface::type() const
63 return m_type;
66Mir::State MirSurface::state() const
68 return m_state;
71void MirSurface::setState(Mir::State state)
73 if (state == m_state)
74 return;
76 m_state = state;
77 Q_EMIT stateChanged(state);
80bool MirSurface::live() const
82 return m_live;
85bool MirSurface::visible() const
87 return m_visible;
90void MirSurface::setLive(bool live)
92// qDebug().nospace() << "MirSurface::setLive("<<live<<") " << name();
93 if (live == m_live)
94 return;
96 m_live = live;
97 Q_EMIT liveChanged(live);
99 if (!m_live && m_views.count() == 0) {
100 deleteLater();
101 }
104QUrl MirSurface::qmlFilePath() const
106 return m_qmlFilePath;
109QUrl MirSurface::screenshotUrl() const
111 return m_screenshotUrl;
114void MirSurface::setScreenshotUrl(QUrl screenshotUrl)
116 if (screenshotUrl == m_screenshotUrl)
117 return;
119 m_screenshotUrl = screenshotUrl;
120 Q_EMIT screenshotUrlChanged(screenshotUrl);
123Mir::OrientationAngle MirSurface::orientationAngle() const
125 return m_orientationAngle;
128void MirSurface::setOrientationAngle(Mir::OrientationAngle angle)
130 if (angle == m_orientationAngle)
131 return;
133 m_orientationAngle = angle;
134 Q_EMIT orientationAngleChanged(angle);
137Mir::ShellChrome MirSurface::shellChrome() const
139 return m_shellChrome;
142void MirSurface::setShellChrome(Mir::ShellChrome shellChrome)
144 if (shellChrome == m_shellChrome)
145 return;
147 m_shellChrome = shellChrome;
148 Q_EMIT shellChromeChanged(shellChrome);
151QString MirSurface::keymapLayout() const
153 return m_keyMap.first;
156QString MirSurface::keymapVariant() const
158 return m_keyMap.second;
161void MirSurface::setKeymap(const QString &layout, const QString &variant)
163 if (layout.isEmpty()) {
164 return;
165 }
166 m_keyMap = qMakePair(layout, variant);
167 Q_EMIT keymapChanged(layout, variant);
170void MirSurface::registerView(qintptr viewId)
172 m_views.insert(viewId, MirSurface::View{false});
173// qDebug().nospace() << "MirSurface[" << name() << "]::registerView(" << viewId << ")"
174// << " after=" << m_views.count();
177void MirSurface::unregisterView(qintptr viewId)
179// qDebug().nospace() << "MirSurface[" << name() << "]::unregisterView(" << viewId << ")"
180// << " after=" << m_views.count() << " live=" << m_live;
181 m_views.remove(viewId);
182 if (!m_live && m_views.count() == 0) {
183 deleteLater();
184 }
185 updateVisibility();
188void MirSurface::setViewVisibility(qintptr viewId, bool visible)
190 if (!m_views.contains(viewId)) return;
192 m_views[viewId].visible = visible;
193 updateVisibility();
196void MirSurface::updateVisibility()
198 bool newVisible = false;
199 QHashIterator<qintptr, View> i(m_views);
200 while (i.hasNext()) {
201 i.next();
202 newVisible |= i.value().visible;
203 }
205 if (newVisible != visible()) {
206// qDebug().nospace() << "MirSurface[" << name() << "]::updateVisibility(" << newVisible << ")";
208 m_visible = newVisible;
209 Q_EMIT visibleChanged(m_visible);
210 }
213bool MirSurface::activeFocus() const
215 return m_activeFocus;
218void MirSurface::setActiveFocus(bool value)
220 if (m_activeFocus == value)
221 return;
223 m_activeFocus = value;
225 Q_EMIT activeFocusChanged(value);
228int MirSurface::width() const
230 return m_width;
233int MirSurface::height() const
235 return m_height;
238void MirSurface::resize(int width, int height)
240 if (m_slowToResize) {
241 if (!m_delayedResizeTimer.isActive()) {
242 m_delayedResize.setWidth(width);
243 m_delayedResize.setHeight(height);
244 m_delayedResizeTimer.start();
245 } else {
246 m_pendingResize.setWidth(width);
247 m_pendingResize.setHeight(height);
248 }
249 } else {
250 doResize(width, height);
251 }
254void MirSurface::applyDelayedResize()
256 doResize(m_delayedResize.width(), m_delayedResize.height());
257 m_delayedResize.setWidth(-1);
258 m_delayedResize.setHeight(-1);
260 if (m_pendingResize.isValid()) {
261 QSize size = m_pendingResize;
262 m_pendingResize.setWidth(-1);
263 m_pendingResize.setHeight(-1);
264 resize(size.width(), size.height());
265 }
268void MirSurface::doResize(int width, int height)
270 bool changed = false;
272 if (width != m_width) {
273 m_width = width;
274 Q_EMIT widthChanged();
275 changed = true;
276 }
278 if (m_height != height) {
279 m_height = height;
280 Q_EMIT heightChanged();
281 changed = true;
282 }
284 if (changed) {
285 Q_EMIT sizeChanged(QSize(width, height));
286 }
289bool MirSurface::isSlowToResize() const
291 return m_slowToResize;
294void MirSurface::setSlowToResize(bool value)
296 if (m_slowToResize != value) {
297 m_slowToResize = value;
298 Q_EMIT slowToResizeChanged();
299 if (!m_slowToResize && m_delayedResizeTimer.isActive()) {
300 m_delayedResizeTimer.stop();
301 applyDelayedResize();
302 }
303 }
306void MirSurface::setMinimumWidth(int value)
308 if (value != m_minimumWidth) {
309 m_minimumWidth = value;
310 Q_EMIT minimumWidthChanged(m_minimumWidth);
311 }
314void MirSurface::setMaximumWidth(int value)
316 if (value != m_maximumWidth) {
317 m_maximumWidth = value;
318 Q_EMIT maximumWidthChanged(m_maximumWidth);
319 }
322void MirSurface::setMinimumHeight(int value)
324 if (value != m_minimumHeight) {
325 m_minimumHeight = value;
326 Q_EMIT minimumHeightChanged(m_minimumHeight);
327 }
330void MirSurface::setMaximumHeight(int value)
332 if (value != m_maximumHeight) {
333 m_maximumHeight = value;
334 Q_EMIT maximumHeightChanged(m_maximumHeight);
335 }
338void MirSurface::setWidthIncrement(int value)
340 if (value != m_widthIncrement) {
341 m_widthIncrement = value;
342 Q_EMIT widthIncrementChanged(m_widthIncrement);
343 }
346void MirSurface::setHeightIncrement(int value)
348 if (value != m_heightIncrement) {
349 m_heightIncrement = value;
350 Q_EMIT heightIncrementChanged(m_heightIncrement);
351 }