monitor.cpp 71.2 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"
Nicolas Carion's avatar
Nicolas Carion committed
23 24 25 26
#include "core.h"
#include "dialogs/profilesdialog.h"
#include "doc/kdenlivedoc.h"
#include "doc/kthumb.h"
27
#include "glwidget.h"
Nicolas Carion's avatar
Nicolas Carion committed
28 29 30 31
#include "kdenlivesettings.h"
#include "lib/audio/audioStreamInfo.h"
#include "mainwindow.h"
#include "mltcontroller/bincontroller.h"
Nicolas Carion's avatar
Nicolas Carion committed
32
#include "mltcontroller/clip.h"
Nicolas Carion's avatar
Nicolas Carion committed
33 34
#include "mltcontroller/clipcontroller.h"
#include "project/projectmanager.h"
35
#include "qmlmanager.h"
36
#include "recmanager.h"
37
#include "monitorproxy.h"
38
#include "scopes/monitoraudiolevel.h"
39
#include "timeline2/model/snapmodel.hpp"
40
#include "transitions/transitionsrepository.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
41
#include "utils/KoIconUtils.h"
42

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

Laurent Montel's avatar
Laurent Montel committed
53
#include "kdenlive_debug.h"
54
#include <QDesktopWidget>
55
#include <QDrag>
56
#include <QFileDialog>
Nicolas Carion's avatar
Nicolas Carion committed
57
#include <QMenu>
58
#include <QMimeData>
Nicolas Carion's avatar
Nicolas Carion committed
59
#include <QMouseEvent>
60
#include <QQuickItem>
61
#include <QScrollBar>
Nicolas Carion's avatar
Nicolas Carion committed
62 63 64 65
#include <QSlider>
#include <QToolBar>
#include <QToolButton>
#include <QVBoxLayout>
66
#include <QWidgetAction>
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
67

68 69
#define SEEK_INACTIVE (-1)

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

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

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

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

Nicolas Carion's avatar
Nicolas Carion committed
130
Monitor::Monitor(Kdenlive::MonitorId id, MonitorManager *manager, QWidget *parent)
131 132 133
    : AbstractMonitor(id, manager, parent)
    , m_controller(nullptr)
    , m_glMonitor(nullptr)
134
    , m_snaps(new SnapModel())
135 136 137 138 139 140 141 142 143 144 145 146
    , m_splitEffect(nullptr)
    , m_splitProducer(nullptr)
    , m_dragStarted(false)
    , m_recManager(nullptr)
    , m_loopClipAction(nullptr)
    , m_sceneVisibilityAction(nullptr)
    , m_multitrackView(nullptr)
    , m_contextMenu(nullptr)
    , m_loopClipTransition(true)
    , m_editMarker(nullptr)
    , m_forceSizeFactor(0)
    , m_lastMonitorSceneType(MonitorSceneDefault)
