]> git.sesse.net Git - kdenlive/blobdiff - src/monitor.cpp
Make monitor context menu consistent with "Monitor" menu
[kdenlive] / src / monitor.cpp
index 9e6a8b083e2c5553c71404893d361e0737cfa0eb..2b1102784169660d7d96afa777e549d5cbd5000d 100644 (file)
@@ -28,6 +28,7 @@
 #include <KDebug>
 #include <KLocale>
 #include <KFileDialog>
+#include <KApplication>
 
 #include "gentime.h"
 #include "monitor.h"
@@ -37,7 +38,7 @@
 #include "docclipbase.h"
 
 Monitor::Monitor(QString name, MonitorManager *manager, QWidget *parent)
-        : QWidget(parent), render(NULL), m_monitorManager(manager), m_name(name), m_isActive(false), m_currentClip(NULL) {
+        : QWidget(parent), render(NULL), m_monitorManager(manager), m_name(name), m_isActive(false), m_currentClip(NULL), m_dragStarted(false) {
     ui.setupUi(this);
     m_scale = 1;
     m_ruler = new SmallRuler();
@@ -54,43 +55,35 @@ Monitor::Monitor(QString name, MonitorManager *manager, QWidget *parent)
     m_playIcon = KIcon("media-playback-start");
     m_pauseIcon = KIcon("media-playback-pause");
 
-    QAction *m_rewAction = toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"));
-    connect(m_rewAction, SIGNAL(triggered()), this, SLOT(slotRewind()));
-    QAction *m_rew1Action = toolbar->addAction(KIcon("media-skip-backward"), i18n("Rewind 1 frame"));
-    connect(m_rew1Action, SIGNAL(triggered()), this, SLOT(slotRewindOneFrame()));
+    QAction *zoneStart = toolbar->addAction(KIcon("kdenlive-zone-start"), i18n("Set zone start"), this, SLOT(slotSetZoneStart()));
+    QAction *zoneEnd = toolbar->addAction(KIcon("kdenlive-zone-end"), i18n("Set zone end"), this, SLOT(slotSetZoneEnd()));
 
-    QToolButton *playButton = new QToolButton(toolbar);
-    QMenu *playMenu = new QMenu(i18n("Play..."), this);
-    playButton->setMenu(playMenu);
-    playButton->setPopupMode(QToolButton::MenuButtonPopup);
-    toolbar->addWidget(playButton);
+    toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"), this, SLOT(slotRewind()));
+    toolbar->addAction(KIcon("media-skip-backward"), i18n("Rewind 1 frame"), this, SLOT(slotRewindOneFrame()));
 
-    m_playAction = playMenu->addAction(m_playIcon, i18n("Play"));
+    QToolButton *playButton = new QToolButton(toolbar);
+    m_playMenu = new QMenu(i18n("Play..."), this);
+    m_playAction = m_playMenu->addAction(m_playIcon, i18n("Play"));
     m_playAction->setCheckable(true);
     connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotPlay()));
-    QAction *m_playSectionAction = playMenu->addAction(m_playIcon, i18n("Play Section"));
-    connect(m_playSectionAction, SIGNAL(triggered()), this, SLOT(slotPlay()));
-    QAction *m_loopSectionAction = playMenu->addAction(m_playIcon, i18n("Loop Section"));
-    connect(m_loopSectionAction, SIGNAL(triggered()), this, SLOT(slotPlay()));
+    m_playMenu->addAction(m_playIcon, i18n("Play Section"), this, SLOT(slotPlayZone()));
+    m_playMenu->addAction(m_playIcon, i18n("Loop Section"), this, SLOT(slotLoopZone()));
 
-    QAction *m_fwd1Action = toolbar->addAction(KIcon("media-skip-forward"), i18n("Forward 1 frame"));
-    connect(m_fwd1Action, SIGNAL(triggered()), this, SLOT(slotForwardOneFrame()));
-    QAction *m_fwdAction = toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"));
-    connect(m_fwdAction, SIGNAL(triggered()), this, SLOT(slotForward()));
+    playButton->setMenu(m_playMenu);
+    playButton->setPopupMode(QToolButton::MenuButtonPopup);
+    toolbar->addWidget(playButton);
 
-    playButton->setDefaultAction(m_playAction);
+    toolbar->addAction(KIcon("media-skip-forward"), i18n("Forward 1 frame"), this, SLOT(slotForwardOneFrame()));
+    toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"), this, SLOT(slotForward()));
 
+    playButton->setDefaultAction(m_playAction);
 
     QToolButton *configButton = new QToolButton(toolbar);
