Graphviz 16.1.1~dev.20260906.1627
Loading...
Searching...
No Matches
imageviewer.cpp
Go to the documentation of this file.
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 v2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include "imageviewer.h"
14#include "mdichild.h"
15#include <QtGlobal>
16
17#include "moc_imageviewer.cpp"
18
20{
21 imageLabel = new QLabel;
22 imageLabel->setBackgroundRole(QPalette::Base);
23 imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
24 imageLabel->setScaledContents(true);
25
26 scrollArea = new QScrollArea;
27 scrollArea->setBackgroundRole(QPalette::Dark);
28 scrollArea->setWidget(imageLabel);
29 setCentralWidget(scrollArea);
30
31 createActions();
32 createMenus();
33
34 setWindowTitle(tr(""));
35 resize(800, 600);
36
37 setWindowIcon(QIcon(QStringLiteral(":/images/icon.png")));
38}
39
40bool ImageViewer::open(const QString &fileName)
41{
42 if (!fileName.isEmpty()) {
43 QImage image(fileName);
44 if (image.isNull()) {
45 return false;
46 }
47 imageLabel->setPixmap(QPixmap::fromImage(image));
48 scaleFactor = 1.0;
49 fitToWindowAct->setEnabled(true);
50 updateActions();
51
52 if (!fitToWindowAct->isChecked())
53 imageLabel->adjustSize();
54 }
55 return true;
56}
57
59{
60#ifndef QT_NO_PRINTER
61 auto get_pixmap = [&]() {
62#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
63 Q_ASSERT(imageLabel->pixmap());
64 return *imageLabel->pixmap();
65#else
66 return imageLabel->pixmap(Qt::ReturnByValue);
67#endif
68 };
69 QPrintDialog dialog(&printer, this);
70 if (dialog.exec()) {
71 QPainter painter(&printer);
72 QRect rect = painter.viewport();
73 QSize size = get_pixmap().size();
74 size.scale(rect.size(), Qt::KeepAspectRatio);
75 painter.setViewport(rect.x(), rect.y(), size.width(),
76 size.height());
77 painter.setWindow(get_pixmap().rect());
78 painter.drawPixmap(0, 0, get_pixmap());
79 }
80#endif
81}
82
84{
85 scaleImage(1.25);
86}
87
89{
90 scaleImage(0.8);
91}
92
94{
95 imageLabel->adjustSize();
96 scaleFactor = 1.0;
97}
98
100{
101 bool fitToWindow = fitToWindowAct->isChecked();
102 scrollArea->setWidgetResizable(fitToWindow);
103 if (!fitToWindow) {
104 normalSize();
105 }
106 updateActions();
107}
108
110{
111 QMessageBox::about(this, tr("About Image Viewer"),
112 tr
113 ("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
114 "and QScrollArea to display an image. QLabel is typically used "
115 "for displaying a text, but it can also display an image. "
116 "QScrollArea provides a scrolling view around another widget. "
117 "If the child widget exceeds the size of the frame, QScrollArea "
118 "automatically provides scroll bars. </p><p>The example "
119 "demonstrates how QLabel's ability to scale its contents "
120 "(QLabel::scaledContents), and QScrollArea's ability to "
121 "automatically resize its contents "
122 "(QScrollArea::widgetResizable), can be used to implement "
123 "zooming and scaling features. </p><p>In addition the example "
124 "shows how to use QPainter to print an image.</p>"));
125}
126
127void ImageViewer::createActions()
128{
129 printAct = new QAction(tr("&Print..."), this);
130 printAct->setShortcut(tr("Ctrl+P"));
131 printAct->setEnabled(false);
132 connect(printAct, &QAction::triggered, this, &ImageViewer::print);
133
134 exitAct = new QAction(tr("E&xit"), this);
135 exitAct->setShortcut(tr("Ctrl+Q"));
136 connect(exitAct, &QAction::triggered, this, &ImageViewer::close);
137
138 zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
139 zoomInAct->setShortcut(tr("Ctrl++"));
140 zoomInAct->setEnabled(false);
141 connect(zoomInAct, &QAction::triggered, this, &ImageViewer::zoomIn);
142
143 zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
144 zoomOutAct->setShortcut(tr("Ctrl+-"));
145 zoomOutAct->setEnabled(false);
146 connect(zoomOutAct, &QAction::triggered, this, &ImageViewer::zoomOut);
147
148 normalSizeAct = new QAction(tr("&Normal Size"), this);
149 normalSizeAct->setShortcut(tr("Ctrl+S"));
150 normalSizeAct->setEnabled(false);
151 connect(normalSizeAct, &QAction::triggered, this, &ImageViewer::normalSize);
152
153 fitToWindowAct = new QAction(tr("&Fit to Window"), this);
154 fitToWindowAct->setEnabled(false);
155 fitToWindowAct->setCheckable(true);
156 fitToWindowAct->setShortcut(tr("Ctrl+F"));
157 connect(fitToWindowAct, &QAction::triggered, this,
159
160 aboutAct = new QAction(tr("&About"), this);
161 connect(aboutAct, &QAction::triggered, this, &ImageViewer::about);
162
163 aboutQtAct = new QAction(tr("About &Qt"), this);
164 connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
165}
166
167void ImageViewer::createMenus()
168{
169
170 viewMenu = new QMenu(tr("&View"), this);
171 viewMenu->addAction(zoomInAct);
172 viewMenu->addAction(zoomOutAct);
173 viewMenu->addAction(normalSizeAct);
174 viewMenu->addSeparator();
175 viewMenu->addAction(fitToWindowAct);
176
177 menuBar()->addMenu(viewMenu);
178}
179
180void ImageViewer::updateActions()
181{
182 zoomInAct->setEnabled(!fitToWindowAct->isChecked());
183 zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
184 normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
185}
186
187void ImageViewer::scaleImage(double factor)
188{
189 auto get_pixmap = [&]() {
190#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
191 Q_ASSERT(imageLabel->pixmap());
192 return *imageLabel->pixmap();
193#else
194 return imageLabel->pixmap(Qt::ReturnByValue);
195#endif
196 };
197 scaleFactor *= factor;
198 imageLabel->resize(scaleFactor * get_pixmap().size());
199
200 adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
201 adjustScrollBar(scrollArea->verticalScrollBar(), factor);
202
203 zoomInAct->setEnabled(scaleFactor < 3.0);
204 zoomOutAct->setEnabled(scaleFactor > 0.333);
205}
206
207void ImageViewer::adjustScrollBar(QScrollBar * scrollBar, double factor)
208{
209 scrollBar->setValue(int (factor * scrollBar->value()
210 +
211 ((factor - 1) * scrollBar->pageStep() / 2)));
212}
213
214void ImageViewer::closeEvent(QCloseEvent * event)
215{
216 this->graphWindow->previewFrm = nullptr;
217 event->accept();
218
219}
bool open(const QString &fileName)
void closeEvent(QCloseEvent *event)
MdiChild * graphWindow
Definition imageviewer.h:34
void fitToWindow()
void normalSize()
std::unique_ptr< ImageViewer > previewFrm
Definition mdichild.h:38
T_cell image
Definition htmlparse.y:334
char * fileName(ingraph_state *sp)
Return name of current file being processed.
Definition ingraphs.c:156