147
{
Nicolas Carion's avatar
Nicolas Carion committed
148
    auto *layout = new QVBoxLayout;
149
    layout->setContentsMargins(0, 0, 0, 0);
150
    layout->setSpacing(0);
151

152 153
    // Create container widget
    m_glWidget = new QWidget;
Nicolas Carion's avatar
Nicolas Carion committed
154
    auto *glayout = new QGridLayout(m_glWidget);
155 156 157
    glayout->setSpacing(0);
    glayout->setContentsMargins(0, 0, 0, 0);
    // Create QML OpenGL widget
Nicolas Carion's avatar
Nicolas Carion committed
158
    m_glMonitor = new GLWidget((int)id);
Laurent Montel's avatar
Laurent Montel committed
159
    connect(m_glMonitor, &GLWidget::passKeyEvent, this, &Monitor::doKeyPressEvent);
160
    connect(m_glMonitor, &GLWidget::panView, this, &Monitor::panView);
161
    connect(m_glMonitor, &GLWidget::seekPosition, this, &Monitor::slotSeekPosition, Qt::DirectConnection);
162
    connect(m_glMonitor, &GLWidget::activateMonitor, this, &AbstractMonitor::slotActivateMonitor, Qt::DirectConnection);
Laurent Montel's avatar
Laurent Montel committed
163
    m_videoWidget = QWidget::createWindowContainer(qobject_cast<QWindow *>(m_glMonitor));
164
    m_videoWidget->setAcceptDrops(true);
Nicolas Carion's avatar
Nicolas Carion committed
165
    auto *leventEater = new QuickEventEater(this);
166 167 168
    m_videoWidget->installEventFilter(leventEater);
    connect(leventEater, &QuickEventEater::addEffect, this, &Monitor::slotAddEffect);

169 170 171 172
    m_qmlManager = new QmlManager(m_glMonitor);
    connect(m_qmlManager, &QmlManager::effectChanged, this, &Monitor::effectChanged);
    connect(m_qmlManager, &QmlManager::effectPointsChanged, this, &Monitor::effectPointsChanged);

Nicolas Carion's avatar
Nicolas Carion committed
173
    auto *monitorEventEater = new QuickMonitorEventEater(this);
174
    m_glWidget->installEventFilter(monitorEventEater);
Laurent Montel's avatar
Laurent Montel committed
175
    connect(monitorEventEater, &QuickMonitorEventEater::doKeyPressEvent, this, &Monitor::doKeyPressEvent);
176

177
    glayout->addWidget(m_videoWidget, 0, 0);
178 179 180 181 182 183
    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
184 185
    connect(m_horizontalScroll, &QAbstractSlider::valueChanged, this, &Monitor::setOffsetX);
    connect(m_verticalScroll, &QAbstractSlider::valueChanged, this, &Monitor::setOffsetY);
Laurent Montel's avatar
Laurent Montel committed
186
    connect(m_glMonitor, &GLWidget::frameDisplayed, this, &Monitor::onFrameDisplayed);
187
    connect(m_glMonitor, &GLWidget::mouseSeek, this, &Monitor::slotMouseSeek);
188
    connect(m_glMonitor, SIGNAL(monitorPlay()), this, SLOT(slotPlay()));
Laurent Montel's avatar
Laurent Montel committed
189
    connect(m_glMonitor, &GLWidget::startDrag, this, &Monitor::slotStartDrag);
190
    connect(m_glMonitor, SIGNAL(switchFullScreen(bool)), this, SLOT(slotSwitchFullScreen(bool)));
Laurent Montel's avatar
Laurent Montel committed
191
    connect(m_glMonitor, &GLWidget::zoomChanged, this, &Monitor::setZoom);
192
    connect(m_glMonitor, SIGNAL(lockMonitor(bool)), this, SLOT(slotLockMonitor(bool)), Qt::DirectConnection);
Laurent Montel's avatar
Laurent Montel committed
193
    connect(m_glMonitor, &GLWidget::showContextMenu, this, &Monitor::slotShowMenu);
194
    connect(m_glMonitor, &GLWidget::gpuNotSupported, this, &Monitor::gpuError);
195

196 197
    m_glWidget->setMinimumSize(QSize(320, 180));
    layout->addWidget(m_glWidget, 10);
198 199 200
    layout->addStretch();

    // Tool bar buttons
201
    m_toolbar = new QToolBar(this);
202 203 204
    QWidget *sp1 = new QWidget(this);
    sp1->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
    m_toolbar->addWidget(sp1);
205 206
    if (id == Kdenlive::ClipMonitor) {
        // Add options for recording
207
        m_recManager = new RecManager(this);
Vincent Pinon's avatar
Vincent Pinon committed
208
        connect(m_recManager, &RecManager::warningMessage, this, &Monitor::warningMessage);
209
        connect(m_recManager, &RecManager::addClipToProject, this, &Monitor::addClipToProject);
210 211
    }

212
    if (id != Kdenlive::DvdMonitor) {
213 214
        m_toolbar->addAction(manager->getAction(QStringLiteral("mark_in")));
        m_toolbar->addAction(manager->getAction(QStringLiteral("mark_out")));
215
    }
216
    m_toolbar->addAction(manager->getAction(QStringLiteral("monitor_seek_backward")));
217

Nicolas Carion's avatar
Nicolas Carion committed
218
    auto *playButton = new QToolButton(m_toolbar);
219
    m_playMenu = new QMenu(i18n("Play..."), this);
Laurent Montel's avatar
Laurent Montel committed
220
    QAction *originalPlayAction = static_cast<KDualAction *>(manager->getAction(QStringLiteral("monitor_play")));
221
    m_playAction = new KDualAction(i18n("Play"), i18n("Pause"), this);
222 223
    m_playAction->setInactiveIcon(KoIconUtils::themedIcon(QStringLiteral("media-playback-start")));
    m_playAction->setActiveIcon(KoIconUtils::themedIcon(QStringLiteral("media-playback-pause")));
224

Laurent Montel's avatar
Laurent Montel committed
225
    QString strippedTooltip = m_playAction->toolTip().remove(QRegExp(QStringLiteral("\\s\\(.*\\)")));
226 227
    // append shortcut if it exists for action
    if (originalPlayAction->shortcut() == QKeySequence(0)) {
Laurent Montel's avatar
Laurent Montel committed
228 229
        m_playAction->setToolTip(strippedTooltip);
    } else {
230
        m_playAction->setToolTip(strippedTooltip + QStringLiteral(" (") + originalPlayAction->shortcut().toString() + QLatin1Char(')'));
231
    }
232
    m_playMenu->addAction(m_playAction);
233
    connect(m_playAction, &QAction::triggered, this, &Monitor::slotSwitchPlay);
234 235 236

    playButton->setMenu(m_playMenu);
    playButton->setPopupMode(QToolButton::MenuButtonPopup);
237
    m_toolbar->addWidget(playButton);
238
    m_toolbar->addAction(manager->getAction(QStringLiteral("monitor_seek_forward")));
239 240

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

243 244
    if (id != Kdenlive::DvdMonitor) {
        if (id == Kdenlive::ClipMonitor) {
245
            m_markerMenu = new QMenu(i18n("Go to marker..."), this);
246 247
        } else {
            m_markerMenu = new QMenu(i18n("Go to guide..."), this);
248
        }
249 250
        m_markerMenu->setEnabled(false);
        m_configMenu->addMenu(m_markerMenu);
Laurent Montel's avatar
Laurent Montel committed
251
        connect(m_markerMenu, &QMenu::triggered, this, &Monitor::slotGoToMarker);
252
        m_forceSize = new KSelectAction(KoIconUtils::themedIcon(QStringLiteral("transform-scale")), i18n("Force Monitor Size"), this);
253 254 255 256 257 258 259 260
        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
261
        connect(m_forceSize, static_cast<void (KSelectAction::*)(QAction *)>(&KSelectAction::triggered), this, &Monitor::slotForceSize);
262
    }
263

264 265 266
    // Create Volume slider popup
    m_audioSlider = new QSlider(Qt::Vertical);
    m_audioSlider->setRange(0, 100);
267 268
    m_audioSlider->setValue(100);
    connect(m_audioSlider, &QSlider::valueChanged, this, &Monitor::slotSetVolume);
Nicolas Carion's avatar
Nicolas Carion committed
269
    auto *widgetslider = new QWidgetAction(this);
270 271
    widgetslider->setText(i18n("Audio volume"));
    widgetslider->setDefaultWidget(m_audioSlider);
Nicolas Carion's avatar
Nicolas Carion committed
272
    auto *menu = new QMenu(this);
273 274 275 276
    menu->addAction(widgetslider);

    m_audioButton = new QToolButton(this);
    m_audioButton->setMenu(menu);
277
    m_audioButton->setToolTip(i18n("Volume"));
278
    m_audioButton->setPopupMode(QToolButton::InstantPopup);
279
    QIcon icon;
Laurent Montel's avatar
Laurent Montel committed
280 281 282 283 284
    if (KdenliveSettings::volume() == 0) {
        icon = KoIconUtils::themedIcon(QStringLiteral("audio-volume-muted"));
    } else {
        icon = KoIconUtils::themedIcon(QStringLiteral("audio-volume-medium"));
    }
285 286 287
    m_audioButton->setIcon(icon);
    m_toolbar->addWidget(m_audioButton);

288 289 290
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setLayout(layout);
    setMinimumHeight(200);
291

292 293 294
    connect(this, &Monitor::scopesClear, m_glMonitor, &GLWidget::releaseAnalyse, Qt::DirectConnection);
    connect(m_glMonitor, &GLWidget::analyseFrame, this, &Monitor::frameUpdated);
    connect(m_glMonitor, &GLWidget::audioSamplesSignal, this, &Monitor::audioSamplesSignal);
295

296
    if (id != Kdenlive::ClipMonitor) {
Nicolas Carion's avatar
linting  
Nicolas Carion committed
297 298
        // TODO: reimplement
        // connect(render, &Render::durationChanged, this, &Monitor::durationChanged);
Vincent Pinon's avatar
Vincent Pinon committed
299
        connect(m_glMonitor->getControllerProxy(), &MonitorProxy::zoneChanged, this, &Monitor::updateTimelineClipZone);
300
    } else {
Vincent Pinon's avatar
Vincent Pinon committed
301
        connect(m_glMonitor->getControllerProxy(), &MonitorProxy::zoneChanged, this, &Monitor::updateClipZone);
302
    }
303
    connect(m_glMonitor->getControllerProxy(), &MonitorProxy::triggerAction, pCore.get(), &Core::triggerAction);
304

305 306 307
    m_sceneVisibilityAction = new QAction(KoIconUtils::themedIcon(QStringLiteral("transform-crop")), i18n("Show/Hide edit mode"), this);
    m_sceneVisibilityAction->setCheckable(true);
    m_sceneVisibilityAction->setChecked(KdenliveSettings::showOnMonitorScene());
308
    connect(m_sceneVisibilityAction, &QAction::triggered, this, &Monitor::slotEnableEffectScene);
309
    m_toolbar->addAction(m_sceneVisibilityAction);
310

311 312 313 314
    m_toolbar->addSeparator();
    m_timePos = new TimecodeDisplay(m_monitorManager->timecode(), this);
    m_toolbar->addWidget(m_timePos);

Nicolas Carion's avatar
Nicolas Carion committed
315
    auto *configButton = new QToolButton(m_toolbar);
316 317
    configButton->setIcon(KoIconUtils::themedIcon(QStringLiteral("kdenlive-menu")));
    configButton->setToolTip(i18n("Options"));
318
    configButton->setMenu(m_configMenu);
319
    configButton->setPopupMode(QToolButton::InstantPopup);
320
    m_toolbar->addWidget(configButton);
Laurent Montel's avatar
Laurent Montel committed
321 322 323
    if (m_recManager) {
        m_toolbar->addAction(m_recManager->switchAction());
    }
324
    /*QWidget *spacer = new QWidget(this);
325
    spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
326 327 328
    m_toolbar->addWidget(spacer);*/

    m_toolbar->addSeparator();
329 330
    int tm = 0;
    int bm = 0;
Laurent Montel's avatar
Laurent Montel committed
331
    m_toolbar->getContentsMargins(nullptr, &tm, nullptr, &bm);
332
    m_audioMeterWidget = new MonitorAudioLevel(m_glMonitor->profile(), m_toolbar->height() - tm - bm, this);
333
    m_toolbar->addWidget(m_audioMeterWidget);
334
    if (!m_audioMeterWidget->isValid) {
335 336 337 338
        KdenliveSettings::setMonitoraudio(0x01);
        m_audioMeterWidget->setVisibility(false);
    } else {
        m_audioMeterWidget->setVisibility((KdenliveSettings::monitoraudio() & m_id) != 0);
339
    }
340

341
    connect(m_timePos, SIGNAL(timeCodeEditingFinished()), this, SLOT(slotSeek()));
342
    layout->addWidget(m_toolbar);
Laurent Montel's avatar
Laurent Montel committed
343 344 345
    if (m_recManager) {
        layout->addWidget(m_recManager->toolbar());
    }
346 347

    // Load monitor overlay qml
348
    loadQmlScene(MonitorSceneDefault);
349

350 351 352 353
    // Info message widget
    m_infoMessage = new KMessageWidget(this);
    layout->addWidget(m_infoMessage);
    m_infoMessage->hide();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
354 355
}