-    QMenu *configMenu = new QMenu(i18n("Misc..."), this);
+    m_configMenu = new QMenu(i18n("Misc..."), this);
     configButton->setIcon(KIcon("system-run"));
-    configButton->setMenu(configMenu);
+    configButton->setMenu(m_configMenu);
     configButton->setPopupMode(QToolButton::QToolButton::InstantPopup);
     toolbar->addWidget(configButton);
-    QAction *resize1Action = configMenu->addAction(KIcon("transform-scale"), i18n("Resize (100%)"));
-    connect(resize1Action, SIGNAL(triggered()), this, SLOT(slotSetSizeOneToOne()));
-    QAction *resize2Action = configMenu->addAction(KIcon("transform-scale"), i18n("Resize (50%)"));
-    connect(resize2Action, SIGNAL(triggered()), this, SLOT(slotSetSizeOneToTwo()));
 
     QWidget *spacer = new QWidget(this);
     spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
@@ -100,6 +93,8 @@ Monitor::Monitor(QString name, MonitorManager *manager, QWidget *parent)
     m_timePos->setInputMask("99:99:99:99");
     toolbar->addWidget(m_timePos);
 
+    connect(m_timePos, SIGNAL(editingFinished()), this, SLOT(slotSeek()));
+
     layout2->addWidget(toolbar);
     ui.button_frame->setLayout(layout2);
     const int toolHeight = toolbar->height();
@@ -107,7 +102,6 @@ Monitor::Monitor(QString name, MonitorManager *manager, QWidget *parent)
 
     //m_ruler->setPixelPerMark(3);
 
-
     QVBoxLayout *rendererBox = new QVBoxLayout(ui.video_frame);
     rendererBox->setContentsMargins(0, 0, 0, 0);
     m_monitorRefresh = new MonitorRefresh(ui.video_frame);
@@ -115,27 +109,24 @@ Monitor::Monitor(QString name, MonitorManager *manager, QWidget *parent)
     render = new Render(m_name, (int) m_monitorRefresh->winId(), -1, this);
     m_monitorRefresh->setRenderer(render);
 
-    m_contextMenu = new QMenu(this);
-    m_contextMenu->addMenu(playMenu);
-    QAction *extractFrame = m_contextMenu->addAction(KIcon("document-new"), i18n("Extract frame"));
-    connect(extractFrame, SIGNAL(triggered()), this, SLOT(slotExtractCurrentFrame()));
-    connect(m_ruler, SIGNAL(seekRenderer(int)), this, SLOT(slotSeek(int)));
 
+
+    connect(m_ruler, SIGNAL(seekRenderer(int)), this, SLOT(slotSeek(int)));
+    connect(m_ruler, SIGNAL(zoneChanged(QPoint)), this, SIGNAL(zoneUpdated(QPoint)));
     connect(render, SIGNAL(durationChanged(int)), this, SLOT(adjustRulerSize(int)));
     connect(render, SIGNAL(rendererPosition(int)), this, SLOT(seekCursor(int)));
     connect(render, SIGNAL(rendererStopped(int)), this, SLOT(rendererStopped(int)));
+
+    m_configMenu->addSeparator();
+    m_configMenu->addAction(KIcon("transform-scale"), i18n("Resize (100%)"), this, SLOT(slotSetSizeOneToOne()));
+    m_configMenu->addAction(KIcon("transform-scale"), i18n("Resize (50%)"), this, SLOT(slotSetSizeOneToTwo()));
+    //render->createVideoXWindow(ui.video_frame->winId(), -1);
+    m_length = 0;
+
     if (name != "clip") {
         connect(render, SIGNAL(rendererPosition(int)), this, SIGNAL(renderPosition(int)));
         connect(render, SIGNAL(durationChanged(int)), this, SIGNAL(durationChanged(int)));
-        QAction *splitView = m_contextMenu->addAction(KIcon("document-new"), i18n("Split view"));
-        splitView->setCheckable(true);
-        connect(splitView, SIGNAL(toggled(bool)), render, SLOT(slotSplitView(bool)));
-    } else {
-        QAction *setThumbFrame = m_contextMenu->addAction(KIcon("document-new"), i18n("Set current image as thumbnail"));
-        connect(setThumbFrame, SIGNAL(triggered()), this, SLOT(slotSetThumbFrame()));
     }
-    //render->createVideoXWindow(ui.video_frame->winId(), -1);
-    m_length = 0;
     m_monitorRefresh->show();
     kDebug() << "/////// BUILDING MONITOR, ID: " << ui.video_frame->winId();
 }
