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