]> git.sesse.net Git - kdenlive/blob - src/headertrack.cpp
Cleanup track header widget and fix crash when bringing context menu while editing...
[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
35 HeaderTrack::HeaderTrack(int index, TrackInfo info, int height, QWidget *parent) :
36         QWidget(parent),
37         m_index(index),
38         m_type(info.type),
39         m_isSelected(false)
40 {
41     setFixedHeight(height);
42     setupUi(this);
43     QColor col = track_number->palette().color(QPalette::Base);
44     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()));
45
46     m_name = info.trackName.isEmpty() ? QString::number(m_index) : info.trackName;
47     track_number->setText(m_name);
48     connect(track_number, SIGNAL(editingFinished()), this, SLOT(slotRenameTrack()));
49
50     buttonVideo->setChecked(info.isBlind);
51     buttonVideo->setToolTip(i18n("Hide track"));
52     buttonAudio->setChecked(info.isMute);
53     buttonAudio->setToolTip(i18n("Mute track"));
54     buttonLock->setChecked(info.isLocked);
55     buttonLock->setToolTip(i18n("Lock track"));
56
57     QPalette p = palette();
58     KColorScheme scheme(p.currentColorGroup(), KColorScheme::Window);
59     p.setColor(QPalette::Button, scheme.background(KColorScheme::ActiveBackground).color().darker(120));
60     setPalette(p);
61
62     if (m_type == VIDEOTRACK) {
63         setBackgroundRole(QPalette::AlternateBase);
64         setAutoFillBackground(true);
65         if (!info.isBlind)
66             buttonVideo->setIcon(KIcon("kdenlive-show-video"));
67         else
68             buttonVideo->setIcon(KIcon("kdenlive-hide-video"));
69     } else {
70         buttonVideo->setHidden(true);
71     }
72     if (!info.isMute)
73         buttonAudio->setIcon(KIcon("kdenlive-show-audio"));
74     else
75         buttonAudio->setIcon(KIcon("kdenlive-hide-audio"));
76
77     if (!info.isLocked)
78         buttonLock->setIcon(KIcon("kdenlive-unlock"));
79     else
80         buttonLock->setIcon(KIcon("kdenlive-lock"));
81
82     connect(buttonVideo, SIGNAL(clicked()), this, SLOT(switchVideo()));
83     connect(buttonAudio, SIGNAL(clicked()), this, SLOT(switchAudio()));
84     connect(buttonLock, SIGNAL(clicked()), this, SLOT(switchLock()));
85
86     // Don't show track buttons if size is too small
87     if (height < 40) {
88         buttonVideo->setHidden(true);
89         buttonAudio->setHidden(true);
90         buttonLock->setHidden(true);
91         //horizontalSpacer;
92     }
93
94     setContextMenuPolicy(Qt::DefaultContextMenu); //Qt::ActionsContextMenu);
95     QAction *insertAction = new QAction(i18n("Insert Track"), this);
96     m_menu.addAction(insertAction);
97     connect(insertAction, SIGNAL(triggered()), this, SLOT(slotAddTrack()));
98
99     QAction *removeAction = new QAction(KIcon("edit-delete"), i18n("Delete Track"), this);
100     m_menu.addAction(removeAction);
101     connect(removeAction, SIGNAL(triggered()), this, SLOT(slotDeleteTrack()));
102
103     QAction *configAction = new QAction(KIcon("configure"), i18n("Configure Track"), this);
104     m_menu.addAction(configAction);
105     connect(configAction, SIGNAL(triggered()), this, SLOT(slotConfigTrack()));
106 }
107
108 /*HeaderTrack::~HeaderTrack()
109 {
110 }*/
111
112 // virtual
113 void HeaderTrack::mousePressEvent(QMouseEvent * event)
114 {
115     if (track_number->hasFocus()) {
116         track_number->clearFocus();
117         return;
118     }
119     if (!m_isSelected) emit selectTrack(m_index);
120     QWidget::mousePressEvent(event);
121 }
122
123 // virtual
124 void HeaderTrack::contextMenuEvent(QContextMenuEvent * event)
125 {
126     if (track_number->hasFocus()) {
127         track_number->clearFocus();
128         return;
129     }
130     m_menu.popup(event->globalPos());
131 }
132
133 void HeaderTrack::mouseDoubleClickEvent(QMouseEvent* event)
134 {
135     if (track_number->hasFocus()) {
136         track_number->clearFocus();
137         return;
138     }
139     slotConfigTrack();
140     QWidget::mouseDoubleClickEvent(event);
141 }
142
143 void HeaderTrack::setSelectedIndex(int ix)
144 {
145     if (m_index == ix) {
146         m_isSelected = true;
147         setBackgroundRole(QPalette::Button);
148         setAutoFillBackground(true);
149     } else if (m_type != VIDEOTRACK) {
150         m_isSelected = false;
151         setAutoFillBackground(false);
152     } else {
153         m_isSelected = false;
154         setBackgroundRole(QPalette::AlternateBase);
155     }
156     update();
157 }
158
159 void HeaderTrack::adjustSize(int height)
160 {
161     // Don't show track buttons if size is too small
162     bool smallTracks = height < 40;
163     if (m_type == VIDEOTRACK)
164         buttonVideo->setHidden(smallTracks);
165     buttonAudio->setHidden(smallTracks);
166     buttonLock->setHidden(smallTracks);
167     setFixedHeight(height);
168 }
169
170 void HeaderTrack::switchVideo()
171 {
172     if (buttonVideo->isChecked())
173         buttonVideo->setIcon(KIcon("kdenlive-hide-video"));
174     else
175         buttonVideo->setIcon(KIcon("kdenlive-show-video"));
176     emit switchTrackVideo(m_index);
177 }
178
179 void HeaderTrack::switchAudio()
180 {
181     if (buttonAudio->isChecked())
182         buttonAudio->setIcon(KIcon("kdenlive-hide-audio"));
183     else
184         buttonAudio->setIcon(KIcon("kdenlive-show-audio"));
185     emit switchTrackAudio(m_index);
186 }
187
188 void HeaderTrack::switchLock(bool emitSignal)
189 {
190     if (buttonLock->isChecked())
191         buttonLock->setIcon(KIcon("kdenlive-lock"));
192     else
193         buttonLock->setIcon(KIcon("kdenlive-unlock"));
194     if (emitSignal)
195         emit switchTrackLock(m_index);
196 }
197
198 void HeaderTrack::setLock(bool lock)
199 {
200     buttonLock->setChecked(lock);
201     switchLock(false);
202 }
203
204 void HeaderTrack::slotDeleteTrack()
205 {
206     QTimer::singleShot(500, this, SLOT(deleteTrack()));
207 }
208
209 void HeaderTrack::deleteTrack()
210 {
211     emit deleteTrack(m_index);
212 }
213
214 void HeaderTrack::slotAddTrack()
215 {
216     emit insertTrack(m_index);
217 }
218
219 void HeaderTrack::slotRenameTrack()
220 {
221     if (m_name != track_number->text()) emit renameTrack(m_index, track_number->text());
222 }
223
224 void HeaderTrack::slotConfigTrack()
225 {
226     emit configTrack(m_index);
227 }
228
229
230 #include "headertrack.moc"