]> git.sesse.net Git - kdenlive/blob - src/headertrack.cpp
Fix includes
[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, const 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(const 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     QDomElement e = doc.documentElement();
139     if (e.tagName() == "effectgroup") {
140         // dropped an effect group
141         QDomNodeList effectlist = e.elementsByTagName("effect");
142         for (int i = 0; i < effectlist.count(); ++i) {
143             effectlist.at(i).toElement().removeAttribute("kdenlive_ix");
144         }
145     } else {
146         // single effect dropped
147         e.removeAttribute("kdenlive_ix");
148     }
149     emit selectTrack(m_index);
150     emit addTrackEffect(e, m_index);
151     /*if (scene() && !scene()->views().isEmpty()) {
152         event->accept();
153         CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
154         if (view) view->slotAddEffect(e, m_info.startPos, track());
155     }*/
156 }
157
158 //virtual
159 void HeaderTrack::dragEnterEvent(QDragEnterEvent *event)
160 {
161     if (buttonLock->isChecked()) {
162         event->setAccepted(false);
163     } else {
164         if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
165             const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
166             QDomDocument doc;
167             doc.setContent(effects, true);
168             if (doc.documentElement().attribute("id") != "speed") {
169                 event->setAccepted(true);
170             }
171         }
172     }
173 }
174
175 void HeaderTrack::setSelectedIndex(int ix)
176 {
177     if (m_index == ix) {
178         m_isSelected = true;
179         setBackgroundRole(QPalette::Button);
180         setAutoFillBackground(true);
181     } else if (m_type != VIDEOTRACK) {
182         m_isSelected = false;
183         setAutoFillBackground(false);
184     } else {
185         m_isSelected = false;
186         setBackgroundRole(QPalette::AlternateBase);
187     }
188     update();
189 }
190
191 void HeaderTrack::adjustSize(int height)
192 {
193     // Don't show track buttons if size is too small
194     bool smallTracks = height < 40;
195     if (m_type == VIDEOTRACK)
196         buttonVideo->setHidden(smallTracks);
197     buttonAudio->setHidden(smallTracks);
198     buttonLock->setHidden(smallTracks);
199     setFixedHeight(height);
200 }
201
202 void HeaderTrack::switchVideo()
203 {
204     if (buttonVideo->isChecked())
205         buttonVideo->setIcon(KIcon("kdenlive-hide-video"));
206     else
207         buttonVideo->setIcon(KIcon("kdenlive-show-video"));
208     emit switchTrackVideo(m_index);
209 }
210
211 void HeaderTrack::switchAudio()
212 {
213     if (buttonAudio->isChecked())
214         buttonAudio->setIcon(KIcon("kdenlive-hide-audio"));
215     else
216         buttonAudio->setIcon(KIcon("kdenlive-show-audio"));
217     emit switchTrackAudio(m_index);
218 }
219
220 void HeaderTrack::switchLock(bool emitSignal)
221 {
222     if (buttonLock->isChecked())
223         buttonLock->setIcon(KIcon("kdenlive-lock"));
224     else
225         buttonLock->setIcon(KIcon("kdenlive-unlock"));
226     if (emitSignal)
227         emit switchTrackLock(m_index);
228 }
229
230 void HeaderTrack::setLock(bool lock)
231 {
232     buttonLock->setChecked(lock);
233     switchLock(false);
234 }
235
236 void HeaderTrack::slotDeleteTrack()
237 {
238     QTimer::singleShot(500, this, SLOT(deleteTrack()));
239 }
240
241 void HeaderTrack::slotRenameTrack()
242 {
243     if (m_name != track_number->text()) emit renameTrack(m_index, track_number->text());
244 }
245
246
247
248 #include "headertrack.moc"