| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "mainwindow.h"
|
|---|
| 43 |
|
|---|
| 44 | #include <QtGui>
|
|---|
| 45 |
|
|---|
| 46 | #include "svgview.h"
|
|---|
| 47 |
|
|---|
| 48 | MainWindow::MainWindow()
|
|---|
| 49 | : QMainWindow()
|
|---|
| 50 | , m_view(new SvgView)
|
|---|
| 51 | {
|
|---|
| 52 | QMenu *fileMenu = new QMenu(tr("&File"), this);
|
|---|
| 53 | QAction *openAction = fileMenu->addAction(tr("&Open..."));
|
|---|
| 54 | openAction->setShortcut(QKeySequence(tr("Ctrl+O")));
|
|---|
| 55 | QAction *quitAction = fileMenu->addAction(tr("E&xit"));
|
|---|
| 56 | quitAction->setShortcut(QKeySequence(tr("Ctrl+Q")));
|
|---|
| 57 |
|
|---|
| 58 | menuBar()->addMenu(fileMenu);
|
|---|
| 59 |
|
|---|
| 60 | QMenu *viewMenu = new QMenu(tr("&View"), this);
|
|---|
| 61 | m_backgroundAction = viewMenu->addAction(tr("&Background"));
|
|---|
| 62 | m_backgroundAction->setEnabled(false);
|
|---|
| 63 | m_backgroundAction->setCheckable(true);
|
|---|
| 64 | m_backgroundAction->setChecked(false);
|
|---|
| 65 | connect(m_backgroundAction, SIGNAL(toggled(bool)), m_view, SLOT(setViewBackground(bool)));
|
|---|
| 66 |
|
|---|
| 67 | m_outlineAction = viewMenu->addAction(tr("&Outline"));
|
|---|
| 68 | m_outlineAction->setEnabled(false);
|
|---|
| 69 | m_outlineAction->setCheckable(true);
|
|---|
| 70 | m_outlineAction->setChecked(true);
|
|---|
| 71 | connect(m_outlineAction, SIGNAL(toggled(bool)), m_view, SLOT(setViewOutline(bool)));
|
|---|
| 72 |
|
|---|
| 73 | menuBar()->addMenu(viewMenu);
|
|---|
| 74 |
|
|---|
| 75 | QMenu *rendererMenu = new QMenu(tr("&Renderer"), this);
|
|---|
| 76 | m_nativeAction = rendererMenu->addAction(tr("&Native"));
|
|---|
| 77 | m_nativeAction->setCheckable(true);
|
|---|
| 78 | m_nativeAction->setChecked(true);
|
|---|
| 79 | #ifndef QT_NO_OPENGL
|
|---|
| 80 | m_glAction = rendererMenu->addAction(tr("&OpenGL"));
|
|---|
| 81 | m_glAction->setCheckable(true);
|
|---|
| 82 | #endif
|
|---|
| 83 | m_imageAction = rendererMenu->addAction(tr("&Image"));
|
|---|
| 84 | m_imageAction->setCheckable(true);
|
|---|
| 85 |
|
|---|
| 86 | #ifndef QT_NO_OPENGL
|
|---|
| 87 | rendererMenu->addSeparator();
|
|---|
| 88 | m_highQualityAntialiasingAction = rendererMenu->addAction(tr("&High Quality Antialiasing"));
|
|---|
| 89 | m_highQualityAntialiasingAction->setEnabled(false);
|
|---|
| 90 | m_highQualityAntialiasingAction->setCheckable(true);
|
|---|
| 91 | m_highQualityAntialiasingAction->setChecked(false);
|
|---|
| 92 | connect(m_highQualityAntialiasingAction, SIGNAL(toggled(bool)), m_view, SLOT(setHighQualityAntialiasing(bool)));
|
|---|
| 93 | #endif
|
|---|
| 94 |
|
|---|
| 95 | QActionGroup *rendererGroup = new QActionGroup(this);
|
|---|
| 96 | rendererGroup->addAction(m_nativeAction);
|
|---|
| 97 | #ifndef QT_NO_OPENGL
|
|---|
| 98 | rendererGroup->addAction(m_glAction);
|
|---|
| 99 | #endif
|
|---|
| 100 | rendererGroup->addAction(m_imageAction);
|
|---|
| 101 |
|
|---|
| 102 | menuBar()->addMenu(rendererMenu);
|
|---|
| 103 |
|
|---|
| 104 | connect(openAction, SIGNAL(triggered()), this, SLOT(openFile()));
|
|---|
| 105 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
|
|---|
| 106 | connect(rendererGroup, SIGNAL(triggered(QAction *)),
|
|---|
| 107 | this, SLOT(setRenderer(QAction *)));
|
|---|
| 108 |
|
|---|
| 109 | setCentralWidget(m_view);
|
|---|
| 110 | setWindowTitle(tr("SVG Viewer"));
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | void MainWindow::openFile(const QString &path)
|
|---|
| 114 | {
|
|---|
| 115 | QString fileName;
|
|---|
| 116 | if (path.isNull())
|
|---|
| 117 | fileName = QFileDialog::getOpenFileName(this, tr("Open SVG File"),
|
|---|
| 118 | m_currentPath, "SVG files (*.svg *.svgz *.svg.gz)");
|
|---|
| 119 | else
|
|---|
| 120 | fileName = path;
|
|---|
| 121 |
|
|---|
| 122 | if (!fileName.isEmpty()) {
|
|---|
| 123 | QFile file(fileName);
|
|---|
| 124 | if (!file.exists()) {
|
|---|
| 125 | QMessageBox::critical(this, tr("Open SVG File"),
|
|---|
| 126 | QString("Could not open file '%1'.").arg(fileName));
|
|---|
| 127 |
|
|---|
| 128 | m_outlineAction->setEnabled(false);
|
|---|
| 129 | m_backgroundAction->setEnabled(false);
|
|---|
| 130 | return;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | m_view->openFile(file);
|
|---|
| 134 |
|
|---|
| 135 | if (!fileName.startsWith(":/")) {
|
|---|
| 136 | m_currentPath = fileName;
|
|---|
| 137 | setWindowTitle(tr("%1 - SVGViewer").arg(m_currentPath));
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | m_outlineAction->setEnabled(true);
|
|---|
| 141 | m_backgroundAction->setEnabled(true);
|
|---|
| 142 |
|
|---|
| 143 | resize(m_view->sizeHint() + QSize(80, 80 + menuBar()->height()));
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | void MainWindow::setRenderer(QAction *action)
|
|---|
| 148 | {
|
|---|
| 149 | #ifndef QT_NO_OPENGL
|
|---|
| 150 | m_highQualityAntialiasingAction->setEnabled(false);
|
|---|
| 151 | #endif
|
|---|
| 152 |
|
|---|
| 153 | if (action == m_nativeAction)
|
|---|
| 154 | m_view->setRenderer(SvgView::Native);
|
|---|
| 155 | #ifndef QT_NO_OPENGL
|
|---|
| 156 | else if (action == m_glAction) {
|
|---|
| 157 | m_highQualityAntialiasingAction->setEnabled(true);
|
|---|
| 158 | m_view->setRenderer(SvgView::OpenGL);
|
|---|
| 159 | }
|
|---|
| 160 | #endif
|
|---|
| 161 | else if (action == m_imageAction) {
|
|---|
| 162 | m_view->setRenderer(SvgView::Image);
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|