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