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