monitor.cpp 87.5 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"
Julius Künzel's avatar
Julius Künzel committed
34
#include "monitormanager.h"
35
#include "profiles/profilemodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
36
#include "project/projectmanager.h"
37
#include "qmlmanager.h"
38
#include "recmanager.h"
39
#include "jobs/cuttask.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
#include <KMessageWidget>
49
#include <KMessageBox>
Nicolas Carion's avatar
Nicolas Carion committed
50
51
#include <KRecentDirs>
#include <KSelectAction>
52
#include <KWindowConfig>
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
        break;
    }
108
    if (event->type() == QEvent::KeyPress) {
Nicolas Carion's avatar
Nicolas Carion committed
109
        auto *ev = static_cast<QKeyEvent *>(event);
110
111
112
113
114
        if (ev) {
            emit doKeyPressEvent(ev);
            return true;
        }
    }
115
116
117
    default:
        break;
    }
118
119
    return QObject::eventFilter(obj, event);
}
120

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

Laurent Montel's avatar
Laurent Montel committed
156
    m_videoWidget = QWidget::createWindowContainer(qobject_cast<QWindow *>(m_glMonitor));
157
    m_videoWidget->setAcceptDrops(true);
Nicolas Carion's avatar
Nicolas Carion committed
158
    auto *leventEater = new QuickEventEater(this);
159
    m_videoWidget->installEventFilter(leventEater);
160
    connect(leventEater, &QuickEventEater::addEffect, this, &Monitor::slotAddEffect);
161
    connect(leventEater, &QuickEventEater::doKeyPressEvent, this, &Monitor::doKeyPressEvent);
162

163
164
165
    m_qmlManager = new QmlManager(m_glMonitor);
    connect(m_qmlManager, &QmlManager::effectChanged, this, &Monitor::effectChanged);
    connect(m_qmlManager, &QmlManager::effectPointsChanged, this, &Monitor::effectPointsChanged);
166
    connect(m_qmlManager, &QmlManager::activateTrack, this, &Monitor::activateTrack);
167

168
    glayout->addWidget(m_videoWidget, 0, 0);
169
170
171
172
173
174
    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
175
176
    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
177
    connect(m_glMonitor, &GLWidget::frameDisplayed, this, &Monitor::onFrameDisplayed, Qt::DirectConnection);
178
    connect(m_glMonitor, &GLWidget::mouseSeek, this, &Monitor::slotMouseSeek);
Vincent Pinon's avatar
Vincent Pinon committed
179
    connect(m_glMonitor, &GLWidget::monitorPlay, this, &Monitor::slotPlay);
Laurent Montel's avatar
Laurent Montel committed
180
    connect(m_glMonitor, &GLWidget::startDrag, this, &Monitor::slotStartDrag);
Vincent Pinon's avatar
Vincent Pinon committed
181
    connect(m_glMonitor, &GLWidget::switchFullScreen, this, &Monitor::slotSwitchFullScreen);
Laurent Montel's avatar
Laurent Montel committed
182
    connect(m_glMonitor, &GLWidget::zoomChanged, this, &Monitor::setZoom);
183
    connect(m_glMonitor, SIGNAL(lockMonitor(bool)), this, SLOT(slotLockMonitor(bool)), Qt::DirectConnection);
Laurent Montel's avatar
Laurent Montel committed
184
    connect(m_glMonitor, &GLWidget::showContextMenu, this, &Monitor::slotShowMenu);
185
    connect(m_glMonitor, &GLWidget::gpuNotSupported, this, &Monitor::gpuError);
186

187
188
    m_glWidget->setMinimumSize(QSize(320, 180));
    layout->addWidget(m_glWidget, 10);
189
190
191
    layout->addStretch();

    // Tool bar buttons
192
    m_toolbar = new QToolBar(this);
193
194
195
    int size = style()->pixelMetric(QStyle::PM_SmallIconSize);
    QSize iconSize(size, size);
    m_toolbar->setIconSize(iconSize);
196

197
    auto *scalingAction = new QComboBox(this);
198
199
200
    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
