source: trunk/examples/opengl/grabber/mainwindow.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 7.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43#include <QtOpenGL>
44
45#include "glwidget.h"
46#include "mainwindow.h"
47
48MainWindow::MainWindow()
49{
50 centralWidget = new QWidget;
51 setCentralWidget(centralWidget);
52
53 glWidget = new GLWidget;
54 pixmapLabel = new QLabel;
55
56 glWidgetArea = new QScrollArea;
57 glWidgetArea->setWidget(glWidget);
58 glWidgetArea->setWidgetResizable(true);
59 glWidgetArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
60 glWidgetArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
61 glWidgetArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
62 glWidgetArea->setMinimumSize(50, 50);
63
64 pixmapLabelArea = new QScrollArea;
65 pixmapLabelArea->setWidget(pixmapLabel);
66 pixmapLabelArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
67 pixmapLabelArea->setMinimumSize(50, 50);
68
69 xSlider = createSlider(SIGNAL(xRotationChanged(int)),
70 SLOT(setXRotation(int)));
71 ySlider = createSlider(SIGNAL(yRotationChanged(int)),
72 SLOT(setYRotation(int)));
73 zSlider = createSlider(SIGNAL(zRotationChanged(int)),
74 SLOT(setZRotation(int)));
75
76 createActions();
77 createMenus();
78
79 QGridLayout *centralLayout = new QGridLayout;
80 centralLayout->addWidget(glWidgetArea, 0, 0);
81 centralLayout->addWidget(pixmapLabelArea, 0, 1);
82 centralLayout->addWidget(xSlider, 1, 0, 1, 2);
83 centralLayout->addWidget(ySlider, 2, 0, 1, 2);
84 centralLayout->addWidget(zSlider, 3, 0, 1, 2);
85 centralWidget->setLayout(centralLayout);
86
87 xSlider->setValue(15 * 16);
88 ySlider->setValue(345 * 16);
89 zSlider->setValue(0 * 16);
90
91 setWindowTitle(tr("Grabber"));
92 resize(400, 300);
93}
94
95void MainWindow::renderIntoPixmap()
96{
97 QSize size = getSize();
98 if (size.isValid()) {
99 QPixmap pixmap = glWidget->renderPixmap(size.width(), size.height());
100 setPixmap(pixmap);
101 }
102}
103
104void MainWindow::grabFrameBuffer()
105{
106 QImage image = glWidget->grabFrameBuffer();
107 setPixmap(QPixmap::fromImage(image));
108}
109
110void MainWindow::clearPixmap()
111{
112 setPixmap(QPixmap());
113}
114
115void MainWindow::about()
116{
117 QMessageBox::about(this, tr("About Grabber"),
118 tr("The <b>Grabber</b> example demonstrates two approaches for "
119 "rendering OpenGL into a Qt pixmap."));
120}
121
122void MainWindow::createActions()
123{
124 renderIntoPixmapAct = new QAction(tr("&Render into Pixmap..."), this);
125 renderIntoPixmapAct->setShortcut(tr("Ctrl+R"));
126 connect(renderIntoPixmapAct, SIGNAL(triggered()),
127 this, SLOT(renderIntoPixmap()));
128
129 grabFrameBufferAct = new QAction(tr("&Grab Frame Buffer"), this);
130 grabFrameBufferAct->setShortcut(tr("Ctrl+G"));
131 connect(grabFrameBufferAct, SIGNAL(triggered()),
132 this, SLOT(grabFrameBuffer()));
133
134 clearPixmapAct = new QAction(tr("&Clear Pixmap"), this);
135 clearPixmapAct->setShortcut(tr("Ctrl+L"));
136 connect(clearPixmapAct, SIGNAL(triggered()), this, SLOT(clearPixmap()));
137
138 exitAct = new QAction(tr("E&xit"), this);
139 exitAct->setShortcuts(QKeySequence::Quit);
140 connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
141
142 aboutAct = new QAction(tr("&About"), this);
143 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
144
145 aboutQtAct = new QAction(tr("About &Qt"), this);
146 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
147}
148
149void MainWindow::createMenus()
150{
151 fileMenu = menuBar()->addMenu(tr("&File"));
152 fileMenu->addAction(renderIntoPixmapAct);
153 fileMenu->addAction(grabFrameBufferAct);
154 fileMenu->addAction(clearPixmapAct);
155 fileMenu->addSeparator();
156 fileMenu->addAction(exitAct);
157
158 helpMenu = menuBar()->addMenu(tr("&Help"));
159 helpMenu->addAction(aboutAct);
160 helpMenu->addAction(aboutQtAct);
161}
162
163QSlider *MainWindow::createSlider(const char *changedSignal,
164 const char *setterSlot)
165{
166 QSlider *slider = new QSlider(Qt::Horizontal);
167 slider->setRange(0, 360 * 16);
168 slider->setSingleStep(16);
169 slider->setPageStep(15 * 16);
170 slider->setTickInterval(15 * 16);
171 slider->setTickPosition(QSlider::TicksRight);
172 connect(slider, SIGNAL(valueChanged(int)), glWidget, setterSlot);
173 connect(glWidget, changedSignal, slider, SLOT(setValue(int)));
174 return slider;
175}
176
177void MainWindow::setPixmap(const QPixmap &pixmap)
178{
179 pixmapLabel->setPixmap(pixmap);
180 QSize size = pixmap.size();
181 if (size - QSize(1, 0) == pixmapLabelArea->maximumViewportSize())
182 size -= QSize(1, 0);
183 pixmapLabel->resize(size);
184}
185
186QSize MainWindow::getSize()
187{
188 bool ok;
189 QString text = QInputDialog::getText(this, tr("Grabber"),
190 tr("Enter pixmap size:"),
191 QLineEdit::Normal,
192 tr("%1 x %2").arg(glWidget->width())
193 .arg(glWidget->height()),
194 &ok);
195 if (!ok)
196 return QSize();
197
198 QRegExp regExp(tr("([0-9]+) *x *([0-9]+)"));
199 if (regExp.exactMatch(text)) {
200 int width = regExp.cap(1).toInt();
201 int height = regExp.cap(2).toInt();
202 if (width > 0 && width < 2048 && height > 0 && height < 2048)
203 return QSize(width, height);
204 }
205
206 return glWidget->size();
207}
Note: See TracBrowser for help on using the repository browser.