Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

imagesplit.cpp 8.1 KB
Newer Older
1 2 3 4
/*
 * imagesplit.cc -- Part of Krita
 *
 * Copyright (c) 2004 Boudewijn Rempt (boud@valdyas.org)
5
 * Copyright (c) 2011 Srikanth Tiyyagura <srikanth.tulasiram@gmail.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *  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.
 */

#include "imagesplit.h"

24 25
#include <QStringList>
#include <QDir>
26
#include <QStandardPaths>
27
#include <QMessageBox>
28

29
#include <klocalizedstring.h>
30
#include <kpluginfactory.h>
Boudewijn Rempt's avatar
Boudewijn Rempt committed
31

32
#include <KisImportExportManager.h>
Yue Liu's avatar
Yue Liu committed
33
#include <KoFileDialog.h>
34
#include <KisDocument.h>
35

36
#include <KisPart.h>
37
#include <kis_debug.h>
38
#include <kis_types.h>
39
#include <KisViewManager.h>
40
#include <kis_image.h>
41
#include <kis_action.h>
42 43 44
#include <kis_paint_layer.h>
#include <kis_painter.h>
#include <kis_paint_device.h>
45
#include <KisMimeDatabase.h>
46

47 48
#include "dlg_imagesplit.h"

49
K_PLUGIN_FACTORY_WITH_JSON(ImagesplitFactory, "kritaimagesplit.json", registerPlugin<Imagesplit>();)
50 51

Imagesplit::Imagesplit(QObject *parent, const QVariantList &)
52
    : KisActionPlugin(parent)
53
{
Michael Abrahams's avatar
Michael Abrahams committed
54
    KisAction *action  = createAction("imagesplit");
55
    connect(action, SIGNAL(triggered()), this, SLOT(slotImagesplit()));
56 57 58 59 60 61
}

Imagesplit::~Imagesplit()
{
}

62
bool Imagesplit::saveAsImage(const QRect &imgSize, const QString &mimeType, const QString &url)
63
{
64
    KisImageSP image = viewManager()->image();
65

66
    KisDocument *document = KisPart::instance()->createDocument();
67

68 69 70 71 72
    { // make sure dst is deleted before calling 'delete exportDocument',
      // since KisDocument checks that its image is properly deref()'d.
      KisImageSP dst = new KisImage(document->createUndoStore(), imgSize.width(), imgSize.height(), image->colorSpace(), image->objectName());
      dst->setResolution(image->xRes(), image->yRes());
      document->setCurrentImage(dst);
73

74 75 76
      KisPaintLayer* paintLayer = new KisPaintLayer(dst, dst->nextLayerName(), 255);
      KisPainter gc(paintLayer->paintDevice());
      gc.bitBlt(QPoint(0, 0), image->projection(), imgSize);
77

78 79 80
      dst->addNode(paintLayer, KisNodeSP(0));
      dst->refreshGraph();
    }
81
    document->setFileBatchMode(true);
82
    if (!document->exportDocumentSync(QUrl::fromLocalFile(url), mimeType.toLatin1())) {
83 84 85 86 87 88 89
        if (document->errorMessage().isEmpty()) {
            QMessageBox::critical(0, i18nc("@title:window", "Krita"), i18n("Could not save\n%1", document->localFilePath()));
        } else {
            QMessageBox::critical(0, i18nc("@title:window", "Krita"), i18n("Could not save %1\nReason: %2", document->localFilePath(), document->errorMessage()));
        }
        return false;
    }
90

91
    delete document;
92 93

    return true;
94 95 96 97
}

