]> git.sesse.net Git - kdenlive/blob - src/clipstabilize.cpp
Merge branch 'master' of git://anongit.kde.org/kdenlive
[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 ClipStabilize::ClipStabilize(const QString &dest, int count, const QString &filterName,QWidget * parent) :
37         QDialog(parent), 
38         m_filtername(filterName),
39         m_count(count),
40         vbox(NULL)
41 {
42     setFont(KGlobalSettings::toolBarFont());
43     setupUi(this);
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());
47
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);
54
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);
63
64     if (m_count == 1) {
65         QString newFile = dest;
66         newFile.append(".mlt");
67         KUrl dest(newFile);
68         dest_url->setMode(KFile::File);
69         dest_url->setUrl(KUrl(newFile));
70         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
71     } else {
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);
76     }
77
78     if (m_filtername=="videostab"){
79         QStringList ls;
80         ls << "shutterangle,type,int,value,0,min,0,max,100,tooltip,Angle that Images could be maximum rotated";
81         fillParameters(ls);
82     }else if (m_filtername=="videostab2"){
83         QStringList ls;
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 << "relative,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";
99         fillParameters(ls);
100
101     }
102
103     //connect(buttonBox,SIGNAL(rejected()), this, SLOT(slotAbortStabilize()));
104
105     vbox=new QVBoxLayout(optionsbox);
106     QHashIterator<QString,QHash<QString,QString> > hi(m_ui_params);
107     while(hi.hasNext()){
108         hi.next();
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"]);
124             vbox->addWidget(ch);
125         
126         }
127     }
128     adjustSize();
129 }
130
131 ClipStabilize::~ClipStabilize()
132 {
133     /*if (m_stabilizeProcess.state() != QProcess::NotRunning) {
134         m_stabilizeProcess.close();
135     }*/
136     KdenliveSettings::setAdd_new_clip(auto_add->isChecked());
137 }
138
139 QStringList ClipStabilize::params()
140 {
141     //we must return a stringlist with:
142     // producerparams << filtername << filterparams << consumer << consumerparams
143     QStringList params;
144     // producer params
145     params << QString();
146     // filter
147     params << m_filtername;
148     QStringList filterparamsList;
149     QHashIterator <QString,QHash<QString,QString> > it(m_ui_params);
150     while (it.hasNext()){
151         it.next();
152         filterparamsList << it.key() + '=' + it.value().value("value");
153     }
154     params << filterparamsList.join(" ");
155     
156     // consumer
157     params << "xml";
158     // consumer params
159     QString title = i18n("Stabilised");
160     params << QString("all=1 title=\"%1\"").arg(title);
161     return params;
162 }
163
164 QString ClipStabilize::destination() const
165 {
166     if (m_count == 1)
167         return dest_url->url().path();
168     else
169         return dest_url->url().path(KUrl::AddTrailingSlash);
170 }
171
172 QString ClipStabilize::desc() const
173 {
174     return i18n("Stabilize clip");
175 }
176
177 void ClipStabilize::slotStartStabilize()
178 {
179     /*
180     if (m_consumer && !m_consumer->is_stopped()) {
181         return;
182     }
183     m_duration = 0;
184     QStringList parameters;
185     QString destination;
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));
194         }
195     } else {
196         destination = dest_url->url().path();
197     }
198     QString s_url = source_url->url().path();
199
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;
202     }
203     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
204
205     if (m_profile){
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()){
210             it.next();
211             filter.set(
212                     it.key().toAscii().data(),
213                     QString::number(it.value()["value"].toDouble()).toAscii().data());
214         }
215         Mlt::Producer p(*m_profile,s_url.toUtf8().data());
216         if (p.is_valid()) {
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);
224             m_timer->start(500);
225             button_start->setEnabled(false);
226         }
227     }
228 */
229 }
230
231
232
233 void ClipStabilize::slotUpdateParams()
234 {
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" ;
245             }
246         }
247     }
248 }
249
250 bool ClipStabilize::autoAddClip() const
251 {
252     return auto_add->isChecked();
253 }
254
255 void ClipStabilize::fillParameters(QStringList lst)
256 {
257
258     m_ui_params.clear();
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();
265             QString val;
266             if (!cont.isEmpty()){
267                 val=cont.takeFirst();
268             }
269             m_ui_params[name][valname]=val;
270         }
271     }
272
273 }
274
275
276 #include "clipstabilize.moc"
277
278