201
    connect(scalingAction, QOverload<int>::of(&QComboBox::activated), this, [this] (int index) {
202
203
204
205
        switch (index) {
            case 1:
                KdenliveSettings::setPreviewScaling(2);
                break;
206
207
208
            case 2:
                KdenliveSettings::setPreviewScaling(4);
                break;
209
            case 3:
210
211
                KdenliveSettings::setPreviewScaling(8);
                break;
212
            case 4:
213
214
215
                KdenliveSettings::setPreviewScaling(16);
                break;
            default:
216
                KdenliveSettings::setPreviewScaling(0);
217
        }
Vincent Pinon's avatar
Vincent Pinon committed
218
219
        emit m_monitorManager->scalingChanged();
        emit m_monitorManager->updatePreviewScaling();
220
    });
221

Vincent Pinon's avatar
Vincent Pinon committed
222
    connect(manager, &MonitorManager::updatePreviewScaling, this, [this, scalingAction]() {
223
        bool scalingChanged = m_glMonitor->updateScaling();
224
225
        switch (KdenliveSettings::previewScaling()) {
            case 2:
226
                scalingAction->setCurrentIndex(1);
227
228
                break;
            case 4:
229
                scalingAction->setCurrentIndex(2);
230
231
                break;
            case 8:
232
                scalingAction->setCurrentIndex(3);
233
234
                break;
            case 16:
235
                scalingAction->setCurrentIndex(4);
236
237
                break;
            default:
238
                scalingAction->setCurrentIndex(0);
239
240
                break;
        }
241
242
243
        if (scalingChanged) {
            refreshMonitorIfActive();
        }
244
    });
245
246
247
248
    scalingAction->setFrame(false);
    scalingAction->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
    m_toolbar->addWidget(scalingAction);
    m_toolbar->addSeparator();
249

250
251
    if (id == Kdenlive::ClipMonitor) {
        // Add options for recording
252
        m_recManager = new RecManager(this);
Vincent Pinon's avatar
Vincent Pinon committed
253
        connect(m_recManager, &RecManager::warningMessage, this, &Monitor::warningMessage);
254
        connect(m_recManager, &RecManager::addClipToProject, this, &Monitor::addClipToProject);
255

256
        m_toolbar->addAction(manager->getAction(QStringLiteral("insert_project_tree")));
257
        m_toolbar->setToolTip(i18n("Insert Zone to Project Bin"));
258
        m_toolbar->addSeparator();
259
260
261
262
        m_streamsButton = new QToolButton(this);
        m_streamsButton->setPopupMode(QToolButton::InstantPopup);
        m_streamsButton->setIcon(QIcon::fromTheme(QStringLiteral("speaker")));
        m_streamAction = m_toolbar->addWidget(m_streamsButton);
263
        m_audioChannels = new QMenu(this);
264
265
        m_streamsButton->setMenu(m_audioChannels);
        m_streamAction->setVisible(false);
Vincent Pinon's avatar
Vincent Pinon committed
266
        connect(m_audioChannels, &QMenu::triggered, this, [this] (QAction *ac) {
267
            //m_audioChannels->show();
268
269
            QList <QAction*> actions = m_audioChannels->actions();
            QMap <int, QString> enabledStreams;
270
271
272
273
274
275
            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
276
                for (auto act : qAsConst(actions)) {
277
278
279
280
281
                    if (act->isChecked() && act != ac) {
                        act->setChecked(false);
                    }
                }
            } else {
Vincent Pinon's avatar
Vincent Pinon committed
282
                for (auto act : qAsConst(actions)) {
283
284
285
286
287
288
289
290
291
                    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('&')));
                        }
                    }
292
293
294
295
296
297
298
299
                }
            }
            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
300
                for (const int st : qAsConst(streams)) {
301
302
303
304
305
306
307
308
309
310
311
312
                    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
313
    } else if (id == Kdenlive::ProjectMonitor) {
314
        connect(m_glMonitor, &GLWidget::paused, m_monitorManager, &MonitorManager::cleanMixer);
315
316
    }

