]> git.sesse.net Git - kdenlive/blob - src/mainwindow.cpp
Effects are now stored in clip as xml, get ready for effectstack connection
[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
45 #include <mlt++/Mlt.h>
46
47 #include "mainwindow.h"
48 #include "kdenlivesettings.h"
49 #include "kdenlivesettingsdialog.h"
50 #include "initeffects.h"
51 #include "profilesdialog.h"
52 #include "projectsettings.h"
53
54 #define ID_STATUS_MSG 1
55 #define ID_EDITMODE_MSG 2
56 #define ID_TIMELINE_MSG 3
57 #define ID_TIMELINE_POS 4
58 #define ID_TIMELINE_FORMAT 5
59  
60 MainWindow::MainWindow(QWidget *parent)
61     : KXmlGuiWindow(parent),
62       fileName(QString()), m_activeDocument(NULL), m_commandStack(NULL)
63 {
64   parseProfiles();
65   m_timelineArea = new KTabWidget(this);
66   m_timelineArea->setHoverCloseButton(true);
67   m_timelineArea->setTabReorderingEnabled(true);
68   connect(m_timelineArea, SIGNAL(currentChanged (int)), this, SLOT(activateDocument()));
69
70   initEffects::parseEffectFiles( &m_audioEffects, &m_videoEffects );
71   m_monitorManager = new MonitorManager();
72
73   projectListDock = new QDockWidget(i18n("Project Tree"), this);
74   projectListDock->setObjectName("project_tree");
75   m_projectList = new ProjectList(this);
76   projectListDock->setWidget(m_projectList);
77   addDockWidget(Qt::TopDockWidgetArea, projectListDock);
78
79   effectListDock = new QDockWidget(i18n("Effect List"), this);
80   effectListDock->setObjectName("effect_list");
81   m_effectList = new EffectsListView(&m_audioEffects, &m_videoEffects, &m_customEffects);
82
83   //m_effectList = new KListWidget(this);
84   effectListDock->setWidget(m_effectList);
85   addDockWidget(Qt::TopDockWidgetArea, effectListDock);
86   
87   effectStackDock = new QDockWidget(i18n("Effect Stack"), this);
88   effectStackDock->setObjectName("effect_stack");
89   effectStack = new EffectStackView(&m_audioEffects, &m_videoEffects, &m_customEffects,this);
90   effectStackDock->setWidget(effectStack);
91   addDockWidget(Qt::TopDockWidgetArea, effectStackDock);
92   
93   transitionConfigDock = new QDockWidget(i18n("Transition"), this);
94   transitionConfigDock->setObjectName("transition");
95   transitionConfig = new KListWidget(this);
96   transitionConfigDock->setWidget(transitionConfig);
97   addDockWidget(Qt::TopDockWidgetArea, transitionConfigDock);
98
99
100   clipMonitorDock = new QDockWidget(i18n("Clip Monitor"), this);
101   clipMonitorDock->setObjectName("clip_monitor");
102   m_clipMonitor = new Monitor("clip", m_monitorManager, this);
103   clipMonitorDock->setWidget(m_clipMonitor);
104   addDockWidget(Qt::TopDockWidgetArea, clipMonitorDock);
105   //m_clipMonitor->stop();
106
107   projectMonitorDock = new QDockWidget(i18n("Project Monitor"), this);
108   projectMonitorDock->setObjectName("project_monitor");
109   m_projectMonitor = new Monitor("project", m_monitorManager, this);
110   projectMonitorDock->setWidget(m_projectMonitor);
111   addDockWidget(Qt::TopDockWidgetArea, projectMonitorDock);
112
113   undoViewDock = new QDockWidget(i18n("Undo History"), this);
114   undoViewDock->setObjectName("undo_history");
115   m_undoView = new QUndoView(this);
116   undoViewDock->setWidget(m_undoView);
117   m_undoView->setStack(m_commandStack);
118   addDockWidget(Qt::TopDockWidgetArea, undoViewDock);
119
120   overviewDock = new QDockWidget(i18n("Project Overview"), this);
121   overviewDock->setObjectName("project_overview");
122   m_overView = new CustomTrackView(NULL, NULL, this);
123   overviewDock->setWidget(m_overView);
124   addDockWidget(Qt::TopDockWidgetArea, overviewDock);
125
126   setupActions();
127   tabifyDockWidget (projectListDock, effectListDock);
128   tabifyDockWidget (projectListDock, effectStackDock);
129   tabifyDockWidget (projectListDock, transitionConfigDock);
130   tabifyDockWidget (projectListDock, undoViewDock);
131   projectListDock->raise();
132
133   tabifyDockWidget (clipMonitorDock, projectMonitorDock);
134   setCentralWidget(m_timelineArea);
135
136   m_timecodeFormat = new KComboBox(this);
137   m_timecodeFormat->addItem(i18n("hh:mm:ss::ff"));
138   m_timecodeFormat->addItem(i18n("Frames"));
139   statusBar()->insertPermanentFixedItem("00:00:00:00", ID_TIMELINE_POS);
140   statusBar()->insertPermanentWidget(ID_TIMELINE_FORMAT, m_timecodeFormat); 
141
142   setupGUI(Default, "kdenliveui.rc");
143
144   connect(projectMonitorDock, SIGNAL(visibilityChanged (bool)), m_projectMonitor, SLOT(refreshMonitor(bool)));
145   connect(clipMonitorDock, SIGNAL(visibilityChanged (bool)), m_clipMonitor, SLOT(refreshMonitor(bool)));
146   connect(m_monitorManager, SIGNAL(connectMonitors ()), this, SLOT(slotConnectMonitors()));
147   connect(m_monitorManager, SIGNAL(raiseClipMonitor (bool)), this, SLOT(slotRaiseMonitor(bool)));
148   connect(m_effectList, SIGNAL(addEffect(QDomElement)), this, SLOT(slotAddEffect(QDomElement)));
149   m_monitorManager->initMonitors(m_clipMonitor, m_projectMonitor);
150         
151   setAutoSaveSettings();
152   newFile();
153 }
154
155 //virtual
156 bool MainWindow::queryClose() 
157 {
158   saveOptions();
159   switch ( KMessageBox::warningYesNoCancel( this, i18n("Save changes to document ?")) ) {
160        case KMessageBox::Yes :
161          // save document here. If saving fails, return false;
162          return true;
163        case KMessageBox::No :
164          return true;
165        default: // cancel
166          return false;
167   }
168 }
169
170 void MainWindow::slotAddEffect(QDomElement effect)
171 {
172   if (!m_activeDocument) return;
173   /*QMap <QString, QString> filter;
174   if (effectType == 0)
175     filter = m_videoEffects.effect(effectName);
176   else if (effectType == 1)
177     filter = m_audioEffects.effect(effectName);
178   else 
179     filter = m_customEffects.effect(effectName);*/
180   TrackView *currentTimeLine = (TrackView *) m_timelineArea->currentWidget();
181   currentTimeLine->projectView()->slotAddEffect(effect);
182 }
183
184 void MainWindow::slotRaiseMonitor(bool clipMonitor)
185 {
186   if (clipMonitor) clipMonitorDock->raise();
187   else projectMonitorDock->raise();
188 }
189
190 void MainWindow::slotSetClipDuration(int id, int duration)
191 {
192   if (!m_activeDocument) return;
193   m_activeDocument->setProducerDuration(id, duration);
194 }
195
196 void MainWindow::slotConnectMonitors()
197 {
198
199   m_projectList->setRenderer(m_clipMonitor->render);
200
201   connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
202
203   connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
204
205   connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
206
207   connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
208
209   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 > &)));
210
211 }
212
213 void MainWindow::setupActions()
214 {
215   KAction* clearAction = new KAction(this);
216   clearAction->setText(i18n("Clear"));
217   clearAction->setIcon(KIcon("document-new"));
218   clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
219   actionCollection()->addAction("clear", clearAction);
220   /*connect(clearAction, SIGNAL(triggered(bool)),
221           textArea, SLOT(clear()));*/
222
223   KAction* profilesAction = new KAction(this);
224   profilesAction->setText(i18n("Manage Profiles"));
225   profilesAction->setIcon(KIcon("document-new"));
226   actionCollection()->addAction("manage_profiles", profilesAction);
227   connect(profilesAction, SIGNAL(triggered(bool)), this, SLOT(slotEditProfiles()));
228
229   KAction* projectAction = new KAction(this);
230   projectAction->setText(i18n("Project Settings"));
231   projectAction->setIcon(KIcon("document-new"));
232   actionCollection()->addAction("project_settings", projectAction);
233   connect(projectAction, SIGNAL(triggered(bool)), this, SLOT(slotEditProjectSettings()));
234
235
236  
237   KStandardAction::quit(kapp, SLOT(quit()),
238                         actionCollection());
239  
240   KStandardAction::open(this, SLOT(openFile()),
241                         actionCollection());
242
243   m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
244                         actionCollection());
245
246   KStandardAction::save(this, SLOT(saveFile()),
247                         actionCollection());
248  
249   KStandardAction::saveAs(this, SLOT(saveFileAs()),
250                         actionCollection());
251  
252   KStandardAction::openNew(this, SLOT(newFile()),
253                         actionCollection());
254
255   KStandardAction::preferences(this, SLOT(slotPreferences()),
256                         actionCollection());
257
258   /*KStandardAction::undo(this, SLOT(undo()),
259                         actionCollection());
260
261   KStandardAction::redo(this, SLOT(redo()),
262                         actionCollection());*/
263
264   readOptions();  
265
266   /*m_redo = m_commandStack->createRedoAction(actionCollection());
267   m_undo = m_commandStack->createUndoAction(actionCollection());*/
268 }
269
270 void MainWindow::saveOptions()
271 {
272   KSharedConfigPtr config = KGlobal::config ();
273   m_fileOpenRecent->saveEntries(KConfigGroup (config, "Recent Files"));
274   config->sync(); 
275 }
276
277 void MainWindow::readOptions()
278 {
279   KSharedConfigPtr config = KGlobal::config ();
280   m_fileOpenRecent->loadEntries(KConfigGroup (config, "Recent Files"));
281 }
282  
283 void MainWindow::newFile()
284 {
285   KdenliveDoc *doc = new KdenliveDoc(KUrl(), 25, 720, 576);
286   TrackView *trackView = new TrackView(doc);
287   m_timelineArea->addTab(trackView, "New Project");
288   if (m_timelineArea->count() == 1)
289     connectDocument(trackView, doc);
290 }
291
292 void MainWindow::activateDocument()
293 {
294   TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
295   KdenliveDoc *currentDoc = currentTab->document();
296   connectDocument(currentTab, currentDoc);
297 }
298  
299 void MainWindow::saveFileAs(const QString &outputFileName)
300 {
301   KSaveFile file(outputFileName);
302   file.open();
303   
304   QByteArray outputByteArray;
305   //outputByteArray.append(textArea->toPlainText());
306   file.write(outputByteArray);
307   file.finalize();
308   file.close();
309   
310   fileName = outputFileName;
311 }
312
313 void MainWindow::saveFileAs()
314 {
315   saveFileAs(KFileDialog::getSaveFileName());
316 }
317  
318 void MainWindow::saveFile()
319 {
320   if(!fileName.isEmpty())
321   {
322     saveFileAs(fileName);
323   }
324   else
325   {
326     saveFileAs();
327   }
328 }
329  
330 void MainWindow::openFile() //changed
331 {
332     KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
333     if (url.isEmpty()) return;
334     m_fileOpenRecent->addUrl (url);
335     openFile(url);
336 }
337  
338 void MainWindow::openFile(const KUrl &url) //new
339 {
340   KdenliveDoc *doc = new KdenliveDoc(url, 25, 720, 576);
341   TrackView *trackView = new TrackView(doc);
342   m_timelineArea->setCurrentIndex(m_timelineArea->addTab(trackView, QIcon(), doc->documentName()));
343   m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), doc->url().path());
344   //connectDocument(trackView, doc);
345 }
346
347
348 void MainWindow::parseProfiles()
349 {
350         //kdDebug()<<" + + YOUR MLT INSTALL WAS FOUND IN: "<< MLT_PREFIX <<endl;
351         if (KdenliveSettings::mltpath().isEmpty()) {
352             KdenliveSettings::setMltpath(QString(MLT_PREFIX) + QString("/share/mlt/profiles/"));
353         }
354         if (KdenliveSettings::rendererpath().isEmpty())
355         {
356             KdenliveSettings::setRendererpath(KStandardDirs::findExe("inigo"));
357         }
358         QStringList profilesFilter;
359         profilesFilter<<"*";
360         QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
361
362         if (profilesList.isEmpty()) {
363             // Cannot find MLT path, try finding inigo
364             QString profilePath = KdenliveSettings::rendererpath();
365             if (!profilePath.isEmpty()) {
366                 profilePath = profilePath.section('/', 0, -3);
367                 KdenliveSettings::setMltpath(profilePath + "/share/mlt/profiles/");
368                 QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
369             }
370
371             if (profilesList.isEmpty()) {
372                 // Cannot find the MLT profiles, ask for location
373                 KUrlRequesterDialog *getUrl = new KUrlRequesterDialog(KdenliveSettings::mltpath(), i18n("Cannot find your Mlt profiles, please give the path"), this);
374                 getUrl->fileDialog()->setMode(KFile::Directory);
375                 getUrl->exec();
376                 KUrl mltPath = getUrl->selectedUrl ();
377                 delete getUrl;
378                 if (mltPath.isEmpty()) exit(1);
379                 KdenliveSettings::setMltpath(mltPath.path());
380                 QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
381             }
382         }
383
384         if (KdenliveSettings::rendererpath().isEmpty()) {
385                 // Cannot find the MLT inigo renderer, ask for location
386                 KUrlRequesterDialog *getUrl = new KUrlRequesterDialog(KdenliveSettings::mltpath(), i18n("Cannot find the inigo program required for rendering (part of Mlt)"), this);
387                 getUrl->exec();
388                 KUrl rendererPath = getUrl->selectedUrl();
389                 delete getUrl;
390                 if (rendererPath.isEmpty()) exit(1);
391                 KdenliveSettings::setRendererpath(rendererPath.path());
392         }
393
394         kDebug()<<"RESULTING MLT PATH: "<<KdenliveSettings::mltpath();
395
396         // Parse MLT profiles to build a list of available video formats
397         if (profilesList.isEmpty()) parseProfiles();
398     }
399
400
401 void MainWindow::slotEditProfiles()
402 {
403   ProfilesDialog *w = new ProfilesDialog;
404   w->exec();
405   delete w;
406 }
407
408 void MainWindow::slotEditProjectSettings()
409 {
410   ProjectSettings *w = new ProjectSettings;
411   w->exec();
412   delete w;
413 }
414
415
416 void MainWindow::slotUpdateMousePosition(int pos)
417 {
418   if (m_activeDocument)
419     switch(m_timecodeFormat->currentIndex()) {
420       case 0:
421         statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
422         break;
423     default:
424       statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
425     }
426 }
427
428 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) //changed
429 {
430   //m_projectMonitor->stop();
431   kDebug()<<"///////////////////   CONNECTING DOC TO PROJECT VIEW ////////////////";
432   if (m_activeDocument) {
433     if (m_activeDocument == doc) return;
434     m_activeDocument->setProducers(m_projectList->producersList());
435     m_activeDocument->setRenderer(NULL);
436   }
437   connect(trackView, SIGNAL(cursorMoved()), m_projectMonitor, SLOT(activateMonitor()));
438   connect(trackView, SIGNAL(mousePosition(int)), this, SLOT(slotUpdateMousePosition(int)));
439   connect(m_projectMonitor, SIGNAL(renderPosition(int)), trackView, SLOT(moveCursorPos(int)));
440   connect(doc, SIGNAL(addProjectClip(DocClipBase *)), m_projectList, SLOT(slotAddClip(DocClipBase *)));
441   connect(doc, SIGNAL(signalDeleteProjectClip(int)), m_projectList, SLOT(slotDeleteClip(int)));
442   connect(doc, SIGNAL(updateClipDisplay(int)), m_projectList, SLOT(slotUpdateClip(int)));
443   connect(doc, SIGNAL(deletTimelineClip(int)), trackView, SLOT(slotDeleteClip(int)));
444   connect(trackView, SIGNAL(clipItemSelected(ClipItem*)), effectStack, SLOT(slotClipItemSelected(ClipItem*)));
445   m_projectList->setDocument(doc);
446   m_monitorManager->setTimecode(doc->timecode());
447   doc->setRenderer(m_projectMonitor->render);
448   //m_undoView->setStack(0);
449   m_commandStack = doc->commandStack();
450
451   m_overView->setScene(trackView->projectScene());
452   m_overView->scale(m_overView->width() / trackView->duration(), m_overView->height() / (50 * trackView->tracksNumber()));
453   //m_overView->fitInView(m_overView->itemAt(0, 50), Qt::KeepAspectRatio);
454   QAction *redo = m_commandStack->createRedoAction(actionCollection());
455   QAction *undo = m_commandStack->createUndoAction(actionCollection());
456
457   QWidget* w = factory()->container("mainToolBar", this);
458   if(w) {
459     if (actionCollection()->action("undo"))
460       delete actionCollection()->action("undo");
461     if(actionCollection()->action("redo"))
462       delete actionCollection()->action("redo");
463
464     actionCollection()->addAction("undo", undo);
465     actionCollection()->addAction("redo", redo);
466     w->addAction(undo);
467     w->addAction(redo);
468   }
469   m_undoView->setStack(doc->commandStack());
470   m_activeDocument = doc;
471 }
472
473 void MainWindow::slotPreferences()
474 {
475   //An instance of your dialog could be already created and could be
476   // cached, in which case you want to display the cached dialog
477   // instead of creating another one
478   if ( KConfigDialog::showDialog( "settings" ) )
479     return;
480
481   // KConfigDialog didn't find an instance of this dialog, so lets
482   // create it :
483   KdenliveSettingsDialog* dialog = new KdenliveSettingsDialog(this);
484   //connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
485   dialog->show();
486 }
487
488 #include "mainwindow.moc"