imageviewer.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include "imageviewer.h"
  11. #include "mdichild.h"
  12. #include <QtGlobal>
  13. ImageViewer::ImageViewer()
  14. {
  15. imageLabel = new QLabel;
  16. imageLabel->setBackgroundRole(QPalette::Base);
  17. imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  18. imageLabel->setScaledContents(true);
  19. scrollArea = new QScrollArea;
  20. scrollArea->setBackgroundRole(QPalette::Dark);
  21. scrollArea->setWidget(imageLabel);
  22. setCentralWidget(scrollArea);
  23. createActions();
  24. createMenus();
  25. setWindowTitle(tr(""));
  26. resize(800, 600);
  27. setWindowIcon(QIcon(QStringLiteral(":/images/icon.png")));
  28. }
  29. bool ImageViewer::open(const QString &fileName)
  30. {
  31. if (!fileName.isEmpty()) {
  32. QImage image(fileName);
  33. if (image.isNull()) {
  34. return false;
  35. }
  36. imageLabel->setPixmap(QPixmap::fromImage(image));
  37. scaleFactor = 1.0;
  38. fitToWindowAct->setEnabled(true);
  39. updateActions();
  40. if (!fitToWindowAct->isChecked())
  41. imageLabel->adjustSize();
  42. }
  43. return true;
  44. }
  45. void ImageViewer::print()
  46. {
  47. #ifndef QT_NO_PRINTER
  48. auto get_pixmap = [&]() {
  49. #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
  50. Q_ASSERT(imageLabel->pixmap());
  51. return *imageLabel->pixmap();
  52. #else
  53. return imageLabel->pixmap(Qt::ReturnByValue);
  54. #endif
  55. };
  56. QPrintDialog dialog(&printer, this);
  57. if (dialog.exec()) {
  58. QPainter painter(&printer);
  59. QRect rect = painter.viewport();
  60. QSize size = get_pixmap().size();
  61. size.scale(rect.size(), Qt::KeepAspectRatio);
  62. painter.setViewport(rect.x(), rect.y(), size.width(),
  63. size.height());
  64. painter.setWindow(get_pixmap().rect());
  65. painter.drawPixmap(0, 0, get_pixmap());
  66. }
  67. #endif
  68. }
  69. void ImageViewer::zoomIn()
  70. {
  71. scaleImage(1.25);
  72. }
  73. void ImageViewer::zoomOut()
  74. {
  75. scaleImage(0.8);
  76. }
  77. void ImageViewer::normalSize()
  78. {
  79. imageLabel->adjustSize();
  80. scaleFactor = 1.0;
  81. }
  82. void ImageViewer::fitToWindow()
  83. {
  84. bool fitToWindow = fitToWindowAct->isChecked();
  85. scrollArea->setWidgetResizable(fitToWindow);
  86. if (!fitToWindow) {
  87. normalSize();
  88. }
  89. updateActions();
  90. }
  91. void ImageViewer::about()
  92. {
  93. QMessageBox::about(this, tr("About Image Viewer"),
  94. tr
  95. ("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
  96. "and QScrollArea to display an image. QLabel is typically used "
  97. "for displaying a text, but it can also display an image. "
  98. "QScrollArea provides a scrolling view around another widget. "
  99. "If the child widget exceeds the size of the frame, QScrollArea "
  100. "automatically provides scroll bars. </p><p>The example "
  101. "demonstrates how QLabel's ability to scale its contents "
  102. "(QLabel::scaledContents), and QScrollArea's ability to "
  103. "automatically resize its contents "
  104. "(QScrollArea::widgetResizable), can be used to implement "
  105. "zooming and scaling features. </p><p>In addition the example "
  106. "shows how to use QPainter to print an image.</p>"));
  107. }
  108. void ImageViewer::createActions()
  109. {
  110. printAct = new QAction(tr("&Print..."), this);
  111. printAct->setShortcut(tr("Ctrl+P"));
  112. printAct->setEnabled(false);
  113. connect(printAct, &QAction::triggered, this, &ImageViewer::print);
  114. exitAct = new QAction(tr("E&xit"), this);
  115. exitAct->setShortcut(tr("Ctrl+Q"));
  116. connect(exitAct, &QAction::triggered, this, &ImageViewer::close);
  117. zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
  118. zoomInAct->setShortcut(tr("Ctrl++"));
  119. zoomInAct->setEnabled(false);
  120. connect(zoomInAct, &QAction::triggered, this, &ImageViewer::zoomIn);
  121. zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
  122. zoomOutAct->setShortcut(tr("Ctrl+-"));
  123. zoomOutAct->setEnabled(false);
  124. connect(zoomOutAct, &QAction::triggered, this, &ImageViewer::zoomOut);
  125. normalSizeAct = new QAction(tr("&Normal Size"), this);
  126. normalSizeAct->setShortcut(tr("Ctrl+S"));
  127. normalSizeAct->setEnabled(false);
  128. connect(normalSizeAct, &QAction::triggered, this, &ImageViewer::normalSize);
  129. fitToWindowAct = new QAction(tr("&Fit to Window"), this);
  130. fitToWindowAct->setEnabled(false);
  131. fitToWindowAct->setCheckable(true);
  132. fitToWindowAct->setShortcut(tr("Ctrl+F"));
  133. connect(fitToWindowAct, &QAction::triggered, this,
  134. &ImageViewer::fitToWindow);
  135. aboutAct = new QAction(tr("&About"), this);
  136. connect(aboutAct, &QAction::triggered, this, &ImageViewer::about);
  137. aboutQtAct = new QAction(tr("About &Qt"), this);
  138. connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
  139. }
  140. void ImageViewer::createMenus()
  141. {
  142. viewMenu = new QMenu(tr("&View"), this);
  143. viewMenu->addAction(zoomInAct);
  144. viewMenu->addAction(zoomOutAct);
  145. viewMenu->addAction(normalSizeAct);
  146. viewMenu->addSeparator();
  147. viewMenu->addAction(fitToWindowAct);
  148. menuBar()->addMenu(viewMenu);
  149. }
  150. void ImageViewer::updateActions()
  151. {
  152. zoomInAct->setEnabled(!fitToWindowAct->isChecked());
  153. zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
  154. normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
  155. }
  156. void ImageViewer::scaleImage(double factor)
  157. {
  158. auto get_pixmap = [&]() {
  159. #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
  160. Q_ASSERT(imageLabel->pixmap());
  161. return *imageLabel->pixmap();
  162. #else
  163. return imageLabel->pixmap(Qt::ReturnByValue);
  164. #endif
  165. };
  166. scaleFactor *= factor;
  167. imageLabel->resize(scaleFactor * get_pixmap().size());
  168. adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
  169. adjustScrollBar(scrollArea->verticalScrollBar(), factor);
  170. zoomInAct->setEnabled(scaleFactor < 3.0);
  171. zoomOutAct->setEnabled(scaleFactor > 0.333);
  172. }
  173. void ImageViewer::adjustScrollBar(QScrollBar * scrollBar, double factor)
  174. {
  175. scrollBar->setValue(int (factor * scrollBar->value()
  176. +
  177. ((factor - 1) * scrollBar->pageStep() / 2)));
  178. }
  179. void ImageViewer::closeEvent(QCloseEvent * event)
  180. {
  181. this->graphWindow->previewFrm = nullptr;
  182. event->accept();
  183. }