1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
3 * Copyright (C) 2011 by Marco Gittler (marco@gitma.de) *
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. *
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. *
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 ***************************************************************************/
22 #include "clipstabilize.h"
23 #include "doubleparameterwidget.h"
26 #include <mlt++/Mlt.h>
27 #include "kdenlivesettings.h"
28 #include <KGlobalSettings>
29 #include <KMessageBox>
30 #include <KColorScheme>
31 #include <QtConcurrentRun>
34 #include <KFileDialog>
36 ClipStabilize::ClipStabilize(KUrl::List urls, const QString ¶ms, Mlt::Filter* filter,QWidget * parent) :
37 QDialog(parent), m_urls(urls), m_duration(0),m_profile(NULL),m_consumer(NULL),m_playlist(NULL),m_filter(filter),vbox(NULL)
39 setFont(KGlobalSettings::toolBarFont());
41 setAttribute(Qt::WA_DeleteOnClose);
42 log_text->setHidden(true);
43 setWindowTitle(i18n("Stabilize Clip"));
44 auto_add->setText(i18np("Add clip to project", "Add clips to project", m_urls.count()));
45 m_profile = new Mlt::Profile(KdenliveSettings::current_profile().toUtf8().constData());
49 QPalette p = palette();
50 KColorScheme scheme(p.currentColorGroup(), KColorScheme::View, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
51 QColor dark_bg = scheme.shade(KColorScheme::DarkShade);
52 QColor selected_bg = scheme.decoration(KColorScheme::FocusColor).color();
53 QColor hover_bg = scheme.decoration(KColorScheme::HoverColor).color();
54 QColor light_bg = scheme.shade(KColorScheme::LightShade);
56 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}\
57 QProgressBar:horizontal#dragOnly {background: %1} QProgressBar:horizontal:hover#dragOnly {background: %3} QProgressBar:horizontal:hover {border: 1px solid %3;border-right: 0px;}\
58 QProgressBar::chunk:horizontal {background: %1;} QProgressBar::chunk:horizontal:hover {background: %3;}\
59 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;}\
60 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;}\
61 QAbstractSpinBox::up-button#dragBox {width:0px;padding:0px;} QAbstractSpinBox[inTimeline=\"true\"]#dragBox { border: 1px solid %2;} QAbstractSpinBox:hover#dragBox {border: 1px solid %3;} ")
62 .arg(dark_bg.name()).arg(selected_bg.name()).arg(hover_bg.name()).arg(light_bg.name()));
63 setStyleSheet(stylesheet);
65 if (m_urls.count() == 1) {
66 QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
67 QString newFile = fileName.append(".mlt");
69 source_url->setUrl(m_urls.at(0));
70 dest_url->setMode(KFile::File);
71 dest_url->setUrl(dest);
72 dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
73 urls_list->setHidden(true);
74 connect(source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
76 label_source->setHidden(true);
77 source_url->setHidden(true);
78 label_dest->setText(i18n("Destination folder"));
79 dest_url->setMode(KFile::Directory);
80 dest_url->setUrl(KUrl(m_urls.at(0).directory()));
81 dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
82 for (int i = 0; i < m_urls.count(); i++)
83 urls_list->addItem(m_urls.at(i).path());
85 if (!params.isEmpty()) {
86 label_profile->setHidden(true);
87 profile_list->setHidden(true);
88 //ffmpeg_params->setPlainText(params.simplified());
89 /*if (!description.isEmpty()) {
90 transcode_info->setText(description);
91 } else transcode_info->setHidden(true);*/
94 if (filtername=="videostab"){
96 ls << "shutterangle,type,int,value,0,min,0,max,100,tooltip,Angle that Images could be maximum rotated";
98 }else if (filtername=="videostab2"){
100 ls << "accuracy,type,int,value,4,min,1,max,10,tooltip,Accuracy of Shakiness detection";
101 ls << "shakiness,type,int,value,4,min,1,max,10,tooltip,How shaky is the Video";
102 ls << "stepsize,type,int,value,6,min,0,max,100,tooltip,Stepsize of Detection process minimum around";
103 ls << "algo,type,bool,value,1,min,0,max,1,tooltip,0 = Bruteforce 1 = small measurement fields";
104 ls << "mincontrast,type,double,value,0.3,min,0,max,1,factor,1,decimals,2,tooltip,Below this Contrast Field is discarded";
105 ls << "show,type,int,value,0,min,0,max,2,tooltip,0 = draw nothing. 1 or 2 show fields and transforms";
106 ls << "smoothing,type,int,value,10,min,0,max,100,tooltip,number of frames for lowpass filtering";
107 ls << "maxshift,type,int,value,-1,min,-1,max,1000,tooltip,max number of pixels to shift";
108 ls << "maxangle,type,int,value,-1,min,-1,max,1000,tooltip,max anglen to rotate (in rad)";
109 ls << "crop,type,bool,value,0,min,0,max,1,tooltip,0 = keep border 1 = black background";
110 ls << "invert,type,bool,value,0,min,0,max,1,tooltip,invert transform";
111 ls << "realtive,type,bool,value,1,min,0,max,1,tooltip,0 = absolute transform 1= relative";
112 ls << "zoom,type,int,value,0,min,-500,max,500,tooltip,additional zoom during transform";
113 ls << "optzoom,type,bool,value,1,min,0,max,1,tooltip,use optimal zoom (calulated from transforms)";
114 ls << "sharpen,type,double,value,0.8,min,0,max,1,decimals,1,tooltip,sharpen transformed image";
118 connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartStabilize()));
119 connect(buttonBox,SIGNAL(rejected()), this, SLOT(slotAbortStabilize()));
121 m_timer=new QTimer(this);
122 connect(m_timer, SIGNAL(timeout()), this, SLOT(slotShowStabilizeInfo()));
124 vbox=new QVBoxLayout(optionsbox);
125 QHashIterator<QString,QHash<QString,QString> > hi(m_ui_params);
128 QHash<QString,QString> val=hi.value();
129 if (val["type"]=="int" || val["type"]=="double"){
130 DoubleParameterWidget *dbl=new DoubleParameterWidget(hi.key(), val["value"].toDouble(),
131 val["min"].toDouble(),val["max"].toDouble(),val["value"].toDouble(),
132 "",0/*id*/,""/*suffix*/,val["decimals"]!=""?val["decimals"].toInt():0,this);
133 dbl->setObjectName(hi.key());
134 dbl->setToolTip(val["tooltip"]);
135 connect(dbl,SIGNAL(valueChanged(double)),this,SLOT(slotUpdateParams()));
136 vbox->addWidget(dbl);
137 }else if (val["type"]=="bool"){
138 QCheckBox *ch=new QCheckBox(hi.key(),this);
139 ch->setCheckState(val["value"] == "0" ? Qt::Unchecked : Qt::Checked);
140 ch->setObjectName(hi.key());
141 connect(ch, SIGNAL(stateChanged(int)) , this,SLOT(slotUpdateParams()));
142 ch->setToolTip(val["tooltip"]);
150 ClipStabilize::~ClipStabilize()
152 /*if (m_stabilizeProcess.state() != QProcess::NotRunning) {
153 m_stabilizeProcess.close();
155 if (m_stabilizeRun.isRunning()){
159 m_stabilizeRun.waitForFinished();
161 if (m_profile) free (m_profile);
162 if (m_consumer) free (m_consumer);
163 if (m_playlist) free (m_playlist);
166 void ClipStabilize::slotStartStabilize()
168 if (m_consumer && !m_consumer->is_stopped()) {
172 QStringList parameters;
174 //QString params = ffmpeg_params->toPlainText().simplified();
175 if (urls_list->count() > 0) {
176 source_url->setUrl(m_urls.takeFirst());
177 destination = dest_url->url().path(KUrl::AddTrailingSlash)+ source_url->url().fileName()+".mlt";
178 QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
179 if (matching.count() > 0) {
180 matching.at(0)->setFlags(Qt::ItemIsSelectable);
181 urls_list->setCurrentItem(matching.at(0));
184 destination = dest_url->url().path();
186 QString s_url = source_url->url().path();
188 if (QFile::exists(destination)) {
189 if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination )) == KMessageBox::No) return;
191 buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
194 m_playlist= new Mlt::Playlist;
195 Mlt::Filter filter(*m_profile,filtername.toUtf8().data());
196 QHashIterator <QString,QHash<QString,QString> > it(m_ui_params);
197 while (it.hasNext()){
200 it.key().toAscii().data(),
201 QString::number(it.value()["value"].toDouble()).toAscii().data());
203 Mlt::Producer p(*m_profile,s_url.toUtf8().data());
205 m_playlist->append(p);
206 m_playlist->attach(filter);
207 m_consumer= new Mlt::Consumer(*m_profile,"xml",destination.toUtf8().data());
208 m_consumer->set("all","1");
209 m_consumer->set("real_time","-2");
210 m_consumer->connect(*m_playlist);
211 m_stabilizeRun = QtConcurrent::run(this, &ClipStabilize::slotRunStabilize);
213 button_start->setEnabled(false);
219 void ClipStabilize::slotRunStabilize()
227 void ClipStabilize::slotAbortStabilize()
233 slotStabilizeFinished(false);
237 void ClipStabilize::slotShowStabilizeInfo()
240 job_progress->setValue((int) (100.0 * m_consumer->position()/m_playlist->get_out() ));
241 if (m_consumer->position()==m_playlist->get_out()){
243 slotStabilizeFinished(true);
248 void ClipStabilize::slotStabilizeFinished(bool success)
250 buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
251 button_start->setEnabled(true);
253 if (m_stabilizeRun.isRunning()){
257 m_stabilizeRun.waitForFinished();
261 log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilize finished."));
262 if (auto_add->isChecked()) {
264 if (urls_list->count() > 0) {
265 url = KUrl(dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName()+".mlt");
266 } else url = dest_url->url();
269 if (urls_list->count() > 0 && m_urls.count() > 0) {
270 slotStartStabilize();
272 } else if (auto_close->isChecked()) accept();
274 log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilizing FAILED!"));
287 void ClipStabilize::slotUpdateParams()
289 for (int i=0;i<vbox->count();i++){
290 QWidget* w=vbox->itemAt(i)->widget();
291 QString name=w->objectName();
292 if (name !="" && m_ui_params.contains(name)){
293 if (m_ui_params[name]["type"]=="int" || m_ui_params[name]["type"]=="double"){
294 DoubleParameterWidget *dbl=(DoubleParameterWidget*)w;
295 m_ui_params[name]["value"]=QString::number((double)(dbl->getValue()));
296 }else if (m_ui_params[name]["type"]=="bool"){
297 QCheckBox *ch=(QCheckBox*)w;
298 m_ui_params[name]["value"]= ch->checkState() == Qt::Checked ? "1" : "0" ;
304 void ClipStabilize::fillParameters(QStringList lst)
308 while (!lst.isEmpty()){
309 QString vallist=lst.takeFirst();
310 QStringList cont=vallist.split(",");
311 QString name=cont.takeFirst();
312 while (!cont.isEmpty()){
313 QString valname=cont.takeFirst();
315 if (!cont.isEmpty()){
316 val=cont.takeFirst();
318 m_ui_params[name][valname]=val;
325 #include "clipstabilize.moc"