@@ -144,6 +135,28 @@ QString Monitor::name() const {
     return m_name;
 }
 
+void Monitor::setupMenu(QMenu *goMenu) {
+    m_contextMenu = new QMenu(this);
+    m_contextMenu->addMenu(m_playMenu);
+    m_contextMenu->addMenu(goMenu);
+
+    //TODO: add save zone to timeline monitor when fixed
+    if (m_name == "clip") m_contextMenu->addAction(KIcon("document-save"), i18n("Save zone"), this, SLOT(slotSaveZone()));
+
+    QAction *extractFrame = m_configMenu->addAction(KIcon("document-new"), i18n("Extract frame"), this, SLOT(slotExtractCurrentFrame()));
+    m_contextMenu->addAction(extractFrame);
+
+    if (m_name != "clip") {
+        QAction *splitView = m_contextMenu->addAction(KIcon("document-new"), i18n("Split view"), render, SLOT(slotSplitView(bool)));
+        splitView->setCheckable(true);
+        m_configMenu->addAction(splitView);
+    } else {
+        QAction *setThumbFrame = m_contextMenu->addAction(KIcon("document-new"), i18n("Set current image as thumbnail"), this, SLOT(slotSetThumbFrame()));
+        m_configMenu->addAction(setThumbFrame);
+    }
+
+}
+
 void Monitor::slotSetSizeOneToOne() {
     QRect r = QApplication::desktop()->screenGeometry();
     const int maxWidth = r.width() - 20;
@@ -186,13 +199,99 @@ void Monitor::resetSize() {
     ui.video_frame->setMinimumSize(0, 0);
 }
 
+void Monitor::slotZoneMoved(int start, int end) {
+    m_ruler->setZone(start, end);
+}
+
+void Monitor::slotSetZoneStart() {
+    m_ruler->setZone(m_position, -1);
+    emit zoneUpdated(m_ruler->zone());
+}
+
+void Monitor::slotSetZoneEnd() {
+    m_ruler->setZone(-1, m_position);
+    emit zoneUpdated(m_ruler->zone());
+}
+
 // virtual
 void Monitor::mousePressEvent(QMouseEvent * event) {
     if (event->button() != Qt::RightButton) {
-        slotPlay();
+        if (ui.video_frame->underMouse()) {
+            m_dragStarted = true;
+            m_DragStartPosition = event->pos();
+        }
     } else m_contextMenu->popup(event->globalPos());
 }
 
+// virtual
+void Monitor::mouseReleaseEvent(QMouseEvent * event) {
+    if (m_dragStarted) {
+        if (ui.video_frame->underMouse()) {
+            slotPlay();
+        } else QWidget::mouseReleaseEvent(event);
+        m_dragStarted = false;
+    }
+}
+
+
+// virtual
+void Monitor::mouseMoveEvent(QMouseEvent *event) {
+    kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
+    if (!m_dragStarted || m_currentClip == NULL) return;
+
+    if ((event->pos() - m_DragStartPosition).manhattanLength()
+            < QApplication::startDragDistance())
+        return;
+
+    {
+        QDrag *drag = new QDrag(this);
+        QMimeData *mimeData = new QMimeData;
+
+        QStringList list;
+        list.append(m_currentClip->getId());
+        QPoint p = m_ruler->zone();
+        list.append(QString::number(p.x()));
+        list.append(QString::number(p.y()));
+        QByteArray data;
+        data.append(list.join(";").toUtf8());
+        mimeData->setData("kdenlive/clip", data);
+        drag->setMimeData(mimeData);
+        QPixmap pix = m_currentClip->thumbnail();
+        kDebug() << "/ / / /CLIP DRAGGED PIXMAP: " << pix.width() << "x" << pix.height();
+        drag->setPixmap(pix);
+        drag->setHotSpot(QPoint(0, 50));
+        drag->start(Qt::MoveAction);
+
+        //Qt::DropAction dropAction;
+        //dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
+
+        //Qt::DropAction dropAction = drag->exec();
+
+    }
+    //event->accept();
+}
+
+/*void Monitor::dragMoveEvent(QDragMoveEvent * event) {
+    event->setDropAction(Qt::IgnoreAction);
+    event->setDropAction(Qt::MoveAction);
+    if (event->mimeData()->hasText()) {
+        event->acceptProposedAction();
+    }
+}
+
+Qt::DropActions Monitor::supportedDropActions() const {
+    // returns what actions are supported when dropping
+    return Qt::MoveAction;
+}*/
+
+QStringList Monitor::mimeTypes() const {
+    QStringList qstrList;
+    // list of accepted mime types for drop
+    qstrList.append("kdenlive/clip");
+    return qstrList;
+}
+
+
 // virtual
 void Monitor::wheelEvent(QWheelEvent * event) {
     if (event->modifiers() == Qt::ControlModifier) {
@@ -219,10 +318,19 @@ void Monitor::slotExtractCurrentFrame() {
     if (!outputFile.isEmpty()) frame.save(outputFile);
 }
 
+bool Monitor::isActive() const {
+    return m_isActive;
+}
+
 void Monitor::activateMonitor() {
     if (!m_isActive) m_monitorManager->activateMonitor(m_name);
 }
 
+void Monitor::slotSeek() {
+    const int frames = m_monitorManager->timecode().getFrameCount(m_timePos->text(), m_monitorManager->timecode().fps());
+    slotSeek(frames);
+}
+
 void Monitor::slotSeek(int pos) {
     if (!m_isActive) m_monitorManager->activateMonitor(m_name);
     if (render == NULL) return;
@@ -250,6 +358,24 @@ void Monitor::slotEnd() {
     m_timePos->setText(m_monitorManager->timecode().getTimecodeFromFrames(m_position));
 }
 
+void Monitor::slotZoneStart() {
+    if (!m_isActive) m_monitorManager->activateMonitor(m_name);
+    render->play(0);
+    m_position = m_ruler->zone().x();
+    render->seekToFrame(m_position);
+    emit renderPosition(m_position);
+    m_timePos->setText(m_monitorManager->timecode().getTimecodeFromFrames(m_position));
+}
+
+void Monitor::slotZoneEnd() {
+    if (!m_isActive) m_monitorManager->activateMonitor(m_name);
+    render->play(0);
+    m_position = m_ruler->zone().y();
+    render->seekToFrame(m_position);
+    emit renderPosition(m_position);
+    m_timePos->setText(m_monitorManager->timecode().getTimecodeFromFrames(m_position));
+}
+
 void Monitor::slotRewind(double speed) {
     if (!m_isActive) m_monitorManager->activateMonitor(m_name);
     if (speed == 0) {
@@ -344,6 +470,14 @@ void Monitor::refreshMonitor(bool visible) {
     }
 }
 
+void Monitor::pause() {
+    if (render == NULL) return;
+    if (!m_isActive) m_monitorManager->activateMonitor(m_name);
+    render->pause();
+    //m_playAction->setChecked(true);
+    //m_playAction->setIcon(m_pauseIcon);
+}
+
 void Monitor::slotPlay() {
     if (render == NULL) return;
     if (!m_isActive) m_monitorManager->activateMonitor(m_name);
@@ -352,11 +486,29 @@ void Monitor::slotPlay() {
     m_playAction->setIcon(m_pauseIcon);
 }
 
+void Monitor::slotPlayZone() {
+    if (render == NULL) return;
+    if (!m_isActive) m_monitorManager->activateMonitor(m_name);
+    QPoint p = m_ruler->zone();
+    render->playZone(GenTime(p.x(), render->fps()), GenTime(p.y(), render->fps()));
+    m_playAction->setChecked(true);
+    m_playAction->setIcon(m_pauseIcon);
+}
+
+void Monitor::slotLoopZone() {
+    if (render == NULL) return;
+    if (!m_isActive) m_monitorManager->activateMonitor(m_name);
+    QPoint p = m_ruler->zone();
+    render->loopZone(GenTime(p.x(), render->fps()), GenTime(p.y(), render->fps()));
+    m_playAction->setChecked(true);
+    m_playAction->setIcon(m_pauseIcon);
+}
+
 void Monitor::slotSetXml(DocClipBase *clip, const int position) {
     if (render == NULL) return;
     if (!m_isActive) m_monitorManager->activateMonitor(m_name);
     if (!clip) return;
-    if (clip != m_currentClip || m_currentClip->producer() == NULL) {
+    if (clip != m_currentClip && clip->producer() != NULL) {
         m_currentClip = clip;
         render->setProducer(clip->producer(), position);
         //m_ruler->slotNewValue(0);
@@ -379,6 +531,14 @@ void Monitor::slotOpenFile(const QString &file) {
     render->setSceneList(doc, 0);
 }
 
+void Monitor::slotSaveZone() {
+    if (render == NULL) return;
+    emit saveZone(render, m_ruler->zone());
+
+    //render->setSceneList(doc, 0);
+}
+
+
 void Monitor::resetProfile() {
     if (render == NULL) return;
     render->resetProfile();