]> git.sesse.net Git - kdenlive/blob - src/mainwindow.cpp
Fix undo/redo delete clip
[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 EffectsListView(&m_audioEffects, &m_videoEffects, &m_customEffects);
75
76   //m_effectList = new KListWidget(this);
77   effectListDock->setWidget(m_effectList);
78   addDockWidget(Qt::TopDockWidgetArea, effectListDock);
79   
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);
85   
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);
91
92
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();
99
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);
105
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);
112
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);
118
119   setupActions();
120   tabifyDockWidget (projectListDock, effectListDock);
121   tabifyDockWidget (projectListDock, effectStackDock);
122   tabifyDockWidget (projectListDock, transitionConfigDock);
123   tabifyDockWidget (projectListDock, undoViewDock);
124   projectListDock->raise();
125
126   tabifyDockWidget (clipMonitorDock, projectMonitorDock);
127   setCentralWidget(m_timelineArea);
128
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); 
134
135   setupGUI(Default, "kdenliveui.rc");
136
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);
142
143   setAutoSaveSettings();
144   newFile();
145 }
146
147 //virtual
148 bool MainWindow::queryClose() 
149 {
150   saveOptions();
151   switch ( KMessageBox::warningYesNoCancel( this, i18n("Save changes to document ?")) ) {
152        case KMessageBox::Yes :
153          // save document here. If saving fails, return false;
154          return true;
155        case KMessageBox::No :
156          return true;
157        default: // cancel
158          return false;
159   }
160 }
161
162 void MainWindow::slotRaiseMonitor(bool clipMonitor)
163 {
164   if (clipMonitor) clipMonitorDock->raise();
165   else projectMonitorDock->raise();
166 }
167
168 void MainWindow::slotSetClipDuration(int id, int duration)
169 {
170   if (!m_activeDocument) return;
171   m_activeDocument->setProducerDuration(id, duration);
172 }
173
174 void MainWindow::slotConnectMonitors()
175 {
176
177   m_projectList->setRenderer(m_clipMonitor->render);
178
179   connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
180
181   connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
182
183   connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
184
185   connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
186
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 > &)));
188
189 }
190
191 void MainWindow::setupActions()
192 {
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()));*/
200  
201   KStandardAction::quit(kapp, SLOT(quit()),
202                         actionCollection());
203  
204   KStandardAction::open(this, SLOT(openFile()),
205                         actionCollection());
206
207   m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
208                         actionCollection());
209
210   KStandardAction::save(this, SLOT(saveFile()),
211                         actionCollection());
212  
213   KStandardAction::saveAs(this, SLOT(saveFileAs()),
214                         actionCollection());
215  
216   KStandardAction::openNew(this, SLOT(newFile()),
217                         actionCollection());
218
219   KStandardAction::preferences(this, SLOT(slotPreferences()),
220                         actionCollection());
221
222   /*KStandardAction::undo(this, SLOT(undo()),
223                         actionCollection());
224
225   KStandardAction::redo(this, SLOT(redo()),
226                         actionCollection());*/
227
228   readOptions();  
229
230   /*m_redo = m_commandStack->createRedoAction(actionCollection());
231   m_undo = m_commandStack->createUndoAction(actionCollection());*/
232 }
233
234 void MainWindow::saveOptions()
235 {
236   KSharedConfigPtr config = KGlobal::config ();
237   m_fileOpenRecent->saveEntries(KConfigGroup (config, "Recent Files"));
238   config->sync(); 
239 }
240
241 void MainWindow::readOptions()
242 {
243   KSharedConfigPtr config = KGlobal::config ();
244   m_fileOpenRecent->loadEntries(KConfigGroup (config, "Recent Files"));
245 }
246  
247 void MainWindow::newFile()
248 {
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);
254 }
255
256 void MainWindow::activateDocument()
257 {
258   TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
259   KdenliveDoc *currentDoc = currentTab->document();
260   connectDocument(currentTab, currentDoc);
261 }
262  
263 void MainWindow::saveFileAs(const QString &outputFileName)
264 {
265   KSaveFile file(outputFileName);
266   file.open();
267   
268   QByteArray outputByteArray;
269   //outputByteArray.append(textArea->toPlainText());
270   file.write(outputByteArray);
271   file.finalize();
272   file.close();
273   
274   fileName = outputFileName;
275 }
276
277 void MainWindow::saveFileAs()
278 {
279   saveFileAs(KFileDialog::getSaveFileName());
280 }
281  
282 void MainWindow::saveFile()
283 {
284   if(!fileName.isEmpty())
285   {
286     saveFileAs(fileName);
287   }
288   else
289   {
290     saveFileAs();
291   }
292 }
293  
294 void MainWindow::openFile() //changed
295 {
296     KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
297     if (url.isEmpty()) return;
298     m_fileOpenRecent->addUrl (url);
299     openFile(url);
300 }
301  
302 void MainWindow::openFile(const KUrl &url) //new
303 {
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);
309 }
310
311 void MainWindow::slotUpdateMousePosition(int pos)
312 {
313   if (m_activeDocument)
314     switch(m_timecodeFormat->currentIndex()) {
315       case 0:
316         statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
317         break;
318     default:
319       statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
320     }
321 }
322
323 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) //changed
324 {
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);
331   }
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 *)));
336   connect(doc, SIGNAL(signalDeleteProjectClip(int)), m_projectList, SLOT(slotDeleteClip(int)));
337   connect(doc, SIGNAL(updateClipDisplay(int)), m_projectList, SLOT(slotUpdateClip(int)));
338   connect(doc, SIGNAL(deletTimelineClip(int)), trackView, SLOT(slotDeleteClip(int)));
339
340   m_projectList->setDocument(doc);
341   m_monitorManager->setTimecode(doc->timecode());
342   doc->setRenderer(m_projectMonitor->render);
343   //m_undoView->setStack(0);
344   m_commandStack = doc->commandStack();
345
346   m_overView->setScene(trackView->projectScene());
347   m_overView->scale(m_overView->width() / trackView->duration(), m_overView->height() / (50 * trackView->tracksNumber()));
348   //m_overView->fitInView(m_overView->itemAt(0, 50), Qt::KeepAspectRatio);
349   QAction *redo = m_commandStack->createRedoAction(actionCollection());
350   QAction *undo = m_commandStack->createUndoAction(actionCollection());
351
352   QWidget* w = factory()->container("mainToolBar", this);
353   if(w) {
354     if (actionCollection()->action("undo"))
355       delete actionCollection()->action("undo");
356     if(actionCollection()->action("redo"))
357       delete actionCollection()->action("redo");
358
359     actionCollection()->addAction("undo", undo);
360     actionCollection()->addAction("redo", redo);
361     w->addAction(undo);
362     w->addAction(redo);
363   }
364   m_undoView->setStack(doc->commandStack());
365   m_activeDocument = doc;
366 }
367
368
369 void MainWindow::slotPreferences()
370 {
371   //An instance of your dialog could be already created and could be
372   // cached, in which case you want to display the cached dialog
373   // instead of creating another one
374   if ( KConfigDialog::showDialog( "settings" ) )
375     return;
376
377   // KConfigDialog didn't find an instance of this dialog, so lets
378   // create it :
379   KConfigDialog* dialog = new KConfigDialog(this, "settings",
380                                           KdenliveSettings::self());
381
382   QWidget *page1 = new QWidget;
383   Ui::ConfigMisc_UI* confWdg = new Ui::ConfigMisc_UI( );
384   confWdg->setupUi(page1);
385
386   dialog->addPage( page1, i18n("Misc"), "misc" );
387
388   //User edited the configuration - update your local copies of the
389   //configuration data
390   connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
391
392   dialog->show();
393 }
394
395 #include "mainwindow.moc"