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