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