Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

renderwidget.cpp 113 KB
Newer Older
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/***************************************************************************
 *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

20

21 22 23
#include "renderwidget.h"
#include "kdenlivesettings.h"
#include "ui_saveprofile_ui.h"
24
#include "timecode.h"
Vincent Pinon's avatar
Vincent Pinon committed
25
#include "dialogs/profilesdialog.h"
26
#include "utils/KoIconUtils.h"
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
27

28
#include "klocalizedstring.h"
29
#include <KMessageBox>
30
#include <KRun>
31
#include <KColorScheme>
32
#include <KNotification>
33 34
#include <KMimeTypeTrader>
#include <KIO/DesktopExecParser>
35

36 37
#include <qglobal.h>
#include <qstring.h>
38
#include <QDebug>
39 40 41 42
#include <QDomDocument>
#include <QTreeWidgetItem>
#include <QHeaderView>
#include <QInputDialog>
43
#include <QProcess>
44
#include <QDBusConnectionInterface>
45
#include <QThread>
46
#include <QScriptEngine>
47
#include <QKeyEvent>
48
#include <QTimer>
49
#include <QStandardPaths>
50
#include <QMimeDatabase>
51
#include <QDir>
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
52

Vincent Pinon's avatar
Vincent Pinon committed
53
#include <locale>
54 55 56
#ifdef Q_OS_MAC
#include <xlocale.h>
#endif
57

58 59

// Render profiles roles
60 61 62 63 64 65 66 67 68 69
enum {GroupRole = Qt::UserRole,
      ExtensionRole,
      StandardRole,
      RenderRole,
      ParamsRole,
      EditableRole,
      ExtraRole,
      BitratesRole,
      DefaultBitrateRole,
      AudioBitratesRole,
70
      DefaultAudioBitrateRole,
71
      SpeedsRole,
72
      ErrorRole
73
     };
74

75 76 77
// Render job roles
const int ParametersRole = Qt::UserRole + 1;
const int TimeRole = Qt::UserRole + 2;
78 79
const int ProgressRole = Qt::UserRole + 3;
const int ExtraInfoRole = Qt::UserRole + 5;
80

81 82
const int DirectRenderType = QTreeWidgetItem::Type;
const int ScriptRenderType = QTreeWidgetItem::UserType;
83 84


85
// Running job status
86 87 88 89 90 91 92 93
enum JOBSTATUS {
    WAITINGJOB = 0,
    STARTINGJOB,
    RUNNINGJOB,
    FINISHEDJOB,
    FAILEDJOB,
    ABORTEDJOB
};
94 95


Laurent Montel's avatar
Laurent Montel committed
96 97
RenderJobItem::RenderJobItem(QTreeWidget * parent, const QStringList & strings, int type)
    : QTreeWidgetItem(parent, strings, type),
98 99
    m_status(-1)
{
100
    setSizeHint(1, QSize(parent->columnWidth(1), parent->fontMetrics().height() * 3));
101 102 103 104 105
    setStatus(WAITINGJOB);
}

void RenderJobItem::setStatus(int status)
{
Laurent Montel's avatar
Laurent Montel committed
106 107
    if (m_status == status)
        return;
108 109 110
    m_status = status;
    switch (status) {
        case WAITINGJOB:
111
            setIcon(0, KoIconUtils::themedIcon(QStringLiteral("media-playback-pause")));
112 113 114 115
            setData(1, Qt::UserRole, i18n("Waiting..."));
            break;
        case FINISHEDJOB:
            setData(1, Qt::UserRole, i18n("Rendering finished"));
116
            setIcon(0, KoIconUtils::themedIcon(QStringLiteral("dialog-ok")));
117
            setData(1, ProgressRole, 100);
118 119 120
            break;
        case FAILEDJOB:
            setData(1, Qt::UserRole, i18n("Rendering crashed"));
121
            setIcon(0, KoIconUtils::themedIcon(QStringLiteral("dialog-close")));
122
            setData(1, ProgressRole, 100);
123 124 125
            break;
        case ABORTEDJOB:
            setData(1, Qt::UserRole, i18n("Rendering aborted"));
126
            setIcon(0, KoIconUtils::themedIcon(QStringLiteral("dialog-cancel")));
127
            setData(1, ProgressRole, 100);
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        default:
            break;
    }
}

int RenderJobItem::status() const
{
    return m_status;
}

void RenderJobItem::setMetadata(const QString &data)
{
    m_data = data;
}

const QString RenderJobItem::metadata() const
{
    return m_data;
}
147 148


Laurent Montel's avatar
Laurent Montel committed
149
RenderWidget::RenderWidget(const QString &projectfolder, bool enableProxy, const MltVideoProfile &profile, QWidget * parent) :
150
        QDialog(parent),
151
        m_projectFolder(projectfolder),
152
        m_profile(profile),
153
        m_blockProcessing(false)
154
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
155
    m_view.setupUi(this);
156 157
    int size = style()->pixelMetric(QStyle::PM_SmallIconSize);
    QSize iconSize(size, size);
158

159
    setWindowTitle(i18n("Rendering"));
160 161 162 163
    m_view.buttonDelete->setIconSize(iconSize);
    m_view.buttonEdit->setIconSize(iconSize);
    m_view.buttonSave->setIconSize(iconSize);
    m_view.buttonFavorite->setIconSize(iconSize);
164

165
    m_view.buttonDelete->setIcon(KoIconUtils::themedIcon(QStringLiteral("trash-empty")));
166 167 168
    m_view.buttonDelete->setToolTip(i18n("Delete profile"));
    m_view.buttonDelete->setEnabled(false);

169
    m_view.buttonEdit->setIcon(KoIconUtils::themedIcon(QStringLiteral("document-edit")));
170 171 172
    m_view.buttonEdit->setToolTip(i18n("Edit profile"));
    m_view.buttonEdit->setEnabled(false);

173
    m_view.buttonSave->setIcon(KoIconUtils::themedIcon(QStringLiteral("document-new")));
174 175
    m_view.buttonSave->setToolTip(i18n("Create new profile"));

176
    m_view.hide_log->setIcon(KoIconUtils::themedIcon(QStringLiteral("go-down")));
177

178
    m_view.buttonFavorite->setIcon(KoIconUtils::themedIcon(QStringLiteral("favorite")));
179
    m_view.buttonFavorite->setToolTip(i18n("Copy profile to favorites"));
180

181
    m_view.out_file->button()->setToolTip(i18n("Select output destination"));
182
    m_view.advanced_params->setMaximumHeight(QFontMetrics(font()).lineSpacing() * 5);
183

184 185 186 187 188 189 190 191 192 193 194 195
    m_view.optionsGroup->setVisible(m_view.options->isChecked());
    connect(m_view.options, SIGNAL(toggled(bool)), m_view.optionsGroup, SLOT(setVisible(bool)));
    m_view.videoLabel->setVisible(m_view.options->isChecked());
    connect(m_view.options, SIGNAL(toggled(bool)), m_view.videoLabel, SLOT(setVisible(bool)));
    m_view.video->setVisible(m_view.options->isChecked());
    connect(m_view.options, SIGNAL(toggled(bool)), m_view.video, SLOT(setVisible(bool)));
    m_view.audioLabel->setVisible(m_view.options->isChecked());
    connect(m_view.options, SIGNAL(toggled(bool)), m_view.audioLabel, SLOT(setVisible(bool)));
    m_view.audio->setVisible(m_view.options->isChecked());
    connect(m_view.options, SIGNAL(toggled(bool)), m_view.audio, SLOT(setVisible(bool)));
    connect(m_view.quality, SIGNAL(valueChanged(int)), this, SLOT(adjustAVQualities(int)));
    connect(m_view.video, SIGNAL(valueChanged(int)), this, SLOT(adjustQuality(int)));
196
    connect(m_view.speed, SIGNAL(valueChanged(int)), this, SLOT(adjustSpeed(int)));
197

198 199
    m_view.buttonRender->setEnabled(false);
    m_view.buttonGenerateScript->setEnabled(false);
200
    setRescaleEnabled(false);
201 202 203 204 205 206 207
    m_view.guides_box->setVisible(false);
    m_view.open_dvd->setVisible(false);
    m_view.create_chapter->setVisible(false);
    m_view.open_browser->setVisible(false);
    m_view.error_box->setVisible(false);
    m_view.tc_type->setEnabled(false);
    m_view.checkTwoPass->setEnabled(false);
208
    m_view.proxy_render->setHidden(!enableProxy);
209

210 211
    KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
    QColor bg = scheme.background(KColorScheme::NegativeBackground).color();
212
    m_view.errorBox->setStyleSheet(QStringLiteral("QGroupBox { background-color: rgb(%1, %2, %3); border-radius: 5px;}; ").arg(bg.red()).arg(bg.green()).arg(bg.blue()));
213
    int height = QFontInfo(font()).pixelSize();
214
    m_view.errorIcon->setPixmap(KoIconUtils::themedIcon(QStringLiteral("dialog-warning")).pixmap(height, height));
215 216 217
    m_view.errorBox->setHidden(true);

    m_infoMessage = new KMessageWidget;
218
    m_view.info->addWidget(m_infoMessage);
219 220 221
    m_infoMessage->setCloseButtonVisible(false);
    m_infoMessage->hide();

222 223 224 225
    m_view.encoder_threads->setMaximum(QThread::idealThreadCount());
    m_view.encoder_threads->setValue(KdenliveSettings::encodethreads());
    connect(m_view.encoder_threads, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateEncodeThreads(int)));

226 227 228
    m_view.rescale_keep->setChecked(KdenliveSettings::rescalekeepratio());
    connect(m_view.rescale_width, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateRescaleWidth(int)));
    connect(m_view.rescale_height, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateRescaleHeight(int)));
229
    m_view.rescale_keep->setIcon(KoIconUtils::themedIcon(QStringLiteral("edit-link")));
230 231
    m_view.rescale_keep->setToolTip(i18n("Preserve aspect ratio"));
    connect(m_view.rescale_keep, SIGNAL(clicked()), this, SLOT(slotSwitchAspectRatio()));
232

233 234 235
    connect(m_view.buttonRender, SIGNAL(clicked()), this, SLOT(slotPrepareExport()));
    connect(m_view.buttonGenerateScript, SIGNAL(clicked()), this, SLOT(slotGenerateScript()));

236 237 238 239
    m_view.abort_job->setEnabled(false);
    m_view.start_script->setEnabled(false);
    m_view.delete_script->setEnabled(false);

240 241 242
    connect(m_view.export_audio, SIGNAL(stateChanged(int)), this, SLOT(slotUpdateAudioLabel(int)));
    m_view.export_audio->setCheckState(Qt::PartiallyChecked);

243
    parseProfiles();
244
    parseScriptFiles();
245 246
    m_view.running_jobs->setUniformRowHeights(false);
    m_view.scripts_list->setUniformRowHeights(false);
247 248
    connect(m_view.start_script, SIGNAL(clicked()), this, SLOT(slotStartScript()));
    connect(m_view.delete_script, SIGNAL(clicked()), this, SLOT(slotDeleteScript()));
249 250
    connect(m_view.scripts_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckScript()));
    connect(m_view.running_jobs, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckJob()));
Laurent Montel's avatar
Laurent Montel committed
251
    connect(m_view.running_jobs, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(slotPlayRendering(QTreeWidgetItem*,int)));
252

253
    connect(m_view.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
254
    connect(m_view.buttonEdit, SIGNAL(clicked()), this, SLOT(slotEditProfile()));
255
    connect(m_view.buttonDelete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
256 257
    connect(m_view.buttonFavorite, SIGNAL(clicked()), this, SLOT(slotCopyToFavorites()));

258
    connect(m_view.abort_job, SIGNAL(clicked()), this, SLOT(slotAbortCurrentJob()));
259
    connect(m_view.start_job, SIGNAL(clicked()), this, SLOT(slotStartCurrentJob()));
260 261 262
    connect(m_view.clean_up, SIGNAL(clicked()), this, SLOT(slotCLeanUpJobs()));
    connect(m_view.hide_log, SIGNAL(clicked()), this, SLOT(slotHideLog()));

263 264
    connect(m_view.buttonClose, SIGNAL(clicked()), this, SLOT(hide()));
    connect(m_view.buttonClose2, SIGNAL(clicked()), this, SLOT(hide()));
265
    connect(m_view.buttonClose3, SIGNAL(clicked()), this, SLOT(hide()));
266
    connect(m_view.rescale, SIGNAL(toggled(bool)), this, SLOT(setRescaleEnabled(bool)));
Laurent Montel's avatar
Laurent Montel committed
267
    connect(m_view.out_file, SIGNAL(textChanged(QString)), this, SLOT(slotUpdateButtons()));
268
    connect(m_view.out_file, SIGNAL(urlSelected(QUrl)), this, SLOT(slotUpdateButtons(QUrl)));
269

270 271
    connect(m_view.formats, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(refreshParams()));
    connect(m_view.formats, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(slotEditItem(QTreeWidgetItem*)));
272

273 274 275 276 277 278 279
    connect(m_view.render_guide, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
    connect(m_view.render_zone, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
    connect(m_view.render_full, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));

    connect(m_view.guide_end, SIGNAL(activated(int)), this, SLOT(slotCheckStartGuidePosition()));
    connect(m_view.guide_start, SIGNAL(activated(int)), this, SLOT(slotCheckEndGuidePosition()));

280
    connect(m_view.tc_overlay, SIGNAL(toggled(bool)), m_view.tc_type, SLOT(setEnabled(bool)));
281

282 283
    //m_view.splitter->setStretchFactor(1, 5);
    //m_view.splitter->setStretchFactor(0, 2);
284

285
    m_view.out_file->setMode(KFile::File);
286
    m_view.out_file->setFocusPolicy(Qt::ClickFocus);
287

288
    m_view.running_jobs->setHeaderLabels(QStringList() << QString() << i18n("File"));
289 290
    m_jobsDelegate = new RenderViewDelegate(this);
    m_view.running_jobs->setItemDelegate(m_jobsDelegate);
291

292
    QHeaderView *header = m_view.running_jobs->header();
293
    header->setSectionResizeMode(0, QHeaderView::Fixed);
294
    header->resizeSection(0, 30);
295
    header->setSectionResizeMode(1, QHeaderView::Interactive);
296

297
    m_view.scripts_list->setHeaderLabels(QStringList() << QString() << i18n("Script Files"));
298 299
    m_scriptsDelegate = new RenderViewDelegate(this);
    m_view.scripts_list->setItemDelegate(m_scriptsDelegate);
300
    header = m_view.scripts_list->header();
301
    header->setSectionResizeMode(0, QHeaderView::Fixed);
302
    header->resizeSection(0, 30);
303

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
304
    // Find path for Kdenlive renderer
305
    m_renderer = QCoreApplication::applicationDirPath() + QStringLiteral("/kdenlive_render");
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
306
    if (!QFile::exists(m_renderer)) {
307
        m_renderer = QStandardPaths::findExecutable(QStringLiteral("kdenlive_render"));
Laurent Montel's avatar
Laurent Montel committed
308
        if (m_renderer.isEmpty())
309
            m_renderer = QStringLiteral("kdenlive_render");
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
310 311
    }

312
    QDBusConnectionInterface* interface = QDBusConnection::sessionBus().interface();
313
    if (!interface || (!interface->isServiceRegistered(QStringLiteral("org.kde.ksmserver")) && !interface->isServiceRegistered(QStringLiteral("org.gnome.SessionManager"))))
314 315
        m_view.shutdown->setEnabled(false);

316
    focusFirstVisibleItem();
317
    adjustSize();
318 319 320 321 322 323
}

QSize RenderWidget::sizeHint() const
{
    // Make sure the widget has minimum size on opening
    return QSize(200, 200);
324 325
}

326 327 328 329 330 331 332 333
RenderWidget::~RenderWidget()
{
    m_view.running_jobs->blockSignals(true);
    m_view.scripts_list->blockSignals(true);
    m_view.running_jobs->clear();
    m_view.scripts_list->clear();
    delete m_jobsDelegate;
    delete m_scriptsDelegate;
334
    delete m_infoMessage;
335 336
}

337
void RenderWidget::slotEditItem(QTreeWidgetItem *item)
338
{
339 340 341 342
    if (item->parent() == NULL) {
        // This is a top level item - group - don't edit
        return;
    }
343
    const QString edit = item->data(0, EditableRole).toString();
Laurent Montel's avatar
Laurent Montel committed
344 345
    if (edit.isEmpty() || !edit.endsWith(QLatin1String("customprofiles.xml")))
        slotSaveProfile();
346 347 348
    else slotEditProfile();
}

349 350
void RenderWidget::showInfoPanel()
{
351 352 353 354 355 356 357
    if (m_view.advanced_params->isVisible()) {
        m_view.advanced_params->setVisible(false);
        KdenliveSettings::setShowrenderparams(false);
    } else {
        m_view.advanced_params->setVisible(true);
        KdenliveSettings::setShowrenderparams(true);
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
358 359
}

360
void RenderWidget::setDocumentPath(const QString &path)
361
{
362
    if (m_view.out_file->url().adjusted(QUrl::RemoveFilename).path() == QUrl::fromLocalFile(m_projectFolder).adjusted(QUrl::RemoveFilename).path()) {
363
        const QString fileName = m_view.out_file->url().fileName();
364
        m_view.out_file->setUrl(QUrl::fromLocalFile(path + fileName));
365
    }
366
    m_projectFolder = path;
367
    parseScriptFiles();
368

369 370
}

371 372
void RenderWidget::slotUpdateGuideBox()
{
373 374 375
    m_view.guides_box->setVisible(m_view.render_guide->isChecked());
}

376 377
void RenderWidget::slotCheckStartGuidePosition()
{
378 379 380 381
    if (m_view.guide_start->currentIndex() > m_view.guide_end->currentIndex())
        m_view.guide_start->setCurrentIndex(m_view.guide_end->currentIndex());
}

382 383
void RenderWidget::slotCheckEndGuidePosition()
{
384 385 386 387
    if (m_view.guide_end->currentIndex() < m_view.guide_start->currentIndex())
        m_view.guide_end->setCurrentIndex(m_view.guide_start->currentIndex());
}

388
void RenderWidget::setGuides(QMap <double, QString> guidesData, double duration)
389
{
390 391
    m_view.guide_start->clear();
    m_view.guide_end->clear();
392
    if (!guidesData.isEmpty()) {
393
        m_view.guide_start->addItem(i18n("Beginning"), "0");
394
        m_view.render_guide->setEnabled(true);
395 396 397 398 399
        m_view.create_chapter->setEnabled(true);
    } else {
        m_view.render_guide->setEnabled(false);
        m_view.create_chapter->setEnabled(false);
    }
400
    double fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
401 402 403 404 405 406 407
    QMapIterator<double, QString> i(guidesData);
    while (i.hasNext()) {
        i.next();
        GenTime pos = GenTime(i.key());
        const QString guidePos = Timecode::getStringTimecode(pos.frames(fps), fps);
        m_view.guide_start->addItem(i.value() + '/' + guidePos, i.key());
        m_view.guide_end->addItem(i.value() + '/' + guidePos, i.key());
408
    }
409
    if (!guidesData.isEmpty())
410 411 412
        m_view.guide_end->addItem(i18n("End"), QString::number(duration));
}

413
/**
Simon Eugster's avatar
Simon Eugster committed
414 415 416
 * Will be called when the user selects an output file via the file dialog.
 * File extension will be added automatically.
 */
