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