DlgAnimationRenderer.cpp 9.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  Copyright (c) 2016 Boudewijn Rempt <boud@valdyas.org>
 *
 *  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 "DlgAnimationRenderer.h"

21
#include <QStandardPaths>
22 23 24 25 26 27 28 29 30 31 32
#include <QPluginLoader>
#include <QJsonObject>

#include <klocalizedstring.h>
#include <kpluginfactory.h>

#include <kis_properties_configuration.h>
#include <kis_debug.h>
#include <KisMimeDatabase.h>
#include <KoJsonTrader.h>
#include <KisImportExportFilter.h>
33 34 35
#include <kis_image.h>
#include <kis_image_animation_interface.h>
#include <kis_time_range.h>
36
#include <KisImportExportManager.h>
37 38 39
#include <kis_config_widget.h>
#include <KisDocument.h>
#include <QHBoxLayout>
40
#include <KisImportExportFilter.h>
41

42
DlgAnimationRenderer::DlgAnimationRenderer(KisImageWSP image, QWidget *parent)
43
    : KoDialog(parent)
44
    , m_image(image)
45 46 47 48 49 50 51
{
    setCaption(i18n("Render Animation"));
    setButtons(Ok | Cancel);
    setDefaultButton(Ok);

    m_page = new WdgAnimaterionRenderer(this);
    m_page->layout()->setMargin(0);
52
    m_page->dirRequester->setMode(KoFileDialog::OpenDirectory);
53
    m_page->dirRequester->setFileName(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
54 55 56 57 58 59 60 61 62

    m_page->intStart->setMinimum(image->animationInterface()->fullClipRange().start());
    m_page->intStart->setMaximum(image->animationInterface()->fullClipRange().end());
    m_page->intStart->setValue(image->animationInterface()->playbackRange().start());

    m_page->intEnd->setMinimum(image->animationInterface()->fullClipRange().start());
    m_page->intEnd->setMaximum(image->animationInterface()->fullClipRange().end());
    m_page->intEnd->setValue(image->animationInterface()->playbackRange().end());

63
    m_sequenceConfigLayout = new QHBoxLayout(m_page->grpExportOptions);
64
    m_encoderConfigLayout = new QHBoxLayout(m_page->grpRenderOptions);
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79
    QStringList mimes = KisImportExportManager::mimeFilter(KisImportExportManager::Export);
    mimes.sort();
    Q_FOREACH(const QString &mime, mimes) {
        QString description = KisMimeDatabase::descriptionForMimeType(mime);
        if (description.isEmpty()) {
            description = mime;
        }
        m_page->cmbMimetype->addItem(description, mime);
        if (mime == "image/png") {
            m_page->cmbMimetype->setCurrentIndex(m_page->cmbMimetype->count() - 1);
        }

    }

80 81 82 83 84 85 86
    setMainWidget(m_page);
    resize(m_page->sizeHint());

    KoJsonTrader trader;
    QList<QPluginLoader *>list = trader.query("Krita/AnimationExporter", "");
    Q_FOREACH(QPluginLoader *loader, list) {
        QJsonObject json = loader->metaData().value("MetaData").toObject();
87
        Q_FOREACH(const QString &mime, json.value("X-KDE-Export").toString().split(",")) {
88 89 90 91 92 93 94

            KLibFactory *factory = qobject_cast<KLibFactory *>(loader->instance());
            if (!factory) {
                warnUI << loader->errorString();
                continue;
            }

95
            QObject* obj = factory->create<KisImportExportFilter>(0);
96 97 98 99 100
            if (!obj || !obj->inherits("KisImportExportFilter")) {
                delete obj;
                continue;
            }

101
            QSharedPointer<KisImportExportFilter>filter(static_cast<KisImportExportFilter*>(obj));
102 103 104 105 106
            if (!filter) {
                delete obj;
                continue;
            }

Boudewijn Rempt's avatar
Boudewijn Rempt committed
107
            m_renderFilters.append(filter);
108 109 110 111 112
            QString description = KisMimeDatabase::descriptionForMimeType(mime);
            if (description.isEmpty()) {
                description = mime;
            }
            m_page->cmbRenderType->addItem(description, mime);
113 114
        }
    }
115
    qDeleteAll(list);
116
    connect(m_page->cmbRenderType, SIGNAL(activated(int)), this, SLOT(selectRenderType(int)));
117
    selectRenderType(m_page->cmbRenderType->currentIndex());
118
    connect(m_page->grpRender, SIGNAL(toggled(bool)), this, SLOT(toggleSequenceType(bool)));
119 120
    connect(m_page->cmbMimetype, SIGNAL(activated(int)), this, SLOT(sequenceMimeTypeSelected(int)));
    sequenceMimeTypeSelected(m_page->cmbMimetype->currentIndex());
121 122
}

123
DlgAnimationRenderer::~DlgAnimationRenderer()
124
{
125 126 127 128 129
    m_encoderConfigWidget->setParent(0);
    m_encoderConfigWidget->deleteLater();
    m_frameExportConfigWidget->setParent(0);
    m_frameExportConfigWidget->deleteLater();

130
    delete m_page;
131

132 133
}

134
KisPropertiesConfigurationSP DlgAnimationRenderer::getSequenceConfiguration() const
135 136 137 138 139 140 141
{
    KisPropertiesConfigurationSP cfg = new KisPropertiesConfiguration();
    cfg->setProperty("basename", m_page->txtBasename->text());
    cfg->setProperty("directory", m_page->dirRequester->fileName());
    cfg->setProperty("first_frame", m_page->intStart->value());
    cfg->setProperty("last_frame", m_page->intEnd->value());
    cfg->setProperty("sequence_start", m_page->sequenceStart->value());
142
    cfg->setProperty("mimetype", m_page->cmbMimetype->currentData().toString());
143 144 145
    return cfg;
}

146
void DlgAnimationRenderer::setSequenceConfiguration(KisPropertiesConfigurationSP cfg)
147 148 149 150 151 152
{
    m_page->txtBasename->setText(cfg->getString("basename", "frame"));
    m_page->dirRequester->setFileName(cfg->getString("directory", QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)));
    m_page->intStart->setValue(cfg->getInt("first_frame", m_image->animationInterface()->playbackRange().start()));
    m_page->intEnd->setValue(cfg->getInt("last_frame", m_image->animationInterface()->playbackRange().end()));
    m_page->sequenceStart->setValue(cfg->getInt("sequence_start", m_image->animationInterface()->playbackRange().start()));
153 154 155 156
    QString mimetype = cfg->getString("mimetype");
    for (int i = 0; i < m_page->cmbMimetype->count(); ++i) {
        if (m_page->cmbMimetype->itemData(i).toString() == mimetype) {
            m_page->cmbMimetype->setCurrentIndex(i);
157
            sequenceMimeTypeSelected(i);
158 159 160
            break;
        }
    }
161 162
}