Julius Künzel's avatar
Julius Künzel committed
317
318
319
320
321
322
323
324
325
326
327
328
329

    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);
    connect(markIn, &QAction::triggered, this, [&, manager]() {
        m_monitorManager->activateMonitor(m_id);
        manager->getAction(QStringLiteral("mark_in"))->trigger();
    });
    connect(markOut, &QAction::triggered, this, [&, manager]() {
        m_monitorManager->activateMonitor(m_id);
        manager->getAction(QStringLiteral("mark_out"))->trigger();
    });
330
331
332
333
    // 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);
334

Nicolas Carion's avatar
Nicolas Carion committed
335
    auto *playButton = new QToolButton(m_toolbar);
336
    m_playMenu = new QMenu(i18n("Play..."), this);
337
    connect(m_playMenu, &QMenu::aboutToShow, this, &Monitor::slotActivateMonitor);
Laurent Montel's avatar
Laurent Montel committed
338
    QAction *originalPlayAction = static_cast<KDualAction *>(manager->getAction(QStringLiteral("monitor_play")));
339
    m_playAction = new KDualAction(i18n("Play"), i18n("Pause"), this);
340
341
    m_playAction->setInactiveIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
    m_playAction->setActiveIcon(QIcon::fromTheme(QStringLiteral("media-playback-pause")));
342

Laurent Montel's avatar
Laurent Montel committed
343
    QString strippedTooltip = m_playAction->toolTip().remove(QRegExp(QStringLiteral("\\s\\(.*\\)")));
344
345
    // append shortcut if it exists for action
    if (originalPlayAction->shortcut() == QKeySequence(0)) {
Laurent Montel's avatar
Laurent Montel committed
346
347
        m_playAction->setToolTip(strippedTooltip);
    } else {
348
        m_playAction->setToolTip(strippedTooltip + QStringLiteral(" (") + originalPlayAction->shortcut().toString() + QLatin1Char(')'));
349
    }
350
    m_playMenu->addAction(m_playAction);
351
    connect(m_playAction, &QAction::triggered, this, &Monitor::slotSwitchPlay);
352
353
354

    playButton->setMenu(m_playMenu);
    playButton->setPopupMode(QToolButton::MenuButtonPopup);
355
    m_toolbar->addWidget(playButton);
356
357
358
359

    // 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
360
    connect(forward, &QAction::triggered, this, [this]() {
361
362
        Monitor::slotForward();
    });
363
364

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

Julius Künzel's avatar
Julius Künzel committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385

    if (id == Kdenlive::ClipMonitor) {
        m_markerMenu = new QMenu(i18n("Go to marker..."), this);
    } else {
        m_markerMenu = new QMenu(i18n("Go to guide..."), this);
    }
    m_markerMenu->setEnabled(false);
    m_configMenu->addMenu(m_markerMenu);
    connect(m_markerMenu, &QMenu::triggered, this, &Monitor::slotGoToMarker);
    m_forceSize = new KSelectAction(QIcon::fromTheme(QStringLiteral("transform-scale")), i18n("Force Monitor Size"), this);
    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);
    connect(m_forceSize, static_cast<void (KSelectAction::*)(QAction *)>(&KSelectAction::triggered), this, &Monitor::slotForceSize);
386

387
388
389
    // Create Volume slider popup
    m_audioSlider = new QSlider(Qt::Vertical);
    m_audioSlider->setRange(0, 100);
390
    m_audioSlider->setValue(KdenliveSettings::volume());
391
    connect(m_audioSlider, &QSlider::valueChanged, this, &Monitor::slotSetVolume);
Nicolas Carion's avatar
Nicolas Carion committed
392
    auto *widgetslider = new QWidgetAction(this);
393
394
    widgetslider->setText(i18n("Audio volume"));
    widgetslider->setDefaultWidget(m_audioSlider);
395
396
    auto *menu = new QMenu(i18n("Volume"), this);
    menu->setIcon(QIcon::fromTheme(QStringLiteral("audio-volume-medium")));
397
    menu->addAction(widgetslider);
398
    m_configMenu->addMenu(menu);
399