void Imagesplit::slotImagesplit()
{
98
    // Taking the title - url from caption function and removing file extension
99
    QStringList strList = ((viewManager()->document())->caption()).split('.');
100 101 102
    QString suffix = strList.at(0);

    // Getting all mime types and converting them into names which are displayed at combo box
103
    QStringList listMimeFilter = KisImportExportManager::supportedMimeTypes(KisImportExportManager::Export);
104
    QString defaultMime = QString::fromLatin1(viewManager()->document()->mimeType());
105
    if (defaultMime.isEmpty()) defaultMime = QString::fromLatin1(viewManager()->document()->nativeFormatMimeType());
106 107
    int defaultMimeIndex = 0;

108
    listMimeFilter.sort();
109
    QStringList filteredMimeTypes;
110
    QStringList listFileType;
111
    int i = 0;
112
    Q_FOREACH (const QString &mimeType, listMimeFilter) {
113 114
        listFileType.append(KisMimeDatabase::descriptionForMimeType(mimeType));
        filteredMimeTypes.append(mimeType);
115 116 117 118
        if (mimeType == defaultMime) {
            defaultMimeIndex = i;
        }
        i++;
119
    }
120

121 122 123
    listMimeFilter = filteredMimeTypes;

    Q_ASSERT(listMimeFilter.size() == listFileType.size());
124

125
    DlgImagesplit *dlgImagesplit = new DlgImagesplit(viewManager(), suffix, listFileType, defaultMimeIndex);
126 127 128
    dlgImagesplit->setObjectName("Imagesplit");
    Q_CHECK_PTR(dlgImagesplit);

129
    KisImageWSP image = viewManager()->image();
130 131 132 133 134 135

    if (dlgImagesplit->exec() == QDialog::Accepted) {

        int numHorizontalLines = dlgImagesplit->horizontalLines();
        int numVerticalLines = dlgImagesplit->verticalLines();

Boudewijn Rempt's avatar
Boudewijn Rempt committed
136 137
        int img_width = image->width() / (numVerticalLines + 1);
        int img_height = image->height() / (numHorizontalLines + 1);
138 139


140
        bool stop = false;
141 142 143 144 145 146

        QString mimeType;
        QString filepath;
        QString homepath;
        QString suffix;

147
        if (dlgImagesplit->autoSave()) {
148
            KoFileDialog dialog(viewManager()->mainWindow(), KoFileDialog::OpenDirectory, "OpenDocument");
149
            dialog.setCaption(i18n("Save Image on Split"));
150
            dialog.setDefaultDir(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
151
            QStringList mimeFilter = viewManager()->document()->importExportManager()->supportedMimeTypes(KisImportExportManager::Export);
152
            QString defaultMime = QString::fromLatin1(viewManager()->document()->mimeType());
153 154
            dialog.setMimeTypeFilters(mimeFilter, defaultMime);

155 156 157 158
            QUrl directory = QUrl::fromUserInput(dialog.filename());

            if (directory.isEmpty())
                return;
159 160 161 162 163 164 165 166 167

            mimeType = listMimeFilter.at(dlgImagesplit->cmbIndex);
            homepath = directory.toLocalFile();
            suffix = KisMimeDatabase::suffixesForMimeType(mimeType).first();
            if (suffix.startsWith("*.")) {
                suffix = suffix.remove(0, 1);
            }
            if (!suffix.startsWith(".")) {
                suffix = suffix.prepend('.');
168
            }
169 170 171 172 173 174 175 176
	    }

        int outerLoop;
        int innerLoop;

        if (dlgImagesplit->sortHorizontal()) {
            outerLoop = numHorizontalLines + 1;
            innerLoop = numVerticalLines + 1;
177 178
        }
        else {
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
            outerLoop = numVerticalLines + 1;
            innerLoop = numHorizontalLines + 1;
        }


        for (int i = 0, k = 1; i < outerLoop; i++) {
            for (int j = 0; j < innerLoop; j++, k++) {
                int row;
                int column;
                if (dlgImagesplit->sortHorizontal()) {
                    row = i;
                    column = j;
                }
                else {
                    row = j;
                    column = i;
                }
196

197 198 199 200 201 202
                if (dlgImagesplit->autoSave()) {
                    QString fileName = dlgImagesplit->suffix() + '_' + QString::number(k) + suffix;
                    filepath = homepath  + '/' + fileName;
		            mimeType = listMimeFilter.at(dlgImagesplit->cmbIndex);
	            }
                else {
203
                    KoFileDialog dialog(viewManager()->mainWindow(), KoFileDialog::SaveFile, "OpenDocument");
204
                    dialog.setCaption(i18n("Save Image on Split"));
205
                    dialog.setDefaultDir(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
206
                    dialog.setMimeTypeFilters(listMimeFilter, defaultMime);
207

Boudewijn Rempt's avatar
Boudewijn Rempt committed
208
                    QUrl url = QUrl::fromUserInput(dialog.filename());
209
                    filepath = url.toLocalFile();
210

211
                    mimeType = KisMimeDatabase::mimeTypeForFile(url.toLocalFile(), false);
212 213 214

                    if (url.isEmpty())
                        return;
215

216
                }
217 218
                if (!saveAsImage(QRect((column * img_width), (row * img_height), img_width, img_height), mimeType, filepath)) {
                    stop = true;
219
                    break;
220 221
                }
            }
222 223 224
            if (stop) {
                break;
            }
225 226 227 228 229 230
        }
    }
    delete dlgImagesplit;
}

#include "imagesplit.moc"