KNewStuff

imagepreviewwidget.cpp
1 /*
2  Copyright (C) 2010 Frederik Gladhorn <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "imagepreviewwidget_p.h"
19 
20 #include <QPaintEvent>
21 #include <QPainter>
22 
23 #include <qstandardpaths.h>
24 
25 #include <core/entryinternal.h>
26 
27 using namespace KNS3;
28 
29 ImagePreviewWidget::ImagePreviewWidget(QWidget *parent) :
30  QWidget(parent)
31 {
32  //installEventFilter(this);
33 }
34 
35 void ImagePreviewWidget::setImage(const QImage &preview)
36 {
37  m_image = preview;
38  m_scaledImage = QImage();
39  updateGeometry();
40  repaint();
41 }
42 
43 void ImagePreviewWidget::mousePressEvent(QMouseEvent *event)
44 {
46  emit clicked();
47 }
48 
49 void ImagePreviewWidget::resizeEvent(QResizeEvent *event)
50 {
51  QWidget::resizeEvent(event);
52  m_scaledImage = QImage();
53  repaint();
54 }
55 
56 void ImagePreviewWidget::paintEvent(QPaintEvent * /*event*/)
57 {
58  if (m_image.isNull()) {
59  return;
60  }
61 
62  QPainter painter(this);
63  int margin = painter.fontMetrics().height() / 2;
64  //painter.drawImage(contentsRect(), m_image);
65 
66  int width = contentsRect().width();
67  int height = contentsRect().height();
68 
69  if (m_scaledImage.isNull()) {
70  QSize scaled = QSize(qMin(width - 2 * margin, m_image.width() * 2), qMin(height - 2 * margin, m_image.height() * 2));
71  m_scaledImage = m_image.scaled(scaled, Qt::KeepAspectRatio, Qt::SmoothTransformation);
72  }
73 
74  QPoint point;
75 
76  point.setX(contentsRect().left() + ((width - m_scaledImage.width()) / 2));
77  point.setY(contentsRect().top() + ((height - m_scaledImage.height()) / 2));
78 
79  QPoint framePoint(point.x() - 5, point.y() - 5);
80  painter.drawImage(point, m_scaledImage);
81 }
82 
83 QSize ImagePreviewWidget::sizeHint() const
84 {
85  if (m_image.isNull()) {
86  return QSize();
87  }
88  QSize sh = m_image.size();
89  sh.scale(maximumSize(), Qt::KeepAspectRatio);
90  return sh;
91 }
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)
Definition: button.cpp:29
SmoothTransformation
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 22:43:40 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.