]> git.sesse.net Git - kdenlive/blob - src/clipdurationdialog.cpp
code cleanup
[kdenlive] / src / clipdurationdialog.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 "clipdurationdialog.h"
22 #include "clipitem.h"
23 #include "kdenlivesettings.h"
24
25 #include <KDebug>
26 #include <KMessageBox>
27
28 #include <QWheelEvent>
29
30 ClipDurationDialog::ClipDurationDialog(AbstractClipItem *clip, Timecode tc, GenTime min, GenTime max, QWidget * parent):
31         QDialog(parent),
32         m_clip(clip),
33         m_tc(tc),
34         m_min(min),
35         m_max(max),
36         m_framesDisplay(KdenliveSettings::frametimecode())
37 {
38     setFont(KGlobalSettings::toolBarFont());
39     m_fps = m_tc.fps();
40     m_view.setupUi(this);
41
42     bool allowCrop = true;
43     if (clip->type() == AVWIDGET) {
44         ClipItem *item = static_cast <ClipItem *>(clip);
45         int t = item->clipType();
46         if (t == COLOR || t == IMAGE || t == TEXT) allowCrop = false;
47     }
48
49     if (!allowCrop || clip->type() == TRANSITIONWIDGET) {
50         m_view.crop_up->hide();
51         m_view.crop_down->hide();
52         m_view.crop_position->hide();
53         m_view.crop_label->hide();
54         m_view.end_up->hide();
55         m_view.end_down->hide();
56         m_view.end_position->hide();
57         m_view.end_label->hide();
58     }
59
60     m_crop = m_clip->cropStart().frames(m_fps);
61
62     if (m_framesDisplay) {
63         QValidator *valid = new QIntValidator();
64         m_view.clip_position->setInputMask("");
65         m_view.clip_position->setValidator(valid);
66         m_view.crop_position->setInputMask("");
67         m_view.clip_position->setValidator(valid);
68         m_view.clip_duration->setInputMask("");
69         m_view.clip_position->setValidator(valid);
70         m_view.end_position->setInputMask("");
71         m_view.clip_position->setValidator(valid);
72     }
73     m_view.clip_position->setText(tc.getDisplayTimecode(m_clip->startPos(), m_framesDisplay));
74     m_view.crop_position->setText(tc.getDisplayTimecode(m_clip->cropStart(), m_framesDisplay));
75     m_view.clip_duration->setText(tc.getDisplayTimecode(m_clip->cropDuration(), m_framesDisplay));
76     m_view.end_position->setText(tc.getDisplayTimecode(m_clip->maxDuration() - m_clip->cropDuration() - m_clip->cropStart(), m_framesDisplay));
77
78     connect(m_view.position_up, SIGNAL(clicked()), this, SLOT(slotPosUp()));
79     connect(m_view.position_down, SIGNAL(clicked()), this, SLOT(slotPosDown()));
80     connect(m_view.crop_up, SIGNAL(clicked()), this, SLOT(slotCropUp()));
81     connect(m_view.crop_down, SIGNAL(clicked()), this, SLOT(slotCropDown()));
82     connect(m_view.duration_up, SIGNAL(clicked()), this, SLOT(slotDurUp()));
83     connect(m_view.duration_down, SIGNAL(clicked()), this, SLOT(slotDurDown()));
84     connect(m_view.end_up, SIGNAL(clicked()), this, SLOT(slotEndUp()));
85     connect(m_view.end_down, SIGNAL(clicked()), this, SLOT(slotEndDown()));
86     connect(m_view.crop_position, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckCrop()));
87     connect(m_view.clip_duration, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckDuration()));
88     connect(m_view.clip_position, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckStart()));
89     connect(m_view.end_position, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckEnd()));
90     adjustSize();
91 }
92
93 ClipDurationDialog::~ClipDurationDialog()
94 {
95 }
96
97 void ClipDurationDialog::slotCheckStart()
98 {
99     int pos = m_tc.getDisplayFrameCount(m_view.clip_position->text(), m_framesDisplay);
100     int dur = m_tc.getDisplayFrameCount(m_view.clip_duration->text(), m_framesDisplay);
101
102     GenTime start(pos, m_fps);
103     GenTime duration(dur, m_fps);
104     if (m_min != GenTime() && start < m_min) {
105         m_view.clip_position->setText(m_tc.getDisplayTimecode(m_min, m_framesDisplay));
106     } else if (m_max != GenTime() && start + duration > m_max) {
107         m_view.clip_position->setText(m_tc.getDisplayTimecode(m_max - duration, m_framesDisplay));
108     }
109 }
110
111 void ClipDurationDialog::slotCheckDuration()
112 {
113     int pos = m_tc.getDisplayFrameCount(m_view.clip_position->text(), m_framesDisplay);
114     int dur = m_tc.getDisplayFrameCount(m_view.clip_duration->text(), m_framesDisplay);
115     int crop = m_tc.getDisplayFrameCount(m_view.crop_position->text(), m_framesDisplay);
116
117     GenTime start(pos, m_fps);
118     GenTime duration(dur, m_fps);
119     GenTime cropStart(crop, m_fps);
120     GenTime maxDuration;
121     if (m_clip->maxDuration() == GenTime()) maxDuration = m_max;
122     else maxDuration = m_max == GenTime() ? start + m_clip->maxDuration() - cropStart : qMin(m_max, start + m_clip->maxDuration() - cropStart);
123     if (maxDuration != GenTime() && start + duration > maxDuration) {
124         m_view.clip_duration->blockSignals(true);
125         m_view.clip_duration->setText(m_tc.getDisplayTimecode(maxDuration - start, m_framesDisplay));
126         m_view.clip_duration->blockSignals(false);
127     }
128
129     dur = m_tc.getDisplayFrameCount(m_view.clip_duration->text(), m_framesDisplay);
130     GenTime durationUp(dur, m_fps);
131     m_view.end_position->blockSignals(true);
132     m_view.end_position->setText(m_tc.getDisplayTimecode(m_clip->maxDuration() - durationUp - cropStart, m_framesDisplay));
133     m_view.end_position->blockSignals(false);
134 }
135
136 void ClipDurationDialog::slotCheckCrop()
137 {
138     int dur = m_tc.getDisplayFrameCount(m_view.clip_duration->text(), m_framesDisplay);
139     int crop = m_tc.getDisplayFrameCount(m_view.crop_position->text(), m_framesDisplay);
140
141     int diff = crop - m_crop;
142     if ((diff > 0 && diff < dur) || diff < 0) {
143         dur -= diff;
144     } else {
145         m_view.crop_position->setText(m_tc.getDisplayTimecode(GenTime(m_crop, m_fps), m_framesDisplay));
146         return;
147     }
148     GenTime duration(dur, m_fps);
149     GenTime cropStart(crop, m_fps);
150     GenTime maxDuration = m_clip->maxDuration();
151     if (maxDuration != GenTime() && cropStart + duration > maxDuration) {
152         m_view.crop_position->setText(m_tc.getDisplayTimecode(GenTime(m_crop, m_fps), m_framesDisplay));
153     } else {
154         m_crop = crop;
155         m_view.clip_duration->blockSignals(true);
156         m_view.clip_duration->setText(m_tc.getDisplayTimecode(duration, m_framesDisplay));
157         m_view.clip_duration->blockSignals(false);
158     }
159 }
160
161 void ClipDurationDialog::slotCheckEnd()
162 {
163     int crop = m_tc.getDisplayFrameCount(m_view.crop_position->text(), m_framesDisplay);
164     int end = m_tc.getDisplayFrameCount(m_view.end_position->text(), m_framesDisplay);
165     int dur = m_clip->maxDuration().frames(m_fps) - crop - end;
166
167     if (dur >= 0) {
168         m_view.clip_duration->setText(m_tc.getDisplayTimecode(GenTime(dur, m_fps), m_framesDisplay));
169     } else {
170         dur = m_tc.getFrameCount(m_view.clip_duration->text());
171         m_view.end_position->blockSignals(true);
172         m_view.end_position->setText(m_tc.getDisplayTimecode(m_clip->maxDuration() - GenTime(crop + dur, m_fps), m_framesDisplay));
173         m_view.end_position->blockSignals(false);
174     }
175 }
176
177 void ClipDurationDialog::slotPosUp()
178 {
179     int position = m_tc.getDisplayFrameCount(m_view.clip_position->text(), m_framesDisplay);
180     //if (duration >= m_clip->duration().frames(m_fps)) return;
181     position ++;
182     m_view.clip_position->setText(m_tc.getDisplayTimecode(GenTime(position, m_fps), m_framesDisplay));
183 }
184
185 void ClipDurationDialog::slotPosDown()
186 {
187     int position = m_tc.getDisplayFrameCount(m_view.clip_position->text(), m_framesDisplay);
188     //if (duration >= m_clip->duration().frames(m_fps)) return;
189     position --;
190     m_view.clip_position->setText(m_tc.getDisplayTimecode(GenTime(position, m_fps), m_framesDisplay));
191 }
192
193 void ClipDurationDialog::slotDurUp()
194 {
195     int duration = m_tc.getDisplayFrameCount(m_view.clip_duration->text(), m_framesDisplay);
196     int crop = m_tc.getDisplayFrameCount(m_view.crop_position->text(), m_framesDisplay);
197
198     if (m_clip->maxDuration() != GenTime() && duration + crop > m_clip->maxDuration().frames(m_fps)) return;
199     duration ++;
200     m_view.clip_duration->setText(m_tc.getDisplayTimecode(GenTime(duration, m_fps), m_framesDisplay));
201 }
202
203 void ClipDurationDialog::slotDurDown()
204 {
205     int duration = m_tc.getDisplayFrameCount(m_view.clip_duration->text(), m_framesDisplay);
206     if (duration <= 0) return;
207     duration --;
208     m_view.clip_duration->setText(m_tc.getDisplayTimecode(GenTime(duration, m_fps), m_framesDisplay));
209 }
210
211 void ClipDurationDialog::slotCropUp()
212 {
213     int duration = m_tc.getDisplayFrameCount(m_view.clip_duration->text(), m_framesDisplay);
214     int crop = m_tc.getDisplayFrameCount(m_view.crop_position->text(), m_framesDisplay);
215
216     if (m_clip->maxDuration() != GenTime() && duration + crop > m_clip->maxDuration().frames(m_fps)) return;
217     crop ++;
218     m_view.crop_position->setText(m_tc.getDisplayTimecode(GenTime(crop, m_fps), m_framesDisplay));
219 }
220
221 void ClipDurationDialog::slotCropDown()
222 {
223     int crop = m_tc.getDisplayFrameCount(m_view.crop_position->text(), m_framesDisplay);
224
225     if (crop <= 0) return;
226     crop --;
227     m_view.crop_position->setText(m_tc.getDisplayTimecode(GenTime(crop, m_fps), m_framesDisplay));
228 }
229
230 void ClipDurationDialog::slotEndUp()
231 {
232     int end = m_tc.getDisplayFrameCount(m_view.end_position->text(), m_framesDisplay);
233     end ++;
234     m_view.end_position->setText(m_tc.getDisplayTimecode(GenTime(end, m_fps), m_framesDisplay));
235 }
236
237 void ClipDurationDialog::slotEndDown()
238 {
239     int end = m_tc.getDisplayFrameCount(m_view.end_position->text(), m_framesDisplay);
240     if (end <= 0) return;
241     end --;
242     m_view.end_position->setText(m_tc.getDisplayTimecode(GenTime(end, m_fps), m_framesDisplay));
243 }
244
245 GenTime ClipDurationDialog::startPos() const
246 {
247     int pos = m_tc.getDisplayFrameCount(m_view.clip_position->text(), m_framesDisplay);
248     return GenTime(pos, m_fps);
249 }
250
251 GenTime ClipDurationDialog::cropStart() const
252 {
253     int pos = m_tc.getDisplayFrameCount(m_view.crop_position->text(), m_framesDisplay);
254     return GenTime(pos, m_fps);
255 }
256
257 GenTime ClipDurationDialog::duration() const
258 {
259     int pos = m_tc.getDisplayFrameCount(m_view.clip_duration->text(), m_framesDisplay);
260     return GenTime(pos, m_fps);
261 }
262
263 void ClipDurationDialog::wheelEvent(QWheelEvent * event)
264 {
265     if (m_view.clip_position->underMouse()) {
266         if (event->delta() > 0)
267             slotPosUp();
268         else
269             slotPosDown();
270     } else if (m_view.clip_duration->underMouse()) {
271         if (event->delta() > 0)
272             slotDurUp();
273         else
274             slotDurDown();
275     } else if (m_view.crop_position->underMouse()) {
276         if (event->delta() > 0)
277             slotCropUp();
278         else
279             slotCropDown();
280     } else if (m_view.end_position->underMouse()) {
281         if (event->delta() > 0)
282             slotEndUp();
283         else
284             slotEndDown();
285     }
286 }
287
288 #include "clipdurationdialog.moc"
289
290