monitor.cpp 86.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/***************************************************************************
 *   Copyright (C) 2007 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          *
 ***************************************************************************/

Vincent Pinon's avatar
Vincent Pinon committed
20
#include "monitor.h"
Nicolas Carion's avatar
Nicolas Carion committed
21
#include "bin/bin.h"
22
#include "bin/projectclip.h"
23
#include "lib/localeHandling.h"
Nicolas Carion's avatar
Nicolas Carion committed
24 25 26 27
#include "core.h"
#include "dialogs/profilesdialog.h"
#include "doc/kdenlivedoc.h"
#include "doc/kthumb.h"
28
#include "glwidget.h"
Nicolas Carion's avatar
Nicolas Carion committed
29 30 31 32
#include "kdenlivesettings.h"
#include "lib/audio/audioStreamInfo.h"
#include "mainwindow.h"
#include "mltcontroller/clipcontroller.h"
33
#include "monitorproxy.h"
34
#include "profiles/profilemodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
35
#include "project/projectmanager.h"
36
#include "qmlmanager.h"
37
#include "recmanager.h"
38 39
#include "jobs/jobmanager.h"
#include "jobs/cutclipjob.h"
40
#include "scopes/monitoraudiolevel.h"
41
#include "timeline2/model/snapmodel.hpp"
42
#include "transitions/transitionsrepository.hpp"
43
#include "utils/thumbnailcache.hpp"
44

45
#include "klocalizedstring.h"
46
#include <KDualAction>
Nicolas Carion's avatar
Nicolas Carion committed
47
#include <KFileWidget>
Nicolas Carion's avatar
Nicolas Carion committed
48 49 50
#include <KMessageWidget>
#include <KRecentDirs>
#include <KSelectAction>
51
#include <KWindowConfig>
52
#include <KColorScheme>
Vincent Pinon's avatar
Vincent Pinon committed
53
#include <kio_version.h>
54

Laurent Montel's avatar
Laurent Montel committed
55
#include "kdenlive_debug.h"
Vincent Pinon's avatar
Vincent Pinon committed
56
#include <QScreen>
57
#include <QDrag>
Nicolas Carion's avatar
Nicolas Carion committed
58
#include <QMenu>
59
#include <QMimeData>
Nicolas Carion's avatar
Nicolas Carion committed
60
#include <QMouseEvent>
61
#include <QQuickItem>
62
#include <QScrollBar>
Nicolas Carion's avatar
Nicolas Carion committed
63 64 65 66
#include <QSlider>
#include <QToolBar>
#include <QToolButton>
#include <QVBoxLayout>
67
#include <QWidgetAction>
Nicolas Carion's avatar
Nicolas Carion committed
68
#include <utility>
69 70
#define SEEK_INACTIVE (-1)

71 72
QuickEventEater::QuickEventEater(QObject *parent)
    : QObject(parent)
73 74 75 76 77
{
}

bool QuickEventEater::eventFilter(QObject *obj, QEvent *event)
{
78
    switch (event->type()) {
Laurent Montel's avatar
Laurent Montel committed
79
    case QEvent::DragEnter: {
Nicolas Carion's avatar
Nicolas Carion committed
80
        auto *ev = reinterpret_cast<QDragEnterEvent *>(event);
81
        if (ev->mimeData()->hasFormat(QStringLiteral("kdenlive/effect"))) {
Laurent Montel's avatar
Laurent Montel committed
82 83
            ev->acceptProposedAction();
            return true;
84
        }
Laurent Montel's avatar
Laurent Montel committed
85 86 87
        break;
    }
    case QEvent::DragMove: {
Nicolas Carion's avatar
Nicolas Carion committed
88
        auto *ev = reinterpret_cast<QDragEnterEvent *>(event);
89
        if (ev->mimeData()->hasFormat(QStringLiteral("kdenlive/effect"))) {
Laurent Montel's avatar
Laurent Montel committed
90 91
            ev->acceptProposedAction();
            return true;
92
        }
Laurent Montel's avatar
Laurent Montel committed
93 94 95
        break;
    }
    case QEvent::Drop: {
Nicolas Carion's avatar
Nicolas Carion committed
96
        auto *ev = static_cast<QDropEvent *>(event);
Laurent Montel's avatar
Laurent Montel committed
97
        if (ev) {
98 99 100 101 102
            QStringList effectData;
            effectData << QString::fromUtf8(ev->mimeData()->data(QStringLiteral("kdenlive/effect")));
            QStringList source = QString::fromUtf8(ev->mimeData()->data(QStringLiteral("kdenlive/effectsource"))).split(QLatin1Char('-'));
            effectData << source;
            emit addEffect(effectData);
Laurent Montel's avatar
Laurent Montel committed
103 104
            ev->accept();
            return true;
105
        }
Laurent Montel's avatar
Laurent Montel committed
106 107 108 109
        break;
    }
    default:
        break;
110 111 112 113
    }
    return QObject::eventFilter(obj, event);
}

