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 examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include "mainwindow.h"
|
---|
42 |
|
---|
43 | #include <QtGui>
|
---|
44 |
|
---|
45 | #include "svgview.h"
|
---|
46 |
|
---|
47 | MainWindow::MainWindow()
|
---|
48 | : QMainWindow()
|
---|
49 | , m_view(new SvgView)
|
---|
50 | {
|
---|
51 | QMenu *fileMenu = new QMenu(tr("&File"), this);
|
---|
52 | QAction *openAction = fileMenu->addAction(tr("&Open..."));
|
---|
53 | openAction->setShortcut(QKeySequence(tr("Ctrl+O")));
|
---|
54 | QAction *quitAction = fileMenu->addAction(tr("E&xit"));
|
---|
55 | quitAction->setShortcuts(QKeySequence::Quit);
|
---|
56 |
|
---|
57 | menuBar()->addMenu(fileMenu);
|
---|
58 |
|
---|
59 | QMenu *viewMenu = new QMenu(tr("&View"), this);
|
---|
60 | m_backgroundAction = viewMenu->addAction(tr("&Background"));
|
---|
61 | m_backgroundAction->setEnabled(false);
|
---|
62 | m_backgroundAction->setCheckable(true);
|
---|
63 | m_backgroundAction->setChecked(false);
|
---|
64 | connect(m_backgroundAction, SIGNAL(toggled(bool)), m_view, SLOT(setViewBackground(bool)));
|
---|
65 |
|
---|
66 | m_outlineAction = viewMenu->addAction(tr("&Outline"));
|
---|
67 | m_outlineAction->setEnabled(false);
|
---|
68 | m_outlineAction->setCheckable(true);
|
---|
69 | m_outlineAction->setChecked(true);
|
---|
70 | connect(m_outlineAction, SIGNAL(toggled(bool)), m_view, SLOT(setViewOutline(bool)));
|
---|
71 |
|
---|
72 | menuBar()->addMenu(viewMenu);
|
---|
73 |
|
---|
74 | QMenu *rendererMenu = new QMenu(tr("&Renderer"), this);
|
---|
75 | m_nativeAction = rendererMenu->addAction(tr("&Native"));
|
---|
76 | m_nativeAction->setCheckable(true);
|
---|
77 | m_nativeAction->setChecked(true);
|
---|
78 | #ifndef QT_NO_OPENGL
|
---|
79 | m_glAction = rendererMenu->addAction(tr("&OpenGL"));
|
---|
80 | m_glAction->setCheckable(true);
|
---|
81 | #endif
|
---|
82 | m_imageAction = rendererMenu->addAction(tr("&Image"));
|
---|
83 | m_imageAction->setCheckable(true);
|
---|
84 |
|
---|
85 | #ifndef QT_NO_OPENGL
|
---|
86 | rendererMenu->addSeparator();
|
---|
87 | m_highQualityAntialiasingAction = rendererMenu->addAction(tr("&High Quality Antialiasing"));
|
---|
88 | m_highQualityAntialiasingAction->setEnabled(false);
|
---|
89 | m_highQualityAntialiasingAction->setCheckable(true);
|
---|
90 | m_highQualityAntialiasingAction->setChecked(false);
|
---|
91 | connect(m_highQualityAntialiasingAction, SIGNAL(toggled(bool)), m_view, SLOT(setHighQualityAntialiasing(bool)));
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | QActionGroup *rendererGroup = new QActionGroup(this);
|
---|
95 | rendererGroup->addAction(m_nativeAction);
|
---|
96 | #ifndef QT_NO_OPENGL
|
---|
97 | rendererGroup->addAction(m_glAction);
|
---|
98 | #endif
|
---|
99 | rendererGroup->addAction(m_imageAction);
|
---|
100 |
|
---|
101 | menuBar()->addMenu(rendererMenu);
|
---|
102 |
|
---|
103 | connect(openAction, SIGNAL(triggered()), this, SLOT(openFile()));
|
---|
104 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
|
---|
105 | connect(rendererGroup, SIGNAL(triggered(QAction*)),
|
---|
106 | this, SLOT(setRenderer(QAction*)));
|
---|
107 |
|
---|
108 | setCentralWidget(m_view);
|
---|
109 | setWindowTitle(tr("SVG Viewer"));
|
---|
110 | }
|
---|
111 |
|
---|
112 | void MainWindow::openFile(const QString &path)
|
---|
113 | {
|
---|
114 | QString fileName;
|
---|
115 | if (path.isNull())
|
---|
116 | fileName = QFileDialog::getOpenFileName(this, tr("Open SVG File"),
|
---|
117 | m_currentPath, "SVG files (*.svg *.svgz *.svg.gz)");
|
---|
118 | else
|
---|
119 | fileName = path;
|
---|
120 |
|
---|
121 | if (!fileName.isEmpty()) {
|
---|
122 | QFile file(fileName);
|
---|
123 | if (!file.exists()) {
|
---|
124 | QMessageBox::critical(this, tr("Open SVG File"),
|
---|
125 | QString("Could not open file '%1'.").arg(fileName));
|
---|
126 |
|
---|
127 | m_outlineAction->setEnabled(false);
|
---|
128 | m_backgroundAction->setEnabled(false);
|
---|
129 | return;
|
---|
130 | }
|
---|
131 |
|
---|
132 | m_view->openFile(file);
|
---|
133 |
|
---|
134 | if (!fileName.startsWith(":/")) {
|
---|
135 | m_currentPath = fileName;
|
---|
136 | setWindowTitle(tr("%1 - SVGViewer").arg(m_currentPath));
|
---|
137 | }
|
---|
138 |
|
---|
139 | m_outlineAction->setEnabled(true);
|
---|
140 | m_backgroundAction->setEnabled(true);
|
---|
141 |
|
---|
142 | resize(m_view->sizeHint() + QSize(80, 80 + menuBar()->height()));
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | void MainWindow::setRenderer(QAction *action)
|
---|
147 | {
|
---|
148 | #ifndef QT_NO_OPENGL
|
---|
149 | m_highQualityAntialiasingAction->setEnabled(false);
|
---|
150 | #endif
|
---|
151 |
|
---|
152 | if (action == m_nativeAction)
|
---|
153 | m_view->setRenderer(SvgView::Native);
|
---|
154 | #ifndef QT_NO_OPENGL
|
---|
155 | else if (action == m_glAction) {
|
---|
156 | m_highQualityAntialiasingAction->setEnabled(true);
|
---|
157 | m_view->setRenderer(SvgView::OpenGL);
|
---|
158 | }
|
---|
159 | #endif
|
---|
160 | else if (action == m_imageAction) {
|
---|
161 | m_view->setRenderer(SvgView::Image);
|
---|
162 | }
|
---|
163 | }
|
---|