renderwidget.cpp 147 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
#include "renderwidget.h"
Nicolas Carion's avatar
Nicolas Carion committed
21
#include "core.h"
22
#include "dialogs/profilesdialog.h"
23
#include "kdenlivesettings.h"
24 25
#include "profiles/profilemodel.hpp"
#include "profiles/profilerepository.hpp"
26
#include "timecode.h"
27 28 29 30 31
#include "monitor/monitor.h"
#include "project/projectmanager.h"
#include "bin/projectitemmodel.h"
#include "doc/kdenlivedoc.h"
#include "xml/xml.hpp"
32
#include "ui_saveprofile_ui.h"
33

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
34

35
#include "klocalizedstring.h"
36
#include <KColorScheme>
37
#include <KIO/DesktopExecParser>
38 39 40 41
#include <KMessageBox>
#include <KMimeTypeTrader>
#include <KNotification>
#include <KRun>
42
#include <kio_version.h>
Nicolas Carion's avatar
Nicolas Carion committed
43
#include <knotifications_version.h>
44
#include <kns3/downloaddialog.h>
45

Laurent Montel's avatar
Laurent Montel committed
46
#include "kdenlive_debug.h"
47 48
#include <QDBusConnectionInterface>
#include <QDir>
49
#include <QDomDocument>
50
#include <QTemporaryFile>
51 52
#include <QHeaderView>
#include <QInputDialog>
53 54
#include <QKeyEvent>
#include <QMimeDatabase>
55
#include <QProcess>
56
#include <QStandardPaths>
57 58 59
#include <QThread>
#include <QTimer>
#include <QTreeWidgetItem>
60
#include <QJsonObject>
61
#include <QJsonArray>
62 63
#include <qglobal.h>
#include <qstring.h>
64
#include <QFileIconProvider>
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
65

66 67 68 69 70
#ifdef KF5_USE_PURPOSE
#include <Purpose/AlternativesModel>
#include <PurposeWidgets/Menu>
#endif

Vincent Pinon's avatar
Vincent Pinon committed
71
#include <locale>
72 73 74
#ifdef Q_OS_MAC
#include <xlocale.h>
#endif
75

76
// Render profiles roles
77 78 79 80 81 82 83 84 85 86 87 88 89
enum {
    GroupRole = Qt::UserRole,
    ExtensionRole,
    StandardRole,
    RenderRole,
    ParamsRole,
    EditableRole,
    ExtraRole,
    BitratesRole,
    DefaultBitrateRole,
    AudioBitratesRole,
    DefaultAudioBitrateRole,
    SpeedsRole,
90
    FieldRole,
91 92
    ErrorRole
};
93

94 95 96
// Render job roles
const int ParametersRole = Qt::UserRole + 1;
const int TimeRole = Qt::UserRole + 2;
97 98
const int ProgressRole = Qt::UserRole + 3;
const int ExtraInfoRole = Qt::UserRole + 5;
99

100
// Running job status
101
enum JOBSTATUS { WAITINGJOB = 0, STARTINGJOB, RUNNINGJOB, FINISHEDJOB, FAILEDJOB, ABORTEDJOB };
102

103 104 105
static QStringList acodecsList;
static QStringList vcodecsList;
static QStringList supportedFormats;
106

107 108 109
RenderJobItem::RenderJobItem(QTreeWidget *parent, const QStringList &strings, int type)
    : QTreeWidgetItem(parent, strings, type)
    , m_status(-1)
110
{
111
    setSizeHint(1, QSize(parent->columnWidth(1), parent->fontMetrics().height() * 3));
112 113 114 115 116
    setStatus(WAITINGJOB);
}

void RenderJobItem::setStatus(int status)
{
117
    if (m_status == status) {
Laurent Montel's avatar
Laurent Montel committed
118
        return;
119
    }
120 121
    m_status = status;
    switch (status) {
122
    case WAITINGJOB:
123
        setIcon(0, QIcon::fromTheme(QStringLiteral("media-playback-pause")));
124 125 126 127
        setData(1, Qt::UserRole, i18n("Waiting..."));
        break;
    case FINISHEDJOB:
        setData(1, Qt::UserRole, i18n("Rendering finished"));
128
        setIcon(0, QIcon::fromTheme(QStringLiteral("dialog-ok")));
129 130 131 132
        setData(1, ProgressRole, 100);
        break;
    case FAILEDJOB:
        setData(1, Qt::UserRole, i18n("Rendering crashed"));
133
        setIcon(0, QIcon::fromTheme(QStringLiteral("dialog-close")));
134 135 136 137
        setData(1, ProgressRole, 100);
        break;
    case ABORTEDJOB:
        setData(1, Qt::UserRole, i18n("Rendering aborted"));
138
        setIcon(0, QIcon::fromTheme(QStringLiteral("dialog-cancel")));
139 140 141
        setData(1, ProgressRole, 100);
    default:
        break;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    }
}

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

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

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

160
RenderWidget::RenderWidget(bool enableProxy, QWidget *parent)
161 162
    : QDialog(parent)
    , m_blockProcessing(false)
163
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
164
    m_view.setupUi(this);
165 166
    int size = style()->pixelMetric(QStyle::PM_SmallIconSize);
    QSize iconSize(size, size);
167

168
    setWindowTitle(i18n("Rendering"));
169 170 171 172
    m_view.buttonDelete->setIconSize(iconSize);
    m_view.buttonEdit->setIconSize(iconSize);
    m_view.buttonSave->setIconSize(iconSize);
    m_view.buttonFavorite->setIconSize(iconSize);
173
    m_view.buttonDownload->setIconSize(iconSize);
174

175
    m_view.buttonDelete->setIcon(QIcon::fromTheme(QStringLiteral("trash-empty")));