114 115
QuickMonitorEventEater::QuickMonitorEventEater(QWidget *parent)
    : QObject(parent)
116 117 118 119 120 121
{
}

bool QuickMonitorEventEater::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::KeyPress) {
Nicolas Carion's avatar
Nicolas Carion committed
122
        auto *ev = static_cast<QKeyEvent *>(event);
123 124 125 126 127 128 129
        if (ev) {
            emit doKeyPressEvent(ev);
            return true;
        }
    }
    return QObject::eventFilter(obj, event);
}
130

Nicolas Carion's avatar
Nicolas Carion committed
131
Monitor::Monitor(Kdenlive::MonitorId id, MonitorManager *manager, QWidget *parent)
132 133 134
    : AbstractMonitor(id, manager, parent)
    , m_controller(nullptr)
    , m_glMonitor(nullptr)
135
    , m_snaps(new SnapModel())
136 137 138 139 140 141 142
    , m_splitEffect(nullptr)
    , m_splitProducer(nullptr)
    , m_dragStarted(false)
    , m_recManager(nullptr)
    , m_loopClipAction(nullptr)
    , m_sceneVisibilityAction(nullptr)
    , m_contextMenu(nullptr)
143
    , m_markerMenu(nullptr)
144
    , m_audioChannels(nullptr)
145 146 147
    , m_loopClipTransition(true)
    , m_editMarker(nullptr)
    , m_forceSizeFactor(0)
148
    , m_offset(id == Kdenlive::ProjectMonitor ? TimelineModel::seekDuration : 0)
149
    , m_lastMonitorSceneType(MonitorSceneDefault)
