]> git.sesse.net Git - kdenlive/blob - src/mainwindow.cpp
effectstack start
[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 #include <QtTest>
26 #include <QtCore>
27
28 #include <KApplication>
29 #include <KAction>
30 #include <KLocale>
31 #include <KActionCollection>
32 #include <KStandardAction>
33 #include <KFileDialog>
34 #include <KMessageBox>
35 #include <KDebug>
36 #include <KIO/NetAccess>
37 #include <KSaveFile>
38 #include <KRuler>
39 #include <KConfigDialog>
40 #include <KXMLGUIFactory>
41 #include <KStatusBar>
42
43 #include <mlt++/Mlt.h>
44
45 #include "mainwindow.h"
46 #include "kdenlivesettings.h"
47 #include "ui_configmisc_ui.h"
48 #include "initeffects.h"
49
50 #define ID_STATUS_MSG 1
51 #define ID_EDITMODE_MSG 2
52 #define ID_TIMELINE_MSG 3
53 #define ID_TIMELINE_POS 4
54 #define ID_TIMELINE_FORMAT 5
55  
56 MainWindow::MainWindow(QWidget *parent)
57     : KXmlGuiWindow(parent),
58       fileName(QString()), m_activeDocument(NULL), m_commandStack(NULL)
59 {
60   m_timelineArea = new KTabWidget(this);
61   m_timelineArea->setHoverCloseButton(true);
62   m_timelineArea->setTabReorderingEnabled(true);
63   connect(m_timelineArea, SIGNAL(currentChanged (int)), this, SLOT(activateDocument()));
64
65   initEffects::parseEffectFiles( &m_audioEffects, &m_videoEffects );
66   m_monitorManager = new MonitorManager();
67
68   projectListDock = new QDockWidget(i18n("Project Tree"), this);
69   projectListDock->setObjectName("project_tree");
70   m_projectList = new ProjectList(this);
71   projectListDock->setWidget(m_projectList);
72   addDockWidget(Qt::TopDockWidgetArea, projectListDock);
73
74   effectListDock = new QDockWidget(i18n("Effect List"), this);
75   effectListDock->setObjectName("effect_list");
76   m_effectList = new EffectsListView(&m_audioEffects, &m_videoEffects, &m_customEffects);
77
78   //m_effectList = new KListWidget(this);
79   effectListDock->setWidget(m_effectList);
80   addDockWidget(Qt::TopDockWidgetArea, effectListDock);
81   
82   effectStackDock = new QDockWidget(i18n("Effect Stack"), this);
83   effectStackDock->setObjectName("effect_stack");
84   effectStack = new EffectStackView(this);
85   effectStackDock->setWidget(effectStack);
86   addDockWidget(Qt::TopDockWidgetArea, effectStackDock);
87   
88   transitionConfigDock = new QDockWidget(i18n("Transition"), this);
89   transitionConfigDock->setObjectName("transition");
90   transitionConfig = new KListWidget(this);
91   transitionConfigDock->setWidget(transitionConfig);
92   addDockWidget(Qt::TopDockWidgetArea, transitionConfigDock);
93
94
95   clipMonitorDock = new QDockWidget(i18n("Clip Monitor"), this);
96   clipMonitorDock->setObjectName("clip_monitor");
97   m_clipMonitor = new Monitor("clip", m_monitorManager, this);
98   clipMonitorDock->setWidget(m_clipMonitor);
99   addDockWidget(Qt::TopDockWidgetArea, clipMonitorDock);
100   //m_clipMonitor->stop();
101
102   projectMonitorDock = new QDockWidget(i18n("Project Monitor"), this);
103   projectMonitorDock->setObjectName("project_monitor");
104   m_projectMonitor = new Monitor("project", m_monitorManager, this);
105   projectMonitorDock->setWidget(m_projectMonitor);
106   addDockWidget(Qt::TopDockWidgetArea, projectMonitorDock);
107
108   undoViewDock = new QDockWidget(i18n("Undo History"), this);
109   undoViewDock->setObjectName("undo_history");
110   m_undoView = new QUndoView(this);
111   undoViewDock->setWidget(m_undoView);
112   m_undoView->setStack(m_commandStack);
113   addDockWidget(Qt::TopDockWidgetArea, undoViewDock);
114
115   overviewDock = new QDockWidget(i18n("Project Overview"), this);
116   overviewDock->setObjectName("project_overview");
117   m_overView = new CustomTrackView(NULL, NULL, this);
118   overviewDock->setWidget(m_overView);
119   addDockWidget(Qt::TopDockWidgetArea, overviewDock);
120
121   setupActions();
122   tabifyDockWidget (projectListDock, effectListDock);
123   tabifyDockWidget (projectListDock, effectStackDock);
124   tabifyDockWidget (projectListDock, transitionConfigDock);
125   tabifyDockWidget (projectListDock, undoViewDock);
126   projectListDock->raise();
127
128   tabifyDockWidget (clipMonitorDock, projectMonitorDock);
129   setCentralWidget(m_timelineArea);
130
131   m_timecodeFormat = new KComboBox(this);
132   m_timecodeFormat->addItem(i18n("hh:mm:ss::ff"));
133   m_timecodeFormat->addItem(i18n("Frames"));
134   statusBar()->insertPermanentFixedItem("00:00:00:00", ID_TIMELINE_POS);
135   statusBar()->insertPermanentWidget(ID_TIMELINE_FORMAT, m_timecodeFormat); 
136
137   setupGUI(Default, "kdenliveui.rc");
138
139   connect(projectMonitorDock, SIGNAL(visibilityChanged (bool)), m_projectMonitor, SLOT(refreshMonitor(bool)));
140   connect(clipMonitorDock, SIGNAL(visibilityChanged (bool)), m_clipMonitor, SLOT(refreshMonitor(bool)));
141   connect(m_monitorManager, SIGNAL(connectMonitors ()), this, SLOT(slotConnectMonitors()));
142   connect(m_monitorManager, SIGNAL(raiseClipMonitor (bool)), this, SLOT(slotRaiseMonitor(bool)));
143   connect(m_effectList, SIGNAL(addEffect(int, const QString&)), this, SLOT(slotAddEffect(int, const QString &)));
144   m_monitorManager->initMonitors(m_clipMonitor, m_projectMonitor);
145         
146   setAutoSaveSettings();
147   newFile();
148 }
149
150 //virtual
151 bool MainWindow::queryClose() 
152 {
153   saveOptions();
154   switch ( KMessageBox::warningYesNoCancel( this, i18n("Save changes to document ?")) ) {
155        case KMessageBox::Yes :
156          // save document here. If saving fails, return false;
157          return true;
158        case KMessageBox::No :
159          return true;
160        default: // cancel
161          return false;
162   }
163 }
164
165 void MainWindow::slotAddEffect(int effectType, const QString &effectName)
166 {
167   if (!m_activeDocument) return;
168   QMap <QString, QString> filter;
169   if (effectType == 0)
170     filter = m_videoEffects.effect(effectName);
171   else if (effectType == 1)
172     filter = m_audioEffects.effect(effectName);
173   else 
174     filter = m_customEffects.effect(effectName);
175   TrackView *currentTimeLine = (TrackView *) m_timelineArea->currentWidget();
176   currentTimeLine->projectView()->slotAddEffect(filter);
177 }
178
179 void MainWindow::slotRaiseMonitor(bool clipMonitor)
180 {
181   if (clipMonitor) clipMonitorDock->raise();
182   else projectMonitorDock->raise();
183 }
184
185 void MainWindow::slotSetClipDuration(int id, int duration)
186 {
187   if (!m_activeDocument) return;
188   m_activeDocument->setProducerDuration(id, duration);
189 }
190
191 void MainWindow::slotConnectMonitors()
192 {
193
194   m_projectList->setRenderer(m_clipMonitor->render);
195
196   connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
197
198   connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
199
200   connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
201
202   connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
203
204   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 > &)));
205
206 }
207
208 void MainWindow::setupActions()
209 {
210   KAction* clearAction = new KAction(this);
211   clearAction->setText(i18n("Clear"));
212   clearAction->setIcon(KIcon("document-new"));
213   clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
214   actionCollection()->addAction("clear", clearAction);
215   /*connect(clearAction, SIGNAL(triggered(bool)),
216           textArea, SLOT(clear()));*/
217  
218   KStandardAction::quit(kapp, SLOT(quit()),
219                         actionCollection());
220  
221   KStandardAction::open(this, SLOT(openFile()),
222                         actionCollection());
223
224   m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
225                         actionCollection());
226
227   KStandardAction::save(this, SLOT(saveFile()),
228                         actionCollection());
229  
230   KStandardAction::saveAs(this, SLOT(saveFileAs()),
231                         actionCollection());
232  
233   KStandardAction::openNew(this, SLOT(newFile()),
234                         actionCollection());
235
236   KStandardAction::preferences(this, SLOT(slotPreferences()),
237                         actionCollection());
238
239   /*KStandardAction::undo(this, SLOT(undo()),
240                         actionCollection());
241
242   KStandardAction::redo(this, SLOT(redo()),
243                         actionCollection());*/
244
245   readOptions();  
246
247   /*m_redo = m_commandStack->createRedoAction(actionCollection());
248   m_undo = m_commandStack->createUndoAction(actionCollection());*/
249 }
250
251 void MainWindow::saveOptions()
252 {
253   KSharedConfigPtr config = KGlobal::config ();
254   m_fileOpenRecent->saveEntries(KConfigGroup (config, "Recent Files"));
255   config->sync(); 
256 }
257
258 void MainWindow::readOptions()
259 {
260   KSharedConfigPtr config = KGlobal::config ();
261   m_fileOpenRecent->loadEntries(KConfigGroup (config, "Recent Files"));
262 }
263  
264 void MainWindow::newFile()
265 {
266   KdenliveDoc *doc = new KdenliveDoc(KUrl(), 25, 720, 576);
267   TrackView *trackView = new TrackView(doc);
268   m_timelineArea->addTab(trackView, "New Project");
269   if (m_timelineArea->count() == 1)
270     connectDocument(trackView, doc);
271 }
272
273 void MainWindow::activateDocument()
274 {
275   TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
276   KdenliveDoc *currentDoc = currentTab->document();
277   connectDocument(currentTab, currentDoc);
278 }
279  
280 void MainWindow::saveFileAs(const QString &outputFileName)
281 {
282   KSaveFile file(outputFileName);
283   file.open();
284   
285   QByteArray outputByteArray;
286   //outputByteArray.append(textArea->toPlainText());
287   file.write(outputByteArray);
288   file.finalize();
289   file.close();
290   
291   fileName = outputFileName;
292 }
293
294 void MainWindow::saveFileAs()
295 {
296   saveFileAs(KFileDialog::getSaveFileName());
297 }
298  
299 void MainWindow::saveFile()
300 {
301   if(!fileName.isEmpty())
302   {
303     saveFileAs(fileName);
304   }
305   else
306   {
307     saveFileAs();
308   }
309 }
310  
311 void MainWindow::openFile() //changed
312 {
313     KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
314     if (url.isEmpty()) return;
315     m_fileOpenRecent->addUrl (url);
316     openFile(url);
317 }
318  
319 void MainWindow::openFile(const KUrl &url) //new
320 {
321   KdenliveDoc *doc = new KdenliveDoc(url, 25, 720, 576);
322   TrackView *trackView = new TrackView(doc);
323   m_timelineArea->setCurrentIndex(m_timelineArea->addTab(trackView, QIcon(), doc->documentName()));
324   m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), doc->url().path());
325   //connectDocument(trackView, doc);
326 }
327
328 void MainWindow::slotUpdateMousePosition(int pos)
329 {
330   if (m_activeDocument)
331     switch(m_timecodeFormat->currentIndex()) {
332       case 0:
333         statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
334         break;
335     default:
336       statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
337     }
338 }
339
340 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) //changed
341 {
342   //m_projectMonitor->stop();
343   kDebug()<<"///////////////////   CONNECTING DOC TO PROJECT VIEW ////////////////";
344   if (m_activeDocument) {
345     if (m_activeDocument == doc) return;
346     m_activeDocument->setProducers(m_projectList->producersList());
347     m_activeDocument->setRenderer(NULL);
348   }
349   connect(trackView, SIGNAL(cursorMoved()), m_projectMonitor, SLOT(activateMonitor()));
350   connect(trackView, SIGNAL(mousePosition(int)), this, SLOT(slotUpdateMousePosition(int)));
351   connect(m_projectMonitor, SIGNAL(renderPosition(int)), trackView, SLOT(moveCursorPos(int)));
352   connect(doc, SIGNAL(addProjectClip(DocClipBase *)), m_projectList, SLOT(slotAddClip(DocClipBase *)));
353   connect(doc, SIGNAL(signalDeleteProjectClip(int)), m_projectList, SLOT(slotDeleteClip(int)));
354   connect(doc, SIGNAL(updateClipDisplay(int)), m_projectList, SLOT(slotUpdateClip(int)));
355   connect(doc, SIGNAL(deletTimelineClip(int)), trackView, SLOT(slotDeleteClip(int)));
356         
357   m_projectList->setDocument(doc);
358   m_monitorManager->setTimecode(doc->timecode());
359   doc->setRenderer(m_projectMonitor->render);
360   //m_undoView->setStack(0);
361   m_commandStack = doc->commandStack();
362
363   m_overView->setScene(trackView->projectScene());
364   m_overView->scale(m_overView->width() / trackView->duration(), m_overView->height() / (50 * trackView->tracksNumber()));
365   //m_overView->fitInView(m_overView->itemAt(0, 50), Qt::KeepAspectRatio);
366   QAction *redo = m_commandStack->createRedoAction(actionCollection());
367   QAction *undo = m_commandStack->createUndoAction(actionCollection());
368
369   QWidget* w = factory()->container("mainToolBar", this);
370   if(w) {
371     if (actionCollection()->action("undo"))
372       delete actionCollection()->action("undo");
373     if(actionCollection()->action("redo"))
374       delete actionCollection()->action("redo");
375
376     actionCollection()->addAction("undo", undo);
377     actionCollection()->addAction("redo", redo);
378     w->addAction(undo);
379     w->addAction(redo);
380   }
381   m_undoView->setStack(doc->commandStack());
382   m_activeDocument = doc;
383 }
384
385 void MainWindow::slotPreferences()
386 {
387   //An instance of your dialog could be already created and could be
388   // cached, in which case you want to display the cached dialog
389   // instead of creating another one
390   if ( KConfigDialog::showDialog( "settings" ) )
391     return;
392
393   // KConfigDialog didn't find an instance of this dialog, so lets
394   // create it :
395   KConfigDialog* dialog = new KConfigDialog(this, "settings",
396                                           KdenliveSettings::self());
397
398   QWidget *page1 = new QWidget;
399   Ui::ConfigMisc_UI* confWdg = new Ui::ConfigMisc_UI( );
400   confWdg->setupUi(page1);
401
402   dialog->addPage( page1, i18n("Misc"), "misc" );
403
404   //User edited the configuration - update your local copies of the
405   //configuration data
406   connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
407
408   dialog->show();
409 }
410
411 #include "mainwindow.moc"