/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/#include "view.h"#include <QtGui>#ifndef QT_NO_OPENGL#include <QtOpenGL>#endif#include <qmath.h>void GraphicsView::wheelEvent(QWheelEvent*e)
{
if (e->modifiers() &Qt::ControlModifier) {
if (e->delta() >0)
view->zoomIn(6);
else
view->zoomOut(6);
e->accept();
} else {
QGraphicsView::wheelEvent(e);
}
}
View::View(constQString&name,QWidget*parent)
: QFrame(parent)
{
setFrameStyle(Sunken | StyledPanel);
graphicsView =new GraphicsView(this);
graphicsView->setRenderHint(QPainter::Antialiasing,false);
graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
graphicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
QSize iconSize(size, size);
QToolButton*zoomInIcon =newQToolButton;
zoomInIcon->setAutoRepeat(true);
zoomInIcon->setAutoRepeatInterval(33);
zoomInIcon->setAutoRepeatDelay(0);
zoomInIcon->setIcon(QPixmap(":/zoomin.png"));
zoomInIcon->setIconSize(iconSize);
QToolButton*zoomOutIcon =newQToolButton;
zoomOutIcon->setAutoRepeat(true);
zoomOutIcon->setAutoRepeatInterval(33);
zoomOutIcon->setAutoRepeatDelay(0);
zoomOutIcon->setIcon(QPixmap(":/zoomout.png"));
zoomOutIcon->setIconSize(iconSize);
zoomSlider =newQSlider;
zoomSlider->setMinimum(0);
zoomSlider->setMaximum(500);
zoomSlider->setValue(250);
zoomSlider->setTickPosition(QSlider::TicksRight);
// Zoom slider layoutQVBoxLayout*zoomSliderLayout =newQVBoxLayout;
zoomSliderLayout->addWidget(zoomInIcon);
zoomSliderLayout->addWidget(zoomSlider);
zoomSliderLayout->addWidget(zoomOutIcon);
QToolButton*rotateLeftIcon =newQToolButton;
rotateLeftIcon->setIcon(QPixmap(":/rotateleft.png"));
rotateLeftIcon->setIconSize(iconSize);
QToolButton*rotateRightIcon =newQToolButton;
rotateRightIcon->setIcon(QPixmap(":/rotateright.png"));
rotateRightIcon->setIconSize(iconSize);
rotateSlider =newQSlider;
rotateSlider->setOrientation(Qt::Horizontal);
rotateSlider->setMinimum(-360);
rotateSlider->setMaximum(360);
rotateSlider->setValue(0);
rotateSlider->setTickPosition(QSlider::TicksBelow);
// Rotate slider layoutQHBoxLayout*rotateSliderLayout =newQHBoxLayout;
rotateSliderLayout->addWidget(rotateLeftIcon);
rotateSliderLayout->addWidget(rotateSlider);
rotateSliderLayout->addWidget(rotateRightIcon);
resetButton =newQToolButton;
resetButton->setText(tr("0"));
resetButton->setEnabled(false);
// Label layoutQHBoxLayout*labelLayout =newQHBoxLayout;
label =newQLabel(name);
label2 =newQLabel(tr("Pointer Mode"));
selectModeButton =newQToolButton;
selectModeButton->setText(tr("Select"));
selectModeButton->setCheckable(true);
selectModeButton->setChecked(true);
dragModeButton =newQToolButton;
dragModeButton->setText(tr("Drag"));
dragModeButton->setCheckable(true);
dragModeButton->setChecked(false);
antialiasButton =newQToolButton;
antialiasButton->setText(tr("Antialiasing"));
antialiasButton->setCheckable(true);
antialiasButton->setChecked(false);
openGlButton =newQToolButton;
openGlButton->setText(tr("OpenGL"));
openGlButton->setCheckable(true);
#ifndef QT_NO_OPENGL
openGlButton->setEnabled(QGLFormat::hasOpenGL());
#else
openGlButton->setEnabled(false);
#endif
printButton =newQToolButton;
printButton->setIcon(QIcon(QPixmap(":/fileprint.png")));
QButtonGroup*pointerModeGroup =newQButtonGroup;
pointerModeGroup->setExclusive(true);
pointerModeGroup->addButton(selectModeButton);
pointerModeGroup->addButton(dragModeButton);
labelLayout->addWidget(label);
labelLayout->addStretch();
labelLayout->addWidget(label2);
labelLayout->addWidget(selectModeButton);
labelLayout->addWidget(dragModeButton);
labelLayout->addStretch();
labelLayout->addWidget(antialiasButton);
labelLayout->addWidget(openGlButton);
labelLayout->addWidget(printButton);
QGridLayout*topLayout =newQGridLayout;
topLayout->addLayout(labelLayout,0,0);
topLayout->addWidget(graphicsView,1,0);
topLayout->addLayout(zoomSliderLayout,1,1);
topLayout->addLayout(rotateSliderLayout,2,0);
topLayout->addWidget(resetButton,2,1);
setLayout(topLayout);
connect(resetButton, SIGNAL(clicked()),this, SLOT(resetView()));
connect(zoomSlider, SIGNAL(valueChanged(int)),this, SLOT(setupMatrix()));
connect(rotateSlider, SIGNAL(valueChanged(int)),this, SLOT(setupMatrix()));
connect(graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)),this, SLOT(setResetButtonEnabled()));
connect(graphicsView->horizontalScrollBar(), SIGNAL(valueChanged(int)),this, SLOT(setResetButtonEnabled()));
connect(selectModeButton, SIGNAL(toggled(bool)),this, SLOT(togglePointerMode()));
connect(dragModeButton, SIGNAL(toggled(bool)),this, SLOT(togglePointerMode()));
connect(antialiasButton, SIGNAL(toggled(bool)),this, SLOT(toggleAntialiasing()));
connect(openGlButton, SIGNAL(toggled(bool)),this, SLOT(toggleOpenGL()));
connect(rotateLeftIcon, SIGNAL(clicked()),this, SLOT(rotateLeft()));
connect(rotateRightIcon, SIGNAL(clicked()),this, SLOT(rotateRight()));
connect(zoomInIcon, SIGNAL(clicked()),this, SLOT(zoomIn()));
connect(zoomOutIcon, SIGNAL(clicked()),this, SLOT(zoomOut()));
connect(printButton, SIGNAL(clicked()),this, SLOT(print()));
setupMatrix();
}
QGraphicsView*View::view() const
{
returnstatic_cast<QGraphicsView*>(graphicsView);
}
void View::resetView()
{
zoomSlider->setValue(250);
rotateSlider->setValue(0);
setupMatrix();
graphicsView->ensureVisible(QRectF(0,0,0,0));
resetButton->setEnabled(false);
}
void View::setResetButtonEnabled()
{
resetButton->setEnabled(true);
}
void View::setupMatrix()
{
qreal scale =qPow(qreal(2), (zoomSlider->value() -250) /qreal(50));
QMatrix matrix;
matrix.scale(scale, scale);
matrix.rotate(rotateSlider->value());
graphicsView->setMatrix(matrix);
setResetButtonEnabled();
}
void View::togglePointerMode()
{
graphicsView->setDragMode(selectModeButton->isChecked()
?QGraphicsView::RubberBandDrag
: QGraphicsView::ScrollHandDrag);
graphicsView->setInteractive(selectModeButton->isChecked());
}
void View::toggleOpenGL()
{
#ifndef QT_NO_OPENGL
graphicsView->setViewport(openGlButton->isChecked() ?newQGLWidget(QGLFormat(QGL::SampleBuffers)) : newQWidget);
#endif
}
void View::toggleAntialiasing()
{
graphicsView->setRenderHint(QPainter::Antialiasing, antialiasButton->isChecked());
}
void View::print()
{
#ifndef QT_NO_PRINTERQPrinter printer;
QPrintDialog dialog(&printer,this);
if (dialog.exec() ==QDialog::Accepted) {
QPainter painter(&printer);
graphicsView->render(&painter);
}
#endif
}
void View::zoomIn(int level)
{
zoomSlider->setValue(zoomSlider->value() + level);
}
void View::zoomOut(int level)
{
zoomSlider->setValue(zoomSlider->value() - level);
}
void View::rotateLeft()
{
rotateSlider->setValue(rotateSlider->value() -10);
}
void View::rotateRight()
{
rotateSlider->setValue(rotateSlider->value() +10);
}
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter
ou par MP !