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