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