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