]> git.sesse.net Git - kdenlive/blob - src/clipdurationdialog.cpp
Add crop end info to clip info dialog (based on a patch by Till Theato):
[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 {
37     setFont(KGlobalSettings::toolBarFont());
38     m_fps = m_tc.fps();
39     m_view.setupUi(this);
40
41     bool allowCrop = true;
42     if (clip->type() == AVWIDGET) {
43         ClipItem *item = static_cast <ClipItem *>(clip);
44         int t = item->clipType();
45         if (t == COLOR || t == IMAGE || t == TEXT) allowCrop = false;
46     }
47
48     if (!allowCrop || clip->type() == TRANSITIONWIDGET) {
49         m_view.crop_up->hide();
50         m_view.crop_down->hide();
51         m_view.crop_position->hide();
52         m_view.crop_label->hide();
53         m_view.end_up->hide();
54         m_view.end_down->hide();
55         m_view.end_position->hide();
56         m_view.end_label->hide();
57     }
58
59     m_crop = m_clip->cropStart().frames(m_fps);
60
61     if (KdenliveSettings::frametimecode()) {
62         QValidator *valid = new QIntValidator();
63         m_view.clip_position->setInputMask("");
64         m_view.clip_position->setValidator(valid);
65         m_view.clip_position->setText(QString::number(m_clip->startPos().frames(m_fps)));
66         m_view.crop_position->setInputMask("");
67         m_view.clip_position->setValidator(valid);
68         m_view.crop_position->setText(QString::number(m_clip->cropStart().frames(m_fps)));
69         m_view.clip_duration->setInputMask("");
70         m_view.clip_position->setValidator(valid);
71         m_view.clip_duration->setText(QString::number(m_clip->cropDuration().frames(m_fps)));
72         m_view.end_position->setInputMask("");
73         m_view.clip_position->setValidator(valid);
74         m_view.end_position->setText(QString::number((m_clip->maxDuration() - m_clip->cropDuration() - m_clip->cropStart()).frames(m_fps)));
75     } else {
76         m_view.clip_position->setText(tc.getTimecode(m_clip->startPos()));
77         m_view.crop_position->setText(tc.getTimecode(m_clip->cropStart()));
78         m_view.clip_duration->setText(tc.getTimecode(m_clip->cropDuration()));
79         m_view.end_position->setText(tc.getTimecode(m_clip->maxDuration() - m_clip->cropDuration() - m_clip->cropStart()));
80     }
81     connect(m_view.position_up, SIGNAL(clicked()), this, SLOT(slotPosUp()));
82     connect(m_view.position_down, SIGNAL(clicked()), this, SLOT(slotPosDown()));
83     connect(m_view.crop_up, SIGNAL(clicked()), this, SLOT(slotCropUp()));
84     connect(m_view.crop_down, SIGNAL(clicked()), this, SLOT(slotCropDown()));
85     connect(m_view.duration_up, SIGNAL(clicked()), this, SLOT(slotDurUp()));
86     connect(m_view.duration_down, SIGNAL(clicked()), this, SLOT(slotDurDown()));
87     connect(m_view.end_up, SIGNAL(clicked()), this, SLOT(slotEndUp()));
88     connect(m_view.end_down, SIGNAL(clicked()), this, SLOT(slotEndDown()));
89     connect(m_view.crop_position, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckCrop()));
90     connect(m_view.clip_duration, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckDuration()));
91     connect(m_view.clip_position, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckStart()));
92     connect(m_view.end_position, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckEnd()));
93     adjustSize();
94 }
95
96 ClipDurationDialog::~ClipDurationDialog()
97 {
98 }
99
100 void ClipDurationDialog::slotCheckStart()
101 {
102     int pos;
103     int dur;
104     if (KdenliveSettings::frametimecode()) {
105         pos = m_view.clip_position->text().toInt();
106         dur = m_view.clip_duration->text().toInt();
107     } else {
108         pos = m_tc.getFrameCount(m_view.clip_position->text());
109         dur = m_tc.getFrameCount(m_view.clip_duration->text());
110     }
111
112     GenTime start(pos, m_fps);
113     GenTime duration(dur, m_fps);
114     if (m_min != GenTime() && start < m_min) {
115         if (KdenliveSettings::frametimecode()) m_view.clip_position->setText(QString::number(m_min.frames(m_fps)));
116         else m_view.clip_position->setText(m_tc.getTimecode(m_min));
117     } else if (m_max != GenTime() && start + duration > m_max) {
118         if (KdenliveSettings::frametimecode()) m_view.clip_position->setText(QString::number((m_max - duration).frames(m_fps)));
119         else m_view.clip_position->setText(m_tc.getTimecode(m_max - duration));
120     }
121 }
122
123 void ClipDurationDialog::slotCheckDuration()
124 {
125     int pos;
126     int dur;
127     int crop;
128     if (KdenliveSettings::frametimecode()) {
129         pos = m_view.clip_position->text().toInt();
130         dur = m_view.clip_duration->text().toInt();
131         crop = m_view.crop_position->text().toInt();
132     } else {
133         pos = m_tc.getFrameCount(m_view.clip_position->text());
134         dur = m_tc.getFrameCount(m_view.clip_duration->text());
135         crop = m_tc.getFrameCount(m_view.crop_position->text());
136     }
137
138     GenTime start(pos, m_fps);
139     GenTime duration(dur, m_fps);
140     GenTime cropStart(crop, m_fps);
141     GenTime maxDuration;
142     if (m_clip->maxDuration() == GenTime()) maxDuration = m_max;
143     else maxDuration = m_max == GenTime() ? start + m_clip->maxDuration() - cropStart : qMin(m_max, start + m_clip->maxDuration() - cropStart);
144     if (maxDuration != GenTime() && start + duration > maxDuration) {
145         m_view.clip_duration->blockSignals(true);
146         if (KdenliveSettings::frametimecode()) m_view.clip_duration->setText(QString::number((maxDuration - start).frames(m_fps)));
147         else m_view.clip_duration->setText(m_tc.getTimecode(maxDuration - start));
148         m_view.clip_duration->blockSignals(false);
149     }
150
151     if (KdenliveSettings::frametimecode()) dur = m_view.clip_duration->text().toInt();
152     else dur = m_tc.getFrameCount(m_view.clip_duration->text());
153     GenTime durationUp(dur, m_fps);
154     m_view.end_position->blockSignals(true);
155     if (KdenliveSettings::frametimecode()) m_view.end_position->setText(QString::number((m_clip->maxDuration() - durationUp - cropStart).frames(m_fps)));
156     else m_view.end_position->setText(m_tc.getTimecode(m_clip->maxDuration() - durationUp - cropStart));
157     m_view.end_position->blockSignals(false);
158 }
159
160 void ClipDurationDialog::slotCheckCrop()
161 {
162     int dur;
163     int crop;
164     if (KdenliveSettings::frametimecode()) {
165         dur = m_view.clip_duration->text().toInt();
166         crop = m_view.crop_position->text().toInt();
167     } else {
168         dur = m_tc.getFrameCount(m_view.clip_duration->text());
169         crop = m_tc.getFrameCount(m_view.crop_position->text());
170     }
171     int diff = crop - m_crop;
172     if ((diff > 0 && diff < dur) || diff < 0) {
173         dur -= diff;
174     } else {
175         if (KdenliveSettings::frametimecode()) m_view.crop_position->setText(QString::number(m_crop));
176         else m_view.crop_position->setText(m_tc.getTimecode(GenTime(m_crop, m_fps)));
177         return;
178     }
179     GenTime duration(dur, m_fps);
180     GenTime cropStart(crop, m_fps);
181     GenTime maxDuration = m_clip->maxDuration();
182     if (maxDuration != GenTime() && cropStart + duration > maxDuration) {
183         if (KdenliveSettings::frametimecode()) m_view.crop_position->setText(QString::number(m_crop));
184         else m_view.crop_position->setText(m_tc.getTimecode(GenTime(m_crop, m_fps)));
185     } else {
186         m_crop = crop;
187         m_view.clip_duration->blockSignals(true);
188         if (KdenliveSettings::frametimecode()) m_view.clip_duration->setText(QString::number(duration.frames(m_fps)));
189         else m_view.clip_duration->setText(m_tc.getTimecode(duration));
190         m_view.clip_duration->blockSignals(false);
191     }
192 }
193
194 void ClipDurationDialog::slotCheckEnd()
195 {
196     int crop;
197     int end;
198     int dur;
199     if (KdenliveSettings::frametimecode()) {
200         crop = m_view.crop_position->text().toInt();
201         end = m_view.end_position->text().toInt();
202         dur = m_clip->maxDuration().frames(m_fps) - crop - end;
203     } else {
204         crop = m_tc.getFrameCount(m_view.crop_position->text());
205         end = m_tc.getFrameCount(m_view.end_position->text());
206         dur = m_tc.getFrameCount(m_tc.getTimecode(m_clip->maxDuration())) - crop - end;
207     }
208     if (dur >= 0) {
209         if (KdenliveSettings::frametimecode()) m_view.clip_duration->setText(QString::number(dur));
210         else m_view.clip_duration->setText(m_tc.getTimecode(GenTime(dur, m_fps)));
211     } else {
212         dur = m_tc.getFrameCount(m_view.clip_duration->text());
213         m_view.end_position->blockSignals(true);
214         if (KdenliveSettings::frametimecode()) m_view.end_position->setText(QString::number(m_clip->maxDuration().frames(m_fps) - (crop + dur)));
215         else m_view.end_position->setText(m_tc.getTimecode(m_clip->maxDuration() - GenTime(crop + dur, m_fps)));
216         m_view.end_position->blockSignals(false);
217     }
218 }
219
220 void ClipDurationDialog::slotPosUp()
221 {
222     int position;
223     if (KdenliveSettings::frametimecode()) position = m_view.clip_position->text().toInt();
224     else position = m_tc.getFrameCount(m_view.clip_position->text());
225     //if (duration >= m_clip->duration().frames(m_fps)) return;
226     position ++;
227     if (KdenliveSettings::frametimecode()) m_view.clip_position->setText(QString::number(position));
228     else m_view.clip_position->setText(m_tc.getTimecode(GenTime(position, m_fps)));
229 }
230
231 void ClipDurationDialog::slotPosDown()
232 {
233     int position;
234     if (KdenliveSettings::frametimecode()) position = m_view.clip_position->text().toInt();
235     else position = m_tc.getFrameCount(m_view.clip_position->text());
236     //if (duration >= m_clip->duration().frames(m_fps)) return;
237     position --;
238     if (KdenliveSettings::frametimecode()) m_view.clip_position->setText(QString::number(position));
239     else m_view.clip_position->setText(m_tc.getTimecode(GenTime(position, m_fps)));
240 }
241
242 void ClipDurationDialog::slotDurUp()
243 {
244     int duration;
245     int crop;
246     if (KdenliveSettings::frametimecode()) {
247         duration = m_view.clip_duration->text().toInt();
248         crop = m_view.crop_position->text().toInt();
249     } else {
250         duration = m_tc.getFrameCount(m_view.clip_duration->text());
251         crop = m_tc.getFrameCount(m_view.crop_position->text());
252     }
253     if (m_clip->maxDuration() != GenTime() && duration + crop > m_clip->maxDuration().frames(m_fps)) return;
254     duration ++;
255     if (KdenliveSettings::frametimecode()) m_view.clip_duration->setText(QString::number(duration));
256     else m_view.clip_duration->setText(m_tc.getTimecode(GenTime(duration, m_fps)));
257 }
258
259 void ClipDurationDialog::slotDurDown()
260 {
261     int duration;
262     if (KdenliveSettings::frametimecode()) {
263         duration = m_view.clip_duration->text().toInt();
264     } else {
265         duration = m_tc.getFrameCount(m_view.clip_duration->text());
266     }
267     if (duration <= 0) return;
268     duration --;
269     if (KdenliveSettings::frametimecode()) m_view.clip_duration->setText(QString::number(duration));
270     else m_view.clip_duration->setText(m_tc.getTimecode(GenTime(duration, m_fps)));
271 }
272
273 void ClipDurationDialog::slotCropUp()
274 {
275     int duration;
276     int crop;
277     if (KdenliveSettings::frametimecode()) {
278         duration = m_view.clip_duration->text().toInt();
279         crop = m_view.crop_position->text().toInt();
280     } else {
281         duration = m_tc.getFrameCount(m_view.clip_duration->text());
282         crop = m_tc.getFrameCount(m_view.crop_position->text());
283     }
284
285     if (m_clip->maxDuration() != GenTime() && duration + crop > m_clip->maxDuration().frames(m_fps)) return;
286     crop ++;
287     if (KdenliveSettings::frametimecode()) m_view.crop_position->setText(QString::number(crop));
288     else m_view.crop_position->setText(m_tc.getTimecode(GenTime(crop, m_fps)));
289 }
290
291 void ClipDurationDialog::slotCropDown()
292 {
293     int crop;
294     if (KdenliveSettings::frametimecode()) {
295         crop = m_view.crop_position->text().toInt();
296     } else {
297         crop = m_tc.getFrameCount(m_view.crop_position->text());
298     }
299
300     if (crop <= 0) return;
301     crop --;
302     if (KdenliveSettings::frametimecode()) m_view.crop_position->setText(QString::number(crop));
303     else m_view.crop_position->setText(m_tc.getTimecode(GenTime(crop, m_fps)));
304 }
305
306 void ClipDurationDialog::slotEndUp()
307 {
308     int end;
309     if (KdenliveSettings::frametimecode()) end = m_view.end_position->text().toInt();
310     else end = m_tc.getFrameCount(m_view.end_position->text());
311     kDebug() << "/ / / / FIRST END: " << end;
312     end ++;
313     if (KdenliveSettings::frametimecode()) m_view.end_position->setText(QString::number(end));
314     else m_view.end_position->setText(m_tc.getTimecode(GenTime(end, m_fps)));
315     kDebug() << "/ / / / SEC END: " << end;
316 }
317
318 void ClipDurationDialog::slotEndDown()
319 {
320     int end;
321     if (KdenliveSettings::frametimecode()) end = m_view.end_position->text().toInt();
322     else end = m_tc.getFrameCount(m_view.end_position->text());
323     if (end <= 0) return;
324     end --;
325     if (KdenliveSettings::frametimecode()) m_view.end_position->setText(QString::number(end));
326     else m_view.end_position->setText(m_tc.getTimecode(GenTime(end, m_fps)));
327 }
328
329 GenTime ClipDurationDialog::startPos() const
330 {
331     int pos;
332     if (KdenliveSettings::frametimecode()) pos = m_view.clip_position->text().toInt();
333     else pos = m_tc.getFrameCount(m_view.clip_position->text());
334     return GenTime(pos, m_fps);
335 }
336
337 GenTime ClipDurationDialog::cropStart() const
338 {
339     int pos;
340     if (KdenliveSettings::frametimecode()) pos = m_view.crop_position->text().toInt();
341     else pos = m_tc.getFrameCount(m_view.crop_position->text());
342     return GenTime(pos, m_fps);
343 }
344
345 GenTime ClipDurationDialog::duration() const
346 {
347     int pos;
348     if (KdenliveSettings::frametimecode()) pos = m_view.clip_duration->text().toInt();
349     else pos = m_tc.getFrameCount(m_view.clip_duration->text());
350     return GenTime(pos, m_fps);
351 }
352
353 void ClipDurationDialog::wheelEvent(QWheelEvent * event)
354 {
355     if (m_view.clip_position->underMouse()) {
356         if (event->delta() > 0)
357             slotPosUp();
358         else
359             slotPosDown();
360     } else if (m_view.clip_duration->underMouse()) {
361         if (event->delta() > 0)
362             slotDurUp();
363         else
364             slotDurDown();
365     } else if (m_view.crop_position->underMouse()) {
366         if (event->delta() > 0)
367             slotCropUp();
368         else
369             slotCropDown();
370     } else if (m_view.end_position->underMouse()) {
371         if (event->delta() > 0)
372             slotEndUp();
373         else
374             slotEndDown();
375     }
376 }
377
378 #include "clipdurationdialog.moc"
379
380