on 46b6ebd help sign in
320 lines 7.2 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)
42// qDebug() << "MirSurface::MirSurface() " << name;
43 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
45 m_delayedResizeTimer.setInterval(600);
46 m_delayedResizeTimer.setSingleShot(true);
47 connect(&m_delayedResizeTimer, &QTimer::timeout, this, &MirSurface::applyDelayedResize);
50MirSurface::~MirSurface()
52// qDebug() << "MirSurface::~MirSurface() " << name();
55QString MirSurface::name() const
57 return m_name;
60Mir::Type MirSurface::type() const
62 return m_type;
65Mir::State MirSurface::state() const
67 return m_state;
70void MirSurface::setState(Mir::State state)
72 if (state == m_state)
73 return;
75 m_state = state;
76 Q_EMIT stateChanged(state);
79bool MirSurface::live() const
81 return m_live;
84bool MirSurface::visible() const
86 return m_visible;
89void MirSurface::setLive(bool live)
91// qDebug().nospace() << "MirSurface::setLive("<<live<<") " << name();
92 if (live == m_live)
93 return;
95 m_live = live;
96 Q_EMIT liveChanged(live);
98 if (!m_live && m_views.count() == 0) {
99 deleteLater();
100 }
103QUrl MirSurface::qmlFilePath() const
105 return m_qmlFilePath;
108QUrl MirSurface::screenshotUrl() const
110 return m_screenshotUrl;
113void MirSurface::setScreenshotUrl(QUrl screenshotUrl)
115 if (screenshotUrl == m_screenshotUrl)
116 return;
118 m_screenshotUrl = screenshotUrl;
119 Q_EMIT screenshotUrlChanged(screenshotUrl);
122Mir::OrientationAngle MirSurface::orientationAngle() const
124 return m_orientationAngle;
127void MirSurface::setOrientationAngle(Mir::OrientationAngle angle)
129 if (angle == m_orientationAngle)
130 return;
132 m_orientationAngle = angle;
133 Q_EMIT orientationAngleChanged(angle);
138void MirSurface::registerView(qintptr viewId)
140 m_views.insert(viewId, MirSurface::View{false});
141// qDebug().nospace() << "MirSurface[" << name() << "]::registerView(" << viewId << ")"
142// << " after=" << m_views.count();
145void MirSurface::unregisterView(qintptr viewId)
147// qDebug().nospace() << "MirSurface[" << name() << "]::unregisterView(" << viewId << ")"
148// << " after=" << m_views.count() << " live=" << m_live;
149 m_views.remove(viewId);
150 if (!m_live && m_views.count() == 0) {
151 deleteLater();
152 }
153 updateVisibility();
156void MirSurface::setViewVisibility(qintptr viewId, bool visible)
158 if (!m_views.contains(viewId)) return;
160 m_views[viewId].visible = visible;
161 updateVisibility();
164void MirSurface::updateVisibility()
166 bool newVisible = false;
167 QHashIterator<qintptr, View> i(m_views);
168 while (i.hasNext()) {
169 i.next();
170 newVisible |= i.value().visible;
171 }
173 if (newVisible != visible()) {
174// qDebug().nospace() << "MirSurface[" << name() << "]::updateVisibility(" << newVisible << ")";
176 m_visible = newVisible;
177 Q_EMIT visibleChanged(m_visible);
178 }
181bool MirSurface::activeFocus() const
183 return m_activeFocus;
186void MirSurface::setActiveFocus(bool value)
188 if (m_activeFocus == value)
189 return;
191 m_activeFocus = value;
193 Q_EMIT activeFocusChanged(value);
196int MirSurface::width() const
198 return m_width;
201int MirSurface::height() const
203 return m_height;
206void MirSurface::resize(int width, int height)
208 if (m_slowToResize) {
209 if (!m_delayedResizeTimer.isActive()) {
210 m_delayedResize.setWidth(width);
211 m_delayedResize.setHeight(height);
212 m_delayedResizeTimer.start();
213 } else {
214 m_pendingResize.setWidth(width);
215 m_pendingResize.setHeight(height);
216 }
217 } else {
218 doResize(width, height);
219 }
222void MirSurface::applyDelayedResize()
224 doResize(m_delayedResize.width(), m_delayedResize.height());
225 m_delayedResize.setWidth(-1);
226 m_delayedResize.setHeight(-1);
228 if (m_pendingResize.isValid()) {
229 QSize size = m_pendingResize;
230 m_pendingResize.setWidth(-1);
231 m_pendingResize.setHeight(-1);
232 resize(size.width(), size.height());
233 }
236void MirSurface::doResize(int width, int height)
238 bool changed = false;
240 if (width != m_width) {
241 m_width = width;
242 Q_EMIT widthChanged();
243 changed = true;
244 }
246 if (m_height != height) {
247 m_height = height;
248 Q_EMIT heightChanged();
249 changed = true;
250 }
252 if (changed) {
253 Q_EMIT sizeChanged(QSize(width, height));
254 }
257bool MirSurface::isSlowToResize() const
259 return m_slowToResize;
262void MirSurface::setSlowToResize(bool value)
264 if (m_slowToResize != value) {
265 m_slowToResize = value;
266 Q_EMIT slowToResizeChanged();
267 if (!m_slowToResize && m_delayedResizeTimer.isActive()) {
268 m_delayedResizeTimer.stop();
269 applyDelayedResize();
270 }
271 }
274void MirSurface::setMinimumWidth(int value)
276 if (value != m_minimumWidth) {
277 m_minimumWidth = value;
278 Q_EMIT minimumWidthChanged(m_minimumWidth);
279 }
282void MirSurface::setMaximumWidth(int value)
284 if (value != m_maximumWidth) {
285 m_maximumWidth = value;
286 Q_EMIT maximumWidthChanged(m_maximumWidth);
287 }
290void MirSurface::setMinimumHeight(int value)
292 if (value != m_minimumHeight) {
293 m_minimumHeight = value;
294 Q_EMIT minimumHeightChanged(m_minimumHeight);
295 }
298void MirSurface::setMaximumHeight(int value)
300 if (value != m_maximumHeight) {
301 m_maximumHeight = value;
302 Q_EMIT maximumHeightChanged(m_maximumHeight);
303 }
306void MirSurface::setWidthIncrement(int value)
308 if (value != m_widthIncrement) {
309 m_widthIncrement = value;
310 Q_EMIT widthIncrementChanged(m_widthIncrement);
311 }
314void MirSurface::setHeightIncrement(int value)
316 if (value != m_heightIncrement) {
317 m_heightIncrement = value;
318 Q_EMIT heightIncrementChanged(m_heightIncrement);
319 }