356 357
Monitor::~Monitor()
{
358
    delete m_audioMeterWidget;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
359
    delete m_glMonitor;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
360 361
    delete m_videoWidget;
    delete m_glWidget;
362 363 364
    delete m_timePos;
}

365 366 367 368 369 370 371 372 373 374
void Monitor::setOffsetX(int x)
{
    m_glMonitor->setOffsetX(x, m_horizontalScroll->maximum());
}

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

375
void Monitor::slotGetCurrentImage(bool request)
376
{
377
    m_glMonitor->sendFrameForAnalysis = request;
378
    m_monitorManager->activateMonitor(m_id);
379
    refreshMonitorIfActive();
380 381
    if (request) {
        // Update analysis state
Laurent Montel's avatar
Laurent Montel committed
382
        QTimer::singleShot(500, m_monitorManager, &MonitorManager::checkScopes);
383 384 385
    } else {
        m_glMonitor->releaseAnalyse();
    }
386 387
}

388
void Monitor::slotAddEffect(const QStringList &effect)
389 390
{
    if (m_id == Kdenlive::ClipMonitor) {
Laurent Montel's avatar
Laurent Montel committed
391
        if (m_controller) {
392
            emit addMasterEffect(m_controller->AbstractProjectItem::clipId(), effect);
Laurent Montel's avatar
Laurent Montel committed
393 394 395
        }
    } else {
        emit addEffect(effect);
396 397 398
    }
}

