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"
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
61 MainWindow::MainWindow(QWidget *parent)
62 : KXmlGuiWindow(parent),
63 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"));
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);
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);
152 setupGUI(Default, "kdenliveui.rc");
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);
161 setAutoSaveSettings();
166 bool MainWindow::queryClose() {
168 switch (KMessageBox::warningYesNoCancel(this, i18n("Save changes to document ?"))) {
169 case KMessageBox::Yes :
170 // save document here. If saving fails, return false;
172 case KMessageBox::No :
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";
185 TrackView *currentTimeLine = (TrackView *) m_timelineArea->currentWidget();
186 currentTimeLine->projectView()->slotAddEffect(effect, pos, track);
189 void MainWindow::slotRaiseMonitor(bool clipMonitor) {
190 if (clipMonitor) clipMonitorDock->raise();
191 else projectMonitorDock->raise();
194 void MainWindow::slotSetClipDuration(int id, int duration) {
195 if (!m_activeDocument) return;
196 m_activeDocument->setProducerDuration(id, duration);
199 void MainWindow::slotConnectMonitors() {
201 m_projectList->setRenderer(m_clipMonitor->render);
203 connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
205 connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
207 connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
209 connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
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 > &)));
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()));*/
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()));
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()));
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()));
243 KStandardAction::quit(kapp, SLOT(quit()),
246 KStandardAction::open(this, SLOT(openFile()),
249 m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
252 KStandardAction::save(this, SLOT(saveFile()),
255 KStandardAction::saveAs(this, SLOT(saveFileAs()),
258 KStandardAction::openNew(this, SLOT(newFile()),
261 KStandardAction::preferences(this, SLOT(slotPreferences()),
264 /*KStandardAction::undo(this, SLOT(undo()),
267 KStandardAction::redo(this, SLOT(redo()),
268 actionCollection());*/
270 connect(actionCollection(), SIGNAL(actionHighlighted(QAction*)),
271 this, SLOT(slotDisplayActionMessage(QAction*)));
272 //connect(actionCollection(), SIGNAL( clearStatusText() ),
273 //statusBar(), SLOT( clear() ) );
277 /*m_redo = m_commandStack->createRedoAction(actionCollection());
278 m_undo = m_commandStack->createUndoAction(actionCollection());*/
281 void MainWindow::slotDisplayActionMessage(QAction *a) {
282 statusBar()->showMessage(a->data().toString(), 3000);
285 void MainWindow::saveOptions() {
286 KSharedConfigPtr config = KGlobal::config();
287 m_fileOpenRecent->saveEntries(KConfigGroup(config, "Recent Files"));
291 void MainWindow::readOptions() {
292 KSharedConfigPtr config = KGlobal::config();
293 m_fileOpenRecent->loadEntries(KConfigGroup(config, "Recent Files"));
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);
304 void MainWindow::activateDocument() {
305 TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
306 KdenliveDoc *currentDoc = currentTab->document();
307 connectDocument(currentTab, currentDoc);
310 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() {
324 saveFileAs(KFileDialog::getSaveFileName());
327 void MainWindow::saveFile() {
328 if (!fileName.isEmpty()) {
329 saveFileAs(fileName);
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);
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);
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/"));
356 if (KdenliveSettings::rendererpath().isEmpty()) {
357 KdenliveSettings::setRendererpath(KStandardDirs::findExe("inigo"));
359 QStringList profilesFilter;
360 profilesFilter << "*";
361 QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
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);
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);
377 KUrl mltPath = getUrl->selectedUrl();
379 if (mltPath.isEmpty()) exit(1);
380 KdenliveSettings::setMltpath(mltPath.path());
381 QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
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);
389 KUrl rendererPath = getUrl->selectedUrl();
391 if (rendererPath.isEmpty()) exit(1);
392 KdenliveSettings::setRendererpath(rendererPath.path());
395 kDebug() << "RESULTING MLT PATH: " << KdenliveSettings::mltpath();
397 // Parse MLT profiles to build a list of available video formats
398 if (profilesList.isEmpty()) parseProfiles();
402 void MainWindow::slotEditProfiles() {
403 ProfilesDialog *w = new ProfilesDialog;
408 void MainWindow::slotEditProjectSettings() {
409 ProjectSettings *w = new ProjectSettings;
415 void MainWindow::slotUpdateMousePosition(int pos) {
416 if (m_activeDocument)
417 switch (m_timecodeFormat->currentIndex()) {
419 statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
422 statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
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);
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)));
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*)));
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();
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());
462 QWidget* w = factory()->container("mainToolBar", this);
464 if (actionCollection()->action("undo"))
465 delete actionCollection()->action("undo");
466 if (actionCollection()->action("redo"))
467 delete actionCollection()->action("redo");
469 actionCollection()->addAction("undo", undo);
470 actionCollection()->addAction("redo", redo);
474 m_undoView->setStack(doc->commandStack());
475 m_activeDocument = doc;
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"))
485 // KConfigDialog didn't find an instance of this dialog, so lets
487 KdenliveSettingsDialog* dialog = new KdenliveSettingsDialog(this);
488 connect( dialog, SIGNAL(settingsChanged(const QString&)), this, SLOT(updateConfiguration()) );
492 void MainWindow::updateConfiguration() {
493 TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
494 if (currentTab) currentTab->refresh();
497 void MainWindow::slotGotProgressInfo(KUrl url, int progress) {
498 statusProgressBar->setValue(progress);
500 statusLabel->setText(tr("Creating Audio Thumbs"));
501 statusProgressBar->setVisible(true);
503 statusLabel->setText("");
504 statusProgressBar->setVisible(false);
508 #include "mainwindow.moc"