]> git.sesse.net Git - kdenlive/blob - src/headertrack.cpp
Show icon on track header if track has effects applied
[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
24 #include <KIcon>
25 #include <KLocale>
26 #include <KDebug>
27 #include <KColorScheme>
28
29 #include <QMouseEvent>
30 #include <QWidget>
31 #include <QPainter>
32 #include <QAction>
33 #include <QTimer>
34 #include <QColor>
35 #include <QDomDocument>
36
37 HeaderTrack::HeaderTrack(int index, TrackInfo info, int height, QWidget *parent) :
38         QWidget(parent),
39         m_index(index),
40         m_type(info.type),
41         m_isSelected(false)
42 {
43     setFixedHeight(height);
44     setupUi(this);
45     QColor col = track_number->palette().color(QPalette::Base);
46     track_number->setStyleSheet(QString("QLineEdit { background-color: transparent;} QLineEdit:hover{ background-color: rgb(%1, %2, %3);} QLineEdit:focus { background-color: rgb(%1, %2, %3);}").arg(col.red()).arg(col.green()).arg(col.blue()));
47
48     m_name = info.trackName.isEmpty() ? QString::number(m_index) : info.trackName;
49     track_number->setText(m_name);
50     connect(track_number, SIGNAL(editingFinished()), this, SLOT(slotRenameTrack()));
51
52     buttonVideo->setChecked(info.isBlind);
53     buttonVideo->setToolTip(i18n("Hide track"));
54     buttonAudio->setChecked(info.isMute);
55     buttonAudio->setToolTip(i18n("Mute track"));
56     buttonLock->setChecked(info.isLocked);
57     buttonLock->setToolTip(i18n("Lock track"));
58     effect_label->setPixmap(KIcon("kdenlive-track_has_effect").pixmap(16, 16));
59     updateEffectLabel(info.effectsList.effectNames());
60     setAcceptDrops(true);
61
62     QPalette p = palette();
63     KColorScheme scheme(p.currentColorGroup(), KColorScheme::Window);
64     p.setColor(QPalette::Button, scheme.background(KColorScheme::ActiveBackground).color().darker(120));
65     setPalette(p);
66
67     if (m_type == VIDEOTRACK) {
68         setBackgroundRole(QPalette::AlternateBase);
69         setAutoFillBackground(true);
70         if (!info.isBlind)
71             buttonVideo->setIcon(KIcon("kdenlive-show-video"));
72         else
73             buttonVideo->setIcon(KIcon("kdenlive-hide-video"));
74     } else {
75         buttonVideo->setHidden(true);
76     }
77     if (!info.isMute)
78         buttonAudio->setIcon(KIcon("kdenlive-show-audio"));
79     else
80         buttonAudio->setIcon(KIcon("kdenlive-hide-audio"));
81
82     if (!info.isLocked)
83         buttonLock->setIcon(KIcon("kdenlive-unlock"));
84     else
85         buttonLock->setIcon(KIcon("kdenlive-lock"));
86
87     connect(buttonVideo, SIGNAL(clicked()), this, SLOT(switchVideo()));
88     connect(buttonAudio, SIGNAL(clicked()), this, SLOT(switchAudio()));
89     connect(buttonLock, SIGNAL(clicked()), this, SLOT(switchLock()));
90
91     // Don't show track buttons if size is too small
92     if (height < 40) {
93         buttonVideo->setHidden(true);
94         buttonAudio->setHidden(true);
95         buttonLock->setHidden(true);
96         //horizontalSpacer;
97     }
98
99     setContextMenuPolicy(Qt::DefaultContextMenu); //Qt::ActionsContextMenu);
100     QAction *insertAction = new QAction(i18n("Insert Track"), this);
101     m_menu.addAction(insertAction);
102     connect(insertAction, SIGNAL(triggered()), this, SLOT(slotAddTrack()));
103
104     QAction *removeAction = new QAction(KIcon("edit-delete"), i18n("Delete Track"), this);
105     m_menu.addAction(removeAction);
106     connect(removeAction, SIGNAL(triggered()), this, SLOT(slotDeleteTrack()));
107
108     QAction *configAction = new QAction(KIcon("configure"), i18n("Configure Track"), this);
109     m_menu.addAction(configAction);
110     connect(configAction, SIGNAL(triggered()), this, SLOT(slotConfigTrack()));
111 }
112
113 /*HeaderTrack::~HeaderTrack()
114 {
115 }*/
116
117 void HeaderTrack::updateEffectLabel(QStringList effects)
118 {
119     QColor col = track_number->palette().color(QPalette::Base);
120     if (!effects.isEmpty()) {
121         effect_label->setHidden(false);
122         effect_label->setToolTip(effects.join("/"));
123     } else {
124         effect_label->setHidden(true);
125         effect_label->setToolTip(QString());
126     }
127 }
128
129 // virtual
130 void HeaderTrack::mousePressEvent(QMouseEvent * event)
131 {
132     if (track_number->hasFocus()) {
133         track_number->clearFocus();
134         return;
135     }
136     if (!m_isSelected) emit selectTrack(m_index);
137     emit showTrackEffects(m_index);
138     QWidget::mousePressEvent(event);
139 }
140
141 // virtual
142 void HeaderTrack::contextMenuEvent(QContextMenuEvent * event)
143 {
144     if (track_number->hasFocus()) {
145         track_number->clearFocus();
146         return;
147     }
148     m_menu.popup(event->globalPos());
149 }
150
151 void HeaderTrack::mouseDoubleClickEvent(QMouseEvent* event)
152 {
153     if (track_number->hasFocus()) {
154         track_number->clearFocus();
155         return;
156     }
157     slotConfigTrack();
158     QWidget::mouseDoubleClickEvent(event);
159 }
160
161 //virtual
162 void HeaderTrack::dropEvent(QDropEvent * event)
163 {
164     const QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
165     QDomDocument doc;
166     doc.setContent(effects, true);
167     const QDomElement e = doc.documentElement();
168     emit selectTrack(m_index);
169     emit addTrackInfo(e, m_index);
170     /*if (scene() && !scene()->views().isEmpty()) {
171         event->accept();
172         CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
173         if (view) view->slotAddEffect(e, m_info.startPos, track());
174     }*/
175 }
176
177 //virtual
178 void HeaderTrack::dragEnterEvent(QDragEnterEvent *event)
179 {
180     if (buttonLock->isChecked()) event->setAccepted(false);
181     else event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
182 }
183
184 void HeaderTrack::setSelectedIndex(int ix)
185 {
186     if (m_index == ix) {
187         m_isSelected = true;
188         setBackgroundRole(QPalette::Button);
189         setAutoFillBackground(true);
190     } else if (m_type != VIDEOTRACK) {
191         m_isSelected = false;
192         setAutoFillBackground(false);
193     } else {
194         m_isSelected = false;
195         setBackgroundRole(QPalette::AlternateBase);
196     }
197     update();
198 }
199
200 void HeaderTrack::adjustSize(int height)
201 {
202     // Don't show track buttons if size is too small
203     bool smallTracks = height < 40;
204     if (m_type == VIDEOTRACK)
205         buttonVideo->setHidden(smallTracks);
206     buttonAudio->setHidden(smallTracks);
207     buttonLock->setHidden(smallTracks);
208     setFixedHeight(height);
209 }
210
211 void HeaderTrack::switchVideo()
212 {
213     if (buttonVideo->isChecked())
214         buttonVideo->setIcon(KIcon("kdenlive-hide-video"));
215     else
216         buttonVideo->setIcon(KIcon("kdenlive-show-video"));
217     emit switchTrackVideo(m_index);
218 }
219
220 void HeaderTrack::switchAudio()
221 {
222     if (buttonAudio->isChecked())
223         buttonAudio->setIcon(KIcon("kdenlive-hide-audio"));
224     else
225         buttonAudio->setIcon(KIcon("kdenlive-show-audio"));
226     emit switchTrackAudio(m_index);
227 }
228
229 void HeaderTrack::switchLock(bool emitSignal)
230 {
231     if (buttonLock->isChecked())
232         buttonLock->setIcon(KIcon("kdenlive-lock"));
233     else
234         buttonLock->setIcon(KIcon("kdenlive-unlock"));
235     if (emitSignal)
236         emit switchTrackLock(m_index);
237 }
238
239 void HeaderTrack::setLock(bool lock)
240 {
241     buttonLock->setChecked(lock);
242     switchLock(false);
243 }
244
245 void HeaderTrack::slotDeleteTrack()
246 {
247     QTimer::singleShot(500, this, SLOT(deleteTrack()));
248 }
249
250 void HeaderTrack::deleteTrack()
251 {
252     emit deleteTrack(m_index);
253 }
254
255 void HeaderTrack::slotAddTrack()
256 {
257     emit insertTrack(m_index);
258 }
259
260 void HeaderTrack::slotRenameTrack()
261 {
262     if (m_name != track_number->text()) emit renameTrack(m_index, track_number->text());
263 }
264
265 void HeaderTrack::slotConfigTrack()
266 {
267     emit configTrack(m_index);
268 }
269
270
271 #include "headertrack.moc"