399 400 401 402 403 404
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
405 406 407
        if (ic.isNull() || ic.name().isEmpty()) {
            continue;
        }
408 409 410 411 412 413 414
        QIcon newIcon = KoIconUtils::themedIcon(ic.name());
        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
415 416 417
        if (ic.isNull() || ic.name().isEmpty()) {
            continue;
        }
418 419 420
        QIcon newIcon = KoIconUtils::themedIcon(ic.name());
        m->setActiveIcon(newIcon);
        ic = m->inactiveIcon();
Laurent Montel's avatar
Laurent Montel committed
421 422 423
        if (ic.isNull() || ic.name().isEmpty()) {
            continue;
        }
424 425 426 427 428
        newIcon = KoIconUtils::themedIcon(ic.name());
        m->setInactiveIcon(newIcon);
    }
}

429
QAction *Monitor::recAction()
430
{
Laurent Montel's avatar
Laurent Montel committed
431 432 433
    if (m_recManager) {
        return m_recManager->switchAction();
    }
Laurent Montel's avatar
Laurent Montel committed
434
    return nullptr;
435 436
}

437 438 439 440 441
void Monitor::slotLockMonitor(bool lock)
{
    m_monitorManager->lockMonitor(m_id, lock);
}

442
void Monitor::setupMenu(QMenu *goMenu, QMenu *overlayMenu, QAction *playZone, QAction *loopZone, QMenu *markerMenu, QAction *loopClip)
443
{
444
    delete m_contextMenu;
445
    m_contextMenu = new QMenu(this);
446
    m_contextMenu->addMenu(m_playMenu);
Laurent Montel's avatar
Laurent Montel committed
447
    if (goMenu) {
448
        m_contextMenu->addMenu(goMenu);
Laurent Montel's avatar
Laurent Montel committed
449
    }
450

451
    if (markerMenu) {
452
        m_contextMenu->addMenu(markerMenu);
Nicolas Carion's avatar
Nicolas Carion committed
453
        QList<QAction *> list = markerMenu->actions();
454
        for (int i = 0; i < list.count(); ++i) {
455
            if (list.at(i)->data().toString() == QLatin1String("edit_marker")) {
456 457 458 459 460
                m_editMarker = list.at(i);
                break;
            }
        }
    }
461

462 463
    m_playMenu->addAction(playZone);
    m_playMenu->addAction(loopZone);
464 465 466 467
    if (loopClip) {
        m_loopClipAction = loopClip;
        m_playMenu->addAction(loopClip);
    }
468

Nicolas Carion's avatar
Nicolas Carion committed
469
    // TODO: add save zone to timeline monitor when fixed
470
    m_contextMenu->addMenu(m_markerMenu);
471
    if (m_id == Kdenlive::ClipMonitor) {
Laurent Montel's avatar
Laurent Montel committed
472
        m_contextMenu->addAction(KoIconUtils::themedIcon(QStringLiteral("document-save")), i18n("Save zone"), this, SLOT(slotSaveZone()));
Nicolas Carion's avatar
Nicolas Carion committed
473 474
        QAction *extractZone =
            m_configMenu->addAction(KoIconUtils::themedIcon(QStringLiteral("document-new")), i18n("Extract Zone"), this, SLOT(slotExtractCurrentZone()));
475
        m_contextMenu->addAction(extractZone);
476
    }
477 478
    m_contextMenu->addAction(m_monitorManager->getAction(QStringLiteral("extract_frame")));
    m_contextMenu->addAction(m_monitorManager->getAction(QStringLiteral("extract_frame_to_project")));
479

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
480
    if (m_id == Kdenlive::ProjectMonitor) {
Nicolas Carion's avatar
Nicolas Carion committed
481 482
        m_multitrackView = m_contextMenu->addAction(KoIconUtils::themedIcon(QStringLiteral("view-split-left-right")), i18n("Multitrack view"), this,
                                                    SIGNAL(multitrackView(bool)));
483 484
        m_multitrackView->setCheckable(true);
        m_configMenu->addAction(m_multitrackView);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
485
    } else if (m_id == Kdenlive::ClipMonitor) {
Nicolas Carion's avatar
Nicolas Carion committed
486 487
        QAction *setThumbFrame = m_contextMenu->addAction(KoIconUtils::themedIcon(QStringLiteral("document-new")), i18n("Set current image as thumbnail"), this,
                                                          SLOT(slotSetThumbFrame()));
488 489 490
        m_configMenu->addAction(setThumbFrame);
    }

Laurent Montel's avatar
Laurent Montel committed
491
    if (overlayMenu) {
492
        m_contextMenu->addMenu(overlayMenu);
Laurent Montel's avatar
Laurent Montel committed
493
    }
494

495 496
    QAction *overlayAudio = m_contextMenu->addAction(QIcon(), i18n("Overlay audio waveform"));
    overlayAudio->setCheckable(true);
Laurent Montel's avatar
Laurent Montel committed
497
    connect(overlayAudio, &QAction::toggled, m_glMonitor, &GLWidget::slotSwitchAudioOverlay);
498
    overlayAudio->setChecked(KdenliveSettings::displayAudioOverlay());
499

500 501
    QAction *switchAudioMonitor = m_configMenu->addAction(i18n("Show Audio Levels"), this, SLOT(slotSwitchAudioMonitor()));
    switchAudioMonitor->setCheckable(true);
Nicolas Carion's avatar
Nicolas Carion committed
502
    switchAudioMonitor->setChecked((KdenliveSettings::monitoraudio() & m_id) != 0);
503
    m_configMenu->addAction(overlayAudio);
504 505 506
    // 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);
507 508
}

