| 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 |
#include "config.h" |
| 12 |
|
| 13 |
#include "imageviewer.h" |
| 14 |
#include "mdichild.h" |
| 15 |
#include <QtGlobal> |
| 16 |
|
| 17 |
ImageViewer::ImageViewer() |
| 18 |
{ |
| 19 |
imageLabel = new QLabel; |
| 20 |
imageLabel->setBackgroundRole(QPalette::Base); |
| 21 |
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); |
| 22 |
imageLabel->setScaledContents(true); |
| 23 |
|
| 24 |
scrollArea = new QScrollArea; |
| 25 |
scrollArea->setBackgroundRole(QPalette::Dark); |
| 26 |
scrollArea->setWidget(imageLabel); |
| 27 |
setCentralWidget(scrollArea); |
| 28 |
|
| 29 |
createActions(); |
| 30 |
createMenus(); |
| 31 |
|
| 32 |
setWindowTitle(tr("")); |
| 33 |
resize(800, 600); |
| 34 |
|
| 35 |
setWindowIcon(QIcon(QStringLiteral(":/images/icon.png"))); |
| 36 |
} |
| 37 |
|
| 38 |
bool ImageViewer::open(const QString &fileName) |
| 39 |
{ |
| 40 |
if (!fileName.isEmpty()) { |
| 41 |
QImage image(fileName); |
| 42 |
if (image.isNull()) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
imageLabel->setPixmap(QPixmap::fromImage(image)); |
| 46 |
scaleFactor = 1.0; |
| 47 |
fitToWindowAct->setEnabled(true); |
| 48 |
updateActions(); |
| 49 |
|
| 50 |
if (!fitToWindowAct->isChecked()) |
| 51 |
imageLabel->adjustSize(); |
| 52 |
} |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
void ImageViewer::print() |
| 57 |
{ |
| 58 |
#ifndef QT_NO_PRINTER |
| 59 |
auto get_pixmap = [&]() { |
| 60 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 61 |
Q_ASSERT(imageLabel->pixmap()); |
| 62 |
return *imageLabel->pixmap(); |
| 63 |
#else |
| 64 |
return imageLabel->pixmap(Qt::ReturnByValue); |
| 65 |
#endif |
| 66 |
}; |
| 67 |
QPrintDialog dialog(&printer, this); |
| 68 |
if (dialog.exec()) { |
| 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(), |
| 74 |
size.height()); |
| 75 |
painter.setWindow(get_pixmap().rect()); |
| 76 |
painter.drawPixmap(0, 0, get_pixmap()); |
| 77 |
} |
| 78 |
#endif |
| 79 |
} |
| 80 |
|
| 81 |
void ImageViewer::zoomIn() |
| 82 |
{ |
| 83 |
scaleImage(1.25); |
| 84 |
} |
| 85 |
|
| 86 |
void ImageViewer::zoomOut() |
| 87 |
{ |
| 88 |
scaleImage(0.8); |
| 89 |
} |
| 90 |
|
| 91 |
void ImageViewer::normalSize() |
| 92 |
{ |
| 93 |
imageLabel->adjustSize(); |
| 94 |
scaleFactor = 1.0; |
| 95 |
} |
| 96 |
|
| 97 |
void ImageViewer::fitToWindow() |
| 98 |
{ |
| 99 |
bool fitToWindow = fitToWindowAct->isChecked(); |
| 100 |
scrollArea->setWidgetResizable(fitToWindow); |
| 101 |
if (!fitToWindow) { |
| 102 |
normalSize(); |
| 103 |
} |
| 104 |
updateActions(); |
| 105 |
} |
| 106 |
|
| 107 |
void ImageViewer::about() |
| 108 |
{ |
| 109 |
QMessageBox::about(this, tr("About Image Viewer"), |
| 110 |
tr |
| 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>")); |
| 123 |
} |
| 124 |
|
| 125 |
void ImageViewer::createActions() |
| 126 |
{ |
| 127 |
printAct = new QAction(tr("&Print..."), this); |
| 128 |
printAct->setShortcut(tr("Ctrl+P")); |
| 129 |
printAct->setEnabled(false); |
| 130 |
connect(printAct, &QAction::triggered, this, &ImageViewer::print); |
| 131 |
|
| 132 |
exitAct = new QAction(tr("E&xit"), this); |
| 133 |
exitAct->setShortcut(tr("Ctrl+Q")); |
| 134 |
connect(exitAct, &QAction::triggered, this, &ImageViewer::close); |
| 135 |
|
| 136 |
zoomInAct = new QAction(tr("Zoom &In (25%)"), this); |
| 137 |
zoomInAct->setShortcut(tr("Ctrl++")); |
| 138 |
zoomInAct->setEnabled(false); |
| 139 |
connect(zoomInAct, &QAction::triggered, this, &ImageViewer::zoomIn); |
| 140 |
|
| 141 |
zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this); |
| 142 |
zoomOutAct->setShortcut(tr("Ctrl+-")); |
| 143 |
zoomOutAct->setEnabled(false); |
| 144 |
connect(zoomOutAct, &QAction::triggered, this, &ImageViewer::zoomOut); |
| 145 |
|
| 146 |
normalSizeAct = new QAction(tr("&Normal Size"), this); |
| 147 |
normalSizeAct->setShortcut(tr("Ctrl+S")); |
| 148 |
normalSizeAct->setEnabled(false); |
| 149 |
connect(normalSizeAct, &QAction::triggered, this, &ImageViewer::normalSize); |
| 150 |
|
| 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, |
| 156 |
&ImageViewer::fitToWindow); |
| 157 |
|
| 158 |
aboutAct = new QAction(tr("&About"), this); |
| 159 |
connect(aboutAct, &QAction::triggered, this, &ImageViewer::about); |
| 160 |
|
| 161 |
aboutQtAct = new QAction(tr("About &Qt"), this); |
| 162 |
connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt); |
| 163 |
} |
| 164 |
|
| 165 |
void ImageViewer::createMenus() |
| 166 |
{ |
| 167 |
|
| 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); |
| 174 |
|
| 175 |
menuBar()->addMenu(viewMenu); |
| 176 |
} |
| 177 |
|
| 178 |
void ImageViewer::updateActions() |
| 179 |
{ |
| 180 |
zoomInAct->setEnabled(!fitToWindowAct->isChecked()); |
| 181 |
zoomOutAct->setEnabled(!fitToWindowAct->isChecked()); |
| 182 |
normalSizeAct->setEnabled(!fitToWindowAct->isChecked()); |
| 183 |
} |
| 184 |
|
| 185 |
void ImageViewer::scaleImage(double factor) |
| 186 |
{ |
| 187 |
auto get_pixmap = [&]() { |
| 188 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 189 |
Q_ASSERT(imageLabel->pixmap()); |
| 190 |
return *imageLabel->pixmap(); |
| 191 |
#else |
| 192 |
return imageLabel->pixmap(Qt::ReturnByValue); |
| 193 |
#endif |
| 194 |
}; |
| 195 |
scaleFactor *= factor; |
| 196 |
imageLabel->resize(scaleFactor * get_pixmap().size()); |
| 197 |
|
| 198 |
adjustScrollBar(scrollArea->horizontalScrollBar(), factor); |
| 199 |
adjustScrollBar(scrollArea->verticalScrollBar(), factor); |
| 200 |
|
| 201 |
zoomInAct->setEnabled(scaleFactor < 3.0); |
| 202 |
zoomOutAct->setEnabled(scaleFactor > 0.333); |
| 203 |
} |
| 204 |
|
| 205 |
void ImageViewer::adjustScrollBar(QScrollBar * scrollBar, double factor) |
| 206 |
{ |
| 207 |
scrollBar->setValue(int (factor * scrollBar->value() |
| 208 |
+ |
| 209 |
((factor - 1) * scrollBar->pageStep() / 2))); |
| 210 |
} |
| 211 |
|
| 212 |
void ImageViewer::closeEvent(QCloseEvent * event) |
| 213 |
{ |
| 214 |
this->graphWindow->previewFrm = nullptr; |
| 215 |
event->accept(); |
| 216 |
|
| 217 |
} |