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 EffectsListView(&m_audioEffects, &m_videoEffects, &m_customEffects);
76 //m_effectList = new KListWidget(this);
77 effectListDock->setWidget(m_effectList);
78 addDockWidget(Qt::TopDockWidgetArea, effectListDock);
80 effectStackDock = new QDockWidget(i18n("Effect Stack"), this);
81 effectStackDock->setObjectName("effect_stack");
82 effectStack = new KListWidget(this);
83 effectStackDock->setWidget(effectStack);
84 addDockWidget(Qt::TopDockWidgetArea, effectStackDock);
86 transitionConfigDock = new QDockWidget(i18n("Transition"), this);
87 transitionConfigDock->setObjectName("transition");
88 transitionConfig = new KListWidget(this);
89 transitionConfigDock->setWidget(transitionConfig);
90 addDockWidget(Qt::TopDockWidgetArea, transitionConfigDock);
93 clipMonitorDock = new QDockWidget(i18n("Clip Monitor"), this);
94 clipMonitorDock->setObjectName("clip_monitor");
95 m_clipMonitor = new Monitor("clip", m_monitorManager, this);
96 clipMonitorDock->setWidget(m_clipMonitor);
97 addDockWidget(Qt::TopDockWidgetArea, clipMonitorDock);
98 //m_clipMonitor->stop();
100 projectMonitorDock = new QDockWidget(i18n("Project Monitor"), this);
101 projectMonitorDock->setObjectName("project_monitor");
102 m_projectMonitor = new Monitor("project", m_monitorManager, this);
103 projectMonitorDock->setWidget(m_projectMonitor);
104 addDockWidget(Qt::TopDockWidgetArea, projectMonitorDock);
106 undoViewDock = new QDockWidget(i18n("Undo History"), this);
107 undoViewDock->setObjectName("undo_history");
108 m_undoView = new QUndoView(this);
109 undoViewDock->setWidget(m_undoView);
110 m_undoView->setStack(m_commandStack);
111 addDockWidget(Qt::TopDockWidgetArea, undoViewDock);
113 overviewDock = new QDockWidget(i18n("Project Overview"), this);
114 overviewDock->setObjectName("project_overview");
115 m_overView = new CustomTrackView(NULL, NULL, this);
116 overviewDock->setWidget(m_overView);
117 addDockWidget(Qt::TopDockWidgetArea, overviewDock);
120 tabifyDockWidget (projectListDock, effectListDock);
121 tabifyDockWidget (projectListDock, effectStackDock);
122 tabifyDockWidget (projectListDock, transitionConfigDock);
123 tabifyDockWidget (projectListDock, undoViewDock);
124 projectListDock->raise();
126 tabifyDockWidget (clipMonitorDock, projectMonitorDock);
127 setCentralWidget(m_timelineArea);
129 m_timecodeFormat = new KComboBox(this);
130 m_timecodeFormat->addItem(i18n("hh:mm:ss::ff"));
131 m_timecodeFormat->addItem(i18n("Frames"));
132 statusBar()->insertPermanentFixedItem("00:00:00:00", ID_TIMELINE_POS);
133 statusBar()->insertPermanentWidget(ID_TIMELINE_FORMAT, m_timecodeFormat);
135 setupGUI(Default, "kdenliveui.rc");
137 connect(projectMonitorDock, SIGNAL(visibilityChanged (bool)), m_projectMonitor, SLOT(refreshMonitor(bool)));
138 connect(clipMonitorDock, SIGNAL(visibilityChanged (bool)), m_clipMonitor, SLOT(refreshMonitor(bool)));
139 connect(m_monitorManager, SIGNAL(connectMonitors ()), this, SLOT(slotConnectMonitors()));
140 connect(m_monitorManager, SIGNAL(raiseClipMonitor (bool)), this, SLOT(slotRaiseMonitor(bool)));
141 m_monitorManager->initMonitors(m_clipMonitor, m_projectMonitor);
143 setAutoSaveSettings();
148 bool MainWindow::queryClose()
151 switch ( KMessageBox::warningYesNoCancel( this, i18n("Save changes to document ?")) ) {
152 case KMessageBox::Yes :
153 // save document here. If saving fails, return false;
155 case KMessageBox::No :
162 void MainWindow::slotRaiseMonitor(bool clipMonitor)
164 if (clipMonitor) clipMonitorDock->raise();
165 else projectMonitorDock->raise();
168 void MainWindow::slotSetClipDuration(int id, int duration)
170 if (!m_activeDocument) return;
171 m_activeDocument->setProducerDuration(id, duration);
174 void MainWindow::slotConnectMonitors()
177 m_projectList->setRenderer(m_clipMonitor->render);
179 connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
181 connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
183 connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
185 connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
187 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 > &)));
191 void MainWindow::setupActions()
193 KAction* clearAction = new KAction(this);
194 clearAction->setText(i18n("Clear"));
195 clearAction->setIcon(KIcon("document-new"));
196 clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
197 actionCollection()->addAction("clear", clearAction);
198 /*connect(clearAction, SIGNAL(triggered(bool)),
199 textArea, SLOT(clear()));*/
201 KStandardAction::quit(kapp, SLOT(quit()),
204 KStandardAction::open(this, SLOT(openFile()),
207 m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
210 KStandardAction::save(this, SLOT(saveFile()),
213 KStandardAction::saveAs(this, SLOT(saveFileAs()),
216 KStandardAction::openNew(this, SLOT(newFile()),
219 KStandardAction::preferences(this, SLOT(slotPreferences()),
222 /*KStandardAction::undo(this, SLOT(undo()),
225 KStandardAction::redo(this, SLOT(redo()),
226 actionCollection());*/
230 /*m_redo = m_commandStack->createRedoAction(actionCollection());
231 m_undo = m_commandStack->createUndoAction(actionCollection());*/
234 void MainWindow::saveOptions()
236 KSharedConfigPtr config = KGlobal::config ();
237 m_fileOpenRecent->saveEntries(KConfigGroup (config, "Recent Files"));
241 void MainWindow::readOptions()
243 KSharedConfigPtr config = KGlobal::config ();
244 m_fileOpenRecent->loadEntries(KConfigGroup (config, "Recent Files"));
247 void MainWindow::newFile()
249 KdenliveDoc *doc = new KdenliveDoc(KUrl(), 25, 720, 576);
250 TrackView *trackView = new TrackView(doc);
251 m_timelineArea->addTab(trackView, "New Project");
252 if (m_timelineArea->count() == 1)
253 connectDocument(trackView, doc);
256 void MainWindow::activateDocument()
258 TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
259 KdenliveDoc *currentDoc = currentTab->document();
260 connectDocument(currentTab, currentDoc);
263 void MainWindow::saveFileAs(const QString &outputFileName)
265 KSaveFile file(outputFileName);
268 QByteArray outputByteArray;
269 //outputByteArray.append(textArea->toPlainText());
270 file.write(outputByteArray);
274 fileName = outputFileName;
277 void MainWindow::saveFileAs()
279 saveFileAs(KFileDialog::getSaveFileName());
282 void MainWindow::saveFile()
284 if(!fileName.isEmpty())
286 saveFileAs(fileName);
294 void MainWindow::openFile() //changed
296 KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
297 if (url.isEmpty()) return;
298 m_fileOpenRecent->addUrl (url);
302 void MainWindow::openFile(const KUrl &url) //new
304 KdenliveDoc *doc = new KdenliveDoc(url, 25, 720, 576);
305 TrackView *trackView = new TrackView(doc);
306 m_timelineArea->setCurrentIndex(m_timelineArea->addTab(trackView, QIcon(), doc->documentName()));
307 m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), doc->url().path());
308 //connectDocument(trackView, doc);
311 void MainWindow::slotUpdateMousePosition(int pos)
313 if (m_activeDocument)
314 switch(m_timecodeFormat->currentIndex()) {
316 statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
319 statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
323 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) //changed
325 //m_projectMonitor->stop();
326 kDebug()<<"/////////////////// CONNECTING DOC TO PROJECT VIEW ////////////////";
327 if (m_activeDocument) {
328 if (m_activeDocument == doc) return;
329 m_activeDocument->setProducers(m_projectList->producersList());
330 m_activeDocument->setRenderer(NULL);
332 connect(trackView, SIGNAL(cursorMoved()), m_projectMonitor, SLOT(activateMonitor()));
333 connect(trackView, SIGNAL(mousePosition(int)), this, SLOT(slotUpdateMousePosition(int)));
334 connect(m_projectMonitor, SIGNAL(renderPosition(int)), trackView, SLOT(moveCursorPos(int)));
335 connect(doc, SIGNAL(addProjectClip(DocClipBase *)), m_projectList, SLOT(slotAddClip(DocClipBase *)));
337 m_projectList->setDocument(doc);
338 m_monitorManager->setTimecode(doc->timecode());
339 doc->setRenderer(m_projectMonitor->render);
340 //m_undoView->setStack(0);
341 m_commandStack = doc->commandStack();
343 m_overView->setScene(trackView->projectScene());
344 m_overView->scale(m_overView->width() / trackView->duration(), m_overView->height() / (50 * trackView->tracksNumber()));
345 //m_overView->fitInView(m_overView->itemAt(0, 50), Qt::KeepAspectRatio);
346 QAction *redo = m_commandStack->createRedoAction(actionCollection());
347 QAction *undo = m_commandStack->createUndoAction(actionCollection());
349 QWidget* w = factory()->container("mainToolBar", this);
351 if (actionCollection()->action("undo"))
352 delete actionCollection()->action("undo");
353 if(actionCollection()->action("redo"))
354 delete actionCollection()->action("redo");
356 actionCollection()->addAction("undo", undo);
357 actionCollection()->addAction("redo", redo);
361 m_undoView->setStack(doc->commandStack());
362 m_activeDocument = doc;
366 void MainWindow::slotPreferences()
368 //An instance of your dialog could be already created and could be
369 // cached, in which case you want to display the cached dialog
370 // instead of creating another one
371 if ( KConfigDialog::showDialog( "settings" ) )
374 // KConfigDialog didn't find an instance of this dialog, so lets
376 KConfigDialog* dialog = new KConfigDialog(this, "settings",
377 KdenliveSettings::self());
379 QWidget *page1 = new QWidget;
380 Ui::ConfigMisc_UI* confWdg = new Ui::ConfigMisc_UI( );
381 confWdg->setupUi(page1);
383 dialog->addPage( page1, i18n("Misc"), "misc" );
385 //User edited the configuration - update your local copies of the
387 connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
392 #include "mainwindow.moc"