176 177 178
    m_view.buttonDelete->setToolTip(i18n("Delete profile"));
    m_view.buttonDelete->setEnabled(false);

179
    m_view.buttonEdit->setIcon(QIcon::fromTheme(QStringLiteral("document-edit")));
180 181 182
    m_view.buttonEdit->setToolTip(i18n("Edit profile"));
    m_view.buttonEdit->setEnabled(false);

183
    m_view.buttonSave->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
184 185
    m_view.buttonSave->setToolTip(i18n("Create new profile"));

186
    m_view.hide_log->setIcon(QIcon::fromTheme(QStringLiteral("go-down")));
187

188
    m_view.buttonFavorite->setIcon(QIcon::fromTheme(QStringLiteral("favorite")));
189
    m_view.buttonFavorite->setToolTip(i18n("Copy profile to favorites"));
190

191 192 193
    m_view.buttonDownload->setIcon(QIcon::fromTheme(QStringLiteral("edit-download")));
    m_view.buttonDownload->setToolTip(i18n("Download New Render Profiles..."));

194
    m_view.out_file->button()->setToolTip(i18n("Select output destination"));
195
    m_view.advanced_params->setMaximumHeight(QFontMetrics(font()).lineSpacing() * 5);
196

197
    m_view.optionsGroup->setVisible(m_view.options->isChecked());
Laurent Montel's avatar
Laurent Montel committed
198
    connect(m_view.options, &QAbstractButton::toggled, m_view.optionsGroup, &QWidget::setVisible);
199
    m_view.videoLabel->setVisible(m_view.options->isChecked());
Laurent Montel's avatar
Laurent Montel committed
200
    connect(m_view.options, &QAbstractButton::toggled, m_view.videoLabel, &QWidget::setVisible);
201
    m_view.video->setVisible(m_view.options->isChecked());
Laurent Montel's avatar
Laurent Montel committed
202
    connect(m_view.options, &QAbstractButton::toggled, m_view.video, &QWidget::setVisible);
203
    m_view.audioLabel->setVisible(m_view.options->isChecked());
Laurent Montel's avatar
Laurent Montel committed
204
    connect(m_view.options, &QAbstractButton::toggled, m_view.audioLabel, &QWidget::setVisible);
205
    m_view.audio->setVisible(m_view.options->isChecked());
Laurent Montel's avatar
Laurent Montel committed
206 207
    connect(m_view.options, &QAbstractButton::toggled, m_view.audio, &QWidget::setVisible);
    connect(m_view.quality, &QAbstractSlider::valueChanged, this, &RenderWidget::adjustAVQualities);
208
    connect(m_view.video, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &RenderWidget::adjustQuality);
Laurent Montel's avatar
Laurent Montel committed
209
    connect(m_view.speed, &QAbstractSlider::valueChanged, this, &RenderWidget::adjustSpeed);
210

211 212
    m_view.buttonRender->setEnabled(false);
    m_view.buttonGenerateScript->setEnabled(false);
213
    setRescaleEnabled(false);
214 215 216 217 218 219 220
    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);
221
    m_view.proxy_render->setHidden(!enableProxy);
222
    connect(m_view.proxy_render, &QCheckBox::toggled, this, &RenderWidget::slotProxyWarn);
223
    KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
224
    QColor bg = scheme.background(KColorScheme::NegativeBackground).color();
225 226
    m_view.errorBox->setStyleSheet(
        QStringLiteral("QGroupBox { background-color: rgb(%1, %2, %3); border-radius: 5px;}; ").arg(bg.red()).arg(bg.green()).arg(bg.blue()));
227
    int height = QFontInfo(font()).pixelSize();
228
    m_view.errorIcon->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-warning")).pixmap(height, height));
229 230 231
    m_view.errorBox->setHidden(true);

    m_infoMessage = new KMessageWidget;
232
    m_view.info->addWidget(m_infoMessage);
233 234 235
    m_infoMessage->setCloseButtonVisible(false);
    m_infoMessage->hide();

236 237 238 239 240
    m_jobInfoMessage = new KMessageWidget;
    m_view.jobInfo->addWidget(m_jobInfoMessage);
    m_jobInfoMessage->setCloseButtonVisible(false);
    m_jobInfoMessage->hide();

241
    m_view.encoder_threads->setMinimum(0);
242
    m_view.encoder_threads->setMaximum(QThread::idealThreadCount());
243
    m_view.encoder_threads->setToolTip(i18n("Encoding threads (0 is automatic)"));
244
    m_view.encoder_threads->setValue(KdenliveSettings::encodethreads());
245
    connect(m_view.encoder_threads, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &RenderWidget::slotUpdateEncodeThreads);
246

247
    m_view.rescale_keep->setChecked(KdenliveSettings::rescalekeepratio());
248 249
    connect(m_view.rescale_width, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &RenderWidget::slotUpdateRescaleWidth);
    connect(m_view.rescale_height, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &RenderWidget::slotUpdateRescaleHeight);
250
    m_view.rescale_keep->setIcon(QIcon::fromTheme(QStringLiteral("edit-link")));
251
    m_view.rescale_keep->setToolTip(i18n("Preserve aspect ratio"));
Laurent Montel's avatar
Laurent Montel committed
252
    connect(m_view.rescale_keep, &QAbstractButton::clicked, this, &RenderWidget::slotSwitchAspectRatio);
253

254
    connect(m_view.buttonRender, SIGNAL(clicked()), this, SLOT(slotPrepareExport()));
Laurent Montel's avatar
Laurent Montel committed
255
    connect(m_view.buttonGenerateScript, &QAbstractButton::clicked, this, &RenderWidget::slotGenerateScript);
256

257 258 259 260
    m_view.abort_job->setEnabled(false);
    m_view.start_script->setEnabled(false);
    m_view.delete_script->setEnabled(false);

