~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to examples/opengl/grabber/mainwindow.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the examples of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:BSD$
 
9
** You may use this file under the terms of the BSD license as follows:
 
10
**
 
11
** "Redistribution and use in source and binary forms, with or without
 
12
** modification, are permitted provided that the following conditions are
 
13
** met:
 
14
**   * Redistributions of source code must retain the above copyright
 
15
**     notice, this list of conditions and the following disclaimer.
 
16
**   * Redistributions in binary form must reproduce the above copyright
 
17
**     notice, this list of conditions and the following disclaimer in
 
18
**     the documentation and/or other materials provided with the
 
19
**     distribution.
 
20
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
 
21
**     of its contributors may be used to endorse or promote products derived
 
22
**     from this software without specific prior written permission.
 
23
**
 
24
**
 
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 
36
**
 
37
** $QT_END_LICENSE$
 
38
**
 
39
****************************************************************************/
 
40
 
 
41
#include "glwidget.h"
 
42
#include "mainwindow.h"
 
43
 
 
44
#include <QtWidgets>
 
45
 
 
46
MainWindow::MainWindow()
 
47
{
 
48
    centralWidget = new QWidget;
 
49
    setCentralWidget(centralWidget);
 
50
 
 
51
    glWidget = new GLWidget;
 
52
    pixmapLabel = new QLabel;
 
53
 
 
54
    glWidgetArea = new QScrollArea;
 
55
    glWidgetArea->setWidget(glWidget);
 
56
    glWidgetArea->setWidgetResizable(true);
 
57
    glWidgetArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
58
    glWidgetArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
59
    glWidgetArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
 
60
    glWidgetArea->setMinimumSize(50, 50);
 
61
 
 
62
    pixmapLabelArea = new QScrollArea;
 
63
    pixmapLabelArea->setWidget(pixmapLabel);
 
64
    pixmapLabelArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
 
65
    pixmapLabelArea->setMinimumSize(50, 50);
 
66
 
 
67
    xSlider = createSlider(SIGNAL(xRotationChanged(int)),
 
68
                           SLOT(setXRotation(int)));
 
69
    ySlider = createSlider(SIGNAL(yRotationChanged(int)),
 
70
                           SLOT(setYRotation(int)));
 
71
    zSlider = createSlider(SIGNAL(zRotationChanged(int)),
 
72
                           SLOT(setZRotation(int)));
 
73
 
 
74
    createActions();
 
75
    createMenus();
 
76
 
 
77
    QGridLayout *centralLayout = new QGridLayout;
 
78
    centralLayout->addWidget(glWidgetArea, 0, 0);
 
79
    centralLayout->addWidget(pixmapLabelArea, 0, 1);
 
80
    centralLayout->addWidget(xSlider, 1, 0, 1, 2);
 
81
    centralLayout->addWidget(ySlider, 2, 0, 1, 2);
 
82
    centralLayout->addWidget(zSlider, 3, 0, 1, 2);
 
83
    centralWidget->setLayout(centralLayout);
 
84
 
 
85
    xSlider->setValue(15 * 16);
 
86
    ySlider->setValue(345 * 16);
 
87
    zSlider->setValue(0 * 16);
 
88
 
 
89
    setWindowTitle(tr("Grabber"));
 
90
    resize(400, 300);
 
91
}
 
92
 
 
93
void MainWindow::renderIntoPixmap()
 
94
{
 
95
    QSize size = getSize();
 
96
    if (size.isValid()) {
 
97
        QPixmap pixmap = glWidget->renderPixmap(size.width(), size.height());
 
98
        setPixmap(pixmap);
 
99
    }
 
100
}
 
101
 
 
102
void MainWindow::grabFrameBuffer()
 
103
{
 
104
    QImage image = glWidget->grabFrameBuffer();
 
105
    setPixmap(QPixmap::fromImage(image));
 
106
}
 
107
 
 
108
void MainWindow::clearPixmap()
 
109
{
 
110
    setPixmap(QPixmap());
 
111
}
 
112
 
 
113
void MainWindow::about()
 
114
{
 
115
    QMessageBox::about(this, tr("About Grabber"),
 
116
            tr("The <b>Grabber</b> example demonstrates two approaches for "
 
117
               "rendering OpenGL into a Qt pixmap."));
 
118
}
 
