kis_custom_pattern.cc 5.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  Copyright (c) 2006 Bart Coppens <kde@bartcoppens.be>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU 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 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.
 */

Boudewijn Rempt's avatar
Boudewijn Rempt committed
19 20
#include "kis_custom_pattern.h"

Boudewijn Rempt's avatar
Boudewijn Rempt committed
21
#include <KoResourceServerProvider.h>
22
#include <resources/KoPattern.h>
Boudewijn Rempt's avatar
Boudewijn Rempt committed
23

24
#include <QLabel>
25
#include <QImage>
26 27
#include <QPushButton>
#include <QComboBox>
Laurent Montel's avatar
Laurent Montel committed
28 29
#include <QPixmap>
#include <QShowEvent>
Boudewijn Rempt's avatar
Boudewijn Rempt committed
30

31

32
#include <QTemporaryFile>
33

34 35
#include "KisDocument.h"
#include "KisViewManager.h"
36 37
#include "kis_image.h"
#include "kis_layer.h"
38
#include "kis_paint_device.h"
Boudewijn Rempt's avatar
Boudewijn Rempt committed
39

Boudewijn Rempt's avatar
Boudewijn Rempt committed
40
#include <kis_debug.h>
41
#include "kis_resource_server_provider.h"
42 43
#include "kis_paint_layer.h"

44
KisCustomPattern::KisCustomPattern(QWidget *parent, const char* name, const QString& caption, KisViewManager* view)
Boudewijn Rempt's avatar
Boudewijn Rempt committed
45
    : KisWdgCustomPattern(parent, name), m_view(view)
46 47
{
    Q_ASSERT(m_view);
Adrian Page's avatar
Adrian Page committed
48
    setWindowTitle(caption);
49 50 51

    m_pattern = 0;

52
    preview->setScaledContents(true);
53

54
    KoResourceServer<KoPattern>* rServer = KoResourceServerProvider::instance()->patternServer(false);
55
    m_rServerAdapter = QSharedPointer<KoAbstractResourceServerAdapter>(new KoResourceServerAdapter<KoPattern>(rServer));
56

57 58
    connect(addButton, SIGNAL(pressed()), this, SLOT(slotAddPredefined()));
    connect(patternButton, SIGNAL(pressed()), this, SLOT(slotUsePattern()));
59 60
    connect(updateButton, SIGNAL(pressed()), this, SLOT(slotUpdateCurrentPattern()));
    connect(cmbSource, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateCurrentPattern()));
61 62
}

Boudewijn Rempt's avatar
Boudewijn Rempt committed
63 64
KisCustomPattern::~KisCustomPattern()
{
65 66 67
    delete m_pattern;
}

68
void KisCustomPattern::slotUpdateCurrentPattern()
Boudewijn Rempt's avatar
Boudewijn Rempt committed
69
{
70
    delete m_pattern;
71
    m_pattern = 0;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
72
    if (m_view && m_view->image()) {
73
        createPattern();
Boudewijn Rempt's avatar
Boudewijn Rempt committed
74
        if (m_pattern) {
Edward Apap's avatar
Edward Apap committed
75
            const qint32 maxSize = 150;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
76
            if ((m_pattern->width() > maxSize) || (m_pattern->height() > maxSize)) {
Edward Apap's avatar
Edward Apap committed
77 78
                float aspectRatio = (float)m_pattern->width() / m_pattern->height();
                qint32 scaledWidth, scaledHeight;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
79

Edward Apap's avatar
Edward Apap committed
80 81 82
                if (m_pattern->width() > m_pattern->height()) {
                    scaledWidth = maxSize;
                    scaledHeight = maxSize / aspectRatio;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
83
                } else {
Edward Apap's avatar
Edward Apap committed
84 85 86
                    scaledWidth = maxSize * aspectRatio;
                    scaledHeight = maxSize;
                }
Boudewijn Rempt's avatar
Boudewijn Rempt committed
87

Edward Apap's avatar
Edward Apap committed
88 89
                if (scaledWidth == 0) scaledWidth++;
                if (scaledHeight == 0) scaledHeight++;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
90

91
                QPixmap scaledPixmap = QPixmap::fromImage(m_pattern->pattern());
92
                preview->setPixmap(scaledPixmap.scaled(scaledWidth, scaledHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation));
Boudewijn Rempt's avatar
Boudewijn Rempt committed
93
            } else {
94
                preview->setPixmap(QPixmap::fromImage(m_pattern->pattern()));
Edward Apap's avatar
Edward Apap committed
95 96
            }
        }
97 98 99
    }
}

