File indexing completed on 2024-04-28 04:20:13

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 #define DEBUG_KP_DOCUMENT_SAVE_OPTIONS_WIDGET 0
0029 
0030 
0031 #include "kpDocumentSaveOptionsPreviewDialog.h"
0032 
0033 #include <QGridLayout>
0034 #include <QLabel>
0035 #include <QPixmap>
0036 
0037 #include "kpLogCategories.h"
0038 #include <KLocalizedString>
0039 
0040 #include "commands/kpCommandSize.h"
0041 #include "kpDefs.h"
0042 #include "document/kpDocument.h"
0043 #include "pixmapfx/kpPixmapFX.h"
0044 #include "generic/widgets/kpResizeSignallingLabel.h"
0045 #include "dialogs/imagelib/transforms/kpTransformPreviewDialog.h"
0046 
0047 
0048 // protected static
0049 const QSize kpDocumentSaveOptionsPreviewDialog::s_pixmapLabelMinimumSize (25, 25);
0050 
0051 
0052 kpDocumentSaveOptionsPreviewDialog::kpDocumentSaveOptionsPreviewDialog (
0053     QWidget *parent )
0054     : kpSubWindow (parent),
0055       m_filePixmap (nullptr),
0056       m_fileSize (0)
0057 {
0058     setWindowTitle (i18nc ("@title:window", "Save Preview"));
0059 
0060     auto *baseWidget = this;//new QWidget (this);
0061     //setMainWidget (baseWidget);
0062 
0063 
0064     auto *lay = new QGridLayout ( baseWidget );
0065 
0066     m_filePixmapLabel = new kpResizeSignallingLabel (baseWidget);
0067     m_fileSizeLabel = new QLabel (baseWidget);
0068 
0069 
0070     m_filePixmapLabel->setMinimumSize (s_pixmapLabelMinimumSize);
0071 
0072 
0073     lay->addWidget (m_filePixmapLabel, 0, 0);
0074     lay->addWidget (m_fileSizeLabel, 1, 0, Qt::AlignHCenter);
0075 
0076 
0077     lay->setRowStretch (0, 1);
0078 
0079 
0080     connect (m_filePixmapLabel, &kpResizeSignallingLabel::resized,
0081              this, &kpDocumentSaveOptionsPreviewDialog::updatePixmapPreview);
0082 }
0083 
0084 kpDocumentSaveOptionsPreviewDialog::~kpDocumentSaveOptionsPreviewDialog ()
0085 {
0086     delete m_filePixmap;
0087 }
0088 
0089 
0090 // public
0091 QSize kpDocumentSaveOptionsPreviewDialog::preferredMinimumSize () const
0092 {
0093     const auto contentsWidth = 180;
0094     const auto totalMarginsWidth = fontMetrics ().height ();
0095 
0096     return  {contentsWidth + totalMarginsWidth, contentsWidth * 3 / 4 + totalMarginsWidth};
0097 }
0098 
0099 
0100 // public slot
0101 void kpDocumentSaveOptionsPreviewDialog::setFilePixmapAndSize (const QImage &pixmap,
0102                                                                qint64 fileSize)
0103 {
0104     delete m_filePixmap;
0105     m_filePixmap = new QImage (pixmap);
0106 
0107     updatePixmapPreview ();
0108 
0109     m_fileSize = fileSize;
0110 
0111     const kpCommandSize::SizeType pixmapSize = kpCommandSize::PixmapSize (pixmap);
0112     // (int cast is safe as long as the file size is not more than 20 million
0113     //  -- i.e. INT_MAX / 100 -- times the pixmap size)
0114     const int percent = pixmapSize ?
0115                             qMax (1,
0116                                   static_cast<int> (static_cast<kpCommandSize::SizeType> (fileSize * 100 / pixmapSize))) :
0117                             0;
0118 #if DEBUG_KP_DOCUMENT_SAVE_OPTIONS_WIDGET
0119     qCDebug(kpLogDialogs) << "kpDocumentSaveOptionsPreviewDialog::setFilePixmapAndSize()"
0120                << " pixmapSize=" << pixmapSize
0121                << " fileSize=" << fileSize
0122                << " raw fileSize/pixmapSize%="
0123                << (pixmapSize ? (kpCommandSize::SizeType) fileSize * 100 / pixmapSize : 0);
0124 #endif
0125 
0126     m_fileSizeLabel->setText (i18np ("1 byte (approx. %2%)", "%1 bytes (approx. %2%)",
0127                                      m_fileSize, percent));
0128 }
0129 
0130 // public slot
0131 void kpDocumentSaveOptionsPreviewDialog::updatePixmapPreview ()
0132 {
0133 #if DEBUG_KP_DOCUMENT_SAVE_OPTIONS_WIDGET
0134     qCDebug(kpLogDialogs) << "kpDocumentSaveOptionsPreviewDialog::updatePreviewPixmap()"
0135                << " filePixmapLabel.size=" << m_filePixmapLabel->size ()
0136                << " filePixmap.size=" << m_filePixmap->size ();
0137 #endif
0138 
0139     if (m_filePixmap)
0140     {
0141         int maxNewWidth = qMin (m_filePixmap->width (),
0142                                 m_filePixmapLabel->width ()),
0143             maxNewHeight = qMin (m_filePixmap->height (),
0144                                  m_filePixmapLabel->height ());
0145 
0146         double keepsAspect = kpTransformPreviewDialog::aspectScale (
0147             maxNewWidth, maxNewHeight,
0148             m_filePixmap->width (), m_filePixmap->height ());
0149     #if DEBUG_KP_DOCUMENT_SAVE_OPTIONS_WIDGET
0150         qCDebug(kpLogDialogs) << "\tmaxNewWidth=" << maxNewWidth
0151                    << " maxNewHeight=" << maxNewHeight
0152                    << " keepsAspect=" << keepsAspect;
0153     #endif
0154 
0155         const int newWidth = kpTransformPreviewDialog::scaleDimension (
0156             m_filePixmap->width (),
0157             keepsAspect,
0158             1,
0159             maxNewWidth);
0160         const int newHeight = kpTransformPreviewDialog::scaleDimension (
0161             m_filePixmap->height (),
0162             keepsAspect,
0163             1,
0164             maxNewHeight);
0165     #if DEBUG_KP_DOCUMENT_SAVE_OPTIONS_WIDGET
0166         qCDebug(kpLogDialogs) << "\tnewWidth=" << newWidth
0167                    << " newHeight=" << newHeight;
0168     #endif
0169 
0170         QImage transformedPixmap =
0171             kpPixmapFX::scale (*m_filePixmap,
0172                                newWidth, newHeight);
0173 
0174 
0175         QImage labelPixmap (m_filePixmapLabel->width (),
0176                             m_filePixmapLabel->height (), QImage::Format_ARGB32_Premultiplied);
0177         labelPixmap.fill(QColor(Qt::transparent).rgba());
0178         kpPixmapFX::setPixmapAt (&labelPixmap,
0179             (labelPixmap.width () - transformedPixmap.width ()) / 2,
0180             (labelPixmap.height () - transformedPixmap.height ()) / 2,
0181             transformedPixmap);
0182 
0183         m_filePixmapLabel->setPixmap(
0184             QPixmap::fromImage(std::move(labelPixmap)));
0185     }
0186     else
0187     {
0188         m_filePixmapLabel->setPixmap (QPixmap ());
0189     }
0190 }
0191 
0192 
0193 // protected virtual [base QWidget]
0194 void kpDocumentSaveOptionsPreviewDialog::closeEvent (QCloseEvent *e)
0195 {
0196 #if DEBUG_KP_DOCUMENT_SAVE_OPTIONS_WIDGET
0197     qCDebug(kpLogDialogs) << "kpDocumentSaveOptionsPreviewDialog::closeEvent()";
0198 #endif
0199 
0200     QWidget::closeEvent (e);
0201 
0202     Q_EMIT finished ();
0203 }
0204 
0205 // protected virtual [base QWidget]
0206 void kpDocumentSaveOptionsPreviewDialog::moveEvent (QMoveEvent *e)
0207 {
0208 #if DEBUG_KP_DOCUMENT_SAVE_OPTIONS_WIDGET
0209     qCDebug(kpLogDialogs) << "kpDocumentSaveOptionsPreviewDialog::moveEvent()";
0210 #endif
0211 
0212     QWidget::moveEvent (e);
0213 
0214     Q_EMIT moved ();
0215 }
0216 
0217 // protected virtual [base QWidget]
0218 void kpDocumentSaveOptionsPreviewDialog::resizeEvent (QResizeEvent *e)
0219 {
0220 #if DEBUG_KP_DOCUMENT_SAVE_OPTIONS_WIDGET
0221     qCDebug(kpLogDialogs) << "kpDocumentSaveOptionsPreviewDialog::resizeEvent()";
0222 #endif
0223 
0224     QWidget::resizeEvent (e);
0225 
0226     Q_EMIT resized ();
0227 }
0228 
0229 #include "moc_kpDocumentSaveOptionsPreviewDialog.cpp"