Laurent Montel's avatar
Laurent Montel committed
261
    connect(m_view.export_audio, &QCheckBox::stateChanged, this, &RenderWidget::slotUpdateAudioLabel);
262 263
    m_view.export_audio->setCheckState(Qt::PartiallyChecked);

264
    checkCodecs();
265
    parseProfiles();
266
    parseScriptFiles();
267 268
    m_view.running_jobs->setUniformRowHeights(false);
    m_view.scripts_list->setUniformRowHeights(false);
Laurent Montel's avatar
Laurent Montel committed
269 270 271 272 273 274 275 276 277 278
    connect(m_view.start_script, &QAbstractButton::clicked, this, &RenderWidget::slotStartScript);
    connect(m_view.delete_script, &QAbstractButton::clicked, this, &RenderWidget::slotDeleteScript);
    connect(m_view.scripts_list, &QTreeWidget::itemSelectionChanged, this, &RenderWidget::slotCheckScript);
    connect(m_view.running_jobs, &QTreeWidget::itemSelectionChanged, this, &RenderWidget::slotCheckJob);
    connect(m_view.running_jobs, &QTreeWidget::itemDoubleClicked, this, &RenderWidget::slotPlayRendering);

    connect(m_view.buttonSave, &QAbstractButton::clicked, this, &RenderWidget::slotSaveProfile);
    connect(m_view.buttonEdit, &QAbstractButton::clicked, this, &RenderWidget::slotEditProfile);
    connect(m_view.buttonDelete, &QAbstractButton::clicked, this, &RenderWidget::slotDeleteProfile);
    connect(m_view.buttonFavorite, &QAbstractButton::clicked, this, &RenderWidget::slotCopyToFavorites);
279
    connect(m_view.buttonDownload, &QAbstractButton::clicked, this, &RenderWidget::slotDownloadNewRenderProfiles);
Laurent Montel's avatar
Laurent Montel committed
280 281 282 283 284 285 286 287 288 289

    connect(m_view.abort_job, &QAbstractButton::clicked, this, &RenderWidget::slotAbortCurrentJob);
    connect(m_view.start_job, &QAbstractButton::clicked, this, &RenderWidget::slotStartCurrentJob);
    connect(m_view.clean_up, &QAbstractButton::clicked, this, &RenderWidget::slotCLeanUpJobs);
    connect(m_view.hide_log, &QAbstractButton::clicked, this, &RenderWidget::slotHideLog);

    connect(m_view.buttonClose, &QAbstractButton::clicked, this, &QWidget::hide);
    connect(m_view.buttonClose2, &QAbstractButton::clicked, this, &QWidget::hide);
    connect(m_view.buttonClose3, &QAbstractButton::clicked, this, &QWidget::hide);
    connect(m_view.rescale, &QAbstractButton::toggled, this, &RenderWidget::setRescaleEnabled);
290 291
    connect(m_view.out_file, &KUrlRequester::textChanged, this, static_cast<void(RenderWidget::*)()>(&RenderWidget::slotUpdateButtons));
    connect(m_view.out_file, &KUrlRequester::urlSelected, this, static_cast<void(RenderWidget::*)(const QUrl&)>(&RenderWidget::slotUpdateButtons));
292

Laurent Montel's avatar
Laurent Montel committed
293 294
    connect(m_view.formats, &QTreeWidget::currentItemChanged, this, &RenderWidget::refreshParams);
    connect(m_view.formats, &QTreeWidget::itemDoubleClicked, this, &RenderWidget::slotEditItem);
295

Laurent Montel's avatar
Laurent Montel committed
296 297 298
    connect(m_view.render_guide, &QAbstractButton::clicked, this, &RenderWidget::slotUpdateGuideBox);
    connect(m_view.render_zone, &QAbstractButton::clicked, this, &RenderWidget::slotUpdateGuideBox);
    connect(m_view.render_full, &QAbstractButton::clicked, this, &RenderWidget::slotUpdateGuideBox);
299

300 301
    connect(m_view.guide_end, static_cast<void(KComboBox::*)(int)>(&KComboBox::activated), this, &RenderWidget::slotCheckStartGuidePosition);
    connect(m_view.guide_start, static_cast<void(KComboBox::*)(int)>(&KComboBox::activated), this, &RenderWidget::slotCheckEndGuidePosition);
302

Laurent Montel's avatar
Laurent Montel committed
303
    connect(m_view.tc_overlay, &QAbstractButton::toggled, m_view.tc_type, &QWidget::setEnabled);
304

305 306
    // m_view.splitter->setStretchFactor(1, 5);
    // m_view.splitter->setStretchFactor(0, 2);
307

308
    m_view.out_file->setMode(KFile::File);
Nicolas Carion's avatar
Nicolas Carion committed
309
#if KIO_VERSION >= QT_VERSION_CHECK(5, 33, 0)
310
    m_view.out_file->setAcceptMode(QFileDialog::AcceptSave);
311
#elif !defined(KIOWIDGETS_DEPRECATED)
312
    m_view.out_file->fileDialog()->setAcceptMode(QFileDialog::AcceptSave);
313 314
#endif

315
    m_view.out_file->setFocusPolicy(Qt::ClickFocus);
316

317
    m_jobsDelegate = new RenderViewDelegate(this);
318
    m_view.running_jobs->setHeaderLabels(QStringList() << QString() << i18n("File"));
319
    m_view.running_jobs->setItemDelegate(m_jobsDelegate);
320

321
    QHeaderView *header = m_view.running_jobs->header();
322
    header->setSectionResizeMode(0, QHeaderView::Fixed);
323
    header->resizeSection(0, size + 4);
324
    header->setSectionResizeMode(1, QHeaderView::Interactive);
325