Nicolas Carion's avatar
Nicolas Carion committed
150
{
Nicolas Carion's avatar
Nicolas Carion committed
151
    auto *layout = new QVBoxLayout;
152
    layout->setContentsMargins(0, 0, 0, 0);
153
    layout->setSpacing(0);
154 155
    // Create container widget
    m_glWidget = new QWidget;
156
    m_glWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
Nicolas Carion's avatar
Nicolas Carion committed
157
    auto *glayout = new QGridLayout(m_glWidget);
158 159 160
    glayout->setSpacing(0);
    glayout->setContentsMargins(0, 0, 0, 0);
    // Create QML OpenGL widget
Nicolas Carion's avatar
Nicolas Carion committed
161
    m_glMonitor = new GLWidget((int)id);
Laurent Montel's avatar
Laurent Montel committed
162
    connect(m_glMonitor, &GLWidget::passKeyEvent, this, &Monitor::doKeyPressEvent);
163
    connect(m_glMonitor, &GLWidget::panView, this, &Monitor::panView);
164
    connect(m_glMonitor->getControllerProxy(), &MonitorProxy::requestSeek, this, &Monitor::processSeek, Qt::DirectConnection);
165
    connect(m_glMonitor->getControllerProxy(), &MonitorProxy::positionChanged, this, &Monitor::slotSeekPosition);
166

Laurent Montel's avatar
Laurent Montel committed
167
    m_videoWidget = QWidget::createWindowContainer(qobject_cast<QWindow *>(m_glMonitor));
168
    m_videoWidget->setAcceptDrops(true);
Nicolas Carion's avatar
Nicolas Carion committed
169
    auto *leventEater = new QuickEventEater(this);
170
    m_videoWidget->installEventFilter(leventEater);
171 172
    connect(leventEater, &QuickEventEater::addEffect, this, &Monitor::slotAddEffect);

173 174 175
    m_qmlManager = new QmlManager(m_glMonitor);
    connect(m_qmlManager, &QmlManager::effectChanged, this, &Monitor::effectChanged);
    connect(m_qmlManager, &QmlManager::effectPointsChanged, this, &Monitor::effectPointsChanged);
176
    connect(m_qmlManager, &QmlManager::activateTrack, this, &Monitor::activateTrack);
177

Nicolas Carion's avatar
Nicolas Carion committed
178
    auto *monitorEventEater = new QuickMonitorEventEater(this);
179
    m_videoWidget->installEventFilter(monitorEventEater);
Laurent Montel's avatar
Laurent Montel committed
180
    connect(monitorEventEater, &QuickMonitorEventEater::doKeyPressEvent, this, &Monitor::doKeyPressEvent);
181

182
    glayout->addWidget(m_videoWidget, 0, 0);
183 184 185 186 187 188
    m_verticalScroll = new QScrollBar(Qt::Vertical);
    glayout->addWidget(m_verticalScroll, 0, 1);
    m_verticalScroll->hide();
    m_horizontalScroll = new QScrollBar(Qt::Horizontal);
    glayout->addWidget(m_horizontalScroll, 1, 0);
    m_horizontalScroll->hide();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
189 190
    connect(m_horizontalScroll, &QAbstractSlider::valueChanged, this, &Monitor::setOffsetX);
    connect(m_verticalScroll, &QAbstractSlider::valueChanged, this, &Monitor::setOffsetY);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
191
    connect(m_glMonitor, &GLWidget::frameDisplayed, this, &Monitor::onFrameDisplayed, Qt::DirectConnection);
192
    connect(m_glMonitor, &GLWidget::mouseSeek, this, &Monitor::slotMouseSeek);
Vincent Pinon's avatar
Vincent Pinon committed
193
    connect(m_glMonitor, &GLWidget::monitorPlay, this, &Monitor::slotPlay);
Laurent Montel's avatar
Laurent Montel committed
194
    connect(m_glMonitor, &GLWidget::startDrag, this, &Monitor::slotStartDrag);
Vincent Pinon's avatar
Vincent Pinon committed
195
    connect(m_glMonitor, &GLWidget::switchFullScreen, this, &Monitor::slotSwitchFullScreen);
Laurent Montel's avatar
Laurent Montel committed
196
    connect(m_glMonitor, &GLWidget::zoomChanged, this, &Monitor::setZoom);
197
    connect(m_glMonitor, SIGNAL(lockMonitor(bool)), this, SLOT(slotLockMonitor(bool)), Qt::DirectConnection);
Laurent Montel's avatar
Laurent Montel committed
198
    connect(m_glMonitor, &GLWidget::showContextMenu, this, &Monitor::slotShowMenu);
199
    connect(m_glMonitor, &GLWidget::gpuNotSupported, this, &Monitor::gpuError);
200

201 202
    m_glWidget->setMinimumSize(QSize(320, 180));
    layout->addWidget(m_glWidget, 10);
203 204 205
    layout->addStretch();

    // Tool bar buttons
206
    m_toolbar = new QToolBar(this);
207 208 209
    int size = style()->pixelMetric(QStyle::PM_SmallIconSize);
    QSize iconSize(size, size);
    m_toolbar->setIconSize(iconSize);
210 211 212 213 214

    QComboBox *scalingAction = new QComboBox(this);
    scalingAction->setToolTip(i18n("Preview resolution - lower resolution means faster preview"));
    // Combobox padding is bad, so manually add a space before text
    scalingAction->addItems({QStringLiteral(" ") + i18n("1:1"),QStringLiteral(" ") + i18n("720p"),QStringLiteral(" ") + i18n("540p"),QStringLiteral(" ") + i18n("360p"),QStringLiteral(" ") + i18n("270p")});
Vincent Pinon's avatar
Vincent Pinon committed
215
    connect(scalingAction, QOverload<int>::of(&QComboBox::activated), this, [this] (int index) {
216 217 218 219
        switch (index) {
            case 1:
                KdenliveSettings::setPreviewScaling(2);
                break;
220 221 222
            case 2:
                KdenliveSettings::setPreviewScaling(4);
                break;
223
            case 3:
224 225
                KdenliveSettings::setPreviewScaling(8);
                break;
226
            case 4:
227 228 229
                KdenliveSettings::setPreviewScaling(16);
                break;
            default:
230
                KdenliveSettings::setPreviewScaling(0);
231
        }
Vincent Pinon's avatar
Vincent Pinon committed
232 233
        emit m_monitorManager->scalingChanged();
        emit m_monitorManager->updatePreviewScaling();
234
    });
235

Vincent Pinon's avatar
Vincent Pinon committed
236
    connect(manager, &MonitorManager::updatePreviewScaling, this, [this, scalingAction]() {
237 238 239
        m_glMonitor->updateScaling();
        switch (KdenliveSettings::previewScaling()) {
            case 2:
240
                scalingAction->setCurrentIndex(1);
241 242
                break;
            case 4:
243
                scalingAction->setCurrentIndex(2);
244 245
                break;
            case 8:
246
                scalingAction->setCurrentIndex(3);
247 248
                break;
            case 16:
249
                scalingAction->setCurrentIndex(4);
250 251
                break;
            default:
252
                scalingAction->setCurrentIndex(0);
253 254 255 256
                break;
        }
        refreshMonitorIfActive();
    });
257 258 259 260
    scalingAction->setFrame(false);
    scalingAction->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
    m_toolbar->addWidget(scalingAction);
    m_toolbar->addSeparator();
261

262 263
    m_speedLabel = new QLabel(this);
    m_speedLabel->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
264
    KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Button);
265
    QColor bg = scheme.background(KColorScheme::PositiveBackground).color();
266 267 268
    m_speedLabel->setStyleSheet(QString("padding-left: %4; padding-right: %4;background-color: rgb(%1,%2,%3);").arg(bg.red()).arg(bg.green()).arg(bg.blue()).arg(m_speedLabel->sizeHint().height()/4));
    m_toolbar->addWidget(m_speedLabel);
    m_speedLabel->setFixedWidth(0);