509 510 511 512 513 514
void Monitor::slotGoToMarker(QAction *action)
{
    int pos = action->data().toInt();
    slotSeek(pos);
}

515
void Monitor::slotForceSize(QAction *a)
516
{
517 518 519 520 521 522 523
    int resizeType = a->data().toInt();
    int profileWidth = 320;
    int profileHeight = 200;
    if (resizeType > 0) {
        // calculate size
        QRect r = QApplication::desktop()->screenGeometry();
        profileHeight = m_glMonitor->profileSize().height() * resizeType / 100;
524
        profileWidth = m_glMonitor->profile()->dar() * profileHeight;
525 526
        if (profileWidth > r.width() * 0.8 || profileHeight > r.height() * 0.7) {
            // reset action to free resize
Nicolas Carion's avatar
format  
Nicolas Carion committed
527
            const QList<QAction *> list = m_forceSize->actions();
528
            for (QAction *ac : list) {
529
                if (ac->data().toInt() == m_forceSizeFactor) {
530 531 532 533
                    m_forceSize->setCurrentAction(ac);
                    break;
                }
            }
534
            warningMessage(i18n("Your screen resolution is not sufficient for this action"));
535 536
            return;
        }
537
    }
538
    switch (resizeType) {
Laurent Montel's avatar
Laurent Montel committed
539 540 541 542 543 544
    case 100:
    case 50:
        // resize full size
        setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
        m_videoWidget->setMinimumSize(profileWidth, profileHeight);
        m_videoWidget->setMaximumSize(profileWidth, profileHeight);
545
        setMinimumSize(QSize(profileWidth, profileHeight + m_toolbar->height() + m_glMonitor->getControllerProxy()->rulerHeight()));
Laurent Montel's avatar
Laurent Montel committed
546 547
        break;
    default:
548 549
        // Free resize
        m_videoWidget->setMinimumSize(profileWidth, profileHeight);
550
        m_videoWidget->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
551
        setMinimumSize(QSize(profileWidth, profileHeight + m_toolbar->height() + m_glMonitor->getControllerProxy()->rulerHeight()));
552 553
        setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        break;
554
    }
555
    m_forceSizeFactor = resizeType;
556 557 558
    updateGeometry();
}

