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>
37 ClipStabilize::ClipStabilize(KUrl::List urls, const QString ¶ms, Mlt::Filter* filter,QWidget * parent) :
38 QDialog(parent), m_urls(urls), m_duration(0),m_profile(NULL),m_consumer(NULL),m_playlist(NULL),m_filter(filter),vbox(NULL)
40 setFont(KGlobalSettings::toolBarFont());
42 setAttribute(Qt::WA_DeleteOnClose);
43 log_text->setHidden(true);
44 setWindowTitle(i18n("Stabilize Clip"));
45 auto_add->setText(i18np("Add clip to project", "Add clips to project", m_urls.count()));
46 m_profile = new Mlt::Profile(KdenliveSettings::current_profile().toUtf8().constData());
50 QPalette p = palette();
51 KColorScheme scheme(p.currentColorGroup(), KColorScheme::View, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
52 QColor dark_bg = scheme.shade(KColorScheme::DarkShade);
53 QColor selected_bg = scheme.decoration(KColorScheme::FocusColor).color();
54 QColor hover_bg = scheme.decoration(KColorScheme::HoverColor).color();
55 QColor light_bg = scheme.shade(KColorScheme::LightShade);
57 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}\
58 QProgressBar:horizontal#dragOnly {background: %1} QProgressBar:horizontal:hover#dragOnly {background: %3} QProgressBar:horizontal:hover {border: 1px solid %3;border-right: 0px;}\
59 QProgressBar::chunk:horizontal {background: %1;} QProgressBar::chunk:horizontal:hover {background: %3;}\
60 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;}\
61 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;}\
62 QAbstractSpinBox::up-button#dragBox {width:0px;padding:0px;} QAbstractSpinBox[inTimeline=\"true\"]#dragBox { border: 1px solid %2;} QAbstractSpinBox:hover#dragBox {border: 1px solid %3;} ")
63 .arg(dark_bg.name()).arg(selected_bg.name()).arg(hover_bg.name()).arg(light_bg.name()));
64 setStyleSheet(stylesheet);
66 if (m_urls.count() == 1) {
67 QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
68 QString newFile = fileName.append(".mlt");
70 source_url->setUrl(m_urls.at(0));
71 dest_url->setMode(KFile::File);
72 dest_url->setUrl(dest);
73 dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
74 urls_list->setHidden(true);
75 connect(source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
77 label_source->setHidden(true);
78 source_url->setHidden(true);
79 label_dest->setText(i18n("Destination folder"));
80 dest_url->setMode(KFile::Directory);
81 dest_url->setUrl(KUrl(m_urls.at(0).directory()));
82 dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
83 for (int i = 0; i < m_urls.count(); i++)
84 urls_list->addItem(m_urls.at(i).path());
86 if (!params.isEmpty()) {
87 label_profile->setHidden(true);
88 profile_list->setHidden(true);
89 //ffmpeg_params->setPlainText(params.simplified());
90 /*if (!description.isEmpty()) {
91 transcode_info->setText(description);
92 } else transcode_info->setHidden(true);*/
95 if (filtername=="videostab"){
97 ls << "shutterangle,type,int,value,0,min,0,max,100";
99 }else if (filtername=="videostab2"){
101 ls << "accuracy,type,int,value,4,min,1,max,10";
102 ls << "stepsize,type,int,value,6,min,0,max,100";
103 ls << "algo,type,int,value,1,min,0,max,1";
104 ls << "mincontrast,type,int,value,0.3,min,0,max,1,factor,1,decimals,2";
105 ls << "show,type,int,value,0,min,0,max,2";
106 ls << "smoothing,type,int,value,10,min,0,max,100";
107 ls << "maxshift,type,int,value,-1,min,-1,max,1000";
108 ls << "maxangle,type,int,value,-1,min,-1,max,1000";
109 ls << "crop,type,int,value,0,min,0,max,1";
110 ls << "invert,type,int,value,0,min,0,max,1";
111 ls << "realtive,type,int,value,1,min,0,max,1";
112 ls << "zoom,type,int,value,0,min,-500,max,500";
113 ls << "optzoom,type,int,value,1,min,0,max,1";
114 ls << "sharpen,type,int,value,0.8,min,0,max,1,decimals,1";
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 DoubleParameterWidget *dbl=new DoubleParameterWidget(hi.key(), val["value"].toDouble(),
130 val["min"].toDouble(),val["max"].toDouble(),val["value"].toDouble(),
131 "comment",0/*id*/,""/*suffix*/,val["decimals"]!=""?val["decimals"].toInt():0,this);
132 dbl->setObjectName(hi.key());
133 connect(dbl,SIGNAL(valueChanged(double)),this,SLOT(slotUpdateParams()));
134 vbox->addWidget(dbl);
140 ClipStabilize::~ClipStabilize()
142 /*if (m_stabilizeProcess.state() != QProcess::NotRunning) {
143 m_stabilizeProcess.close();
145 if (m_stabilizeRun.isRunning()){
149 m_stabilizeRun.waitForFinished();
151 if (m_profile) free (m_profile);
152 if (m_consumer) free (m_consumer);
153 if (m_playlist) free (m_playlist);
156 void ClipStabilize::slotStartStabilize()
158 if (m_consumer && !m_consumer->is_stopped()) {
162 QStringList parameters;
164 //QString params = ffmpeg_params->toPlainText().simplified();
165 if (urls_list->count() > 0) {
166 source_url->setUrl(m_urls.takeFirst());
167 destination = dest_url->url().path(KUrl::AddTrailingSlash)+ source_url->url().fileName()+".mlt";
168 QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
169 if (matching.count() > 0) {
170 matching.at(0)->setFlags(Qt::ItemIsSelectable);
171 urls_list->setCurrentItem(matching.at(0));
174 destination = dest_url->url().path();
176 QString s_url = source_url->url().path();
178 if (QFile::exists(destination)) {
179 if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination )) == KMessageBox::No) return;
181 buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
184 m_playlist= new Mlt::Playlist;
185 Mlt::Filter filter(*m_profile,filtername.toUtf8().data());
186 QHashIterator <QString,QHash<QString,QString> > it(m_ui_params);
187 while (it.hasNext()){
190 it.key().toAscii().data(),
191 QString::number(it.value()["value"].toDouble()).toAscii().data());
193 Mlt::Producer p(*m_profile,s_url.toUtf8().data());
195 m_playlist->append(p);
196 m_playlist->attach(filter);
197 m_consumer= new Mlt::Consumer(*m_profile,"xml",destination.toUtf8().data());
198 m_consumer->set("all","1");
199 m_consumer->set("real_time","-2");
200 m_consumer->connect(*m_playlist);
201 m_stabilizeRun = QtConcurrent::run(this, &ClipStabilize::slotRunStabilize);
203 button_start->setEnabled(false);
209 void ClipStabilize::slotRunStabilize()
217 void ClipStabilize::slotAbortStabilize()
223 slotStabilizeFinished(false);
227 void ClipStabilize::slotShowStabilizeInfo()
230 job_progress->setValue((int) (100.0 * m_consumer->position()/m_playlist->get_out() ));
231 if (m_consumer->position()==m_playlist->get_out()){
233 slotStabilizeFinished(true);
238 void ClipStabilize::slotStabilizeFinished(bool success)
240 buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
241 button_start->setEnabled(true);
243 if (m_stabilizeRun.isRunning()){
247 m_stabilizeRun.waitForFinished();
251 log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilize finished."));
252 if (auto_add->isChecked()) {
254 if (urls_list->count() > 0) {
255 url = KUrl(dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName()+".mlt");
256 } else url = dest_url->url();
259 if (urls_list->count() > 0 && m_urls.count() > 0) {
260 slotStartStabilize();
262 } else if (auto_close->isChecked()) accept();
264 log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilizing FAILED!"));
277 void ClipStabilize::slotUpdateParams()
279 for (int i=0;i<vbox->count();i++){
280 QWidget* w=vbox->itemAt(i)->widget();
281 QString name=w->objectName();
282 if (name !="" && m_ui_params.contains(name)){
283 if (m_ui_params[name]["type"]=="int" || m_ui_params[name]["type"]=="double"){
284 DoubleParameterWidget *dbl=(DoubleParameterWidget*)w;
285 m_ui_params[name]["value"]=QString::number((double)(dbl->getValue()));
291 void ClipStabilize::fillParameters(QStringList lst)
295 while (!lst.isEmpty()){
296 QString vallist=lst.takeFirst();
297 QStringList cont=vallist.split(",");
298 QString name=cont.takeFirst();
299 while (!cont.isEmpty()){
300 QString valname=cont.takeFirst();
302 if (!cont.isEmpty()){
303 val=cont.takeFirst();
305 m_ui_params[name][valname]=val;
312 #include "clipstabilize.moc"