File indexing completed on 2025-01-05 03:59:54
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2007-09-06 0007 * Description : a widget to display camera capture preview. 0008 * 0009 * SPDX-FileCopyrightText: 2007-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> 0010 * 0011 * SPDX-License-Identifier: GPL-2.0-or-later 0012 * 0013 * ============================================================ */ 0014 0015 #include "capturewidget.h" 0016 0017 // Qt includes 0018 0019 #include <QPainter> 0020 #include <QImage> 0021 #include <QPixmap> 0022 #include <QRect> 0023 0024 // KDE includes 0025 0026 #include <klocalizedstring.h> 0027 0028 namespace Digikam 0029 { 0030 0031 class Q_DECL_HIDDEN CaptureWidget::Private 0032 { 0033 public: 0034 0035 explicit Private() 0036 { 0037 } 0038 0039 QPixmap pixmap; 0040 QImage preview; 0041 }; 0042 0043 CaptureWidget::CaptureWidget(QWidget* const parent) 0044 : QWidget(parent), 0045 d (new Private) 0046 { 0047 setAttribute(Qt::WA_DeleteOnClose); 0048 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); 0049 } 0050 0051 CaptureWidget::~CaptureWidget() 0052 { 0053 delete d; 0054 } 0055 0056 void CaptureWidget::setPreview(const QImage& preview) 0057 { 0058 d->preview = preview; 0059 d->pixmap = QPixmap(contentsRect().size()); 0060 0061 updatePixmap(); 0062 repaint(); 0063 } 0064 0065 void CaptureWidget::updatePixmap() 0066 { 0067 d->pixmap.fill(palette().window().color()); 0068 QPainter p(&(d->pixmap)); 0069 0070 if (!d->preview.isNull()) 0071 { 0072 QPixmap pix = QPixmap::fromImage(d->preview.scaled(contentsRect().size(), 0073 Qt::KeepAspectRatio, Qt::SmoothTransformation)); 0074 p.drawPixmap((contentsRect().width() - pix.width()) / 2, 0075 (contentsRect().height() - pix.height()) / 2, pix, 0076 0, 0, pix.width(), pix.height()); 0077 } 0078 else 0079 { 0080 p.setPen(QPen(palette().text().color())); 0081 p.drawText(0, 0, d->pixmap.width(), d->pixmap.height(), 0082 Qt::AlignCenter | Qt::TextWordWrap, 0083 i18n("Cannot display camera preview")); 0084 } 0085 0086 p.end(); 0087 } 0088 0089 void CaptureWidget::paintEvent(QPaintEvent*) 0090 { 0091 QPainter p(this); 0092 p.drawPixmap(contentsRect().top(), contentsRect().left(), d->pixmap); 0093 p.end(); 0094 } 0095 0096 void CaptureWidget::resizeEvent(QResizeEvent*) 0097 { 0098 blockSignals(true); 0099 d->pixmap = QPixmap(contentsRect().size()); 0100 updatePixmap(); 0101 repaint(); 0102 blockSignals(false); 0103 } 0104 0105 } // namespace Digikam 0106 0107 #include "moc_capturewidget.cpp"