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(const QString &dest, int count, const QString &filterName,QWidget * parent) :
38 m_filtername(filterName),
42 setFont(KGlobalSettings::toolBarFont());
44 setWindowTitle(i18n("Stabilize Clip"));
45 auto_add->setText(i18np("Add clip to project", "Add clips to project", count));
46 auto_add->setChecked(KdenliveSettings::add_new_clip());
48 QPalette p = palette();
49 KColorScheme scheme(p.currentColorGroup(), KColorScheme::View, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
50 QColor dark_bg = scheme.shade(KColorScheme::DarkShade);
51 QColor selected_bg = scheme.decoration(KColorScheme::FocusColor).color();
52 QColor hover_bg = scheme.decoration(KColorScheme::HoverColor).color();
53 QColor light_bg = scheme.shade(KColorScheme::LightShade);
55 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}\
56 QProgressBar:horizontal#dragOnly {background: %1} QProgressBar:horizontal:hover#dragOnly {background: %3} QProgressBar:horizontal:hover {border: 1px solid %3;border-right: 0px;}\
57 QProgressBar::chunk:horizontal {background: %1;} QProgressBar::chunk:horizontal:hover {background: %3;}\
58 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;}\
59 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;}\
60 QAbstractSpinBox::up-button#dragBox {width:0px;padding:0px;} QAbstractSpinBox[inTimeline=\"true\"]#dragBox { border: 1px solid %2;} QAbstractSpinBox:hover#dragBox {border: 1px solid %3;} ")
61 .arg(dark_bg.name()).arg(selected_bg.name()).arg(hover_bg.name()).arg(light_bg.name()));
62 setStyleSheet(stylesheet);
65 QString newFile = dest;
66 newFile.append(".mlt");
68 dest_url->setMode(KFile::File);
69 dest_url->setUrl(KUrl(newFile));
70 dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
72 label_dest->setText(i18n("Destination folder"));
73 dest_url->setMode(KFile::Directory);
74 dest_url->setUrl(KUrl(dest));
75 dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
78 if (m_filtername=="videostab"){
80 ls << "shutterangle,type,int,value,0,min,0,max,100,tooltip,Angle that Images could be maximum rotated";
82 }else if (m_filtername=="videostab2"){
84 ls << "accuracy,type,int,value,4,min,1,max,10,tooltip,Accuracy of Shakiness detection";
85 ls << "shakiness,type,int,value,4,min,1,max,10,tooltip,How shaky is the Video";
86 ls << "stepsize,type,int,value,6,min,0,max,100,tooltip,Stepsize of Detection process minimum around";
87 ls << "algo,type,bool,value,1,min,0,max,1,tooltip,0 = Bruteforce 1 = small measurement fields";
88 ls << "mincontrast,type,double,value,0.3,min,0,max,1,factor,1,decimals,2,tooltip,Below this Contrast Field is discarded";
89 ls << "show,type,int,value,0,min,0,max,2,tooltip,0 = draw nothing. 1 or 2 show fields and transforms";
90 ls << "smoothing,type,int,value,10,min,0,max,100,tooltip,number of frames for lowpass filtering";
91 ls << "maxshift,type,int,value,-1,min,-1,max,1000,tooltip,max number of pixels to shift";
92 ls << "maxangle,type,int,value,-1,min,-1,max,1000,tooltip,max anglen to rotate (in rad)";
93 ls << "crop,type,bool,value,0,min,0,max,1,tooltip,0 = keep border 1 = black background";
94 ls << "invert,type,bool,value,0,min,0,max,1,tooltip,invert transform";
95 ls << "realtive,type,bool,value,1,min,0,max,1,tooltip,0 = absolute transform 1= relative";
96 ls << "zoom,type,int,value,0,min,-500,max,500,tooltip,additional zoom during transform";
97 ls << "optzoom,type,bool,value,1,min,0,max,1,tooltip,use optimal zoom (calulated from transforms)";
98 ls << "sharpen,type,double,value,0.8,min,0,max,1,decimals,1,tooltip,sharpen transformed image";
103 //connect(buttonBox,SIGNAL(rejected()), this, SLOT(slotAbortStabilize()));
105 vbox=new QVBoxLayout(optionsbox);
106 QHashIterator<QString,QHash<QString,QString> > hi(m_ui_params);
109 QHash<QString,QString> val=hi.value();
110 if (val["type"]=="int" || val["type"]=="double"){
111 DoubleParameterWidget *dbl=new DoubleParameterWidget(hi.key(), val["value"].toDouble(),
112 val["min"].toDouble(),val["max"].toDouble(),val["value"].toDouble(),
113 "",0/*id*/,""/*suffix*/,val["decimals"]!=""?val["decimals"].toInt():0,this);
114 dbl->setObjectName(hi.key());
115 dbl->setToolTip(val["tooltip"]);
116 connect(dbl,SIGNAL(valueChanged(double)),this,SLOT(slotUpdateParams()));
117 vbox->addWidget(dbl);
118 }else if (val["type"]=="bool"){
119 QCheckBox *ch=new QCheckBox(hi.key(),this);
120 ch->setCheckState(val["value"] == "0" ? Qt::Unchecked : Qt::Checked);
121 ch->setObjectName(hi.key());
122 connect(ch, SIGNAL(stateChanged(int)) , this,SLOT(slotUpdateParams()));
123 ch->setToolTip(val["tooltip"]);
131 ClipStabilize::~ClipStabilize()
133 /*if (m_stabilizeProcess.state() != QProcess::NotRunning) {
134 m_stabilizeProcess.close();
136 KdenliveSettings::setAdd_new_clip(auto_add->isChecked());
139 QStringList ClipStabilize::params()
141 //we must return a stringlist with:
142 // producerparams << filtername << filterparams << consumer << consumerparams
147 params << m_filtername;
148 QStringList filterparamsList;
149 QHashIterator <QString,QHash<QString,QString> > it(m_ui_params);
150 while (it.hasNext()){
152 filterparamsList << it.key() + '=' + it.value().value("value");
154 params << filterparamsList.join(" ");
159 QString title = i18n("Stabilised");
160 params << QString("all=1 title=\"%1\"").arg(title);
164 QString ClipStabilize::destination() const
167 return dest_url->url().path();
169 return dest_url->url().path(KUrl::AddTrailingSlash);
172 QString ClipStabilize::desc() const
174 return i18n("Stabilize clip");
177 void ClipStabilize::slotStartStabilize()
180 if (m_consumer && !m_consumer->is_stopped()) {
184 QStringList parameters;
186 //QString params = ffmpeg_params->toPlainText().simplified();
187 if (urls_list->count() > 0) {
188 source_url->setUrl(m_urls.takeFirst());
189 destination = dest_url->url().path(KUrl::AddTrailingSlash)+ source_url->url().fileName()+".mlt";
190 QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
191 if (matching.count() > 0) {
192 matching.at(0)->setFlags(Qt::ItemIsSelectable);
193 urls_list->setCurrentItem(matching.at(0));
196 destination = dest_url->url().path();
198 QString s_url = source_url->url().path();
200 if (QFile::exists(destination)) {
201 if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination )) == KMessageBox::No) return;
203 buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
206 m_playlist= new Mlt::Playlist;
207 Mlt::Filter filter(*m_profile,filtername.toUtf8().data());
208 QHashIterator <QString,QHash<QString,QString> > it(m_ui_params);
209 while (it.hasNext()){
212 it.key().toAscii().data(),
213 QString::number(it.value()["value"].toDouble()).toAscii().data());
215 Mlt::Producer p(*m_profile,s_url.toUtf8().data());
217 m_playlist->append(p);
218 m_playlist->attach(filter);
219 m_consumer= new Mlt::Consumer(*m_profile,"xml",destination.toUtf8().data());
220 m_consumer->set("all",1);
221 m_consumer->set("real_time",-2);
222 m_consumer->connect(*m_playlist);
223 m_stabilizeRun = QtConcurrent::run(this, &ClipStabilize::slotRunStabilize);
225 button_start->setEnabled(false);
233 void ClipStabilize::slotUpdateParams()
235 for (int i=0;i<vbox->count();i++){
236 QWidget* w=vbox->itemAt(i)->widget();
237 QString name=w->objectName();
238 if (!name.isEmpty() && m_ui_params.contains(name)){
239 if (m_ui_params[name]["type"]=="int" || m_ui_params[name]["type"]=="double"){
240 DoubleParameterWidget *dbl=(DoubleParameterWidget*)w;
241 m_ui_params[name]["value"]=QString::number((double)(dbl->getValue()));
242 }else if (m_ui_params[name]["type"]=="bool"){
243 QCheckBox *ch=(QCheckBox*)w;
244 m_ui_params[name]["value"]= ch->checkState() == Qt::Checked ? "1" : "0" ;
250 bool ClipStabilize::autoAddClip() const
252 return auto_add->isChecked();
255 void ClipStabilize::fillParameters(QStringList lst)
259 while (!lst.isEmpty()){
260 QString vallist=lst.takeFirst();
261 QStringList cont=vallist.split(',');
262 QString name=cont.takeFirst();
263 while (!cont.isEmpty()){
264 QString valname=cont.takeFirst();
266 if (!cont.isEmpty()){
267 val=cont.takeFirst();
269 m_ui_params[name][valname]=val;
276 #include "clipstabilize.moc"