269 270
    if (id == Kdenlive::ClipMonitor) {
        // Add options for recording
271
        m_recManager = new RecManager(this);
Vincent Pinon's avatar
Vincent Pinon committed
272
        connect(m_recManager, &RecManager::warningMessage, this, &Monitor::warningMessage);
273
        connect(m_recManager, &RecManager::addClipToProject, this, &Monitor::addClipToProject);
274

275
        m_toolbar->addAction(manager->getAction(QStringLiteral("insert_project_tree")));
276
        m_toolbar->setToolTip(i18n("Insert Zone to Project Bin"));
277
        m_toolbar->addSeparator();
278 279 280 281
        m_streamsButton = new QToolButton(this);
        m_streamsButton->setPopupMode(QToolButton::InstantPopup);
        m_streamsButton->setIcon(QIcon::fromTheme(QStringLiteral("speaker")));
        m_streamAction = m_toolbar->addWidget(m_streamsButton);
282
        m_audioChannels = new QMenu(this);
283 284
        m_streamsButton->setMenu(m_audioChannels);
        m_streamAction->setVisible(false);
Vincent Pinon's avatar
Vincent Pinon committed
285
        connect(m_audioChannels, &QMenu::triggered, this, [this] (QAction *ac) {
286
            //m_audioChannels->show();
287 288
            QList <QAction*> actions = m_audioChannels->actions();
            QMap <int, QString> enabledStreams;
289 290 291 292 293 294
            if (ac->data().toInt() == INT_MAX) {
                // Merge stream selected, clear all others
                enabledStreams.clear();
                enabledStreams.insert(INT_MAX, i18n("Merged streams"));
                // Disable all other streams
                QSignalBlocker bk(m_audioChannels);
Vincent Pinon's avatar
Vincent Pinon committed
295
                for (auto act : qAsConst(actions)) {
296 297 298 299 300
                    if (act->isChecked() && act != ac) {
                        act->setChecked(false);
                    }
                }
            } else {
Vincent Pinon's avatar
Vincent Pinon committed
301
                for (auto act : qAsConst(actions)) {
302 303 304 305 306 307 308 309 310
                    if (act->isChecked()) {
                        // Audio stream is selected
                        if (act->data().toInt() == INT_MAX) {
                            QSignalBlocker bk(m_audioChannels);
                            act->setChecked(false);
                        } else {
                            enabledStreams.insert(act->data().toInt(), act->text().remove(QLatin1Char('&')));
                        }
                    }
311 312 313 314 315 316 317 318
                }
            }
            if (!enabledStreams.isEmpty()) {
                // Only 1 stream wanted, easy
                QMap <QString, QString> props;
                props.insert(QStringLiteral("audio_index"), QString::number(enabledStreams.firstKey()));
                QList <int> streams = enabledStreams.keys();
                QStringList astreams;
Vincent Pinon's avatar
Vincent Pinon committed
319
                for (const int st : qAsConst(streams)) {
320 321 322 323 324 325 326 327 328 329 330 331
                    astreams << QString::number(st);
                }
                props.insert(QStringLiteral("kdenlive:active_streams"), astreams.join(QLatin1Char(';')));
                m_controller->setProperties(props, true);
            } else {
                // No active stream
                QMap <QString, QString> props;
                props.insert(QStringLiteral("audio_index"), QStringLiteral("-1"));
                props.insert(QStringLiteral("kdenlive:active_streams"), QStringLiteral("-1"));
                m_controller->setProperties(props, true);
            }
        });
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
332
    } else if (id == Kdenlive::ProjectMonitor) {
333
        connect(m_glMonitor, &GLWidget::paused, m_monitorManager, &MonitorManager::cleanMixer);
334 335
    }

336
    if (id != Kdenlive::DvdMonitor) {
337 338 339 340
        QAction *markIn = new QAction(QIcon::fromTheme(QStringLiteral("zone-in")), i18n("Set Zone In"), this);
        QAction *markOut = new QAction(QIcon::fromTheme(QStringLiteral("zone-out")), i18n("Set Zone Out"), this);
        m_toolbar->addAction(markIn);
        m_toolbar->addAction(markOut);
Vincent Pinon's avatar
Vincent Pinon committed
341
        connect(markIn, &QAction::triggered, this, [&, manager]() {
342 343 344
            m_monitorManager->activateMonitor(m_id);
            manager->getAction(QStringLiteral("mark_in"))->trigger();
        });
Vincent Pinon's avatar
Vincent Pinon committed
345
        connect(markOut, &QAction::triggered, this, [&, manager]() {
346 347 348
            m_monitorManager->activateMonitor(m_id);
            manager->getAction(QStringLiteral("mark_out"))->trigger();
        });
349
    }
350 351 352 353
    // Per monitor rewind action
    QAction *rewind = new QAction(QIcon::fromTheme(QStringLiteral("media-seek-backward")), i18n("Rewind"), this);
    m_toolbar->addAction(rewind);
    connect(rewind, &QAction::triggered, this, &Monitor::slotRewind);
354

Nicolas Carion's avatar
Nicolas Carion committed
355
    auto *playButton = new QToolButton(m_toolbar);
356
    m_playMenu = new QMenu(i18n("Play..."), this);
357
    connect(m_playMenu, &QMenu::aboutToShow, this, &Monitor::slotActivateMonitor);
