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