File indexing completed on 2024-03-24 05:57:08
0001 /** 0002 * SPDX-FileCopyrightText: 2021 by Alexander Stippich <a.stippich@gmx.net> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "InProgressPainter.h" 0008 0009 #include <QPainter> 0010 0011 #include "skanpage_debug.h" 0012 0013 InProgressPainter::InProgressPainter(QQuickItem *parent) : QQuickPaintedItem(parent) 0014 { 0015 } 0016 0017 InProgressPainter::~InProgressPainter() 0018 { 0019 if (m_scanInterface != nullptr) { 0020 m_scanInterface->unlockScanImage(); 0021 } 0022 } 0023 0024 void InProgressPainter::initialize(Skanpage *skanpageApp) 0025 { 0026 m_scanInterface = skanpageApp->ksaneInterface(); 0027 connect(skanpageApp, &Skanpage::progressChanged, this, &InProgressPainter::updateImage); 0028 } 0029 0030 void InProgressPainter::paint(QPainter *painter) 0031 { 0032 if (m_scanInterface != nullptr && m_progress >= 0) { 0033 m_scanInterface->lockScanImage(); 0034 0035 const int imageHeight = m_scanInterface->scanImage()->height(); 0036 const int imageWidth = m_scanInterface->scanImage()->width(); 0037 const int itemHeight = height(); 0038 const int itemWidth = width(); 0039 0040 double scaleHeight = static_cast<double>(itemHeight)/imageHeight; 0041 double scaleWidth = static_cast<double>(itemWidth)/imageWidth; 0042 double scale = qMin(scaleHeight, qMin(scaleWidth, 1.0)); 0043 painter->drawImage(QRectF((itemWidth - scale * imageWidth)/2, (itemHeight - scale * imageHeight)/2, scale * imageWidth, scale * imageHeight), *m_scanInterface->scanImage()); 0044 0045 m_scanInterface->unlockScanImage(); 0046 } else { 0047 painter->fillRect(QRect(0, 0, width(), height()), QColorConstants::Transparent); 0048 } 0049 } 0050 0051 void InProgressPainter::updateImage(int progress) 0052 { 0053 m_progress = progress; 0054 update(); 0055 } 0056 0057 #include "moc_InProgressPainter.cpp"