Laurent Montel's avatar
Laurent Montel committed
358
    QAction *originalPlayAction = static_cast<KDualAction *>(manager->getAction(QStringLiteral("monitor_play")));
359
    m_playAction = new KDualAction(i18n("Play"), i18n("Pause"), this);
360 361
    m_playAction->setInactiveIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
    m_playAction->setActiveIcon(QIcon::fromTheme(QStringLiteral("media-playback-pause")));
362

Laurent Montel's avatar
Laurent Montel committed
363
    QString strippedTooltip = m_playAction->toolTip().remove(QRegExp(QStringLiteral("\\s\\(.*\\)")));
364 365
    // append shortcut if it exists for action
    if (originalPlayAction->shortcut() == QKeySequence(0)) {
Laurent Montel's avatar
Laurent Montel committed
366 367
        m_playAction->setToolTip(strippedTooltip);
    } else {
368
        m_playAction->setToolTip(strippedTooltip + QStringLiteral(" (") + originalPlayAction->shortcut().toString() + QLatin1Char(')'));
369
    }
370
    m_playMenu->addAction(m_playAction);
371
    connect(m_playAction, &QAction::triggered, this, &Monitor::slotSwitchPlay);
372 373 374

    playButton->setMenu(m_playMenu);
    playButton->setPopupMode(QToolButton::MenuButtonPopup);
375
    m_toolbar->addWidget(playButton);
376 377 378 379

    // Per monitor forward action
    QAction *forward = new QAction(QIcon::fromTheme(QStringLiteral("media-seek-forward")), i18n("Forward"), this);
    m_toolbar->addAction(forward);
Vincent Pinon's avatar
Vincent Pinon committed
380
    connect(forward, &QAction::triggered, this, [this]() {
381 382
        Monitor::slotForward();
    });
383 384

    playButton->setDefaultAction(m_playAction);
385
    m_configMenu = new QMenu(i18n("Misc..."), this);
386

387 388
    if (id != Kdenlive::DvdMonitor) {
        if (id == Kdenlive::ClipMonitor) {
389
            m_markerMenu = new QMenu(i18n("Go to marker..."), this);
390 391
        } else {
            m_markerMenu = new QMenu(i18n("Go to guide..."), this);
392
        }
393 394
        m_markerMenu->setEnabled(false);
        m_configMenu->addMenu(m_markerMenu);
Laurent Montel's avatar
Laurent Montel committed
395
        connect(m_markerMenu, &QMenu::triggered, this, &Monitor::slotGoToMarker);
396
        m_forceSize = new KSelectAction(QIcon::fromTheme(QStringLiteral("transform-scale")), i18n("Force Monitor Size"), this);
397 398 399 400 401 402 403 404
        QAction *fullAction = m_forceSize->addAction(QIcon(), i18n("Force 100%"));
        fullAction->setData(100);
        QAction *halfAction = m_forceSize->addAction(QIcon(), i18n("Force 50%"));
        halfAction->setData(50);
        QAction *freeAction = m_forceSize->addAction(QIcon(), i18n("Free Resize"));
        freeAction->setData(0);
        m_configMenu->addAction(m_forceSize);
        m_forceSize->setCurrentAction(freeAction);
Nicolas Carion's avatar
Nicolas Carion committed
405
        connect(m_forceSize, static_cast<void (KSelectAction::*)(QAction *)>(&KSelectAction::triggered), this, &Monitor::slotForceSize);
406
    }
407

408 409 410
    // Create Volume slider popup
    m_audioSlider = new QSlider(Qt::Vertical);
    m_audioSlider->setRange(0, 100);
411
    m_audioSlider->setValue(KdenliveSettings::volume());
412
    connect(m_audioSlider, &QSlider::valueChanged, this, &Monitor::slotSetVolume);
Nicolas Carion's avatar
Nicolas Carion committed
413
    auto *widgetslider = new QWidgetAction(this);
414 415
    widgetslider->setText(i18n("Audio volume"));
    widgetslider->setDefaultWidget(m_audioSlider);
416 417
    auto *menu = new QMenu(i18n("Volume"), this);
    menu->setIcon(QIcon::fromTheme(QStringLiteral("audio-volume-medium")));
418
    menu->addAction(widgetslider);
419
    m_configMenu->addMenu(menu);
420

421
    /*QIcon icon;
Laurent Montel's avatar
Laurent Montel committed
422
    if (KdenliveSettings::volume() == 0) {
423
        icon = QIcon::fromTheme(QStringLiteral("audio-volume-muted"));
Laurent Montel's avatar
Laurent Montel committed
424
    } else {
425
        icon = QIcon::fromTheme(QStringLiteral("audio-volume-medium"));
Laurent Montel's avatar
Laurent Montel committed
426
    }
427
    m_audioButton->setIcon(icon);*/
428

429
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
430 431
    setLayout(layout);
    setMinimumHeight(200);
432