326
    m_view.scripts_list->setHeaderLabels(QStringList() << QString() << i18n("Stored Playlists"));
327 328
    m_scriptsDelegate = new RenderViewDelegate(this);
    m_view.scripts_list->setItemDelegate(m_scriptsDelegate);
329
    header = m_view.scripts_list->header();
330
    header->setSectionResizeMode(0, QHeaderView::Fixed);
331
    header->resizeSection(0, size + 4);
332

333
// Find path for Kdenlive renderer
334 335 336
#ifdef Q_OS_WIN
    m_renderer = QCoreApplication::applicationDirPath() + QStringLiteral("/kdenlive_render.exe");
#else
337
    m_renderer = QCoreApplication::applicationDirPath() + QStringLiteral("/kdenlive_render");
338
#endif
339
    if (!QFile::exists(m_renderer)) {
340
        m_renderer = QStandardPaths::findExecutable(QStringLiteral("kdenlive_render"));
341
        if (m_renderer.isEmpty()) {
342
            m_renderer = QStringLiteral("kdenlive_render");
343
        }
344 345
    }

346
    QDBusConnectionInterface *interface = QDBusConnection::sessionBus().interface();
347 348
    if ((interface == nullptr) ||
        (!interface->isServiceRegistered(QStringLiteral("org.kde.ksmserver")) && !interface->isServiceRegistered(QStringLiteral("org.gnome.SessionManager")))) {
349
        m_view.shutdown->setEnabled(false);
350
    }
351 352 353 354

#ifdef KF5_USE_PURPOSE
    m_shareMenu = new Purpose::Menu();
    m_view.shareButton->setMenu(m_shareMenu);
355
    m_view.shareButton->setIcon( QIcon::fromTheme(QStringLiteral("document-share")));
356
    connect(m_shareMenu, &Purpose::Menu::finished, this, &RenderWidget::slotShareActionFinished);
357 358 359
#else
    m_view.shareButton->setEnabled(false);
#endif
360 361 362 363
    m_view.parallel_process->setChecked(KdenliveSettings::parallelrender());
    connect(m_view.parallel_process, &QCheckBox::stateChanged, [this] (int state) {
        KdenliveSettings::setParallelrender(state == Qt::Checked);
    });
364 365 366 367
    m_view.field_order->setEnabled(false);
    connect(m_view.scanning_list, QOverload<int>::of(&QComboBox::currentIndexChanged), [this] (int index) {
        m_view.field_order->setEnabled(index == 2);
    });
368
    refreshView();
369
    focusFirstVisibleItem();
370
    adjustSize();
371 372
}

373 374
void RenderWidget::slotShareActionFinished(const QJsonObject &output, int error, const QString &message)
{
375
#ifdef KF5_USE_PURPOSE
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    m_jobInfoMessage->hide();
    if (error) {
        KMessageBox::error(this, i18n("There was a problem sharing the document: %1", message),
                           i18n("Share"));
    } else {
        const QString url = output["url"].toString();
        if (url.isEmpty()) {
            m_jobInfoMessage->setMessageType(KMessageWidget::Positive);
            m_jobInfoMessage->setText(i18n("Document shared successfully"));
            m_jobInfoMessage->show();
        } else {
            KMessageBox::information(this, i18n("You can find the shared document at: <a href=\"%1\">%1</a>", url),
                                     i18n("Share"), QString(),
                                     KMessageBox::Notify | KMessageBox::AllowLink);
        }
    }
#endif
393
}
394

395 396 397 398
QSize RenderWidget::sizeHint() const
{
    // Make sure the widget has minimum size on opening
    return QSize(200, 200);
399 400
}

401 402 403 404 405 406 407 408
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;
409
    delete m_infoMessage;
410
    delete m_jobInfoMessage;
411 412
}

413
void RenderWidget::slotEditItem(QTreeWidgetItem *item)
414
{
Laurent Montel's avatar
Laurent Montel committed
415
    if (item->parent() == nullptr) {
416 417 418
        // This is a top level item - group - don't edit
        return;
    }
419
    const QString edit = item->data(0, EditableRole).toString();
420
    if (edit.isEmpty() || !edit.endsWith(QLatin1String("customprofiles.xml"))) {
Laurent Montel's avatar
Laurent Montel committed
421
        slotSaveProfile();
422 423 424
    } else {
        slotEditProfile();
    }
425 426
}

427 428
void RenderWidget::showInfoPanel()
{
429 430 431 432 433 434 435
    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
436 437
}

438
void RenderWidget::updateDocumentPath()
439
{
440 441
    if (m_view.out_file->url().isEmpty()) {
        return;
442
    }
443 444
    const QString fileName = m_view.out_file->url().fileName();
    m_view.out_file->setUrl(QUrl::fromLocalFile(QDir(pCore->currentDoc()->projectDataFolder()).absoluteFilePath(fileName)));
445
    parseScriptFiles();
446 447
}

448 449
void RenderWidget::slotUpdateGuideBox()
{
450 451 452
    m_view.guides_box->setVisible(m_view.render_guide->isChecked());
}

453 454
void RenderWidget::slotCheckStartGuidePosition()
{
455
    if (m_view.guide_start->currentIndex() > m_view.guide_end->currentIndex()) {
456
        m_view.guide_start->setCurrentIndex(m_view.guide_end->currentIndex());
457
    }
458 459
}

460 461
void RenderWidget::slotCheckEndGuidePosition()
{
462
    if (m_view.guide_end->currentIndex() < m_view.guide_start->currentIndex()) {
463
        m_view.guide_end->setCurrentIndex(m_view.guide_start->currentIndex());
464
    }
465 466
}

