examples/painting/svgviewer/mainwindow.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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->setShortcuts(QKeySequence::Quit);
       
    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 }