]> git.sesse.net Git - kdenlive/blob - src/headertrack.cpp
Merge branch 'master' of git://anongit.kde.org/kdenlive
[kdenlive] / src / headertrack.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 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 #include "headertrack.h"
22 #include "effectslist.h"
23 #include "kdenlivesettings.h"
24
25 #include <KIcon>
26 #include <KLocale>
27 #include <KDebug>
28
29 #include <QMouseEvent>
30 #include <QWidget>
31 #include <QPainter>
32 #include <QAction>
33 #include <QTimer>
34 #include <QDomDocument>
35
36 HeaderTrack::HeaderTrack(int index, TrackInfo info, int height, QList <QAction *> actions, QWidget *parent) :
37         QWidget(parent),
38         m_index(index),
39         m_type(info.type),
40         m_isSelected(false)
41 {
42     setFixedHeight(height);
43     setupUi(this);
44
45     m_name = info.trackName.isEmpty() ? QString::number(m_index) : info.trackName;
46     track_number->setText(m_name);
47     connect(track_number, SIGNAL(editingFinished()), this, SLOT(slotRenameTrack()));
48
49     buttonVideo->setChecked(info.isBlind);
50     buttonVideo->setToolTip(i18n("Hide track"));
51     buttonAudio->setChecked(info.isMute);
52     buttonAudio->setToolTip(i18n("Mute track"));
53     buttonLock->setChecked(info.isLocked);
54     buttonLock->setToolTip(i18n("Lock track"));
55     effect_label->setPixmap(KIcon("kdenlive-track_has_effect").pixmap(16, 16));
56     updateEffectLabel(info.effectsList.effectNames());
57     setAcceptDrops(true);
58
59     if (m_type == VIDEOTRACK) {
60         setBackgroundRole(QPalette::AlternateBase);
61         setAutoFillBackground(true);
62         if (!info.isBlind)
63             buttonVideo->setIcon(KIcon("kdenlive-show-video"));
64         else
65             buttonVideo->setIcon(KIcon("kdenlive-hide-video"));
66     } else {
67         buttonVideo->setHidden(true);
68     }
69     if (!info.isMute)
70         buttonAudio->setIcon(KIcon("kdenlive-show-audio"));
71     else
72         buttonAudio->setIcon(KIcon("kdenlive-hide-audio"));
73
74     if (!info.isLocked)
75         buttonLock->setIcon(KIcon("kdenlive-unlock"));
76     else
77         buttonLock->setIcon(KIcon("kdenlive-lock"));
78
79     connect(buttonVideo, SIGNAL(clicked()), this, SLOT(switchVideo()));
80     connect(buttonAudio, SIGNAL(clicked()), this, SLOT(switchAudio()));
81     connect(buttonLock, SIGNAL(clicked()), this, SLOT(switchLock()));
82
83     // Don't show track buttons if size is too small
84     if (height < 40) {
85         buttonVideo->setHidden(true);
86         buttonAudio->setHidden(true);
87         buttonLock->setHidden(true);
88         //horizontalSpacer;
89     }
90
91     setContextMenuPolicy(Qt::ActionsContextMenu);
92     addActions(actions);
93 }
94
95 /*HeaderTrack::~HeaderTrack()
96 {
97 }*/
98
99 void HeaderTrack::updateEffectLabel(QStringList effects)
100 {
101     if (!effects.isEmpty()) {
102         effect_label->setHidden(false);
103         effect_label->setToolTip(effects.join("/"));
104     } else {
105         effect_label->setHidden(true);
106         effect_label->setToolTip(QString());
107     }
108 }
109
110 // virtual
111 void HeaderTrack::mousePressEvent(QMouseEvent * event)
112 {
113     if (track_number->hasFocus()) {
114         track_number->clearFocus();
115         return;
116     }
117     if (!m_isSelected) emit selectTrack(m_index);
118     emit showTrackEffects(m_index);
119     QWidget::mousePressEvent(event);
120 }
121
122 void HeaderTrack::mouseDoubleClickEvent(QMouseEvent* event)
123 {
124     if (track_number->hasFocus()) {
125         track_number->clearFocus();
126         return;
127     }
128     emit configTrack(m_index);
129     QWidget::mouseDoubleClickEvent(event);
130 }
131
132 //virtual
133 void HeaderTrack::dropEvent(QDropEvent * event)
134 {
135     const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
136     QDomDocument doc;
137     doc.setContent(effects, true);
138     const QDomElement e = doc.documentElement();
139     emit selectTrack(m_index);
140     emit addTrackInfo(e, m_index);
141     /*if (scene() && !scene()->views().isEmpty()) {
142         event->accept();
143         CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
144         if (view) view->slotAddEffect(e, m_info.startPos, track());
145     }*/
146 }
147
148 //virtual
149 void HeaderTrack::dragEnterEvent(QDragEnterEvent *event)
150 {
151     if (buttonLock->isChecked()) {
152         event->setAccepted(false);
153     } else {
154         if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
155             const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
156             QDomDocument doc;
157             doc.setContent(effects, true);
158             if (doc.documentElement().attribute("id") != "speed") {
159                 event->setAccepted(true);
160             }
161         }
162     }
163 }
164
165 void HeaderTrack::setSelectedIndex(int ix)
166 {
167     if (m_index == ix) {
168         m_isSelected = true;
169         setBackgroundRole(QPalette::Button);
170         setAutoFillBackground(true);
171     } else if (m_type != VIDEOTRACK) {
172         m_isSelected = false;
173         setAutoFillBackground(false);
174     } else {
175         m_isSelected = false;
176         setBackgroundRole(QPalette::AlternateBase);
177     }
178     update();
179 }
180
181 void HeaderTrack::adjustSize(int height)
182 {
183     // Don't show track buttons if size is too small
184     bool smallTracks = height < 40;
185     if (m_type == VIDEOTRACK)
186         buttonVideo->setHidden(smallTracks);
187     buttonAudio->setHidden(smallTracks);
188     buttonLock->setHidden(smallTracks);
189     setFixedHeight(height);
190 }
191
192 void HeaderTrack::switchVideo()
193 {
194     if (buttonVideo->isChecked())
195         buttonVideo->setIcon(KIcon("kdenlive-hide-video"));
196     else
197         buttonVideo->setIcon(KIcon("kdenlive-show-video"));
198     emit switchTrackVideo(m_index);
199 }
200
201 void HeaderTrack::switchAudio()
202 {
203     if (buttonAudio->isChecked())
204         buttonAudio->setIcon(KIcon("kdenlive-hide-audio"));
205     else
206         buttonAudio->setIcon(KIcon("kdenlive-show-audio"));
207     emit switchTrackAudio(m_index);
208 }
209
210 void HeaderTrack::switchLock(bool emitSignal)
211 {
212     if (buttonLock->isChecked())
213         buttonLock->setIcon(KIcon("kdenlive-lock"));
214     else
215         buttonLock->setIcon(KIcon("kdenlive-unlock"));
216     if (emitSignal)
217         emit switchTrackLock(m_index);
218 }
219
220 void HeaderTrack::setLock(bool lock)
221 {
222     buttonLock->setChecked(lock);
223     switchLock(false);
224 }
225
226 void HeaderTrack::slotDeleteTrack()
227 {
228     QTimer::singleShot(500, this, SLOT(deleteTrack()));
229 }
230
231 void HeaderTrack::slotRenameTrack()
232 {
233     if (m_name != track_number->text()) emit renameTrack(m_index, track_number->text());
234 }
235
236
237
238 #include "headertrack.moc"