mainwindow.cpp 5.9 KB

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