433 434
    connect(this, &Monitor::scopesClear, m_glMonitor, &GLWidget::releaseAnalyse, Qt::DirectConnection);
    connect(m_glMonitor, &GLWidget::analyseFrame, this, &Monitor::frameUpdated);
435

436
    if (id == Kdenlive::ProjectMonitor) {
Nicolas Carion's avatar
linting  
Nicolas Carion committed
437 438
        // TODO: reimplement
        // connect(render, &Render::durationChanged, this, &Monitor::durationChanged);
439
        connect(m_glMonitor->getControllerProxy(), &MonitorProxy::saveZone, this, &Monitor::zoneUpdated);
440
        connect(m_glMonitor->getControllerProxy(), &MonitorProxy::saveZoneWithUndo, this, &Monitor::zoneUpdatedWithUndo);
441
    } else if (id == Kdenlive::ClipMonitor) {
442
        connect(m_glMonitor->getControllerProxy(), &MonitorProxy::saveZone, this, &Monitor::updateClipZone);
443
    }
444
    connect(m_glMonitor->getControllerProxy(), &MonitorProxy::triggerAction, pCore.get(), &Core::triggerAction);
445 446 447
    connect(m_glMonitor->getControllerProxy(), &MonitorProxy::seekNextKeyframe, this, &Monitor::seekToNextKeyframe);
    connect(m_glMonitor->getControllerProxy(), &MonitorProxy::seekPreviousKeyframe, this, &Monitor::seekToPreviousKeyframe);
    connect(m_glMonitor->getControllerProxy(), &MonitorProxy::addRemoveKeyframe, this, &Monitor::addRemoveKeyframe);
448
    connect(m_glMonitor->getControllerProxy(), &MonitorProxy::seekToKeyframe, this, &Monitor::slotSeekToKeyFrame);
449

450
    m_sceneVisibilityAction = new QAction(QIcon::fromTheme(QStringLiteral("transform-crop")), i18n("Show/Hide edit mode"), this);
451 452
    m_sceneVisibilityAction->setCheckable(true);
    m_sceneVisibilityAction->setChecked(KdenliveSettings::showOnMonitorScene());
453
    connect(m_sceneVisibilityAction, &QAction::triggered, this, &Monitor::slotEnableEffectScene);
454
    m_toolbar->addAction(m_sceneVisibilityAction);
455

456
    m_toolbar->addSeparator();
457
    m_timePos = new TimecodeDisplay(pCore->timecode(), this);
458 459
    m_toolbar->addWidget(m_timePos);

Nicolas Carion's avatar
Nicolas Carion committed
460
    auto *configButton = new QToolButton(m_toolbar);
461
    configButton->setIcon(QIcon::fromTheme(QStringLiteral("kdenlive-menu")));
462
    configButton->setToolTip(i18n("Options"));
463
    configButton->setMenu(m_configMenu);
464
    configButton->setPopupMode(QToolButton::InstantPopup);
465
    m_toolbar->addWidget(configButton);
466
    m_toolbar->addSeparator();
467 468
    QMargins mrg = m_toolbar->contentsMargins();
    m_audioMeterWidget = new MonitorAudioLevel(m_toolbar->height() - mrg.top() - mrg.bottom(), this);
469
    m_toolbar->addWidget(m_audioMeterWidget);
470
    if (!m_audioMeterWidget->isValid) {
471 472 473 474
        KdenliveSettings::setMonitoraudio(0x01);
        m_audioMeterWidget->setVisibility(false);
    } else {
        m_audioMeterWidget->setVisibility((KdenliveSettings::monitoraudio() & m_id) != 0);
475
    }
476

477
    connect(m_timePos, SIGNAL(timeCodeEditingFinished()), this, SLOT(slotSeek()));
478
    layout->addWidget(m_toolbar);
Laurent Montel's avatar
Laurent Montel committed
479 480 481
    if (m_recManager) {
        layout->addWidget(m_recManager->toolbar());
    }
482 483

    // Load monitor overlay qml
484
    loadQmlScene(MonitorSceneDefault);
485

486 487 488 489 490
    // Monitor dropped fps timer
    m_droppedTimer.setInterval(1000);
    m_droppedTimer.setSingleShot(false);
    connect(&m_droppedTimer, &QTimer::timeout, this, &Monitor::checkDrops);

491 492 493 494
    // Info message widget
    m_infoMessage = new KMessageWidget(this);
    layout->addWidget(m_infoMessage);
    m_infoMessage->hide();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
495 496
}

497 498
Monitor::~Monitor()
{
499
    delete m_audioMeterWidget;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
500
    delete m_glMonitor;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
501 502
    delete m_videoWidget;
    delete m_glWidget;
503 504 505
    delete m_timePos;
}

506 507 508 509 510 511 512 513 514 515
void Monitor::setOffsetX(int x)
{
    m_glMonitor->setOffsetX(x, m_horizontalScroll->maximum());
}

void Monitor::setOffsetY(int y)
{
    m_glMonitor->setOffsetY(y, m_verticalScroll->maximum());
}

