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