400
    /*QIcon icon;
Laurent Montel's avatar
Laurent Montel committed
401
    if (KdenliveSettings::volume() == 0) {
402
        icon = QIcon::fromTheme(QStringLiteral("audio-volume-muted"));
Laurent Montel's avatar
Laurent Montel committed
403
    } else {
404
        icon = QIcon::fromTheme(QStringLiteral("audio-volume-medium"));
Laurent Montel's avatar
Laurent Montel committed
405
    }
406
    m_audioButton->setIcon(icon);*/
407

408
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
409
410
    setLayout(layout);
    setMinimumHeight(200);
411

412
413
    connect(this, &Monitor::scopesClear, m_glMonitor, &GLWidget::releaseAnalyse, Qt::DirectConnection);
    connect(m_glMonitor, &GLWidget::analyseFrame, this, &Monitor::frameUpdated);
414
    m_timePos = new TimecodeDisplay(pCore->timecode(), this);
415

416
    if (id == Kdenlive::ProjectMonitor) {
Nicolas Carion's avatar
linting    
Nicolas Carion committed
417
418
        // TODO: reimplement
        // connect(render, &Render::durationChanged, this, &Monitor::durationChanged);
419
        connect(m_glMonitor->getControllerProxy(), &MonitorProxy::saveZone, this, &Monitor::zoneUpdated);
420
        connect(m_glMonitor->getControllerProxy(), &MonitorProxy::saveZoneWithUndo, this, &Monitor::zoneUpdatedWithUndo);
421
    } else if (id == Kdenlive::ClipMonitor) {
422
        connect(m_glMonitor->getControllerProxy(), &MonitorProxy::saveZone, this, &Monitor::updateClipZone);
423
    }
424
    m_glMonitor->getControllerProxy()->setTimeCode(m_timePos);
425
    connect(m_glMonitor->getControllerProxy(), &MonitorProxy::triggerAction, pCore.get(), &Core::triggerAction);
426
427
428
    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);
429
    connect(m_glMonitor->getControllerProxy(), &MonitorProxy::seekToKeyframe, this, &Monitor::slotSeekToKeyFrame);
430

431
    m_toolbar->addAction(manager->getAction(QStringLiteral("monitor_editmode")));
432

433
434
435
    m_toolbar->addSeparator();
    m_toolbar->addWidget(m_timePos);

Nicolas Carion's avatar
Nicolas Carion committed
436
    auto *configButton = new QToolButton(m_toolbar);
437
    configButton->setIcon(QIcon::fromTheme(QStringLiteral("kdenlive-menu")));
438
    configButton->setToolTip(i18n("Options"));
439
    configButton->setMenu(m_configMenu);
440
    configButton->setPopupMode(QToolButton::InstantPopup);
441
    m_toolbar->addWidget(configButton);
442
    m_toolbar->addSeparator();
443
444
    QMargins mrg = m_toolbar->contentsMargins();
    m_audioMeterWidget = new MonitorAudioLevel(m_toolbar->height() - mrg.top() - mrg.bottom(), this);
445
    m_toolbar->addWidget(m_audioMeterWidget);
446
    if (!m_audioMeterWidget->isValid) {
447
448
449
450
        KdenliveSettings::setMonitoraudio(0x01);
        m_audioMeterWidget->setVisibility(false);
    } else {
        m_audioMeterWidget->setVisibility((KdenliveSettings::monitoraudio() & m_id) != 0);
451
    }
452

453
    connect(m_timePos, SIGNAL(timeCodeEditingFinished()), this, SLOT(slotSeek()));
454
    layout->addWidget(m_toolbar);
Laurent Montel's avatar
Laurent Montel committed
455
456
457
    if (m_recManager) {
        layout->addWidget(m_recManager->toolbar());
    }
458
459

    // Load monitor overlay qml
460
    loadQmlScene(MonitorSceneDefault);
461

462
463
464
465
466
    // Monitor dropped fps timer
    m_droppedTimer.setInterval(1000);
    m_droppedTimer.setSingleShot(false);
    connect(&m_droppedTimer, &QTimer::timeout, this, &Monitor::checkDrops);

467
468
469
470
    // Info message widget
    m_infoMessage = new KMessageWidget(this);
    layout->addWidget(m_infoMessage);
    m_infoMessage->hide();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
