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