]> git.sesse.net Git - kdenlive/blob - src/mainwindow.cpp
function for changing transitions
[kdenlive] / src / mainwindow.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21
22 #include <QTextStream>
23 #include <QTimer>
24 #include <QAction>
25 #include <QtTest>
26 #include <QtCore>
27
28 #include <KApplication>
29 #include <KAction>
30 #include <KLocale>
31 #include <KGlobal>
32 #include <KActionCollection>
33 #include <KStandardAction>
34 #include <KFileDialog>
35 #include <KMessageBox>
36 #include <KDebug>
37 #include <KIO/NetAccess>
38 #include <KSaveFile>
39 #include <KRuler>
40 #include <KConfigDialog>
41 #include <KXMLGUIFactory>
42 #include <KStatusBar>
43 #include <kstandarddirs.h>
44 #include <KUrlRequesterDialog>
45 #include <KTemporaryFile>
46 #include <KActionMenu>
47 #include <KMenu>
48 #include <ktogglefullscreenaction.h>
49
50 #include <mlt++/Mlt.h>
51
52 #include "mainwindow.h"
53 #include "kdenlivesettings.h"
54 #include "kdenlivesettingsdialog.h"
55 #include "initeffects.h"
56 #include "profilesdialog.h"
57 #include "projectsettings.h"
58 #include "events.h"
59 #include "renderjob.h"
60
61 #define ID_STATUS_MSG 1
62 #define ID_EDITMODE_MSG 2
63 #define ID_TIMELINE_MSG 3
64 #define ID_TIMELINE_BUTTONS 5
65 #define ID_TIMELINE_POS 6
66 #define ID_TIMELINE_FORMAT 7
67
68 MainWindow::MainWindow(QWidget *parent)
69         : KXmlGuiWindow(parent),
70         m_activeDocument(NULL), m_activeTimeline(NULL), m_renderWidget(NULL), m_jogProcess(NULL) {
71     parseProfiles();
72
73     m_commandStack = new QUndoGroup;
74     m_timelineArea = new KTabWidget(this);
75     m_timelineArea->setTabReorderingEnabled(true);
76     m_timelineArea->setTabBarHidden(true);
77
78     QToolButton *closeTabButton = new QToolButton;
79     connect(closeTabButton, SIGNAL(clicked()), this, SLOT(slotRemoveTab()));
80     closeTabButton->setIcon(KIcon("tab-close"));
81     closeTabButton->adjustSize();
82     closeTabButton->setToolTip(i18n("Close the current tab"));
83     m_timelineArea->setCornerWidget(closeTabButton);
84     connect(m_timelineArea, SIGNAL(currentChanged(int)), this, SLOT(activateDocument()));
85
86     initEffects::parseEffectFiles(&m_audioEffects, &m_videoEffects, &m_transitions);
87     m_monitorManager = new MonitorManager();
88
89     projectListDock = new QDockWidget(i18n("Project Tree"), this);
90     projectListDock->setObjectName("project_tree");
91     m_projectList = new ProjectList(this);
92     projectListDock->setWidget(m_projectList);
93     addDockWidget(Qt::TopDockWidgetArea, projectListDock);
94
95     effectListDock = new QDockWidget(i18n("Effect List"), this);
96     effectListDock->setObjectName("effect_list");
97     m_effectList = new EffectsListView(&m_audioEffects, &m_videoEffects, &m_customEffects);
98
99     //m_effectList = new KListWidget(this);
100     effectListDock->setWidget(m_effectList);
101     addDockWidget(Qt::TopDockWidgetArea, effectListDock);
102
103     effectStackDock = new QDockWidget(i18n("Effect Stack"), this);
104     effectStackDock->setObjectName("effect_stack");
105     effectStack = new EffectStackView(&m_audioEffects, &m_videoEffects, &m_customEffects, this);
106     effectStackDock->setWidget(effectStack);
107     addDockWidget(Qt::TopDockWidgetArea, effectStackDock);
108
109     transitionConfigDock = new QDockWidget(i18n("Transition"), this);
110     transitionConfigDock->setObjectName("transition");
111     transitionConfig = new TransitionSettings(&m_transitions, this);
112     transitionConfigDock->setWidget(transitionConfig);
113     addDockWidget(Qt::TopDockWidgetArea, transitionConfigDock);
114
115
116     clipMonitorDock = new QDockWidget(i18n("Clip Monitor"), this);
117     clipMonitorDock->setObjectName("clip_monitor");
118     m_clipMonitor = new Monitor("clip", m_monitorManager, this);
119     clipMonitorDock->setWidget(m_clipMonitor);
120     addDockWidget(Qt::TopDockWidgetArea, clipMonitorDock);
121     //m_clipMonitor->stop();
122
123     projectMonitorDock = new QDockWidget(i18n("Project Monitor"), this);
124     projectMonitorDock->setObjectName("project_monitor");
125     m_projectMonitor = new Monitor("project", m_monitorManager, this);
126     projectMonitorDock->setWidget(m_projectMonitor);
127     addDockWidget(Qt::TopDockWidgetArea, projectMonitorDock);
128
129     recMonitorDock = new QDockWidget(i18n("Record Monitor"), this);
130     recMonitorDock->setObjectName("record_monitor");
131     m_recMonitor = new RecMonitor("record", this);
132     recMonitorDock->setWidget(m_recMonitor);
133     addDockWidget(Qt::TopDockWidgetArea, recMonitorDock);
134
135     connect(m_recMonitor, SIGNAL(addProjectClip(KUrl)), this, SLOT(slotAddProjectClip(KUrl)));
136
137     undoViewDock = new QDockWidget(i18n("Undo History"), this);
138     undoViewDock->setObjectName("undo_history");
139     m_undoView = new QUndoView(this);
140     m_undoView->setCleanIcon(KIcon("edit-clear"));
141     m_undoView->setEmptyLabel(i18n("Clean"));
142     undoViewDock->setWidget(m_undoView);
143     m_undoView->setGroup(m_commandStack);
144     addDockWidget(Qt::TopDockWidgetArea, undoViewDock);
145
146     overviewDock = new QDockWidget(i18n("Project Overview"), this);
147     overviewDock->setObjectName("project_overview");
148     m_overView = new CustomTrackView(NULL, NULL, this);
149     overviewDock->setWidget(m_overView);
150     addDockWidget(Qt::TopDockWidgetArea, overviewDock);
151
152     setupActions();
153     tabifyDockWidget(projectListDock, effectListDock);
154     tabifyDockWidget(projectListDock, effectStackDock);
155     tabifyDockWidget(projectListDock, transitionConfigDock);
156     tabifyDockWidget(projectListDock, undoViewDock);
157     projectListDock->raise();
158
159     tabifyDockWidget(clipMonitorDock, projectMonitorDock);
160     setCentralWidget(m_timelineArea);
161
162     m_timecodeFormat = new KComboBox(this);
163     m_timecodeFormat->addItem(i18n("hh:mm:ss::ff"));
164     m_timecodeFormat->addItem(i18n("Frames"));
165
166     statusProgressBar = new QProgressBar(this);
167     statusProgressBar->setMinimum(0);
168     statusProgressBar->setMaximum(100);
169     statusProgressBar->setMaximumWidth(150);
170     statusProgressBar->setVisible(false);
171     statusLabel = new QLabel(this);
172
173     QWidget *w = new QWidget;
174     timeline_buttons_ui.setupUi(w);
175     timeline_buttons_ui.buttonVideo->setDown(KdenliveSettings::videothumbnails());
176     timeline_buttons_ui.buttonAudio->setDown(KdenliveSettings::audiothumbnails());
177     connect(timeline_buttons_ui.buttonVideo, SIGNAL(clicked()), this, SLOT(slotSwitchVideoThumbs()));
178     connect(timeline_buttons_ui.buttonAudio, SIGNAL(clicked()), this, SLOT(slotSwitchAudioThumbs()));
179     connect(timeline_buttons_ui.buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotFitZoom()));
180
181     statusBar()->insertPermanentWidget(0, statusProgressBar, 1);
182     statusBar()->insertPermanentWidget(1, statusLabel, 1);
183     statusBar()->insertPermanentWidget(ID_TIMELINE_BUTTONS, w);
184     statusBar()->insertPermanentFixedItem("00:00:00:00", ID_TIMELINE_POS);
185     statusBar()->insertPermanentWidget(ID_TIMELINE_FORMAT, m_timecodeFormat);
186     statusBar()->setMaximumHeight(statusBar()->font().pointSize() * 4);
187
188     timeline_buttons_ui.buttonVideo->setIcon(KIcon("video-mpeg"));
189     timeline_buttons_ui.buttonVideo->setToolTip(i18n("Show video thumbnails"));
190     timeline_buttons_ui.buttonAudio->setIcon(KIcon("audio-mpeg"));
191     timeline_buttons_ui.buttonAudio->setToolTip(i18n("Show audio thumbnails"));
192     timeline_buttons_ui.buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
193     timeline_buttons_ui.buttonFitZoom->setToolTip(i18n("Fit zoom to project"));
194
195     setupGUI(Default, NULL /*"kdenliveui.rc"*/);
196     kDebug() << factory() << " " << factory()->container("video_effects_menu", this);
197
198     // build effects menus
199     QAction *action;
200     QMenu *videoEffectsMenu = (QMenu*)(factory()->container("video_effects_menu", this));
201     QStringList effects = m_videoEffects.effectNames();
202     foreach(QString name, effects) {
203         action = new QAction(name, this);
204         action->setData(name);
205         videoEffectsMenu->addAction(action);
206     }
207     QMenu *audioEffectsMenu = (QMenu*)(factory()->container("audio_effects_menu", this));
208     effects = m_audioEffects.effectNames();
209     foreach(QString name, effects) {
210         action = new QAction(name, this);
211         action->setData(name);
212         audioEffectsMenu->addAction(action);
213     }
214     QMenu *customEffectsMenu = (QMenu*)(factory()->container("custom_effects_menu", this));
215     effects = m_customEffects.effectNames();
216     foreach(QString name, effects) {
217         action = new QAction(name, this);
218         action->setData(name);
219         customEffectsMenu->addAction(action);
220     }
221
222     connect(videoEffectsMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotAddVideoEffect(QAction *)));
223     connect(audioEffectsMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotAddAudioEffect(QAction *)));
224     connect(customEffectsMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotAddCustomEffect(QAction *)));
225
226     m_timelineContextMenu = new QMenu(this);
227     m_timelineContextClipMenu = new QMenu(this);
228     m_timelineContextTransitionMenu = new QMenu(this);
229
230     action = actionCollection()->action("delete_timeline_clip");
231     m_timelineContextClipMenu->addAction(action);
232     m_timelineContextClipMenu->addMenu(videoEffectsMenu);
233     m_timelineContextClipMenu->addMenu(audioEffectsMenu);
234     m_timelineContextClipMenu->addMenu(customEffectsMenu);
235
236     connect(projectMonitorDock, SIGNAL(visibilityChanged(bool)), m_projectMonitor, SLOT(refreshMonitor(bool)));
237     connect(clipMonitorDock, SIGNAL(visibilityChanged(bool)), m_clipMonitor, SLOT(refreshMonitor(bool)));
238     //connect(m_monitorManager, SIGNAL(connectMonitors()), this, SLOT(slotConnectMonitors()));
239     connect(m_monitorManager, SIGNAL(raiseClipMonitor(bool)), this, SLOT(slotRaiseMonitor(bool)));
240     connect(m_effectList, SIGNAL(addEffect(QDomElement)), this, SLOT(slotAddEffect(QDomElement)));
241     m_monitorManager->initMonitors(m_clipMonitor, m_projectMonitor);
242     slotConnectMonitors();
243
244     setAutoSaveSettings();
245     newFile();
246
247     activateShuttleDevice();
248 }
249
250 //virtual
251 bool MainWindow::queryClose() {
252     saveOptions();
253     switch (KMessageBox::warningYesNoCancel(this, i18n("Save changes to document ?"))) {
254     case KMessageBox::Yes :
255         // save document here. If saving fails, return false;
256         return true;
257     case KMessageBox::No :
258         return true;
259     default: // cancel
260         return false;
261     }
262 }
263
264 void MainWindow::activateShuttleDevice() {
265     if (m_jogProcess) delete m_jogProcess;
266     m_jogProcess = NULL;
267     if (KdenliveSettings::enableshuttle() == false) return;
268     m_jogProcess = new JogShuttle(KdenliveSettings::shuttledevice());
269     connect(m_jogProcess, SIGNAL(rewind1()), m_monitorManager, SLOT(slotRewindOneFrame()));
270     connect(m_jogProcess, SIGNAL(forward1()), m_monitorManager, SLOT(slotForwardOneFrame()));
271     connect(m_jogProcess, SIGNAL(rewind(double)), m_monitorManager, SLOT(slotRewind(double)));
272     connect(m_jogProcess, SIGNAL(forward(double)), m_monitorManager, SLOT(slotForward(double)));
273     connect(m_jogProcess, SIGNAL(stop()), m_monitorManager, SLOT(slotPlay()));
274     connect(m_jogProcess, SIGNAL(button(int)), this, SLOT(slotShuttleButton(int)));
275 }
276
277 void MainWindow::slotShuttleButton(int code) {
278     switch (code) {
279     case 5:
280         slotShuttleAction(KdenliveSettings::shuttle1());
281         break;
282     case 6:
283         slotShuttleAction(KdenliveSettings::shuttle2());
284         break;
285     case 7:
286         slotShuttleAction(KdenliveSettings::shuttle3());
287         break;
288     case 8:
289         slotShuttleAction(KdenliveSettings::shuttle4());
290         break;
291     case 9:
292         slotShuttleAction(KdenliveSettings::shuttle5());
293         break;
294     }
295 }
296
297 void MainWindow::slotShuttleAction(int code) {
298     switch (code) {
299     case 0:
300         return;
301     case 1:
302         m_monitorManager->slotPlay();
303         break;
304     default:
305         m_monitorManager->slotPlay();
306         break;
307     }
308 }
309
310 void MainWindow::slotFullScreen() {
311     //KToggleFullScreenAction::setFullScreen(this, actionCollection()->action("fullscreen")->isChecked());
312 }
313
314 void MainWindow::slotAddEffect(QDomElement effect, GenTime pos, int track) {
315     if (!m_activeDocument) return;
316     if (effect.isNull()) {
317         kDebug() << "--- ERROR, TRYING TO APPEND NULL EFFECT";
318         return;
319     }
320     TrackView *currentTimeLine = (TrackView *) m_timelineArea->currentWidget();
321     currentTimeLine->projectView()->slotAddEffect(effect, pos, track);
322 }
323
324 void MainWindow::slotRaiseMonitor(bool clipMonitor) {
325     if (clipMonitor) clipMonitorDock->raise();
326     else projectMonitorDock->raise();
327 }
328
329 void MainWindow::slotSetClipDuration(int id, int duration) {
330     if (!m_activeDocument) return;
331     m_activeDocument->setProducerDuration(id, duration);
332 }
333
334 void MainWindow::slotConnectMonitors() {
335
336     m_projectList->setRenderer(m_clipMonitor->render);
337     connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
338     connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
339     connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
340     connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
341     connect(m_clipMonitor->render, SIGNAL(replyGetFileProperties(int, const QMap < QString, QString > &, const QMap < QString, QString > &)), m_projectList, SLOT(slotReplyGetFileProperties(int, const QMap < QString, QString > &, const QMap < QString, QString > &)));
342 }
343
344 void MainWindow::setupActions() {
345     KAction* clearAction = new KAction(this);
346     clearAction->setText(i18n("Clear"));
347     clearAction->setIcon(KIcon("document-new"));
348     clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
349     actionCollection()->addAction("clear", clearAction);
350     /*connect(clearAction, SIGNAL(triggered(bool)),
351             textArea, SLOT(clear()));*/
352
353     KAction* profilesAction = new KAction(this);
354     profilesAction->setText(i18n("Manage Profiles"));
355     profilesAction->setIcon(KIcon("document-new"));
356     actionCollection()->addAction("manage_profiles", profilesAction);
357     connect(profilesAction, SIGNAL(triggered(bool)), this, SLOT(slotEditProfiles()));
358
359     KAction* projectAction = new KAction(this);
360     projectAction->setText(i18n("Project Settings"));
361     projectAction->setIcon(KIcon("document-new"));
362     actionCollection()->addAction("project_settings", projectAction);
363     connect(projectAction, SIGNAL(triggered(bool)), this, SLOT(slotEditProjectSettings()));
364
365     KAction* projectRender = new KAction(this);
366     projectRender->setText(i18n("Render Project"));
367     projectRender->setIcon(KIcon("document-new"));
368     actionCollection()->addAction("project_render", projectRender);
369     connect(projectRender, SIGNAL(triggered(bool)), this, SLOT(slotRenderProject()));
370
371     KAction* monitorPlay = new KAction(this);
372     monitorPlay->setText(i18n("Play"));
373     monitorPlay->setIcon(KIcon("media-playback-start"));
374     monitorPlay->setShortcut(Qt::Key_Space);
375     actionCollection()->addAction("monitor_play", monitorPlay);
376     connect(monitorPlay, SIGNAL(triggered(bool)), m_monitorManager, SLOT(slotPlay()));
377
378     KAction* monitorSeekBackward = new KAction(this);
379     monitorSeekBackward->setText(i18n("Rewind"));
380     monitorSeekBackward->setIcon(KIcon("media-seek-backward"));
381     monitorSeekBackward->setShortcut(Qt::Key_J);
382     actionCollection()->addAction("monitor_seek_backward", monitorSeekBackward);
383     connect(monitorSeekBackward, SIGNAL(triggered(bool)), m_monitorManager, SLOT(slotRewind()));
384
385     KAction* monitorSeekBackwardOneFrame = new KAction(this);
386     monitorSeekBackwardOneFrame->setText(i18n("Rewind 1 Frame"));
387     monitorSeekBackwardOneFrame->setIcon(KIcon("media-skip-backward"));
388     monitorSeekBackwardOneFrame->setShortcut(Qt::Key_Left);
389     actionCollection()->addAction("monitor_seek_backward-one-frame", monitorSeekBackwardOneFrame);
390     connect(monitorSeekBackwardOneFrame, SIGNAL(triggered(bool)), m_monitorManager, SLOT(slotRewindOneFrame()));
391
392     KAction* monitorSeekForward = new KAction(this);
393     monitorSeekForward->setText(i18n("Forward"));
394     monitorSeekForward->setIcon(KIcon("media-seek-forward"));
395     monitorSeekForward->setShortcut(Qt::Key_L);
396     actionCollection()->addAction("monitor_seek_forward", monitorSeekForward);
397     connect(monitorSeekForward, SIGNAL(triggered(bool)), m_monitorManager, SLOT(slotForward()));
398
399     KAction* monitorSeekForwardOneFrame = new KAction(this);
400     monitorSeekForwardOneFrame->setText(i18n("Forward 1 Frame"));
401     monitorSeekForwardOneFrame->setIcon(KIcon("media-skip-forward"));
402     monitorSeekForwardOneFrame->setShortcut(Qt::Key_Right);
403     actionCollection()->addAction("monitor_seek_forward-one-frame", monitorSeekForwardOneFrame);
404     connect(monitorSeekForwardOneFrame, SIGNAL(triggered(bool)), m_monitorManager, SLOT(slotForwardOneFrame()));
405
406     KAction* deleteTimelineClip = new KAction(this);
407     deleteTimelineClip->setText(i18n("Delete Clip"));
408     deleteTimelineClip->setShortcut(Qt::Key_Delete);
409     deleteTimelineClip->setIcon(KIcon("edit-delete"));
410     actionCollection()->addAction("delete_timeline_clip", deleteTimelineClip);
411     connect(deleteTimelineClip, SIGNAL(triggered(bool)), this, SLOT(slotDeleteTimelineClip()));
412
413     KStandardAction::quit(kapp, SLOT(quit()),
414                           actionCollection());
415
416     KStandardAction::open(this, SLOT(openFile()),
417                           actionCollection());
418
419     m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
420                        actionCollection());
421
422     KStandardAction::save(this, SLOT(saveFile()),
423                           actionCollection());
424
425     KStandardAction::saveAs(this, SLOT(saveFileAs()),
426                             actionCollection());
427
428     KStandardAction::openNew(this, SLOT(newFile()),
429                              actionCollection());
430
431     KStandardAction::preferences(this, SLOT(slotPreferences()),
432                                  actionCollection());
433
434     KStandardAction::undo(this, SLOT(undo()),
435                           actionCollection());
436
437     KStandardAction::redo(this, SLOT(redo()),
438                           actionCollection());
439
440     KStandardAction::fullScreen(this, SLOT(slotFullScreen()), this, actionCollection());
441
442     connect(actionCollection(), SIGNAL(actionHovered(QAction*)),
443             this, SLOT(slotDisplayActionMessage(QAction*)));
444     //connect(actionCollection(), SIGNAL( clearStatusText() ),
445     //statusBar(), SLOT( clear() ) );
446
447     readOptions();
448 }
449
450 void MainWindow::undo() {
451     m_commandStack->undo();
452 }
453
454 void MainWindow::redo() {
455     m_commandStack->redo();
456 }
457
458 void MainWindow::slotDisplayActionMessage(QAction *a) {
459     statusBar()->showMessage(a->data().toString(), 3000);
460 }
461
462 void MainWindow::saveOptions() {
463     KSharedConfigPtr config = KGlobal::config();
464     m_fileOpenRecent->saveEntries(KConfigGroup(config, "Recent Files"));
465     config->sync();
466 }
467
468 void MainWindow::readOptions() {
469     KSharedConfigPtr config = KGlobal::config();
470     m_fileOpenRecent->loadEntries(KConfigGroup(config, "Recent Files"));
471 }
472
473 void MainWindow::newFile() {
474     QString profileName;
475     if (m_timelineArea->count() == 0) profileName = KdenliveSettings::default_profile();
476     else {
477         ProjectSettings *w = new ProjectSettings;
478         w->exec();
479         profileName = w->selectedProfile();
480         delete w;
481     }
482     MltVideoProfile prof = ProfilesDialog::getVideoProfile(profileName);
483     if (prof.width == 0) prof = ProfilesDialog::getVideoProfile("dv_pal");
484     KdenliveDoc *doc = new KdenliveDoc(KUrl(), prof, m_commandStack);
485     TrackView *trackView = new TrackView(doc, this);
486     m_timelineArea->addTab(trackView, KIcon("kdenlive"), i18n("Untitled") + " / " + prof.description);
487     if (m_timelineArea->count() == 1)
488         connectDocument(trackView, doc);
489     else m_timelineArea->setTabBarHidden(false);
490 }
491
492 void MainWindow::activateDocument() {
493     TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
494     KdenliveDoc *currentDoc = currentTab->document();
495     connectDocument(currentTab, currentDoc);
496 }
497
498 void MainWindow::slotRemoveTab() {
499     QWidget *w = m_timelineArea->currentWidget();
500     // closing current document
501     int ix = m_timelineArea->currentIndex() + 1;
502     if (ix == m_timelineArea->count()) ix = 0;
503     m_timelineArea->setCurrentIndex(ix);
504     TrackView *tabToClose = (TrackView *) w;
505     KdenliveDoc *docToClose = tabToClose->document();
506     m_timelineArea->removeTab(m_timelineArea->indexOf(w));
507     if (m_timelineArea->count() == 1) m_timelineArea->setTabBarHidden(true);
508     delete docToClose;
509     delete w;
510 }
511
512 void MainWindow::saveFileAs(const QString &outputFileName) {
513     m_projectMonitor->saveSceneList(outputFileName, m_activeDocument->documentInfoXml());
514     m_activeDocument->setUrl(KUrl(outputFileName));
515     setCaption(m_activeDocument->description());
516     m_timelineArea->setTabText(m_timelineArea->currentIndex(), m_activeDocument->description());
517     m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), m_activeDocument->url().path());
518     m_activeDocument->setModified(false);
519 }
520
521 void MainWindow::saveFileAs() {
522     QString outputFile = KFileDialog::getSaveFileName();
523     if (QFile::exists(outputFile)) {
524         if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it ?")) == KMessageBox::No) return;
525     }
526     saveFileAs(outputFile);
527 }
528
529 void MainWindow::saveFile() {
530     if (!m_activeDocument) return;
531     if (m_activeDocument->url().isEmpty()) {
532         saveFileAs();
533     } else {
534         saveFileAs(m_activeDocument->url().path());
535     }
536 }
537
538 void MainWindow::openFile() { //changed
539     KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
540     if (url.isEmpty()) return;
541     m_fileOpenRecent->addUrl(url);
542     openFile(url);
543 }
544
545 void MainWindow::openFile(const KUrl &url) { //new
546     //TODO: get video profile from url before opening it
547     MltVideoProfile prof = ProfilesDialog::getVideoProfile(KdenliveSettings::default_profile());
548     if (prof.width == 0) prof = ProfilesDialog::getVideoProfile("dv_pal");
549     KdenliveDoc *doc = new KdenliveDoc(url, prof, m_commandStack);
550     TrackView *trackView = new TrackView(doc, this);
551     m_timelineArea->setCurrentIndex(m_timelineArea->addTab(trackView, KIcon("kdenlive"), doc->description()));
552     m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), doc->url().path());
553     if (m_timelineArea->count() > 1) m_timelineArea->setTabBarHidden(false);
554     //connectDocument(trackView, doc);
555 }
556
557
558 void MainWindow::parseProfiles() {
559     //kdDebug()<<" + + YOUR MLT INSTALL WAS FOUND IN: "<< MLT_PREFIX <<endl;
560     if (KdenliveSettings::mltpath().isEmpty()) {
561         KdenliveSettings::setMltpath(QString(MLT_PREFIX) + QString("/share/mlt/profiles/"));
562     }
563     if (KdenliveSettings::rendererpath().isEmpty()) {
564         KdenliveSettings::setRendererpath(KStandardDirs::findExe("inigo"));
565     }
566     QStringList profilesFilter;
567     profilesFilter << "*";
568     QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
569
570     if (profilesList.isEmpty()) {
571         // Cannot find MLT path, try finding inigo
572         QString profilePath = KdenliveSettings::rendererpath();
573         if (!profilePath.isEmpty()) {
574             profilePath = profilePath.section('/', 0, -3);
575             KdenliveSettings::setMltpath(profilePath + "/share/mlt/profiles/");
576             QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
577         }
578
579         if (profilesList.isEmpty()) {
580             // Cannot find the MLT profiles, ask for location
581             KUrlRequesterDialog *getUrl = new KUrlRequesterDialog(KdenliveSettings::mltpath(), i18n("Cannot find your Mlt profiles, please give the path"), this);
582             getUrl->fileDialog()->setMode(KFile::Directory);
583             getUrl->exec();
584             KUrl mltPath = getUrl->selectedUrl();
585             delete getUrl;
586             if (mltPath.isEmpty()) exit(1);
587             KdenliveSettings::setMltpath(mltPath.path());
588             QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
589         }
590     }
591
592     if (KdenliveSettings::rendererpath().isEmpty()) {
593         // Cannot find the MLT inigo renderer, ask for location
594         KUrlRequesterDialog *getUrl = new KUrlRequesterDialog(KdenliveSettings::mltpath(), i18n("Cannot find the inigo program required for rendering (part of Mlt)"), this);
595         getUrl->exec();
596         KUrl rendererPath = getUrl->selectedUrl();
597         delete getUrl;
598         if (rendererPath.isEmpty()) exit(1);
599         KdenliveSettings::setRendererpath(rendererPath.path());
600     }
601
602     kDebug() << "RESULTING MLT PATH: " << KdenliveSettings::mltpath();
603
604     // Parse MLT profiles to build a list of available video formats
605     if (profilesList.isEmpty()) parseProfiles();
606 }
607
608
609 void MainWindow::slotEditProfiles() {
610     ProfilesDialog *w = new ProfilesDialog;
611     w->exec();
612     delete w;
613 }
614
615 void MainWindow::slotEditProjectSettings() {
616     ProjectSettings *w = new ProjectSettings;
617     if (w->exec() == QDialog::Accepted) {
618         QString profile = w->selectedProfile();
619         m_activeDocument->setProfilePath(profile);
620         m_monitorManager->resetProfiles(profile);
621         setCaption(m_activeDocument->description());
622         KdenliveSettings::setCurrent_profile(m_activeDocument->profilePath());
623         if (m_renderWidget) m_renderWidget->setDocumentStandard(m_activeDocument->getDocumentStandard());
624         m_monitorManager->setTimecode(m_activeDocument->timecode());
625         m_timelineArea->setTabText(m_timelineArea->currentIndex(), m_activeDocument->description());
626
627         // We need to desactivate & reactivate monitors to get a refresh
628         m_monitorManager->switchMonitors();
629     }
630     delete w;
631 }
632
633 void MainWindow::slotRenderProject() {
634     if (!m_renderWidget) {
635         m_renderWidget = new RenderWidget(this);
636         connect(m_renderWidget, SIGNAL(doRender(const QString&, const QString&, const QStringList &, bool, bool)), this, SLOT(slotDoRender(const QString&, const QString&, const QStringList &, bool, bool)));
637     }
638     /*TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
639     if (currentTab) m_renderWidget->setTimeline(currentTab);
640     m_renderWidget->setDocument(m_activeDocument);*/
641     m_renderWidget->show();
642 }
643
644 void MainWindow::slotDoRender(const QString &dest, const QString &render, const QStringList &avformat_args, bool zoneOnly, bool playAfter) {
645     if (dest.isEmpty()) return;
646     int in;
647     int out;
648     TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
649     if (currentTab && zoneOnly) {
650         in = currentTab->inPoint();
651         out = currentTab->outPoint();
652     }
653     KTemporaryFile temp;
654     temp.setAutoRemove(false);
655     temp.setSuffix(".westley");
656     if (temp.open()) {
657         m_projectMonitor->saveSceneList(temp.fileName());
658         QStringList args;
659         args << "-erase";
660         if (zoneOnly) args << "in=" + QString::number(in) << "out=" + QString::number(out);
661         QString videoPlayer = "-";
662         if (playAfter) videoPlayer = "kmplayer";
663         args << "inigo" << m_activeDocument->profilePath() << render << videoPlayer << temp.fileName() << dest << avformat_args;
664         QProcess::startDetached("kdenlive_render", args);
665     }
666 }
667
668 void MainWindow::slotUpdateMousePosition(int pos) {
669     if (m_activeDocument)
670         switch (m_timecodeFormat->currentIndex()) {
671         case 0:
672             statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
673             break;
674         default:
675             statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
676         }
677 }
678
679 void MainWindow::slotUpdateDocumentState(bool modified) {
680     setCaption(m_activeDocument->description(), modified);
681     if (modified) {
682         m_timelineArea->setTabTextColor(m_timelineArea->currentIndex(), palette().color(QPalette::Link));
683         m_timelineArea->setTabIcon(m_timelineArea->currentIndex(), KIcon("document-save"));
684     } else {
685         m_timelineArea->setTabTextColor(m_timelineArea->currentIndex(), palette().color(QPalette::Text));
686         m_timelineArea->setTabIcon(m_timelineArea->currentIndex(), KIcon("kdenlive"));
687     }
688 }
689
690 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) { //changed
691     //m_projectMonitor->stop();
692     kDebug() << "///////////////////   CONNECTING DOC TO PROJECT VIEW ////////////////";
693     if (m_activeDocument) {
694         if (m_activeDocument == doc) return;
695         m_activeDocument->backupMltPlaylist();
696         if (m_activeTimeline) {
697             disconnect(m_projectMonitor, SIGNAL(renderPosition(int)), m_activeTimeline, SLOT(moveCursorPos(int)));
698             disconnect(m_projectMonitor, SIGNAL(durationChanged(int)), m_activeTimeline, SLOT(setDuration(int)));
699             disconnect(m_activeDocument, SIGNAL(addProjectClip(DocClipBase *)), m_projectList, SLOT(slotAddClip(DocClipBase *)));
700             disconnect(m_activeDocument, SIGNAL(signalDeleteProjectClip(int)), m_projectList, SLOT(slotDeleteClip(int)));
701             disconnect(m_activeDocument, SIGNAL(updateClipDisplay(int)), m_projectList, SLOT(slotUpdateClip(int)));
702             disconnect(m_activeDocument, SIGNAL(deletTimelineClip(int)), m_activeTimeline, SLOT(slotDeleteClip(int)));
703             disconnect(m_activeDocument, SIGNAL(thumbsProgress(KUrl, int)), this, SLOT(slotGotProgressInfo(KUrl, int)));
704             disconnect(m_activeTimeline, SIGNAL(clipItemSelected(ClipItem*)), effectStack, SLOT(slotClipItemSelected(ClipItem*)));
705             disconnect(m_activeTimeline, SIGNAL(transitionItemSelected(Transition*)), transitionConfig, SLOT(slotTransitionItemSelected(Transition*)));
706             disconnect(timeline_buttons_ui.zoom_slider, SIGNAL(valueChanged(int)), m_activeTimeline, SLOT(slotChangeZoom(int)));
707             disconnect(m_activeDocument, SIGNAL(docModified(bool)), this, SLOT(slotUpdateDocumentState(bool)));
708             disconnect(effectStack, SIGNAL(updateClipEffect(ClipItem*, QDomElement, QDomElement)), m_activeTimeline->projectView(), SLOT(slotUpdateClipEffect(ClipItem*, QDomElement, QDomElement)));
709             disconnect(effectStack, SIGNAL(removeEffect(ClipItem*, QDomElement)), m_activeTimeline->projectView(), SLOT(slotDeleteEffect(ClipItem*, QDomElement)));
710             disconnect(effectStack, SIGNAL(changeEffectState(ClipItem*, QDomElement, bool)), m_activeTimeline->projectView(), SLOT(slotChangeEffectState(ClipItem*, QDomElement, bool)));
711             disconnect(effectStack, SIGNAL(refreshEffectStack(ClipItem*)), m_activeTimeline->projectView(), SLOT(slotRefreshEffects(ClipItem*)));
712             disconnect(transitionConfig, SIGNAL(transitionUpdated(QDomElement, QDomElement)), trackView->projectView() , SLOT(slotTransitionUpdated(QDomElement, QDomElement)));
713             disconnect(m_activeTimeline->projectView(), SIGNAL(activateDocumentMonitor()), m_projectMonitor, SLOT(activateMonitor()));
714         }
715         m_activeDocument->setRenderer(NULL);
716         disconnect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
717         m_clipMonitor->stop();
718     }
719     m_monitorManager->resetProfiles(doc->profilePath());
720     m_projectList->setDocument(doc);
721     connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
722     connect(trackView, SIGNAL(cursorMoved()), m_projectMonitor, SLOT(activateMonitor()));
723     connect(trackView, SIGNAL(mousePosition(int)), this, SLOT(slotUpdateMousePosition(int)));
724     connect(m_projectMonitor, SIGNAL(renderPosition(int)), trackView, SLOT(moveCursorPos(int)));
725     connect(m_projectMonitor, SIGNAL(durationChanged(int)), trackView, SLOT(setDuration(int)));
726     connect(doc, SIGNAL(addProjectClip(DocClipBase *)), m_projectList, SLOT(slotAddClip(DocClipBase *)));
727     connect(doc, SIGNAL(signalDeleteProjectClip(int)), m_projectList, SLOT(slotDeleteClip(int)));
728     connect(doc, SIGNAL(updateClipDisplay(int)), m_projectList, SLOT(slotUpdateClip(int)));
729     connect(doc, SIGNAL(deletTimelineClip(int)), trackView, SLOT(slotDeleteClip(int)));
730     connect(doc, SIGNAL(thumbsProgress(KUrl, int)), this, SLOT(slotGotProgressInfo(KUrl, int)));
731     connect(doc, SIGNAL(docModified(bool)), this, SLOT(slotUpdateDocumentState(bool)));
732
733     connect(trackView, SIGNAL(clipItemSelected(ClipItem*)), effectStack, SLOT(slotClipItemSelected(ClipItem*)));
734     connect(trackView, SIGNAL(transitionItemSelected(Transition*)), transitionConfig, SLOT(slotTransitionItemSelected(Transition*)));
735     timeline_buttons_ui.zoom_slider->setValue(trackView->currentZoom());
736     connect(timeline_buttons_ui.zoom_slider, SIGNAL(valueChanged(int)), trackView, SLOT(slotChangeZoom(int)));
737     connect(trackView->projectView(), SIGNAL(zoomIn()), this, SLOT(slotZoomIn()));
738     connect(trackView->projectView(), SIGNAL(zoomOut()), this, SLOT(slotZoomOut()));
739     connect(effectStack, SIGNAL(updateClipEffect(ClipItem*, QDomElement, QDomElement)), trackView->projectView(), SLOT(slotUpdateClipEffect(ClipItem*, QDomElement, QDomElement)));
740     connect(effectStack, SIGNAL(removeEffect(ClipItem*, QDomElement)), trackView->projectView(), SLOT(slotDeleteEffect(ClipItem*, QDomElement)));
741     connect(effectStack, SIGNAL(changeEffectState(ClipItem*, QDomElement, bool)), trackView->projectView(), SLOT(slotChangeEffectState(ClipItem*, QDomElement, bool)));
742     connect(effectStack, SIGNAL(refreshEffectStack(ClipItem*)), trackView->projectView(), SLOT(slotRefreshEffects(ClipItem*)));
743     connect(transitionConfig, SIGNAL(transitionUpdated(QDomElement, QDomElement)), trackView->projectView() , SLOT(slotTransitionUpdated(QDomElement, QDomElement)));
744     connect(trackView->projectView(), SIGNAL(activateDocumentMonitor()), m_projectMonitor, SLOT(activateMonitor()));
745     trackView->projectView()->setContextMenu(m_timelineContextMenu, m_timelineContextClipMenu, m_timelineContextTransitionMenu);
746     m_activeTimeline = trackView;
747     KdenliveSettings::setCurrent_profile(doc->profilePath());
748     if (m_renderWidget) m_renderWidget->setDocumentStandard(doc->getDocumentStandard());
749     m_monitorManager->setTimecode(doc->timecode());
750     doc->setRenderer(m_projectMonitor->render);
751     m_commandStack->setActiveStack(doc->commandStack());
752     if (m_commandStack->isClean()) kDebug() << "////////////  UNDO STACK IS CLEAN";
753     else  kDebug() << "////////////  UNDO STACK IS NOT CLEAN*******************";
754
755     m_overView->setScene(trackView->projectScene());
756     //m_overView->scale(m_overView->width() / trackView->duration(), m_overView->height() / (50 * trackView->tracksNumber()));
757     //m_overView->fitInView(m_overView->itemAt(0, 50), Qt::KeepAspectRatio);
758
759     setCaption(doc->description());
760     m_activeDocument = doc;
761 }
762
763 void MainWindow::slotPreferences() {
764     //An instance of your dialog could be already created and could be
765     // cached, in which case you want to display the cached dialog
766     // instead of creating another one
767     if (KConfigDialog::showDialog("settings"))
768         return;
769
770     // KConfigDialog didn't find an instance of this dialog, so lets
771     // create it :
772     KdenliveSettingsDialog* dialog = new KdenliveSettingsDialog(this);
773     connect(dialog, SIGNAL(settingsChanged(const QString&)), this, SLOT(updateConfiguration()));
774     dialog->show();
775 }
776
777 void MainWindow::updateConfiguration() {
778     //TODO: we should apply settings to all projects, not only the current one
779     TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
780     if (currentTab) {
781         currentTab->refresh();
782         currentTab->projectView()->checkAutoScroll();
783         currentTab->projectView()->checkTrackHeight();
784         if (m_activeDocument) m_activeDocument->clipManager()->checkAudioThumbs();
785     }
786     timeline_buttons_ui.buttonAudio->setDown(KdenliveSettings::audiothumbnails());
787     timeline_buttons_ui.buttonVideo->setDown(KdenliveSettings::videothumbnails());
788     activateShuttleDevice();
789
790 }
791
792 void MainWindow::slotSwitchVideoThumbs() {
793     KdenliveSettings::setVideothumbnails(!KdenliveSettings::videothumbnails());
794     TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
795     if (currentTab) {
796         currentTab->refresh();
797     }
798     timeline_buttons_ui.buttonVideo->setDown(KdenliveSettings::videothumbnails());
799 }
800
801 void MainWindow::slotSwitchAudioThumbs() {
802     KdenliveSettings::setAudiothumbnails(!KdenliveSettings::audiothumbnails());
803     TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
804     if (currentTab) {
805         currentTab->refresh();
806         currentTab->projectView()->checkAutoScroll();
807         if (m_activeDocument) m_activeDocument->clipManager()->checkAudioThumbs();
808     }
809     timeline_buttons_ui.buttonAudio->setDown(KdenliveSettings::audiothumbnails());
810 }
811
812 void MainWindow::slotDeleteTimelineClip() {
813     TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
814     if (currentTab) {
815         currentTab->projectView()->deleteSelectedClips();
816     }
817 }
818
819 void MainWindow::slotAddProjectClip(KUrl url) {
820     if (m_activeDocument)
821         m_activeDocument->slotAddClipFile(url, QString());
822 }
823
824 void MainWindow::slotAddVideoEffect(QAction *result) {
825     if (!result) return;
826     QDomElement effect = m_videoEffects.getEffectByName(result->data().toString());
827     slotAddEffect(effect);
828 }
829
830 void MainWindow::slotAddAudioEffect(QAction *result) {
831     if (!result) return;
832     QDomElement effect = m_audioEffects.getEffectByName(result->data().toString());
833     slotAddEffect(effect);
834 }
835
836 void MainWindow::slotAddCustomEffect(QAction *result) {
837     if (!result) return;
838     QDomElement effect = m_customEffects.getEffectByName(result->data().toString());
839     slotAddEffect(effect);
840 }
841
842 void MainWindow::slotZoomIn() {
843     timeline_buttons_ui.zoom_slider->setValue(timeline_buttons_ui.zoom_slider->value() - 1);
844 }
845
846 void MainWindow::slotZoomOut() {
847     timeline_buttons_ui.zoom_slider->setValue(timeline_buttons_ui.zoom_slider->value() + 1);
848 }
849
850 void MainWindow::slotFitZoom() {
851     TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
852     if (currentTab) {
853         timeline_buttons_ui.zoom_slider->setValue(currentTab->fitZoom());
854     }
855 }
856
857 void MainWindow::slotGotProgressInfo(KUrl url, int progress) {
858     statusProgressBar->setValue(progress);
859     if (progress > 0) {
860         statusLabel->setText(tr("Creating Audio Thumbs"));
861         statusProgressBar->setVisible(true);
862     } else {
863         statusLabel->setText("");
864         statusProgressBar->setVisible(false);
865     }
866 }
867
868 void MainWindow::customEvent(QEvent* e) {
869     if (e->type() == QEvent::User) {
870         // The timeline playing position changed...
871         kDebug() << "RECIEVED JOG EVEMNT!!!";
872     }
873 }
874
875
876 #include "mainwindow.moc"