471
472
}

473
474
Monitor::~Monitor()
{
475
    delete m_audioMeterWidget;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
476
    delete m_glMonitor;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
477
478
    delete m_videoWidget;
    delete m_glWidget;
479
480
481
    delete m_timePos;
}

482
483
484
485
486
487
488
489
490
491
void Monitor::setOffsetX(int x)
{
    m_glMonitor->setOffsetX(x, m_horizontalScroll->maximum());
}

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

492
void Monitor::slotGetCurrentImage(bool request)
493
{
494
    m_glMonitor->sendFrameForAnalysis = request;
495
    refreshMonitorIfActive(true);
496
497
    if (request) {
        // Update analysis state
Laurent Montel's avatar
Laurent Montel committed
498
        QTimer::singleShot(500, m_monitorManager, &MonitorManager::checkScopes);
499
500
501
    } else {
        m_glMonitor->releaseAnalyse();
    }
502
503
}

504
void Monitor::slotAddEffect(const QStringList &effect)
505
506
{
    if (m_id == Kdenlive::ClipMonitor) {
Laurent Montel's avatar
Laurent Montel committed
507
        if (m_controller) {
508
            emit addMasterEffect(m_controller->AbstractProjectItem::clipId(), effect);
Laurent Montel's avatar
Laurent Montel committed
509
510
511
        }
    } else {
        emit addEffect(effect);
512
513
514
    }
}

515
516
517
518
519
520
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
521
522
523
        if (ic.isNull() || ic.name().isEmpty()) {
            continue;
        }
524
        QIcon newIcon = QIcon::fromTheme(ic.name());
525
526
527
528
529
530
        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
531
532
533
        if (ic.isNull() || ic.name().isEmpty()) {
            continue;
        }
534
        QIcon newIcon = QIcon::fromTheme(ic.name());
535
536
        m->setActiveIcon(newIcon);
        ic = m->inactiveIcon();
Laurent Montel's avatar
Laurent Montel committed
537
538
539
        if (ic.isNull() || ic.name().isEmpty()) {
            continue;
        }
540
        newIcon = QIcon::fromTheme(ic.name());
541
542
543
544
        m->setInactiveIcon(newIcon);
    }
}

545
QAction *Monitor::recAction()
546
{
Laurent Montel's avatar
Laurent Montel committed
547
    if (m_recManager) {
548
        return m_recManager->recAction();
Laurent Montel's avatar
Laurent Montel committed
549
    }
Laurent Montel's avatar
Laurent Montel committed
550
    return nullptr;
551
552
}

553
554
555
556
557
void Monitor::slotLockMonitor(bool lock)
{
    m_monitorManager->lockMonitor(m_id, lock);
}

558
void Monitor::setupMenu(QMenu *goMenu, QMenu *overlayMenu, QAction *playZone, QAction *loopZone, QMenu *markerMenu, QAction *loopClip)
559
{
560
    delete m_contextMenu;
561
    m_contextMenu = new QMenu(this);
562
    m_contextMenu->addMenu(m_playMenu);
Laurent Montel's avatar
Laurent Montel committed
563
    if (goMenu) {
564
        m_contextMenu->addMenu(goMenu);
Laurent Montel's avatar
Laurent Montel committed
565
    }
566

567
    if (markerMenu) {
568
        m_contextMenu->addMenu(markerMenu);
Nicolas Carion's avatar
Nicolas Carion committed
569
        QList<QAction *> list = markerMenu->actions();
570
        for (int i = 0; i < list.count(); ++i) {
571
            if (list.at(i)->objectName() == QLatin1String("edit_marker")) {
572
573
574
575
576
                m_editMarker = list.at(i);
                break;
            }
        }
    }
577

578
579
    m_playMenu->addAction(playZone);
    m_playMenu->addAction(loopZone);
580
581
582
583
    if (loopClip) {
        m_loopClipAction = loopClip;
        m_playMenu->addAction(loopClip);
    }
584

585
    m_contextMenu->addMenu(m_markerMenu);
586
    if (m_id == Kdenlive::ClipMonitor) {
587
        //m_contextMenu->addAction(QIcon::fromTheme(QStringLiteral("document-save")), i18n("Save zone"), this, SLOT(slotSaveZone()));
Nicolas Carion's avatar
Nicolas Carion committed
588
        QAction *extractZone =
589
            m_configMenu->addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18n("Extract Zone"), this, SLOT(slotExtractCurrentZone()));
590
        m_contextMenu->addAction(extractZone);
591
        m_contextMenu->addAction(m_monitorManager->getAction(QStringLiteral("insert_project_tree")));
592
    }
