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>
43 #include <mlt++/Mlt.h>
45 #include "mainwindow.h"
46 #include "kdenlivesettings.h"
47 #include "ui_configmisc_ui.h"
48 #include "initeffects.h"
50 #define ID_STATUS_MSG 1
51 #define ID_EDITMODE_MSG 2
52 #define ID_TIMELINE_MSG 3
53 #define ID_TIMELINE_POS 4
54 #define ID_TIMELINE_FORMAT 5
56 MainWindow::MainWindow(QWidget *parent)
57 : KXmlGuiWindow(parent),
58 fileName(QString()), m_activeDocument(NULL), m_commandStack(NULL)
60 m_timelineArea = new KTabWidget(this);
61 m_timelineArea->setHoverCloseButton(true);
62 m_timelineArea->setTabReorderingEnabled(true);
63 connect(m_timelineArea, SIGNAL(currentChanged (int)), this, SLOT(activateDocument()));
65 initEffects::parseEffectFiles( &m_audioEffects, &m_videoEffects );
66 m_monitorManager = new MonitorManager();
68 projectListDock = new QDockWidget(i18n("Project Tree"), this);
69 projectListDock->setObjectName("project_tree");
70 m_projectList = new ProjectList(this);
71 projectListDock->setWidget(m_projectList);
72 addDockWidget(Qt::TopDockWidgetArea, projectListDock);
74 effectListDock = new QDockWidget(i18n("Effect List"), this);
75 effectListDock->setObjectName("effect_list");
76 m_effectList = new EffectsListView(&m_audioEffects, &m_videoEffects, &m_customEffects);
78 //m_effectList = new KListWidget(this);
79 effectListDock->setWidget(m_effectList);
80 addDockWidget(Qt::TopDockWidgetArea, effectListDock);
82 effectStackDock = new QDockWidget(i18n("Effect Stack"), this);
83 effectStackDock->setObjectName("effect_stack");
84 effectStack = new EffectStackView(this);
85 effectStackDock->setWidget(effectStack);
86 addDockWidget(Qt::TopDockWidgetArea, effectStackDock);
88 transitionConfigDock = new QDockWidget(i18n("Transition"), this);
89 transitionConfigDock->setObjectName("transition");
90 transitionConfig = new KListWidget(this);
91 transitionConfigDock->setWidget(transitionConfig);
92 addDockWidget(Qt::TopDockWidgetArea, transitionConfigDock);
95 clipMonitorDock = new QDockWidget(i18n("Clip Monitor"), this);
96 clipMonitorDock->setObjectName("clip_monitor");
97 m_clipMonitor = new Monitor("clip", m_monitorManager, this);
98 clipMonitorDock->setWidget(m_clipMonitor);
99 addDockWidget(Qt::TopDockWidgetArea, clipMonitorDock);
100 //m_clipMonitor->stop();
102 projectMonitorDock = new QDockWidget(i18n("Project Monitor"), this);
103 projectMonitorDock->setObjectName("project_monitor");
104 m_projectMonitor = new Monitor("project", m_monitorManager, this);
105 projectMonitorDock->setWidget(m_projectMonitor);
106 addDockWidget(Qt::TopDockWidgetArea, projectMonitorDock);
108 undoViewDock = new QDockWidget(i18n("Undo History"), this);
109 undoViewDock->setObjectName("undo_history");
110 m_undoView = new QUndoView(this);
111 undoViewDock->setWidget(m_undoView);
112 m_undoView->setStack(m_commandStack);
113 addDockWidget(Qt::TopDockWidgetArea, undoViewDock);
115 overviewDock = new QDockWidget(i18n("Project Overview"), this);
116 overviewDock->setObjectName("project_overview");
117 m_overView = new CustomTrackView(NULL, NULL, this);
118 overviewDock->setWidget(m_overView);
119 addDockWidget(Qt::TopDockWidgetArea, overviewDock);
122 tabifyDockWidget (projectListDock, effectListDock);
123 tabifyDockWidget (projectListDock, effectStackDock);
124 tabifyDockWidget (projectListDock, transitionConfigDock);
125 tabifyDockWidget (projectListDock, undoViewDock);
126 projectListDock->raise();
128 tabifyDockWidget (clipMonitorDock, projectMonitorDock);
129 setCentralWidget(m_timelineArea);
131 m_timecodeFormat = new KComboBox(this);
132 m_timecodeFormat->addItem(i18n("hh:mm:ss::ff"));
133 m_timecodeFormat->addItem(i18n("Frames"));
134 statusBar()->insertPermanentFixedItem("00:00:00:00", ID_TIMELINE_POS);
135 statusBar()->insertPermanentWidget(ID_TIMELINE_FORMAT, m_timecodeFormat);
137 setupGUI(Default, "kdenliveui.rc");
139 connect(projectMonitorDock, SIGNAL(visibilityChanged (bool)), m_projectMonitor, SLOT(refreshMonitor(bool)));
140 connect(clipMonitorDock, SIGNAL(visibilityChanged (bool)), m_clipMonitor, SLOT(refreshMonitor(bool)));
141 connect(m_monitorManager, SIGNAL(connectMonitors ()), this, SLOT(slotConnectMonitors()));
142 connect(m_monitorManager, SIGNAL(raiseClipMonitor (bool)), this, SLOT(slotRaiseMonitor(bool)));
143 connect(m_effectList, SIGNAL(addEffect(int, const QString&)), this, SLOT(slotAddEffect(int, const QString &)));
144 m_monitorManager->initMonitors(m_clipMonitor, m_projectMonitor);
146 setAutoSaveSettings();
151 bool MainWindow::queryClose()
154 switch ( KMessageBox::warningYesNoCancel( this, i18n("Save changes to document ?")) ) {
155 case KMessageBox::Yes :
156 // save document here. If saving fails, return false;
158 case KMessageBox::No :
165 void MainWindow::slotAddEffect(int effectType, const QString &effectName)
167 if (!m_activeDocument) return;
168 QMap <QString, QString> filter;
170 filter = m_videoEffects.effect(effectName);
171 else if (effectType == 1)
172 filter = m_audioEffects.effect(effectName);
174 filter = m_customEffects.effect(effectName);
175 TrackView *currentTimeLine = (TrackView *) m_timelineArea->currentWidget();
176 currentTimeLine->projectView()->slotAddEffect(filter);
179 void MainWindow::slotRaiseMonitor(bool clipMonitor)
181 if (clipMonitor) clipMonitorDock->raise();
182 else projectMonitorDock->raise();
185 void MainWindow::slotSetClipDuration(int id, int duration)
187 if (!m_activeDocument) return;
188 m_activeDocument->setProducerDuration(id, duration);
191 void MainWindow::slotConnectMonitors()
194 m_projectList->setRenderer(m_clipMonitor->render);
196 connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
198 connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
200 connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
202 connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
204 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 > &)));
208 void MainWindow::setupActions()
210 KAction* clearAction = new KAction(this);
211 clearAction->setText(i18n("Clear"));
212 clearAction->setIcon(KIcon("document-new"));
213 clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
214 actionCollection()->addAction("clear", clearAction);
215 /*connect(clearAction, SIGNAL(triggered(bool)),
216 textArea, SLOT(clear()));*/
218 KStandardAction::quit(kapp, SLOT(quit()),
221 KStandardAction::open(this, SLOT(openFile()),
224 m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
227 KStandardAction::save(this, SLOT(saveFile()),
230 KStandardAction::saveAs(this, SLOT(saveFileAs()),
233 KStandardAction::openNew(this, SLOT(newFile()),
236 KStandardAction::preferences(this, SLOT(slotPreferences()),
239 /*KStandardAction::undo(this, SLOT(undo()),
242 KStandardAction::redo(this, SLOT(redo()),
243 actionCollection());*/
247 /*m_redo = m_commandStack->createRedoAction(actionCollection());
248 m_undo = m_commandStack->createUndoAction(actionCollection());*/
251 void MainWindow::saveOptions()
253 KSharedConfigPtr config = KGlobal::config ();
254 m_fileOpenRecent->saveEntries(KConfigGroup (config, "Recent Files"));
258 void MainWindow::readOptions()
260 KSharedConfigPtr config = KGlobal::config ();
261 m_fileOpenRecent->loadEntries(KConfigGroup (config, "Recent Files"));
264 void MainWindow::newFile()
266 KdenliveDoc *doc = new KdenliveDoc(KUrl(), 25, 720, 576);
267 TrackView *trackView = new TrackView(doc);
268 m_timelineArea->addTab(trackView, "New Project");
269 if (m_timelineArea->count() == 1)
270 connectDocument(trackView, doc);
273 void MainWindow::activateDocument()
275 TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
276 KdenliveDoc *currentDoc = currentTab->document();
277 connectDocument(currentTab, currentDoc);
280 void MainWindow::saveFileAs(const QString &outputFileName)
282 KSaveFile file(outputFileName);
285 QByteArray outputByteArray;
286 //outputByteArray.append(textArea->toPlainText());
287 file.write(outputByteArray);
291 fileName = outputFileName;
294 void MainWindow::saveFileAs()
296 saveFileAs(KFileDialog::getSaveFileName());
299 void MainWindow::saveFile()
301 if(!fileName.isEmpty())
303 saveFileAs(fileName);
311 void MainWindow::openFile() //changed
313 KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
314 if (url.isEmpty()) return;
315 m_fileOpenRecent->addUrl (url);
319 void MainWindow::openFile(const KUrl &url) //new
321 KdenliveDoc *doc = new KdenliveDoc(url, 25, 720, 576);
322 TrackView *trackView = new TrackView(doc);
323 m_timelineArea->setCurrentIndex(m_timelineArea->addTab(trackView, QIcon(), doc->documentName()));
324 m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), doc->url().path());
325 //connectDocument(trackView, doc);
328 void MainWindow::slotUpdateMousePosition(int pos)
330 if (m_activeDocument)
331 switch(m_timecodeFormat->currentIndex()) {
333 statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
336 statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
340 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) //changed
342 //m_projectMonitor->stop();
343 kDebug()<<"/////////////////// CONNECTING DOC TO PROJECT VIEW ////////////////";
344 if (m_activeDocument) {
345 if (m_activeDocument == doc) return;
346 m_activeDocument->setProducers(m_projectList->producersList());
347 m_activeDocument->setRenderer(NULL);
349 connect(trackView, SIGNAL(cursorMoved()), m_projectMonitor, SLOT(activateMonitor()));
350 connect(trackView, SIGNAL(mousePosition(int)), this, SLOT(slotUpdateMousePosition(int)));
351 connect(m_projectMonitor, SIGNAL(renderPosition(int)), trackView, SLOT(moveCursorPos(int)));
352 connect(doc, SIGNAL(addProjectClip(DocClipBase *)), m_projectList, SLOT(slotAddClip(DocClipBase *)));
353 connect(doc, SIGNAL(signalDeleteProjectClip(int)), m_projectList, SLOT(slotDeleteClip(int)));
354 connect(doc, SIGNAL(updateClipDisplay(int)), m_projectList, SLOT(slotUpdateClip(int)));
355 connect(doc, SIGNAL(deletTimelineClip(int)), trackView, SLOT(slotDeleteClip(int)));
357 m_projectList->setDocument(doc);
358 m_monitorManager->setTimecode(doc->timecode());
359 doc->setRenderer(m_projectMonitor->render);
360 //m_undoView->setStack(0);
361 m_commandStack = doc->commandStack();
363 m_overView->setScene(trackView->projectScene());
364 m_overView->scale(m_overView->width() / trackView->duration(), m_overView->height() / (50 * trackView->tracksNumber()));
365 //m_overView->fitInView(m_overView->itemAt(0, 50), Qt::KeepAspectRatio);
366 QAction *redo = m_commandStack->createRedoAction(actionCollection());
367 QAction *undo = m_commandStack->createUndoAction(actionCollection());
369 QWidget* w = factory()->container("mainToolBar", this);
371 if (actionCollection()->action("undo"))
372 delete actionCollection()->action("undo");
373 if(actionCollection()->action("redo"))
374 delete actionCollection()->action("redo");
376 actionCollection()->addAction("undo", undo);
377 actionCollection()->addAction("redo", redo);
381 m_undoView->setStack(doc->commandStack());
382 m_activeDocument = doc;
385 void MainWindow::slotPreferences()
387 //An instance of your dialog could be already created and could be
388 // cached, in which case you want to display the cached dialog
389 // instead of creating another one
390 if ( KConfigDialog::showDialog( "settings" ) )
393 // KConfigDialog didn't find an instance of this dialog, so lets
395 KConfigDialog* dialog = new KConfigDialog(this, "settings",
396 KdenliveSettings::self());
398 QWidget *page1 = new QWidget;
399 Ui::ConfigMisc_UI* confWdg = new Ui::ConfigMisc_UI( );
400 confWdg->setupUi(page1);
402 dialog->addPage( page1, i18n("Misc"), "misc" );
404 //User edited the configuration - update your local copies of the
406 connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
411 #include "mainwindow.moc"