467
void RenderWidget::setGuides(const QList<CommentedTime> &guidesList, double duration)
468
{
469 470
    m_view.guide_start->clear();
    m_view.guide_end->clear();
471
    if (!guidesList.isEmpty()) {
472
        m_view.guide_start->addItem(i18n("Beginning"), "0");
473
        m_view.render_guide->setEnabled(true);
474 475 476 477 478
        m_view.create_chapter->setEnabled(true);
    } else {
        m_view.render_guide->setEnabled(false);
        m_view.create_chapter->setEnabled(false);
    }
479
    double fps = pCore->getCurrentProfile()->fps();
480 481 482
    for (int i = 0; i < guidesList.count(); i++) {
        CommentedTime c = guidesList.at(i);
        GenTime pos = c.time();
483
        const QString guidePos = Timecode::getStringTimecode(pos.frames(fps), fps);
484 485
        m_view.guide_start->addItem(c.comment() + QLatin1Char('/') + guidePos, pos.seconds());
        m_view.guide_end->addItem(c.comment() + QLatin1Char('/') + guidePos, pos.seconds());
486
    }
487
    if (!guidesList.isEmpty()) {
488
        m_view.guide_end->addItem(i18n("End"), QString::number(duration));
489
    }
490 491
}

492
/**
493 494 495
 * Will be called when the user selects an output file via the file dialog.
 * File extension will be added automatically.
 */
496
void RenderWidget::slotUpdateButtons(const QUrl &url)
497
{
498 499 500 501
    if (m_view.out_file->url().isEmpty()) {
        m_view.buttonGenerateScript->setEnabled(false);
        m_view.buttonRender->setEnabled(false);
    } else {
502 503
        updateButtons(); // This also checks whether the selected format is available
    }
504
    if (url.isValid()) {
505
        QTreeWidgetItem *item = m_view.formats->currentItem();
Nicolas Carion's avatar
Nicolas Carion committed
506
        if ((item == nullptr) || (item->parent() == nullptr)) { // categories have no parent
507 508
            m_view.buttonRender->setEnabled(false);
            m_view.buttonGenerateScript->setEnabled(false);
509 510
            return;
        }
511
        const QString extension = item->data(0, ExtensionRole).toString();
512
        m_view.out_file->setUrl(filenameWithExtension(url, extension));
513 514 515
    }
}

516 517 518 519
/**
 * 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!
 */
520 521
void RenderWidget::slotUpdateButtons()
{
522 523 524 525 526 527
    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
528 529
}

530 531
void RenderWidget::slotSaveProfile()
{
532
    Ui::SaveProfile_UI ui;
533
    QPointer<QDialog> d = new QDialog(this);
534
    ui.setupUi(d);
535

536
    QString customGroup;
537
    QStringList arguments = m_view.advanced_params->toPlainText().split(' ', QString::SkipEmptyParts);
538
    if (!arguments.isEmpty()) {
539
        ui.parameters->setText(arguments.join(QLatin1Char(' ')));
540
    }
541
    ui.profile_name->setFocus();
542
    QTreeWidgetItem *item = m_view.formats->currentItem();
543
    if ((item != nullptr) && (item->parent() != nullptr)) { // not a category
544
        // Duplicate current item settings
545 546
        customGroup = item->parent()->text(0);
        ui.extension->setText(item->data(0, ExtensionRole).toString());
547 548
        if (ui.parameters->toPlainText().contains(QStringLiteral("%bitrate")) || ui.parameters->toPlainText().contains(QStringLiteral("%quality"))) {
            if (ui.parameters->toPlainText().contains(QStringLiteral("%quality"))) {
549 550 551 552 553 554
                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"));
            }
Nicolas Carion's avatar
Nicolas Carion committed
555
            if (item->data(0, BitratesRole).canConvert(QVariant::StringList) && (item->data(0, BitratesRole).toStringList().count() != 0)) {
556
                QStringList bitrates = item->data(0, BitratesRole).toStringList();
557
                ui.vbitrates_list->setText(bitrates.join(QLatin1Char(',')));
558
                if (item->data(0, DefaultBitrateRole).canConvert(QVariant::String)) {
559
                    ui.default_vbitrate->setValue(item->data(0, DefaultBitrateRole).toInt());
560
                }
561
            }
562 563
        } else {
            ui.vbitrates->setHidden(true);
564
        }
565 566
        if (ui.parameters->toPlainText().contains(QStringLiteral("%audiobitrate")) || ui.parameters->toPlainText().contains(QStringLiteral("%audioquality"))) {
            if (ui.parameters->toPlainText().contains(QStringLiteral("%audioquality"))) {
567 568 569 570 571 572
                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"));
            }
573 574
            if ((item != nullptr) && item->data(0, AudioBitratesRole).canConvert(QVariant::StringList) &&
                (item->data(0, AudioBitratesRole).toStringList().count() != 0)) {
575
                QStringList bitrates = item->data(0, AudioBitratesRole).toStringList();
576
                ui.abitrates_list->setText(bitrates.join(QLatin1Char(',')));
577
                if (item->data(0, DefaultAudioBitrateRole).canConvert(QVariant::String)) {
578
                    ui.default_abitrate->setValue(item->data(0, DefaultAudioBitrateRole).toInt());
579
                }
580
            }
581 582
        } else {
            ui.abitrates->setHidden(true);
583
        }
584

Nicolas Carion's avatar
Nicolas Carion committed
585
        if (item->data(0, SpeedsRole).canConvert(QVariant::StringList) && (item->data(0, SpeedsRole).toStringList().count() != 0)) {
586 587 588
            QStringList speeds = item->data(0, SpeedsRole).toStringList();
            ui.speeds_list->setText(speeds.join('\n'));
        }
589
    }
590

591 592 593
    if (customGroup.isEmpty()) {
        customGroup = i18nc("Group Name", "Custom");
    }
594
    ui.group_name->setText(customGroup);
595

596
    if (d->exec() == QDialog::Accepted && !ui.profile_name->text().simplified().isEmpty()) {
597 598
        QString newProfileName = ui.profile_name->text().simplified();
        QString newGroupName = ui.group_name->text().simplified();
599 600 601
        if (newGroupName.isEmpty()) {
            newGroupName = i18nc("Group Name", "Custom");
        }
602 603

        QDomDocument doc;
604 605 606 607
        QDomElement profileElement = doc.createElement(QStringLiteral("profile"));
        profileElement.setAttribute(QStringLiteral("name"), newProfileName);
        profileElement.setAttribute(QStringLiteral("category"), newGroupName);
        profileElement.setAttribute(QStringLiteral("extension"), ui.extension->text().simplified());
608
        QString args = ui.parameters->toPlainText().simplified();
609 610
        profileElement.setAttribute(QStringLiteral("args"), args);
        if (args.contains(QStringLiteral("%bitrate"))) {
611
            // profile has a variable bitrate
612 613 614 615 616
            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());
617
        }
618

619
        if (args.contains(QStringLiteral("%audiobitrate"))) {
620
            // profile has a variable bitrate
621 622 623
            profileElement.setAttribute(QStringLiteral("defaultaudiobitrate"), QString::number(ui.default_abitrate->value()));
            profileElement.setAttribute(QStringLiteral("audiobitrates"), ui.abitrates_list->text());
        } else if (args.contains(QStringLiteral("%audioquality"))) {
624
            // profile has a variable bitrate
625 626
            profileElement.setAttribute(QStringLiteral("defaultaudioquality"), QString::number(ui.default_abitrate->value()));
            profileElement.setAttribute(QStringLiteral("audioqualities"), ui.abitrates_list->text());
627
        }
628 629 630
        QString speeds_list_str = ui.speeds_list->toPlainText();
        if (!speeds_list_str.isEmpty()) {
            profileElement.setAttribute(QStringLiteral("speeds"), speeds_list_str.replace('\n', ';').simplified());
631
        }
632

633 634 635
        doc.appendChild(profileElement);
        saveProfile(doc.documentElement());

636
        parseProfiles();
637 638 639 640
    }
    delete d;
}

