18 imageLabel =
new QLabel;
19 imageLabel->setBackgroundRole(QPalette::Base);
20 imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
21 imageLabel->setScaledContents(
true);
23 scrollArea =
new QScrollArea;
24 scrollArea->setBackgroundRole(QPalette::Dark);
25 scrollArea->setWidget(imageLabel);
26 setCentralWidget(scrollArea);
31 setWindowTitle(tr(
""));
34 setWindowIcon(QIcon(QStringLiteral(
":/images/icon.png")));
44 imageLabel->setPixmap(QPixmap::fromImage(
image));
46 fitToWindowAct->setEnabled(
true);
49 if (!fitToWindowAct->isChecked())
50 imageLabel->adjustSize();
58 auto get_pixmap = [&]() {
59#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
60 Q_ASSERT(imageLabel->pixmap());
61 return *imageLabel->pixmap();
63 return imageLabel->pixmap(Qt::ReturnByValue);
66 QPrintDialog dialog(&printer,
this);
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(),
74 painter.setWindow(get_pixmap().rect());
75 painter.drawPixmap(0, 0, get_pixmap());
92 imageLabel->adjustSize();
108 QMessageBox::about(
this, tr(
"About Image Viewer"),
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>"));
124void ImageViewer::createActions()
126 printAct =
new QAction(tr(
"&Print..."),
this);
127 printAct->setShortcut(tr(
"Ctrl+P"));
128 printAct->setEnabled(
false);
131 exitAct =
new QAction(tr(
"E&xit"),
this);
132 exitAct->setShortcut(tr(
"Ctrl+Q"));
133 connect(exitAct, &QAction::triggered,
this, &ImageViewer::close);
135 zoomInAct =
new QAction(tr(
"Zoom &In (25%)"),
this);
136 zoomInAct->setShortcut(tr(
"Ctrl++"));
137 zoomInAct->setEnabled(
false);
140 zoomOutAct =
new QAction(tr(
"Zoom &Out (25%)"),
this);
141 zoomOutAct->setShortcut(tr(
"Ctrl+-"));
142 zoomOutAct->setEnabled(
false);
145 normalSizeAct =
new QAction(tr(
"&Normal Size"),
this);
146 normalSizeAct->setShortcut(tr(
"Ctrl+S"));
147 normalSizeAct->setEnabled(
false);
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,
157 aboutAct =
new QAction(tr(
"&About"),
this);
160 aboutQtAct =
new QAction(tr(
"About &Qt"),
this);
161 connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
164void ImageViewer::createMenus()
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);
174 menuBar()->addMenu(viewMenu);
177void ImageViewer::updateActions()
179 zoomInAct->setEnabled(!fitToWindowAct->isChecked());
180 zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
181 normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
184void ImageViewer::scaleImage(
double factor)
186 auto get_pixmap = [&]() {
187#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
188 Q_ASSERT(imageLabel->pixmap());
189 return *imageLabel->pixmap();
191 return imageLabel->pixmap(Qt::ReturnByValue);
194 scaleFactor *= factor;
195 imageLabel->resize(scaleFactor * get_pixmap().size());
197 adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
198 adjustScrollBar(scrollArea->verticalScrollBar(), factor);
200 zoomInAct->setEnabled(scaleFactor < 3.0);
201 zoomOutAct->setEnabled(scaleFactor > 0.333);
204void ImageViewer::adjustScrollBar(QScrollBar * scrollBar,
double factor)
206 scrollBar->setValue(
int (factor * scrollBar->value()
208 ((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.