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