KNewStuff
imagepreviewwidget.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright (C) 2010 Frederik Gladhorn <gladhorn@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Lesser General Public 00006 License as published by the Free Software Foundation; either 00007 version 2.1 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Lesser General Public License for more details. 00013 00014 You should have received a copy of the GNU Lesser General Public 00015 License along with this library. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #include "imagepreviewwidget.h" 00019 00020 #include <QtGui/QPaintEvent> 00021 #include <QtGui/QPainter> 00022 00023 #include <kstandarddirs.h> 00024 00025 #include <core/entryinternal.h> 00026 00027 using namespace KNS3; 00028 00029 ImagePreviewWidget::ImagePreviewWidget(QWidget *parent) : 00030 QWidget(parent) 00031 { 00032 //installEventFilter(this); 00033 00034 QString framefile = KStandardDirs::locate("data", "knewstuff/pics/thumb_frame.png"); 00035 m_frameImage = QPixmap(framefile); 00036 } 00037 00038 void ImagePreviewWidget::setImage(const QImage &preview) 00039 { 00040 m_image = preview; 00041 m_scaledImage = QImage(); 00042 updateGeometry(); 00043 repaint(); 00044 } 00045 00046 void ImagePreviewWidget::mousePressEvent(QMouseEvent* event) 00047 { 00048 QWidget::mousePressEvent(event); 00049 emit clicked(); 00050 } 00051 00052 void ImagePreviewWidget::resizeEvent(QResizeEvent* event) 00053 { 00054 QWidget::resizeEvent(event); 00055 m_scaledImage = QImage(); 00056 repaint(); 00057 } 00058 00059 void ImagePreviewWidget::paintEvent(QPaintEvent *event) 00060 { 00061 if (m_image.isNull()) { 00062 return; 00063 } 00064 00065 QPainter painter(this); 00066 int margin = painter.fontMetrics().height() / 2; 00067 //painter.drawImage(contentsRect(), m_image); 00068 00069 int width = contentsRect().width(); 00070 int height = contentsRect().height(); 00071 00072 if (m_scaledImage.isNull()) { 00073 QSize scaled = QSize(qMin(width - 2*margin, m_image.width()*2), qMin(height - 2*margin, m_image.height()*2)); 00074 m_scaledImage = m_image.scaled(scaled, Qt::KeepAspectRatio, Qt::SmoothTransformation); 00075 } 00076 00077 QPoint point; 00078 00079 point.setX(contentsRect().left() + ((width - m_scaledImage.width()) / 2)); 00080 point.setY(contentsRect().top() + ((height - m_scaledImage.height()) / 2)); 00081 00082 QPoint framePoint(point.x() - 5, point.y() - 5); 00083 painter.drawPixmap(framePoint, m_frameImage.scaled(m_scaledImage.width() + 10, m_scaledImage.height() + 10)); 00084 painter.drawImage(point, m_scaledImage); 00085 } 00086 00087 QSize ImagePreviewWidget::sizeHint() const 00088 { 00089 if (m_image.isNull()) { 00090 return QSize(); 00091 } 00092 QSize sh = m_image.size(); 00093 sh.scale(maximumSize(), Qt::KeepAspectRatio); 00094 return sh; 00095 }
KDE 4.6 API Reference