516
void Monitor::slotGetCurrentImage(bool request)
517
{
518
    m_glMonitor->sendFrameForAnalysis = request;
519
    Kdenlive::MonitorId id = m_monitorManager->activeMonitor()->id();
520
    m_monitorManager->activateMonitor(m_id);
521
    refreshMonitorIfActive(true);
522 523
    if (request) {
        // Update analysis state
Laurent Montel's avatar
Laurent Montel committed
524
        QTimer::singleShot(500, m_monitorManager, &MonitorManager::checkScopes);
525 526 527
    } else {
        m_glMonitor->releaseAnalyse();
    }
528
    m_monitorManager->activateMonitor(id);
529 530
}

531
void Monitor::slotAddEffect(const QStringList &effect)
532 533
{
    if (m_id == Kdenlive::ClipMonitor) {
Laurent Montel's avatar
Laurent Montel committed
534
        if (m_controller) {
535
            emit addMasterEffect(m_controller->AbstractProjectItem::clipId(), effect);
Laurent Montel's avatar
Laurent Montel committed
536 537 538
        }
    } else {
        emit addEffect(effect);
539 540 541
    }
}

542 543 544 545 546 547
void Monitor::refreshIcons()
{
    QList<QAction *> allMenus = this->findChildren<QAction *>();
    for (int i = 0; i < allMenus.count(); i++) {
        QAction *m = allMenus.at(i);
        QIcon ic = m->icon();
Laurent Montel's avatar
Laurent Montel committed
548 549 550
        if (ic.isNull() || ic.name().isEmpty()) {
            continue;
        }
551
        QIcon newIcon = QIcon::fromTheme(ic.name());
552 553 554 555 556 557
        m->setIcon(newIcon);
    }
    QList<KDualAction *> allButtons = this->findChildren<KDualAction *>();
    for (int i = 0; i < allButtons.count(); i++) {
        KDualAction *m = allButtons.at(i);
        QIcon ic = m->activeIcon();
Laurent Montel's avatar
Laurent Montel committed
558 559 560
        if (ic.isNull() || ic.name().isEmpty()) {
            continue;
        }
561
        QIcon newIcon = QIcon::fromTheme(ic.name());
562 563
        m->setActiveIcon(newIcon);
        ic = m->inactiveIcon();
Laurent Montel's avatar
Laurent Montel committed
564 565 566
        if (ic.isNull() || ic.name().isEmpty()) {
            continue;
        }
567
        newIcon = QIcon::fromTheme(ic.name());
568 569 570 571
        m->setInactiveIcon(newIcon);
    }
}

572
QAction *Monitor::recAction()
573
{
Laurent Montel's avatar
Laurent Montel committed
574
    if (m_recManager) {
575
        return m_recManager->recAction();
Laurent Montel's avatar
Laurent Montel committed
576
    }
Laurent Montel's avatar
Laurent Montel committed
577
    return nullptr;
578 579
}

580 581 582 583 584
void Monitor::slotLockMonitor(bool lock)
{
    m_monitorManager->lockMonitor(m_id, lock);
}

585
void Monitor::setupMenu(QMenu *goMenu, QMenu *overlayMenu, QAction *playZone, QAction *loopZone, QMenu *markerMenu, QAction *loopClip)
586
{
587
    delete m_contextMenu;
588
    m_contextMenu = new QMenu(this);
589
    m_contextMenu->addMenu(m_playMenu);
Laurent Montel's avatar
Laurent Montel committed
590
    if (goMenu) {
591
        m_contextMenu->addMenu(goMenu);
Laurent Montel's avatar
Laurent Montel committed
592
    }
593

594
    if (markerMenu) {
595
        m_contextMenu->addMenu(markerMenu);
Nicolas Carion's avatar
Nicolas Carion committed
596
        QList<QAction *> list = markerMenu->actions();
597
        for (int i = 0; i < list.count(); ++i) {
598
            if (list.at(i)->objectName() == QLatin1String("edit_marker")) {
599 600 601 602 603
                m_editMarker = list.at(i);
                break;
            }
        }
    }
604

605 606
    m_playMenu->addAction(playZone);
    m_playMenu->addAction(loopZone);
607 608 609 610
    if (loopClip) {
        m_loopClipAction = loopClip;
        m_playMenu->addAction(loopClip);
    }
611

612
    m_contextMenu->addMenu(m_markerMenu);
613
    if (m_id == Kdenlive::ClipMonitor) {
614
        //m_contextMenu->addAction(QIcon::fromTheme(QStringLiteral("document-save")), i18n("Save zone"), this, SLOT(slotSaveZone()));
Nicolas Carion's avatar
Nicolas Carion committed
615
        QAction *extractZone =
616
            m_configMenu->addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18n("Extract Zone"), this, SLOT(slotExtractCurrentZone()));
617
        m_contextMenu->addAction(extractZone);
618
        m_contextMenu->addAction(m_monitorManager->getAction(QStringLiteral("insert_project_tree")));
619
    }
