]> git.sesse.net Git - kdenlive/blob - src/clipstabilize.cpp
use doubleparamterwidget
[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 #include "doubleparameterwidget.h"
24
25 #include <KDebug>
26 #include <mlt++/Mlt.h>
27 #include "kdenlivesettings.h"
28 #include <KGlobalSettings>
29 #include <KMessageBox>
30 #include <KColorScheme>
31 #include <QtConcurrentRun>
32 #include <QTimer>
33 #include <QSlider>
34 #include <KFileDialog>
35
36
37 ClipStabilize::ClipStabilize(KUrl::List urls, const QString &params, Mlt::Filter* filter,QWidget * parent) :
38         QDialog(parent), m_urls(urls), m_duration(0),m_profile(NULL),m_consumer(NULL),m_playlist(NULL),m_filter(filter),vbox(NULL)
39 {
40     setFont(KGlobalSettings::toolBarFont());
41     setupUi(this);
42     setAttribute(Qt::WA_DeleteOnClose);
43     log_text->setHidden(true);
44     setWindowTitle(i18n("Stabilize Clip"));
45     auto_add->setText(i18np("Add clip to project", "Add clips to project", m_urls.count()));
46         m_profile = new Mlt::Profile(KdenliveSettings::current_profile().toUtf8().constData());
47         filtername=params;
48
49
50         QPalette p = palette();
51         KColorScheme scheme(p.currentColorGroup(), KColorScheme::View, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
52         QColor dark_bg = scheme.shade(KColorScheme::DarkShade);
53         QColor selected_bg = scheme.decoration(KColorScheme::FocusColor).color();
54         QColor hover_bg = scheme.decoration(KColorScheme::HoverColor).color();
55         QColor light_bg = scheme.shade(KColorScheme::LightShade);
56
57         QString stylesheet(QString("QProgressBar:horizontal {border: 1px solid %1;border-radius:0px;border-top-left-radius: 4px;border-bottom-left-radius: 4px;border-right: 0px;background:%4;padding: 0px;text-align:left center}\
58                                 QProgressBar:horizontal#dragOnly {background: %1} QProgressBar:horizontal:hover#dragOnly {background: %3} QProgressBar:horizontal:hover {border: 1px solid %3;border-right: 0px;}\
59                                 QProgressBar::chunk:horizontal {background: %1;} QProgressBar::chunk:horizontal:hover {background: %3;}\
60                                 QProgressBar:horizontal[inTimeline=\"true\"] { border: 1px solid %2;border-right: 0px;background: %4;padding: 0px;text-align:left center } QProgressBar::chunk:horizontal[inTimeline=\"true\"] {background: %2;}\
61                                 QAbstractSpinBox#dragBox {border: 1px solid %1;border-top-right-radius: 4px;border-bottom-right-radius: 4px;padding-right:0px;} QAbstractSpinBox::down-button#dragBox {width:0px;padding:0px;}\
62                                 QAbstractSpinBox::up-button#dragBox {width:0px;padding:0px;} QAbstractSpinBox[inTimeline=\"true\"]#dragBox { border: 1px solid %2;} QAbstractSpinBox:hover#dragBox {border: 1px solid %3;} ")
63                         .arg(dark_bg.name()).arg(selected_bg.name()).arg(hover_bg.name()).arg(light_bg.name()));
64         setStyleSheet(stylesheet);
65
66     if (m_urls.count() == 1) {
67         QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
68         QString newFile = fileName.append(".mlt");
69         KUrl dest(newFile);
70         source_url->setUrl(m_urls.at(0));
71         dest_url->setMode(KFile::File);
72         dest_url->setUrl(dest);
73         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
74         urls_list->setHidden(true);
75         connect(source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
76     } else {
77         label_source->setHidden(true);
78         source_url->setHidden(true);
79         label_dest->setText(i18n("Destination folder"));
80         dest_url->setMode(KFile::Directory);
81         dest_url->setUrl(KUrl(m_urls.at(0).directory()));
82         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
83         for (int i = 0; i < m_urls.count(); i++)
84             urls_list->addItem(m_urls.at(i).path());
85     }
86     if (!params.isEmpty()) {
87         label_profile->setHidden(true);
88         profile_list->setHidden(true);
89         //ffmpeg_params->setPlainText(params.simplified());
90         /*if (!description.isEmpty()) {
91             transcode_info->setText(description);
92         } else transcode_info->setHidden(true);*/
93     } 
94
95         if (filtername=="videostab"){
96                 QStringList ls;
97                 ls << "shutterangle,type,int,value,0,min,0,max,100";
98                 fillParameters(ls);
99         }else if (filtername=="videostab2"){
100                 QStringList ls;
101                 ls << "accuracy,type,int,value,4,min,1,max,10";
102                 ls << "stepsize,type,int,value,6,min,0,max,100";
103                 ls << "algo,type,int,value,1,min,0,max,1";
104                 ls << "mincontrast,type,int,value,0.3,min,0,max,1,factor,1,decimals,2";
105                 ls << "show,type,int,value,0,min,0,max,2";
106                 ls << "smoothing,type,int,value,10,min,0,max,100";
107                 ls << "maxshift,type,int,value,-1,min,-1,max,1000";
108                 ls << "maxangle,type,int,value,-1,min,-1,max,1000";
109                 ls << "crop,type,int,value,0,min,0,max,1";
110                 ls << "invert,type,int,value,0,min,0,max,1";
111                 ls << "realtive,type,int,value,1,min,0,max,1";
112                 ls << "zoom,type,int,value,0,min,-500,max,500";
113                 ls << "optzoom,type,int,value,1,min,0,max,1";
114                 ls << "sharpen,type,int,value,0.8,min,0,max,1,decimals,1";
115                 fillParameters(ls);
116
117         }
118     connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartStabilize()));
119         connect(buttonBox,SIGNAL(rejected()), this, SLOT(slotAbortStabilize()));
120
121         m_timer=new QTimer(this);
122         connect(m_timer, SIGNAL(timeout()), this, SLOT(slotShowStabilizeInfo()));
123
124         vbox=new QVBoxLayout(optionsbox);
125         QHashIterator<QString,QHash<QString,QString> > hi(m_ui_params);
126         while(hi.hasNext()){
127                 hi.next();
128                 QHash<QString,QString> val=hi.value();
129                 DoubleParameterWidget *dbl=new DoubleParameterWidget(hi.key(), val["value"].toDouble(),
130                                 val["min"].toDouble(),val["max"].toDouble(),val["value"].toDouble(),
131                                 "comment",0/*id*/,""/*suffix*/,val["decimals"]!=""?val["decimals"].toInt():0,this);
132                 dbl->setObjectName(hi.key());
133                 connect(dbl,SIGNAL(valueChanged(double)),this,SLOT(slotUpdateParams()));
134                 vbox->addWidget(dbl);
135         }
136
137     adjustSize();
138 }
139
140 ClipStabilize::~ClipStabilize()
141 {
142     /*if (m_stabilizeProcess.state() != QProcess::NotRunning) {
143         m_stabilizeProcess.close();
144     }*/
145         if (m_stabilizeRun.isRunning()){
146                 if (m_consumer){
147                         m_consumer->stop();
148                 }
149                 m_stabilizeRun.waitForFinished();
150         }
151         if (m_profile) free (m_profile);
152         if (m_consumer) free (m_consumer);
153         if (m_playlist) free (m_playlist);
154 }
155
156 void ClipStabilize::slotStartStabilize()
157 {
158     if (m_consumer && !m_consumer->is_stopped()) {
159         return;
160     }
161     m_duration = 0;
162     QStringList parameters;
163     QString destination;
164     //QString params = ffmpeg_params->toPlainText().simplified();
165     if (urls_list->count() > 0) {
166         source_url->setUrl(m_urls.takeFirst());
167         destination = dest_url->url().path(KUrl::AddTrailingSlash)+ source_url->url().fileName()+".mlt";
168         QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
169         if (matching.count() > 0) {
170             matching.at(0)->setFlags(Qt::ItemIsSelectable);
171             urls_list->setCurrentItem(matching.at(0));
172         }
173     } else {
174         destination = dest_url->url().path();
175     }
176     QString s_url = source_url->url().path();
177
178     if (QFile::exists(destination)) {
179                         if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination )) == KMessageBox::No) return;
180     }
181     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
182
183         if (m_profile){
184                 m_playlist= new Mlt::Playlist;
185                 Mlt::Filter filter(*m_profile,filtername.toUtf8().data());
186                 QHashIterator <QString,QHash<QString,QString> > it(m_ui_params);
187                 while (it.hasNext()){
188                         it.next();
189                         filter.set(
190                                         it.key().toAscii().data(),
191                                         QString::number(it.value()["value"].toDouble()).toAscii().data());
192                 }
193                 Mlt::Producer p(*m_profile,s_url.toUtf8().data());
194                 if (p.is_valid()) {
195                         m_playlist->append(p);
196                         m_playlist->attach(filter);
197                         m_consumer= new Mlt::Consumer(*m_profile,"xml",destination.toUtf8().data());
198                         m_consumer->set("all","1");
199                         m_consumer->set("real_time","-2");
200                         m_consumer->connect(*m_playlist);
201                         m_stabilizeRun = QtConcurrent::run(this, &ClipStabilize::slotRunStabilize);
202                         m_timer->start(500);
203                         button_start->setEnabled(false);
204                 }
205         }
206
207 }
208
209 void ClipStabilize::slotRunStabilize()
210 {
211         if (m_consumer)
212         {
213                 m_consumer->run();
214         }
215 }
216
217 void ClipStabilize::slotAbortStabilize()
218 {
219         if (m_consumer)
220         {
221                 m_timer->stop();
222                 m_consumer->stop();
223                 slotStabilizeFinished(false);
224         }
225 }
226
227 void ClipStabilize::slotShowStabilizeInfo()
228 {
229         if (m_playlist){
230         job_progress->setValue((int) (100.0 * m_consumer->position()/m_playlist->get_out() ));
231                 if (m_consumer->position()==m_playlist->get_out()){
232                         m_timer->stop();
233                         slotStabilizeFinished(true);
234                 }
235         }
236 }
237
238 void ClipStabilize::slotStabilizeFinished(bool success)
239 {
240     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
241     button_start->setEnabled(true);
242     m_duration = 0;
243     if (m_stabilizeRun.isRunning()){
244         if (m_consumer){
245             m_consumer->stop();
246         }
247         m_stabilizeRun.waitForFinished();
248     }
249
250     if (success) {
251         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilize finished."));
252         if (auto_add->isChecked()) {
253             KUrl url;
254             if (urls_list->count() > 0) {
255                                 url = KUrl(dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName()+".mlt");
256             } else url = dest_url->url();
257             emit addClip(url);
258         }
259         if (urls_list->count() > 0 && m_urls.count() > 0) {
260             slotStartStabilize();
261             return;
262         } else if (auto_close->isChecked()) accept();
263     } else {
264         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilizing FAILED!"));
265     }
266         if (m_playlist){
267                 free(m_playlist);
268                 m_playlist=NULL;
269         }
270         if (m_consumer){
271                 free(m_consumer);
272                 m_consumer=NULL;
273         }
274
275 }
276
277 void ClipStabilize::slotUpdateParams()
278 {
279         for (int i=0;i<vbox->count();i++){
280                 QWidget* w=vbox->itemAt(i)->widget();
281                 QString name=w->objectName();
282                 if (name !="" && m_ui_params.contains(name)){
283                         if (m_ui_params[name]["type"]=="int" || m_ui_params[name]["type"]=="double"){
284                                 DoubleParameterWidget *dbl=(DoubleParameterWidget*)w;
285                                 m_ui_params[name]["value"]=QString::number((double)(dbl->getValue()));
286                         }
287                 }
288         }
289 }
290
291 void ClipStabilize::fillParameters(QStringList lst)
292 {
293
294         m_ui_params.clear();
295         while (!lst.isEmpty()){
296                 QString vallist=lst.takeFirst();
297                 QStringList cont=vallist.split(",");
298                 QString name=cont.takeFirst();
299                 while (!cont.isEmpty()){
300                         QString valname=cont.takeFirst();
301                         QString val;
302                         if (!cont.isEmpty()){
303                                 val=cont.takeFirst();
304                         }
305                         m_ui_params[name][valname]=val;
306                 }
307         }
308
309 }
310
311
312 #include "clipstabilize.moc"
313
314