Unity 8
ShellView.cpp
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  */
16 
17 #include "ShellView.h"
18 
19 // Qt
20 #include <QQmlContext>
21 #include <QQuickItem>
22 #include <QtQuick/private/qquickitem_p.h>
23 #include <QtQuick/private/qquickrectangle_p.h>
24 #include <QtQuick/private/qquicktext_p.h>
25 
26 // local
27 #include <paths.h>
28 
29 ShellView::ShellView(QQmlEngine *engine, QObject *qmlArgs)
30  : QQuickView(engine, nullptr)
31 {
32  setResizeMode(QQuickView::SizeRootObjectToView);
33  setColor("black");
34  setTitle(QStringLiteral("Unity8"));
35 
36  rootContext()->setContextProperty(QStringLiteral("applicationArguments"), qmlArgs);
37 
38  connect(this, &QQuickView::statusChanged, this, [this] {
39  if (status() == QQuickView::Error) {
40  QQuickRectangle *rect = new QQuickRectangle(contentItem());
41  rect->setColor(Qt::white);
42  QQuickItemPrivate::get(rect)->anchors()->setFill(contentItem());
43 
44  QString errorsString;
45  for(const QQmlError &e: errors()) {
46  errorsString += e.toString() + "\n";
47  }
48  QQuickText *text = new QQuickText(rect);
49  text->setColor(Qt::black);
50  text->setWrapMode(QQuickText::Wrap);
51  text->setText(QString("There was an error loading Unity8:\n%1").arg(errorsString));
52  QQuickItemPrivate::get(text)->anchors()->setFill(rect);
53  }
54  }
55  );
56 
57  QUrl source(::qmlDirectory() + "/OrientedShell.qml");
58  setSource(source);
59 
60  connect(this, &QWindow::widthChanged, this, &ShellView::onWidthChanged);
61  connect(this, &QWindow::heightChanged, this, &ShellView::onHeightChanged);
62 }
63 
64 void ShellView::onWidthChanged(int w)
65 {
66  // For good measure in case SizeRootObjectToView doesn't fulfill its promise.
67  //
68  // There's at least one situation that's know to leave the root object with an outdated size.
69  // (really looks like Qt bug)
70  // Happens when starting unity8 with an external monitor already connected.
71  // The QResizeEvent we get still has the size of the first screen and since the resize move is triggered
72  // from the resize event handler, the root item doesn't get resized.
73  // TODO: Confirm the Qt bug and submit a patch upstream
74  if (rootObject()) {
75  rootObject()->setWidth(w);
76  }
77 }
78 
79 void ShellView::onHeightChanged(int h)
80 {
81  // See comment in ShellView::onWidthChanged()
82  if (rootObject()) {
83  rootObject()->setHeight(h);
84  }
85 }