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