demos/chip/view.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the demonstration applications of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "view.h"
00025 
00026 #include <QtGui>
00027 #include <QtOpenGL>
00028 
00029 #include <math.h>
00030 
00031 View::View(const QString &name, QWidget *parent)
00032     : QFrame(parent)
00033 {
00034     setFrameStyle(Sunken | StyledPanel);
00035     graphicsView = new QGraphicsView;
00036     graphicsView->setRenderHint(QPainter::Antialiasing, false);
00037     graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
00038 
00039     int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
00040     QSize iconSize(size, size);
00041 
00042     QToolButton *zoomInIcon = new QToolButton;
00043     zoomInIcon->setAutoRepeat(true);
00044     zoomInIcon->setAutoRepeatInterval(33);
00045     zoomInIcon->setAutoRepeatDelay(0);
00046     zoomInIcon->setIcon(QPixmap(":/zoomin.png"));
00047     zoomInIcon->setIconSize(iconSize);
00048     QToolButton *zoomOutIcon = new QToolButton;
00049     zoomOutIcon->setAutoRepeat(true);
00050     zoomOutIcon->setAutoRepeatInterval(33);
00051     zoomOutIcon->setAutoRepeatDelay(0);
00052     zoomOutIcon->setIcon(QPixmap(":/zoomout.png"));
00053     zoomOutIcon->setIconSize(iconSize);
00054     zoomSlider = new QSlider;
00055     zoomSlider->setMinimum(0);
00056     zoomSlider->setMaximum(500);
00057     zoomSlider->setValue(250);
00058     zoomSlider->setTickPosition(QSlider::TicksRight);
00059 
00060     // Zoom slider layout
00061     QVBoxLayout *zoomSliderLayout = new QVBoxLayout;
00062     zoomSliderLayout->addWidget(zoomInIcon);
00063     zoomSliderLayout->addWidget(zoomSlider);
00064     zoomSliderLayout->addWidget(zoomOutIcon);
00065 
00066     QToolButton *rotateLeftIcon = new QToolButton;
00067     rotateLeftIcon->setIcon(QPixmap(":/rotateleft.png"));
00068     rotateLeftIcon->setIconSize(iconSize);
00069     QToolButton *rotateRightIcon = new QToolButton;
00070     rotateRightIcon->setIcon(QPixmap(":/rotateright.png"));
00071     rotateRightIcon->setIconSize(iconSize);
00072     rotateSlider = new QSlider;
00073     rotateSlider->setOrientation(Qt::Horizontal);
00074     rotateSlider->setMinimum(-360);
00075     rotateSlider->setMaximum(360);
00076     rotateSlider->setValue(0);
00077     rotateSlider->setTickPosition(QSlider::TicksBelow);
00078 
00079     // Rotate slider layout
00080     QHBoxLayout *rotateSliderLayout = new QHBoxLayout;
00081     rotateSliderLayout->addWidget(rotateLeftIcon);
00082     rotateSliderLayout->addWidget(rotateSlider);
00083     rotateSliderLayout->addWidget(rotateRightIcon);
00084 
00085     resetButton = new QToolButton;
00086     resetButton->setText(tr("0"));
00087     resetButton->setEnabled(false);
00088 
00089     // Label layout
00090     QHBoxLayout *labelLayout = new QHBoxLayout;
00091     label = new QLabel(name);
00092     antialiasButton = new QToolButton;
00093     antialiasButton->setText(tr("Antialiasing"));
00094     antialiasButton->setCheckable(true);
00095     antialiasButton->setChecked(false);
00096     openGlButton = new QToolButton;
00097     openGlButton->setText(tr("OpenGL"));
00098     openGlButton->setCheckable(true);
00099     openGlButton->setEnabled(QGLFormat::hasOpenGL());
00100     printButton = new QToolButton;
00101     printButton->setIcon(QIcon(QPixmap(":/fileprint.png")));
00102 
00103     labelLayout->addWidget(label);
00104     labelLayout->addStretch();
00105     labelLayout->addWidget(antialiasButton);
00106     labelLayout->addWidget(openGlButton);
00107     labelLayout->addWidget(printButton);
00108 
00109     QGridLayout *topLayout = new QGridLayout;
00110     topLayout->addLayout(labelLayout, 0, 0);
00111     topLayout->addWidget(graphicsView, 1, 0);
00112     topLayout->addLayout(zoomSliderLayout, 1, 1);
00113     topLayout->addLayout(rotateSliderLayout, 2, 0);
00114     topLayout->addWidget(resetButton, 2, 1);
00115     setLayout(topLayout);
00116 
00117     connect(resetButton, SIGNAL(clicked()), this, SLOT(resetView()));
00118     connect(zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
00119     connect(rotateSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
00120     connect(graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled()));
00121     connect(graphicsView->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled()));
00122     connect(antialiasButton, SIGNAL(toggled(bool)), this, SLOT(toggleAntialiasing()));
00123     connect(openGlButton, SIGNAL(toggled(bool)), this, SLOT(toggleOpenGL()));
00124     connect(rotateLeftIcon, SIGNAL(clicked()), this, SLOT(rotateLeft()));
00125     connect(rotateRightIcon, SIGNAL(clicked()), this, SLOT(rotateRight()));
00126     connect(zoomInIcon, SIGNAL(clicked()), this, SLOT(zoomIn()));
00127     connect(zoomOutIcon, SIGNAL(clicked()), this, SLOT(zoomOut()));
00128     connect(printButton, SIGNAL(clicked()), this, SLOT(print()));
00129 
00130     setupMatrix();
00131 }
00132 
00133 QGraphicsView *View::view() const
00134 {
00135     return graphicsView;
00136 }
00137 
00138 void View::resetView()
00139 {
00140     zoomSlider->setValue(250);
00141     rotateSlider->setValue(0);
00142     setupMatrix();
00143     graphicsView->ensureVisible(QRectF(0, 0, 0, 0));
00144 
00145     resetButton->setEnabled(false);
00146 }
00147 
00148 void View::setResetButtonEnabled()
00149 {
00150     resetButton->setEnabled(true);
00151 }
00152 
00153 void View::setupMatrix()
00154 {
00155     qreal scale = ::pow(2.0, (zoomSlider->value() - 250) / 50.0);
00156 
00157     QMatrix matrix;
00158     matrix.scale(scale, scale);
00159     matrix.rotate(rotateSlider->value());
00160 
00161     graphicsView->setMatrix(matrix);
00162     setResetButtonEnabled();
00163 }
00164 
00165 void View::toggleOpenGL()
00166 {
00167     graphicsView->setViewport(openGlButton->isChecked() ? new QGLWidget(QGLFormat(QGL::SampleBuffers)) : new QWidget);
00168 }
00169 
00170 void View::toggleAntialiasing()
00171 {
00172     graphicsView->setRenderHint(QPainter::Antialiasing, antialiasButton->isChecked());
00173 }
00174 
00175 void View::print()
00176 {
00177     QPrinter printer;
00178     QPrintDialog dialog(&printer, this);
00179     if (dialog.exec() == QDialog::Accepted) {
00180         QPainter painter(&printer);
00181         graphicsView->render(&painter);
00182     }
00183 }
00184 
00185 void View::zoomIn()
00186 {
00187     zoomSlider->setValue(zoomSlider->value() + 1);
00188 }
00189 
00190 void View::zoomOut()
00191 {
00192     zoomSlider->setValue(zoomSlider->value() - 1);
00193 }
00194 
00195 void View::rotateLeft()
00196 {
00197     rotateSlider->setValue(rotateSlider->value() - 10);
00198 }
00199 
00200 void View::rotateRight()
00201 {
00202     rotateSlider->setValue(rotateSlider->value() + 10);
00203 }
00204 

Generated on Thu Mar 15 11:52:29 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1