]> git.sesse.net Git - kdenlive/blob - src/mainwindow.cpp
update timeline duration
[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   connect(actionCollection(), SIGNAL( actionHighlighted( QAction* ) ), 
265     this, SLOT( slotDisplayActionMessage( QAction* ) ) );
266   //connect(actionCollection(), SIGNAL( clearStatusText() ),
267     //statusBar(), SLOT( clear() ) );
268
269   readOptions();  
270
271   /*m_redo = m_commandStack->createRedoAction(actionCollection());
272   m_undo = m_commandStack->createUndoAction(actionCollection());*/
273 }
274
275 void MainWindow::slotDisplayActionMessage( QAction *a)
276 {
277   statusBar()->showMessage(a->data().toString(), 3000);
278 }
279
280 void MainWindow::saveOptions()
281 {
282   KSharedConfigPtr config = KGlobal::config ();
283   m_fileOpenRecent->saveEntries(KConfigGroup (config, "Recent Files"));
284   config->sync(); 
285 }
286
287 void MainWindow::readOptions()
288 {
289   KSharedConfigPtr config = KGlobal::config ();
290   m_fileOpenRecent->loadEntries(KConfigGroup (config, "Recent Files"));
291 }
292  
293 void MainWindow::newFile()
294 {
295   KdenliveDoc *doc = new KdenliveDoc(KUrl(), 25, 720, 576);
296   TrackView *trackView = new TrackView(doc);
297   m_timelineArea->addTab(trackView, "New Project");
298   if (m_timelineArea->count() == 1)
299     connectDocument(trackView, doc);
300 }
301
302 void MainWindow::activateDocument()
303 {
304   TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
305   KdenliveDoc *currentDoc = currentTab->document();
306   connectDocument(currentTab, currentDoc);
307 }
308  
309 void MainWindow::saveFileAs(const QString &outputFileName)
310 {
311   KSaveFile file(outputFileName);
312   file.open();
313   
314   QByteArray outputByteArray;
315   //outputByteArray.append(textArea->toPlainText());
316   file.write(outputByteArray);
317   file.finalize();
318   file.close();
319   
320   fileName = outputFileName;
321 }
322
323 void MainWindow::saveFileAs()
324 {
325   saveFileAs(KFileDialog::getSaveFileName());
326 }
327  
328 void MainWindow::saveFile()
329 {
330   if(!fileName.isEmpty())
331   {
332     saveFileAs(fileName);
333   }
334   else
335   {
336     saveFileAs();
337   }
338 }
339  
340 void MainWindow::openFile() //changed
341 {
342     KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
343     if (url.isEmpty()) return;
344     m_fileOpenRecent->addUrl (url);
345     openFile(url);
346 }
347  
348 void MainWindow::openFile(const KUrl &url) //new
349 {
350   KdenliveDoc *doc = new KdenliveDoc(url, 25, 720, 576);
351   TrackView *trackView = new TrackView(doc);
352   m_timelineArea->setCurrentIndex(m_timelineArea->addTab(trackView, QIcon(), doc->documentName()));
353   m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), doc->url().path());
354   //connectDocument(trackView, doc);
355 }
356
357
358 void MainWindow::parseProfiles()
359 {
360         //kdDebug()<<" + + YOUR MLT INSTALL WAS FOUND IN: "<< MLT_PREFIX <<endl;
361         if (KdenliveSettings::mltpath().isEmpty()) {
362             KdenliveSettings::setMltpath(QString(MLT_PREFIX) + QString("/share/mlt/profiles/"));
363         }
364         if (KdenliveSettings::rendererpath().isEmpty())
365         {
366             KdenliveSettings::setRendererpath(KStandardDirs::findExe("inigo"));
367         }
368         QStringList profilesFilter;
369         profilesFilter<<"*";
370         QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
371
372         if (profilesList.isEmpty()) {
373             // Cannot find MLT path, try finding inigo
374             QString profilePath = KdenliveSettings::rendererpath();
375             if (!profilePath.isEmpty()) {
376                 profilePath = profilePath.section('/', 0, -3);
377                 KdenliveSettings::setMltpath(profilePath + "/share/mlt/profiles/");
378                 QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
379             }
380
381             if (profilesList.isEmpty()) {
382                 // Cannot find the MLT profiles, ask for location
383                 KUrlRequesterDialog *getUrl = new KUrlRequesterDialog(KdenliveSettings::mltpath(), i18n("Cannot find your Mlt profiles, please give the path"), this);
384                 getUrl->fileDialog()->setMode(KFile::Directory);
385                 getUrl->exec();
386                 KUrl mltPath = getUrl->selectedUrl ();
387                 delete getUrl;
388                 if (mltPath.isEmpty()) exit(1);
389                 KdenliveSettings::setMltpath(mltPath.path());
390                 QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
391             }
392         }
393
394         if (KdenliveSettings::rendererpath().isEmpty()) {
395                 // Cannot find the MLT inigo renderer, ask for location
396                 KUrlRequesterDialog *getUrl = new KUrlRequesterDialog(KdenliveSettings::mltpath(), i18n("Cannot find the inigo program required for rendering (part of Mlt)"), this);
397                 getUrl->exec();
398                 KUrl rendererPath = getUrl->selectedUrl();
399                 delete getUrl;
400                 if (rendererPath.isEmpty()) exit(1);
401                 KdenliveSettings::setRendererpath(rendererPath.path());
402         }
403
404         kDebug()<<"RESULTING MLT PATH: "<<KdenliveSettings::mltpath();
405
406         // Parse MLT profiles to build a list of available video formats
407         if (profilesList.isEmpty()) parseProfiles();
408     }
409
410
411 void MainWindow::slotEditProfiles()
412 {
413   ProfilesDialog *w = new ProfilesDialog;
414   w->exec();
415   delete w;
416 }
417
418 void MainWindow::slotEditProjectSettings()
419 {
420   ProjectSettings *w = new ProjectSettings;
421   w->exec();
422   delete w;
423 }
424
425
426 void MainWindow::slotUpdateMousePosition(int pos)
427 {
428   if (m_activeDocument)
429     switch(m_timecodeFormat->currentIndex()) {
430       case 0:
431         statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
432         break;
433     default:
434       statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
435     }
436 }
437
438 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) //changed
439 {
440   //m_projectMonitor->stop();
441   kDebug()<<"///////////////////   CONNECTING DOC TO PROJECT VIEW ////////////////";
442   if (m_activeDocument) {
443     if (m_activeDocument == doc) return;
444     m_activeDocument->setProducers(m_projectList->producersList());
445     m_activeDocument->setRenderer(NULL);
446   }
447   connect(trackView, SIGNAL(cursorMoved()), m_projectMonitor, SLOT(activateMonitor()));
448   connect(trackView, SIGNAL(mousePosition(int)), this, SLOT(slotUpdateMousePosition(int)));
449   connect(m_projectMonitor, SIGNAL(renderPosition(int)), trackView, SLOT(moveCursorPos(int)));
450   connect(m_projectMonitor, SIGNAL(durationChanged(int)), trackView->projectView(), SLOT(setDuration(int)));
451   connect(doc, SIGNAL(addProjectClip(DocClipBase *)), m_projectList, SLOT(slotAddClip(DocClipBase *)));
452   connect(doc, SIGNAL(signalDeleteProjectClip(int)), m_projectList, SLOT(slotDeleteClip(int)));
453   connect(doc, SIGNAL(updateClipDisplay(int)), m_projectList, SLOT(slotUpdateClip(int)));
454   connect(doc, SIGNAL(deletTimelineClip(int)), trackView, SLOT(slotDeleteClip(int)));
455   connect(trackView, SIGNAL(clipItemSelected(ClipItem*)), effectStack, SLOT(slotClipItemSelected(ClipItem*)));
456   connect(effectStack, SIGNAL(updateClipEffect(ClipItem*, QDomElement)), trackView->projectView(), SLOT(slotUpdateClipEffect(ClipItem*, QDomElement)));
457   connect(effectStack, SIGNAL(removeEffect(ClipItem*, QDomElement)), trackView->projectView(), SLOT(slotDeleteEffect(ClipItem*, QDomElement)));
458   connect(effectStack, SIGNAL(refreshEffectStack(ClipItem*)), trackView->projectView(), SLOT(slotRefreshEffects(ClipItem*)));
459
460   m_projectList->setDocument(doc);
461   m_monitorManager->setTimecode(doc->timecode());
462   doc->setRenderer(m_projectMonitor->render);
463   //m_undoView->setStack(0);
464   m_commandStack = doc->commandStack();
465
466   m_overView->setScene(trackView->projectScene());
467   m_overView->scale(m_overView->width() / trackView->duration(), m_overView->height() / (50 * trackView->tracksNumber()));
468   //m_overView->fitInView(m_overView->itemAt(0, 50), Qt::KeepAspectRatio);
469   QAction *redo = m_commandStack->createRedoAction(actionCollection());
470   QAction *undo = m_commandStack->createUndoAction(actionCollection());
471
472   QWidget* w = factory()->container("mainToolBar", this);
473   if(w) {
474     if (actionCollection()->action("undo"))
475       delete actionCollection()->action("undo");
476     if(actionCollection()->action("redo"))
477       delete actionCollection()->action("redo");
478
479     actionCollection()->addAction("undo", undo);
480     actionCollection()->addAction("redo", redo);
481     w->addAction(undo);
482     w->addAction(redo);
483   }
484   m_undoView->setStack(doc->commandStack());
485   m_activeDocument = doc;
486 }
487
488 void MainWindow::slotPreferences()
489 {
490   //An instance of your dialog could be already created and could be
491   // cached, in which case you want to display the cached dialog
492   // instead of creating another one
493   if ( KConfigDialog::showDialog( "settings" ) )
494     return;
495
496   // KConfigDialog didn't find an instance of this dialog, so lets
497   // create it :
498   KdenliveSettingsDialog* dialog = new KdenliveSettingsDialog(this);
499   //connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
500   dialog->show();
501 }
502
503 #include "mainwindow.moc"