File indexing completed on 2024-04-14 05:38:06

0001 /***************************************************************************
0002  *   Copyright (C) 2009-2018 by Daniel Nicoletti                           *
0003  *   dantti12@gmail.com                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; see the file COPYING. If not, write to       *
0017  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
0018  *   Boston, MA 02110-1301, USA.                                           *
0019  ***************************************************************************/
0020 
0021 #include "ScreenShotViewer.h"
0022 
0023 #include <QIcon>
0024 #include <QDir>
0025 #include <QParallelAnimationGroup>
0026 #include <QPropertyAnimation>
0027 #include <QGraphicsOpacityEffect>
0028 #include <QTemporaryFile>
0029 #include <KPixmapSequence>
0030 #include <KIO/Job>
0031 
0032 #include <KIconLoader>
0033 #include <KLocalizedString>
0034 
0035 #include "ClickableLabel.h"
0036 
0037 ScreenShotViewer::ScreenShotViewer(const QUrl &url, QWidget *parent)
0038  : QScrollArea(parent)
0039 {
0040     m_screenshotL = new ClickableLabel(this);
0041     m_screenshotL->setCursor(Qt::PointingHandCursor);
0042     m_screenshotL->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
0043     m_screenshotL->resize(250, 200);
0044     resize(250, 200);
0045 
0046     setFrameShape(NoFrame);
0047     setFrameShadow(Plain);
0048     setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
0049     setWidget(m_screenshotL);
0050     setWindowIcon(QIcon::fromTheme(QLatin1String("layer-visible-on")));
0051 
0052     auto tempFile = new QTemporaryFile(QDir::tempPath() + QLatin1String("/apper.XXXXXX.png"));
0053     tempFile->open();
0054     KIO::FileCopyJob *job = KIO::file_copy(url,
0055                                            QUrl::fromLocalFile(tempFile->fileName()),
0056                                            -1,
0057                                            KIO::Overwrite | KIO::HideProgressInfo);
0058     connect(job, &KIO::FileCopyJob::result, this, &ScreenShotViewer::resultJob);
0059 
0060     m_busySeq = new KPixmapSequenceOverlayPainter(this);
0061     m_busySeq->setSequence(KIconLoader::global()->loadPixmapSequence(QLatin1String("process-working"), KIconLoader::SizeSmallMedium));
0062     m_busySeq->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
0063     m_busySeq->setWidget(m_screenshotL);
0064     m_busySeq->start();
0065 
0066     connect(m_screenshotL, SIGNAL(clicked()), this, SLOT(deleteLater()));
0067 }
0068 
0069 ScreenShotViewer::~ScreenShotViewer()
0070 {
0071 }
0072 
0073 void ScreenShotViewer::resultJob(KJob *job)
0074 {
0075     m_busySeq->stop();
0076     auto fJob = qobject_cast<KIO::FileCopyJob*>(job);
0077     if (!fJob->error()) {
0078         m_screenshot = QPixmap(fJob->destUrl().toLocalFile());
0079 
0080         QPropertyAnimation *anim1 = new QPropertyAnimation(this, "size");
0081         anim1->setDuration(500);
0082         anim1->setStartValue(size());
0083         anim1->setEndValue(m_screenshot.size());
0084         anim1->setEasingCurve(QEasingCurve::OutCubic);
0085 
0086         connect(anim1, &QPropertyAnimation::finished, this, &ScreenShotViewer::fadeIn);
0087         anim1->start();
0088     } else {
0089         m_screenshotL->setText(i18n("Could not find screen shot."));
0090     }
0091 }
0092 
0093 void ScreenShotViewer::fadeIn()
0094 {
0095     auto effect = new QGraphicsOpacityEffect(m_screenshotL);
0096     effect->setOpacity(0);
0097     auto anim = new QPropertyAnimation(effect, "opacity");
0098     anim->setDuration(500);
0099     anim->setStartValue(qreal(0));
0100     anim->setEndValue(qreal(1));
0101     m_screenshotL->setGraphicsEffect(effect);
0102     m_screenshotL->setPixmap(m_screenshot);
0103     m_screenshotL->adjustSize();
0104 
0105     anim->start();
0106 }
0107 
0108 #include "moc_ScreenShotViewer.cpp"