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