KNewStuff

imagepreviewwidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Frederik Gladhorn <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #include "imagepreviewwidget_p.h"
8 
9 #include <QPaintEvent>
10 #include <QPainter>
11 
12 #include <qstandardpaths.h>
13 
14 #include <core/entryinternal.h>
15 
16 using namespace KNS3;
17 
18 ImagePreviewWidget::ImagePreviewWidget(QWidget *parent) :
19  QWidget(parent)
20 {
21  //installEventFilter(this);
22 }
23 
24 void ImagePreviewWidget::setImage(const QImage &preview)
25 {
26  m_image = preview;
27  m_scaledImage = QImage();
28  updateGeometry();
29  repaint();
30 }
31 
32 void ImagePreviewWidget::mousePressEvent(QMouseEvent *event)
33 {
35  Q_EMIT clicked();
36 }
37 
38 void ImagePreviewWidget::resizeEvent(QResizeEvent *event)
39 {
40  QWidget::resizeEvent(event);
41  m_scaledImage = QImage();
42  repaint();
43 }
44 
45 void ImagePreviewWidget::paintEvent(QPaintEvent * /*event*/)
46 {
47  if (m_image.isNull()) {
48  return;
49  }
50 
51  QPainter painter(this);
52  int margin = painter.fontMetrics().height() / 2;
53  //painter.drawImage(contentsRect(), m_image);
54 
55  int width = contentsRect().width();
56  int height = contentsRect().height();
57 
58  if (m_scaledImage.isNull()) {
59  QSize scaled = QSize(qMin(width - 2 * margin, m_image.width() * 2), qMin(height - 2 * margin, m_image.height() * 2));
60  m_scaledImage = m_image.scaled(scaled, Qt::KeepAspectRatio, Qt::SmoothTransformation);
61  }
62 
63  QPoint point;
64 
65  point.setX(contentsRect().left() + ((width - m_scaledImage.width()) / 2));
66  point.setY(contentsRect().top() + ((height - m_scaledImage.height()) / 2));
67 
68  QPoint framePoint(point.x() - 5, point.y() - 5);
69  painter.drawImage(point, m_scaledImage);
70 }
71 
72 QSize ImagePreviewWidget::sizeHint() const
73 {
74  if (m_image.isNull()) {
75  return QSize();
76  }
77  QSize sh = m_image.size();
78  sh.scale(maximumSize(), Qt::KeepAspectRatio);
79  return sh;
80 }
void scale(int width, int height, Qt::AspectRatioMode mode)
int x() const const
int y() const const
QTextStream & left(QTextStream &stream)
virtual void mousePressEvent(QMouseEvent *event)
KeepAspectRatio
void setX(int x)
void setY(int y)
virtual void resizeEvent(QResizeEvent *event)
The namespace for the KNewStuff classes.
Definition: button.cpp:19
SmoothTransformation
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 18 2021 22:43:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.