| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
**************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#include <QAction> |
| 26 |
#include <QApplication> |
| 27 |
#include <QContextMenuEvent> |
| 28 |
#include <QClipboard> |
| 29 |
#include <QCursor> |
| 30 |
#include <QFileInfo> |
| 31 |
#include <QImageReader> |
| 32 |
#include <QLabel> |
| 33 |
#include <QMenu> |
| 34 |
#include <QMessageBox> |
| 35 |
#include <QMimeData> |
| 36 |
#include <QPainter> |
| 37 |
#include <QPixmap> |
| 38 |
#include <QPrintDialog> |
| 39 |
#include <QPrinter> |
| 40 |
#include <QScrollArea> |
| 41 |
#include <QScrollBar> |
| 42 |
|
| 43 |
|
| 44 |
#include "ImageView.h" |
| 45 |
#include "BitmapFactory.h" |
| 46 |
|
| 47 |
using namespace Gui; |
| 48 |
|
| 49 |
ImageView::ImageView(QWidget* parent) |
| 50 |
: MDIView(nullptr, parent) |
| 51 |
, imageLabel(new QLabel) |
| 52 |
, scrollArea(new QScrollArea) |
| 53 |
, scaleFactor {1.0} |
| 54 |
, dragging {false} |
| 55 |
{ |
| 56 |
imageLabel->setBackgroundRole(QPalette::Base); |
| 57 |
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); |
| 58 |
imageLabel->setScaledContents(true); |
| 59 |
|
| 60 |
scrollArea->setBackgroundRole(QPalette::Dark); |
| 61 |
scrollArea->setWidget(imageLabel); |
| 62 |
scrollArea->setVisible(false); |
| 63 |
setCentralWidget(scrollArea); |
| 64 |
setAcceptDrops(true); |
| 65 |
setWindowIcon(Gui::BitmapFactory().pixmap("colors")); |
| 66 |
} |
| 67 |
|
| 68 |
bool ImageView::loadFile(const QString& fileName) |
| 69 |
{ |
| 70 |
QImageReader reader(fileName); |
| 71 |
reader.setAutoTransform(true); |
| 72 |
QImage image = reader.read(); |
| 73 |
if (image.isNull()) { |
| 74 |
QMessageBox::information( |
| 75 |
this, |
| 76 |
tr("Failed to load image file"), |
| 77 |
tr("Cannot load file %1: %2").arg(fileName, reader.errorString()) |
| 78 |
); |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
setImage(image); |
| 83 |
setWindowFilePath(fileName); |
| 84 |
|
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
void ImageView::setImage(const QImage& image) |
| 89 |
{ |
| 90 |
rawImage = image; |
| 91 |
imageLabel->setPixmap(QPixmap::fromImage(image)); |
| 92 |
imageLabel->adjustSize(); |
| 93 |
scrollArea->setVisible(true); |
| 94 |
scaleFactor = 1.0; |
| 95 |
} |
| 96 |
|
| 97 |
void ImageView::scaleImage(double factor) |
| 98 |
{ |
| 99 |
scaleFactor *= factor; |
| 100 |
|
| 101 |
imageLabel->resize(scaleFactor * imageLabel->pixmap(Qt::ReturnByValue).size()); |
| 102 |
|
| 103 |
adjustScrollBar(scrollArea->horizontalScrollBar(), factor); |
| 104 |
adjustScrollBar(scrollArea->verticalScrollBar(), factor); |
| 105 |
} |
| 106 |
|
| 107 |
void ImageView::adjustScrollBar(QScrollBar* scrollBar, double factor) |
| 108 |
{ |
| 109 |
scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep() / 2))); |
| 110 |
} |
| 111 |
|
| 112 |
bool ImageView::canZoomIn() const |
| 113 |
{ |
| 114 |
int maxWidth {10000}; |
| 115 |
return !isFitToWindow() && imageLabel->width() < maxWidth; |
| 116 |
} |
| 117 |
|
| 118 |
bool ImageView::canZoomOut() const |
| 119 |
{ |
| 120 |
int minWidth {200}; |
| 121 |
return !isFitToWindow() && imageLabel->width() > minWidth; |
| 122 |
} |
| 123 |
|
| 124 |
void ImageView::zoomIn() |
| 125 |
{ |
| 126 |
double scale {1.25}; |
| 127 |
scaleImage(scale); |
| 128 |
} |
| 129 |
|
| 130 |
void ImageView::zoomOut() |
| 131 |
{ |
| 132 |
double scale {0.8}; |
| 133 |
scaleImage(scale); |
| 134 |
} |
| 135 |
|
| 136 |
void ImageView::normalSize() |
| 137 |
{ |
| 138 |
imageLabel->adjustSize(); |
| 139 |
scaleFactor = 1.0; |
| 140 |
} |
| 141 |
|
| 142 |
void ImageView::fitToWindow(bool fitView) |
| 143 |
{ |
| 144 |
scrollArea->setWidgetResizable(fitView); |
| 145 |
if (!fitView) { |
| 146 |
normalSize(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
bool ImageView::isFitToWindow() const |
| 151 |
{ |
| 152 |
return scrollArea->widgetResizable(); |
| 153 |
} |
| 154 |
|
| 155 |
bool ImageView::canDrag() const |
| 156 |
{ |
| 157 |
return scrollArea->verticalScrollBar()->isVisible() |
| 158 |
|| scrollArea->horizontalScrollBar()->isVisible(); |
| 159 |
} |
| 160 |
|
| 161 |
void ImageView::startDrag() |
| 162 |
{ |
| 163 |
dragging = true; |
| 164 |
} |
| 165 |
|
| 166 |
void ImageView::stopDrag() |
| 167 |
{ |
| 168 |
dragging = false; |
| 169 |
} |
| 170 |
|
| 171 |
bool ImageView::isDragging() const |
| 172 |
{ |
| 173 |
return dragging; |
| 174 |
} |
| 175 |
|
| 176 |
void ImageView::contextMenuEvent(QContextMenuEvent* event) |
| 177 |
{ |
| 178 |
QMenu menu; |
| 179 |
QAction* fitToWindowAct = menu.addAction(tr("Fit to Window")); |
| 180 |
fitToWindowAct->setCheckable(true); |
| 181 |
fitToWindowAct->setChecked(isFitToWindow()); |
| 182 |
connect(fitToWindowAct, &QAction::toggled, this, &ImageView::fitToWindow); |
| 183 |
|
| 184 |
QAction* zoomInAct = menu.addAction(tr("Zoom In"), this, &ImageView::zoomIn); |
| 185 |
zoomInAct->setEnabled(canZoomIn()); |
| 186 |
|
| 187 |
QAction* zoomOutAct = menu.addAction(tr("Zoom Out"), this, &ImageView::zoomOut); |
| 188 |
zoomOutAct->setEnabled(canZoomOut()); |
| 189 |
|
| 190 |
menu.exec(event->globalPos()); |
| 191 |
} |
| 192 |
|
| 193 |
void ImageView::mousePressEvent(QMouseEvent* event) |
| 194 |
{ |
| 195 |
if (event->buttons().testFlag(Qt::MiddleButton)) { |
| 196 |
if (canDrag()) { |
| 197 |
setCursor(QCursor(Qt::ClosedHandCursor)); |
| 198 |
startDrag(); |
| 199 |
dragPos = event->pos(); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
void ImageView::mouseReleaseEvent(QMouseEvent* event) |
| 205 |
{ |
| 206 |
if (!event->buttons().testFlag(Qt::MiddleButton)) { |
| 207 |
if (isDragging()) { |
| 208 |
stopDrag(); |
| 209 |
unsetCursor(); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
void ImageView::mouseMoveEvent(QMouseEvent* event) |
| 215 |
{ |
| 216 |
if (isDragging()) { |
| 217 |
QScrollBar* hBar = scrollArea->horizontalScrollBar(); |
| 218 |
QScrollBar* vBar = scrollArea->verticalScrollBar(); |
| 219 |
QPoint delta = event->pos() - dragPos; |
| 220 |
hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x())); |
| 221 |
vBar->setValue(vBar->value() - delta.y()); |
| 222 |
dragPos = event->pos(); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
void ImageView::dropEvent(QDropEvent* event) |
| 227 |
{ |
| 228 |
const QMimeData* data = event->mimeData(); |
| 229 |
if (data->hasUrls()) { |
| 230 |
loadImageFromUrl(data->urls()); |
| 231 |
} |
| 232 |
else { |
| 233 |
MDIView::dropEvent(event); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void ImageView::dragEnterEvent(QDragEnterEvent* event) |
| 238 |
{ |
| 239 |
const QMimeData* data = event->mimeData(); |
| 240 |
if (data->hasUrls()) { |
| 241 |
event->accept(); |
| 242 |
} |
| 243 |
else { |
| 244 |
event->ignore(); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
bool ImageView::isImageFormat(const QFileInfo& fileInfo) |
| 249 |
{ |
| 250 |
QString ext = fileInfo.suffix().toLower(); |
| 251 |
QByteArray suffix = ext.toLatin1(); |
| 252 |
QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats(); |
| 253 |
auto it = std::find_if( |
| 254 |
supportedFormats.begin(), |
| 255 |
supportedFormats.end(), |
| 256 |
[suffix](const QByteArray& image) { return (image == suffix); } |
| 257 |
); |
| 258 |
|
| 259 |
return (it != supportedFormats.end()); |
| 260 |
} |
| 261 |
|
| 262 |
void ImageView::loadImageFromUrl(const QList<QUrl>& urls) |
| 263 |
{ |
| 264 |
if (urls.isEmpty()) { |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
const QUrl& url = urls.first(); |
| 269 |
const QFileInfo info(url.toLocalFile()); |
| 270 |
if (info.exists() && info.isFile()) { |
| 271 |
if (isImageFormat(info)) { |
| 272 |
loadFile(info.absoluteFilePath()); |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
void ImageView::pasteImage() |
| 278 |
{ |
| 279 |
QImage image = imageFromClipboard(); |
| 280 |
if (!image.isNull()) { |
| 281 |
setImage(image); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
bool ImageView::canPasteImage() const |
| 286 |
{ |
| 287 |
return !imageFromClipboard().isNull(); |
| 288 |
} |
| 289 |
|
| 290 |
QImage ImageView::imageFromClipboard() |
| 291 |
{ |
| 292 |
QImage image; |
| 293 |
if (const QMimeData* mimeData = QApplication::clipboard()->mimeData()) { |
| 294 |
if (mimeData->hasImage()) { |
| 295 |
image = qvariant_cast<QImage>(mimeData->imageData()); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
return image; |
| 300 |
} |
| 301 |
|
| 302 |
void ImageView::print(QPrinter* printer) |
| 303 |
{ |
| 304 |
QPainter painter(printer); |
| 305 |
QPixmap pixmap = QPixmap::fromImage(rawImage); |
| 306 |
QRect rect = painter.viewport(); |
| 307 |
QSize size = pixmap.size(); |
| 308 |
size.scale(rect.size(), Qt::KeepAspectRatio); |
| 309 |
painter.setViewport(rect.x(), rect.y(), size.width(), size.height()); |
| 310 |
painter.setWindow(pixmap.rect()); |
| 311 |
painter.drawPixmap(0, 0, pixmap); |
| 312 |
} |
| 313 |
|
| 314 |
bool ImageView::onMsg(const char* pMsg, const char** ppReturn) |
| 315 |
{ |
| 316 |
Q_UNUSED(ppReturn) |
| 317 |
if (strcmp("ViewFit", pMsg) == 0) { |
| 318 |
fitToWindow(true); |
| 319 |
return true; |
| 320 |
} |
| 321 |
if (strcmp("ZoomIn", pMsg) == 0) { |
| 322 |
zoomIn(); |
| 323 |
return true; |
| 324 |
} |
| 325 |
if (strcmp("ZoomOut", pMsg) == 0) { |
| 326 |
zoomOut(); |
| 327 |
return true; |
| 328 |
} |
| 329 |
if (strcmp("Paste", pMsg) == 0) { |
| 330 |
pasteImage(); |
| 331 |
return true; |
| 332 |
} |
| 333 |
if (strcmp("Print", pMsg) == 0) { |
| 334 |
print(); |
| 335 |
return true; |
| 336 |
} |
| 337 |
if (strcmp("PrintPreview", pMsg) == 0) { |
| 338 |
printPreview(); |
| 339 |
return true; |
| 340 |
} |
| 341 |
if (strcmp("PrintPdf", pMsg) == 0) { |
| 342 |
printPdf(); |
| 343 |
return true; |
| 344 |
} |
| 345 |
|
| 346 |
return false; |
| 347 |
} |
| 348 |
|
| 349 |
bool ImageView::onHasMsg(const char* pMsg) const |
| 350 |
{ |
| 351 |
if (strcmp("ViewFit", pMsg) == 0) { |
| 352 |
return true; |
| 353 |
} |
| 354 |
if (strcmp("ZoomIn", pMsg) == 0) { |
| 355 |
return canZoomIn(); |
| 356 |
} |
| 357 |
if (strcmp("ZoomOut", pMsg) == 0) { |
| 358 |
return canZoomOut(); |
| 359 |
} |
| 360 |
if (strcmp("Paste", pMsg) == 0) { |
| 361 |
return canPasteImage(); |
| 362 |
} |
| 363 |
if (strcmp("Print", pMsg) == 0) { |
| 364 |
return true; |
| 365 |
} |
| 366 |
if (strcmp("PrintPreview", pMsg) == 0) { |
| 367 |
return true; |
| 368 |
} |
| 369 |
if (strcmp("PrintPdf", pMsg) == 0) { |
| 370 |
return true; |
| 371 |
} |
| 372 |
else if (strcmp("AllowsOverlayOnHover", pMsg) == 0) { |
| 373 |
return true; |
| 374 |
} |
| 375 |
|
| 376 |
return false; |
| 377 |
} |
| 378 |
|
| 379 |
#include "moc_ImageView.cpp" |