559 560 561 562 563 564 565 566 567 568
QString Monitor::getTimecodeFromFrames(int pos)
{
    return m_monitorManager->timecode().getTimecodeFromFrames(pos);
}

double Monitor::fps() const
{
    return m_monitorManager->timecode().fps();
}

569 570 571 572 573
Timecode Monitor::timecode() const
{
    return m_monitorManager->timecode();
}

574
void Monitor::updateMarkers()
575
{
576
    if (m_controller) {
577
        m_markerMenu->clear();
578
        QList<CommentedTime> markers = m_controller->getMarkerModel()->getAllMarkers();
579
        if (!markers.isEmpty()) {
580
            for (int i = 0; i < markers.count(); ++i) {
Nicolas Carion's avatar
Nicolas Carion committed
581
                int pos = (int)markers.at(i).time().frames(m_monitorManager->timecode().fps());
Laurent Montel's avatar
Laurent Montel committed
582
                QString position = m_monitorManager->timecode().getTimecode(markers.at(i).time()) + QLatin1Char(' ') + markers.at(i).comment();
583 584 585
                QAction *go = m_markerMenu->addAction(position);
                go->setData(pos);
            }
586
        }
587 588 589 590
        m_markerMenu->setEnabled(!m_markerMenu->isEmpty());
    }
}

