]> git.sesse.net Git - kdenlive/blob - plugins/sampleplugin/sampleplugin.cpp
clean up reindentation
[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->setPath(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             playlist.appendChild(prod);
81         } else {
82             for (int i = 0; i < view.duration->value(); i++) {
83                 // Create the producers
84                 QDomElement prod = doc.createElement("producer");
85                 prod.setAttribute("mlt_service", "pango");
86                 prod.setAttribute("in", "0");
87                 prod.setAttribute("out", QString::number((int) fps));
88                 prod.setAttribute("text", QString::number(view.duration->value() - i));
89                 //FIXME: the font and pad values are approximate, the pango producer seems unable
90                 // to produce a predictable frame size.
91                 prod.setAttribute("font", QString::number(view.font->value()) + "px");
92                 //prod.setAttribute("pad", 50);
93                 playlist.appendChild(prod);
94             }
95         }
96         mlt.appendChild(playlist);
97         doc.appendChild(mlt);
98         QFile file(view.path->url().path());
99         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
100             kWarning() << "//////  ERROR writing to file: " << view.path->url().path();
101             KMessageBox::error(0, i18n("Cannot write to file %1", view.path->url().path()));
102             return KUrl();
103         }
104         QTextStream out(&file);
105         out << doc.toString();
106         if (file.error() != QFile::NoError) {
107             KMessageBox::error(0, i18n("Cannot write to file %1", view.path->url().path()));
108             file.close();
109             return KUrl();
110         }
111         file.close();
112         return view.path->url();
113     }
114     return KUrl();
115 }
116
117 Q_EXPORT_PLUGIN2(kdenlive_sampleplugin, SamplePlugin)