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