593
594
    m_contextMenu->addAction(m_monitorManager->getAction(QStringLiteral("extract_frame")));
    m_contextMenu->addAction(m_monitorManager->getAction(QStringLiteral("extract_frame_to_project")));
595
    m_contextMenu->addAction(m_monitorManager->getAction(QStringLiteral("add_project_note")));
596

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
597
    if (m_id == Kdenlive::ProjectMonitor) {
598
        m_contextMenu->addAction(m_monitorManager->getAction(QStringLiteral("monitor_multitrack")));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
599
    } else if (m_id == Kdenlive::ClipMonitor) {
600
601
        QAction *setThumbFrame =
            m_contextMenu->addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18n("Set current image as thumbnail"), this, SLOT(slotSetThumbFrame()));
602
        m_configMenu->addAction(setThumbFrame);
603
604
605
        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
606
        connect(alwaysShowAudio, &QAction::triggered, this, [this](bool checked) {
607
608
609
610
611
612
            KdenliveSettings::setAlwaysShowMonitorAudio(checked);
            m_glMonitor->rootObject()->setProperty("permanentAudiothumb", checked);
        });
        alwaysShowAudio->setChecked(KdenliveSettings::alwaysShowMonitorAudio());
        m_contextMenu->addAction(alwaysShowAudio);
        m_configMenu->addAction(alwaysShowAudio);
613
614
    }

Laurent Montel's avatar
Laurent Montel committed
615
    if (overlayMenu) {
616
        m_contextMenu->addMenu(overlayMenu);
Laurent Montel's avatar
Laurent Montel committed
617
    }
618

619
620
    QAction *switchAudioMonitor = m_configMenu->addAction(i18n("Show Audio Levels"), this, SLOT(slotSwitchAudioMonitor()));
    switchAudioMonitor->setCheckable(true);
Nicolas Carion's avatar
Nicolas Carion committed
621
    switchAudioMonitor->setChecked((KdenliveSettings::monitoraudio() & m_id) != 0);
622
623
    
    if (m_id == Kdenlive::ClipMonitor) {
624
        QAction *recordTimecode = new QAction(i18n("Show Source Timecode"), this);
625
626
627
628
629
        recordTimecode->setCheckable(true);
        connect(recordTimecode, &QAction::triggered, this, &Monitor::slotSwitchRecTimecode);
        recordTimecode->setChecked(KdenliveSettings::rectimecode());
        m_configMenu->addAction(recordTimecode);
    }
630

631
632
633
    // 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);
634
635
}

636
637
638
639
640
641
void Monitor::slotGoToMarker(QAction *action)
{
    int pos = action->data().toInt();
    slotSeek(pos);
}

