File indexing completed on 2025-02-23 04:34:24

0001 /**
0002  * \file imageviewer.cpp
0003  * Window to view image.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 10 Jun 2009
0008  *
0009  * Copyright (C) 2003-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "imageviewer.h"
0028 #include <QImage>
0029 #include <QLabel>
0030 #include <QPushButton>
0031 #include <QGuiApplication>
0032 #include <QScreen>
0033 #if QT_VERSION < 0x060000
0034 #include <QDesktopWidget>
0035 #endif
0036 #include <QVBoxLayout>
0037 
0038 /**
0039  * Constructor.
0040  *
0041  * @param parent parent widget
0042  * @param img    image to display in window
0043  */
0044 ImageViewer::ImageViewer(QWidget* parent, const QImage& img)
0045   : QDialog(parent)
0046 {
0047   setObjectName(QLatin1String("ImageViewer"));
0048   setModal(true);
0049   setWindowTitle(tr("View Picture"));
0050   auto vlayout = new QVBoxLayout(this);
0051   auto hlayout = new QHBoxLayout;
0052   auto hspacer = new QSpacerItem(16, 0, QSizePolicy::Expanding,
0053                                          QSizePolicy::Minimum);
0054   m_image = new QLabel(this);
0055   auto closeButton = new QPushButton(tr("&Close"), this);
0056   m_image->setScaledContents(true);
0057   QSize imageSize(img.size());
0058   QSize desktopSize(QGuiApplication::primaryScreen()->availableGeometry().size());
0059 #if QT_VERSION >= 0x060000
0060   desktopSize -= QSize(12, 12 + vlayout->spacing() + closeButton->height() +
0061                        vlayout->contentsMargins().bottom());
0062 #else
0063   desktopSize -= QSize(12, 12 + vlayout->spacing() + closeButton->height() +
0064                        vlayout->margin());
0065 #endif
0066   QPixmap pm = imageSize.width() > desktopSize.width() ||
0067                imageSize.height() > desktopSize.height()
0068       ? QPixmap::fromImage(img.scaled(desktopSize, Qt::KeepAspectRatio))
0069       : QPixmap::fromImage(img);
0070 #if QT_VERSION >= 0x050500
0071   // Try workaround for QTBUG-46846,
0072   // images are cropped on high pixel density displays.
0073   pm.setDevicePixelRatio(m_image->devicePixelRatio());
0074 #endif
0075   m_image->setPixmap(pm);
0076   vlayout->addWidget(m_image);
0077   hlayout->addItem(hspacer);
0078   hlayout->addWidget(closeButton);
0079   connect(closeButton, &QAbstractButton::clicked, this, &QDialog::accept);
0080   vlayout->addLayout(hlayout);
0081 }