Boudewijn Rempt's avatar
Boudewijn Rempt committed
100 101
void KisCustomPattern::slotAddPredefined()
{
102 103 104 105 106
    if (!m_pattern)
        return;

    // Save in the directory that is likely to be: ~/.kde/share/apps/krita/patterns
    // a unique file with this pattern name
Boudewijn Rempt's avatar
Boudewijn Rempt committed
107
    QString dir = KoResourceServerProvider::instance()->patternServer()->saveLocation();
108 109
    QString extension;

Jaison Lee's avatar
Jaison Lee committed
110 111
    QString tempFileName;
    {
112
        QTemporaryFile file(dir +  QLatin1String("/krita_XXXXXX") + QLatin1String(".pat") );
Jaison Lee's avatar
Jaison Lee committed
113 114 115 116
        file.setAutoRemove(false);
        file.open();
        tempFileName = file.fileName();
    }
117

Adrian Page's avatar
Adrian Page committed
118
    // Save it to that file
Jaison Lee's avatar
Jaison Lee committed
119
    m_pattern->setFilename(tempFileName);
120 121 122

    // Add it to the pattern server, so that it automatically gets to the mediators, and
    // so to the other pattern choosers can pick it up, if they want to
123
    m_rServerAdapter->addResource(m_pattern->clone());
124 125
}

Boudewijn Rempt's avatar
Boudewijn Rempt committed
126 127
void KisCustomPattern::slotUsePattern()
{
128 129
    if (!m_pattern)
        return;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
130
    KoPattern* copy = m_pattern->clone();
131 132 133 134 135 136

    Q_CHECK_PTR(copy);

    emit(activatedResource(copy));
}

Boudewijn Rempt's avatar
Boudewijn Rempt committed
137 138
void KisCustomPattern::createPattern()
{
139
    if (!m_view) return;
140

141 142
    KisPaintDeviceSP dev;
    QString name;
143 144 145 146
    KisImageWSP image = m_view->image();
    if (!image) return;
    QRect rc = image->bounds();

147 148 149
    if (cmbSource->currentIndex() == 0) {
        dev = m_view->activeNode()->projection();
        name = m_view->activeNode()->name();
150 151
        QRect rc2 = dev->exactBounds();
        rc = rc.intersected(rc2);
152 153 154 155 156 157 158 159
    }
    else {
        image->lock();
        dev = image->projection();
        image->unlock();
        name = image->objectName();
    }
    if (!dev) return;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
160
    // warn when creating large patterns
161

162
    QSize size = rc.size();
Boudewijn Rempt's avatar
Boudewijn Rempt committed
163
    if (size.width() > 1000 || size.height() > 1000) {
164
        lblWarning->setText(i18n("The current image is too big to create a pattern. "
165
                                "The pattern will be scaled down."));
Boudewijn Rempt's avatar
Boudewijn Rempt committed
166 167
        size.scale(1000, 1000, Qt::KeepAspectRatio);
    }
168

Boudewijn Rempt's avatar
Boudewijn Rempt committed
169
    QString dir = KoResourceServerProvider::instance()->patternServer()->saveLocation();
170
    m_pattern = new KoPattern(dev->createThumbnail(size.width(), size.height(), rc, /*oversample*/ 1,
171 172
                                                    KoColorConversionTransformation::internalRenderingIntent(),
                                                    KoColorConversionTransformation::internalConversionFlags()), name, dir);
173 174 175
}