]> git.sesse.net Git - kdenlive/blob - plugins/sampleplugin/sampleplugin.cpp
Const'ref
[kdenlive] / plugins / sampleplugin / sampleplugin.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "sampleplugin.h"
22 #include "ui_countdown_ui.h"
23
24 #include <KUrlRequester>
25 #include <KIntSpinBox>
26 #include <KDebug>
27 #include <KMessageBox>
28 #include <KApplication>
29
30 #include <QDialog>
31 #include <QDomDocument>
32 #include <QInputDialog>
33 #include <QProcess>
34
35 QStringList SamplePlugin::generators(const QStringList &producers) const
36 {
37     QStringList result;
38     if (producers.contains("pango")) result << i18n("Countdown");
39     if (producers.contains("noise")) result << i18n("Noise");
40     return result;
41 }
42
43
44 KUrl SamplePlugin::generatedClip(const QString &renderer, const QString &generator, const KUrl &projectFolder, const QStringList &/*lumaNames*/, const QStringList &/*lumaFiles*/, const double fps, const int /*width*/, const int height)
45 {
46     QString prePath;
47     if (generator == i18n("Noise")) {
48         prePath = projectFolder.path() + "/noise";
49     } else prePath = projectFolder.path() + "/counter";
50     int ct = 0;
51     QString counter = QString::number(ct).rightJustified(5, '0', false);
52     while (QFile::exists(prePath + counter + ".mlt")) {
53         ct++;
54         counter = QString::number(ct).rightJustified(5, '0', false);
55     }
56     QPointer<QDialog> d = new QDialog;
57     Ui::CountDown_UI view;
58     view.setupUi(d);
59     if (generator == i18n("Noise")) {
60         d->setWindowTitle(i18n("Create Noise Clip"));
61         view.font_label->setHidden(true);
62         view.font->setHidden(true);
63     } else {
64         d->setWindowTitle(i18n("Create Countdown Clip"));
65         view.font->setValue(height);
66     }
67
68     // Set single file mode. Default seems to be File::ExistingOnly.
69     view.path->setMode(KFile::File);
70
71     QString clipFile = prePath + counter + ".mlt";
72     view.path->setUrl(KUrl(clipFile));
73     KUrl result;
74     
75     if (d->exec() == QDialog::Accepted) {
76         QProcess generatorProcess;
77
78         // Disable VDPAU so that rendering will work even if there is a Kdenlive instance using VDPAU
79 #if QT_VERSION >= 0x040600
80         QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
81         env.insert("MLT_NO_VDPAU", "1");
82         generatorProcess.setProcessEnvironment(env);
83 #else
84         QStringList env = QProcess::systemEnvironment();
85         env << "MLT_NO_VDPAU=1";
86         generatorProcess.setEnvironment(env);
87 #endif
88         QStringList args;
89         if (generator == i18n("Noise")) {
90             args << "noise:" << "in=0" << QString("out=" + QString::number((int) fps * view.duration->value()));
91         }
92         else {
93             // Countdown producer
94             for (int i = 0; i < view.duration->value(); i++) {
95                 // Create the producers
96                 args << "pango:" << "in=0" << QString("out=" + QString::number((int) fps * view.duration->value()));
97                 args << QString("text=" + QString::number(view.duration->value() - i));
98                 args << QString("font=" + QString::number(view.font->value()) + "px");
99             }
100         }
101
102         args << "-consumer"<<QString("xml:%1").arg(view.path->url().path());
103         generatorProcess.start(renderer, args);
104         if (generatorProcess.waitForFinished()) {
105             if (generatorProcess.exitStatus() == QProcess::CrashExit) {
106                 kDebug() << "/// Generator failed: ";
107                 QString error = generatorProcess.readAllStandardError();
108                 KMessageBox::sorry(kapp->activeWindow(), i18n("Failed to generate clip:\n%1", error, i18n("Generator Failed")));
109             }
110             else {
111                 result = view.path->url();
112             }
113         } else {
114             kDebug() << "/// Generator failed: ";
115             QString error = generatorProcess.readAllStandardError();
116             KMessageBox::sorry(kapp->activeWindow(), i18n("Failed to generate clip:\n%1", error, i18n("Generator Failed")));
117         }
118     }
119     delete d;
120     return result;
121 }
122
123 Q_EXPORT_PLUGIN2(kdenlive_sampleplugin, SamplePlugin)
124
125 #include "sampleplugin.moc"