1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
22 #include <QTextStream>
28 #include <KApplication>
31 #include <KActionCollection>
32 #include <KStandardAction>
33 #include <KFileDialog>
34 #include <KMessageBox>
36 #include <KIO/NetAccess>
39 #include <KConfigDialog>
40 #include <KXMLGUIFactory>
42 #include <kstandarddirs.h>
43 #include <KUrlRequesterDialog>
45 #include <mlt++/Mlt.h>
47 #include "mainwindow.h"
48 #include "kdenlivesettings.h"
49 #include "kdenlivesettingsdialog.h"
50 #include "initeffects.h"
51 #include "profilesdialog.h"
52 #include "projectsettings.h"
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
60 MainWindow::MainWindow(QWidget *parent)
61 : KXmlGuiWindow(parent),
62 fileName(QString()), m_activeDocument(NULL), m_commandStack(NULL)
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()));
70 initEffects::parseEffectFiles( &m_audioEffects, &m_videoEffects );
71 m_monitorManager = new MonitorManager();
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);
79 effectListDock = new QDockWidget(i18n("Effect List"), this);
80 effectListDock->setObjectName("effect_list");
81 m_effectList = new EffectsListView(&m_audioEffects, &m_videoEffects, &m_customEffects);
83 //m_effectList = new KListWidget(this);
84 effectListDock->setWidget(m_effectList);
85 addDockWidget(Qt::TopDockWidgetArea, effectListDock);
87 effectStackDock = new QDockWidget(i18n("Effect Stack"), this);
88 effectStackDock->setObjectName("effect_stack");
89 effectStack = new EffectStackView(this);
90 effectStackDock->setWidget(effectStack);
91 addDockWidget(Qt::TopDockWidgetArea, effectStackDock);
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);
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();
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);
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);
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);
127 tabifyDockWidget (projectListDock, effectListDock);
128 tabifyDockWidget (projectListDock, effectStackDock);
129 tabifyDockWidget (projectListDock, transitionConfigDock);
130 tabifyDockWidget (projectListDock, undoViewDock);
131 projectListDock->raise();
133 tabifyDockWidget (clipMonitorDock, projectMonitorDock);
134 setCentralWidget(m_timelineArea);
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);
142 setupGUI(Default, "kdenliveui.rc");
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(int, const QString&)), this, SLOT(slotAddEffect(int, const QString &)));
149 m_monitorManager->initMonitors(m_clipMonitor, m_projectMonitor);
151 setAutoSaveSettings();
156 bool MainWindow::queryClose()
159 switch ( KMessageBox::warningYesNoCancel( this, i18n("Save changes to document ?")) ) {
160 case KMessageBox::Yes :
161 // save document here. If saving fails, return false;
163 case KMessageBox::No :
170 void MainWindow::slotAddEffect(int effectType, const QString &effectName)
172 if (!m_activeDocument) return;
173 QMap <QString, QString> filter;
175 filter = m_videoEffects.effect(effectName);
176 else if (effectType == 1)
177 filter = m_audioEffects.effect(effectName);
179 filter = m_customEffects.effect(effectName);
180 TrackView *currentTimeLine = (TrackView *) m_timelineArea->currentWidget();
181 currentTimeLine->projectView()->slotAddEffect(filter);
184 void MainWindow::slotRaiseMonitor(bool clipMonitor)
186 if (clipMonitor) clipMonitorDock->raise();
187 else projectMonitorDock->raise();
190 void MainWindow::slotSetClipDuration(int id, int duration)
192 if (!m_activeDocument) return;
193 m_activeDocument->setProducerDuration(id, duration);
196 void MainWindow::slotConnectMonitors()
199 m_projectList->setRenderer(m_clipMonitor->render);
201 connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
203 connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
205 connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
207 connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
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 > &)));
213 void MainWindow::setupActions()
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()));*/
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()));
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()));
237 KStandardAction::quit(kapp, SLOT(quit()),
240 KStandardAction::open(this, SLOT(openFile()),
243 m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
246 KStandardAction::save(this, SLOT(saveFile()),
249 KStandardAction::saveAs(this, SLOT(saveFileAs()),
252 KStandardAction::openNew(this, SLOT(newFile()),
255 KStandardAction::preferences(this, SLOT(slotPreferences()),
258 /*KStandardAction::undo(this, SLOT(undo()),
261 KStandardAction::redo(this, SLOT(redo()),
262 actionCollection());*/
266 /*m_redo = m_commandStack->createRedoAction(actionCollection());
267 m_undo = m_commandStack->createUndoAction(actionCollection());*/
270 void MainWindow::saveOptions()
272 KSharedConfigPtr config = KGlobal::config ();
273 m_fileOpenRecent->saveEntries(KConfigGroup (config, "Recent Files"));
277 void MainWindow::readOptions()
279 KSharedConfigPtr config = KGlobal::config ();
280 m_fileOpenRecent->loadEntries(KConfigGroup (config, "Recent Files"));
283 void MainWindow::newFile()
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);
292 void MainWindow::activateDocument()
294 TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
295 KdenliveDoc *currentDoc = currentTab->document();
296 connectDocument(currentTab, currentDoc);
299 void MainWindow::saveFileAs(const QString &outputFileName)
301 KSaveFile file(outputFileName);
304 QByteArray outputByteArray;
305 //outputByteArray.append(textArea->toPlainText());
306 file.write(outputByteArray);
310 fileName = outputFileName;
313 void MainWindow::saveFileAs()
315 saveFileAs(KFileDialog::getSaveFileName());
318 void MainWindow::saveFile()
320 if(!fileName.isEmpty())
322 saveFileAs(fileName);
330 void MainWindow::openFile() //changed
332 KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
333 if (url.isEmpty()) return;
334 m_fileOpenRecent->addUrl (url);
338 void MainWindow::openFile(const KUrl &url) //new
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);
348 void MainWindow::parseProfiles()
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/"));
354 if (KdenliveSettings::rendererpath().isEmpty())
356 KdenliveSettings::setRendererpath(KStandardDirs::findExe("inigo"));
358 QStringList profilesFilter;
360 QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
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);
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);
376 KUrl mltPath = getUrl->selectedUrl ();
378 if (mltPath.isEmpty()) exit(1);
379 KdenliveSettings::setMltpath(mltPath.path());
380 QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
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);
388 KUrl rendererPath = getUrl->selectedUrl();
390 if (rendererPath.isEmpty()) exit(1);
391 KdenliveSettings::setRendererpath(rendererPath.path());
394 kDebug()<<"RESULTING MLT PATH: "<<KdenliveSettings::mltpath();
396 // Parse MLT profiles to build a list of available video formats
397 if (profilesList.isEmpty()) parseProfiles();
401 void MainWindow::slotEditProfiles()
403 ProfilesDialog *w = new ProfilesDialog;
408 void MainWindow::slotEditProjectSettings()
410 ProjectSettings *w = new ProjectSettings;
416 void MainWindow::slotUpdateMousePosition(int pos)
418 if (m_activeDocument)
419 switch(m_timecodeFormat->currentIndex()) {
421 statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
424 statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
428 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);
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();
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());
457 QWidget* w = factory()->container("mainToolBar", this);
459 if (actionCollection()->action("undo"))
460 delete actionCollection()->action("undo");
461 if(actionCollection()->action("redo"))
462 delete actionCollection()->action("redo");
464 actionCollection()->addAction("undo", undo);
465 actionCollection()->addAction("redo", redo);
469 m_undoView->setStack(doc->commandStack());
470 m_activeDocument = doc;
473 void MainWindow::slotPreferences()
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" ) )
481 // KConfigDialog didn't find an instance of this dialog, so lets
483 KdenliveSettingsDialog* dialog = new KdenliveSettingsDialog(this);
484 //connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
488 #include "mainwindow.moc"