]> git.sesse.net Git - kdenlive/blob - src/mainwindow.cpp
undo/redo clip move
[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
40 #include <mlt++/Mlt.h>
41
42 #include "mainwindow.h"
43 #include "kdenlivesettings.h"
44 #include "ui_configmisc_ui.h"
45  
46 MainWindow::MainWindow(QWidget *parent)
47     : KXmlGuiWindow(parent),
48       fileName(QString()), m_activeDocument(NULL), m_commandStack(NULL)
49 {
50   m_timelineArea = new KTabWidget(this);
51   m_timelineArea->setHoverCloseButton(true);
52   m_timelineArea->setTabReorderingEnabled(true);
53   connect(m_timelineArea, SIGNAL(currentChanged (int)), this, SLOT(activateDocument()));
54
55   m_monitorManager = new MonitorManager();
56
57   projectListDock = new QDockWidget(i18n("Project Tree"), this);
58   projectListDock->setObjectName("project_tree");
59   m_projectList = new ProjectList(this);
60   projectListDock->setWidget(m_projectList);
61   addDockWidget(Qt::TopDockWidgetArea, projectListDock);
62
63   effectListDock = new QDockWidget(i18n("Effect List"), this);
64   effectListDock->setObjectName("effect_list");
65   effectList = new KListWidget(this);
66   effectListDock->setWidget(effectList);
67   addDockWidget(Qt::TopDockWidgetArea, effectListDock);
68   
69   effectStackDock = new QDockWidget(i18n("Effect Stack"), this);
70   effectStackDock->setObjectName("effect_stack");
71   effectStack = new KListWidget(this);
72   effectStackDock->setWidget(effectStack);
73   addDockWidget(Qt::TopDockWidgetArea, effectStackDock);
74   
75   transitionConfigDock = new QDockWidget(i18n("Transition"), this);
76   transitionConfigDock->setObjectName("transition");
77   transitionConfig = new KListWidget(this);
78   transitionConfigDock->setWidget(transitionConfig);
79   addDockWidget(Qt::TopDockWidgetArea, transitionConfigDock);
80
81   Mlt::Factory::init(NULL);
82
83   clipMonitorDock = new QDockWidget(i18n("Clip Monitor"), this);
84   clipMonitorDock->setObjectName("clip_monitor");
85   m_clipMonitor = new Monitor("clip", m_monitorManager, this);
86   clipMonitorDock->setWidget(m_clipMonitor);
87   addDockWidget(Qt::TopDockWidgetArea, clipMonitorDock);
88   
89   projectMonitorDock = new QDockWidget(i18n("Project Monitor"), this);
90   projectMonitorDock->setObjectName("project_monitor");
91   m_projectMonitor = new Monitor("project", m_monitorManager, this);
92   projectMonitorDock->setWidget(m_projectMonitor);
93   addDockWidget(Qt::TopDockWidgetArea, projectMonitorDock);
94
95   undoViewDock = new QDockWidget(i18n("Undo History"), this);
96   undoViewDock->setObjectName("undo_history");
97   m_undoView = new QUndoView(this);
98   undoViewDock->setWidget(m_undoView);
99   m_undoView->setStack(m_commandStack);
100   addDockWidget(Qt::TopDockWidgetArea, undoViewDock);
101
102   overviewDock = new QDockWidget(i18n("Project Overview"), this);
103   overviewDock->setObjectName("project_overview");
104   m_overView = new CustomTrackView(NULL, NULL, this);
105   overviewDock->setWidget(m_overView);
106   addDockWidget(Qt::TopDockWidgetArea, overviewDock);
107
108   setupActions();
109   tabifyDockWidget (projectListDock, effectListDock);
110   tabifyDockWidget (projectListDock, effectStackDock);
111   tabifyDockWidget (projectListDock, transitionConfigDock);
112   tabifyDockWidget (projectListDock, undoViewDock);
113   projectListDock->raise();
114
115   tabifyDockWidget (clipMonitorDock, projectMonitorDock);
116   setCentralWidget(m_timelineArea);
117   setupGUI(Default, "kdenliveui.rc");
118
119   connect(projectMonitorDock, SIGNAL(visibilityChanged (bool)), m_projectMonitor, SLOT(refreshMonitor(bool)));
120   connect(clipMonitorDock, SIGNAL(visibilityChanged (bool)), m_clipMonitor, SLOT(refreshMonitor(bool)));
121   connect(m_monitorManager, SIGNAL(connectMonitors ()), this, SLOT(slotConnectMonitors()));
122   connect(m_monitorManager, SIGNAL(raiseClipMonitor (bool)), this, SLOT(slotRaiseMonitor(bool)));
123   m_monitorManager->initMonitors(m_clipMonitor, m_projectMonitor);
124
125   setAutoSaveSettings();
126   newFile();
127 }
128
129 //virtual
130 bool MainWindow::queryClose() 
131 {
132   saveOptions();
133   switch ( KMessageBox::warningYesNoCancel( this, i18n("Save changes to document ?")) ) {
134        case KMessageBox::Yes :
135          // save document here. If saving fails, return false;
136          return true;
137        case KMessageBox::No :
138          return true;
139        default: // cancel
140          return false;
141   }
142 }
143
144 void MainWindow::slotRaiseMonitor(bool clipMonitor)
145 {
146   if (clipMonitor) clipMonitorDock->raise();
147   else projectMonitorDock->raise();
148 }
149
150 void MainWindow::slotConnectMonitors()
151 {
152
153   m_projectList->setRenderer(m_clipMonitor->render);
154
155   connect(m_projectList, SIGNAL(clipSelected(const QDomElement &)), m_clipMonitor, SLOT(slotSetXml(const QDomElement &)));
156
157   connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
158
159   connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
160
161   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 > &)));
162
163 }
164
165 void MainWindow::setupActions()
166 {
167   KAction* clearAction = new KAction(this);
168   clearAction->setText(i18n("Clear"));
169   clearAction->setIcon(KIcon("document-new"));
170   clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
171   actionCollection()->addAction("clear", clearAction);
172   /*connect(clearAction, SIGNAL(triggered(bool)),
173           textArea, SLOT(clear()));*/
174  
175   KStandardAction::quit(kapp, SLOT(quit()),
176                         actionCollection());
177  
178   KStandardAction::open(this, SLOT(openFile()),
179                         actionCollection());
180
181   m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
182                         actionCollection());
183
184   KStandardAction::save(this, SLOT(saveFile()),
185                         actionCollection());
186  
187   KStandardAction::saveAs(this, SLOT(saveFileAs()),
188                         actionCollection());
189  
190   KStandardAction::openNew(this, SLOT(newFile()),
191                         actionCollection());
192
193   /*KStandardAction::undo(this, SLOT(undo()),
194                         actionCollection());
195
196   KStandardAction::redo(this, SLOT(redo()),
197                         actionCollection());*/
198
199   readOptions();  
200
201   /*m_redo = m_commandStack->createRedoAction(actionCollection());
202   m_undo = m_commandStack->createUndoAction(actionCollection());*/
203 }
204
205 void MainWindow::saveOptions()
206 {
207   KSharedConfigPtr config = KGlobal::config ();
208   m_fileOpenRecent->saveEntries(KConfigGroup (config, "Recent Files"));
209   config->sync(); 
210 }
211
212 void MainWindow::readOptions()
213 {
214   KSharedConfigPtr config = KGlobal::config ();
215   m_fileOpenRecent->loadEntries(KConfigGroup (config, "Recent Files"));
216 }
217  
218 void MainWindow::newFile()
219 {
220   KdenliveDoc *doc = new KdenliveDoc(KUrl(), 25, 720, 576);
221   TrackView *trackView = new TrackView(doc);
222   m_timelineArea->addTab(trackView, "New Project");
223   if (m_timelineArea->count() == 1)
224     connectDocument(trackView, doc);
225 }
226
227 void MainWindow::activateDocument()
228 {
229   TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
230   KdenliveDoc *currentDoc = currentTab->document();
231   connectDocument(currentTab, currentDoc);
232 }
233  
234 void MainWindow::saveFileAs(const QString &outputFileName)
235 {
236   KSaveFile file(outputFileName);
237   file.open();
238   
239   QByteArray outputByteArray;
240   //outputByteArray.append(textArea->toPlainText());
241   file.write(outputByteArray);
242   file.finalize();
243   file.close();
244   
245   fileName = outputFileName;
246 }
247
248 void MainWindow::saveFileAs()
249 {
250   saveFileAs(KFileDialog::getSaveFileName());
251 }
252  
253 void MainWindow::saveFile()
254 {
255   if(!fileName.isEmpty())
256   {
257     saveFileAs(fileName);
258   }
259   else
260   {
261     saveFileAs();
262   }
263 }
264  
265 void MainWindow::openFile() //changed
266 {
267     KUrl url = KFileDialog::getOpenUrl(KUrl(), "application/vnd.kde.kdenlive;*.kdenlive");
268     if (url.isEmpty()) return;
269     m_fileOpenRecent->addUrl (url);
270     openFile(url);
271 }
272  
273 void MainWindow::openFile(const KUrl &url) //new
274 {
275   KdenliveDoc *doc = new KdenliveDoc(url, 25, 720, 576);
276   TrackView *trackView = new TrackView(doc);
277   m_timelineArea->setCurrentIndex(m_timelineArea->addTab(trackView, QIcon(), doc->documentName()));
278   m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), doc->url().path());
279   //connectDocument(trackView, doc);
280 }
281
282 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) //changed
283 {
284   //m_projectMonitor->stop();
285   if (m_activeDocument) {
286     if (m_activeDocument == doc) return;
287     m_activeDocument->setProducers(m_projectList->producersList());
288     m_activeDocument->setRenderer(NULL);
289   }
290   m_projectList->setDocument(doc);
291   m_monitorManager->setTimecode(doc->timecode());
292   doc->setRenderer(m_projectMonitor->render);
293   //m_undoView->setStack(0);
294   m_commandStack = doc->commandStack();
295
296   m_overView->setScene(trackView->projectScene());
297   m_overView->scale(m_overView->width() / trackView->duration(), m_overView->height() / (50 * trackView->tracksNumber()));
298   //m_overView->fitInView(m_overView->itemAt(0, 50), Qt::KeepAspectRatio);
299   QAction *redo = m_commandStack->createRedoAction(actionCollection());
300   QAction *undo = m_commandStack->createUndoAction(actionCollection());
301
302   QWidget* w = factory()->container("mainToolBar", this);
303   if(w) {
304     if (actionCollection()->action("undo"))
305       delete actionCollection()->action("undo");
306     if(actionCollection()->action("redo"))
307       delete actionCollection()->action("redo");
308
309     actionCollection()->addAction("undo", undo);
310     actionCollection()->addAction("redo", redo);
311     w->addAction(undo);
312     w->addAction(redo);
313   }
314   m_undoView->setStack(doc->commandStack());
315   m_activeDocument = doc;
316 }
317
318
319 void MainWindow::slotPreferences()
320 {
321   //An instance of your dialog could be already created and could be
322   // cached, in which case you want to display the cached dialog
323   // instead of creating another one
324   if ( KConfigDialog::showDialog( "settings" ) )
325     return;
326
327   // KConfigDialog didn't find an instance of this dialog, so lets
328   // create it :
329   KConfigDialog* dialog = new KConfigDialog(this, "settings",
330                                           KdenliveSettings::self());
331
332   QWidget *page1 = new QWidget;
333   Ui::ConfigMisc_UI* confWdg = new Ui::ConfigMisc_UI( );
334   confWdg->setupUi(page1);
335
336   dialog->addPage( page1, i18n("Misc"), "misc" );
337
338   //User edited the configuration - update your local copies of the
339   //configuration data
340   connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateConfiguration()) );
341
342   dialog->show();
343 }
344
345 #include "mainwindow.moc"