kritacreator.cpp 3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*  This file is part of the Calligra project.
    Copyright 2015 Friedrich W. H. Kossebau <kossebau@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kritacreator.h"

// Calligra
#include <KoStore.h>

// Qt
#include <QPainter>


extern "C"
{
    KDE_EXPORT ThumbCreator *new_creator()
    {
        return new KritaCreator;
    }
}

KritaCreator::KritaCreator()
{
}

KritaCreator::~KritaCreator()
{
}

bool KritaCreator::create(const QString &path, int width, int height, QImage &image)
{
    // for now just rely on the rendered data inside the file,
    // do not load Krita code for rendering ourselves, as that currently (2.9)
    // means loading all plugins, resources etc.
    KoStore *store = KoStore::createStore(path, KoStore::Read);

    if (!store) {
        return false;
    }

    QImage thumbnail;
    bool biggerSizeNeeded = true;

    // first check if normal thumbnail is good enough
    if (// ORA thumbnail?
        store->open(QLatin1String("Thumbnails/thumbnail.png")) ||
Halla Rempt's avatar
Halla Rempt committed
62
        // KRA thumbnail?
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
        store->open(QLatin1String("preview.png"))) {

        const QByteArray thumbnailData = store->read(store->size());
        store->close();

        if (thumbnail.loadFromData(thumbnailData)) {
            biggerSizeNeeded = (thumbnail.width() < width || thumbnail.height() < height);
        }
    }

    // for bigger sizes see if full rendered image is available, both ORA & KRA
    if (biggerSizeNeeded && store->open(QLatin1String("mergedimage.png"))) {
        const QByteArray mergedImageData = store->read(store->size());

        QImage mergedImage;
        if (mergedImage.loadFromData(mergedImageData)) {
            thumbnail = mergedImage.scaled(width, height,
                                           Qt::KeepAspectRatio, Qt::SmoothTransformation);
        }
    }

    const bool createdThumbnail = !thumbnail.isNull();
    if (createdThumbnail) {
        // transparent areas put a white background behind the thumbnail
        // (or better checkerboard?)
        image = QImage(thumbnail.size(), QImage::Format_RGB32);
        image.fill(QColor(Qt::white).rgb());
        QPainter p(&image);
        p.drawImage(QPoint(0, 0), thumbnail);
    }

    delete store;

    return createdThumbnail;
}

ThumbCreator::Flags KritaCreator::flags() const
{
    return DrawFrame;
}