tests/manual/qdesktopwidget/main.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 class DesktopView : public QGraphicsView
       
    45 {
       
    46     Q_OBJECT
       
    47 public:
       
    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:
       
    73     void moveEvent(QMoveEvent *e)
       
    74     {
       
    75         if (that) {
       
    76             that->setRect(appRect());
       
    77             scene->update();
       
    78         }
       
    79         QGraphicsView::moveEvent(e);
       
    80     }
       
    81     void resizeEvent(QResizeEvent *e)
       
    82     {
       
    83         if (that) {
       
    84             that->setRect(appRect());
       
    85         }
       
    86         QGraphicsView::resizeEvent(e);
       
    87     }
       
    88 
       
    89 private slots:
       
    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 
       
   167     QRect appRect() const
       
   168     {
       
   169         QRect rect = frameGeometry();
       
   170         if (!QApplication::desktop()->isVirtualDesktop()) {
       
   171             rect.translate(thatRoot);
       
   172         }
       
   173         return rect;
       
   174     }
       
   175 
       
   176     void desktopResized(int screen)
       
   177     {
       
   178         qDebug() << "Screen was resized: " << screen
       
   179                 << ", new size =" << QApplication::desktop()->screenGeometry(screen);
       
   180     }
       
   181     void desktopWorkAreaResized(int screen)
       
   182     {
       
   183         qDebug() << "Screen workarea was resized: " << screen
       
   184                 << ", new size =" << QApplication::desktop()->availableGeometry(screen);
       
   185     }
       
   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 
       
   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 }