| 1 |
/* |
| 2 |
* Copyright (C) 2014-2016 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 |
*/ |
| 16 |
|
| 17 |
#include "MirSurfaceItem.h" |
| 18 |
|
| 19 |
#include <paths.h> |
| 20 |
|
| 21 |
#include <QGuiApplication> |
| 22 |
#include <QQuickView> |
| 23 |
#include <QQmlContext> |
| 24 |
#include <QQmlProperty> |
| 25 |
#include <QQmlEngine> |
| 26 |
#include <QString> |
| 27 |
|
| 28 |
#include <QDebug> |
| 29 |
|
| 30 |
using namespace lomiri::shell::application; |
| 31 |
|
| 32 |
#define MIRSURFACEITEM_DEBUG 0 |
| 33 |
|
| 34 |
#if MIRSURFACEITEM_DEBUG |
| 35 |
#define DEBUG_MSG(params) qDebug().nospace() << "MirSurfaceItem::" << __func__ << " " << params |
| 36 |
#else |
| 37 |
#define DEBUG_MSG(params) ((void)0) |
| 38 |
#endif |
| 39 |
|
| 40 |
MirSurfaceItem::MirSurfaceItem(QQuickItem *parent) |
| 41 |
: MirSurfaceItemInterface(parent) |
| 42 |
, m_qmlSurface(nullptr) |
| 43 |
, m_qmlContentComponent(nullptr) |
| 44 |
, m_qmlItem(nullptr) |
| 45 |
, m_consumesInput(false) |
| 46 |
, m_orientationAngle(nullptr) |
| 47 |
, m_surfaceWidth(0) |
| 48 |
, m_surfaceHeight(0) |
| 49 |
, m_touchPressCount(0) |
| 50 |
, m_touchReleaseCount(0) |
| 51 |
, m_mousePressCount(0) |
| 52 |
, m_mouseReleaseCount(0) |
| 53 |
{ |
| 54 |
DEBUG_MSG((void*)(this) << name()); |
| 55 |
setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton | Qt::RightButton | |
| 56 |
Qt::ExtraButton1 | Qt::ExtraButton2 | Qt::ExtraButton3 | Qt::ExtraButton4 | |
| 57 |
Qt::ExtraButton5 | Qt::ExtraButton6 | Qt::ExtraButton7 | Qt::ExtraButton8 | |
| 58 |
Qt::ExtraButton9 | Qt::ExtraButton10 | Qt::ExtraButton11 | |
| 59 |
Qt::ExtraButton12 | Qt::ExtraButton13); |
| 60 |
|
| 61 |
connect(this, &QQuickItem::activeFocusChanged, this, &MirSurfaceItem::updateMirSurfaceActiveFocus); |
| 62 |
connect(this, &QQuickItem::visibleChanged, this, &MirSurfaceItem::updateMirSurfaceExposure); |
| 63 |
|
| 64 |
connect(this, &MirSurfaceItem::consumesInputChanged, this, [this]() { |
| 65 |
updateMirSurfaceActiveFocus(hasActiveFocus()); |
| 66 |
}); |
| 67 |
|
| 68 |
// We're just clipping contents in the mock. The real QtMir would copy only relevant buffer instead |
| 69 |
setClip(true); |
| 70 |
} |
| 71 |
|
| 72 |
MirSurfaceItem::~MirSurfaceItem() |
| 73 |
{ |
| 74 |
DEBUG_MSG((void*)(this) << name()); |
| 75 |
setSurface(nullptr); |
| 76 |
delete m_orientationAngle; |
| 77 |
} |
| 78 |
|
| 79 |
void MirSurfaceItem::printComponentErrors() |
| 80 |
{ |
| 81 |
QList<QQmlError> errors = m_qmlContentComponent->errors(); |
| 82 |
for (int i = 0; i < errors.count(); ++i) { |
| 83 |
qDebug() << errors[i]; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
Mir::Type MirSurfaceItem::type() const |
| 88 |
{ |
| 89 |
if (m_qmlSurface) { |
| 90 |
return m_qmlSurface->type(); |
| 91 |
} else { |
| 92 |
return Mir::UnknownType; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
Mir::State MirSurfaceItem::surfaceState() const |
| 97 |
{ |
| 98 |
if (m_qmlSurface) { |
| 99 |
return m_qmlSurface->state(); |
| 100 |
} else { |
| 101 |
return Mir::UnknownState; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
QString MirSurfaceItem::name() const |
| 106 |
{ |
| 107 |
if (m_qmlSurface) { |
| 108 |
return m_qmlSurface->name(); |
| 109 |
} else { |
| 110 |
return QString(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
bool MirSurfaceItem::live() const |
| 115 |
{ |
| 116 |
if (m_qmlSurface) { |
| 117 |
return m_qmlSurface->live(); |
| 118 |
} else { |
| 119 |
return false; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
Mir::ShellChrome MirSurfaceItem::shellChrome() const |
| 124 |
{ |
| 125 |
if (m_qmlSurface) { |
| 126 |
return m_qmlSurface->shellChrome(); |
| 127 |
} else { |
| 128 |
return Mir::NormalChrome; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
Mir::OrientationAngle MirSurfaceItem::orientationAngle() const |
| 133 |
{ |
| 134 |
if (m_orientationAngle) { |
| 135 |
Q_ASSERT(!m_qmlSurface); |
| 136 |
return *m_orientationAngle; |
| 137 |
} else if (m_qmlSurface) { |
| 138 |
return m_qmlSurface->orientationAngle(); |
| 139 |
} else { |
| 140 |
return Mir::Angle0; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void MirSurfaceItem::setOrientationAngle(Mir::OrientationAngle angle) |
| 145 |
{ |
| 146 |
DEBUG_MSG(angle); |
| 147 |
|
| 148 |
if (m_qmlSurface) { |
| 149 |
Q_ASSERT(!m_orientationAngle); |
| 150 |
m_qmlSurface->setOrientationAngle(angle); |
| 151 |
} else if (!m_orientationAngle) { |
| 152 |
m_orientationAngle = new Mir::OrientationAngle; |
| 153 |
*m_orientationAngle = angle; |
| 154 |
Q_EMIT orientationAngleChanged(angle); |
| 155 |
} else if (*m_orientationAngle != angle) { |
| 156 |
*m_orientationAngle = angle; |
| 157 |
Q_EMIT orientationAngleChanged(angle); |
| 158 |
} |
| 159 |
|
| 160 |
if (m_qmlItem) { |
| 161 |
QQmlProperty orientationProp(m_qmlItem, "orientationAngle"); |
| 162 |
if (orientationProp.isValid()) { |
| 163 |
orientationProp.write(QVariant::fromValue(orientationAngle())); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
void MirSurfaceItem::updateScreenshot(QUrl screenshotUrl) |
| 169 |
{ |
| 170 |
if (m_qmlItem) { |
| 171 |
QQmlProperty screenshotSource(m_qmlItem, "screenshotSource"); |
| 172 |
if (screenshotSource.isValid()) { |
| 173 |
screenshotSource.write(QVariant::fromValue(screenshotUrl)); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
void MirSurfaceItem::onComponentStatusChanged(QQmlComponent::Status status) |
| 179 |
{ |
| 180 |
if (status == QQmlComponent::Ready) { |
| 181 |
createQmlContentItem(); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
void MirSurfaceItem::createQmlContentItem() |
| 186 |
{ |
| 187 |
DEBUG_MSG(""); |
| 188 |
|
| 189 |
m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create()); |
| 190 |
m_qmlItem->setParentItem(this); |
| 191 |
|
| 192 |
if (m_fillMode == FillMode::Stretch && width() != 0 && height() != 0) { |
| 193 |
m_qmlItem->setSize(QSize(this->width(), this->height())); |
| 194 |
} else { |
| 195 |
m_qmlItem->setSize(m_qmlSurface->size()); |
| 196 |
} |
| 197 |
setImplicitWidth(m_qmlItem->width()); |
| 198 |
setImplicitHeight(m_qmlItem->height()); |
| 199 |
|
| 200 |
{ |
| 201 |
QQmlProperty screenshotSource(m_qmlItem, "screenshotSource"); |
| 202 |
if (screenshotSource.isValid()) { |
| 203 |
screenshotSource.write(QVariant::fromValue(m_qmlSurface->screenshotUrl())); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
{ |
| 208 |
QQmlProperty orientationProp(m_qmlItem, "orientationAngle"); |
| 209 |
if (orientationProp.isValid()) { |
| 210 |
orientationProp.write(QVariant::fromValue(orientationAngle())); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
{ |
| 215 |
QQmlProperty surfaceProperty(m_qmlItem, "surface"); |
| 216 |
if (surfaceProperty.isValid()) { |
| 217 |
surfaceProperty.write(QVariant::fromValue(m_qmlSurface)); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
void MirSurfaceItem::touchEvent(QTouchEvent * event) |
| 223 |
{ |
| 224 |
if (event->type() == QEvent::TouchBegin) { |
| 225 |
m_touchTrail.clear(); |
| 226 |
} |
| 227 |
|
| 228 |
if (event->touchPointStates() & Qt::TouchPointPressed) { |
| 229 |
++m_touchPressCount; |
| 230 |
Q_EMIT touchPressCountChanged(m_touchPressCount); |
| 231 |
} else if (event->touchPointStates() & Qt::TouchPointReleased) { |
| 232 |
++m_touchReleaseCount; |
| 233 |
Q_EMIT touchReleaseCountChanged(m_touchReleaseCount); |
| 234 |
} |
| 235 |
|
| 236 |
Q_FOREACH(const QTouchEvent::TouchPoint &touchPoint, event->touchPoints()) { |
| 237 |
QString id(touchPoint.id()); |
| 238 |
QVariantList list = m_touchTrail[id].toList(); |
| 239 |
list.append(QVariant::fromValue(touchPoint.pos())); |
| 240 |
if (list.count() > 100) list.pop_front(); |
| 241 |
m_touchTrail[id] = list; |
| 242 |
} |
| 243 |
|
| 244 |
if (m_qmlItem) { |
| 245 |
QQmlProperty touchTrail(m_qmlItem, "touchTrail"); |
| 246 |
touchTrail.write(m_touchTrail); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
void MirSurfaceItem::mousePressEvent(QMouseEvent * event) |
| 251 |
{ |
| 252 |
m_mousePressCount++; |
| 253 |
Q_EMIT mousePressCountChanged(m_mousePressCount); |
| 254 |
event->accept(); |
| 255 |
} |
| 256 |
|
| 257 |
void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event) |
| 258 |
{ |
| 259 |
event->accept(); |
| 260 |
} |
| 261 |
|
| 262 |
void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event) |
| 263 |
{ |
| 264 |
m_mouseReleaseCount++; |
| 265 |
Q_EMIT mouseReleaseCountChanged(m_mouseReleaseCount); |
| 266 |
event->accept(); |
| 267 |
} |
| 268 |
|
| 269 |
void MirSurfaceItem::setSurface(MirSurfaceInterface* surface) |
| 270 |
{ |
| 271 |
DEBUG_MSG("this=" << (void*)(this) << " name=" << name() << " surface=" << surface); |
| 272 |
|
| 273 |
if (m_qmlSurface == surface) { |
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
if (m_qmlSurface) { |
| 278 |
delete m_qmlItem; |
| 279 |
m_qmlItem = nullptr; |
| 280 |
|
| 281 |
delete m_qmlContentComponent; |
| 282 |
m_qmlContentComponent = nullptr; |
| 283 |
|
| 284 |
if (hasActiveFocus() && m_consumesInput && m_qmlSurface->live()) { |
| 285 |
m_qmlSurface->setActiveFocus(false); |
| 286 |
} |
| 287 |
|
| 288 |
disconnect(m_qmlSurface, nullptr, this, nullptr); |
| 289 |
m_qmlSurface->unregisterView((qintptr)this); |
| 290 |
} |
| 291 |
|
| 292 |
m_qmlSurface = static_cast<MirSurface*>(surface); |
| 293 |
|
| 294 |
if (m_qmlSurface) { |
| 295 |
m_qmlSurface->registerView((qintptr)this); |
| 296 |
|
| 297 |
updateSurfaceSize(); |
| 298 |
updateMirSurfaceExposure(); |
| 299 |
|
| 300 |
if (m_orientationAngle) { |
| 301 |
m_qmlSurface->setOrientationAngle(*m_orientationAngle); |
| 302 |
connect(m_qmlSurface, &MirSurfaceInterface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged); |
| 303 |
delete m_orientationAngle; |
| 304 |
m_orientationAngle = nullptr; |
| 305 |
} else { |
| 306 |
connect(m_qmlSurface, &MirSurfaceInterface::orientationAngleChanged, this, &MirSurfaceItem::orientationAngleChanged); |
| 307 |
Q_EMIT orientationAngleChanged(m_qmlSurface->orientationAngle()); |
| 308 |
} |
| 309 |
|
| 310 |
connect(m_qmlSurface, &MirSurface::screenshotUrlChanged, this, &MirSurfaceItem::updateScreenshot); |
| 311 |
connect(m_qmlSurface, &MirSurface::liveChanged, this, [this] (bool live) { |
| 312 |
if (!live) { |
| 313 |
setSurface(nullptr); |
| 314 |
} |
| 315 |
Q_EMIT liveChanged(live); |
| 316 |
}); |
| 317 |
connect(m_qmlSurface, &MirSurface::stateChanged, this, &MirSurfaceItem::surfaceStateChanged); |
| 318 |
connect(m_qmlSurface, &MirSurface::sizeChanged, this, [this] () { |
| 319 |
setImplicitSize(m_qmlSurface->width(), m_qmlSurface->height()); |
| 320 |
if (m_fillMode == FillMode::Stretch) { |
| 321 |
m_qmlItem->setSize(QSize(this->width(), this->height())); |
| 322 |
} else { |
| 323 |
m_qmlItem->setSize(m_qmlSurface->size()); |
| 324 |
} |
| 325 |
}); |
| 326 |
m_surfaceWidth = surface->size().width(); |
| 327 |
m_surfaceHeight = surface->size().height(); |
| 328 |
|
| 329 |
QUrl qmlComponentFilePath; |
| 330 |
if (!m_qmlSurface->qmlFilePath().isEmpty()) { |
| 331 |
qmlComponentFilePath = m_qmlSurface->qmlFilePath(); |
| 332 |
} else { |
| 333 |
qmlComponentFilePath = QUrl("qrc:///QtMir/Application/MirSurfaceItem.qml"); |
| 334 |
} |
| 335 |
|
| 336 |
m_qmlContentComponent = new QQmlComponent(QQmlEngine::contextForObject(parent())->engine(), qmlComponentFilePath); |
| 337 |
|
| 338 |
switch (m_qmlContentComponent->status()) { |
| 339 |
case QQmlComponent::Ready: |
| 340 |
createQmlContentItem(); |
| 341 |
m_qmlSurface->setReady(); |
| 342 |
qDebug() << "content created" << m_surfaceWidth << implicitWidth() << width(); |
| 343 |
break; |
| 344 |
case QQmlComponent::Loading: |
| 345 |
connect(m_qmlContentComponent, &QQmlComponent::statusChanged, |
| 346 |
this, &MirSurfaceItem::onComponentStatusChanged); |
| 347 |
break; |
| 348 |
case QQmlComponent::Error: |
| 349 |
printComponentErrors(); |
| 350 |
qFatal("MirSurfaceItem: failed to create content component."); |
| 351 |
break; |
| 352 |
default: |
| 353 |
qFatal("MirSurfaceItem: Unhandled component status"); |
| 354 |
} |
| 355 |
|
| 356 |
if (m_consumesInput) { |
| 357 |
m_qmlSurface->setActiveFocus(hasActiveFocus()); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
Q_EMIT surfaceChanged(m_qmlSurface); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
void MirSurfaceItem::updateMirSurfaceActiveFocus(bool focused) |
| 366 |
{ |
| 367 |
if (m_qmlSurface && m_consumesInput && m_qmlSurface->live()) { |
| 368 |
m_qmlSurface->setActiveFocus(focused); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
void MirSurfaceItem::updateMirSurfaceExposure() |
| 373 |
{ |
| 374 |
if (!m_qmlSurface) return; |
| 375 |
|
| 376 |
m_qmlSurface->setViewExposure((qintptr)this, isVisible()); |
| 377 |
} |
| 378 |
|
| 379 |
void MirSurfaceItem::setConsumesInput(bool value) |
| 380 |
{ |
| 381 |
if (m_consumesInput != value) { |
| 382 |
m_consumesInput = value; |
| 383 |
Q_EMIT consumesInputChanged(value); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
int MirSurfaceItem::surfaceWidth() const |
| 388 |
{ |
| 389 |
return m_surfaceWidth; |
| 390 |
} |
| 391 |
|
| 392 |
void MirSurfaceItem::setSurfaceWidth(int value) |
| 393 |
{ |
| 394 |
// qDebug() << "setSurfaceWidth called" << value; |
| 395 |
if (value != -1 && m_surfaceWidth != value) { |
| 396 |
m_surfaceWidth = value; |
| 397 |
Q_EMIT surfaceWidthChanged(m_surfaceWidth); |
| 398 |
updateSurfaceSize(); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
int MirSurfaceItem::surfaceHeight() const |
| 403 |
{ |
| 404 |
return m_surfaceHeight; |
| 405 |
} |
| 406 |
|
| 407 |
void MirSurfaceItem::setSurfaceHeight(int value) |
| 408 |
{ |
| 409 |
if (value != -1 && m_surfaceHeight != value) { |
| 410 |
m_surfaceHeight = value; |
| 411 |
Q_EMIT surfaceHeightChanged(m_surfaceHeight); |
| 412 |
updateSurfaceSize(); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
void MirSurfaceItem::updateSurfaceSize() |
| 417 |
{ |
| 418 |
if (m_qmlSurface && m_surfaceWidth > 0 && m_surfaceHeight > 0) { |
| 419 |
m_qmlSurface->resize(m_surfaceWidth, m_surfaceHeight); |
| 420 |
if (m_qmlItem) { |
| 421 |
if (m_fillMode == FillMode::Stretch) { |
| 422 |
m_qmlItem->setWidth(width()); |
| 423 |
m_qmlItem->setHeight(height()); |
| 424 |
} else { |
| 425 |
m_qmlItem->setWidth(m_surfaceWidth); |
| 426 |
m_qmlItem->setHeight(m_surfaceHeight); |
| 427 |
} |
| 428 |
} |
| 429 |
qDebug() << this << "setting implicitsize" << m_surfaceWidth << m_surfaceHeight; |
| 430 |
setImplicitSize(m_surfaceWidth, m_surfaceHeight); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
void MirSurfaceItem::setFillMode(FillMode value) |
| 435 |
{ |
| 436 |
if (value != m_fillMode) { |
| 437 |
m_fillMode = value; |
| 438 |
Q_EMIT fillModeChanged(m_fillMode); |
| 439 |
} |
| 440 |
} |