417
void RenderWidget::slotUpdateButtons(const QUrl &url)
418
{
419 420 421 422
    if (m_view.out_file->url().isEmpty()) {
        m_view.buttonGenerateScript->setEnabled(false);
        m_view.buttonRender->setEnabled(false);
    } else {
423 424
        updateButtons(); // This also checks whether the selected format is available
    }
425
    if (url.isValid()) {
426 427
        QTreeWidgetItem *item = m_view.formats->currentItem();
        if (!item || !item->parent()) { // categories have no parent
428 429
            m_view.buttonRender->setEnabled(false);
            m_view.buttonGenerateScript->setEnabled(false);
430 431
            return;
        }
432
        const QString extension = item->data(0, ExtensionRole).toString();
Laurent Montel's avatar
Laurent Montel committed
433
        m_view.out_file->setUrl(filenameWithExtension(url, extension));
434 435 436
    }
}

Simon Eugster's avatar
Simon Eugster committed
437 438 439 440
/**
 * Will be called when the user changes the output file path in the text line.
 * File extension must NOT be added, would make editing impossible!
 */
441 442
void RenderWidget::slotUpdateButtons()
{
443 444 445 446 447 448
    if (m_view.out_file->url().isEmpty()) {
        m_view.buttonRender->setEnabled(false);
        m_view.buttonGenerateScript->setEnabled(false);
    } else {
        updateButtons(); // This also checks whether the selected format is available
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
449 450
}

451 452
void RenderWidget::slotSaveProfile()
{
453
    Ui::SaveProfile_UI ui;
454
    QPointer<QDialog> d = new QDialog(this);
455
    ui.setupUi(d);
456

457
    QString customGroup;
458
    QStringList arguments = m_view.advanced_params->toPlainText().split(' ', QString::SkipEmptyParts);
459
    if (!arguments.isEmpty()) {
460
        ui.parameters->setText(arguments.join(QStringLiteral(" ")));
461
    }
462
    ui.profile_name->setFocus();
463 464
    QTreeWidgetItem *item = m_view.formats->currentItem();
    if (item && item->parent()) { //not a category
465
        // Duplicate current item settings
466 467
        customGroup = item->parent()->text(0);
        ui.extension->setText(item->data(0, ExtensionRole).toString());
468 469
        if (ui.parameters->toPlainText().contains(QStringLiteral("%bitrate")) || ui.parameters->toPlainText().contains(QStringLiteral("%quality"))) {
            if (ui.parameters->toPlainText().contains(QStringLiteral("%quality"))) {
470 471 472 473 474 475
                ui.vbitrates_label->setText(i18n("Qualities"));
                ui.default_vbitrate_label->setText(i18n("Default quality"));
            } else {
                ui.vbitrates_label->setText(i18n("Bitrates"));
                ui.default_vbitrate_label->setText(i18n("Default bitrate"));
            }
476 477
            if (item->data(0, BitratesRole).canConvert(QVariant::StringList) && item->data(0, BitratesRole).toStringList().count()) {
                QStringList bitrates = item->data(0, BitratesRole).toStringList();
478
                ui.vbitrates_list->setText(bitrates.join(QStringLiteral(",")));
479 480
                if (item->data(0, DefaultBitrateRole).canConvert(QVariant::String))
                    ui.default_vbitrate->setValue(item->data(0, DefaultBitrateRole).toInt());
481
            }
482
        }
483
        else ui.vbitrates->setHidden(true);
484 485
        if (ui.parameters->toPlainText().contains(QStringLiteral("%audiobitrate")) || ui.parameters->toPlainText().contains(QStringLiteral("%audioquality"))) {
            if (ui.parameters->toPlainText().contains(QStringLiteral("%audioquality"))) {
486 487 488 489 490 491
                ui.abitrates_label->setText(i18n("Qualities"));
                ui.default_abitrate_label->setText(i18n("Default quality"));
            } else {
                ui.abitrates_label->setText(i18n("Bitrates"));
                ui.default_abitrate_label->setText(i18n("Default bitrate"));
            }
492 493
            if ( item && item->data(0, AudioBitratesRole).canConvert(QVariant::StringList) && item->data(0, AudioBitratesRole).toStringList().count()) {
                QStringList bitrates = item->data(0, AudioBitratesRole).toStringList();
494
                ui.abitrates_list->setText(bitrates.join(QStringLiteral(",")));
495 496
                if (item->data(0, DefaultAudioBitrateRole).canConvert(QVariant::String))
                    ui.default_abitrate->setValue(item->data(0, DefaultAudioBitrateRole).toInt());
497
            }
498
        }
499
        else ui.abitrates->setHidden(true);
500 501 502 503 504

        if (item->data(0, SpeedsRole).canConvert(QVariant::StringList) && item->data(0, SpeedsRole).toStringList().count()) {
            QStringList speeds = item->data(0, SpeedsRole).toStringList();
            ui.speeds_list->setText(speeds.join('\n'));
        }
505
    }
506

507 508
    if (customGroup.isEmpty()) customGroup = i18nc("Group Name", "Custom");
    ui.group_name->setText(customGroup);
509

510
    if (d->exec() == QDialog::Accepted && !ui.profile_name->text().simplified().isEmpty()) {
511 512
        QString newProfileName = ui.profile_name->text().simplified();
        QString newGroupName = ui.group_name->text().simplified();
Yuri Chornoivan's avatar
Yuri Chornoivan committed
513
        if (newGroupName.isEmpty()) newGroupName = i18nc("Group Name", "Custom");
514 515

        QDomDocument doc;
516 517 518 519
        QDomElement profileElement = doc.createElement(QStringLiteral("profile"));
        profileElement.setAttribute(QStringLiteral("name"), newProfileName);
        profileElement.setAttribute(QStringLiteral("category"), newGroupName);
        profileElement.setAttribute(QStringLiteral("extension"), ui.extension->text().simplified());
520
        QString args = ui.parameters->toPlainText().simplified();
521 522
        profileElement.setAttribute(QStringLiteral("args"), args);
        if (args.contains(QStringLiteral("%bitrate"))) {
523
            // profile has a variable bitrate
524 525 526 527 528
            profileElement.setAttribute(QStringLiteral("defaultbitrate"), QString::number(ui.default_vbitrate->value()));
            profileElement.setAttribute(QStringLiteral("bitrates"), ui.vbitrates_list->text());
        } else if (args.contains(QStringLiteral("%quality"))) {
            profileElement.setAttribute(QStringLiteral("defaultquality"), QString::number(ui.default_vbitrate->value()));
            profileElement.setAttribute(QStringLiteral("qualities"), ui.vbitrates_list->text());
529
        }
530

531
        if (args.contains(QStringLiteral("%audiobitrate"))) {
532
            // profile has a variable bitrate
533 534 535
            profileElement.setAttribute(QStringLiteral("defaultaudiobitrate"), QString::number(ui.default_abitrate->value()));
            profileElement.setAttribute(QStringLiteral("audiobitrates"), ui.abitrates_list->text());
        } else if (args.contains(QStringLiteral("%audioquality"))) {
536
            // profile has a variable bitrate
537 538
            profileElement.setAttribute(QStringLiteral("defaultaudioquality"), QString::number(ui.default_abitrate->value()));
            profileElement.setAttribute(QStringLiteral("audioqualities"), ui.abitrates_list->text());
539
        }
540
        if (!ui.speeds_list->toPlainText().isEmpty()) {
541 542
            profileElement.setAttribute(QStringLiteral("speeds"), ui.speeds_list->toPlainText().replace('\n', ';').simplified());
        }
543

544 545 546
        doc.appendChild(profileElement);
        saveProfile(doc.documentElement());

547
        parseProfiles();
548 549 550 551 552
    }
    delete d;
}


553
bool RenderWidget::saveProfile(QDomElement newprofile)
554
{
555 556
    QDir dir(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/export/");
    if (!dir.exists()) {
557
        dir.mkpath(QStringLiteral("."));
558
    }
559
    QString exportFile = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/export/customprofiles.xml";
560
    QDomDocument doc;
561
    QFile file(dir.absoluteFilePath(QStringLiteral("customprofiles.xml")));
562 563 564 565
    doc.setContent(&file, false);
    file.close();
    QDomElement documentElement;
    QDomElement profiles = doc.documentElement();
566
    if (profiles.isNull() || profiles.tagName() != QLatin1String("profiles")) {
567
        doc.clear();
568 569
        profiles = doc.createElement(QStringLiteral("profiles"));
        profiles.setAttribute(QStringLiteral("version"), 1);
570 571
        doc.appendChild(profiles);
    }
572
    int version = profiles.attribute(QStringLiteral("version"), 0).toInt();
573 574
    if (version < 1) {
        doc.clear();
575 576
        profiles = doc.createElement(QStringLiteral("profiles"));
        profiles.setAttribute(QStringLiteral("version"), 1);
577 578
        doc.appendChild(profiles);
    }
579

580

581
    QDomNodeList profilelist = doc.elementsByTagName(QStringLiteral("profile"));
582 583 584
    QString newProfileName = newprofile.attribute(QStringLiteral("name"));
    // Check existing profiles
    QStringList existingProfileNames;
585 586 587
    int i = 0;
    while (!profilelist.item(i).isNull()) {
        documentElement = profilelist.item(i).toElement();
588
        QString profileName = documentElement.attribute(QStringLiteral("name"));
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
        existingProfileNames << profileName;
        i++;
    }
    // Check if a profile with that same name already exists
    bool ok;
    while (existingProfileNames.contains(newProfileName)) {
        QString updatedProfileName = QInputDialog::getText(this, i18n("Profile already exists"), i18n("This profile name already exists. Change the name if you don't want to overwrite it."), QLineEdit::Normal, newProfileName, &ok);
        if (!ok) return false;
        if (updatedProfileName == newProfileName) {
            // remove previous profile
            profiles.removeChild(profilelist.item(existingProfileNames.indexOf(newProfileName)));
            break;
        } else {
            newProfileName = updatedProfileName;
            newprofile.setAttribute(QStringLiteral("name"), newProfileName);
604
        }
605
    }
606

607
    profiles.appendChild(newprofile);
608

609
    //QCString save = doc.toString().utf8();
610

611
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
612
        KMessageBox::sorry(this, i18n("Unable to write to file %1", dir.absoluteFilePath("customprofiles.xml")));
613
        return false;
614 615 616 617
    }
    QTextStream out(&file);
    out << doc.toString();
    if (file.error() != QFile::NoError) {
618
        KMessageBox::error(this, i18n("Cannot write to file %1", dir.absoluteFilePath("customprofiles.xml")));
619
        file.close();
620
        return false;
621 622
    }
    file.close();
623
    return true;
624 625 626 627
}

void RenderWidget::slotCopyToFavorites()
{
628 629
    QTreeWidgetItem *item = m_view.formats->currentItem();
    if (!item || !item->parent())
Laurent Montel's avatar
Laurent Montel committed
630
        return;
631
    QString currentGroup = item->parent()->text(0);
632

633 634 635
    QString params = item->data(0, ParamsRole).toString();
    QString extension = item->data(0, ExtensionRole).toString();
    QString currentProfile = item->text(0);
636
    QDomDocument doc;
637 638 639 640 641 642 643
    QDomElement profileElement = doc.createElement(QStringLiteral("profile"));
    profileElement.setAttribute(QStringLiteral("name"), currentProfile);
    profileElement.setAttribute(QStringLiteral("category"), i18nc("Category Name", "Custom"));
    profileElement.setAttribute(QStringLiteral("destinationid"), QStringLiteral("favorites"));
    profileElement.setAttribute(QStringLiteral("extension"), extension);
    profileElement.setAttribute(QStringLiteral("args"), params);
    if (params.contains(QStringLiteral("%bitrate"))) {
644
        // profile has a variable bitrate
645 646
        profileElement.setAttribute(QStringLiteral("defaultbitrate"), item->data(0, DefaultBitrateRole).toString());
        profileElement.setAttribute(QStringLiteral("bitrates"), item->data(0, BitratesRole).toStringList().join(QStringLiteral(",")));
647
    } else if (params.contains(QStringLiteral("%quality"))) {
648 649
        profileElement.setAttribute(QStringLiteral("defaultquality"), item->data(0, DefaultBitrateRole).toString());
        profileElement.setAttribute(QStringLiteral("qualities"), item->data(0, BitratesRole).toStringList().join(QStringLiteral(",")));
650
    }
651
    if (params.contains(QStringLiteral("%audiobitrate"))) {
652
        // profile has a variable bitrate
653 654
        profileElement.setAttribute(QStringLiteral("defaultaudiobitrate"), item->data(0, DefaultAudioBitrateRole).toString());
        profileElement.setAttribute(QStringLiteral("audiobitrates"), item->data(0, AudioBitratesRole).toStringList().join(QStringLiteral(",")));
655
    } else if (params.contains(QStringLiteral("%audioquality"))) {
656
        // profile has a variable bitrate
657 658 659
        profileElement.setAttribute(QStringLiteral("defaultaudioquality"), item->data(0, DefaultAudioBitrateRole).toString());
        profileElement.setAttribute(QStringLiteral("audioqualities"), item->data(0, AudioBitratesRole).toStringList().join(QStringLiteral(",")));
    }
660
    if (item->data(0, SpeedsRole).canConvert(QVariant::StringList) && item->data(0, SpeedsRole).toStringList().count()) {
661 662
        // profile has a variable speed
        profileElement.setAttribute(QStringLiteral("speeds"), item->data(0, SpeedsRole).toStringList().join(QStringLiteral(";")));
663
    }
664
    doc.appendChild(profileElement);
665
    if (saveProfile(doc.documentElement())) {
666
        parseProfiles(profileElement.attribute(QStringLiteral("name")));
667
    }
668 669
}

670 671
void RenderWidget::slotEditProfile()
{
672 673 674
    QTreeWidgetItem *item = m_view.formats->currentItem();
    if (!item || !item->parent()) return;
    QString currentGroup = item->parent()->text(0);
675

676
    QString params = item->data(0, ParamsRole).toString();
677 678

    Ui::SaveProfile_UI ui;
679
    QPointer<QDialog> d = new QDialog(this);
680
    ui.setupUi(d);
681

682
    QString customGroup = item->parent()->text(0);
Yuri Chornoivan's avatar
Yuri Chornoivan committed
683
    if (customGroup.isEmpty()) customGroup = i18nc("Group Name", "Custom");
684 685
    ui.group_name->setText(customGroup);

686 687
    ui.profile_name->setText(item->text(0));
    ui.extension->setText(item->data(0, ExtensionRole).toString());
688 689
    ui.parameters->setText(params);
    ui.profile_name->setFocus();
690 691
    if (params.contains(QStringLiteral("%bitrate")) || ui.parameters->toPlainText().contains(QStringLiteral("%quality"))) {
        if (params.contains(QStringLiteral("%quality"))) {
692 693 694 695 696 697
            ui.vbitrates_label->setText(i18n("Qualities"));
            ui.default_vbitrate_label->setText(i18n("Default quality"));
        } else {
            ui.vbitrates_label->setText(i18n("Bitrates"));
            ui.default_vbitrate_label->setText(i18n("Default bitrate"));
        }
698 699
        if ( item->data(0, BitratesRole).canConvert(QVariant::StringList) && item->data(0, BitratesRole).toStringList().count()) {
            QStringList bitrates = item->data(0, BitratesRole).toStringList();
700
            ui.vbitrates_list->setText(bitrates.join(QStringLiteral(",")));
701 702
            if (item->data(0, DefaultBitrateRole).canConvert(QVariant::String))
                ui.default_vbitrate->setValue(item->data(0, DefaultBitrateRole).toInt());
703
        }
Laurent Montel's avatar
Laurent Montel committed
704 705
    } else {
        ui.vbitrates->setHidden(true);
706
    }
Laurent Montel's avatar
Laurent Montel committed
707

708 709
    if (params.contains(QStringLiteral("%audiobitrate")) || ui.parameters->toPlainText().contains(QStringLiteral("%audioquality"))) {
        if (params.contains(QStringLiteral("%audioquality"))) {
710 711 712 713 714 715
            ui.abitrates_label->setText(i18n("Qualities"));
            ui.default_abitrate_label->setText(i18n("Default quality"));
        } else {
            ui.abitrates_label->setText(i18n("Bitrates"));
            ui.default_abitrate_label->setText(i18n("Default bitrate"));
        }
716 717
        if ( item->data(0, AudioBitratesRole).canConvert(QVariant::StringList) && item->data(0, AudioBitratesRole).toStringList().count()) {
            QStringList bitrates = item->data(0, AudioBitratesRole).toStringList();
718
            ui.abitrates_list->setText(bitrates.join(QStringLiteral(",")));
719 720
            if (item->data(0, DefaultAudioBitrateRole).canConvert(QVariant::String))
                ui.default_abitrate->setValue(item->data(0, DefaultAudioBitrateRole).toInt());
721 722 723
        }
    }
    else ui.abitrates->setHidden(true);
724 725 726 727 728 729

    if ( item->data(0, SpeedsRole).canConvert(QVariant::StringList) && item->data(0, SpeedsRole).toStringList().count()) {
        QStringList speeds = item->data(0, SpeedsRole).toStringList();
        ui.speeds_list->setText(speeds.join('\n'));
    }

730
    d->setWindowTitle(i18n("Edit Profile"));
731

732
    if (d->exec() == QDialog::Accepted) {
733
        slotDeleteProfile(false);
734
        QString exportFile = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/export/customprofiles.xml";
735 736 737 738 739
        QDomDocument doc;
        QFile file(exportFile);
        doc.setContent(&file, false);
        file.close();
        QDomElement documentElement;
740 741
        QDomElement profiles = doc.documentElement();

742
        if (profiles.isNull() || profiles.tagName() != QLatin1String("profiles")) {
743
            doc.clear();
744 745
            profiles = doc.createElement(QStringLiteral("profiles"));
            profiles.setAttribute(QStringLiteral("version"), 1);
746 747 748
            doc.appendChild(profiles);
        }

749
        int version = profiles.attribute(QStringLiteral("version"), 0).toInt();
750 751
        if (version < 1) {
            doc.clear();
752 753
            profiles = doc.createElement(QStringLiteral("profiles"));
            profiles.setAttribute(QStringLiteral("version"), 1);
754
            doc.appendChild(profiles);
755 756 757 758
        }

        QString newProfileName = ui.profile_name->text().simplified();
        QString newGroupName = ui.group_name->text().simplified();
Yuri Chornoivan's avatar
Yuri Chornoivan committed
759
        if (newGroupName.isEmpty()) newGroupName = i18nc("Group Name", "Custom");
760
        QDomNodeList profilelist = doc.elementsByTagName(QStringLiteral("profile"));
761 762 763 764
        int i = 0;
        while (!profilelist.item(i).isNull()) {
            // make sure a profile with same name doesn't exist
            documentElement = profilelist.item(i).toElement();
765
            QString profileName = documentElement.attribute(QStringLiteral("name"));
766 767 768
            if (profileName == newProfileName) {
                // a profile with that same name already exists
                bool ok;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
769
                newProfileName = QInputDialog::getText(this, i18n("Profile already exists"), i18n("This profile name already exists. Change the name if you don't want to overwrite it."), QLineEdit::Normal, newProfileName, &ok);
770 771 772
                if (!ok) return;
                if (profileName == newProfileName) {
                    profiles.removeChild(profilelist.item(i));
773 774 775
                    break;
                }
            }
776
            ++i;
777
        }
778

779 780 781 782
        QDomElement profileElement = doc.createElement(QStringLiteral("profile"));
        profileElement.setAttribute(QStringLiteral("name"), newProfileName);
        profileElement.setAttribute(QStringLiteral("category"), newGroupName);
        profileElement.setAttribute(QStringLiteral("extension"), ui.extension->text().simplified());
783
        QString args = ui.parameters->toPlainText().simplified();
784 785
        profileElement.setAttribute(QStringLiteral("args"), args);
        if (args.contains(QStringLiteral("%bitrate"))) {
786
            // profile has a variable bitrate
787 788 789 790 791
            profileElement.setAttribute(QStringLiteral("defaultbitrate"), QString::number(ui.default_vbitrate->value()));
            profileElement.setAttribute(QStringLiteral("bitrates"), ui.vbitrates_list->text());
        } else if(args.contains(QStringLiteral("%quality"))) {
            profileElement.setAttribute(QStringLiteral("defaultquality"), QString::number(ui.default_vbitrate->value()));
            profileElement.setAttribute(QStringLiteral("qualities"), ui.vbitrates_list->text());
792
        }
793
        if (args.contains(QStringLiteral("%audiobitrate"))) {
794
            // profile has a variable bitrate
795 796 797 798 799
            profileElement.setAttribute(QStringLiteral("defaultaudiobitrate"), QString::number(ui.default_abitrate->value()));
            profileElement.setAttribute(QStringLiteral("audiobitrates"), ui.abitrates_list->text());
        } else if (args.contains(QStringLiteral("%audioquality"))) {
            profileElement.setAttribute(QStringLiteral("defaultaudioquality"), QString::number(ui.default_abitrate->value()));
            profileElement.setAttribute(QStringLiteral("audioqualities"), ui.abitrates_list->text());
800 801
        }

802
        if (!ui.speeds_list->toPlainText().isEmpty()) {
803 804 805
            // profile has a variable speed
            profileElement.setAttribute(QStringLiteral("speeds"), ui.speeds_list->toPlainText().replace('\n', ';').simplified());
        }
806

807
        profiles.appendChild(profileElement);
808 809

        //QCString save = doc.toString().utf8();
810
        delete d;
811
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
812
            KMessageBox::error(this, i18n("Cannot write to file %1", exportFile));
813 814 815 816
            return;
        }
        QTextStream out(&file);
        out << doc.toString();
817 818 819 820 821
        if (file.error() != QFile::NoError) {
            KMessageBox::error(this, i18n("Cannot write to file %1", exportFile));
            file.close();
            return;
        }
822
        file.close();
823
        parseProfiles();
824
    } else delete d;
825 826
}

