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