1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtGui>
43
44 class DesktopView : public QGraphicsView
45 {
46 Q_OBJECT
47 public:
DesktopView()48 DesktopView()
49 : that(0)
50 {
51 scene = new QGraphicsScene;
52 setScene(scene);
53
54 QDesktopWidget *desktop = QApplication::desktop();
55 connect(desktop, SIGNAL(resized(int)), this, SLOT(updateScene()));
56 connect(desktop, SIGNAL(resized(int)), this, SLOT(desktopResized(int)));
57 connect(desktop, SIGNAL(workAreaResized(int)), this, SLOT(updateScene()));
58 connect(desktop, SIGNAL(workAreaResized(int)), this, SLOT(desktopWorkAreaResized(int)));
59 connect(desktop, SIGNAL(screenCountChanged(int)), this, SLOT(updateScene()));
60 connect(desktop, SIGNAL(screenCountChanged(int)), this, SLOT(desktopScreenCountChanged(int)));
61
62 updateScene();
63
64 QTransform transform;
65 transform.scale(0.25, 0.25);
66 setTransform(transform);
67
68 setBackgroundBrush(Qt::darkGray);
69 desktopScreenCountChanged(-1);
70 }
71
72 protected:
moveEvent(QMoveEvent * e)73 void moveEvent(QMoveEvent *e)
74 {
75 if (that) {
76 that->setRect(appRect());
77 scene->update();
78 }
79 QGraphicsView::moveEvent(e);
80 }
resizeEvent(QResizeEvent * e)81 void resizeEvent(QResizeEvent *e)
82 {
83 if (that) {
84 that->setRect(appRect());
85 }
86 QGraphicsView::resizeEvent(e);
87 }
88
89 private slots:
updateScene()90 void updateScene()
91 {
92 scene->clear();
93
94 const QDesktopWidget *desktop = QApplication::desktop();
95 const bool isVirtualDesktop = desktop->isVirtualDesktop();
96 const int homeScreen = desktop->screenNumber(this);
97
98 QRect sceneRect;
99 int screenCount = desktop->screenCount();
100 for (int s = 0; s < screenCount; ++s) {
101 const bool isPrimary = desktop->primaryScreen() == s;
102 const QRect screenRect = desktop->screenGeometry(s);
103 const QRect workRect = desktop->availableGeometry(s);
104 const QBrush fillBrush = palette().brush(isPrimary ? QPalette::Active : QPalette::Inactive, QPalette::Highlight);
105 QGraphicsRectItem *screen = new QGraphicsRectItem(0, 0, screenRect.width(), screenRect.height());
106
107 if (isVirtualDesktop) {
108 thatRoot = QPoint();
109 screen->setPos(screenRect.x(), screenRect.y());
110 } else {
111 // for non-virtual desktops we assume that screens are
112 // simply next to each other
113 if (s)
114 screen->setPos(sceneRect.right(), 0);
115 if (s == homeScreen)
116 thatRoot = screen->pos().toPoint();
117 }
118
119 screen->setBrush(fillBrush);
120 scene->addItem(screen);
121 sceneRect.setLeft(qMin(sceneRect.left(), screenRect.left()));
122 sceneRect.setRight(qMax(sceneRect.right(), screenRect.right()));
123 sceneRect.setTop(qMin(sceneRect.top(), screenRect.top()));
124 sceneRect.setBottom(qMax(sceneRect.bottom(), screenRect.bottom()));
125
126 QGraphicsRectItem *workArea = new QGraphicsRectItem(screen);
127 workArea->setRect(0, 0, workRect.width(), workRect.height());
128 workArea->setPos(workRect.x() - screenRect.x(), workRect.y() - screenRect.y());
129 workArea->setBrush(Qt::white);
130
131 QGraphicsSimpleTextItem *screenNumber = new QGraphicsSimpleTextItem(workArea);
132 screenNumber->setText(QString::number(s));
133 screenNumber->setPen(QPen(Qt::black, 1));
134 screenNumber->setBrush(fillBrush);
135 screenNumber->setFont(QFont("Arial Black", 18));
136 screenNumber->setTransform(QTransform().scale(10, 10));
137 screenNumber->setTransformOriginPoint(screenNumber->boundingRect().center());
138 QSizeF center = (workRect.size() - screenNumber->boundingRect().size()) / 2;
139 screenNumber->setPos(center.width(), center.height());
140
141 screen->show();
142 screen->setZValue(1);
143 }
144
145 if (isVirtualDesktop) {
146 QGraphicsRectItem *virtualDesktop = new QGraphicsRectItem;
147 virtualDesktop->setRect(sceneRect);
148 virtualDesktop->setPen(QPen(Qt::black));
149 virtualDesktop->setBrush(Qt::DiagCrossPattern);
150 scene->addItem(virtualDesktop);
151 virtualDesktop->setZValue(-1);
152 virtualDesktop->show();
153 }
154
155 that = new QGraphicsRectItem;
156 that->setBrush(Qt::red);
157 that->setOpacity(0.5);
158 that->setZValue(2);
159 that->setRect(appRect());
160 that->show();
161 scene->addItem(that);
162
163 scene->setSceneRect(sceneRect);
164 scene->update();
165 }
166
appRect() const167 QRect appRect() const
168 {
169 QRect rect = frameGeometry();
170 if (!QApplication::desktop()->isVirtualDesktop()) {
171 rect.translate(thatRoot);
172 }
173 return rect;
174 }
175
desktopResized(int screen)176 void desktopResized(int screen)
177 {
178 qDebug() << "Screen was resized: " << screen
179 << ", new size =" << QApplication::desktop()->screenGeometry(screen);
180 }
desktopWorkAreaResized(int screen)181 void desktopWorkAreaResized(int screen)
182 {
183 qDebug() << "Screen workarea was resized: " << screen
184 << ", new size =" << QApplication::desktop()->availableGeometry(screen);
185 }
desktopScreenCountChanged(int screenCount)186 void desktopScreenCountChanged(int screenCount)
187 {
188 QDesktopWidget *desktop = QApplication::desktop();
189 qDebug() << "";
190 if (screenCount != -1) {
191 qDebug() << "Screen count was changed to " << screenCount;
192 } else {
193 screenCount = desktop->screenCount();
194 qDebug() << "Screen count: " << screenCount;
195 }
196 for (int i = 0; i < screenCount; ++i) {
197 qDebug() << " #" << i << ": geometry =" << desktop->screenGeometry(i)
198 << "; available geometry =" << desktop->availableGeometry(i);
199 }
200 qDebug() << "";
201 }
202
203 private:
204 QGraphicsScene *scene;
205 QGraphicsRectItem *that;
206 QPoint thatRoot;
207 };
208
209 #include "main.moc"
210
main(int argc,char ** argv)211 int main(int argc, char **argv)
212 {
213 QApplication app(argc, argv);
214
215 DesktopView view;
216 view.show();
217
218 return app.exec();
219 }
220