827 828
void RenderWidget::slotDeleteProfile(bool refresh)
{
829 830
    //TODO: delete a profile installed by KNewStuff the easy way
    /*
831
    QString edit = m_view.formats->currentItem()->data(EditableRole).toString();
832
    if (!edit.endsWith(QLatin1String("customprofiles.xml"))) {
833
        // This is a KNewStuff installed file, process through KNS
834 835 836 837 838 839
        KNS::Engine engine(0);
        if (engine.init("kdenlive_render.knsrc")) {
            KNS::Entry::List entries;
        }
        return;
    }*/
840 841 842 843
    QTreeWidgetItem *item = m_view.formats->currentItem();
    if (!item || !item->parent()) return;
    QString currentProfile = item->text(0);
    QString currentGroup = item->parent()->text(0);
844

845
    QString exportFile = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/export/customprofiles.xml";
846 847 848 849 850 851
    QDomDocument doc;
    QFile file(exportFile);
    doc.setContent(&file, false);
    file.close();

    QDomElement documentElement;
852
    QDomNodeList profiles = doc.elementsByTagName(QStringLiteral("profile"));
853
    int i = 0;
854 855 856
    QString profileName;
    while (!profiles.item(i).isNull()) {
        documentElement = profiles.item(i).toElement();
857
        profileName = documentElement.attribute(QStringLiteral("name"));
858
        if (profileName == currentProfile) {
859