Refactor common POSIX support out of individual qplatformdefs.h
[qt-netbsd.git] / demos / chip / view.cpp
blob7af30740a693b65a1117e0bb74e405a86f61d854
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 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 demonstration applications 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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include "view.h"
44 #include <QtGui>
45 #ifndef QT_NO_OPENGL
46 #include <QtOpenGL>
47 #endif
49 #include <qmath.h>
51 View::View(const QString &name, QWidget *parent)
52 : QFrame(parent)
54 setFrameStyle(Sunken | StyledPanel);
55 graphicsView = new QGraphicsView;
56 graphicsView->setRenderHint(QPainter::Antialiasing, false);
57 graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
58 graphicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
59 graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
61 int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
62 QSize iconSize(size, size);
64 QToolButton *zoomInIcon = new QToolButton;
65 zoomInIcon->setAutoRepeat(true);
66 zoomInIcon->setAutoRepeatInterval(33);
67 zoomInIcon->setAutoRepeatDelay(0);
68 zoomInIcon->setIcon(QPixmap(":/zoomin.png"));
69 zoomInIcon->setIconSize(iconSize);
70 QToolButton *zoomOutIcon = new QToolButton;
71 zoomOutIcon->setAutoRepeat(true);
72 zoomOutIcon->setAutoRepeatInterval(33);
73 zoomOutIcon->setAutoRepeatDelay(0);
74 zoomOutIcon->setIcon(QPixmap(":/zoomout.png"));
75 zoomOutIcon->setIconSize(iconSize);
76 zoomSlider = new QSlider;
77 zoomSlider->setMinimum(0);
78 zoomSlider->setMaximum(500);
79 zoomSlider->setValue(250);
80 zoomSlider->setTickPosition(QSlider::TicksRight);
82 // Zoom slider layout
83 QVBoxLayout *zoomSliderLayout = new QVBoxLayout;
84 zoomSliderLayout->addWidget(zoomInIcon);
85 zoomSliderLayout->addWidget(zoomSlider);
86 zoomSliderLayout->addWidget(zoomOutIcon);
88 QToolButton *rotateLeftIcon = new QToolButton;
89 rotateLeftIcon->setIcon(QPixmap(":/rotateleft.png"));
90 rotateLeftIcon->setIconSize(iconSize);
91 QToolButton *rotateRightIcon = new QToolButton;
92 rotateRightIcon->setIcon(QPixmap(":/rotateright.png"));
93 rotateRightIcon->setIconSize(iconSize);
94 rotateSlider = new QSlider;
95 rotateSlider->setOrientation(Qt::Horizontal);
96 rotateSlider->setMinimum(-360);
97 rotateSlider->setMaximum(360);
98 rotateSlider->setValue(0);
99 rotateSlider->setTickPosition(QSlider::TicksBelow);
101 // Rotate slider layout
102 QHBoxLayout *rotateSliderLayout = new QHBoxLayout;
103 rotateSliderLayout->addWidget(rotateLeftIcon);
104 rotateSliderLayout->addWidget(rotateSlider);
105 rotateSliderLayout->addWidget(rotateRightIcon);
107 resetButton = new QToolButton;
108 resetButton->setText(tr("0"));
109 resetButton->setEnabled(false);
111 // Label layout
112 QHBoxLayout *labelLayout = new QHBoxLayout;
113 label = new QLabel(name);
114 antialiasButton = new QToolButton;
115 antialiasButton->setText(tr("Antialiasing"));
116 antialiasButton->setCheckable(true);
117 antialiasButton->setChecked(false);
118 openGlButton = new QToolButton;
119 openGlButton->setText(tr("OpenGL"));
120 openGlButton->setCheckable(true);
121 #ifndef QT_NO_OPENGL
122 openGlButton->setEnabled(QGLFormat::hasOpenGL());
123 #else
124 openGlButton->setEnabled(false);
125 #endif
126 printButton = new QToolButton;
127 printButton->setIcon(QIcon(QPixmap(":/fileprint.png")));
129 labelLayout->addWidget(label);
130 labelLayout->addStretch();
131 labelLayout->addWidget(antialiasButton);
132 labelLayout->addWidget(openGlButton);
133 labelLayout->addWidget(printButton);
135 QGridLayout *topLayout = new QGridLayout;
136 topLayout->addLayout(labelLayout, 0, 0);
137 topLayout->addWidget(graphicsView, 1, 0);
138 topLayout->addLayout(zoomSliderLayout, 1, 1);
139 topLayout->addLayout(rotateSliderLayout, 2, 0);
140 topLayout->addWidget(resetButton, 2, 1);
141 setLayout(topLayout);
143 connect(resetButton, SIGNAL(clicked()), this, SLOT(resetView()));
144 connect(zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
145 connect(rotateSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
146 connect(graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled()));
147 connect(graphicsView->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled()));
148 connect(antialiasButton, SIGNAL(toggled(bool)), this, SLOT(toggleAntialiasing()));
149 connect(openGlButton, SIGNAL(toggled(bool)), this, SLOT(toggleOpenGL()));
150 connect(rotateLeftIcon, SIGNAL(clicked()), this, SLOT(rotateLeft()));
151 connect(rotateRightIcon, SIGNAL(clicked()), this, SLOT(rotateRight()));
152 connect(zoomInIcon, SIGNAL(clicked()), this, SLOT(zoomIn()));
153 connect(zoomOutIcon, SIGNAL(clicked()), this, SLOT(zoomOut()));
154 connect(printButton, SIGNAL(clicked()), this, SLOT(print()));
156 setupMatrix();
159 QGraphicsView *View::view() const
161 return graphicsView;
164 void View::resetView()
166 zoomSlider->setValue(250);
167 rotateSlider->setValue(0);
168 setupMatrix();
169 graphicsView->ensureVisible(QRectF(0, 0, 0, 0));
171 resetButton->setEnabled(false);
174 void View::setResetButtonEnabled()
176 resetButton->setEnabled(true);
179 void View::setupMatrix()
181 qreal scale = qPow(qreal(2), (zoomSlider->value() - 250) / qreal(50));
183 QMatrix matrix;
184 matrix.scale(scale, scale);
185 matrix.rotate(rotateSlider->value());
187 graphicsView->setMatrix(matrix);
188 setResetButtonEnabled();
191 void View::toggleOpenGL()
193 #ifndef QT_NO_OPENGL
194 graphicsView->setViewport(openGlButton->isChecked() ? new QGLWidget(QGLFormat(QGL::SampleBuffers)) : new QWidget);
195 #endif
198 void View::toggleAntialiasing()
200 graphicsView->setRenderHint(QPainter::Antialiasing, antialiasButton->isChecked());
203 void View::print()
205 #ifndef QT_NO_PRINTER
206 QPrinter printer;
207 QPrintDialog dialog(&printer, this);
208 if (dialog.exec() == QDialog::Accepted) {
209 QPainter painter(&printer);
210 graphicsView->render(&painter);
212 #endif
215 void View::zoomIn()
217 zoomSlider->setValue(zoomSlider->value() + 1);
220 void View::zoomOut()
222 zoomSlider->setValue(zoomSlider->value() - 1);
225 void View::rotateLeft()
227 rotateSlider->setValue(rotateSlider->value() - 10);
230 void View::rotateRight()
232 rotateSlider->setValue(rotateSlider->value() + 10);