File indexing completed on 2024-05-19 11:43:33

0001 /*
0002     SPDX-FileCopyrightText: 2010 Frederik Gladhorn <gladhorn@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "imagepreviewwidget_p.h"
0008 
0009 #include <QPaintEvent>
0010 #include <QPainter>
0011 
0012 #include <qstandardpaths.h>
0013 
0014 #include <core/entryinternal.h>
0015 
0016 using namespace KNS3;
0017 
0018 ImagePreviewWidget::ImagePreviewWidget(QWidget *parent)
0019     : QWidget(parent)
0020 {
0021     // installEventFilter(this);
0022 }
0023 
0024 void ImagePreviewWidget::setImage(const QImage &preview)
0025 {
0026     m_image = preview;
0027     m_scaledImage = QImage();
0028     updateGeometry();
0029     repaint();
0030 }
0031 
0032 void ImagePreviewWidget::mousePressEvent(QMouseEvent *event)
0033 {
0034     QWidget::mousePressEvent(event);
0035     Q_EMIT clicked();
0036 }
0037 
0038 void ImagePreviewWidget::resizeEvent(QResizeEvent *event)
0039 {
0040     QWidget::resizeEvent(event);
0041     m_scaledImage = QImage();
0042     repaint();
0043 }
0044 
0045 void ImagePreviewWidget::paintEvent(QPaintEvent * /*event*/)
0046 {
0047     if (m_image.isNull()) {
0048         return;
0049     }
0050 
0051     QPainter painter(this);
0052     int margin = painter.fontMetrics().height() / 2;
0053     // painter.drawImage(contentsRect(), m_image);
0054 
0055     int width = contentsRect().width();
0056     int height = contentsRect().height();
0057 
0058     if (m_scaledImage.isNull()) {
0059         QSize scaled = QSize(qMin(width - 2 * margin, m_image.width() * 2), qMin(height - 2 * margin, m_image.height() * 2));
0060         m_scaledImage = m_image.scaled(scaled, Qt::KeepAspectRatio, Qt::SmoothTransformation);
0061     }
0062 
0063     QPoint point;
0064 
0065     point.setX(contentsRect().left() + ((width - m_scaledImage.width()) / 2));
0066     point.setY(contentsRect().top() + ((height - m_scaledImage.height()) / 2));
0067 
0068     QPoint framePoint(point.x() - 5, point.y() - 5);
0069     painter.drawImage(point, m_scaledImage);
0070 }
0071 
0072 QSize ImagePreviewWidget::sizeHint() const
0073 {
0074     if (m_image.isNull()) {
0075         return QSize();
0076     }
0077     QSize sh = m_image.size();
0078     sh.scale(maximumSize(), Qt::KeepAspectRatio);
0079     return sh;
0080 }
0081 
0082 #include "moc_imagepreviewwidget_p.cpp"