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>
26 #include <KApplication>
29 #include <KActionCollection>
30 #include <KStandardAction>
31 #include <KFileDialog>
32 #include <KMessageBox>
34 #include <KIO/NetAccess>
37 #include <KConfigDialog>
38 #include <KXMLGUIFactory>
41 #include <mlt++/Mlt.h>
43 #include "mainwindow.h"
44 #include "kdenlivesettings.h"
45 #include "ui_configmisc_ui.h"
46 #include "initeffects.h"
48 #define ID_STATUS_MSG 1
49 #define ID_EDITMODE_MSG 2
50 #define ID_TIMELINE_MSG 3
51 #define ID_TIMELINE_POS 4
52 #define ID_TIMELINE_FORMAT 5
54 MainWindow::MainWindow(QWidget *parent)
55 : KXmlGuiWindow(parent),
56 fileName(QString()), m_activeDocument(NULL), m_commandStack(NULL)
58 m_timelineArea = new KTabWidget(this);
59 m_timelineArea->setHoverCloseButton(true);
60 m_timelineArea->setTabReorderingEnabled(true);
61 connect(m_timelineArea, SIGNAL(currentChanged (int)), this, SLOT(activateDocument()));
63 initEffects::parseEffectFiles( &m_audioEffects, &m_videoEffects );
64 m_monitorManager = new MonitorManager();
66 projectListDock = new QDockWidget(i18n("Project Tree"), this);
67 projectListDock->setObjectName("project_tree");
68 m_projectList = new ProjectList(this);
69 projectListDock->setWidget(m_projectList);
70 addDockWidget(Qt::TopDockWidgetArea, projectListDock);
72 effectListDock = new QDockWidget(i18n("Effect List"), this);
73 effectListDock->setObjectName("effect_list");
74 m_effectList = new KListWidget(this);
75 effectListDock->setWidget(m_effectList);
76 addDockWidget(Qt::TopDockWidgetArea, effectListDock);
78 effectStackDock = new QDockWidget(i18n("Effect Stack"), this);
79 effectStackDock->setObjectName("effect_stack");
80 effectStack = new KListWidget(this);
81 effectStackDock->setWidget(effectStack);
82 addDockWidget(Qt::TopDockWidgetArea, effectStackDock);
84 transitionConfigDock = new QDockWidget(i18n("Transition"), this);
85 transitionConfigDock->setObjectName("transition");
86 transitionConfig = new KListWidget(this);
87 transitionConfigDock->setWidget(transitionConfig);
88 addDockWidget(Qt::TopDockWidgetArea, transitionConfigDock);
91 clipMonitorDock = new QDockWidget(i18n("Clip Monitor"), this);
92 clipMonitorDock->setObjectName("clip_monitor");
93 m_clipMonitor = new Monitor("clip", m_monitorManager, this);
94 clipMonitorDock->setWidget(m_clipMonitor);
95 addDockWidget(Qt::TopDockWidgetArea, clipMonitorDock);
96 //m_clipMonitor->stop();
98 projectMonitorDock = new QDockWidget(i18n("Project Monitor"), this);
99 projectMonitorDock->setObjectName("project_monitor");
100 m_projectMonitor = new Monitor("project", m_monitorManager, this);
101 projectMonitorDock->setWidget(m_projectMonitor);
102 addDockWidget(Qt::TopDockWidgetArea, projectMonitorDock);
104 undoViewDock = new QDockWidget(i18n("Undo History"), this);
105 undoViewDock->setObjectName("undo_history");
106 m_undoView = new QUndoView(this);
107 undoViewDock->setWidget(m_undoView);
108 m_undoView->setStack(m_commandStack);
109 addDockWidget(Qt::TopDockWidgetArea, undoViewDock);
111 overviewDock = new QDockWidget(i18n("Project Overview"), this);
112 overviewDock->setObjectName("project_overview");
113 m_overView = new CustomTrackView(NULL, NULL, this);
114 overviewDock->setWidget(m_overView);
115 addDockWidget(Qt::TopDockWidgetArea, overviewDock);
118 tabifyDockWidget (projectListDock, effectListDock);
119 tabifyDockWidget (projectListDock, effectStackDock);
120 tabifyDockWidget (projectListDock, transitionConfigDock);
121 tabifyDockWidget (projectListDock, undoViewDock);
122 projectListDock->raise();
124 tabifyDockWidget (clipMonitorDock, projectMonitorDock);
125 setCentralWidget(m_timelineArea);
127 m_timecodeFormat = new KComboBox(this);
128 m_timecodeFormat->addItem(i18n("hh:mm:ss::ff"));
129 m_timecodeFormat->addItem(i18n("Frames"));
130 statusBar()->insertPermanentFixedItem("00:00:00:00", ID_TIMELINE_POS);
131 statusBar()->insertPermanentWidget(ID_TIMELINE_FORMAT, m_timecodeFormat);
133 setupGUI(Default, "kdenliveui.rc");
135 connect(projectMonitorDock, SIGNAL(visibilityChanged (bool)), m_projectMonitor, SLOT(refreshMonitor(bool)));
136 connect(clipMonitorDock, SIGNAL(visibilityChanged (bool)), m_clipMonitor, SLOT(refreshMonitor(bool)));
137 connect(m_monitorManager, SIGNAL(connectMonitors ()), this, SLOT(slotConnectMonitors()));
138 connect(m_monitorManager, SIGNAL(raiseClipMonitor (bool)), this, SLOT(slotRaiseMonitor(bool)));
139 m_monitorManager->initMonitors(m_clipMonitor, m_projectMonitor);
141 setAutoSaveSettings();
147 bool MainWindow::queryClose()
150 switch ( KMessageBox::warningYesNoCancel( this, i18n("Save changes to document ?")) ) {
151 case KMessageBox::Yes :
152 // save document here. If saving fails, return false;
154 case KMessageBox::No :
161 void MainWindow::fillEffectsList()
163 QStringList videoEffects = m_videoEffects.effectNames();
164 m_effectList->addItems(videoEffects);
167 void MainWindow::slotRaiseMonitor(bool clipMonitor)
169 if (clipMonitor) clipMonitorDock->raise();
170 else projectMonitorDock->raise();
173 void MainWindow::slotSetClipDuration(int id, int duration)
175 if (!m_activeDocument) return;
176 m_activeDocument->setProducerDuration(id, duration);
179 void MainWindow::slotConnectMonitors()
182 m_projectList->setRenderer(m_clipMonitor->render);
184 connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
186 connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
188 connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
190 connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
192 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 > &)));
196 void MainWindow::setupActions()
198 KAction* clearAction = new KAction(this);
199 clearAction->setText(i18n("Clear"));
200 clearAction->setIcon(KIcon("document-new"));
201 clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
202 actionCollection()->addAction("clear", clearAction);
203 /*connect(clearAction, SIGNAL(triggered(bool)),
204 textArea, SLOT(clear()));*/
206 KStandardAction::quit(kapp, SLOT(quit()),
209 KStandardAction::open(this, SLOT(openFile()),
212 m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
215 KStandardAction::save(this, SLOT(saveFile()),
218 KStandardAction::saveAs(this, SLOT(saveFileAs()),
221 KStandardAction::openNew(this, SLOT(newFile()),
224 KStandardAction::preferences(this, SLOT(slotPreferences()),
227 /*KStandardAction::undo(this, SLOT(undo()),
230 KStandardAction::redo(this, SLOT(redo()),
231 actionCollection());*/
235 /*m_redo = m_commandStack->createRedoAction(actionCollection());
236 m_undo = m_commandStack->createUndoAction(actionCollection());*/
239 void MainWindow::saveOptions()
241 KSharedConfigPtr config = KGlobal::config ();
242 m_fileOpenRecent->saveEntries(KConfigGroup (config, "Recent Files"));
246 void MainWindow::readOptions()
248 KSharedConfigPtr config = KGlobal::config ();
249 m_fileOpenRecent->loadEntries(KConfigGroup (config, "Recent Files"));
252 void MainWindow::newFile()
254 KdenliveDoc *doc = new KdenliveDoc(KUrl(), 25, 720, 576);
255 TrackView *trackView = new TrackView(doc);
256 m_timelineArea->addTab(trackView, "New Project");
257 if (m_timelineArea->count() == 1)
258 connectDocument(trackView, doc);
261 void MainWindow::activateDocument()
263 TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
264 KdenliveDoc *currentDoc = currentTab->document();
265 connectDocument(currentTab, currentDoc);
268 void MainWindow::saveFileAs(const QString &outputFileName)
270 KSaveFile file(outputFileName);
273 QByteArray outputByteArray;
274 //outputByteArray.append(textArea->toPlainText());
275 file.write(outputByteArray);
279 fileName = outputFileName;
282 void MainWindow::saveFileAs()
284 saveFileAs(KFileDialog::getSaveFileName());
287 void MainWindow::saveFile()
289 if(!fileName.isEmpty())
291 saveFileAs(fileName);
299 void MainWindow::openFile() //changed
301 KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
302 if (url.isEmpty()) return;
303 m_fileOpenRecent->addUrl (url);
307 void MainWindow::openFile(const KUrl &url) //new
309 KdenliveDoc *doc = new KdenliveDoc(url, 25, 720, 576);
310 TrackView *trackView = new TrackView(doc);
311 m_timelineArea->setCurrentIndex(m_timelineArea->addTab(trackView, QIcon(), doc->documentName()));
312 m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), doc->url().path());
313 //connectDocument(trackView, doc);
316 void MainWindow::slotUpdateMousePosition(int pos)
318 if (m_activeDocument)
319 switch(m_timecodeFormat->currentIndex()) {
321 statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
324 statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
328 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) //changed
330 //m_projectMonitor->stop();
331 kDebug()<<"/////////////////// CONNECTING DOC TO PROJECT VIEW ////////////////";
332 if (m_activeDocument) {
333 if (m_activeDocument == doc) return;
334 m_activeDocument->setProducers(m_projectList->producersList());
335 m_activeDocument->setRenderer(NULL);
337 connect(trackView, SIGNAL(cursorMoved()), m_projectMonitor, SLOT(activateMonitor()));
338 connect(trackView, SIGNAL(mousePosition(int)), this, SLOT(slotUpdateMousePosition(int)));
339 connect(m_projectMonitor, SIGNAL(renderPosition(int)), trackView, SLOT(moveCursorPos(int)));
340 connect(doc, SIGNAL(addProjectClip(DocClipBase *)), m_projectList, SLOT(slotAddClip(DocClipBase *)));
342 m_projectList->setDocument(doc);
343 m_monitorManager->setTimecode(doc->timecode());
344 doc->setRenderer(m_projectMonitor->render);
345 //m_undoView->setStack(0);
346 m_commandStack = doc->commandStack();
348 m_overView->setScene(trackView->projectScene());
349 m_overView->scale(m_overView->width() / trackView->duration(), m_overView->height() / (50 * trackView->tracksNumber()));
350 //m_overView->fitInView(m_overView->itemAt(0, 50), Qt::KeepAspectRatio);
351 QAction *redo = m_commandStack->createRedoAction(actionCollection());
352 QAction *undo = m_commandStack->createUndoAction(actionCollection());
354 QWidget* w = factory()->container("mainToolBar", this);
356 if (actionCollection()->action("undo"))
357 delete actionCollection()->action("undo");
358 if(actionCollection()->action("redo"))
359 delete actionCollection()->action("redo");
361 actionCollection()->addAction("undo", undo);
362 actionCollection()->addAction("redo", redo);
366 m_undoView->setStack(doc->commandStack());
367 m_activeDocument = doc;
371 void MainWindow::slotPreferences()
373 //An instance of your dialog could be already created and could be
374 // cached, in which case you want to display the cached dialog
375 // instead of creating another one
376 if ( KConfigDialog::showDialog( "settings" ) )
379 // KConfigDialog didn't find an instance of this dialog, so lets
381 KConfigDialog* dialog = new KConfigDialog(this, "settings",
382 KdenliveSettings::self());
384 QWidget *page1 = new QWidget;
385 Ui::ConfigMisc_UI* confWdg = new Ui::ConfigMisc_UI( );
386 confWdg->setupUi(page1);
388 dialog->addPage( page1, i18n("Misc"), "misc" );
390 //User edited the configuration - update your local copies of the
392 connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
397 #include "mainwindow.moc"