642
void Monitor::slotForceSize(QAction *a)
643
{
644
645
646
647
648
    int resizeType = a->data().toInt();
    int profileWidth = 320;
    int profileHeight = 200;
    if (resizeType > 0) {
        // calculate size
Vincent Pinon's avatar
Vincent Pinon committed
649
        QRect r = QApplication::primaryScreen()->geometry();
650
        profileHeight = m_glMonitor->profileSize().height() * resizeType / 100;
Vincent Pinon's avatar
Vincent Pinon committed
651
        profileWidth = int(pCore->getCurrentProfile()->dar() * profileHeight);
652
653
        if (profileWidth > r.width() * 0.8 || profileHeight > r.height() * 0.7) {
            // reset action to free resize
Nicolas Carion's avatar
format    
Nicolas Carion committed
654
            const QList<QAction *> list = m_forceSize->actions();
655
            for (QAction *ac : list) {
656
                if (ac->data().toInt() == m_forceSizeFactor) {
657
658
659
660
                    m_forceSize->setCurrentAction(ac);
                    break;
                }
            }
661
            warningMessage(i18n("Your screen resolution is not sufficient for this action"));
662
663
            return;
        }
664
    }
665
    switch (resizeType) {
Laurent Montel's avatar
Laurent Montel committed
666
667
668
669
    case 100:
    case 50:
        // resize full size
        setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
670
        profileHeight += m_glMonitor->m_displayRulerHeight;
Laurent Montel's avatar
Laurent Montel committed
671
672
        m_videoWidget->setMinimumSize(profileWidth, profileHeight);
        m_videoWidget->setMaximumSize(profileWidth, profileHeight);
673
        setMinimumSize(QSize(profileWidth, profileHeight + m_toolbar->height()));
Laurent Montel's avatar
Laurent Montel committed
674
675
        break;
    default:
676
677
        // Free resize
        m_videoWidget->setMinimumSize(profileWidth, profileHeight);
678
        m_videoWidget->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
679
        setMinimumSize(QSize(profileWidth, profileHeight + m_toolbar->height() + m_glMonitor->getControllerProxy()->rulerHeight()));
680
        setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
681
        break;
682
    }
683
    m_forceSizeFactor = resizeType;
684
685
686
    updateGeometry();
}

687

688
void Monitor::updateMarkers()
689
{
690
    if (m_controller) {
691
        m_markerMenu->clear();
692
        QList<CommentedTime> markers = m_controller->getMarkerModel()->getAllMarkers();
693
        if (!markers.isEmpty()) {
694
            for (int i = 0; i < markers.count(); ++i) {
Vincent Pinon's avatar
Vincent Pinon committed
695
                int pos = markers.at(i).time().frames(pCore->getCurrentFps());
696
                QString position = pCore->timecode().getTimecode(markers.at(i).time()) + QLatin1Char(' ') + markers.at(i).comment();
697
698
699
                QAction *go = m_markerMenu->addAction(position);
                go->setData(pos);
            }
700
        }
701
702
703
704
        m_markerMenu->setEnabled(!m_markerMenu->isEmpty());
    }
}

705
706
707
708

void Monitor::updateDocumentUuid()
{
    m_glMonitor->rootObject()->setProperty("documentId", pCore->currentDoc()->uuid);
709
710
}

711
712
void Monitor::slotSeekToPreviousSnap()
{
Laurent Montel's avatar
Laurent Montel committed
713
    if (m_controller) {
714
        m_glMonitor->getControllerProxy()->setPosition(getSnapForPos(true).frames(pCore->getCurrentFps()));
Laurent Montel's avatar
Laurent Montel committed
715
    }
716
717
}

718
719
void Monitor::slotSeekToNextSnap()
{
Laurent Montel's avatar
Laurent Montel committed
720
    if (m_controller) {
721
        m_glMonitor->getControllerProxy()->setPosition(getSnapForPos(false).frames(pCore->getCurrentFps()));
Laurent Montel's avatar
Laurent Montel committed
722
    }
723
724
}

725
int Monitor::position()
726
{
727
    return m_glMonitor->getControllerProxy()->getPosition();
728
729
}

730
731
GenTime Monitor::getSnapForPos(bool previous)
{
732
    int frame = previous ? m_snaps->getPreviousPoint(m_glMonitor->getCurrentPos()) : m_snaps->getNextPoint(m_glMonitor->getCurrentPos());
Nicolas Carion's avatar
Nicolas Carion committed
733
    return {frame, pCore->getCurrentFps()};
734
735
}

736
void Monitor::slotLoadClipZone(const QPoint &zone)
737
{
738
    m_glMonitor->getControllerProxy()->setZone(zone.x(), zone.y(), false);
739
    checkOverlay();
740
741
}

