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