on 3df724b help sign in
336 lines 7.6 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);
138Mir::ShellChrome MirSurface::shellChrome() const
140 return m_shellChrome;
143void MirSurface::setShellChrome(Mir::ShellChrome shellChrome)
145 if (shellChrome == m_shellChrome)
146 return;
148 m_shellChrome = shellChrome;
149 Q_EMIT shellChromeChanged(shellChrome);
154void MirSurface::registerView(qintptr viewId)
156 m_views.insert(viewId, MirSurface::View{false});
157// qDebug().nospace() << "MirSurface[" << name() << "]::registerView(" << viewId << ")"
158// << " after=" << m_views.count();
161void MirSurface::unregisterView(qintptr viewId)
163// qDebug().nospace() << "MirSurface[" << name() << "]::unregisterView(" << viewId << ")"
164// << " after=" << m_views.count() << " live=" << m_live;
165 m_views.remove(viewId);
166 if (!m_live && m_views.count() == 0) {
167 deleteLater();
168 }
169 updateVisibility();
172void MirSurface::setViewVisibility(qintptr viewId, bool visible)
174 if (!m_views.contains(viewId)) return;
176 m_views[viewId].visible = visible;
177 updateVisibility();
180void MirSurface::updateVisibility()
182 bool newVisible = false;
183 QHashIterator<qintptr, View> i(m_views);
184 while (i.hasNext()) {
185 i.next();
186 newVisible |= i.value().visible;
187 }
189 if (newVisible != visible()) {
190// qDebug().nospace() << "MirSurface[" << name() << "]::updateVisibility(" << newVisible << ")";
192 m_visible = newVisible;
193 Q_EMIT visibleChanged(m_visible);
194 }
197bool MirSurface::activeFocus() const
199 return m_activeFocus;
202void MirSurface::setActiveFocus(bool value)
204 if (m_activeFocus == value)
205 return;
207 m_activeFocus = value;
209 Q_EMIT activeFocusChanged(value);
212int MirSurface::width() const
214 return m_width;
217int MirSurface::height() const
219 return m_height;
222void MirSurface::resize(int width, int height)
224 if (m_slowToResize) {
225 if (!m_delayedResizeTimer.isActive()) {
226 m_delayedResize.setWidth(width);
227 m_delayedResize.setHeight(height);
228 m_delayedResizeTimer.start();
229 } else {
230 m_pendingResize.setWidth(width);
231 m_pendingResize.setHeight(height);
232 }
233 } else {
234 doResize(width, height);
235 }
238void MirSurface::applyDelayedResize()
240 doResize(m_delayedResize.width(), m_delayedResize.height());
241 m_delayedResize.setWidth(-1);
242 m_delayedResize.setHeight(-1);
244 if (m_pendingResize.isValid()) {
245 QSize size = m_pendingResize;
246 m_pendingResize.setWidth(-1);
247 m_pendingResize.setHeight(-1);
248 resize(size.width(), size.height());
249 }
252void MirSurface::doResize(int width, int height)
254 bool changed = false;
256 if (width != m_width) {
257 m_width = width;
258 Q_EMIT widthChanged();
259 changed = true;
260 }
262 if (m_height != height) {
263 m_height = height;
264 Q_EMIT heightChanged();
265 changed = true;
266 }
268 if (changed) {
269 Q_EMIT sizeChanged(QSize(width, height));
270 }
273bool MirSurface::isSlowToResize() const
275 return m_slowToResize;
278void MirSurface::setSlowToResize(bool value)
280 if (m_slowToResize != value) {
281 m_slowToResize = value;
282 Q_EMIT slowToResizeChanged();
283 if (!m_slowToResize && m_delayedResizeTimer.isActive()) {
284 m_delayedResizeTimer.stop();
285 applyDelayedResize();
286 }
287 }
290void MirSurface::setMinimumWidth(int value)
292 if (value != m_minimumWidth) {
293 m_minimumWidth = value;
294 Q_EMIT minimumWidthChanged(m_minimumWidth);
295 }
298void MirSurface::setMaximumWidth(int value)
300 if (value != m_maximumWidth) {
301 m_maximumWidth = value;
302 Q_EMIT maximumWidthChanged(m_maximumWidth);
303 }
306void MirSurface::setMinimumHeight(int value)
308 if (value != m_minimumHeight) {
309 m_minimumHeight = value;
310 Q_EMIT minimumHeightChanged(m_minimumHeight);
311 }
314void MirSurface::setMaximumHeight(int value)
316 if (value != m_maximumHeight) {
317 m_maximumHeight = value;
318 Q_EMIT maximumHeightChanged(m_maximumHeight);
319 }
322void MirSurface::setWidthIncrement(int value)
324 if (value != m_widthIncrement) {
325 m_widthIncrement = value;
326 Q_EMIT widthIncrementChanged(m_widthIncrement);
327 }
330void MirSurface::setHeightIncrement(int value)
332 if (value != m_heightIncrement) {
333 m_heightIncrement = value;
334 Q_EMIT heightIncrementChanged(m_heightIncrement);
335 }