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()));
238 KStandardAction::quit(kapp, SLOT(quit()),
241 KStandardAction::open(this, SLOT(openFile()),
244 m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
247 KStandardAction::save(this, SLOT(saveFile()),
250 KStandardAction::saveAs(this, SLOT(saveFileAs()),
253 KStandardAction::openNew(this, SLOT(newFile()),
256 KStandardAction::preferences(this, SLOT(slotPreferences()),
259 /*KStandardAction::undo(this, SLOT(undo()),
262 KStandardAction::redo(this, SLOT(redo()),
263 actionCollection());*/
265 connect(actionCollection(), SIGNAL(actionHighlighted(QAction*)),
266 this, SLOT(slotDisplayActionMessage(QAction*)));
267 //connect(actionCollection(), SIGNAL( clearStatusText() ),
268 //statusBar(), SLOT( clear() ) );
272 /*m_redo = m_commandStack->createRedoAction(actionCollection());
273 m_undo = m_commandStack->createUndoAction(actionCollection());*/
276 void MainWindow::slotDisplayActionMessage(QAction *a) {
277 statusBar()->showMessage(a->data().toString(), 3000);
280 void MainWindow::saveOptions() {
281 KSharedConfigPtr config = KGlobal::config();
282 m_fileOpenRecent->saveEntries(KConfigGroup(config, "Recent Files"));
286 void MainWindow::readOptions() {
287 KSharedConfigPtr config = KGlobal::config();
288 m_fileOpenRecent->loadEntries(KConfigGroup(config, "Recent Files"));
291 void MainWindow::newFile() {
292 KdenliveDoc *doc = new KdenliveDoc(KUrl(), 25, 720, 576);
293 TrackView *trackView = new TrackView(doc);
294 m_timelineArea->addTab(trackView, "New Project");
295 if (m_timelineArea->count() == 1)
296 connectDocument(trackView, doc);
299 void MainWindow::activateDocument() {
300 TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
301 KdenliveDoc *currentDoc = currentTab->document();
302 connectDocument(currentTab, currentDoc);
305 void MainWindow::saveFileAs(const QString &outputFileName) {
306 KSaveFile file(outputFileName);
309 QByteArray outputByteArray;
310 //outputByteArray.append(textArea->toPlainText());
311 file.write(outputByteArray);
315 fileName = outputFileName;
318 void MainWindow::saveFileAs() {
319 saveFileAs(KFileDialog::getSaveFileName());
322 void MainWindow::saveFile() {
323 if (!fileName.isEmpty()) {
324 saveFileAs(fileName);
330 void MainWindow::openFile() { //changed
331 KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
332 if (url.isEmpty()) return;
333 m_fileOpenRecent->addUrl(url);
337 void MainWindow::openFile(const KUrl &url) { //new
338 KdenliveDoc *doc = new KdenliveDoc(url, 25, 720, 576);
339 TrackView *trackView = new TrackView(doc);
340 m_timelineArea->setCurrentIndex(m_timelineArea->addTab(trackView, QIcon(), doc->documentName()));
341 m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), doc->url().path());
342 //connectDocument(trackView, doc);
346 void MainWindow::parseProfiles() {
347 //kdDebug()<<" + + YOUR MLT INSTALL WAS FOUND IN: "<< MLT_PREFIX <<endl;
348 if (KdenliveSettings::mltpath().isEmpty()) {
349 KdenliveSettings::setMltpath(QString(MLT_PREFIX) + QString("/share/mlt/profiles/"));
351 if (KdenliveSettings::rendererpath().isEmpty()) {
352 KdenliveSettings::setRendererpath(KStandardDirs::findExe("inigo"));
354 QStringList profilesFilter;
355 profilesFilter << "*";
356 QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
358 if (profilesList.isEmpty()) {
359 // Cannot find MLT path, try finding inigo
360 QString profilePath = KdenliveSettings::rendererpath();
361 if (!profilePath.isEmpty()) {
362 profilePath = profilePath.section('/', 0, -3);
363 KdenliveSettings::setMltpath(profilePath + "/share/mlt/profiles/");
364 QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
367 if (profilesList.isEmpty()) {
368 // Cannot find the MLT profiles, ask for location
369 KUrlRequesterDialog *getUrl = new KUrlRequesterDialog(KdenliveSettings::mltpath(), i18n("Cannot find your Mlt profiles, please give the path"), this);
370 getUrl->fileDialog()->setMode(KFile::Directory);
372 KUrl mltPath = getUrl->selectedUrl();
374 if (mltPath.isEmpty()) exit(1);
375 KdenliveSettings::setMltpath(mltPath.path());
376 QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
380 if (KdenliveSettings::rendererpath().isEmpty()) {
381 // Cannot find the MLT inigo renderer, ask for location
382 KUrlRequesterDialog *getUrl = new KUrlRequesterDialog(KdenliveSettings::mltpath(), i18n("Cannot find the inigo program required for rendering (part of Mlt)"), this);
384 KUrl rendererPath = getUrl->selectedUrl();
386 if (rendererPath.isEmpty()) exit(1);
387 KdenliveSettings::setRendererpath(rendererPath.path());
390 kDebug() << "RESULTING MLT PATH: " << KdenliveSettings::mltpath();
392 // Parse MLT profiles to build a list of available video formats
393 if (profilesList.isEmpty()) parseProfiles();
397 void MainWindow::slotEditProfiles() {
398 ProfilesDialog *w = new ProfilesDialog;
403 void MainWindow::slotEditProjectSettings() {
404 ProjectSettings *w = new ProjectSettings;
410 void MainWindow::slotUpdateMousePosition(int pos) {
411 if (m_activeDocument)
412 switch (m_timecodeFormat->currentIndex()) {
414 statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
417 statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
421 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) { //changed
422 //m_projectMonitor->stop();
423 kDebug() << "/////////////////// CONNECTING DOC TO PROJECT VIEW ////////////////";
424 if (m_activeDocument) {
425 if (m_activeDocument == doc) return;
426 m_activeDocument->setProducers(m_projectList->producersList());
427 m_activeDocument->setRenderer(NULL);
429 connect(trackView, SIGNAL(cursorMoved()), m_projectMonitor, SLOT(activateMonitor()));
430 connect(trackView, SIGNAL(mousePosition(int)), this, SLOT(slotUpdateMousePosition(int)));
431 connect(m_projectMonitor, SIGNAL(renderPosition(int)), trackView, SLOT(moveCursorPos(int)));
432 connect(m_projectMonitor, SIGNAL(durationChanged(int)), trackView->projectView(), SLOT(setDuration(int)));
433 connect(doc, SIGNAL(addProjectClip(DocClipBase *)), m_projectList, SLOT(slotAddClip(DocClipBase *)));
434 connect(doc, SIGNAL(signalDeleteProjectClip(int)), m_projectList, SLOT(slotDeleteClip(int)));
435 connect(doc, SIGNAL(updateClipDisplay(int)), m_projectList, SLOT(slotUpdateClip(int)));
436 connect(doc, SIGNAL(deletTimelineClip(int)), trackView, SLOT(slotDeleteClip(int)));
437 connect(doc, SIGNAL(thumbsProgress(KUrl, int)), this, SLOT(slotGotProgressInfo(KUrl, int)));
439 connect(trackView, SIGNAL(clipItemSelected(ClipItem*)), effectStack, SLOT(slotClipItemSelected(ClipItem*)));
440 connect(effectStack, SIGNAL(updateClipEffect(ClipItem*, QDomElement, QDomElement)), trackView->projectView(), SLOT(slotUpdateClipEffect(ClipItem*, QDomElement, QDomElement)));
441 connect(effectStack, SIGNAL(removeEffect(ClipItem*, QDomElement)), trackView->projectView(), SLOT(slotDeleteEffect(ClipItem*, QDomElement)));
442 connect(effectStack, SIGNAL(changeEffectState(ClipItem*, QDomElement, bool)), trackView->projectView(), SLOT(slotChangeEffectState(ClipItem*, QDomElement, bool)));
443 connect(effectStack, SIGNAL(refreshEffectStack(ClipItem*)), trackView->projectView(), SLOT(slotRefreshEffects(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() {
474 //An instance of your dialog could be already created and could be
475 // cached, in which case you want to display the cached dialog
476 // instead of creating another one
477 if (KConfigDialog::showDialog("settings"))
480 // KConfigDialog didn't find an instance of this dialog, so lets
482 KdenliveSettingsDialog* dialog = new KdenliveSettingsDialog(this);
483 //connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
487 void MainWindow::slotGotProgressInfo(KUrl url, int progress) {
488 statusProgressBar->setValue(progress);
490 statusLabel->setText(tr("Creating Audio Thumbs"));
491 statusProgressBar->setVisible(true);
493 statusLabel->setText("");
494 statusProgressBar->setVisible(false);
498 #include "mainwindow.moc"