641
bool RenderWidget::saveProfile(QDomElement newprofile)
642
{
643
    QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/export/"));
644
    if (!dir.exists()) {
645
        dir.mkpath(QStringLiteral("."));
646
    }
647
    QDomDocument doc;
648
    QFile file(dir.absoluteFilePath(QStringLiteral("customprofiles.xml")));
649 650 651 652
    doc.setContent(&file, false);
    file.close();
    QDomElement documentElement;
    QDomElement profiles = doc.documentElement();
653
    if (profiles.isNull() || profiles.tagName() != QLatin1String("profiles")) {
654
        doc.clear();
655 656
        profiles = doc.createElement(QStringLiteral("profiles"));
        profiles.setAttribute(QStringLiteral("version"), 1);
657 658
        doc.appendChild(profiles);
    }
Laurent Montel's avatar
Laurent Montel committed
659
    int version = profiles.attribute(QStringLiteral("version"), nullptr).toInt();
660 661
    if (version < 1) {
        doc.clear();
662 663
        profiles = doc.createElement(QStringLiteral("profiles"));
        profiles.setAttribute(QStringLiteral("version"), 1);
664 665
        doc.appendChild(profiles);
    }
666

667
    QDomNodeList profilelist = doc.elementsByTagName(QStringLiteral("profile"));
668 669 670
    QString newProfileName = newprofile.attribute(QStringLiteral("name"));
    // Check existing profiles
    QStringList existingProfileNames;
671 672 673
    int i = 0;
    while (!profilelist.item(i).isNull()) {
        documentElement = profilelist.item(i).toElement();
674
        QString profileName = documentElement.attribute(QStringLiteral("name"));
675 676 677 678 679 680
        existingProfileNames << profileName;
        i++;
    }
    // Check if a profile with that same name already exists
    bool ok;
    while (existingProfileNames.contains(newProfileName)) {
681 682 683
        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);
684 685 686
        if (!ok) {
            return false;
        }
687 688 689 690 691 692 693
        if (updatedProfileName == newProfileName) {
            // remove previous profile
            profiles.removeChild(profilelist.item(existingProfileNames.indexOf(newProfileName)));
            break;
        } else {
            newProfileName = updatedProfileName;
            newprofile.setAttribute(QStringLiteral("name"), newProfileName);
694
        }
695
    }
696

697
    profiles.appendChild(newprofile);
698

699
    // QCString save = doc.toString().utf8();
700

701
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
702
        KMessageBox::sorry(this, i18n("Unable to write to file %1", dir.absoluteFilePath("customprofiles.xml")));
703
        return false;
704 705 706 707
    }
    QTextStream out(&file);
    out << doc.toString();
    if (file.error() != QFile::NoError) {
708
        KMessageBox::error(this, i18n("Cannot write to file %1", dir.absoluteFilePath("customprofiles.xml")));
709
        file.close();
710
        return false;
711 712
    }
    file.close();
713
    return true;
714 715 716 717
}

