]> git.sesse.net Git - kdenlive/blob - src/mainwindow.cpp
Start implementing effects
[kdenlive] / src / mainwindow.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21
22 #include <QTextStream>
23 #include <QTimer>
24 #include <QAction>
25
26 #include <KApplication>
27 #include <KAction>
28 #include <KLocale>
29 #include <KActionCollection>
30 #include <KStandardAction>
31 #include <KFileDialog>
32 #include <KMessageBox>
33 #include <KDebug>
34 #include <KIO/NetAccess>
35 #include <KSaveFile>
36 #include <KRuler>
37 #include <KConfigDialog>
38 #include <KXMLGUIFactory>
39 #include <KStatusBar>
40
41 #include <mlt++/Mlt.h>
42
43 #include "mainwindow.h"
44 #include "kdenlivesettings.h"
45 #include "ui_configmisc_ui.h"
46 #include "initeffects.h"
47
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
53  
54 MainWindow::MainWindow(QWidget *parent)
55     : KXmlGuiWindow(parent),
56       fileName(QString()), m_activeDocument(NULL), m_commandStack(NULL)
57 {
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()));
62
63   initEffects::parseEffectFiles( &m_audioEffects, &m_videoEffects );
64   m_monitorManager = new MonitorManager();
65
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);
71
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);
77   
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);
83   
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);
89
90
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();
97
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);
103
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);
110
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);
116
117   setupActions();
118   tabifyDockWidget (projectListDock, effectListDock);
119   tabifyDockWidget (projectListDock, effectStackDock);
120   tabifyDockWidget (projectListDock, transitionConfigDock);
121   tabifyDockWidget (projectListDock, undoViewDock);
122   projectListDock->raise();
123
124   tabifyDockWidget (clipMonitorDock, projectMonitorDock);
125   setCentralWidget(m_timelineArea);
126
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); 
132
133   setupGUI(Default, "kdenliveui.rc");
134
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);
140
141   setAutoSaveSettings();
142   fillEffectsList();
143   newFile();
144 }
145
146 //virtual
147 bool MainWindow::queryClose() 
148 {
149   saveOptions();
150   switch ( KMessageBox::warningYesNoCancel( this, i18n("Save changes to document ?")) ) {
151        case KMessageBox::Yes :
152          // save document here. If saving fails, return false;
153          return true;
154        case KMessageBox::No :
155          return true;
156        default: // cancel
157          return false;
158   }
159 }
160
161 void MainWindow::fillEffectsList()
162 {
163   QStringList videoEffects = m_videoEffects.effectNames();
164   m_effectList->addItems(videoEffects);
165 }
166
167 void MainWindow::slotRaiseMonitor(bool clipMonitor)
168 {
169   if (clipMonitor) clipMonitorDock->raise();
170   else projectMonitorDock->raise();
171 }
172
173 void MainWindow::slotSetClipDuration(int id, int duration)
174 {
175   if (!m_activeDocument) return;
176   m_activeDocument->setProducerDuration(id, duration);
177 }
178
179 void MainWindow::slotConnectMonitors()
180 {
181
182   m_projectList->setRenderer(m_clipMonitor->render);
183
184   connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
185
186   connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
187
188   connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
189
190   connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
191
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 > &)));
193
194 }
195
196 void MainWindow::setupActions()
197 {
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()));*/
205  
206   KStandardAction::quit(kapp, SLOT(quit()),
207                         actionCollection());
208  
209   KStandardAction::open(this, SLOT(openFile()),
210                         actionCollection());
211
212   m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
213                         actionCollection());
214
215   KStandardAction::save(this, SLOT(saveFile()),
216                         actionCollection());
217  
218   KStandardAction::saveAs(this, SLOT(saveFileAs()),
219                         actionCollection());
220  
221   KStandardAction::openNew(this, SLOT(newFile()),
222                         actionCollection());
223
224   KStandardAction::preferences(this, SLOT(slotPreferences()),
225                         actionCollection());
226
227   /*KStandardAction::undo(this, SLOT(undo()),
228                         actionCollection());
229
230   KStandardAction::redo(this, SLOT(redo()),
231                         actionCollection());*/
232
233   readOptions();  
234
235   /*m_redo = m_commandStack->createRedoAction(actionCollection());
236   m_undo = m_commandStack->createUndoAction(actionCollection());*/
237 }
238
239 void MainWindow::saveOptions()
240 {
241   KSharedConfigPtr config = KGlobal::config ();
242   m_fileOpenRecent->saveEntries(KConfigGroup (config, "Recent Files"));
243   config->sync(); 
244 }
245
246 void MainWindow::readOptions()
247 {
248   KSharedConfigPtr config = KGlobal::config ();
249   m_fileOpenRecent->loadEntries(KConfigGroup (config, "Recent Files"));
250 }
251  
252 void MainWindow::newFile()
253 {
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);
259 }
260
261 void MainWindow::activateDocument()
262 {
263   TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
264   KdenliveDoc *currentDoc = currentTab->document();
265   connectDocument(currentTab, currentDoc);
266 }
267  
268 void MainWindow::saveFileAs(const QString &outputFileName)
269 {
270   KSaveFile file(outputFileName);
271   file.open();
272   
273   QByteArray outputByteArray;
274   //outputByteArray.append(textArea->toPlainText());
275   file.write(outputByteArray);
276   file.finalize();
277   file.close();
278   
279   fileName = outputFileName;
280 }
281
282 void MainWindow::saveFileAs()
283 {
284   saveFileAs(KFileDialog::getSaveFileName());
285 }
286  
287 void MainWindow::saveFile()
288 {
289   if(!fileName.isEmpty())
290   {
291     saveFileAs(fileName);
292   }
293   else
294   {
295     saveFileAs();
296   }
297 }
298  
299 void MainWindow::openFile() //changed
300 {
301     KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
302     if (url.isEmpty()) return;
303     m_fileOpenRecent->addUrl (url);
304     openFile(url);
305 }
306  
307 void MainWindow::openFile(const KUrl &url) //new
308 {
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);
314 }
315
316 void MainWindow::slotUpdateMousePosition(int pos)
317 {
318   if (m_activeDocument)
319     switch(m_timecodeFormat->currentIndex()) {
320       case 0:
321         statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
322         break;
323     default:
324       statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
325     }
326 }
327
328 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) //changed
329 {
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);
336   }
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 *)));
341
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();
347
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());
353
354   QWidget* w = factory()->container("mainToolBar", this);
355   if(w) {
356     if (actionCollection()->action("undo"))
357       delete actionCollection()->action("undo");
358     if(actionCollection()->action("redo"))
359       delete actionCollection()->action("redo");
360
361     actionCollection()->addAction("undo", undo);
362     actionCollection()->addAction("redo", redo);
363     w->addAction(undo);
364     w->addAction(redo);
365   }
366   m_undoView->setStack(doc->commandStack());
367   m_activeDocument = doc;
368 }
369
370
371 void MainWindow::slotPreferences()
372 {
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" ) )
377     return;
378
379   // KConfigDialog didn't find an instance of this dialog, so lets
380   // create it :
381   KConfigDialog* dialog = new KConfigDialog(this, "settings",
382                                           KdenliveSettings::self());
383
384   QWidget *page1 = new QWidget;
385   Ui::ConfigMisc_UI* confWdg = new Ui::ConfigMisc_UI( );
386   confWdg->setupUi(page1);
387
388   dialog->addPage( page1, i18n("Misc"), "misc" );
389
390   //User edited the configuration - update your local copies of the
391   //configuration data
392   connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
393
394   dialog->show();
395 }
396
397 #include "mainwindow.moc"