]> git.sesse.net Git - kdenlive/blob - src/mainwindow.cpp
More efficient usage of MLT producers
[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 #include <QtTest>
26 #include <QtCore>
27 #include <QKeyEvent>
28
29 #include <KApplication>
30 #include <KAction>
31 #include <KLocale>
32 #include <KGlobal>
33 #include <KActionCollection>
34 #include <KStandardAction>
35 #include <KFileDialog>
36 #include <KMessageBox>
37 #include <KDebug>
38 #include <KIO/NetAccess>
39 #include <KSaveFile>
40 #include <KRuler>
41 #include <KConfigDialog>
42 #include <KXMLGUIFactory>
43 #include <KStatusBar>
44 #include <kstandarddirs.h>
45 #include <KUrlRequesterDialog>
46 #include <KTemporaryFile>
47 #include <KActionMenu>
48 #include <KMenu>
49 #include <locale.h>
50 #include <ktogglefullscreenaction.h>
51 #include <KFileItem>
52
53 #include <mlt++/Mlt.h>
54
55 #include "mainwindow.h"
56 #include "kdenlivesettings.h"
57 #include "kdenlivesettingsdialog.h"
58 #include "initeffects.h"
59 #include "profilesdialog.h"
60 #include "projectsettings.h"
61 #include "events.h"
62 #include "renderjob.h"
63 #include "clipmanager.h"
64 #include "projectlist.h"
65 #include "monitor.h"
66 #include "recmonitor.h"
67 #include "monitormanager.h"
68 #include "kdenlivedoc.h"
69 #include "trackview.h"
70 #include "customtrackview.h"
71 #include "effectslistview.h"
72 #include "effectstackview.h"
73 #include "transitionsettings.h"
74 #include "renderwidget.h"
75 #include "renderer.h"
76 #include "jogshuttle.h"
77 #include "clipproperties.h"
78
79 #define ID_STATUS_MSG 1
80 #define ID_EDITMODE_MSG 2
81 #define ID_TIMELINE_MSG 3
82 #define ID_TIMELINE_BUTTONS 5
83 #define ID_TIMELINE_POS 6
84 #define ID_TIMELINE_FORMAT 7
85
86 namespace Mlt {
87 class Producer;
88 };
89
90 EffectsList MainWindow::videoEffects;
91 EffectsList MainWindow::audioEffects;
92 EffectsList MainWindow::customEffects;
93 EffectsList MainWindow::transitions;
94
95 MainWindow::MainWindow(QWidget *parent)
96         : KXmlGuiWindow(parent),
97         m_activeDocument(NULL), m_activeTimeline(NULL), m_renderWidget(NULL), m_jogProcess(NULL), m_findActivated(false), m_initialized(false) {
98     setlocale(LC_NUMERIC, "POSIX");
99     parseProfiles();
100     setFont(KGlobalSettings::toolBarFont());
101     m_commandStack = new QUndoGroup;
102     m_timelineArea = new KTabWidget(this);
103     m_timelineArea->setTabReorderingEnabled(true);
104     m_timelineArea->setTabBarHidden(true);
105
106     QToolButton *closeTabButton = new QToolButton;
107     connect(closeTabButton, SIGNAL(clicked()), this, SLOT(slotRemoveTab()));
108     closeTabButton->setIcon(KIcon("tab-close"));
109     closeTabButton->adjustSize();
110     closeTabButton->setToolTip(i18n("Close the current tab"));
111     m_timelineArea->setCornerWidget(closeTabButton);
112     connect(m_timelineArea, SIGNAL(currentChanged(int)), this, SLOT(activateDocument()));
113
114     connect(&m_findTimer, SIGNAL(timeout()), this, SLOT(findTimeout()));
115     m_findTimer.setSingleShot(true);
116
117     initEffects::parseEffectFiles();
118     m_monitorManager = new MonitorManager();
119
120     projectListDock = new QDockWidget(i18n("Project Tree"), this);
121     projectListDock->setObjectName("project_tree");
122     m_projectList = new ProjectList(this);
123     projectListDock->setWidget(m_projectList);
124     addDockWidget(Qt::TopDockWidgetArea, projectListDock);
125
126     effectListDock = new QDockWidget(i18n("Effect List"), this);
127     effectListDock->setObjectName("effect_list");
128     m_effectList = new EffectsListView();
129
130     //m_effectList = new KListWidget(this);
131     effectListDock->setWidget(m_effectList);
132     addDockWidget(Qt::TopDockWidgetArea, effectListDock);
133
134     effectStackDock = new QDockWidget(i18n("Effect Stack"), this);
135     effectStackDock->setObjectName("effect_stack");
136     effectStack = new EffectStackView(this);
137     effectStackDock->setWidget(effectStack);
138     addDockWidget(Qt::TopDockWidgetArea, effectStackDock);
139
140     transitionConfigDock = new QDockWidget(i18n("Transition"), this);
141     transitionConfigDock->setObjectName("transition");
142     transitionConfig = new TransitionSettings(this);
143     transitionConfigDock->setWidget(transitionConfig);
144     addDockWidget(Qt::TopDockWidgetArea, transitionConfigDock);
145
146
147     clipMonitorDock = new QDockWidget(i18n("Clip Monitor"), this);
148     clipMonitorDock->setObjectName("clip_monitor");
149     m_clipMonitor = new Monitor("clip", m_monitorManager, this);
150     clipMonitorDock->setWidget(m_clipMonitor);
151     addDockWidget(Qt::TopDockWidgetArea, clipMonitorDock);
152     //m_clipMonitor->stop();
153
154     projectMonitorDock = new QDockWidget(i18n("Project Monitor"), this);
155     projectMonitorDock->setObjectName("project_monitor");
156     m_projectMonitor = new Monitor("project", m_monitorManager, this);
157     projectMonitorDock->setWidget(m_projectMonitor);
158     addDockWidget(Qt::TopDockWidgetArea, projectMonitorDock);
159
160     recMonitorDock = new QDockWidget(i18n("Record Monitor"), this);
161     recMonitorDock->setObjectName("record_monitor");
162     m_recMonitor = new RecMonitor("record", this);
163     recMonitorDock->setWidget(m_recMonitor);
164     addDockWidget(Qt::TopDockWidgetArea, recMonitorDock);
165
166     connect(m_recMonitor, SIGNAL(addProjectClip(KUrl)), this, SLOT(slotAddProjectClip(KUrl)));
167
168     undoViewDock = new QDockWidget(i18n("Undo History"), this);
169     undoViewDock->setObjectName("undo_history");
170     m_undoView = new QUndoView(this);
171     m_undoView->setCleanIcon(KIcon("edit-clear"));
172     m_undoView->setEmptyLabel(i18n("Clean"));
173     undoViewDock->setWidget(m_undoView);
174     m_undoView->setGroup(m_commandStack);
175     addDockWidget(Qt::TopDockWidgetArea, undoViewDock);
176
177     //overviewDock = new QDockWidget(i18n("Project Overview"), this);
178     //overviewDock->setObjectName("project_overview");
179     //m_overView = new CustomTrackView(NULL, NULL, this);
180     //overviewDock->setWidget(m_overView);
181     //addDockWidget(Qt::TopDockWidgetArea, overviewDock);
182
183     setupActions();
184     //tabifyDockWidget(projectListDock, effectListDock);
185     tabifyDockWidget(projectListDock, effectStackDock);
186     tabifyDockWidget(projectListDock, transitionConfigDock);
187     //tabifyDockWidget(projectListDock, undoViewDock);
188     projectListDock->raise();
189
190     tabifyDockWidget(clipMonitorDock, projectMonitorDock);
191     tabifyDockWidget(clipMonitorDock, recMonitorDock);
192     setCentralWidget(m_timelineArea);
193
194     setupGUI(Default, NULL /*"kdenliveui.rc"*/);
195     kDebug() << factory() << " " << factory()->container("video_effects_menu", this);
196
197     // build effects menus
198     QAction *action;
199     QMenu *videoEffectsMenu = static_cast<QMenu*>(factory()->container("video_effects_menu", this));
200     QStringList effects = videoEffects.effectNames();
201     foreach(const QString &name, effects) {
202         action = new QAction(name, this);
203         action->setData(name);
204         videoEffectsMenu->addAction(action);
205     }
206     QMenu *audioEffectsMenu = static_cast<QMenu*>(factory()->container("audio_effects_menu", this));
207     effects = audioEffects.effectNames();
208     foreach(const QString &name, effects) {
209         action = new QAction(name, this);
210         action->setData(name);
211         audioEffectsMenu->addAction(action);
212     }
213     QMenu *customEffectsMenu = static_cast<QMenu*>(factory()->container("custom_effects_menu", this));
214     effects = customEffects.effectNames();
215     foreach(const QString &name, effects) {
216         action = new QAction(name, this);
217         action->setData(name);
218         customEffectsMenu->addAction(action);
219     }
220
221     QMenu *viewMenu = static_cast<QMenu*>(factory()->container("dockwindows", this));
222     const QList<QAction *> viewActions = createPopupMenu()->actions();
223     viewMenu->insertActions(NULL, viewActions);
224
225     connect(videoEffectsMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotAddVideoEffect(QAction *)));
226     connect(audioEffectsMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotAddAudioEffect(QAction *)));
227     connect(customEffectsMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotAddCustomEffect(QAction *)));
228
229     m_timelineContextMenu = new QMenu(this);
230     m_timelineContextClipMenu = new QMenu(this);
231     m_timelineContextTransitionMenu = new QMenu(this);
232
233
234     QMenu *transitionsMenu = new QMenu(i18n("Add Transition"), this);
235     effects = transitions.effectNames();
236     foreach(const QString &name, effects) {
237         action = new QAction(name, this);
238         action->setData(name);
239         transitionsMenu->addAction(action);
240     }
241     connect(transitionsMenu, SIGNAL(triggered(QAction *)), this, SLOT(slotAddTransition(QAction *)));
242
243     m_timelineContextMenu->addAction(actionCollection()->action(KStandardAction::name(KStandardAction::Paste)));
244
245     m_timelineContextClipMenu->addAction(actionCollection()->action("delete_timeline_clip"));
246     m_timelineContextClipMenu->addAction(actionCollection()->action("cut_timeline_clip"));
247     m_timelineContextClipMenu->addAction(actionCollection()->action(KStandardAction::name(KStandardAction::Copy)));
248
249     QMenu *markersMenu = (QMenu*)(factory()->container("marker_menu", this));
250     m_timelineContextClipMenu->addMenu(markersMenu);
251     m_timelineContextClipMenu->addMenu(transitionsMenu);
252     m_timelineContextClipMenu->addMenu(videoEffectsMenu);
253     m_timelineContextClipMenu->addMenu(audioEffectsMenu);
254     m_timelineContextClipMenu->addMenu(customEffectsMenu);
255
256     m_timelineContextTransitionMenu->addAction(actionCollection()->action("delete_timeline_clip"));
257     m_timelineContextTransitionMenu->addAction(actionCollection()->action(KStandardAction::name(KStandardAction::Copy)));
258
259     connect(projectMonitorDock, SIGNAL(visibilityChanged(bool)), m_projectMonitor, SLOT(refreshMonitor(bool)));
260     connect(clipMonitorDock, SIGNAL(visibilityChanged(bool)), m_clipMonitor, SLOT(refreshMonitor(bool)));
261     //connect(m_monitorManager, SIGNAL(connectMonitors()), this, SLOT(slotConnectMonitors()));
262     connect(m_monitorManager, SIGNAL(raiseClipMonitor(bool)), this, SLOT(slotRaiseMonitor(bool)));
263     connect(m_effectList, SIGNAL(addEffect(QDomElement)), this, SLOT(slotAddEffect(QDomElement)));
264     m_monitorManager->initMonitors(m_clipMonitor, m_projectMonitor);
265     slotConnectMonitors();
266
267     setAutoSaveSettings();
268
269     if (KdenliveSettings::openlastproject()) {
270         openLastFile();
271     } else newFile();
272
273     activateShuttleDevice();
274 }
275
276 void MainWindow::queryQuit() {
277     kDebug() << "----- SAVING CONFUIG";
278     if (queryClose()) kapp->quit();
279 }
280
281 //virtual
282 bool MainWindow::queryClose() {
283     saveOptions();
284     if (m_activeDocument && m_activeDocument->isModified()) {
285         switch (KMessageBox::warningYesNoCancel(this, i18n("Save changes to document ?"))) {
286         case KMessageBox::Yes :
287             // save document here. If saving fails, return false;
288             saveFile();
289             return true;
290         case KMessageBox::No :
291             return true;
292         default: // cancel
293             return false;
294         }
295     }
296     return true;
297 }
298
299 void MainWindow::saveProperties(KConfig*) {
300     // save properties here,used by session management
301     saveFile();
302 }
303
304
305 void MainWindow::readProperties(KConfig *config) {
306     // read properties here,used by session management
307     QString Lastproject = config->group("Recent Files").readPathEntry("File1", QString());
308     openFile(KUrl(Lastproject));
309 }
310
311 void MainWindow::activateShuttleDevice() {
312     if (m_jogProcess) delete m_jogProcess;
313     m_jogProcess = NULL;
314     if (KdenliveSettings::enableshuttle() == false) return;
315     m_jogProcess = new JogShuttle(KdenliveSettings::shuttledevice());
316     connect(m_jogProcess, SIGNAL(rewind1()), m_monitorManager, SLOT(slotRewindOneFrame()));
317     connect(m_jogProcess, SIGNAL(forward1()), m_monitorManager, SLOT(slotForwardOneFrame()));
318     connect(m_jogProcess, SIGNAL(rewind(double)), m_monitorManager, SLOT(slotRewind(double)));
319     connect(m_jogProcess, SIGNAL(forward(double)), m_monitorManager, SLOT(slotForward(double)));
320     connect(m_jogProcess, SIGNAL(stop()), m_monitorManager, SLOT(slotPlay()));
321     connect(m_jogProcess, SIGNAL(button(int)), this, SLOT(slotShuttleButton(int)));
322 }
323
324 void MainWindow::slotShuttleButton(int code) {
325     switch (code) {
326     case 5:
327         slotShuttleAction(KdenliveSettings::shuttle1());
328         break;
329     case 6:
330         slotShuttleAction(KdenliveSettings::shuttle2());
331         break;
332     case 7:
333         slotShuttleAction(KdenliveSettings::shuttle3());
334         break;
335     case 8:
336         slotShuttleAction(KdenliveSettings::shuttle4());
337         break;
338     case 9:
339         slotShuttleAction(KdenliveSettings::shuttle5());
340         break;
341     }
342 }
343
344 void MainWindow::slotShuttleAction(int code) {
345     switch (code) {
346     case 0:
347         return;
348     case 1:
349         m_monitorManager->slotPlay();
350         break;
351     default:
352         m_monitorManager->slotPlay();
353         break;
354     }
355 }
356
357 void MainWindow::slotFullScreen() {
358     //KToggleFullScreenAction::setFullScreen(this, actionCollection()->action("fullscreen")->isChecked());
359 }
360
361 void MainWindow::slotAddEffect(QDomElement effect, GenTime pos, int track) {
362     if (!m_activeDocument) return;
363     if (effect.isNull()) {
364         kDebug() << "--- ERROR, TRYING TO APPEND NULL EFFECT";
365         return;
366     }
367     TrackView *currentTimeLine = (TrackView *) m_timelineArea->currentWidget();
368     currentTimeLine->projectView()->slotAddEffect(effect, pos, track);
369 }
370
371 void MainWindow::slotRaiseMonitor(bool clipMonitor) {
372     if (clipMonitor) clipMonitorDock->raise();
373     else projectMonitorDock->raise();
374 }
375
376 void MainWindow::slotSetClipDuration(int id, int duration) {
377     if (!m_activeDocument) return;
378     m_activeDocument->setProducerDuration(id, duration);
379 }
380
381 void MainWindow::slotConnectMonitors() {
382
383     m_projectList->setRenderer(m_clipMonitor->render);
384     connect(m_projectList, SIGNAL(receivedClipDuration(int, int)), this, SLOT(slotSetClipDuration(int, int)));
385     connect(m_projectList, SIGNAL(showClipProperties(DocClipBase *)), this, SLOT(slotShowClipProperties(DocClipBase *)));
386     connect(m_projectList, SIGNAL(getFileProperties(const QDomElement &, int)), m_clipMonitor->render, SLOT(getFileProperties(const QDomElement &, int)));
387     connect(m_clipMonitor->render, SIGNAL(replyGetImage(int, int, const QPixmap &, int, int)), m_projectList, SLOT(slotReplyGetImage(int, int, const QPixmap &, int, int)));
388     connect(m_clipMonitor->render, SIGNAL(replyGetFileProperties(int, Mlt::Producer*, const QMap < QString, QString > &, const QMap < QString, QString > &)), m_projectList, SLOT(slotReplyGetFileProperties(int, Mlt::Producer*, const QMap < QString, QString > &, const QMap < QString, QString > &)));
389     connect(m_clipMonitor, SIGNAL(refreshClipThumbnail(int)), m_projectList, SLOT(slotRefreshClipThumbnail(int)));
390 }
391
392 void MainWindow::setupActions() {
393
394     m_timecodeFormat = new KComboBox(this);
395     m_timecodeFormat->addItem(i18n("hh:mm:ss::ff"));
396     m_timecodeFormat->addItem(i18n("Frames"));
397
398     statusProgressBar = new QProgressBar(this);
399     statusProgressBar->setMinimum(0);
400     statusProgressBar->setMaximum(100);
401     statusProgressBar->setMaximumWidth(150);
402     statusProgressBar->setVisible(false);
403
404     QWidget *w = new QWidget;
405
406     QHBoxLayout *layout = new QHBoxLayout;
407     w->setLayout(layout);
408     layout->setContentsMargins(5, 0, 5, 0);
409     QToolBar *toolbar = new QToolBar("statusToolBar", this);
410
411
412     m_toolGroup = new QActionGroup(this);
413
414     QString style1 = "QToolButton {background-color: rgba(230, 230, 230, 220); border-style: inset; border:1px solid #999999;border-radius: 3px;margin: 0px 3px;padding: 0px;} QToolButton:checked { background-color: rgba(224, 224, 0, 100); border-style: inset; border:1px solid #cc6666;border-radius: 3px;}";
415
416     m_buttonSelectTool = toolbar->addAction(KIcon("kdenlive-select-tool"), i18n("Selection tool"));
417     m_buttonSelectTool->setCheckable(true);
418     m_buttonSelectTool->setChecked(true);
419
420     m_buttonRazorTool = toolbar->addAction(KIcon("edit-cut"), i18n("Razor tool"));
421     m_buttonRazorTool->setCheckable(true);
422     m_buttonRazorTool->setChecked(false);
423
424     m_toolGroup->addAction(m_buttonSelectTool);
425     m_toolGroup->addAction(m_buttonRazorTool);
426     m_toolGroup->setExclusive(true);
427     toolbar->setToolButtonStyle(Qt::ToolButtonIconOnly);
428
429     QWidget * actionWidget;
430     actionWidget = toolbar->widgetForAction(m_buttonSelectTool);
431     actionWidget->setMaximumWidth(24);
432     actionWidget->setMinimumHeight(17);
433
434     actionWidget = toolbar->widgetForAction(m_buttonRazorTool);
435     actionWidget->setMaximumWidth(24);
436     actionWidget->setMinimumHeight(17);
437
438     toolbar->setStyleSheet(style1);
439     connect(m_toolGroup, SIGNAL(triggered(QAction *)), this, SLOT(slotChangeTool(QAction *)));
440
441     toolbar->addSeparator();
442     m_buttonFitZoom = toolbar->addAction(KIcon("zoom-fit-best"), i18n("Fit zoom to project"));
443     m_buttonFitZoom->setCheckable(false);
444     connect(m_buttonFitZoom, SIGNAL(triggered()), this, SLOT(slotFitZoom()));
445
446     actionWidget = toolbar->widgetForAction(m_buttonFitZoom);
447     actionWidget->setMaximumWidth(24);
448     actionWidget->setMinimumHeight(17);
449
450     m_zoomSlider = new QSlider(Qt::Horizontal, this);
451     m_zoomSlider->setMaximum(13);
452     m_zoomSlider->setPageStep(1);
453
454     m_zoomSlider->setMaximumWidth(150);
455     m_zoomSlider->setMinimumWidth(100);
456
457     const int contentHeight = QFontMetrics(w->font()).height() + 8;
458     QString style = "QSlider::groove:horizontal { background-color: rgba(230, 230, 230, 220);border: 1px solid #999999;height: 8px;border-radius: 3px;margin-top:3px }";
459     style.append("QSlider::handle:horizontal {  background-color: white; border: 1px solid #999999;width: 9px;margin: -2px 0;border-radius: 3px; }");
460     m_zoomSlider->setStyleSheet(style);
461
462     //m_zoomSlider->setMaximumHeight(contentHeight);
463     //m_zoomSlider->height() + 5;
464     statusBar()->setMinimumHeight(contentHeight);
465
466
467     toolbar->addWidget(m_zoomSlider);
468
469     m_buttonVideoThumbs = toolbar->addAction(KIcon("kdenlive-show-videothumb"), i18n("Show video thumbnails"));
470     m_buttonVideoThumbs->setCheckable(true);
471     m_buttonVideoThumbs->setChecked(KdenliveSettings::videothumbnails());
472     connect(m_buttonVideoThumbs, SIGNAL(triggered()), this, SLOT(slotSwitchVideoThumbs()));
473
474     m_buttonAudioThumbs = toolbar->addAction(KIcon("kdenlive-show-audiothumb"), i18n("Show audio thumbnails"));
475     m_buttonAudioThumbs->setCheckable(true);
476     m_buttonAudioThumbs->setChecked(KdenliveSettings::audiothumbnails());
477     connect(m_buttonAudioThumbs, SIGNAL(triggered()), this, SLOT(slotSwitchAudioThumbs()));
478
479     m_buttonShowMarkers = toolbar->addAction(KIcon("kdenlive-show-markers"), i18n("Show markers comments"));
480     m_buttonShowMarkers->setCheckable(true);
481     m_buttonShowMarkers->setChecked(KdenliveSettings::showmarkers());
482     connect(m_buttonShowMarkers, SIGNAL(triggered()), this, SLOT(slotSwitchMarkersComments()));
483
484     m_buttonSnap = toolbar->addAction(KIcon("kdenlive-snap"), i18n("Snap"));
485     m_buttonSnap->setCheckable(true);
486     m_buttonSnap->setChecked(KdenliveSettings::snaptopoints());
487     connect(m_buttonSnap, SIGNAL(triggered()), this, SLOT(slotSwitchSnap()));
488     layout->addWidget(toolbar);
489
490
491     actionWidget = toolbar->widgetForAction(m_buttonVideoThumbs);
492     actionWidget->setMaximumWidth(24);
493     actionWidget->setMinimumHeight(17);
494
495     actionWidget = toolbar->widgetForAction(m_buttonAudioThumbs);
496     actionWidget->setMaximumWidth(24);
497     actionWidget->setMinimumHeight(17);
498
499     actionWidget = toolbar->widgetForAction(m_buttonShowMarkers);
500     actionWidget->setMaximumWidth(24);
501     actionWidget->setMinimumHeight(17);
502
503     actionWidget = toolbar->widgetForAction(m_buttonSnap);
504     actionWidget->setMaximumWidth(24);
505     actionWidget->setMinimumHeight(17);
506
507     m_messageLabel = new StatusBarMessageLabel(this);
508     m_messageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
509
510     statusBar()->addWidget(m_messageLabel, 10);
511     statusBar()->addWidget(statusProgressBar, 0);
512     statusBar()->insertPermanentWidget(ID_TIMELINE_BUTTONS, w);
513     statusBar()->insertPermanentFixedItem("00:00:00:00", ID_TIMELINE_POS);
514     statusBar()->insertPermanentWidget(ID_TIMELINE_FORMAT, m_timecodeFormat);
515     statusBar()->setMaximumHeight(statusBar()->font().pointSize() * 4);
516     m_messageLabel->hide();
517
518     actionCollection()->addAction("select_tool", m_buttonSelectTool);
519     actionCollection()->addAction("razor_tool", m_buttonRazorTool);
520
521     actionCollection()->addAction("show_video_thumbs", m_buttonVideoThumbs);
522     actionCollection()->addAction("show_audio_thumbs", m_buttonAudioThumbs);
523     actionCollection()->addAction("show_markers", m_buttonShowMarkers);
524     actionCollection()->addAction("snap", m_buttonSnap);
525     actionCollection()->addAction("zoom_fit", m_buttonFitZoom);
526
527     m_projectSearch = new KAction(KIcon("edit-find"), i18n("Find"), this);
528     actionCollection()->addAction("project_find", m_projectSearch);
529     connect(m_projectSearch, SIGNAL(triggered(bool)), this, SLOT(slotFind()));
530     m_projectSearch->setShortcut(Qt::Key_Slash);
531
532     m_projectSearchNext = new KAction(KIcon("go-down-search"), i18n("Find Next"), this);
533     actionCollection()->addAction("project_find_next", m_projectSearchNext);
534     connect(m_projectSearchNext, SIGNAL(triggered(bool)), this, SLOT(slotFindNext()));
535     m_projectSearchNext->setShortcut(Qt::Key_F3);
536     m_projectSearchNext->setEnabled(false);
537
538     KAction* profilesAction = new KAction(KIcon("document-new"), i18n("Manage Profiles"), this);
539     actionCollection()->addAction("manage_profiles", profilesAction);
540     connect(profilesAction, SIGNAL(triggered(bool)), this, SLOT(slotEditProfiles()));
541
542     KAction* projectAction = new KAction(KIcon("configure"), i18n("Project Settings"), this);
543     actionCollection()->addAction("project_settings", projectAction);
544     connect(projectAction, SIGNAL(triggered(bool)), this, SLOT(slotEditProjectSettings()));
545
546     KAction* projectRender = new KAction(KIcon("media-record"), i18n("Render"), this);
547     actionCollection()->addAction("project_render", projectRender);
548     connect(projectRender, SIGNAL(triggered(bool)), this, SLOT(slotRenderProject()));
549
550     KAction* monitorPlay = new KAction(KIcon("media-playback-start"), i18n("Play"), this);
551     monitorPlay->setShortcut(Qt::Key_Space);
552     actionCollection()->addAction("monitor_play", monitorPlay);
553     connect(monitorPlay, SIGNAL(triggered(bool)), m_monitorManager, SLOT(slotPlay()));
554
555     KAction* monitorSeekBackward = new KAction(KIcon("media-seek-backward"), i18n("Rewind"), this);
556     monitorSeekBackward->setShortcut(Qt::Key_J);
557     actionCollection()->addAction("monitor_seek_backward", monitorSeekBackward);
558     connect(monitorSeekBackward, SIGNAL(triggered(bool)), m_monitorManager, SLOT(slotRewind()));
559
560     KAction* monitorSeekBackwardOneFrame = new KAction(KIcon("media-skip-backward"), i18n("Rewind 1 Frame"), this);
561     monitorSeekBackwardOneFrame->setShortcut(Qt::Key_Left);
562     actionCollection()->addAction("monitor_seek_backward-one-frame", monitorSeekBackwardOneFrame);
563     connect(monitorSeekBackwardOneFrame, SIGNAL(triggered(bool)), m_monitorManager, SLOT(slotRewindOneFrame()));
564
565     KAction* monitorSeekSnapBackward = new KAction(KIcon("media-seek-backward"), i18n("Go to Previous Snap Point"), this);
566     monitorSeekSnapBackward->setShortcut(Qt::ALT + Qt::Key_Left);
567     actionCollection()->addAction("monitor_seek_snap_backward", monitorSeekSnapBackward);
568     connect(monitorSeekSnapBackward, SIGNAL(triggered(bool)), this, SLOT(slotSnapRewind()));
569
570     KAction* monitorSeekForward = new KAction(KIcon("media-seek-forward"), i18n("Forward"), this);
571     monitorSeekForward->setShortcut(Qt::Key_L);
572     actionCollection()->addAction("monitor_seek_forward", monitorSeekForward);
573     connect(monitorSeekForward, SIGNAL(triggered(bool)), m_monitorManager, SLOT(slotForward()));
574
575     KAction* clipStart = new KAction(KIcon("media-seek-backward"), i18n("Go to Clip Start"), this);
576     clipStart->setShortcut(Qt::Key_Home);
577     actionCollection()->addAction("seek_clip_start", clipStart);
578     connect(clipStart, SIGNAL(triggered(bool)), this, SLOT(slotClipStart()));
579
580     KAction* clipEnd = new KAction(KIcon("media-seek-forward"), i18n("Go to Clip End"), this);
581     clipEnd->setShortcut(Qt::Key_End);
582     actionCollection()->addAction("seek_clip_end", clipEnd);
583     connect(clipEnd, SIGNAL(triggered(bool)), this, SLOT(slotClipEnd()));
584
585     KAction* projectStart = new KAction(KIcon("media-seek-backward"), i18n("Go to Project Start"), this);
586     projectStart->setShortcut(Qt::CTRL + Qt::Key_Home);
587     actionCollection()->addAction("seek_start", projectStart);
588     connect(projectStart, SIGNAL(triggered(bool)), m_monitorManager, SLOT(slotStart()));
589
590     KAction* projectEnd = new KAction(KIcon("media-seek-forward"), i18n("Go to Project End"), this);
591     projectEnd->setShortcut(Qt::CTRL + Qt::Key_End);
592     actionCollection()->addAction("seek_end", projectEnd);
593     connect(projectEnd, SIGNAL(triggered(bool)), m_monitorManager, SLOT(slotEnd()));
594
595     KAction* monitorSeekForwardOneFrame = new KAction(KIcon("media-skip-forward"), i18n("Forward 1 Frame"), this);
596     monitorSeekForwardOneFrame->setShortcut(Qt::Key_Right);
597     actionCollection()->addAction("monitor_seek_forward-one-frame", monitorSeekForwardOneFrame);
598     connect(monitorSeekForwardOneFrame, SIGNAL(triggered(bool)), m_monitorManager, SLOT(slotForwardOneFrame()));
599
600     KAction* monitorSeekSnapForward = new KAction(KIcon("media-seek-forward"), i18n("Go to Next Snap Point"), this);
601     monitorSeekSnapForward->setShortcut(Qt::ALT + Qt::Key_Right);
602     actionCollection()->addAction("monitor_seek_snap_forward", monitorSeekSnapForward);
603     connect(monitorSeekSnapForward, SIGNAL(triggered(bool)), this, SLOT(slotSnapForward()));
604
605     KAction* deleteTimelineClip = new KAction(KIcon("edit-delete"), i18n("Delete Selected Item"), this);
606     deleteTimelineClip->setShortcut(Qt::Key_Delete);
607     actionCollection()->addAction("delete_timeline_clip", deleteTimelineClip);
608     connect(deleteTimelineClip, SIGNAL(triggered(bool)), this, SLOT(slotDeleteTimelineClip()));
609
610     KAction* cutTimelineClip = new KAction(KIcon("edit-cut"), i18n("Cut Clip"), this);
611     cutTimelineClip->setShortcut(Qt::SHIFT + Qt::Key_R);
612     actionCollection()->addAction("cut_timeline_clip", cutTimelineClip);
613     connect(cutTimelineClip, SIGNAL(triggered(bool)), this, SLOT(slotCutTimelineClip()));
614
615     KAction* addClipMarker = new KAction(KIcon("bookmark-new"), i18n("Add Marker to Clip"), this);
616     actionCollection()->addAction("add_clip_marker", addClipMarker);
617     connect(addClipMarker, SIGNAL(triggered(bool)), this, SLOT(slotAddClipMarker()));
618
619     KAction* deleteClipMarker = new KAction(KIcon("edit-delete"), i18n("Delete Marker from Clip"), this);
620     actionCollection()->addAction("delete_clip_marker", deleteClipMarker);
621     connect(deleteClipMarker, SIGNAL(triggered(bool)), this, SLOT(slotDeleteClipMarker()));
622
623     KAction* deleteAllClipMarkers = new KAction(KIcon("edit-delete"), i18n("Delete All Markers from Clip"), this);
624     actionCollection()->addAction("delete_all_clip_markers", deleteAllClipMarkers);
625     connect(deleteAllClipMarkers, SIGNAL(triggered(bool)), this, SLOT(slotDeleteAllClipMarkers()));
626
627     KAction* editClipMarker = new KAction(KIcon("document-properties"), i18n("Edit Marker"), this);
628     actionCollection()->addAction("edit_clip_marker", editClipMarker);
629     connect(editClipMarker, SIGNAL(triggered(bool)), this, SLOT(slotEditClipMarker()));
630
631     KAction *addGuide = new KAction(KIcon("document-new"), i18n("Add Guide"), this);
632     actionCollection()->addAction("add_guide", addGuide);
633     connect(addGuide, SIGNAL(triggered()), this, SLOT(slotAddGuide()));
634
635     QAction *delGuide = new KAction(KIcon("edit-delete"), i18n("Delete Guide"), this);
636     actionCollection()->addAction("delete_guide", delGuide);
637     connect(delGuide, SIGNAL(triggered()), this, SLOT(slotDeleteGuide()));
638
639     QAction *editGuide = new KAction(KIcon("document-properties"), i18n("Edit Guide"), this);
640     actionCollection()->addAction("edit_guide", editGuide);
641     connect(editGuide, SIGNAL(triggered()), this, SLOT(slotEditGuide()));
642
643     QAction *delAllGuides = new KAction(KIcon("edit-delete"), i18n("Delete All Guides"), this);
644     actionCollection()->addAction("delete_all_guides", delAllGuides);
645     connect(delAllGuides, SIGNAL(triggered()), this, SLOT(slotDeleteAllGuides()));
646
647     KStandardAction::quit(this, SLOT(queryQuit()),
648                           actionCollection());
649
650     KStandardAction::open(this, SLOT(openFile()),
651                           actionCollection());
652
653     m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(openFile(const KUrl &)),
654                        actionCollection());
655
656     KStandardAction::save(this, SLOT(saveFile()),
657                           actionCollection());
658
659     KStandardAction::saveAs(this, SLOT(saveFileAs()),
660                             actionCollection());
661
662     KStandardAction::openNew(this, SLOT(newFile()),
663                              actionCollection());
664
665     KStandardAction::preferences(this, SLOT(slotPreferences()),
666                                  actionCollection());
667
668     KStandardAction::copy(this, SLOT(slotCopy()),
669                           actionCollection());
670
671     KStandardAction::paste(this, SLOT(slotPaste()),
672                            actionCollection());
673
674     KStandardAction::undo(this, SLOT(undo()),
675                           actionCollection());
676
677     KStandardAction::redo(this, SLOT(redo()),
678                           actionCollection());
679
680     KStandardAction::fullScreen(this, SLOT(slotFullScreen()), this, actionCollection());
681
682     connect(actionCollection(), SIGNAL(actionHovered(QAction*)),
683             this, SLOT(slotDisplayActionMessage(QAction*)));
684     //connect(actionCollection(), SIGNAL( clearStatusText() ),
685     //statusBar(), SLOT( clear() ) );
686
687     readOptions();
688 }
689
690 void MainWindow::undo() {
691     m_commandStack->undo();
692 }
693
694 void MainWindow::redo() {
695     m_commandStack->redo();
696 }
697
698 void MainWindow::slotDisplayActionMessage(QAction *a) {
699     statusBar()->showMessage(a->data().toString(), 3000);
700 }
701
702 void MainWindow::saveOptions() {
703     KdenliveSettings::self()->writeConfig();
704     KSharedConfigPtr config = KGlobal::config();
705     m_fileOpenRecent->saveEntries(KConfigGroup(config, "Recent Files"));
706     config->sync();
707 }
708
709 void MainWindow::readOptions() {
710     KSharedConfigPtr config = KGlobal::config();
711     m_fileOpenRecent->loadEntries(KConfigGroup(config, "Recent Files"));
712 }
713
714 void MainWindow::newFile() {
715     QString profileName;
716     KUrl projectFolder;
717     if (m_timelineArea->count() == 0) profileName = KdenliveSettings::default_profile();
718     else {
719         ProjectSettings *w = new ProjectSettings;
720         if (w->exec() != QDialog::Accepted) return;
721         profileName = w->selectedProfile();
722         projectFolder = w->selectedFolder();
723         delete w;
724     }
725     MltVideoProfile prof;
726     if (!profileName.isEmpty()) prof = ProfilesDialog::getVideoProfile(profileName);
727     else prof = ProfilesDialog::getVideoProfile("dv_pal");
728     if (prof.width == 0) prof = ProfilesDialog::getVideoProfile("dv_pal");
729     KdenliveDoc *doc = new KdenliveDoc(KUrl(), projectFolder, prof, m_commandStack, this);
730     TrackView *trackView = new TrackView(doc, this);
731     m_timelineArea->addTab(trackView, KIcon("kdenlive"), i18n("Untitled") + " / " + prof.description);
732     if (m_timelineArea->count() == 1) {
733         connectDocumentInfo(doc);
734         connectDocument(trackView, doc);
735     } else m_timelineArea->setTabBarHidden(false);
736 }
737
738 void MainWindow::activateDocument() {
739     TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
740     KdenliveDoc *currentDoc = currentTab->document();
741     connectDocumentInfo(currentDoc);
742     connectDocument(currentTab, currentDoc);
743 }
744
745 void MainWindow::slotRemoveTab() {
746     QWidget *w = m_timelineArea->currentWidget();
747     // closing current document
748     int ix = m_timelineArea->currentIndex() + 1;
749     if (ix == m_timelineArea->count()) ix = 0;
750     m_timelineArea->setCurrentIndex(ix);
751     TrackView *tabToClose = (TrackView *) w;
752     KdenliveDoc *docToClose = tabToClose->document();
753     m_timelineArea->removeTab(m_timelineArea->indexOf(w));
754     if (m_timelineArea->count() == 1) m_timelineArea->setTabBarHidden(true);
755     delete docToClose;
756     delete w;
757 }
758
759 void MainWindow::saveFileAs(const QString &outputFileName) {
760     m_projectMonitor->saveSceneList(outputFileName, m_activeDocument->documentInfoXml());
761     m_activeDocument->setUrl(KUrl(outputFileName));
762     setCaption(m_activeDocument->description());
763     m_timelineArea->setTabText(m_timelineArea->currentIndex(), m_activeDocument->description());
764     m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), m_activeDocument->url().path());
765     m_activeDocument->setModified(false);
766     m_fileOpenRecent->addUrl(KUrl(outputFileName));
767 }
768
769 void MainWindow::saveFileAs() {
770     QString outputFile = KFileDialog::getSaveFileName(KUrl(), "*.kdenlive|Kdenlive project files (*.kdenlive)");
771     if (QFile::exists(outputFile)) {
772         if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it ?")) == KMessageBox::No) return;
773     }
774     saveFileAs(outputFile);
775 }
776
777 void MainWindow::saveFile() {
778     if (!m_activeDocument) return;
779     if (m_activeDocument->url().isEmpty()) {
780         saveFileAs();
781     } else {
782         saveFileAs(m_activeDocument->url().path());
783     }
784 }
785
786 void MainWindow::openFile() {
787     KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.kdenlive|Kdenlive project files (*.kdenlive)\n*.westley|MLT project files (*.westley)");
788     if (url.isEmpty()) return;
789     m_fileOpenRecent->addUrl(url);
790     openFile(url);
791 }
792
793 void MainWindow::openLastFile() {
794     KSharedConfigPtr config = KGlobal::config();
795     QString Lastproject = config->group("Recent Files").readPathEntry("File1", QString());
796     openFile(KUrl(Lastproject));
797 }
798
799 void MainWindow::openFile(const KUrl &url) {
800     // Check for backup file
801     bool recovery = false;
802     QString directory = url.directory();
803     QString fileName = url.fileName();
804     KUrl recoveryUrl;
805     recoveryUrl.setDirectory(directory);
806     recoveryUrl.setFileName("~" + fileName);
807     if (KIO::NetAccess::exists(recoveryUrl, KIO::NetAccess::SourceSide, this)) {
808         KFileItem bkup(KFileItem::Unknown, KFileItem::Unknown, recoveryUrl, true);
809         KFileItem src(KFileItem::Unknown, KFileItem::Unknown, url, true);
810         if (bkup.time(KFileItem::ModificationTime) > src.time(KFileItem::ModificationTime)) {
811             // Backup file is more recent than source file, ask user for recovery
812             if (KMessageBox::questionYesNo(this, i18n("A newer recovery file exists for <b>%1</b>\nOpen recovery file ?", url.fileName())) == KMessageBox::Yes) recovery = true;
813         }
814     }
815
816     //TODO: get video profile from url before opening it
817     MltVideoProfile prof = ProfilesDialog::getVideoProfile(KdenliveSettings::default_profile());
818     if (prof.width == 0) prof = ProfilesDialog::getVideoProfile("dv_pal");
819     KdenliveDoc *doc;
820     if (recovery) {
821         doc = new KdenliveDoc(recoveryUrl, KUrl(), prof, m_commandStack, this);
822         doc->setUrl(url);
823         doc->setModified(true);
824     } else doc = new KdenliveDoc(url, KUrl(), prof, m_commandStack, this);
825     connectDocumentInfo(doc);
826     TrackView *trackView = new TrackView(doc, this);
827     m_timelineArea->setCurrentIndex(m_timelineArea->addTab(trackView, KIcon("kdenlive"), doc->description()));
828     m_timelineArea->setTabToolTip(m_timelineArea->currentIndex(), doc->url().path());
829     if (m_timelineArea->count() > 1) m_timelineArea->setTabBarHidden(false);
830     slotGotProgressInfo(QString(), -1);
831     m_projectMonitor->refreshMonitor(true);
832     //connectDocument(trackView, doc);
833 }
834
835
836 void MainWindow::parseProfiles() {
837     //kdDebug()<<" + + YOUR MLT INSTALL WAS FOUND IN: "<< MLT_PREFIX <<endl;
838
839     //KdenliveSettings::setDefaulttmpfolder();
840     if (KdenliveSettings::mltpath().isEmpty()) {
841         KdenliveSettings::setMltpath(QString(MLT_PREFIX) + QString("/share/mlt/profiles/"));
842     }
843     if (KdenliveSettings::rendererpath().isEmpty()) {
844         KdenliveSettings::setRendererpath(KStandardDirs::findExe("inigo"));
845     }
846     QStringList profilesFilter;
847     profilesFilter << "*";
848     QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
849
850     if (profilesList.isEmpty()) {
851         // Cannot find MLT path, try finding inigo
852         QString profilePath = KdenliveSettings::rendererpath();
853         if (!profilePath.isEmpty()) {
854             profilePath = profilePath.section('/', 0, -3);
855             KdenliveSettings::setMltpath(profilePath + "/share/mlt/profiles/");
856             QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
857         }
858
859         if (profilesList.isEmpty()) {
860             // Cannot find the MLT profiles, ask for location
861             KUrlRequesterDialog *getUrl = new KUrlRequesterDialog(KdenliveSettings::mltpath(), i18n("Cannot find your Mlt profiles, please give the path"), this);
862             getUrl->fileDialog()->setMode(KFile::Directory);
863             getUrl->exec();
864             KUrl mltPath = getUrl->selectedUrl();
865             delete getUrl;
866             if (mltPath.isEmpty()) kapp->quit();
867             KdenliveSettings::setMltpath(mltPath.path());
868             QStringList profilesList = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
869         }
870     }
871
872     if (KdenliveSettings::rendererpath().isEmpty()) {
873         // Cannot find the MLT inigo renderer, ask for location
874         KUrlRequesterDialog *getUrl = new KUrlRequesterDialog(KdenliveSettings::mltpath(), i18n("Cannot find the inigo program required for rendering (part of Mlt)"), this);
875         getUrl->exec();
876         KUrl rendererPath = getUrl->selectedUrl();
877         delete getUrl;
878         if (rendererPath.isEmpty()) kapp->quit();
879         KdenliveSettings::setRendererpath(rendererPath.path());
880     }
881
882     kDebug() << "RESULTING MLT PATH: " << KdenliveSettings::mltpath();
883
884     // Parse MLT profiles to build a list of available video formats
885     if (profilesList.isEmpty()) parseProfiles();
886 }
887
888
889 void MainWindow::slotEditProfiles() {
890     ProfilesDialog *w = new ProfilesDialog;
891     w->exec();
892     delete w;
893 }
894
895 void MainWindow::slotEditProjectSettings() {
896     ProjectSettings *w = new ProjectSettings;
897     if (w->exec() == QDialog::Accepted) {
898         QString profile = w->selectedProfile();
899         m_activeDocument->setProfilePath(profile);
900         m_monitorManager->resetProfiles(profile);
901         setCaption(m_activeDocument->description());
902         KdenliveSettings::setCurrent_profile(m_activeDocument->profilePath());
903         if (m_renderWidget) m_renderWidget->setDocumentStandard(m_activeDocument->getDocumentStandard());
904         m_monitorManager->setTimecode(m_activeDocument->timecode());
905         m_timelineArea->setTabText(m_timelineArea->currentIndex(), m_activeDocument->description());
906
907         // We need to desactivate & reactivate monitors to get a refresh
908         m_monitorManager->switchMonitors();
909     }
910     delete w;
911 }
912
913 void MainWindow::slotRenderProject() {
914     if (!m_renderWidget) {
915         m_renderWidget = new RenderWidget(this);
916         connect(m_renderWidget, SIGNAL(doRender(const QString&, const QString&, const QStringList &, bool, bool)), this, SLOT(slotDoRender(const QString&, const QString&, const QStringList &, bool, bool)));
917     }
918     /*TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
919     if (currentTab) m_renderWidget->setTimeline(currentTab);
920     m_renderWidget->setDocument(m_activeDocument);*/
921     m_renderWidget->show();
922 }
923
924 void MainWindow::slotDoRender(const QString &dest, const QString &render, const QStringList &avformat_args, bool zoneOnly, bool playAfter) {
925     if (dest.isEmpty()) return;
926     int in;
927     int out;
928     TrackView *currentTab = (TrackView *) m_timelineArea->currentWidget();
929     if (currentTab && zoneOnly) {
930         in = currentTab->inPoint();
931         out = currentTab->outPoint();
932     }
933     KTemporaryFile temp;
934     temp.setAutoRemove(false);
935     temp.setSuffix(".westley");
936     if (temp.open()) {
937         m_projectMonitor->saveSceneList(temp.fileName());
938         QStringList args;
939         args << "-erase";
940         if (zoneOnly) args << "in=" + QString::number(in) << "out=" + QString::number(out);
941         QString videoPlayer = "-";
942         if (playAfter) {
943             videoPlayer = KdenliveSettings::defaultplayerapp();
944             if (videoPlayer.isEmpty()) KMessageBox::sorry(this, i18n("Cannot play video after rendering because the default video player application is not set.\nPlease define it in Kdenlive settings dialog."));
945         }
946         args << KdenliveSettings::rendererpath() << m_activeDocument->profilePath() << render << videoPlayer << temp.fileName() << dest << avformat_args;
947         QProcess::startDetached("kdenlive_render", args);
948     }
949 }
950
951 void MainWindow::slotUpdateMousePosition(int pos) {
952     if (m_activeDocument)
953         switch (m_timecodeFormat->currentIndex()) {
954         case 0:
955             statusBar()->changeItem(m_activeDocument->timecode().getTimecodeFromFrames(pos), ID_TIMELINE_POS);
956             break;
957         default:
958             statusBar()->changeItem(QString::number(pos), ID_TIMELINE_POS);
959         }
960 }
961
962 void MainWindow::slotUpdateDocumentState(bool modified) {
963     setCaption(m_activeDocument->description(), modified);
964     if (modified) {
965         m_timelineArea->setTabTextColor(m_timelineArea->currentIndex(), palette().color(QPalette::Link));
966         m_timelineArea->setTabIcon(m_timelineArea->currentIndex(), KIcon("document-save"));
967     } else {
968         m_timelineArea->setTabTextColor(m_timelineArea->currentIndex(), palette().color(QPalette::Text));
969         m_timelineArea->setTabIcon(m_timelineArea->currentIndex(), KIcon("kdenlive"));
970     }
971 }
972
973 void MainWindow::connectDocumentInfo(KdenliveDoc *doc) {
974     if (m_activeDocument) {
975         if (m_activeDocument == doc) return;
976         disconnect(m_activeDocument, SIGNAL(progressInfo(const QString &, int)), this, SLOT(slotGotProgressInfo(const QString &, int)));
977     }
978     connect(doc, SIGNAL(progressInfo(const QString &, int)), this, SLOT(slotGotProgressInfo(const QString &, int)));
979 }
980
981 void MainWindow::connectDocument(TrackView *trackView, KdenliveDoc *doc) { //changed
982     //m_projectMonitor->stop();
983     kDebug() << "///////////////////   CONNECTING DOC TO PROJECT VIEW ////////////////";
984     if (m_activeDocument) {
985         if (m_activeDocument == doc) return;
986         m_activeDocument->backupMltPlaylist();
987         if (m_activeTimeline) {
988             disconnect(m_projectMonitor, SIGNAL(renderPosition(int)), m_activeTimeline, SLOT(moveCursorPos(int)));
989             disconnect(m_projectMonitor, SIGNAL(durationChanged(int)), m_activeTimeline, SLOT(setDuration(int)));
990             disconnect(m_activeDocument, SIGNAL(addProjectClip(DocClipBase *)), m_projectList, SLOT(slotAddClip(DocClipBase *)));
991             disconnect(m_activeDocument, SIGNAL(addProjectFolder(const QString, int, bool, bool)), m_projectList, SLOT(slotAddFolder(const QString, int, bool, bool)));
992             disconnect(m_activeDocument, SIGNAL(signalDeleteProjectClip(int)), m_projectList, SLOT(slotDeleteClip(int)));
993             disconnect(m_activeDocument, SIGNAL(updateClipDisplay(int)), m_projectList, SLOT(slotUpdateClip(int)));
994             disconnect(m_activeDocument, SIGNAL(refreshClipThumbnail(int)), m_projectList, SLOT(slotRefreshClipThumbnail(int)));
995             disconnect(m_activeDocument, SIGNAL(deletTimelineClip(int)), m_activeTimeline, SLOT(slotDeleteClip(int)));
996             disconnect(m_activeTimeline, SIGNAL(clipItemSelected(ClipItem*)), effectStack, SLOT(slotClipItemSelected(ClipItem*)));
997             disconnect(m_activeTimeline, SIGNAL(clipItemSelected(ClipItem*)), this, SLOT(slotActivateEffectStackView()));
998             disconnect(m_activeTimeline, SIGNAL(transitionItemSelected(Transition*)), transitionConfig, SLOT(slotTransitionItemSelected(Transition*)));
999             disconnect(m_activeTimeline, SIGNAL(transitionItemSelected(Transition*)), this, SLOT(slotActivateTransitionView()));
1000             disconnect(m_zoomSlider, SIGNAL(valueChanged(int)), m_activeTimeline, SLOT(slotChangeZoom(int)));
1001             disconnect(m_activeTimeline->projectView(), SIGNAL(displayMessage(const QString&, MessageType)), m_messageLabel, SLOT(setMessage(const QString&, MessageType)));
1002             disconnect(m_activeTimeline->projectView(), SIGNAL(showClipFrame(DocClipBase *, const int)), m_clipMonitor, SLOT(slotSetXml(DocClipBase *, const int)));
1003
1004             disconnect(m_activeDocument, SIGNAL(docModified(bool)), this, SLOT(slotUpdateDocumentState(bool)));
1005             disconnect(effectStack, SIGNAL(updateClipEffect(ClipItem*, QDomElement, QDomElement, int)), m_activeTimeline->projectView(), SLOT(slotUpdateClipEffect(ClipItem*, QDomElement, QDomElement, int)));
1006             disconnect(effectStack, SIGNAL(removeEffect(ClipItem*, QDomElement)), m_activeTimeline->projectView(), SLOT(slotDeleteEffect(ClipItem*, QDomElement)));
1007             disconnect(effectStack, SIGNAL(changeEffectState(ClipItem*, int, bool)), m_activeTimeline->projectView(), SLOT(slotChangeEffectState(ClipItem*, int, bool)));
1008             disconnect(effectStack, SIGNAL(changeEffectPosition(ClipItem*, int, int)), trackView->projectView(), SLOT(slotChangeEffectPosition(ClipItem*, int, int)));
1009             disconnect(effectStack, SIGNAL(refreshEffectStack(ClipItem*)), m_activeTimeline->projectView(), SLOT(slotRefreshEffects(ClipItem*)));
1010             disconnect(transitionConfig, SIGNAL(transitionUpdated(Transition *, QDomElement)), trackView->projectView() , SLOT(slotTransitionUpdated(Transition *, QDomElement)));
1011             disconnect(m_activeTimeline->projectView(), SIGNAL(activateDocumentMonitor()), m_projectMonitor, SLOT(activateMonitor()));
1012         }
1013         m_activeDocument->setRenderer(NULL);
1014         disconnect(m_projectList, SIGNAL(clipSelected(DocClipBase *)), m_clipMonitor, SLOT(slotSetXml(DocClipBase *)));
1015         m_clipMonitor->stop();
1016     }
1017     KdenliveSettings::setProject_fps(doc->fps());
1018     m_monitorManager->resetProfiles(doc->profilePath());
1019     m_projectList->setDocument(doc);
1020     connect(m_projectList, SIGNAL(clipSelected(DocClipBase *)), m_clipMonitor, SLOT(slotSetXml(DocClipBase *)));
1021     connect(trackView, SIGNAL(cursorMoved()), m_projectMonitor, SLOT(activateMonitor()));
1022     connect(trackView, SIGNAL(mousePosition(int)), this, SLOT(slotUpdateMousePosition(int)));
1023     connect(m_projectMonitor, SIGNAL(renderPosition(int)), trackView, SLOT(moveCursorPos(int)));
1024     connect(m_projectMonitor, SIGNAL(durationChanged(int)), trackView, SLOT(setDuration(int)));
1025     connect(doc, SIGNAL(addProjectClip(DocClipBase *)), m_projectList, SLOT(slotAddClip(DocClipBase *)));
1026     connect(doc, SIGNAL(addProjectFolder(const QString, int, bool, bool)), m_projectList, SLOT(slotAddFolder(const QString, int, bool, bool)));
1027     connect(doc, SIGNAL(signalDeleteProjectClip(int)), m_projectList, SLOT(slotDeleteClip(int)));
1028     connect(doc, SIGNAL(updateClipDisplay(int)), m_projectList, SLOT(slotUpdateClip(int)));
1029     connect(doc, SIGNAL(refreshClipThumbnail(int)), m_projectList, SLOT(slotRefreshClipThumbnail(int)));
1030
1031     connect(doc, SIGNAL(deletTimelineClip(int)), trackView, SLOT(slotDeleteClip(int)));
1032     connect(doc, SIGNAL(docModified(bool)), this, SLOT(slotUpdateDocumentState(bool)));
1033
1034
1035
1036     connect(trackView, SIGNAL(clipItemSelected(ClipItem*)), effectStack, SLOT(slotClipItemSelected(ClipItem*)));
1037     connect(trackView, SIGNAL(clipItemSelected(ClipItem*)), this, SLOT(slotActivateEffectStackView()));
1038     connect(trackView, SIGNAL(transitionItemSelected(Transition*)), transitionConfig, SLOT(slotTransitionItemSelected(Transition*)));
1039     connect(trackView, SIGNAL(transitionItemSelected(Transition*)), this, SLOT(slotActivateTransitionView()));
1040     m_zoomSlider->setValue(doc->zoom());
1041     connect(m_zoomSlider, SIGNAL(valueChanged(int)), trackView, SLOT(slotChangeZoom(int)));
1042     connect(trackView->projectView(), SIGNAL(zoomIn()), this, SLOT(slotZoomIn()));
1043     connect(trackView->projectView(), SIGNAL(zoomOut()), this, SLOT(slotZoomOut()));
1044     connect(trackView->projectView(), SIGNAL(displayMessage(const QString&, MessageType)), m_messageLabel, SLOT(setMessage(const QString&, MessageType)));
1045
1046     connect(trackView->projectView(), SIGNAL(showClipFrame(DocClipBase *, const int)), m_clipMonitor, SLOT(slotSetXml(DocClipBase *, const int)));
1047
1048
1049     connect(effectStack, SIGNAL(updateClipEffect(ClipItem*, QDomElement, QDomElement, int)), trackView->projectView(), SLOT(slotUpdateClipEffect(ClipItem*, QDomElement, QDomElement, int)));
1050     connect(effectStack, SIGNAL(removeEffect(ClipItem*, QDomElement)), trackView->projectView(), SLOT(slotDeleteEffect(ClipItem*, QDomElement)));
1051     connect(effectStack, SIGNAL(changeEffectState(ClipItem*, int, bool)), trackView->projectView(), SLOT(slotChangeEffectState(ClipItem*, int, bool)));
1052     connect(effectStack, SIGNAL(changeEffectPosition(ClipItem*, int, int)), trackView->projectView(), SLOT(slotChangeEffectPosition(ClipItem*, int, int)));
1053     connect(effectStack, SIGNAL(refreshEffectStack(ClipItem*)), trackView->projectView(), SLOT(slotRefreshEffects(ClipItem*)));
1054     connect(transitionConfig, SIGNAL(transitionUpdated(Transition *, QDomElement)), trackView->projectView() , SLOT(slotTransitionUpdated(Transition *, QDomElement)));
1055     connect(trackView->projectView(), SIGNAL(activateDocumentMonitor()), m_projectMonitor, SLOT(activateMonitor()));
1056     trackView->projectView()->setContextMenu(m_timelineContextMenu, m_timelineContextClipMenu, m_timelineContextTransitionMenu);
1057     m_activeTimeline = trackView;
1058     KdenliveSettings::setCurrent_profile(doc->profilePath());
1059     if (m_renderWidget) m_renderWidget->setDocumentStandard(doc->getDocumentStandard());
1060     m_monitorManager->setTimecode(doc->timecode());
1061     doc->setRenderer(m_projectMonitor->render);
1062     m_commandStack->setActiveStack(doc->commandStack());
1063
1064     doc->updateAllProjectClips();
1065
1066     if (m_commandStack->isClean()) kDebug() << "////////////  UNDO STACK IS CLEAN";
1067     else  kDebug() << "////////////  UNDO STACK IS NOT CLEAN*******************";
1068
1069     //m_overView->setScene(trackView->projectScene());
1070     //m_overView->scale(m_overView->width() / trackView->duration(), m_overView->height() / (50 * trackView->tracksNumber()));
1071     //m_overView->fitInView(m_overView->itemAt(0, 50), Qt::KeepAspectRatio);
1072
1073     setCaption(doc->description());
1074     m_activeDocument = doc;
1075 }
1076
1077 void MainWindow::slotPreferences() {
1078     //An instance of your dialog could be already created and could be
1079     // cached, in which case you want to display the cached dialog
1080     // instead of creating another one
1081     if (KConfigDialog::showDialog("settings"))
1082         return;
1083
1084     // KConfigDialog didn't find an instance of this dialog, so lets
1085     // create it :
1086     KdenliveSettingsDialog* dialog = new KdenliveSettingsDialog(this);
1087     connect(dialog, SIGNAL(settingsChanged(const QString&)), this, SLOT(updateConfiguration()));
1088     dialog->show();
1089 }
1090
1091 void MainWindow::updateConfiguration() {
1092     //TODO: we should apply settings to all projects, not only the current one
1093     if (m_activeTimeline) {
1094         m_activeTimeline->refresh();
1095         m_activeTimeline->projectView()->checkAutoScroll();
1096         m_activeTimeline->projectView()->checkTrackHeight();
1097         if (m_activeDocument) m_activeDocument->clipManager()->checkAudioThumbs();
1098     }
1099     m_buttonAudioThumbs->setChecked(KdenliveSettings::audiothumbnails());
1100     m_buttonVideoThumbs->setChecked(KdenliveSettings::videothumbnails());
1101     activateShuttleDevice();
1102
1103 }
1104
1105 void MainWindow::slotSwitchVideoThumbs() {
1106     KdenliveSettings::setVideothumbnails(!KdenliveSettings::videothumbnails());
1107     if (m_activeTimeline) {
1108         m_activeTimeline->refresh();
1109     }
1110     m_buttonVideoThumbs->setChecked(KdenliveSettings::videothumbnails());
1111 }
1112
1113 void MainWindow::slotSwitchAudioThumbs() {
1114     KdenliveSettings::setAudiothumbnails(!KdenliveSettings::audiothumbnails());
1115     if (m_activeTimeline) {
1116         m_activeTimeline->refresh();
1117         m_activeTimeline->projectView()->checkAutoScroll();
1118         if (m_activeDocument) m_activeDocument->clipManager()->checkAudioThumbs();
1119     }
1120     m_buttonAudioThumbs->setChecked(KdenliveSettings::audiothumbnails());
1121 }
1122
1123 void MainWindow::slotSwitchMarkersComments() {
1124     KdenliveSettings::setShowmarkers(!KdenliveSettings::showmarkers());
1125     if (m_activeTimeline) {
1126         m_activeTimeline->refresh();
1127     }
1128     m_buttonShowMarkers->setChecked(KdenliveSettings::showmarkers());
1129 }
1130
1131 void MainWindow::slotSwitchSnap() {
1132     KdenliveSettings::setSnaptopoints(!KdenliveSettings::snaptopoints());
1133     m_buttonShowMarkers->setChecked(KdenliveSettings::snaptopoints());
1134 }
1135
1136
1137 void MainWindow::slotDeleteTimelineClip() {
1138     if (m_activeTimeline) {
1139         m_activeTimeline->projectView()->deleteSelectedClips();
1140     }
1141 }
1142
1143 void MainWindow::slotAddClipMarker() {
1144     if (m_activeTimeline) {
1145         m_activeTimeline->projectView()->slotAddClipMarker();
1146     }
1147 }
1148
1149 void MainWindow::slotDeleteClipMarker() {
1150     if (m_activeTimeline) {
1151         m_activeTimeline->projectView()->slotDeleteClipMarker();
1152     }
1153 }
1154
1155 void MainWindow::slotDeleteAllClipMarkers() {
1156     if (m_activeTimeline) {
1157         m_activeTimeline->projectView()->slotDeleteAllClipMarkers();
1158     }
1159 }
1160
1161 void MainWindow::slotEditClipMarker() {
1162     if (m_activeTimeline) {
1163         m_activeTimeline->projectView()->slotEditClipMarker();
1164     }
1165 }
1166
1167 void MainWindow::slotAddGuide() {
1168     if (m_activeTimeline)
1169         m_activeTimeline->projectView()->slotAddGuide();
1170 }
1171
1172 void MainWindow::slotEditGuide() {
1173     if (m_activeTimeline)
1174         m_activeTimeline->projectView()->slotEditGuide();
1175 }
1176
1177 void MainWindow::slotDeleteGuide() {
1178     if (m_activeTimeline)
1179         m_activeTimeline->projectView()->slotDeleteGuide();
1180 }
1181
1182 void MainWindow::slotDeleteAllGuides() {
1183     if (m_activeTimeline)
1184         m_activeTimeline->projectView()->slotDeleteAllGuides();
1185 }
1186
1187 void MainWindow::slotCutTimelineClip() {
1188     if (m_activeTimeline) {
1189         m_activeTimeline->projectView()->cutSelectedClips();
1190     }
1191 }
1192
1193 void MainWindow::slotAddProjectClip(KUrl url) {
1194     if (m_activeDocument)
1195         m_activeDocument->slotAddClipFile(url, QString());
1196 }
1197
1198 void MainWindow::slotAddTransition(QAction *result) {
1199     if (!result) return;
1200     QDomElement effect = transitions.getEffectByName(result->data().toString());
1201     if (m_activeTimeline) {
1202         m_activeTimeline->projectView()->slotAddTransitionToSelectedClips(effect);
1203     }
1204 }
1205
1206 void MainWindow::slotAddVideoEffect(QAction *result) {
1207     if (!result) return;
1208     QDomElement effect = videoEffects.getEffectByName(result->data().toString());
1209     slotAddEffect(effect);
1210 }
1211
1212 void MainWindow::slotAddAudioEffect(QAction *result) {
1213     if (!result) return;
1214     QDomElement effect = audioEffects.getEffectByName(result->data().toString());
1215     slotAddEffect(effect);
1216 }
1217
1218 void MainWindow::slotAddCustomEffect(QAction *result) {
1219     if (!result) return;
1220     QDomElement effect = customEffects.getEffectByName(result->data().toString());
1221     slotAddEffect(effect);
1222 }
1223
1224 void MainWindow::slotZoomIn() {
1225     m_zoomSlider->setValue(m_zoomSlider->value() - 1);
1226 }
1227
1228 void MainWindow::slotZoomOut() {
1229     m_zoomSlider->setValue(m_zoomSlider->value() + 1);
1230 }
1231
1232 void MainWindow::slotFitZoom() {
1233     if (m_activeTimeline) {
1234         m_zoomSlider->setValue(m_activeTimeline->fitZoom());
1235     }
1236 }
1237
1238 void MainWindow::slotGotProgressInfo(const QString &message, int progress) {
1239     statusProgressBar->setValue(progress);
1240     if (progress >= 0) {
1241         if (!message.isEmpty()) m_messageLabel->setMessage(message, InformationMessage);//statusLabel->setText(message);
1242         statusProgressBar->setVisible(true);
1243     } else {
1244         m_messageLabel->setMessage(QString(), DefaultMessage);
1245         statusProgressBar->setVisible(false);
1246     }
1247 }
1248
1249 void MainWindow::slotShowClipProperties(DocClipBase *clip) {
1250     if (clip->clipType() == TEXT) {
1251         m_activeDocument->editTextClip(clip->getProperty("xml"), clip->getId());
1252         return;
1253     }
1254     ClipProperties dia(clip, m_activeDocument->timecode(), m_activeDocument->fps(), this);
1255     connect(&dia, SIGNAL(addMarker(int, GenTime, QString)), m_activeTimeline->projectView(), SLOT(slotAddClipMarker(int, GenTime, QString)));
1256     if (dia.exec() == QDialog::Accepted) {
1257         m_projectList->slotUpdateClipProperties(dia.clipId(), dia.properties());
1258         if (dia.needsTimelineRefresh()) {
1259             // update clip occurences in timeline
1260             m_activeTimeline->projectView()->slotUpdateClip(dia.clipId());
1261         }
1262     }
1263 }
1264
1265 void MainWindow::customEvent(QEvent* e) {
1266     if (e->type() == QEvent::User) {
1267         // The timeline playing position changed...
1268         kDebug() << "RECIEVED JOG EVEMNT!!!";
1269     }
1270 }
1271 void MainWindow::slotActivateEffectStackView() {
1272     effectStack->raiseWindow(effectStackDock);
1273 }
1274
1275 void MainWindow::slotActivateTransitionView() {
1276     transitionConfig->raiseWindow(transitionConfigDock);
1277 }
1278
1279 void MainWindow::slotSnapRewind() {
1280     if (m_monitorManager->projectMonitorFocused()) {
1281         if (m_activeTimeline)
1282             m_activeTimeline->projectView()->slotSeekToPreviousSnap();
1283     }
1284 }
1285
1286 void MainWindow::slotSnapForward() {
1287     if (m_monitorManager->projectMonitorFocused()) {
1288         if (m_activeTimeline)
1289             m_activeTimeline->projectView()->slotSeekToNextSnap();
1290     }
1291 }
1292
1293 void MainWindow::slotClipStart() {
1294     if (m_monitorManager->projectMonitorFocused()) {
1295         if (m_activeTimeline)
1296             m_activeTimeline->projectView()->clipStart();
1297     }
1298 }
1299
1300 void MainWindow::slotClipEnd() {
1301     if (m_monitorManager->projectMonitorFocused()) {
1302         if (m_activeTimeline)
1303             m_activeTimeline->projectView()->clipEnd();
1304     }
1305 }
1306
1307 void MainWindow::slotChangeTool(QAction * action) {
1308     if (action == m_buttonSelectTool) slotSetTool(SELECTTOOL);
1309     else if (action == m_buttonRazorTool) slotSetTool(RAZORTOOL);
1310 }
1311
1312 void MainWindow::slotSetTool(PROJECTTOOL tool) {
1313     if (m_activeDocument && m_activeTimeline) {
1314         //m_activeDocument->setTool(tool);
1315         m_activeTimeline->projectView()->setTool(tool);
1316     }
1317 }
1318
1319 void MainWindow::slotCopy() {
1320     if (!m_activeDocument || !m_activeTimeline) return;
1321     m_activeTimeline->projectView()->copyClip();
1322 }
1323
1324 void MainWindow::slotPaste() {
1325     if (!m_activeDocument || !m_activeTimeline) return;
1326     m_activeTimeline->projectView()->pasteClip();
1327 }
1328
1329 void MainWindow::slotFind() {
1330     if (!m_activeDocument || !m_activeTimeline) return;
1331     m_projectSearch->setEnabled(false);
1332     m_findActivated = true;
1333     m_findString = QString();
1334     m_activeTimeline->projectView()->initSearchStrings();
1335     statusBar()->showMessage(i18n("Starting -- find text as you type"));
1336     m_findTimer.start(5000);
1337     qApp->installEventFilter(this);
1338 }
1339
1340 void MainWindow::slotFindNext() {
1341     if (m_activeTimeline && m_activeTimeline->projectView()->findNextString(m_findString)) {
1342         statusBar()->showMessage(i18n("Found : %1", m_findString));
1343     } else {
1344         statusBar()->showMessage(i18n("Reached end of project"));
1345     }
1346     m_findTimer.start(4000);
1347 }
1348
1349 void MainWindow::findAhead() {
1350     if (m_activeTimeline && m_activeTimeline->projectView()->findString(m_findString)) {
1351         m_projectSearchNext->setEnabled(true);
1352         statusBar()->showMessage(i18n("Found : %1", m_findString));
1353     } else {
1354         m_projectSearchNext->setEnabled(false);
1355         statusBar()->showMessage(i18n("Not found : %1", m_findString));
1356     }
1357 }
1358
1359 void MainWindow::findTimeout() {
1360     m_projectSearchNext->setEnabled(false);
1361     m_findActivated = false;
1362     m_findString = QString();
1363     statusBar()->showMessage(i18n("Find stopped"), 3000);
1364     if (m_activeTimeline) m_activeTimeline->projectView()->clearSearchStrings();
1365     m_projectSearch->setEnabled(true);
1366     removeEventFilter(this);
1367 }
1368
1369 void MainWindow::keyPressEvent(QKeyEvent *ke) {
1370     if (m_findActivated) {
1371         if (ke->key() == Qt::Key_Backspace) {
1372             m_findString = m_findString.left(m_findString.length() - 1);
1373
1374             if (!m_findString.isEmpty()) {
1375                 findAhead();
1376             } else {
1377                 findTimeout();
1378             }
1379
1380             m_findTimer.start(4000);
1381             ke->accept();
1382             return;
1383         } else if (ke->key() == Qt::Key_Escape) {
1384             findTimeout();
1385             ke->accept();
1386             return;
1387         } else if (ke->key() == Qt::Key_Space || !ke->text().trimmed().isEmpty()) {
1388             m_findString += ke->text();
1389
1390             findAhead();
1391
1392             m_findTimer.start(4000);
1393             ke->accept();
1394             return;
1395         }
1396     } else KXmlGuiWindow::keyPressEvent(ke);
1397 }
1398
1399 bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
1400     if (m_findActivated) {
1401         if (event->type() == QEvent::ShortcutOverride) {
1402             QKeyEvent* ke = (QKeyEvent*) event;
1403             if (ke->text().trimmed().isEmpty()) return false;
1404             ke->accept();
1405             return true;
1406         } else return false;
1407     } else {
1408         // pass the event on to the parent class
1409         return QMainWindow::eventFilter(obj, event);
1410     }
1411 }
1412
1413 #include "mainwindow.moc"