void RenderWidget::slotCopyToFavorites()
{
718
    QTreeWidgetItem *item = m_view.formats->currentItem();
Nicolas Carion's avatar
Nicolas Carion committed
719
    if ((item == nullptr) || (item->parent() == nullptr)) {
Laurent Montel's avatar
Laurent Montel committed
720
        return;
721
    }
722

723 724 725
    QString params = item->data(0, ParamsRole).toString();
    QString extension = item->data(0, ExtensionRole).toString();
    QString currentProfile = item->text(0);
726
    QDomDocument doc;
727 728 729 730 731 732 733
    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"))) {
734
        // profile has a variable bitrate
735
        profileElement.setAttribute(QStringLiteral("defaultbitrate"), item->data(0, DefaultBitrateRole).toString());
736
        profileElement.setAttribute(QStringLiteral("bitrates"), item->data(0, BitratesRole).toStringList().join(QLatin1Char(',')));
737
    } else if (params.contains(QStringLiteral("%quality"))) {
738
        profileElement.setAttribute(QStringLiteral("defaultquality"), item->data(0, DefaultBitrateRole).toString());
739
        profileElement.setAttribute(QStringLiteral("qualities"), item->data(0, BitratesRole).toStringList().join(QLatin1Char(',')));
740
    }
741
    if (params.contains(QStringLiteral("%audiobitrate"))) {
742
        // profile has a variable bitrate
743
        profileElement.setAttribute(QStringLiteral("defaultaudiobitrate"), item->data(0, DefaultAudioBitrateRole).toString());
744
        profileElement.setAttribute(QStringLiteral("audiobitrates"), item->data(0, AudioBitratesRole).toStringList().join(QLatin1Char(',')));
745
    } else if (params.contains(QStringLiteral("%audioquality"))) {
746
        // profile has a variable bitrate
747
        profileElement.setAttribute(QStringLiteral("defaultaudioquality"), item->data(0, DefaultAudioBitrateRole).toString());
748
        profileElement.setAttribute(QStringLiteral("audioqualities"), item->data(0, AudioBitratesRole).toStringList().join(QLatin1Char(',')));
749
    }
Nicolas Carion's avatar
Nicolas Carion committed
750
    if (item->data(0, SpeedsRole).canConvert(QVariant::StringList) && (item->data(0, SpeedsRole).toStringList().count() != 0)) {
751
        // profile has a variable speed
752
        profileElement.setAttribute(QStringLiteral("speeds"), item->data(0, SpeedsRole).toStringList().join(QLatin1Char(';')));
753
    }
754
    doc.appendChild(profileElement);
755
    if (saveProfile(doc.documentElement())) {
756
        parseProfiles(profileElement.attribute(QStringLiteral("name")));
757
    }
758 759
}

760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
void RenderWidget::slotDownloadNewRenderProfiles()
{
    if (getNewStuff(QStringLiteral(":data/kdenlive_renderprofiles.knsrc")) > 0) {
        reloadProfiles();
    }
}

int RenderWidget::getNewStuff(const QString &configFile)
{
    KNS3::Entry::List entries;
    QPointer<KNS3::DownloadDialog> dialog = new KNS3::DownloadDialog(configFile);
    if (dialog->exec() != 0) {
        entries = dialog->changedEntries();
    }
    for (const KNS3::Entry &entry : entries) {
        if (entry.status() == KNS3::Entry::Installed) {
            qCDebug(KDENLIVE_LOG) << "// Installed files: " << entry.installedFiles();
        }
    }
    delete dialog;
    return entries.size();
}

