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