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