view.cpp Example File

Find this file on Gitorious.

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2011 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. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41.  
  42. #include "view.h"
  43.  
  44. #include <QtGui>
  45. #ifndef QT_NO_OPENGL
  46. #include <QtOpenGL>
  47. #endif
  48.  
  49. #include <qmath.h>
  50.  
  51. View::View(const QString &name, QWidget *parent)
  52.     : QFrame(parent)
  53.  {
  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);
  60.  
  61.     int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
  62.     QSize iconSize(size, size);
  63.  
  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);
  81.  
  82.     // Zoom slider layout
  83.     QVBoxLayout *zoomSliderLayout = new QVBoxLayout;
  84.     zoomSliderLayout->addWidget(zoomInIcon);
  85.     zoomSliderLayout->addWidget(zoomSlider);
  86.     zoomSliderLayout->addWidget(zoomOutIcon);
  87.  
  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);
  100.  
  101.     // Rotate slider layout
  102.     QHBoxLayout *rotateSliderLayout = new QHBoxLayout;
  103.     rotateSliderLayout->addWidget(rotateLeftIcon);
  104.     rotateSliderLayout->addWidget(rotateSlider);
  105.     rotateSliderLayout->addWidget(rotateRightIcon);
  106.  
  107.     resetButton = new QToolButton;
  108.     resetButton->setText(tr("0"));
  109.     resetButton->setEnabled(false);
  110.  
  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")));
  128.  
  129.     labelLayout->addWidget(label);
  130.     labelLayout->addStretch();
  131.     labelLayout->addWidget(antialiasButton);
  132.     labelLayout->addWidget(openGlButton);
  133.     labelLayout->addWidget(printButton);
  134.  
  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);
  142.  
  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()));
  155.  
  156.     setupMatrix();
  157. }
  158.  
  159. QGraphicsView *View::view() const
  160.  {
  161.     return graphicsView;
  162. }
  163.  
  164. void View::resetView()
  165.  {
  166.     zoomSlider->setValue(250);
  167.     rotateSlider->setValue(0);
  168.     setupMatrix();
  169.     graphicsView->ensureVisible(QRectF(0, 0, 0, 0));
  170.  
  171.     resetButton->setEnabled(false);
  172. }
  173.  
  174. void View::setResetButtonEnabled()
  175.  {
  176.     resetButton->setEnabled(true);
  177. }
  178.  
  179. void View::setupMatrix()
  180.  {
  181.     qreal scale = qPow(qreal(2), (zoomSlider->value() - 250) / qreal(50));
  182.  
  183.     QMatrix matrix;
  184.     matrix.scale(scale, scale);
  185.     matrix.rotate(rotateSlider->value());
  186.  
  187.     graphicsView->setMatrix(matrix);
  188.     setResetButtonEnabled();
  189. }
  190.  
  191. void View::toggleOpenGL()
  192.  {
  193. #ifndef QT_NO_OPENGL
  194.     graphicsView->setViewport(openGlButton->isChecked() ? new QGLWidget(QGLFormat(QGL::SampleBuffers)) : new QWidget);
  195. #endif
  196. }
  197.  
  198. void View::toggleAntialiasing()
  199.  {
  200.     graphicsView->setRenderHint(QPainter::Antialiasing, antialiasButton->isChecked());
  201. }
  202.  
  203. void View::print()
  204.  {
  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);
  211.     }
  212. #endif
  213. }
  214.  
  215. void View::zoomIn()
  216.  {
  217.     zoomSlider->setValue(zoomSlider->value() + 1);
  218. }
  219.  
  220. void View::zoomOut()
  221.  {
  222.     zoomSlider->setValue(zoomSlider->value() - 1);
  223. }
  224.  
  225. void View::rotateLeft()
  226.  {
  227.     rotateSlider->setValue(rotateSlider->value() - 10);
  228. }
  229.  
  230. void View::rotateRight()
  231.  {
  232.     rotateSlider->setValue(rotateSlider->value() + 10);
  233. }
Notes provided by the Qt Community

No notes