]> git.sesse.net Git - kdenlive/blob - src/clipstabilize.cpp
Remove not implemented function
[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 QStringList &urls, const QString &filterName,QWidget * parent) :
37     QDialog(parent),
38     m_filtername(filterName),
39     m_urls(urls),
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", urls.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_urls.count() == 1) {
65                                QString newFile = m_urls.first();
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(KUrl(m_urls.first()).directory()));
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,180,tooltip,Angle that Images could be maximum rotated";
81                                fillParameters(ls);
82 }else if (m_filtername=="videostab2"){
83                                // Some default params have to be set:
84                                m_fixedParams << "algo=1" << "relative=1";
85                                QStringList ls;
86                                ls << "accuracy,type,int,value,8,min,1,max,10,tooltip,Accuracy of Shakiness detection";
87                                ls << "shakiness,type,int,value,4,min,1,max,10,tooltip,How shaky is the Video";
88                                ls << "stepsize,type,int,value,6,min,0,max,100,tooltip,Stepsize of Detection process minimum around";
89                                //ls << "algo,type,bool,value,1,min,0,max,1,tooltip,0 = Bruteforce 1 = small measurement fields";
90                                ls << "mincontrast,type,double,value,0.3,min,0,max,1,factor,1,decimals,2,tooltip,Below this Contrast Field is discarded";
91                                //ls << "show,type,int,value,0,min,0,max,2,tooltip,0 = draw nothing. 1 or 2 show fields and transforms";
92                                ls << "smoothing,type,int,value,10,min,0,max,100,tooltip,number of frames for lowpass filtering";
93                                ls << "maxshift,type,int,value,-1,min,-1,max,1000,tooltip,max number of pixels to shift";
94                                ls << "maxangle,type,double,value,-1,min,-1,max,3.14,decimals,2,tooltip,max angle to rotate (in rad)";
95                                ls << "crop,type,bool,value,0,min,0,max,1,tooltip,0 = keep border  1 = black background";
96                                //ls << "invert,type,bool,value,0,min,0,max,1,tooltip,invert transform";
97                                //ls << "relative,type,bool,value,1,min,0,max,1,tooltip,0 = absolute transform  1= relative";
98                                ls << "zoom,type,int,value,0,min,-500,max,500,tooltip,additional zoom during transform";
99                                ls << "optzoom,type,bool,value,1,min,0,max,1,tooltip,use optimal zoom (calulated from transforms)";
100                                ls << "sharpen,type,double,value,0.8,min,0,max,1,decimals,1,tooltip,sharpen transformed image";
101                                fillParameters(ls);
102
103 }
104
105                                connect(buttonBox->button(QDialogButtonBox::Ok),SIGNAL(clicked()), this, SLOT(slotValidate()));
106
107                                vbox=new QVBoxLayout(optionsbox);
108                                QHashIterator<QString,QHash<QString,QString> > hi(m_ui_params);
109                                while(hi.hasNext()){
110                                hi.next();
111                                QHash<QString,QString> val=hi.value();
112                                if (val["type"]=="int" || val["type"]=="double"){
113         DoubleParameterWidget *dbl=new DoubleParameterWidget(hi.key(), val["value"].toDouble(),
114                 val["min"].toDouble(),val["max"].toDouble(),val["value"].toDouble(),
115                 "",0/*id*/,""/*suffix*/,val["decimals"]!=""?val["decimals"].toInt():0,this);
116         dbl->setObjectName(hi.key());
117         dbl->setToolTip(val["tooltip"]);
118         connect(dbl,SIGNAL(valueChanged(double)),this,SLOT(slotUpdateParams()));
119         vbox->addWidget(dbl);
120     }else if (val["type"]=="bool"){
121                            QCheckBox *ch=new QCheckBox(hi.key(),this);
122                            ch->setCheckState(val["value"] == "0" ? Qt::Unchecked : Qt::Checked);
123                            ch->setObjectName(hi.key());
124                            connect(ch, SIGNAL(stateChanged(int)) , this,SLOT(slotUpdateParams()));
125                            ch->setToolTip(val["tooltip"]);
126                            vbox->addWidget(ch);
127
128                        }
129 }
130 adjustSize();
131 }
132
133 ClipStabilize::~ClipStabilize()
134 {
135     /*if (m_stabilizeProcess.state() != QProcess::NotRunning) {
136         m_stabilizeProcess.close();
137     }*/
138     KdenliveSettings::setAdd_new_clip(auto_add->isChecked());
139 }
140
141 QStringList ClipStabilize::params()
142 {
143     //we must return a stringlist with:
144     // producerparams << filtername << filterparams << consumer << consumerparams
145     QStringList params;
146     // producer params
147     params << QString();
148     // filter
149     params << m_filtername;
150     QStringList filterparamsList = m_fixedParams;
151     QHashIterator <QString,QHash<QString,QString> > it(m_ui_params);
152     while (it.hasNext()){
153         it.next();
154         filterparamsList << it.key() + '=' + it.value().value("value");
155     }
156     params << filterparamsList.join(" ");
157     
158     // consumer
159     params << "xml";
160     // consumer params
161     QString title = i18n("Stabilised");
162     params << QString("all=1 title=\"%1\"").arg(title);
163     return params;
164 }
165
166 QString ClipStabilize::destination() const
167 {
168     if (m_urls.count() == 1)
169         return dest_url->url().path();
170     else
171         return dest_url->url().path(KUrl::AddTrailingSlash);
172 }
173
174 QString ClipStabilize::desc() const
175 {
176     return i18n("Stabilize clip");
177 }
178
179 void ClipStabilize::slotStartStabilize()
180 {
181     /*
182     if (m_consumer && !m_consumer->is_stopped()) {
183         return;
184     }
185     m_duration = 0;
186     QStringList parameters;
187     QString destination;
188     //QString params = ffmpeg_params->toPlainText().simplified();
189     if (urls_list->count() > 0) {
190         source_url->setUrl(m_urls.takeFirst());
191         destination = dest_url->url().path(KUrl::AddTrailingSlash)+ source_url->url().fileName()+".mlt";
192         QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
193         if (matching.count() > 0) {
194             matching.at(0)->setFlags(Qt::ItemIsSelectable);
195             urls_list->setCurrentItem(matching.at(0));
196         }
197     } else {
198         destination = dest_url->url().path();
199     }
200     QString s_url = source_url->url().path();
201
202     if (QFile::exists(destination)) {
203             if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination )) == KMessageBox::No) return;
204     }
205     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
206
207     if (m_profile){
208         m_playlist= new Mlt::Playlist;
209         Mlt::Filter filter(*m_profile,filtername.toUtf8().data());
210         QHashIterator <QString,QHash<QString,QString> > it(m_ui_params);
211         while (it.hasNext()){
212             it.next();
213             filter.set(
214                     it.key().toAscii().data(),
215                     QString::number(it.value()["value"].toDouble()).toAscii().data());
216         }
217         Mlt::Producer p(*m_profile,s_url.toUtf8().data());
218         if (p.is_valid()) {
219             m_playlist->append(p);
220             m_playlist->attach(filter);
221             m_consumer= new Mlt::Consumer(*m_profile,"xml",destination.toUtf8().data());
222             m_consumer->set("all",1);
223             m_consumer->set("real_time",-2);
224             m_consumer->connect(*m_playlist);
225             m_stabilizeRun = QtConcurrent::run(this, &ClipStabilize::slotRunStabilize);
226             m_timer->start(500);
227             button_start->setEnabled(false);
228         }
229     }
230 */
231 }
232
233
234
235 void ClipStabilize::slotUpdateParams()
236 {
237     for (int i=0;i<vbox->count();++i){
238         QWidget* w=vbox->itemAt(i)->widget();
239         QString name=w->objectName();
240         if (!name.isEmpty() && m_ui_params.contains(name)){
241             if (m_ui_params[name]["type"]=="int" || m_ui_params[name]["type"]=="double"){
242                 DoubleParameterWidget *dbl=(DoubleParameterWidget*)w;
243                 m_ui_params[name]["value"]=QString::number((double)(dbl->getValue()));
244             }else if (m_ui_params[name]["type"]=="bool"){
245                 QCheckBox *ch=(QCheckBox*)w;
246                 m_ui_params[name]["value"]= ch->checkState() == Qt::Checked ? "1" : "0" ;
247             }
248         }
249     }
250 }
251
252 bool ClipStabilize::autoAddClip() const
253 {
254     return auto_add->isChecked();
255 }
256
257 void ClipStabilize::fillParameters(QStringList lst)
258 {
259
260     m_ui_params.clear();
261     while (!lst.isEmpty()){
262         QString vallist=lst.takeFirst();
263         QStringList cont=vallist.split(',');
264         QString name=cont.takeFirst();
265         while (!cont.isEmpty()){
266             QString valname=cont.takeFirst();
267             QString val;
268             if (!cont.isEmpty()){
269                 val=cont.takeFirst();
270             }
271             m_ui_params[name][valname]=val;
272         }
273     }
274
275 }
276
277 void ClipStabilize::slotValidate()
278 {
279     if (m_urls.count() == 1) {
280         if (QFile::exists(dest_url->url().path())) {
281             if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", dest_url->url().path() )) == KMessageBox::No) return;
282         }
283     }
284     else {
285         KUrl folder(dest_url->url());
286         QStringList existingFiles;
287         foreach(const QString &path, m_urls) {
288             KUrl dest = folder;
289             dest.addPath(KUrl(path).fileName());
290             if (QFile::exists(dest.path() + ".mlt")) existingFiles.append(dest.path() + ".mlt");
291         }
292         if (!existingFiles.isEmpty()) {
293             if (KMessageBox::warningContinueCancelList(this, i18n("The stabilize job will overwrite the following files:"), existingFiles) ==  KMessageBox::Cancel) return;
294         }
295     }
296     accept();
297 }
298
299 #include "clipstabilize.moc"
300
301