742
743
void Monitor::slotSetZoneStart()
{
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
    QPoint oldZone = m_glMonitor->getControllerProxy()->zone();
    int currentIn = m_glMonitor->getCurrentPos();
    int updatedZoneOut = -1;
    if (currentIn > oldZone.y()) {
        updatedZoneOut = qMin(m_glMonitor->duration(), currentIn + (oldZone.y() - oldZone.x()));
    }
    
    Fun undo_zone = [this, oldZone, updatedZoneOut]() {
            m_glMonitor->getControllerProxy()->setZoneIn(oldZone.x());
            if (updatedZoneOut > -1) {
                m_glMonitor->getControllerProxy()->setZoneOut(oldZone.y());
            }
            checkOverlay();
            return true;
    };
    Fun redo_zone = [this, currentIn, updatedZoneOut]() {
            if (updatedZoneOut > -1) {
                m_glMonitor->getControllerProxy()->setZoneOut(updatedZoneOut);
            }
            m_glMonitor->getControllerProxy()->setZoneIn(currentIn);
            checkOverlay();
            return true;
    };
    redo_zone();
    pCore->pushUndo(undo_zone, redo_zone, i18n("Set Zone"));
769
770
}

771
void Monitor::slotSetZoneEnd()
772
{
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
    QPoint oldZone = m_glMonitor->getControllerProxy()->zone();
    int currentOut = m_glMonitor->getCurrentPos() + 1;
    int updatedZoneIn = -1;
    if (currentOut < oldZone.x()) {
        updatedZoneIn = qMax(0, currentOut - (oldZone.y() - oldZone.x()));
    }
    Fun undo_zone = [this, oldZone, updatedZoneIn]() {
            m_glMonitor->getControllerProxy()->setZoneOut(oldZone.y());
            if (updatedZoneIn > -1) {
                m_glMonitor->getControllerProxy()->setZoneIn(oldZone.x());
            }
            checkOverlay();
            return true;
    };
    
    Fun redo_zone = [this, currentOut, updatedZoneIn]() {
            if (updatedZoneIn > -1) {
                m_glMonitor->getControllerProxy()->setZoneIn(updatedZoneIn);
            }
            m_glMonitor->getControllerProxy()->setZoneOut(currentOut);
            checkOverlay();
            return true;
    };
    redo_zone();
    pCore->pushUndo(undo_zone, redo_zone, i18n("Set Zone"));
798
799
}

800
// virtual
Laurent Montel's avatar
Laurent Montel committed
801
void Monitor::mousePressEvent(QMouseEvent *event)
802
{
803
    m_monitorManager->activateMonitor(m_id);
Nicolas Carion's avatar
Nicolas Carion committed
804
    if ((event->button() & Qt::RightButton) == 0u) {
805
        if (m_glWidget->geometry().contains(event->pos())) {
806
            m_DragStartPosition = event->pos();
807
            event->accept();
808
        }
809
    } else if (m_contextMenu) {
810
        slotActivateMonitor();
811
        m_contextMenu->popup(event->globalPos());
812
        event->accept();
813
    }
814
    QWidget::mousePressEvent(event);
815
816
}

817
818
void Monitor::slotShowMenu(const QPoint pos)
{
819
    slotActivateMonitor();
Laurent Montel's avatar
Laurent Montel committed
820
    if (m_contextMenu) {
821
822
823
824
825
826
827
828
829
830
831
        if (m_markerMenu) {
            // Fill guide menu
            m_markerMenu->clear();
            std::shared_ptr<MarkerListModel> model;
            if (m_id == Kdenlive::ClipMonitor && m_controller) {
                model = m_controller->getMarkerModel();
            } else if (m_id == Kdenlive::ProjectMonitor && pCore->currentDoc()) {
                model = pCore->currentDoc()->getGuideModel();
            }
            if (model) {
                QList<CommentedTime> markersList = model->getAllMarkers();
Vincent Pinon's avatar
Vincent Pinon committed
832
                for (const CommentedTime &mkr : qAsConst(markersList)) {
833
834
835
836
837
838
                    QAction *a = m_markerMenu->addAction(mkr.comment());
                    a->setData(mkr.time().frames(pCore->getCurrentFps()));
                }
            }
            m_markerMenu->setEnabled(!m_markerMenu->isEmpty());
        }
Laurent Montel's avatar
Laurent Montel committed
839
840
        m_contextMenu->popup(pos);
    }