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(&m_audioEffects, &m_videoEffects, &m_customEffects,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(QDomElement)), this, SLOT(slotAddEffect(QDomElement)));
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(QDomElement effect)
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(effect);
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());*/
264 connect(actionCollection(), SIGNAL( actionHighlighted( QAction* ) ),
265 this, SLOT( slotDisplayActionMessage( QAction* ) ) );
266 //connect(actionCollection(), SIGNAL( clearStatusText() ),
267 //statusBar(), SLOT( clear() ) );
271 /*m_redo = m_commandStack->createRedoAction(actionCollection());
272 m_undo = m_commandStack->createUndoAction(actionCollection());*/
275 void MainWindow::slotDisplayActionMessage( QAction *a)
277 statusBar()->showMessage(a->data().toString(), 3000);
280 void MainWindow::saveOptions()
282 KSharedConfigPtr config = KGlobal::config ();
283 m_fileOpenRecent->saveEntries(KConfigGroup (config, "Recent Files"));
287 void MainWindow::readOptions()
289 KSharedConfigPtr config = KGlobal::config ();
290 m_fileOpenRecent->loadEntries(KConfigGroup (config, "Recent Files"));
293 void MainWindow::newFile()
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);
302 void MainWindow::activateDocument()
304 TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
305 KdenliveDoc *currentDoc = currentTab->document();
306 connectDocument(currentTab, currentDoc);
309 void MainWindow::saveFileAs(const QString &outputFileName)
311 KSaveFile file(outputFileName);
314 QByteArray outputByteArray;
315 //outputByteArray.append(textArea->toPlainText());
316 file.write(outputByteArray);
320 fileName = outputFileName;
323 void MainWindow::saveFileAs()
325 saveFileAs(KFileDialog::getSaveFileName());
328 void MainWindow::saveFile()
330 if(!fileName.isEmpty())
332 saveFileAs(fileName);
340 void MainWindow::openFile() //changed
342 KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
343 if (url.isEmpty()) return;
344 m_fileOpenRecent->addUrl (url);
348 void MainWindow::openFile(const KUrl &url) //new
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);
358 void MainWindow::parseProfiles()
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/"));
364 if (KdenliveSettings::rendererpath().isEmpty())
366 KdenliveSettings::setRendererpath(KStandardDirs::findExe("inigo"));
368 QStringList profilesFilter;
370 QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
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);
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);
386 KUrl mltPath = getUrl->selectedUrl ();
388 if (mltPath.isEmpty()) exit(1);
389 KdenliveSettings::setMltpath(mltPath.path());
390 QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
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);
398 KUrl rendererPath = getUrl->selectedUrl();
400 if (rendererPath.isEmpty()) exit(1);
401 KdenliveSettings::setRendererpath(rendererPath.path());
404 kDebug()<<"RESULTING MLT PATH: "<<KdenliveSettings::mltpath();
406 // Parse MLT profiles to build a list of available video formats
407 if (profilesList.isEmpty()) parseProfiles();
411 void MainWindow::slotEditProfiles()
413 ProfilesDialog *w = new ProfilesDialog;
418 void MainWindow::slotEditProjectSettings()
420 ProjectSettings *w = new ProjectSettings;
426 void MainWindow::slotUpdateMousePosition(int pos)
428 if (m_activeDocument)
429 switch(m_timecodeFormat->currentIndex()) {
431 statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
434 statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
438 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) //changed
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);
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*)));
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();
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());
472 QWidget* w = factory()->container("mainToolBar", this);
474 if (actionCollection()->action("undo"))
475 delete actionCollection()->action("undo");
476 if(actionCollection()->action("redo"))
477 delete actionCollection()->action("redo");
479 actionCollection()->addAction("undo", undo);
480 actionCollection()->addAction("redo", redo);
484 m_undoView->setStack(doc->commandStack());
485 m_activeDocument = doc;
488 void MainWindow::slotPreferences()
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" ) )
496 // KConfigDialog didn't find an instance of this dialog, so lets
498 KdenliveSettingsDialog* dialog = new KdenliveSettingsDialog(this);
499 //connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
503 #include "mainwindow.moc"