]> git.sesse.net Git - kdenlive/blob - src/clipstabilize.cpp
ugly hack to read all parameters to set.
[kdenlive] / src / clipstabilize.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *   Copyright (C) 2011 by Marco Gittler (marco@gitma.de)                  *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
19  ***************************************************************************/
20
21
22 #include "clipstabilize.h"
23
24 #include <KDebug>
25 #include <mlt++/Mlt.h>
26 #include "kdenlivesettings.h"
27 #include <KGlobalSettings>
28 #include <KMessageBox>
29 #include <QtConcurrentRun>
30 #include <QTimer>
31 #include <QSlider>
32 #include <KFileDialog>
33
34
35 ClipStabilize::ClipStabilize(KUrl::List urls, const QString &params, Mlt::Filter* filter,QWidget * parent) :
36         QDialog(parent), m_urls(urls), m_duration(0),m_profile(NULL),m_consumer(NULL),m_playlist(NULL),m_filter(filter),vbox(NULL)
37 {
38     setFont(KGlobalSettings::toolBarFont());
39     setupUi(this);
40     setAttribute(Qt::WA_DeleteOnClose);
41     log_text->setHidden(true);
42     setWindowTitle(i18n("Stabilize Clip"));
43     auto_add->setText(i18np("Add clip to project", "Add clips to project", m_urls.count()));
44         m_profile = new Mlt::Profile(KdenliveSettings::current_profile().toUtf8().constData());
45         filtername=params;
46
47     if (m_urls.count() == 1) {
48         QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
49         QString newFile = fileName.append(".mlt");
50         KUrl dest(newFile);
51         source_url->setUrl(m_urls.at(0));
52         dest_url->setMode(KFile::File);
53         dest_url->setUrl(dest);
54         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
55         urls_list->setHidden(true);
56         connect(source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
57     } else {
58         label_source->setHidden(true);
59         source_url->setHidden(true);
60         label_dest->setText(i18n("Destination folder"));
61         dest_url->setMode(KFile::Directory);
62         dest_url->setUrl(KUrl(m_urls.at(0).directory()));
63         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
64         for (int i = 0; i < m_urls.count(); i++)
65             urls_list->addItem(m_urls.at(i).path());
66     }
67     if (!params.isEmpty()) {
68         label_profile->setHidden(true);
69         profile_list->setHidden(true);
70         //ffmpeg_params->setPlainText(params.simplified());
71         /*if (!description.isEmpty()) {
72             transcode_info->setText(description);
73         } else transcode_info->setHidden(true);*/
74     } 
75
76         if (filtername=="videostab"){
77                 QStringList ls;
78                 ls << "shutterangle,type,int,value,0,min,0,max,100,factor,1";
79                 fillParamaters(ls);
80         }else if (filtername=="videostab2"){
81                 QStringList ls;
82                 ls << "accuracy,type,int,value,4,min,1,max,10,factor,1";
83                 ls << "stepsize,type,int,value,6,min,0,max,100,factor,1";
84                 ls << "algo,type,int,value,0,min,0,max,1,factor,1";
85                 ls << "mincontrast,type,int,value,30,min,0,max,100,factor,100";
86                 ls << "show,type,int,value,0,min,0,max,2,factor,1";
87                 ls << "smoothing,type,int,value,10,min,0,max,100,factor,1";
88                 ls << "maxshift,type,int,value,-1,min,-1,max,1000,factor,1";
89                 ls << "maxangle,type,int,value,-1,min,-1,max,1000,factor,1";
90                 ls << "crop,type,int,value,0,min,0,max,1,factor,1";
91                 ls << "invert,type,int,value,0,min,0,max,1,factor,1";
92                 ls << "realtive,type,int,value,1,min,0,max,1,factor,1";
93                 ls << "zoom,type,int,value,0,min,-500,max,500,factor,1";
94                 ls << "optzoom,type,int,value,1,min,0,max,1,factor,1";
95                 ls << "sharpen,type,int,value,8,min,0,max,100,factor,10";
96                 fillParamaters(ls);
97
98         }
99     connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartStabilize()));
100         connect(buttonBox,SIGNAL(rejected()), this, SLOT(slotAbortStabilize()));
101
102         m_timer=new QTimer(this);
103         connect(m_timer, SIGNAL(timeout()), this, SLOT(slotShowStabilizeInfo()));
104
105         vbox=new QVBoxLayout(optionsbox);
106         QHashIterator<QString,QHash<QString,QString> > hi(m_ui_params);
107         while(hi.hasNext()){
108                 hi.next();
109                 qDebug() << hi.key() << hi.value();
110                 QHash<QString,QString> val=hi.value();
111                 QLabel *l=new QLabel(hi.key(),this);
112                 QSlider* s=new QSlider(Qt::Horizontal,this);
113                 qDebug() << val["max"].toInt() << val["min"].toInt() << val["value"].toInt();
114                 s->setMaximum(val["max"].toInt());
115                 s->setMinimum(val["min"].toInt());
116                 s->setValue(val["value"].toInt());
117                 s->setObjectName(hi.key());
118                 connect(s,SIGNAL(sliderMoved(int)),this,SLOT(slotUpdateParams()));
119                 vbox->addWidget(l);
120                 vbox->addWidget(s);
121         }
122
123     adjustSize();
124 }
125
126 ClipStabilize::~ClipStabilize()
127 {
128     /*if (m_stabilizeProcess.state() != QProcess::NotRunning) {
129         m_stabilizeProcess.close();
130     }*/
131         if (m_profile) free (m_profile);
132         if (m_consumer) free (m_consumer);
133         if (m_playlist) free (m_playlist);
134 }
135
136 void ClipStabilize::slotStartStabilize()
137 {
138     if (m_consumer && !m_consumer->is_stopped()) {
139         return;
140     }
141     m_duration = 0;
142     QStringList parameters;
143     QString destination;
144     //QString params = ffmpeg_params->toPlainText().simplified();
145     if (urls_list->count() > 0) {
146         source_url->setUrl(m_urls.takeFirst());
147         destination = dest_url->url().path();
148         QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
149         if (matching.count() > 0) {
150             matching.at(0)->setFlags(Qt::ItemIsSelectable);
151             urls_list->setCurrentItem(matching.at(0));
152         }
153     } else {
154         destination = dest_url->url().path();
155     }
156     QString s_url = source_url->url().path();
157
158     if (QFile::exists(destination)) {
159         if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination )) == KMessageBox::No) return;
160     }
161     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
162
163         if (m_profile){
164                 qDebug() << m_profile->description();
165                 m_playlist= new Mlt::Playlist;
166                 Mlt::Filter filter(*m_profile,filtername.toUtf8().data());
167                 QHashIterator <QString,QHash<QString,QString> > it(m_ui_params);
168                 while (it.hasNext()){
169                         it.next();
170                         filter.set(it.key().toAscii().data(),it.value()["value"].toAscii().data());
171                 }
172                 Mlt::Producer p(*m_profile,s_url.toUtf8().data());
173                 m_playlist->append(p);
174                 m_playlist->attach(filter);
175                 //m_producer->set("out",20);
176                 m_consumer= new Mlt::Consumer(*m_profile,"xml",destination.toUtf8().data());
177                 m_consumer->set("all","1");
178                 m_consumer->set("real_time","-2");
179                 m_consumer->connect(*m_playlist);
180                 QtConcurrent::run(this, &ClipStabilize::slotRunStabilize);
181                 button_start->setEnabled(false);
182         }
183
184 }
185
186 void ClipStabilize::slotRunStabilize()
187 {
188         if (m_consumer)
189         {
190                 m_timer->start(500);
191                 m_consumer->run();
192         }
193 }
194
195 void ClipStabilize::slotAbortStabilize()
196 {
197         if (m_consumer)
198         {
199                 m_timer->stop();
200                 m_consumer->stop();
201                 slotStabilizeFinished(false);
202         }
203 }
204
205 void ClipStabilize::slotShowStabilizeInfo()
206 {
207         if (m_playlist){
208         job_progress->setValue((int) (100.0 * m_consumer->position()/m_playlist->get_out() ));
209                 if (m_consumer->position()==m_playlist->get_out()){
210                         m_timer->stop();
211                         slotStabilizeFinished(true);
212                 }
213         }
214 }
215
216 void ClipStabilize::slotStabilizeFinished(bool success)
217 {
218     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
219     button_start->setEnabled(true);
220     m_duration = 0;
221
222     if (success) {
223         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilize finished."));
224         if (auto_add->isChecked()) {
225             KUrl url;
226             if (urls_list->count() > 0) {
227                 url = KUrl(dest_url->url());
228             } else url = dest_url->url();
229             emit addClip(url);
230         }
231         if (urls_list->count() > 0 && m_urls.count() > 0) {
232             slotStartStabilize();
233             return;
234         } else if (auto_close->isChecked()) accept();
235     } else {
236         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilizing FAILED!"));
237     }
238
239 }
240
241 void ClipStabilize::slotUpdateParams()
242 {
243         for (int i=0;i<vbox->count();i++){
244                 QWidget* w=vbox->itemAt(i)->widget();
245                 QString name=w->objectName();
246                 if (name !="" && m_ui_params.contains(name)){
247                         if (m_ui_params[name]["type"]=="int"){
248                                 QSlider *s=(QSlider*)w;
249                                 m_ui_params[name]["value"]=QString::number((double)(s->value()/m_ui_params[name]["factor"].toInt()));
250                                 qDebug() << name << m_ui_params[name]["value"];
251                         }
252                 }
253         }
254 }
255
256 void ClipStabilize::fillParamaters(QStringList lst)
257 {
258
259         m_ui_params.clear();
260         while (!lst.isEmpty()){
261                 QString vallist=lst.takeFirst();
262                 QStringList cont=vallist.split(",");
263                 QString name=cont.takeFirst();
264                 while (!cont.isEmpty()){
265                         QString valname=cont.takeFirst();
266                         QString val;
267                         if (!cont.isEmpty()){
268                                 val=cont.takeFirst();
269                         }
270                         m_ui_params[name][valname]=val;
271                 }
272         }
273
274 }
275
276
277 #include "clipstabilize.moc"
278
279