620 621
    m_contextMenu->addAction(m_monitorManager->getAction(QStringLiteral("extract_frame")));
    m_contextMenu->addAction(m_monitorManager->getAction(QStringLiteral("extract_frame_to_project")));
622
    m_contextMenu->addAction(m_monitorManager->getAction(QStringLiteral("add_project_note")));
623

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
624
    if (m_id == Kdenlive::ProjectMonitor) {
625
        m_contextMenu->addAction(m_monitorManager->getAction(QStringLiteral("monitor_multitrack")));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
626
    } else if (m_id == Kdenlive::ClipMonitor) {
627 628
        QAction *setThumbFrame =
            m_contextMenu->addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18n("Set current image as thumbnail"), this, SLOT(slotSetThumbFrame()));
629
        m_configMenu->addAction(setThumbFrame);
630 631 632
        QAction *alwaysShowAudio =
            new QAction(QIcon::fromTheme(QStringLiteral("kdenlive-show-audiothumb")), i18n("Always show audio thumbnails"), this);
        alwaysShowAudio->setCheckable(true);
Vincent Pinon's avatar
Vincent Pinon committed
633
        connect(alwaysShowAudio, &QAction::triggered, this, [this](bool checked) {
634 635 636 637 638 639
            KdenliveSettings::setAlwaysShowMonitorAudio(checked);
            m_glMonitor->rootObject()->setProperty("permanentAudiothumb", checked);
        });
        alwaysShowAudio->setChecked(KdenliveSettings::alwaysShowMonitorAudio());
        m_contextMenu->addAction(alwaysShowAudio);
        m_configMenu->addAction(alwaysShowAudio);
640 641
    }

Laurent Montel's avatar
Laurent Montel committed
642
    if (overlayMenu) {
643
        m_contextMenu->addMenu(overlayMenu);
Laurent Montel's avatar
Laurent Montel committed
644
    }
645

646 647
    QAction *switchAudioMonitor = m_configMenu->addAction(i18n("Show Audio Levels"), this, SLOT(slotSwitchAudioMonitor()));
    switchAudioMonitor->setCheckable(true);
Nicolas Carion's avatar
Nicolas Carion committed
648
    switchAudioMonitor->setChecked((KdenliveSettings::monitoraudio() & m_id) != 0);
649

650 651 652
    // For some reason, the frame in QAbstracSpinBox (base class of TimeCodeDisplay) needs to be displayed once, then hidden
    // or it will never appear (supposed to appear on hover).
    m_timePos->setFrame(false);
653 654
}

655 656 657 658 659 660
void Monitor::slotGoToMarker(QAction *action)
{
    int pos = action->data().toInt();
    slotSeek(pos);
}

661
void Monitor::slotForceSize(QAction *a)
662
{
663 664 665 666 667
    int resizeType = a->data().toInt();
    int profileWidth = 320;
    int profileHeight = 200;
    if (resizeType > 0) {
        // calculate size
Vincent Pinon's avatar
Vincent Pinon committed
668
        QRect r = QApplication::primaryScreen()->geometry();
669
        profileHeight = m_glMonitor->profileSize().height() * resizeType / 100;
670
        profileWidth = pCore->getCurrentProfile()->dar() * profileHeight;
671 672
        if (profileWidth > r.width() * 0.8 || profileHeight > r.height() * 0.7) {
            // reset action to free resize
Nicolas Carion's avatar
format  
Nicolas Carion committed
673
            const QList<QAction *> list = m_forceSize->actions();
674
            for (QAction *ac : list) {
675
                if (ac->data().toInt() == m_forceSizeFactor) {
676 677 678 679
                    m_forceSize->setCurrentAction(ac);
                    break;
                }
            }
680
            warningMessage(i18n("Your screen resolution is not sufficient for this action"));
681 682
            return;
        }
683
    }
684
    switch (resizeType) {
Laurent Montel's avatar
Laurent Montel committed
685 686 687 688
    case 100:
    case 50:
        // resize full size
        setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
689
        profileHeight += m_glMonitor->m_displayRulerHeight;
Laurent Montel's avatar
Laurent Montel committed
690 691
        m_videoWidget->setMinimumSize(profileWidth, profileHeight);
        m_videoWidget->setMaximumSize(profileWidth, profileHeight);
692
        setMinimumSize(QSize(profileWidth, profileHeight + m_toolbar->height()));
Laurent Montel's avatar
Laurent Montel committed
693 694
        break;
    default:
695 696
        // Free resize
        m_videoWidget->setMinimumSize(profileWidth, profileHeight);
697
        m_videoWidget->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
698
        setMinimumSize(QSize(profileWidth, profileHeight + m_toolbar->height() + m_glMonitor->getControllerProxy()->rulerHeight()));
699
        setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
700
        break;
701
    }
702
    m_forceSizeFactor = resizeType;
703 704 705
    updateGeometry();
}

706

707
void Monitor::updateMarkers()
708
{
709
    if (m_controller) {
710
        m_markerMenu->clear();