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 m_monitorManager = new MonitorManager();
64 projectListDock = new QDockWidget(i18n("Project Tree"), this);
65 projectListDock->setObjectName("project_tree");
66 m_projectList = new ProjectList(this);
67 projectListDock->setWidget(m_projectList);
68 addDockWidget(Qt::TopDockWidgetArea, projectListDock);
70 effectListDock = new QDockWidget(i18n("Effect List"), this);
71 effectListDock->setObjectName("effect_list");
72 effectList = new KListWidget(this);
73 effectListDock->setWidget(effectList);
74 addDockWidget(Qt::TopDockWidgetArea, effectListDock);
76 effectStackDock = new QDockWidget(i18n("Effect Stack"), this);
77 effectStackDock->setObjectName("effect_stack");
78 effectStack = new KListWidget(this);
79 effectStackDock->setWidget(effectStack);
80 addDockWidget(Qt::TopDockWidgetArea, effectStackDock);
82 transitionConfigDock = new QDockWidget(i18n("Transition"), this);
83 transitionConfigDock->setObjectName("transition");
84 transitionConfig = new KListWidget(this);
85 transitionConfigDock->setWidget(transitionConfig);
86 addDockWidget(Qt::TopDockWidgetArea, transitionConfigDock);
88 Mlt::Factory::init(NULL);
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);
96 projectMonitorDock = new QDockWidget(i18n("Project Monitor"), this);
97 projectMonitorDock->setObjectName("project_monitor");
98 m_projectMonitor = new Monitor("project", m_monitorManager, this);
99 projectMonitorDock->setWidget(m_projectMonitor);
100 addDockWidget(Qt::TopDockWidgetArea, projectMonitorDock);
102 undoViewDock = new QDockWidget(i18n("Undo History"), this);
103 undoViewDock->setObjectName("undo_history");
104 m_undoView = new QUndoView(this);
105 undoViewDock->setWidget(m_undoView);
106 m_undoView->setStack(m_commandStack);
107 addDockWidget(Qt::TopDockWidgetArea, undoViewDock);
109 overviewDock = new QDockWidget(i18n("Project Overview"), this);
110 overviewDock->setObjectName("project_overview");
111 m_overView = new CustomTrackView(NULL, NULL, this);
112 overviewDock->setWidget(m_overView);
113 addDockWidget(Qt::TopDockWidgetArea, overviewDock);
116 tabifyDockWidget (projectListDock, effectListDock);
117 tabifyDockWidget (projectListDock, effectStackDock);
118 tabifyDockWidget (projectListDock, transitionConfigDock);
119 tabifyDockWidget (projectListDock, undoViewDock);
120 projectListDock->raise();
122 tabifyDockWidget (clipMonitorDock, projectMonitorDock);
123 setCentralWidget(m_timelineArea);
125 m_timecodeFormat = new KComboBox(this);
126 m_timecodeFormat->addItem(i18n("hh:mm:ss::ff"));
127 m_timecodeFormat->addItem(i18n("Frames"));
128 statusBar()->insertPermanentFixedItem("00:00:00:00", ID_TIMELINE_POS);
129 statusBar()->insertPermanentWidget(ID_TIMELINE_FORMAT, m_timecodeFormat);
131 setupGUI(Default, "kdenliveui.rc");
133 connect(projectMonitorDock, SIGNAL(visibilityChanged (bool)), m_projectMonitor, SLOT(refreshMonitor(bool)));
134 connect(clipMonitorDock, SIGNAL(visibilityChanged (bool)), m_clipMonitor, SLOT(refreshMonitor(bool)));
135 connect(m_monitorManager, SIGNAL(connectMonitors ()), this, SLOT(slotConnectMonitors()));
136 connect(m_monitorManager, SIGNAL(raiseClipMonitor (bool)), this, SLOT(slotRaiseMonitor(bool)));
137 m_monitorManager->initMonitors(m_clipMonitor, m_projectMonitor);
139 setAutoSaveSettings();
144 bool MainWindow::queryClose()
147 switch ( KMessageBox::warningYesNoCancel( this, i18n("Save changes to document ?")) ) {
148 case KMessageBox::Yes :
149 // save document here. If saving fails, return false;
151 case KMessageBox::No :
158 void MainWindow::slotRaiseMonitor(bool clipMonitor)
160 if (clipMonitor) clipMonitorDock->raise();
161 else projectMonitorDock->raise();
164 void MainWindow::slotSetClipDuration(int id, int duration)
166 if (!m_activeDocument) return;
167 m_activeDocument->setProducerDuration(id, duration);
170 void MainWindow::slotConnectMonitors()
173 m_projectList->setRenderer(m_clipMonitor->render);
175 connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
177 connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
179 connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
181 connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
183 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 > &)));
187 void MainWindow::setupActions()
189 KAction* clearAction = new KAction(this);
190 clearAction->setText(i18n("Clear"));
191 clearAction->setIcon(KIcon("document-new"));
192 clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
193 actionCollection()->addAction("clear", clearAction);
194 /*connect(clearAction, SIGNAL(triggered(bool)),
195 textArea, SLOT(clear()));*/
197 KStandardAction::quit(kapp, SLOT(quit()),
200 KStandardAction::open(this, SLOT(openFile()),
203 m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
206 KStandardAction::save(this, SLOT(saveFile()),
209 KStandardAction::saveAs(this, SLOT(saveFileAs()),
212 KStandardAction::openNew(this, SLOT(newFile()),
215 /*KStandardAction::undo(this, SLOT(undo()),
218 KStandardAction::redo(this, SLOT(redo()),
219 actionCollection());*/
223 /*m_redo = m_commandStack->createRedoAction(actionCollection());
224 m_undo = m_commandStack->createUndoAction(actionCollection());*/
227 void MainWindow::saveOptions()
229 KSharedConfigPtr config = KGlobal::config ();
230 m_fileOpenRecent->saveEntries(KConfigGroup (config, "Recent Files"));
234 void MainWindow::readOptions()
236 KSharedConfigPtr config = KGlobal::config ();
237 m_fileOpenRecent->loadEntries(KConfigGroup (config, "Recent Files"));
240 void MainWindow::newFile()
242 KdenliveDoc *doc = new KdenliveDoc(KUrl(), 25, 720, 576);
243 TrackView *trackView = new TrackView(doc);
244 m_timelineArea->addTab(trackView, "New Project");
245 if (m_timelineArea->count() == 1)
246 connectDocument(trackView, doc);
249 void MainWindow::activateDocument()
251 TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
252 KdenliveDoc *currentDoc = currentTab->document();
253 connectDocument(currentTab, currentDoc);
256 void MainWindow::saveFileAs(const QString &outputFileName)
258 KSaveFile file(outputFileName);
261 QByteArray outputByteArray;
262 //outputByteArray.append(textArea->toPlainText());
263 file.write(outputByteArray);
267 fileName = outputFileName;
270 void MainWindow::saveFileAs()
272 saveFileAs(KFileDialog::getSaveFileName());
275 void MainWindow::saveFile()
277 if(!fileName.isEmpty())
279 saveFileAs(fileName);
287 void MainWindow::openFile() //changed
289 KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
290 if (url.isEmpty()) return;
291 m_fileOpenRecent->addUrl (url);
295 void MainWindow::openFile(const KUrl &url) //new
297 KdenliveDoc *doc = new KdenliveDoc(url, 25, 720, 576);
298 TrackView *trackView = new TrackView(doc);
299 m_timelineArea->setCurrentIndex(m_timelineArea->addTab(trackView, QIcon(), doc->documentName()));
300 m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), doc->url().path());
301 //connectDocument(trackView, doc);
304 void MainWindow::slotUpdateMousePosition(int pos)
306 if (m_activeDocument)
307 switch(m_timecodeFormat->currentIndex()) {
309 statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
312 statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
316 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) //changed
318 //m_projectMonitor->stop();
319 if (m_activeDocument) {
320 if (m_activeDocument == doc) return;
321 m_activeDocument->setProducers(m_projectList->producersList());
322 m_activeDocument->setRenderer(NULL);
324 connect(trackView, SIGNAL(mousePosition(int)), this, SLOT(slotUpdateMousePosition(int)));
325 m_projectList->setDocument(doc);
326 m_monitorManager->setTimecode(doc->timecode());
327 doc->setRenderer(m_projectMonitor->render);
328 //m_undoView->setStack(0);
329 m_commandStack = doc->commandStack();
331 m_overView->setScene(trackView->projectScene());
332 m_overView->scale(m_overView->width() / trackView->duration(), m_overView->height() / (50 * trackView->tracksNumber()));
333 //m_overView->fitInView(m_overView->itemAt(0, 50), Qt::KeepAspectRatio);
334 QAction *redo = m_commandStack->createRedoAction(actionCollection());
335 QAction *undo = m_commandStack->createUndoAction(actionCollection());
337 QWidget* w = factory()->container("mainToolBar", this);
339 if (actionCollection()->action("undo"))
340 delete actionCollection()->action("undo");
341 if(actionCollection()->action("redo"))
342 delete actionCollection()->action("redo");
344 actionCollection()->addAction("undo", undo);
345 actionCollection()->addAction("redo", redo);
349 m_undoView->setStack(doc->commandStack());
350 m_activeDocument = doc;
354 void MainWindow::slotPreferences()
356 //An instance of your dialog could be already created and could be
357 // cached, in which case you want to display the cached dialog
358 // instead of creating another one
359 if ( KConfigDialog::showDialog( "settings" ) )
362 // KConfigDialog didn't find an instance of this dialog, so lets
364 KConfigDialog* dialog = new KConfigDialog(this, "settings",
365 KdenliveSettings::self());
367 QWidget *page1 = new QWidget;
368 Ui::ConfigMisc_UI* confWdg = new Ui::ConfigMisc_UI( );
369 confWdg->setupUi(page1);
371 dialog->addPage( page1, i18n("Misc"), "misc" );
373 //User edited the configuration - update your local copies of the
375 connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
380 #include "mainwindow.moc"