783 784
void RenderWidget::slotEditProfile()
{
785
    QTreeWidgetItem *item = m_view.formats->currentItem();
Nicolas Carion's avatar
Nicolas Carion committed
786
    if ((item == nullptr) || (item->parent() == nullptr)) {
787 788
        return;
    }
789
    QString params = item->data(0, ParamsRole).toString();
790 791

    Ui::SaveProfile_UI ui;
792
    QPointer<QDialog> d = new QDialog(this);
793
    ui.setupUi(d);
794

795
    QString customGroup = item->parent()->text(0);
796 797 798
    if (customGroup.isEmpty()) {
        customGroup = i18nc("Group Name", "Custom");
    }
799 800
    ui.group_name->setText(customGroup);

801 802
    ui.profile_name->setText(item->text(0));
    ui.extension->setText(item->data(0, ExtensionRole).toString());
803 804
    ui.parameters->setText(params);
    ui.profile_name->setFocus();
805 806
    if (params.contains(QStringLiteral("%bitrate")) || ui.parameters->toPlainText().contains(QStringLiteral("%quality"))) {
        if (params.contains(QStringLiteral("%quality"))) {
807 808 809 810 811 812
            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"));
        }
Nicolas Carion's avatar
Nicolas Carion committed
813
        if (item->data(0, BitratesRole).canConvert(QVariant::StringList) && (item->data(0, BitratesRole).toStringList().count() != 0)) {
814
            QStringList bitrates = item->data(0, BitratesRole).toStringList();
815
            ui.vbitrates_list->setText(bitrates.join(QLatin1Char(',')));
816
            if (item->data(0, DefaultBitrateRole).canConvert(QVariant::String)) {
817
                ui.default_vbitrate->setValue(item->data(0, DefaultBitrateRole).toInt());
818
            }
819
        }
Laurent Montel's avatar
Laurent Montel committed
820 821
    } else {
        ui.vbitrates->setHidden(true);
822
    }
Laurent Montel's avatar
Laurent Montel committed
823

824 825
    if (params.contains(QStringLiteral("%audiobitrate")) || ui.parameters->toPlainText().contains(QStringLiteral("%audioquality"))) {
        if (params.contains(QStringLiteral("%audioquality"))) {
826 827 828 829 830 831
            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"));
        }
Nicolas Carion's avatar
Nicolas Carion committed
832
        if (item->data(0, AudioBitratesRole).canConvert(QVariant::StringList) && (item->data(0, AudioBitratesRole).toStringList().count() != 0)) {
833
            QStringList bitrates = item->data(0, AudioBitratesRole).toStringList();
834
            ui.abitrates_list->setText(bitrates.join(QLatin1Char(',')));
835
            if (item->data(0, DefaultAudioBitrateRole).canConvert(QVariant::String)) {
836
                ui.default_abitrate->setValue(item->data(0, DefaultAudioBitrateRole).toInt());
837
            }
838
        }
839 840
    } else {
        ui.abitrates->setHidden(true);
841
    }
842

Nicolas Carion's avatar
Nicolas Carion committed
843
    if (item->data(0, SpeedsRole).canConvert(QVariant::StringList) && (item->data(0, SpeedsRole).toStringList().count() != 0)) {
844 845 846 847
        QStringList speeds = item->data(0, SpeedsRole).toStringList();
        ui.speeds_list->setText(speeds.join('\n'));
    }

848
    d->setWindowTitle(i18n("Edit Profile"));
849

850
    if (d->exec() == QDialog::Accepted) {
851
        slotDeleteProfile(true);
852
        QString exportFile = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/export/customprofiles.xml");
853 854 855 856 857
        QDomDocument doc;
        QFile file(exportFile);
        doc.setContent(&file, false);
        file.close();
        QDomElement documentElement;
858 859
        QDomElement profiles = doc.documentElement();

860
        if (profiles.isNull() || profiles.tagName() != QLatin1String("profiles")) {
861
            doc.clear();
862 863
            profiles = doc.createElement(QStringLiteral("profiles"));
            profiles.setAttribute(QStringLiteral("version"), 1);
864 865 866
            doc.appendChild(profiles);
        }

Laurent Montel's avatar
Laurent Montel committed
867
        int version = profiles.attribute(QStringLiteral("version"), nullptr).toInt();
868 869
        if (version < 1) {
            doc.clear();
870 871
            profiles = doc.createElement(QStringLiteral("profiles"));
            profiles.setAttribute(QStringLiteral("version"), 1);
872
            doc.appendChild(profiles);
873 874 875 876
        }

        QString newProfileName = ui.profile_name->text().simplified();
        QString newGroupName = ui.group_name->text().simplified();
877 878 879
        if (newGroupName.isEmpty()) {
            newGroupName = i18nc("Group Name", "Custom");
        }
880
        QDomNodeList profilelist = doc.elementsByTagName(QStringLiteral("profile"));
881 882 883 884
        int i = 0;
        while (!profilelist.item(i).isNull()) {
            // make sure a profile with same name doesn't exist
            documentElement = profilelist.item(i).toElement();
885
            QString profileName = documentElement.attribute(QStringLiteral("name"));
886 887 888
            if (profileName == newProfileName) {
                // a profile with that same name already exists
                bool ok;
889 890 891
                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);
892 893 894
                if (!ok) {
                    return;
                }
895 896
                if (profileName == newProfileName) {
                    profiles.removeChild(profilelist.item(i));
897 898 899
                    break;
                }
            }
900
            ++i;
901
        }
902

903 904 905 906
        QDomElement profileElement = doc.createElement(QStringLiteral("profile"));
        profileElement.setAttribute(QStringLiteral("name"), newProfileName);
        profileElement.setAttribute(QStringLiteral("category"), newGroupName);
        profileElement.setAttribute(QStringLiteral("extension"), ui.extension->text().simplified());
907
        QString args = ui.parameters->toPlainText().simplified();
908 909
        profileElement.setAttribute(QStringLiteral("args"), args);
        if (args.contains(QStringLiteral("%bitrate"))) {
910
            // profile has a variable bitrate
911 912
            profileElement.setAttribute(QStringLiteral("defaultbitrate"), QString::number(ui.default_vbitrate->value()));
            profileElement.setAttribute(QStringLiteral("bitrates"), ui.vbitrates_list->text());
913
        } else if (args.contains(QStringLiteral("%quality"))) {
914 915
            profileElement.setAttribute(QStringLiteral("defaultquality"), QString::number(ui.default_vbitrate->value()));
            profileElement.setAttribute(QStringLiteral("qualities"), ui.vbitrates_list->text());
916
        }
917
        if (args.contains(QStringLiteral("%audiobitrate"))) {
918
            // profile has a variable bitrate
919 920 921 922 923
            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());
924 925
        }

926 927
        QString speeds_list_str = ui.speeds_list->toPlainText();
        if (!speeds_list_str.isEmpty()) {
928
            // profile has a variable speed
929
            profileElement.setAttribute(QStringLiteral("speeds"), speeds_list_str.replace('\n', ';').simplified());
930
        }
931

932
        profiles.appendChild(profileElement);
933

934
        // QCString save = doc.toString().utf8();
935
        delete d;
936
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
937
            KMessageBox::error(this, i18n("Cannot write to file %1", exportFile));
938 939 940 941
            return;
        }
        QTextStream out(&file);
        out << doc.toString();
942 943 944 945 946
        if (file.error() != QFile::NoError) {
            KMessageBox::error(this, i18n("Cannot write to file %1", exportFile));
            file.close();
            return;
        }
947
        file.close();
948
        parseProfiles();
949 950 951
    } else {
        delete d;
    }
952 953
}

954
void RenderWidget::slotDeleteProfile(bool dontRefresh)
955
{
956
    // TODO: delete a profile installed by KNewStuff the easy way
957
    /*
958
    QString edit = m_view.formats->currentItem()->data(EditableRole).toString();
959
    if (!edit.endsWith(QLatin1String("customprofiles.xml"))) {
960
        // This is a KNewStuff installed file, process through KNS
961 962 963 964 965 966
        KNS::Engine engine(0);
        if (engine.init("kdenlive_render.knsrc")) {
            KNS::Entry::List entries;
        }
        return;
    }*/
967
    QTreeWidgetItem *item = m_view.formats->currentItem();
Nicolas Carion's avatar
Nicolas Carion committed
968
    if ((item == nullptr) || (item->parent() == nullptr)) {
969 970
        return;
    }
971
    QString currentProfile = item->text(0);
972

973
    QString exportFile = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/export/customprofiles.xml");