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