]> git.sesse.net Git - kdenlive/blob - plugins/sampleplugin/sampleplugin.cpp
2d40a04116aa4af09cfa5a7a6224c66432bdbcbb
[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
29 #include <QDialog>
30 #include <QDomDocument>
31 #include <QInputDialog>
32
33 QStringList SamplePlugin::generators(const QStringList producers) const
34 {
35     QStringList result;
36     if (producers.contains("pango")) result << i18n("Countdown");
37     if (producers.contains("noise")) result << i18n("Noise");
38     return result;
39 }
40
41
42 KUrl SamplePlugin::generatedClip(const QString &generator, const KUrl &projectFolder, const QStringList &/*lumaNames*/, const QStringList &/*lumaFiles*/, const double fps, const int /*width*/, const int height)
43 {
44     QString prePath;
45     if (generator == i18n("Noise")) {
46         prePath = projectFolder.path() + "/noise";
47     } else prePath = projectFolder.path() + "/counter";
48     int ct = 0;
49     QString counter = QString::number(ct).rightJustified(5, '0', false);
50     while (QFile::exists(prePath + counter + ".mlt")) {
51         ct++;
52         counter = QString::number(ct).rightJustified(5, '0', false);
53     }
54     QDialog d;
55     Ui::CountDown_UI view;
56     view.setupUi(&d);
57     if (generator == i18n("Noise")) {
58         d.setWindowTitle(tr("Create Noise Clip"));
59         view.font_label->setHidden(true);
60         view.font->setHidden(true);
61     } else {
62         d.setWindowTitle(tr("Create Countdown Clip"));
63         view.font->setValue(height);
64     }
65
66     // Set single file mode. Default seems to be File::ExistingOnly.
67     view.path->setMode(KFile::File);
68
69     QString clipFile = prePath + counter + ".mlt";
70     view.path->setUrl(KUrl(clipFile));
71     if (d.exec() == QDialog::Accepted) {
72         QDomDocument doc;
73         QDomElement mlt = doc.createElement("mlt");
74         QDomElement playlist = doc.createElement("playlist");
75         if (generator == i18n("Noise")) {
76             QDomElement prod = doc.createElement("producer");
77             prod.setAttribute("mlt_service", "noise");
78             prod.setAttribute("in", "0");
79             prod.setAttribute("out", QString::number((int) fps * view.duration->value()));
80             prod.setAttribute("resource", "&lt;producer&gt;");
81             playlist.appendChild(prod);
82         } else {
83             for (int i = 0; i < view.duration->value(); i++) {
84                 // Create the producers
85                 QDomElement prod = doc.createElement("producer");
86                 prod.setAttribute("mlt_service", "pango");
87                 prod.setAttribute("in", "0");
88                 prod.setAttribute("out", QString::number((int) fps));
89                 prod.setAttribute("text", QString::number(view.duration->value() - i));
90                 prod.setAttribute("resource", "&lt;producer&gt;");
91                 //FIXME: the font and pad values are approximate, the pango producer seems unable
92                 // to produce a predictable frame size.
93                 prod.setAttribute("font", QString::number(view.font->value()) + "px");
94                 //prod.setAttribute("pad", 50);
95                 playlist.appendChild(prod);
96             }
97         }
98         mlt.appendChild(playlist);
99         doc.appendChild(mlt);
100         QFile file(view.path->url().path());
101         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
102             kWarning() << "//////  ERROR writing to file: " << view.path->url().path();
103             KMessageBox::error(0, i18n("Cannot write to file %1", view.path->url().path()));
104             return KUrl();
105         }
106         QTextStream out(&file);
107         out << doc.toString();
108         if (file.error() != QFile::NoError) {
109             KMessageBox::error(0, i18n("Cannot write to file %1", view.path->url().path()));
110             file.close();
111             return KUrl();
112         }
113         file.close();
114         return view.path->url();
115     }
116     return KUrl();
117 }
118
119 Q_EXPORT_PLUGIN2(kdenlive_sampleplugin, SamplePlugin)