119
 
 
120
void MainWindow::createActions()
 
121
{
 
122
    renderIntoPixmapAct = new QAction(tr("&Render into Pixmap..."), this);
 
123
    renderIntoPixmapAct->setShortcut(tr("Ctrl+R"));
 
124
    connect(renderIntoPixmapAct, SIGNAL(triggered()),
 
125
            this, SLOT(renderIntoPixmap()));
 
126
 
 
127
    grabFrameBufferAct = new QAction(tr("&Grab Frame Buffer"), this);
 
128
    grabFrameBufferAct->setShortcut(tr("Ctrl+G"));
 
129
    connect(grabFrameBufferAct, SIGNAL(triggered()),
 
130
            this, SLOT(grabFrameBuffer()));
 
131
 
 
132
    clearPixmapAct = new QAction(tr("&Clear Pixmap"), this);
 
133
    clearPixmapAct->setShortcut(tr("Ctrl+L"));
 
134
    connect(clearPixmapAct, SIGNAL(triggered()), this, SLOT(clearPixmap()));
 
135
 
 
136
    exitAct = new QAction(tr("E&xit"), this);
 
137
    exitAct->setShortcuts(QKeySequence::Quit);
 
138
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
 
139
 
 
140
    aboutAct = new QAction(tr("&About"), this);
 
141
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
 
142
 
 
143
    aboutQtAct = new QAction(tr("About &Qt"), this);
 
144
    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
145
}
 
146
 
 
147
void MainWindow::createMenus()
 
148
{
 
149
    fileMenu = menuBar()->addMenu(tr("&File"));
 
150
    fileMenu->addAction(renderIntoPixmapAct);
 
151
    fileMenu->addAction(grabFrameBufferAct);
 
152
    fileMenu->addAction(clearPixmapAct);
 
153
    fileMenu->addSeparator();
 
154
    fileMenu->addAction(exitAct);
 
155
 
 
156
    helpMenu = menuBar()->addMenu(tr("&Help"));
 
157
    helpMenu->addAction(aboutAct);
 
158
    helpMenu->addAction(aboutQtAct);
 
159
}
 
160
 
 
161
QSlider *MainWindow::createSlider(const char *changedSignal,
 
162
                                  const char *setterSlot)
 
163
{
 
164
    QSlider *slider = new QSlider(Qt::Horizontal);
 
165
    slider->setRange(0, 360 * 16);
 
166
    slider->setSingleStep(16);
 
167
    slider->setPageStep(15 * 16);
 
168
    slider->setTickInterval(15 * 16);
 
169
    slider->setTickPosition(QSlider::TicksRight);
 
170
    connect(slider, SIGNAL(valueChanged(int)), glWidget, setterSlot);
 
171
    connect(glWidget, changedSignal, slider, SLOT(setValue(int)));
 
172
    return slider;
 
173
}
 
174
 
 
175
void MainWindow::setPixmap(const QPixmap &pixmap)
 
176
{
 
177
    pixmapLabel->setPixmap(pixmap);
 
178
    QSize size = pixmap.size();
 
179
    if (size - QSize(1, 0) == pixmapLabelArea->maximumViewportSize())
 
180
        size -= QSize(1, 0);
 
181
    pixmapLabel->resize(size);
 
182
}
 
183
 
 
184
QSize MainWindow::getSize()
 
185
{
 
186
    bool ok;
 
187
    QString text = QInputDialog::getText(this, tr("Grabber"),
 
188
                                         tr("Enter pixmap size:"),
 
189
                                         QLineEdit::Normal,
 
190
                                         tr("%1 x %2").arg(glWidget->width())
 
191
                                                      .arg(glWidget->height()),
 
192
                                         &ok);
 
193
    if (!ok)
 
194
        return QSize();
 
195
 
 
196
    QRegExp regExp(tr("([0-9]+) *x *([0-9]+)"));
 
197
    if (regExp.exactMatch(text)) {
 
198
        int width = regExp.cap(1).toInt();
 
199
        int height = regExp.cap(2).toInt();
 
200
        if (width > 0 && width < 2048 && height > 0 && height < 2048)
 
201
            return QSize(width, height);
 
202
    }
 
203
 
 
204
    return glWidget->size();
 
205
}