]> git.sesse.net Git - kdenlive/blob - src/mainwindow.cpp
Only create audio thumbs when they are enabled, clear cache when disabled
[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     parseProfiles();
65     m_timelineArea = new KTabWidget(this);
66     m_timelineArea->setHoverCloseButton(true);
67     m_timelineArea->setTabReorderingEnabled(true);
68     m_timelineArea->setTabBarHidden(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     statusProgressBar->setMaximumWidth(150);
145     statusProgressBar->setVisible(false);
146     statusLabel = new QLabel(this);
147
148     statusBar()->insertPermanentWidget(0, statusProgressBar, 1);
149     statusBar()->insertPermanentWidget(1, statusLabel, 1);
150     statusBar()->insertPermanentFixedItem("00:00:00:00", ID_TIMELINE_POS);
151     statusBar()->insertPermanentWidget(ID_TIMELINE_FORMAT, m_timecodeFormat);
152
153     setupGUI(Default, "kdenliveui.rc");
154
155     connect(projectMonitorDock, SIGNAL(visibilityChanged(bool)), m_projectMonitor, SLOT(refreshMonitor(bool)));
156     connect(clipMonitorDock, SIGNAL(visibilityChanged(bool)), m_clipMonitor, SLOT(refreshMonitor(bool)));
157     connect(m_monitorManager, SIGNAL(connectMonitors()), this, SLOT(slotConnectMonitors()));
158     connect(m_monitorManager, SIGNAL(raiseClipMonitor(bool)), this, SLOT(slotRaiseMonitor(bool)));
159     connect(m_effectList, SIGNAL(addEffect(QDomElement)), this, SLOT(slotAddEffect(QDomElement)));
160     m_monitorManager->initMonitors(m_clipMonitor, m_projectMonitor);
161
162     setAutoSaveSettings();
163     newFile();
164 }
165
166 //virtual
167 bool MainWindow::queryClose() {
168     saveOptions();
169     switch (KMessageBox::warningYesNoCancel(this, i18n("Save changes to document ?"))) {
170     case KMessageBox::Yes :
171         // save document here. If saving fails, return false;
172         return true;
173     case KMessageBox::No :
174         return true;
175     default: // cancel
176         return false;
177     }
178 }
179
180 void MainWindow::slotAddEffect(QDomElement effect, GenTime pos, int track) {
181     if (!m_activeDocument) return;
182     if (effect.isNull()) {
183         kDebug() << "--- ERROR, TRYING TO APPEND NULL EFFECT";
184         return;
185     }
186     TrackView *currentTimeLine = (TrackView *) m_timelineArea->currentWidget();
187     currentTimeLine->projectView()->slotAddEffect(effect, pos, track);
188 }
189
190 void MainWindow::slotRaiseMonitor(bool clipMonitor) {
191     if (clipMonitor) clipMonitorDock->raise();
192     else projectMonitorDock->raise();
193 }
194
195 void MainWindow::slotSetClipDuration(int id, int duration) {
196     if (!m_activeDocument) return;
197     m_activeDocument->setProducerDuration(id, duration);
198 }
199
200 void MainWindow::slotConnectMonitors() {
201
202     m_projectList->setRenderer(m_clipMonitor->render);
203
204     connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
205
206     connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
207
208     connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
209
210     connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
211
212     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 > &)));
213
214 }
215
216 void MainWindow::setupActions() {
217     KAction* clearAction = new KAction(this);
218     clearAction->setText(i18n("Clear"));
219     clearAction->setIcon(KIcon("document-new"));
220     clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
221     actionCollection()->addAction("clear", clearAction);
222     /*connect(clearAction, SIGNAL(triggered(bool)),
223             textArea, SLOT(clear()));*/
224
225     KAction* profilesAction = new KAction(this);
226     profilesAction->setText(i18n("Manage Profiles"));
227     profilesAction->setIcon(KIcon("document-new"));
228     actionCollection()->addAction("manage_profiles", profilesAction);
229     connect(profilesAction, SIGNAL(triggered(bool)), this, SLOT(slotEditProfiles()));
230
231     KAction* projectAction = new KAction(this);
232     projectAction->setText(i18n("Project Settings"));
233     projectAction->setIcon(KIcon("document-new"));
234     actionCollection()->addAction("project_settings", projectAction);
235     connect(projectAction, SIGNAL(triggered(bool)), this, SLOT(slotEditProjectSettings()));
236
237     KAction* monitorPlay = new KAction(this);
238     monitorPlay->setText(i18n("Play"));
239     monitorPlay->setIcon(KIcon("media-playback-start"));
240     monitorPlay->setShortcut(Qt::Key_Space);
241     actionCollection()->addAction("monitor_play", monitorPlay);
242     connect(monitorPlay, SIGNAL(triggered(bool)), m_monitorManager, SLOT(slotPlay()));
243
244     KStandardAction::quit(kapp, SLOT(quit()),
245                           actionCollection());
246
247     KStandardAction::open(this, SLOT(openFile()),
248                           actionCollection());
249
250     m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
251                        actionCollection());
252
253     KStandardAction::save(this, SLOT(saveFile()),
254                           actionCollection());
255
256     KStandardAction::saveAs(this, SLOT(saveFileAs()),
257                             actionCollection());
258
259     KStandardAction::openNew(this, SLOT(newFile()),
260                              actionCollection());
261
262     KStandardAction::preferences(this, SLOT(slotPreferences()),
263                                  actionCollection());
264
265     /*KStandardAction::undo(this, SLOT(undo()),
266                           actionCollection());
267
268     KStandardAction::redo(this, SLOT(redo()),
269                           actionCollection());*/
270
271     connect(actionCollection(), SIGNAL(actionHighlighted(QAction*)),
272             this, SLOT(slotDisplayActionMessage(QAction*)));
273     //connect(actionCollection(), SIGNAL( clearStatusText() ),
274     //statusBar(), SLOT( clear() ) );
275
276     readOptions();
277
278     /*m_redo = m_commandStack->createRedoAction(actionCollection());
279     m_undo = m_commandStack->createUndoAction(actionCollection());*/
280 }
281
282 void MainWindow::slotDisplayActionMessage(QAction *a) {
283     statusBar()->showMessage(a->data().toString(), 3000);
284 }
285
286 void MainWindow::saveOptions() {
287     KSharedConfigPtr config = KGlobal::config();
288     m_fileOpenRecent->saveEntries(KConfigGroup(config, "Recent Files"));
289     config->sync();
290 }
291
292 void MainWindow::readOptions() {
293     KSharedConfigPtr config = KGlobal::config();
294     m_fileOpenRecent->loadEntries(KConfigGroup(config, "Recent Files"));
295 }
296
297 void MainWindow::newFile() {
298     KdenliveDoc *doc = new KdenliveDoc(KUrl(), 25, 720, 576);
299     TrackView *trackView = new TrackView(doc);
300     m_timelineArea->addTab(trackView, i18n("Untitled") + " / " + ProfilesDialog::getProfileDescription(KdenliveSettings::default_profile()));
301     if (m_timelineArea->count() == 1)
302         connectDocument(trackView, doc);
303     else m_timelineArea->setTabBarHidden(false);
304 }
305
306 void MainWindow::activateDocument() {
307     TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
308     KdenliveDoc *currentDoc = currentTab->document();
309     connectDocument(currentTab, currentDoc);
310 }
311
312 void MainWindow::saveFileAs(const QString &outputFileName) {
313     KSaveFile file(outputFileName);
314     file.open();
315
316     QByteArray outputByteArray;
317     //outputByteArray.append(textArea->toPlainText());
318     file.write(outputByteArray);
319     file.finalize();
320     file.close();
321
322     fileName = outputFileName;
323 }
324
325 void MainWindow::saveFileAs() {
326     saveFileAs(KFileDialog::getSaveFileName());
327 }
328
329 void MainWindow::saveFile() {
330     if (!fileName.isEmpty()) {
331         saveFileAs(fileName);
332     } else {
333         saveFileAs();
334     }
335 }
336
337 void MainWindow::openFile() { //changed
338     KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
339     if (url.isEmpty()) return;
340     m_fileOpenRecent->addUrl(url);
341     openFile(url);
342 }
343
344 void MainWindow::openFile(const KUrl &url) { //new
345     KdenliveDoc *doc = new KdenliveDoc(url, 25, 720, 576);
346     TrackView *trackView = new TrackView(doc);
347     m_timelineArea->setCurrentIndex(m_timelineArea->addTab(trackView, QIcon(), doc->documentName()));
348     m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), doc->url().path());
349     if (m_timelineArea->count() > 1) m_timelineArea->setTabBarHidden(false);
350     //connectDocument(trackView, doc);
351 }
352
353
354 void MainWindow::parseProfiles() {
355     //kdDebug()<<" + + YOUR MLT INSTALL WAS FOUND IN: "<< MLT_PREFIX <<endl;
356     if (KdenliveSettings::mltpath().isEmpty()) {
357         KdenliveSettings::setMltpath(QString(MLT_PREFIX) + QString("/share/mlt/profiles/"));
358     }
359     if (KdenliveSettings::rendererpath().isEmpty()) {
360         KdenliveSettings::setRendererpath(KStandardDirs::findExe("inigo"));
361     }
362     QStringList profilesFilter;
363     profilesFilter << "*";
364     QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
365
366     if (profilesList.isEmpty()) {
367         // Cannot find MLT path, try finding inigo
368         QString profilePath = KdenliveSettings::rendererpath();
369         if (!profilePath.isEmpty()) {
370             profilePath = profilePath.section('/', 0, -3);
371             KdenliveSettings::setMltpath(profilePath + "/share/mlt/profiles/");
372             QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
373         }
374
375         if (profilesList.isEmpty()) {
376             // Cannot find the MLT profiles, ask for location
377             KUrlRequesterDialog *getUrl = new KUrlRequesterDialog(KdenliveSettings::mltpath(), i18n("Cannot find your Mlt profiles, please give the path"), this);
378             getUrl->fileDialog()->setMode(KFile::Directory);
379             getUrl->exec();
380             KUrl mltPath = getUrl->selectedUrl();
381             delete getUrl;
382             if (mltPath.isEmpty()) exit(1);
383             KdenliveSettings::setMltpath(mltPath.path());
384             QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
385         }
386     }
387
388     if (KdenliveSettings::rendererpath().isEmpty()) {
389         // Cannot find the MLT inigo renderer, ask for location
390         KUrlRequesterDialog *getUrl = new KUrlRequesterDialog(KdenliveSettings::mltpath(), i18n("Cannot find the inigo program required for rendering (part of Mlt)"), this);
391         getUrl->exec();
392         KUrl rendererPath = getUrl->selectedUrl();
393         delete getUrl;
394         if (rendererPath.isEmpty()) exit(1);
395         KdenliveSettings::setRendererpath(rendererPath.path());
396     }
397
398     kDebug() << "RESULTING MLT PATH: " << KdenliveSettings::mltpath();
399
400     // Parse MLT profiles to build a list of available video formats
401     if (profilesList.isEmpty()) parseProfiles();
402 }
403
404
405 void MainWindow::slotEditProfiles() {
406     ProfilesDialog *w = new ProfilesDialog;
407     w->exec();
408     delete w;
409 }
410
411 void MainWindow::slotEditProjectSettings() {
412     ProjectSettings *w = new ProjectSettings;
413     w->exec();
414     delete w;
415 }
416
417
418 void MainWindow::slotUpdateMousePosition(int pos) {
419     if (m_activeDocument)
420         switch (m_timecodeFormat->currentIndex()) {
421         case 0:
422             statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
423             break;
424         default:
425             statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
426         }
427 }
428
429 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) { //changed
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(m_projectMonitor, SIGNAL(durationChanged(int)), trackView->projectView(), SLOT(setDuration(int)));
441     connect(doc, SIGNAL(addProjectClip(DocClipBase *)), m_projectList, SLOT(slotAddClip(DocClipBase *)));
442     connect(doc, SIGNAL(signalDeleteProjectClip(int)), m_projectList, SLOT(slotDeleteClip(int)));
443     connect(doc, SIGNAL(updateClipDisplay(int)), m_projectList, SLOT(slotUpdateClip(int)));
444     connect(doc, SIGNAL(deletTimelineClip(int)), trackView, SLOT(slotDeleteClip(int)));
445     connect(doc, SIGNAL(thumbsProgress(KUrl, int)), this, SLOT(slotGotProgressInfo(KUrl, int)));
446
447     connect(trackView, SIGNAL(clipItemSelected(ClipItem*)), effectStack, SLOT(slotClipItemSelected(ClipItem*)));
448     connect(effectStack, SIGNAL(updateClipEffect(ClipItem*, QDomElement, QDomElement)), trackView->projectView(), SLOT(slotUpdateClipEffect(ClipItem*, QDomElement, QDomElement)));
449     connect(effectStack, SIGNAL(removeEffect(ClipItem*, QDomElement)), trackView->projectView(), SLOT(slotDeleteEffect(ClipItem*, QDomElement)));
450     connect(effectStack, SIGNAL(changeEffectState(ClipItem*, QDomElement, bool)), trackView->projectView(), SLOT(slotChangeEffectState(ClipItem*, QDomElement, bool)));
451     connect(effectStack, SIGNAL(refreshEffectStack(ClipItem*)), trackView->projectView(), SLOT(slotRefreshEffects(ClipItem*)));
452
453     m_projectList->setDocument(doc);
454     m_monitorManager->setTimecode(doc->timecode());
455     doc->setRenderer(m_projectMonitor->render);
456     //m_undoView->setStack(0);
457     m_commandStack = doc->commandStack();
458
459     m_overView->setScene(trackView->projectScene());
460     m_overView->scale(m_overView->width() / trackView->duration(), m_overView->height() / (50 * trackView->tracksNumber()));
461     //m_overView->fitInView(m_overView->itemAt(0, 50), Qt::KeepAspectRatio);
462     QAction *redo = m_commandStack->createRedoAction(actionCollection());
463     QAction *undo = m_commandStack->createUndoAction(actionCollection());
464
465     QWidget* w = factory()->container("mainToolBar", this);
466     if (w) {
467         if (actionCollection()->action("undo"))
468             delete actionCollection()->action("undo");
469         if (actionCollection()->action("redo"))
470             delete actionCollection()->action("redo");
471
472         actionCollection()->addAction("undo", undo);
473         actionCollection()->addAction("redo", redo);
474         w->addAction(undo);
475         w->addAction(redo);
476     }
477     m_undoView->setStack(doc->commandStack());
478     m_activeDocument = doc;
479 }
480
481 void MainWindow::slotPreferences() {
482     //An instance of your dialog could be already created and could be
483     // cached, in which case you want to display the cached dialog
484     // instead of creating another one
485     if (KConfigDialog::showDialog("settings"))
486         return;
487
488     // KConfigDialog didn't find an instance of this dialog, so lets
489     // create it :
490     KdenliveSettingsDialog* dialog = new KdenliveSettingsDialog(this);
491     connect(dialog, SIGNAL(settingsChanged(const QString&)), this, SLOT(updateConfiguration()));
492     dialog->show();
493 }
494
495 void MainWindow::updateConfiguration() {
496     //TODO: we should apply settings to all projects, not only the current one
497     TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
498     if (currentTab) {
499         currentTab->refresh();
500         currentTab->projectView()->checkAutoScroll();
501         if (m_activeDocument) m_activeDocument->clipManager()->checkAudioThumbs();
502     }
503 }
504
505 void MainWindow::slotGotProgressInfo(KUrl url, int progress) {
506     statusProgressBar->setValue(progress);
507     if (progress > 0) {
508         statusLabel->setText(tr("Creating Audio Thumbs"));
509         statusProgressBar->setVisible(true);
510     } else {
511         statusLabel->setText("");
512         statusProgressBar->setVisible(false);
513     }
514 }
515
516 #include "mainwindow.moc"