Laurent Montel's avatar
Laurent Montel committed
591
void Monitor::setGuides(const QMap<double, QString> &guides)
592
{
Nicolas Carion's avatar
linting  
Nicolas Carion committed
593
    // TODO: load guides model
594 595
    m_markerMenu->clear();
    QMapIterator<double, QString> i(guides);
Laurent Montel's avatar
Laurent Montel committed
596
    QList<CommentedTime> guidesList;
597 598 599 600
    while (i.hasNext()) {
        i.next();
        CommentedTime timeGuide(GenTime(i.key()), i.value());
        guidesList << timeGuide;
Nicolas Carion's avatar
Nicolas Carion committed
601
        int pos = (int)timeGuide.time().frames(m_monitorManager->timecode().fps());
Laurent Montel's avatar
Laurent Montel committed
602
        QString position = m_monitorManager->timecode().getTimecode(timeGuide.time()) + QLatin1Char(' ') + timeGuide.comment();
603 604 605
        QAction *go = m_markerMenu->addAction(position);
        go->setData(pos);
    }
Nicolas Carion's avatar
linting  
Nicolas Carion committed
606
    // m_ruler->setMarkers(guidesList);
607 608 609 610
    m_markerMenu->setEnabled(!m_markerMenu->isEmpty());
    checkOverlay();
}

611 612
void Monitor::slotSeekToPreviousSnap()
{
Laurent Montel's avatar
Laurent Montel committed
613
    if (m_controller) {
614
        m_glMonitor->seek(getSnapForPos(true).frames(m_monitorManager->timecode().fps()));
Laurent Montel's avatar
Laurent Montel committed
615
    }
616 617
}

618 619
void Monitor::slotSeekToNextSnap()
{
Laurent Montel's avatar
Laurent Montel committed
620
    if (m_controller) {
621
        m_glMonitor->seek(getSnapForPos(false).frames(m_monitorManager->timecode().fps()));
Laurent Montel's avatar
Laurent Montel committed
622
    }
623 624
}

625
int Monitor::position()
626
{
627
    return m_glMonitor->getCurrentPos();
628 629
}

630 631
GenTime Monitor::getSnapForPos(bool previous)
{
632 633
    int frame = previous ? m_snaps->getPreviousPoint(m_glMonitor->getCurrentPos()) : m_snaps->getNextPoint(m_glMonitor->getCurrentPos());
    return GenTime(frame, pCore->getCurrentFps());
634 635
}

636
void Monitor::slotLoadClipZone(const QPoint &zone)
637
{
638
    m_glMonitor->getControllerProxy()->setZone(zone.x(), zone.y());
639
    checkOverlay();
640 641
}

642 643
void Monitor::slotSetZoneStart()
{
644
    m_glMonitor->getControllerProxy()->setZoneIn(m_glMonitor->getCurrentPos());
645 646
    if (m_controller) {
        m_controller->setZone(m_glMonitor->getControllerProxy()->zone());
647 648 649
    } else {
        // timeline
        emit timelineZoneChanged();
650
    }
651
    checkOverlay();
652 653
}

