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