19 imageLabel =
new QLabel;
20 imageLabel->setBackgroundRole(QPalette::Base);
21 imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
22 imageLabel->setScaledContents(
true);
24 scrollArea =
new QScrollArea;
25 scrollArea->setBackgroundRole(QPalette::Dark);
26 scrollArea->setWidget(imageLabel);
27 setCentralWidget(scrollArea);
32 setWindowTitle(tr(
""));
35 setWindowIcon(QIcon(QStringLiteral(
":/images/icon.png")));
45 imageLabel->setPixmap(QPixmap::fromImage(
image));
47 fitToWindowAct->setEnabled(
true);
50 if (!fitToWindowAct->isChecked())
51 imageLabel->adjustSize();
59 auto get_pixmap = [&]() {
60#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
61 Q_ASSERT(imageLabel->pixmap());
62 return *imageLabel->pixmap();
64 return imageLabel->pixmap(Qt::ReturnByValue);
67 QPrintDialog dialog(&printer,
this);
69 QPainter painter(&printer);
70 QRect rect = painter.viewport();
71 QSize size = get_pixmap().size();
72 size.scale(rect.size(), Qt::KeepAspectRatio);
73 painter.setViewport(rect.x(), rect.y(), size.width(),
75 painter.setWindow(get_pixmap().rect());
76 painter.drawPixmap(0, 0, get_pixmap());
93 imageLabel->adjustSize();
109 QMessageBox::about(
this, tr(
"About Image Viewer"),
111 (
"<p>The <b>Image Viewer</b> example shows how to combine QLabel "
112 "and QScrollArea to display an image. QLabel is typically used "
113 "for displaying a text, but it can also display an image. "
114 "QScrollArea provides a scrolling view around another widget. "
115 "If the child widget exceeds the size of the frame, QScrollArea "
116 "automatically provides scroll bars. </p><p>The example "
117 "demonstrates how QLabel's ability to scale its contents "
118 "(QLabel::scaledContents), and QScrollArea's ability to "
119 "automatically resize its contents "
120 "(QScrollArea::widgetResizable), can be used to implement "
121 "zooming and scaling features. </p><p>In addition the example "
122 "shows how to use QPainter to print an image.</p>"));
125void ImageViewer::createActions()
127 printAct =
new QAction(tr(
"&Print..."),
this);
128 printAct->setShortcut(tr(
"Ctrl+P"));
129 printAct->setEnabled(
false);
132 exitAct =
new QAction(tr(
"E&xit"),
this);
133 exitAct->setShortcut(tr(
"Ctrl+Q"));
134 connect(exitAct, &QAction::triggered,
this, &ImageViewer::close);
136 zoomInAct =
new QAction(tr(
"Zoom &In (25%)"),
this);
137 zoomInAct->setShortcut(tr(
"Ctrl++"));
138 zoomInAct->setEnabled(
false);
141 zoomOutAct =
new QAction(tr(
"Zoom &Out (25%)"),
this);
142 zoomOutAct->setShortcut(tr(
"Ctrl+-"));
143 zoomOutAct->setEnabled(
false);
146 normalSizeAct =
new QAction(tr(
"&Normal Size"),
this);
147 normalSizeAct->setShortcut(tr(
"Ctrl+S"));
148 normalSizeAct->setEnabled(
false);
151 fitToWindowAct =
new QAction(tr(
"&Fit to Window"),
this);
152 fitToWindowAct->setEnabled(
false);
153 fitToWindowAct->setCheckable(
true);
154 fitToWindowAct->setShortcut(tr(
"Ctrl+F"));
155 connect(fitToWindowAct, &QAction::triggered,
this,
158 aboutAct =
new QAction(tr(
"&About"),
this);
161 aboutQtAct =
new QAction(tr(
"About &Qt"),
this);
162 connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
165void ImageViewer::createMenus()
168 viewMenu =
new QMenu(tr(
"&View"),
this);
169 viewMenu->addAction(zoomInAct);
170 viewMenu->addAction(zoomOutAct);
171 viewMenu->addAction(normalSizeAct);
172 viewMenu->addSeparator();
173 viewMenu->addAction(fitToWindowAct);
175 menuBar()->addMenu(viewMenu);
178void ImageViewer::updateActions()
180 zoomInAct->setEnabled(!fitToWindowAct->isChecked());
181 zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
182 normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
185void ImageViewer::scaleImage(
double factor)
187 auto get_pixmap = [&]() {
188#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
189 Q_ASSERT(imageLabel->pixmap());
190 return *imageLabel->pixmap();
192 return imageLabel->pixmap(Qt::ReturnByValue);
195 scaleFactor *= factor;
196 imageLabel->resize(scaleFactor * get_pixmap().size());
198 adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
199 adjustScrollBar(scrollArea->verticalScrollBar(), factor);
201 zoomInAct->setEnabled(scaleFactor < 3.0);
202 zoomOutAct->setEnabled(scaleFactor > 0.333);
205void ImageViewer::adjustScrollBar(QScrollBar * scrollBar,
double factor)
207 scrollBar->setValue(
int (factor * scrollBar->value()
209 ((factor - 1) * scrollBar->pageStep() / 2)));
bool open(const QString &fileName)
void closeEvent(QCloseEvent *event)
std::unique_ptr< ImageViewer > previewFrm
char * fileName(ingraph_state *sp)
Return name of current file being processed.