17#include "moc_imageviewer.cpp"
21 imageLabel =
new QLabel;
22 imageLabel->setBackgroundRole(QPalette::Base);
23 imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
24 imageLabel->setScaledContents(
true);
26 scrollArea =
new QScrollArea;
27 scrollArea->setBackgroundRole(QPalette::Dark);
28 scrollArea->setWidget(imageLabel);
29 setCentralWidget(scrollArea);
34 setWindowTitle(tr(
""));
37 setWindowIcon(QIcon(QStringLiteral(
":/images/icon.png")));
47 imageLabel->setPixmap(QPixmap::fromImage(
image));
49 fitToWindowAct->setEnabled(
true);
52 if (!fitToWindowAct->isChecked())
53 imageLabel->adjustSize();
61 auto get_pixmap = [&]() {
62#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
63 Q_ASSERT(imageLabel->pixmap());
64 return *imageLabel->pixmap();
66 return imageLabel->pixmap(Qt::ReturnByValue);
69 QPrintDialog dialog(&printer,
this);
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(),
77 painter.setWindow(get_pixmap().rect());
78 painter.drawPixmap(0, 0, get_pixmap());
95 imageLabel->adjustSize();
111 QMessageBox::about(
this, tr(
"About Image Viewer"),
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>"));
127void ImageViewer::createActions()
129 printAct =
new QAction(tr(
"&Print..."),
this);
130 printAct->setShortcut(tr(
"Ctrl+P"));
131 printAct->setEnabled(
false);
134 exitAct =
new QAction(tr(
"E&xit"),
this);
135 exitAct->setShortcut(tr(
"Ctrl+Q"));
136 connect(exitAct, &QAction::triggered,
this, &ImageViewer::close);
138 zoomInAct =
new QAction(tr(
"Zoom &In (25%)"),
this);
139 zoomInAct->setShortcut(tr(
"Ctrl++"));
140 zoomInAct->setEnabled(
false);
143 zoomOutAct =
new QAction(tr(
"Zoom &Out (25%)"),
this);
144 zoomOutAct->setShortcut(tr(
"Ctrl+-"));
145 zoomOutAct->setEnabled(
false);
148 normalSizeAct =
new QAction(tr(
"&Normal Size"),
this);
149 normalSizeAct->setShortcut(tr(
"Ctrl+S"));
150 normalSizeAct->setEnabled(
false);
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,
160 aboutAct =
new QAction(tr(
"&About"),
this);
163 aboutQtAct =
new QAction(tr(
"About &Qt"),
this);
164 connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
167void ImageViewer::createMenus()
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);
177 menuBar()->addMenu(viewMenu);
180void ImageViewer::updateActions()
182 zoomInAct->setEnabled(!fitToWindowAct->isChecked());
183 zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
184 normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
187void ImageViewer::scaleImage(
double factor)
189 auto get_pixmap = [&]() {
190#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
191 Q_ASSERT(imageLabel->pixmap());
192 return *imageLabel->pixmap();
194 return imageLabel->pixmap(Qt::ReturnByValue);
197 scaleFactor *= factor;
198 imageLabel->resize(scaleFactor * get_pixmap().size());
200 adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
201 adjustScrollBar(scrollArea->verticalScrollBar(), factor);
203 zoomInAct->setEnabled(scaleFactor < 3.0);
204 zoomOutAct->setEnabled(scaleFactor > 0.333);
207void ImageViewer::adjustScrollBar(QScrollBar * scrollBar,
double factor)
209 scrollBar->setValue(
int (factor * scrollBar->value()
211 ((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.