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"
47 #define ID_STATUS_MSG 1
48 #define ID_EDITMODE_MSG 2
49 #define ID_TIMELINE_MSG 3
50 #define ID_TIMELINE_POS 4
51 #define ID_TIMELINE_FORMAT 5
53 MainWindow::MainWindow(QWidget *parent)
54 : KXmlGuiWindow(parent),
55 fileName(QString()), m_activeDocument(NULL), m_commandStack(NULL)
57 m_timelineArea = new KTabWidget(this);
58 m_timelineArea->setHoverCloseButton(true);
59 m_timelineArea->setTabReorderingEnabled(true);
60 connect(m_timelineArea, SIGNAL(currentChanged (int)), this, SLOT(activateDocument()));
62 Mlt::Factory::init(NULL);
63 m_monitorManager = new MonitorManager();
65 projectListDock = new QDockWidget(i18n("Project Tree"), this);
66 projectListDock->setObjectName("project_tree");
67 m_projectList = new ProjectList(this);
68 projectListDock->setWidget(m_projectList);
69 addDockWidget(Qt::TopDockWidgetArea, projectListDock);
71 effectListDock = new QDockWidget(i18n("Effect List"), this);
72 effectListDock->setObjectName("effect_list");
73 effectList = new KListWidget(this);
74 effectListDock->setWidget(effectList);
75 addDockWidget(Qt::TopDockWidgetArea, effectListDock);
77 effectStackDock = new QDockWidget(i18n("Effect Stack"), this);
78 effectStackDock->setObjectName("effect_stack");
79 effectStack = new KListWidget(this);
80 effectStackDock->setWidget(effectStack);
81 addDockWidget(Qt::TopDockWidgetArea, effectStackDock);
83 transitionConfigDock = new QDockWidget(i18n("Transition"), this);
84 transitionConfigDock->setObjectName("transition");
85 transitionConfig = new KListWidget(this);
86 transitionConfigDock->setWidget(transitionConfig);
87 addDockWidget(Qt::TopDockWidgetArea, transitionConfigDock);
90 clipMonitorDock = new QDockWidget(i18n("Clip Monitor"), this);
91 clipMonitorDock->setObjectName("clip_monitor");
92 m_clipMonitor = new Monitor("clip", m_monitorManager, this);
93 clipMonitorDock->setWidget(m_clipMonitor);
94 addDockWidget(Qt::TopDockWidgetArea, clipMonitorDock);
95 //m_clipMonitor->stop();
97 projectMonitorDock = new QDockWidget(i18n("Project Monitor"), this);
98 projectMonitorDock->setObjectName("project_monitor");
99 m_projectMonitor = new Monitor("project", m_monitorManager, this);
100 projectMonitorDock->setWidget(m_projectMonitor);
101 addDockWidget(Qt::TopDockWidgetArea, projectMonitorDock);
103 undoViewDock = new QDockWidget(i18n("Undo History"), this);
104 undoViewDock->setObjectName("undo_history");
105 m_undoView = new QUndoView(this);
106 undoViewDock->setWidget(m_undoView);
107 m_undoView->setStack(m_commandStack);
108 addDockWidget(Qt::TopDockWidgetArea, undoViewDock);
110 overviewDock = new QDockWidget(i18n("Project Overview"), this);
111 overviewDock->setObjectName("project_overview");
112 m_overView = new CustomTrackView(NULL, NULL, this);
113 overviewDock->setWidget(m_overView);
114 addDockWidget(Qt::TopDockWidgetArea, overviewDock);
117 tabifyDockWidget (projectListDock, effectListDock);
118 tabifyDockWidget (projectListDock, effectStackDock);
119 tabifyDockWidget (projectListDock, transitionConfigDock);
120 tabifyDockWidget (projectListDock, undoViewDock);
121 projectListDock->raise();
123 tabifyDockWidget (clipMonitorDock, projectMonitorDock);
124 setCentralWidget(m_timelineArea);
126 m_timecodeFormat = new KComboBox(this);
127 m_timecodeFormat->addItem(i18n("hh:mm:ss::ff"));
128 m_timecodeFormat->addItem(i18n("Frames"));
129 statusBar()->insertPermanentFixedItem("00:00:00:00", ID_TIMELINE_POS);
130 statusBar()->insertPermanentWidget(ID_TIMELINE_FORMAT, m_timecodeFormat);
132 setupGUI(Default, "kdenliveui.rc");
134 connect(projectMonitorDock, SIGNAL(visibilityChanged (bool)), m_projectMonitor, SLOT(refreshMonitor(bool)));
135 connect(clipMonitorDock, SIGNAL(visibilityChanged (bool)), m_clipMonitor, SLOT(refreshMonitor(bool)));
136 connect(m_monitorManager, SIGNAL(connectMonitors ()), this, SLOT(slotConnectMonitors()));
137 connect(m_monitorManager, SIGNAL(raiseClipMonitor (bool)), this, SLOT(slotRaiseMonitor(bool)));
138 m_monitorManager->initMonitors(m_clipMonitor, m_projectMonitor);
140 setAutoSaveSettings();
145 bool MainWindow::queryClose()
148 switch ( KMessageBox::warningYesNoCancel( this, i18n("Save changes to document ?")) ) {
149 case KMessageBox::Yes :
150 // save document here. If saving fails, return false;
152 case KMessageBox::No :
159 void MainWindow::slotRaiseMonitor(bool clipMonitor)
161 if (clipMonitor) clipMonitorDock->raise();
162 else projectMonitorDock->raise();
165 void MainWindow::slotSetClipDuration(int id, int duration)
167 if (!m_activeDocument) return;
168 m_activeDocument->setProducerDuration(id, duration);
171 void MainWindow::slotConnectMonitors()
174 m_projectList->setRenderer(m_clipMonitor->render);
176 connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
178 connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
180 connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
182 connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
184 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 > &)));
188 void MainWindow::setupActions()
190 KAction* clearAction = new KAction(this);
191 clearAction->setText(i18n("Clear"));
192 clearAction->setIcon(KIcon("document-new"));
193 clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
194 actionCollection()->addAction("clear", clearAction);
195 /*connect(clearAction, SIGNAL(triggered(bool)),
196 textArea, SLOT(clear()));*/
198 KStandardAction::quit(kapp, SLOT(quit()),
201 KStandardAction::open(this, SLOT(openFile()),
204 m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
207 KStandardAction::save(this, SLOT(saveFile()),
210 KStandardAction::saveAs(this, SLOT(saveFileAs()),
213 KStandardAction::openNew(this, SLOT(newFile()),
216 /*KStandardAction::undo(this, SLOT(undo()),
219 KStandardAction::redo(this, SLOT(redo()),
220 actionCollection());*/
224 /*m_redo = m_commandStack->createRedoAction(actionCollection());
225 m_undo = m_commandStack->createUndoAction(actionCollection());*/
228 void MainWindow::saveOptions()
230 KSharedConfigPtr config = KGlobal::config ();
231 m_fileOpenRecent->saveEntries(KConfigGroup (config, "Recent Files"));
235 void MainWindow::readOptions()
237 KSharedConfigPtr config = KGlobal::config ();
238 m_fileOpenRecent->loadEntries(KConfigGroup (config, "Recent Files"));
241 void MainWindow::newFile()
243 KdenliveDoc *doc = new KdenliveDoc(KUrl(), 25, 720, 576);
244 TrackView *trackView = new TrackView(doc);
245 m_timelineArea->addTab(trackView, "New Project");
246 if (m_timelineArea->count() == 1)
247 connectDocument(trackView, doc);
250 void MainWindow::activateDocument()
252 TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
253 KdenliveDoc *currentDoc = currentTab->document();
254 connectDocument(currentTab, currentDoc);
257 void MainWindow::saveFileAs(const QString &outputFileName)
259 KSaveFile file(outputFileName);
262 QByteArray outputByteArray;
263 //outputByteArray.append(textArea->toPlainText());
264 file.write(outputByteArray);
268 fileName = outputFileName;
271 void MainWindow::saveFileAs()
273 saveFileAs(KFileDialog::getSaveFileName());
276 void MainWindow::saveFile()
278 if(!fileName.isEmpty())
280 saveFileAs(fileName);
288 void MainWindow::openFile() //changed
290 KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
291 if (url.isEmpty()) return;
292 m_fileOpenRecent->addUrl (url);
296 void MainWindow::openFile(const KUrl &url) //new
298 KdenliveDoc *doc = new KdenliveDoc(url, 25, 720, 576);
299 TrackView *trackView = new TrackView(doc);
300 m_timelineArea->setCurrentIndex(m_timelineArea->addTab(trackView, QIcon(), doc->documentName()));
301 m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), doc->url().path());
302 //connectDocument(trackView, doc);
305 void MainWindow::slotUpdateMousePosition(int pos)
307 if (m_activeDocument)
308 switch(m_timecodeFormat->currentIndex()) {
310 statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
313 statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
317 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) //changed
319 //m_projectMonitor->stop();
320 if (m_activeDocument) {
321 if (m_activeDocument == doc) return;
322 m_activeDocument->setProducers(m_projectList->producersList());
323 m_activeDocument->setRenderer(NULL);
325 connect(trackView, SIGNAL(mousePosition(int)), this, SLOT(slotUpdateMousePosition(int)));
326 m_projectList->setDocument(doc);
327 m_monitorManager->setTimecode(doc->timecode());
328 doc->setRenderer(m_projectMonitor->render);
329 //m_undoView->setStack(0);
330 m_commandStack = doc->commandStack();
332 m_overView->setScene(trackView->projectScene());
333 m_overView->scale(m_overView->width() / trackView->duration(), m_overView->height() / (50 * trackView->tracksNumber()));
334 //m_overView->fitInView(m_overView->itemAt(0, 50), Qt::KeepAspectRatio);
335 QAction *redo = m_commandStack->createRedoAction(actionCollection());
336 QAction *undo = m_commandStack->createUndoAction(actionCollection());
338 QWidget* w = factory()->container("mainToolBar", this);
340 if (actionCollection()->action("undo"))
341 delete actionCollection()->action("undo");
342 if(actionCollection()->action("redo"))
343 delete actionCollection()->action("redo");
345 actionCollection()->addAction("undo", undo);
346 actionCollection()->addAction("redo", redo);
350 m_undoView->setStack(doc->commandStack());
351 m_activeDocument = doc;
355 void MainWindow::slotPreferences()
357 //An instance of your dialog could be already created and could be
358 // cached, in which case you want to display the cached dialog
359 // instead of creating another one
360 if ( KConfigDialog::showDialog( "settings" ) )
363 // KConfigDialog didn't find an instance of this dialog, so lets
365 KConfigDialog* dialog = new KConfigDialog(this, "settings",
366 KdenliveSettings::self());
368 QWidget *page1 = new QWidget;
369 Ui::ConfigMisc_UI* confWdg = new Ui::ConfigMisc_UI( );
370 confWdg->setupUi(page1);
372 dialog->addPage( page1, i18n("Misc"), "misc" );
374 //User edited the configuration - update your local copies of the
376 connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
381 #include "mainwindow.moc"