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