163
KisPropertiesConfigurationSP DlgAnimationRenderer::getFrameExportConfiguration() const
164
{
165 166
    if (m_frameExportConfigWidget) {
        return m_frameExportConfigWidget->configuration();
167 168 169 170
    }
    return 0;
}

171
bool DlgAnimationRenderer::renderToVideo() const
172 173 174 175
{
    return m_page->grpRender->isChecked();
}

176
KisPropertiesConfigurationSP DlgAnimationRenderer::getVideoConfiguration() const
177
{
178
    if (!m_page->grpRender->isChecked()) {
179 180 181 182 183 184 185 186
        return 0;
    }
    KisPropertiesConfigurationSP cfg = new KisPropertiesConfiguration();
    cfg->setProperty("filename", m_page->videoFilename->fileName());
    cfg->setProperty("delete_sequence", m_page->chkDeleteSequence->isChecked());
    return cfg;
}

187
void DlgAnimationRenderer::setVideoConfiguration(KisPropertiesConfigurationSP cfg)
188 189 190 191
{

}

192
KisPropertiesConfigurationSP DlgAnimationRenderer::getEncoderConfiguration() const
193
{
194
    if (!m_page->grpRender->isChecked()) {
195 196 197 198 199 200
        return 0;
    }
    KisPropertiesConfigurationSP cfg = new KisPropertiesConfiguration();
    return cfg;
}

201
void DlgAnimationRenderer::setEncoderConfiguration(KisPropertiesConfigurationSP cfg)
202 203 204 205
{

}

206
void DlgAnimationRenderer::selectRenderType(int index)
207
{
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    if (m_encoderConfigWidget) {
        m_encoderConfigLayout->removeWidget(m_encoderConfigWidget);
        m_encoderConfigWidget->hide();
        m_encoderConfigWidget->setParent(0);
        m_encoderConfigWidget->deleteLater();
        m_encoderConfigWidget = 0;
    }

    if (index >= m_renderFilters.size()) return;

    QSharedPointer<KisImportExportFilter> filter = m_renderFilters[index];
    QString mimetype = m_page->cmbMimetype->itemData(index).toString();

    qDebug() << ">>>>>>>>>" << mimetype;

    if (filter) {
        m_encoderConfigWidget = filter->createConfigurationWidget(m_page->grpExportOptions, KisDocument::nativeFormatMimeType(), mimetype.toLatin1());
        if (m_encoderConfigWidget) {
            m_encoderConfigLayout->addWidget(m_encoderConfigWidget);
            m_encoderConfigWidget->setConfiguration(filter->lastSavedConfiguration());
            m_encoderConfigWidget->show();
            resize(sizeHint());
        }
        else {
            m_encoderConfigWidget = 0;
        }
    }
235
}
236

237
void DlgAnimationRenderer::toggleSequenceType(bool toggle)
238 239 240 241 242 243 244 245 246
{
    m_page->cmbMimetype->setEnabled(!toggle);
    for (int i = 0; i < m_page->cmbMimetype->count(); ++i) {
        if (m_page->cmbMimetype->itemData(i).toString() == "image/png") {
            m_page->cmbMimetype->setCurrentIndex(i);
            break;
        }
    }
}
247

248
void DlgAnimationRenderer::sequenceMimeTypeSelected(int index)
249
{
250 251 252 253 254 255
    if (m_frameExportConfigWidget) {
        m_sequenceConfigLayout->removeWidget(m_frameExportConfigWidget);
        m_frameExportConfigWidget->hide();
        m_frameExportConfigWidget->setParent(0);
        m_frameExportConfigWidget->deleteLater();
        m_frameExportConfigWidget = 0;
256 257
    }
    QString mimetype = m_page->cmbMimetype->itemData(index).toString();
258 259
    KisImportExportFilter *filter = KisImportExportManager::filterForMimeType(mimetype, KisImportExportManager::Export);
    if (filter) {
260 261 262 263 264
        m_frameExportConfigWidget = filter->createConfigurationWidget(m_page->grpExportOptions, KisDocument::nativeFormatMimeType(), mimetype.toLatin1());
        if (m_frameExportConfigWidget) {
            m_sequenceConfigLayout->addWidget(m_frameExportConfigWidget);
            m_frameExportConfigWidget->setConfiguration(filter->lastSavedConfiguration());
            m_frameExportConfigWidget->show();
265
            resize(sizeHint());
266
        }
267
        else {
268
            m_frameExportConfigWidget = 0;
269
        }
270
        delete filter;
271 272
    }
}
273