[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the demonstration applications of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 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.
|
---|
| 23 | **
|
---|
| 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.
|
---|
| 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
| 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include <QPainter>
|
---|
| 43 | #include <QApplication>
|
---|
| 44 |
|
---|
| 45 | #include "embeddedsvgviewer.h"
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | EmbeddedSvgViewer::EmbeddedSvgViewer(const QString &filePath)
|
---|
| 50 | {
|
---|
| 51 | qApp->setStyleSheet(" QSlider:vertical { width: 50px; } \
|
---|
| 52 | QSlider::groove:vertical { border: 1px solid black; border-radius: 3px; width: 6px; } \
|
---|
| 53 | QSlider::handle:vertical { height: 25px; margin: 0 -22px; image: url(':/files/v-slider-handle.svg'); } \
|
---|
| 54 | ");
|
---|
| 55 |
|
---|
| 56 | m_renderer = new QSvgRenderer(filePath);
|
---|
| 57 | m_imageSize = m_renderer->viewBox().size();
|
---|
| 58 |
|
---|
| 59 | m_viewBoxCenter = (QPointF(m_imageSize.width() / qreal(2.0), m_imageSize.height() / qreal(2.0)));
|
---|
| 60 |
|
---|
| 61 | m_zoomSlider = new QSlider(Qt::Vertical, this);
|
---|
| 62 | m_zoomSlider->setMaximum(150);
|
---|
| 63 | m_zoomSlider->setMinimum(1);
|
---|
| 64 |
|
---|
| 65 | connect(m_zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setZoom(int)));
|
---|
| 66 | m_zoomSlider->setValue(100);
|
---|
| 67 |
|
---|
| 68 | m_quitButton = new QPushButton("Quit", this);
|
---|
| 69 |
|
---|
| 70 | connect(m_quitButton, SIGNAL(pressed()), QApplication::instance(), SLOT(quit()));
|
---|
| 71 |
|
---|
| 72 | if (m_renderer->animated())
|
---|
| 73 | connect(m_renderer, SIGNAL(repaintNeeded()), this, SLOT(update()));
|
---|
| 74 |
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void EmbeddedSvgViewer::paintEvent(QPaintEvent *event)
|
---|
| 78 | {
|
---|
| 79 | Q_UNUSED(event)
|
---|
| 80 | QPainter painter(this);
|
---|
| 81 | m_renderer->setViewBox(m_viewBox);
|
---|
| 82 | m_renderer->render(&painter);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | void EmbeddedSvgViewer::mouseMoveEvent ( QMouseEvent * event )
|
---|
| 87 | {
|
---|
| 88 | int incX = int((event->globalX() - m_mousePress.x()) * m_imageScale);
|
---|
| 89 | int incY = int((event->globalY() - m_mousePress.y()) * m_imageScale);
|
---|
| 90 |
|
---|
| 91 | QPointF newCenter;
|
---|
| 92 | newCenter.setX(m_viewBoxCenterOnMousePress.x() - incX);
|
---|
| 93 | newCenter.setY(m_viewBoxCenterOnMousePress.y() - incY);
|
---|
| 94 |
|
---|
| 95 | QRectF newViewBox = getViewBox(newCenter);
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | // Do a bounded move on the horizontal:
|
---|
| 99 | if ( (newViewBox.left() >= m_viewBoxBounds.left()) &&
|
---|
| 100 | (newViewBox.right() <= m_viewBoxBounds.right()) )
|
---|
| 101 | {
|
---|
| 102 | m_viewBoxCenter.setX(newCenter.x());
|
---|
| 103 | m_viewBox.setLeft(newViewBox.left());
|
---|
| 104 | m_viewBox.setRight(newViewBox.right());
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | // do a bounded move on the vertical:
|
---|
| 108 | if ( (newViewBox.top() >= m_viewBoxBounds.top()) &&
|
---|
| 109 | (newViewBox.bottom() <= m_viewBoxBounds.bottom()) )
|
---|
| 110 | {
|
---|
| 111 | m_viewBoxCenter.setY(newCenter.y());
|
---|
| 112 | m_viewBox.setTop(newViewBox.top());
|
---|
| 113 | m_viewBox.setBottom(newViewBox.bottom());
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | update();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | void EmbeddedSvgViewer::mousePressEvent ( QMouseEvent * event )
|
---|
| 120 | {
|
---|
| 121 | m_viewBoxCenterOnMousePress = m_viewBoxCenter;
|
---|
| 122 | m_mousePress = event->globalPos();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | QRectF EmbeddedSvgViewer::getViewBox(QPointF viewBoxCenter)
|
---|
| 127 | {
|
---|
| 128 | QRectF result;
|
---|
| 129 | result.setLeft(viewBoxCenter.x() - (m_viewBoxSize.width() / 2));
|
---|
| 130 | result.setTop(viewBoxCenter.y() - (m_viewBoxSize.height() / 2));
|
---|
| 131 | result.setRight(viewBoxCenter.x() + (m_viewBoxSize.width() / 2));
|
---|
| 132 | result.setBottom(viewBoxCenter.y() + (m_viewBoxSize.height() / 2));
|
---|
| 133 | return result;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | void EmbeddedSvgViewer::updateImageScale()
|
---|
| 137 | {
|
---|
| 138 | m_imageScale = qMax( (qreal)m_imageSize.width() / (qreal)width(),
|
---|
| 139 | (qreal)m_imageSize.height() / (qreal)height())*m_zoomLevel;
|
---|
| 140 |
|
---|
| 141 | m_viewBoxSize.setWidth((qreal)width() * m_imageScale);
|
---|
| 142 | m_viewBoxSize.setHeight((qreal)height() * m_imageScale);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | void EmbeddedSvgViewer::resizeEvent ( QResizeEvent * /* event */ )
|
---|
| 147 | {
|
---|
| 148 | qreal origZoom = m_zoomLevel;
|
---|
| 149 |
|
---|
| 150 | // Get the new bounds:
|
---|
| 151 | m_zoomLevel = 1.0;
|
---|
| 152 | updateImageScale();
|
---|
| 153 | m_viewBoxBounds = getViewBox(QPointF(m_imageSize.width() / 2.0, m_imageSize.height() / 2.0));
|
---|
| 154 |
|
---|
| 155 | m_zoomLevel = origZoom;
|
---|
| 156 | updateImageScale();
|
---|
| 157 | m_viewBox = getViewBox(m_viewBoxCenter);
|
---|
| 158 |
|
---|
| 159 | QRect sliderRect;
|
---|
| 160 | sliderRect.setLeft(width() - m_zoomSlider->sizeHint().width());
|
---|
| 161 | sliderRect.setRight(width());
|
---|
| 162 | sliderRect.setTop(height()/4);
|
---|
| 163 | sliderRect.setBottom(height() - (height()/4));
|
---|
| 164 | m_zoomSlider->setGeometry(sliderRect);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 | void EmbeddedSvgViewer::setZoom(int newZoom)
|
---|
| 169 | {
|
---|
| 170 | m_zoomLevel = qreal(newZoom) / qreal(100);
|
---|
| 171 |
|
---|
| 172 | updateImageScale();
|
---|
| 173 | m_viewBox = getViewBox(m_viewBoxCenter);
|
---|
| 174 |
|
---|
| 175 | update();
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 |
|
---|