ExportManager.h 3.47 KB
Newer Older
Boudhayan Gupta's avatar
Boudhayan Gupta committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 *  Copyright (C) 2015 Boudhayan Gupta <bgupta@kde.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#ifndef EXPORTMANAGER_H
#define EXPORTMANAGER_H

#include <QObject>
#include <QIODevice>
25
#include <QMap>
Boudhayan Gupta's avatar
Boudhayan Gupta committed
26
#include <QPrinter>
Boudhayan Gupta's avatar
Boudhayan Gupta committed
27
#include <QPixmap>
28
#include <QDateTime>
Boudhayan Gupta's avatar
Boudhayan Gupta committed
29 30
#include <QUrl>

31 32
#include "PlatformBackends/ImageGrabber.h"

33 34
class QTemporaryDir;

Boudhayan Gupta's avatar
Boudhayan Gupta committed
35 36 37 38 39 40 41 42 43 44 45 46
class ExportManager : public QObject
{
    Q_OBJECT

    // singleton-ize the class

    public:

    static ExportManager* instance();

    private:

Laurent Montel's avatar
Laurent Montel committed
47
    explicit ExportManager(QObject *parent = nullptr);
Boudhayan Gupta's avatar
Boudhayan Gupta committed
48 49 50 51 52 53 54 55 56 57
    virtual ~ExportManager();

    ExportManager(ExportManager const&) = delete;
    void operator= (ExportManager const&) = delete;

    // now the usual stuff

    public:

    Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap NOTIFY pixmapChanged)
58 59
    Q_PROPERTY(QString windowTitle READ windowTitle WRITE setWindowTitle)
    Q_PROPERTY(ImageGrabber::GrabMode grabMode READ grabMode WRITE setGrabMode)
Boudhayan Gupta's avatar
Boudhayan Gupta committed
60

61
    QString defaultSaveLocation() const;
62
    bool isFileExists(const QUrl &url) const;
Boudhayan Gupta's avatar
Boudhayan Gupta committed
63 64
    void setPixmap(const QPixmap &pixmap);
    QPixmap pixmap() const;
65
    void updatePixmapTimestamp();
66 67 68 69
    void setWindowTitle(const QString &windowTitle);
    QString windowTitle() const;
    ImageGrabber::GrabMode grabMode() const;
    void setGrabMode(const ImageGrabber::GrabMode &grabMode);
Boudhayan Gupta's avatar
Boudhayan Gupta committed
70

71 72
    static const QMap<QString, QString> filenamePlaceholders;

73
    Q_SIGNALS:
Boudhayan Gupta's avatar
Boudhayan Gupta committed
74 75 76

    void errorMessage(const QString &str);
    void pixmapChanged(const QPixmap &pixmap);
Boudhayan Gupta's avatar
Boudhayan Gupta committed
77
    void imageSaved(const QUrl &savedAt);
78
    void forceNotify(const QUrl &savedAt);
Boudhayan Gupta's avatar
Boudhayan Gupta committed
79

80
    public Q_SLOTS:
Boudhayan Gupta's avatar
Boudhayan Gupta committed
81 82

    QUrl getAutosaveFilename();
83
    QUrl tempSave(const QString &mimetype = QStringLiteral("png"));
Boudhayan Gupta's avatar
Boudhayan Gupta committed
84

85
    void doSave(const QUrl &url = QUrl(), bool notify = false);
Laurent Montel's avatar
Laurent Montel committed
86
    bool doSaveAs(QWidget *parentWindow = nullptr, bool notify = false);
Boudhayan Gupta's avatar
Boudhayan Gupta committed
87 88
    void doCopyToClipboard();
    void doPrint(QPrinter *printer);
Boudhayan Gupta's avatar
Boudhayan Gupta committed
89 90 91

    private:

92
    QString truncatedFilename(const QString &filename);
Boudhayan Gupta's avatar
Boudhayan Gupta committed
93
    QString makeAutosaveFilename();
94 95 96
    using FileNameAlreadyUsedCheck = bool (ExportManager::*)(const QUrl&) const;
    QString autoIncrementFilename(const QString &baseName, const QString &extension,
                                  FileNameAlreadyUsedCheck isFileNameUsed);
Boudhayan Gupta's avatar
Boudhayan Gupta committed
97 98
    QString makeSaveMimetype(const QUrl &url);
    bool writeImage(QIODevice *device, const QByteArray &format);
Boudhayan Gupta's avatar
Boudhayan Gupta committed
99
    bool save(const QUrl &url);
Boudhayan Gupta's avatar
Boudhayan Gupta committed
100 101
    bool localSave(const QUrl &url, const QString &mimetype);
    bool remoteSave(const QUrl &url, const QString &mimetype);
102
    bool isTempFileAlreadyUsed(const QUrl &url) const;
Boudhayan Gupta's avatar
Boudhayan Gupta committed
103 104

    QPixmap mSavePixmap;
105
    QDateTime mPixmapTimestamp;
Boudhayan Gupta's avatar
Boudhayan Gupta committed
106
    QUrl mTempFile;
107 108
    QTemporaryDir *mTempDir;
    QList<QUrl> mUsedTempFileNames;
109 110
    QString mWindowTitle;
    ImageGrabber::GrabMode mGrabMode;
Boudhayan Gupta's avatar
Boudhayan Gupta committed
111 112 113
};

#endif // EXPORTMANAGER_H