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