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