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