654
void Monitor::slotSetZoneEnd(bool discardLastFrame)
655
{
656 657
    int pos = m_glMonitor->getCurrentPos() - (discardLastFrame ? 1 : 0);
    m_glMonitor->getControllerProxy()->setZoneOut(pos);
658 659 660
    if (m_controller) {
        m_controller->setZone(m_glMonitor->getControllerProxy()->zone());
    }
661
    checkOverlay();
662 663
}

664
// virtual
Laurent Montel's avatar
Laurent Montel committed
665
void Monitor::mousePressEvent(QMouseEvent *event)
666
{
667
    m_monitorManager->activateMonitor(m_id);
Nicolas Carion's avatar
Nicolas Carion committed
668
    if ((event->button() & Qt::RightButton) == 0u) {
669
        if (m_glWidget->geometry().contains(event->pos())) {
670
            m_DragStartPosition = event->pos();
671
            event->accept();
672
        }
673
    } else if (m_contextMenu) {
674
        slotActivateMonitor();
675
        m_contextMenu->popup(event->globalPos());
676
        event->accept();
677
    }
678
    QWidget::mousePressEvent(event);
679 680
}

681 682
void Monitor::slotShowMenu(const QPoint pos)
{
683
    slotActivateMonitor();
Laurent Montel's avatar
Laurent Montel committed
684 685 686
    if (m_contextMenu) {
        m_contextMenu->popup(pos);
    }
687 688
}

689 690
void Monitor::resizeEvent(QResizeEvent *event)
{
Vincent Pinon's avatar
Vincent Pinon committed
691
    Q_UNUSED(event)
692
    if (m_glMonitor->zoom() > 0.0f) {
Vincent Pinon's avatar
Vincent Pinon committed
693 694
        float horizontal = float(m_horizontalScroll->value()) / float(m_horizontalScroll->maximum());
        float vertical = float(m_verticalScroll->value()) / float(m_verticalScroll->maximum());
695 696 697 698 699 700 701 702 703 704 705
        adjustScrollBars(horizontal, vertical);
    } else {
        m_horizontalScroll->hide();
        m_verticalScroll->hide();
    }
}

void Monitor::adjustScrollBars(float horizontal, float vertical)
{
    if (m_glMonitor->zoom() > 1.0f) {
        m_horizontalScroll->setPageStep(m_glWidget->width());
Vincent Pinon's avatar
Vincent Pinon committed
706
        m_horizontalScroll->setMaximum((int)((float)m_glMonitor->profileSize().width() * m_glMonitor->zoom()) - m_horizontalScroll->pageStep());
707 708 709 710
        m_horizontalScroll->setValue(qRound(horizontal * m_horizontalScroll->maximum()));
        emit m_horizontalScroll->valueChanged(m_horizontalScroll->value());
        m_horizontalScroll->show();
    } else {
Vincent Pinon's avatar
Vincent Pinon committed
711
        int max = (int)((float)m_glMonitor->profileSize().width() * m_glMonitor->zoom()) - m_glWidget->width();
712 713 714 715 716 717
        emit m_horizontalScroll->valueChanged(qRound(0.5 * max));
        m_horizontalScroll->hide();
    }

    if (m_glMonitor->zoom() > 1.0f) {
        m_verticalScroll->setPageStep(m_glWidget->height());
Vincent Pinon's avatar
Vincent Pinon committed
718
        m_verticalScroll->setMaximum((int)((float)m_glMonitor->profileSize().height() * m_glMonitor->zoom()) - m_verticalScroll->pageStep());
Nicolas Carion's avatar
Nicolas Carion committed
719
        m_verticalScroll->setValue((int)((float)m_verticalScroll->maximum() * vertical));
720 721 722
        emit m_verticalScroll->valueChanged(m_verticalScroll->value());
        m_verticalScroll->show();
    } else {
Vincent Pinon's avatar
Vincent Pinon committed
723
        int max = (int)((float)m_glMonitor->profileSize().height() * m_glMonitor->zoom()) - m_glWidget->height();
724 725 726 727 728 729 730
        emit m_verticalScroll->valueChanged(qRound(0.5 * max));
        m_verticalScroll->hide();
    }
}

void Monitor::setZoom()
{
731
    if (qFuzzyCompare(m_glMonitor->zoom(), 1.0f)) {