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