source: trunk/demos/qtdemo/mainwindow.cpp@ 224

Last change on this file since 224 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 15.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "mainwindow.h"
43#include "menumanager.h"
44#include "colors.h"
45#include "dockitem.h"
46#include "demotextitem.h"
47#include "imageitem.h"
48#include "demoitem.h"
49#include "demoscene.h"
50
51#ifndef QT_NO_OPENGL
52 #include <QGLWidget>
53#endif
54//#define QT_NO_OPENGL
55
56MainWindow::MainWindow(QWidget *parent) : QGraphicsView(parent), updateTimer(this)
57{
58 this->currentFps = Colors::fps;
59 this->loop = false;
60 this->fpsMedian = -1;
61 this->fpsLabel = 0;
62 this->pausedLabel = 0;
63 this->doneAdapt = false;
64 this->useTimer = false;
65 this->updateTimer.setSingleShot(true);
66 this->trolltechLogo = 0;
67 this->qtLogo = 0;
68 this->setupWidget();
69 this->setupScene();
70 this->setupSceneItems();
71 this->drawBackgroundToPixmap();
72}
73
74MainWindow::~MainWindow()
75{
76 delete this->trolltechLogo;
77 delete this->qtLogo;
78}
79
80void MainWindow::setupWidget()
81{
82 QRect screenRect = QApplication::desktop()->screenGeometry(QApplication::desktop()->primaryScreen());
83 QRect windowRect(0, 0, 800, 600);
84 if (screenRect.width() < 800)
85 windowRect.setWidth(screenRect.width());
86 if (screenRect.height() < 600)
87 windowRect.setHeight(screenRect.height());
88 windowRect.moveCenter(screenRect.center());
89 this->setGeometry(windowRect);
90 this->setMinimumSize(80, 60);
91 setWindowTitle(tr("Qt Examples and Demos"));
92 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
93 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
94 setFrameStyle(QFrame::NoFrame);
95 this->setRenderingSystem();
96 connect(&this->updateTimer, SIGNAL(timeout()), this, SLOT(tick()));
97}
98
99void MainWindow::setRenderingSystem()
100{
101 QWidget *viewport = 0;
102
103 if (Colors::direct3dRendering){
104 viewport->setAttribute(Qt::WA_MSWindowsUseDirect3D);
105 setCacheMode(QGraphicsView::CacheNone);
106 if (Colors::verbose)
107 qDebug() << "- using Direct3D";
108 }
109#ifndef QT_NO_OPENGL
110 else if (Colors::openGlRendering){
111 QGLWidget *glw = new QGLWidget(QGLFormat(QGL::SampleBuffers));
112 if (Colors::noScreenSync)
113 glw->format().setSwapInterval(0);
114 glw->setAutoFillBackground(false);
115 viewport = glw;
116 setCacheMode(QGraphicsView::CacheNone);
117 if (Colors::verbose)
118 qDebug() << "- using OpenGL";
119 }
120#endif
121 else{ // software rendering
122 viewport = new QWidget;
123 setCacheMode(QGraphicsView::CacheBackground);
124 if (Colors::verbose)
125 qDebug() << "- using software rendering";
126 }
127
128 setViewport(viewport);
129}
130
131void MainWindow::start()
132{
133 this->switchTimerOnOff(true);
134 this->demoStartTime.restart();
135 MenuManager::instance()->itemSelected(MenuManager::ROOT, Colors::rootMenuName);
136 if (Colors::verbose)
137 qDebug("- starting demo");
138}
139
140void MainWindow::enableMask(bool enable)
141{
142 if (!enable || Colors::noWindowMask)
143 this->clearMask();
144 else {
145 QPolygon region;
146 region.setPoints(9,
147 // north side:
148 0, 0,
149 800, 0,
150 // east side:
151 // 800, 70,
152 // 790, 90,
153 // 790, 480,
154 // 800, 500,
155 800, 600,
156 // south side:
157 700, 600,
158 670, 590,
159 130, 590,
160 100, 600,
161 0, 600,
162 // west side:
163 // 0, 550,
164 // 10, 530,
165 // 10, 520,
166 // 0, 520,
167 0, 0);
168 this->setMask(QRegion(region));
169 }
170}
171
172void MainWindow::setupScene()
173{
174 this->scene = new DemoScene(this);
175 this->scene->setSceneRect(0, 0, 800, 600);
176 setScene(this->scene);
177 this->scene->setItemIndexMethod(QGraphicsScene::NoIndex);
178}
179
180void MainWindow::drawItems(QPainter *painter, int numItems, QGraphicsItem **items, const QStyleOptionGraphicsItem* options)
181{
182 QGraphicsView::drawItems(painter, numItems, items, options);
183}
184
185void MainWindow::switchTimerOnOff(bool on)
186{
187 bool ticker = MenuManager::instance()->ticker && MenuManager::instance()->ticker->scene();
188 if (ticker)
189 MenuManager::instance()->ticker->tickOnPaint = !on || Colors::noTimerUpdate;
190
191 if (on && !Colors::noTimerUpdate){
192 this->useTimer = true;
193 this->setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
194 this->fpsTime = QTime::currentTime();
195 this->updateTimer.start(int(1000 / Colors::fps));
196 }
197 else{
198 this->useTimer = false;
199 this->updateTimer.stop();
200 if (Colors::softwareRendering)
201 if (Colors::noTicker)
202 this->setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate);
203 else
204 this->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
205 else