1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "clipdurationdialog.h"
23 #include "kdenlivesettings.h"
26 #include <KMessageBox>
28 #include <QWheelEvent>
30 ClipDurationDialog::ClipDurationDialog(AbstractClipItem *clip, Timecode tc, GenTime min, GenTime max, QWidget * parent):
37 setFont(KGlobalSettings::toolBarFont());
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;
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();
59 m_crop = m_clip->cropStart().frames(m_fps);
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)));
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()));
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()));
96 ClipDurationDialog::~ClipDurationDialog()
100 void ClipDurationDialog::slotCheckStart()
104 if (KdenliveSettings::frametimecode()) {
105 pos = m_view.clip_position->text().toInt();
106 dur = m_view.clip_duration->text().toInt();
108 pos = m_tc.getFrameCount(m_view.clip_position->text());
109 dur = m_tc.getFrameCount(m_view.clip_duration->text());
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));
123 void ClipDurationDialog::slotCheckDuration()
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();
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());
138 GenTime start(pos, m_fps);
139 GenTime duration(dur, m_fps);
140 GenTime cropStart(crop, m_fps);
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);
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);
160 void ClipDurationDialog::slotCheckCrop()
164 if (KdenliveSettings::frametimecode()) {
165 dur = m_view.clip_duration->text().toInt();
166 crop = m_view.crop_position->text().toInt();
168 dur = m_tc.getFrameCount(m_view.clip_duration->text());
169 crop = m_tc.getFrameCount(m_view.crop_position->text());
171 int diff = crop - m_crop;
172 if ((diff > 0 && diff < dur) || diff < 0) {
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)));
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)));
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);
194 void ClipDurationDialog::slotCheckEnd()
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;
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;
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)));
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);
220 void ClipDurationDialog::slotPosUp()
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;
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)));
231 void ClipDurationDialog::slotPosDown()
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;
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)));
242 void ClipDurationDialog::slotDurUp()
246 if (KdenliveSettings::frametimecode()) {
247 duration = m_view.clip_duration->text().toInt();
248 crop = m_view.crop_position->text().toInt();
250 duration = m_tc.getFrameCount(m_view.clip_duration->text());
251 crop = m_tc.getFrameCount(m_view.crop_position->text());
253 if (m_clip->maxDuration() != GenTime() && duration + crop > m_clip->maxDuration().frames(m_fps)) return;
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)));
259 void ClipDurationDialog::slotDurDown()
262 if (KdenliveSettings::frametimecode()) {
263 duration = m_view.clip_duration->text().toInt();
265 duration = m_tc.getFrameCount(m_view.clip_duration->text());
267 if (duration <= 0) return;
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)));
273 void ClipDurationDialog::slotCropUp()
277 if (KdenliveSettings::frametimecode()) {
278 duration = m_view.clip_duration->text().toInt();
279 crop = m_view.crop_position->text().toInt();
281 duration = m_tc.getFrameCount(m_view.clip_duration->text());
282 crop = m_tc.getFrameCount(m_view.crop_position->text());
285 if (m_clip->maxDuration() != GenTime() && duration + crop > m_clip->maxDuration().frames(m_fps)) return;
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)));
291 void ClipDurationDialog::slotCropDown()
294 if (KdenliveSettings::frametimecode()) {
295 crop = m_view.crop_position->text().toInt();
297 crop = m_tc.getFrameCount(m_view.crop_position->text());
300 if (crop <= 0) return;
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)));
306 void ClipDurationDialog::slotEndUp()
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;
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;
318 void ClipDurationDialog::slotEndDown()
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;
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)));
329 GenTime ClipDurationDialog::startPos() const
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);
337 GenTime ClipDurationDialog::cropStart() const
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);
345 GenTime ClipDurationDialog::duration() const
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);
353 void ClipDurationDialog::wheelEvent(QWheelEvent * event)
355 if (m_view.clip_position->underMouse()) {
356 if (event->delta() > 0)
360 } else if (m_view.clip_duration->underMouse()) {
361 if (event->delta() > 0)
365 } else if (m_view.crop_position->underMouse()) {
366 if (event->delta() > 0)
370 } else if (m_view.end_position->underMouse()) {
371 if (event->delta() > 0)
378 #include "clipdurationdialog.moc"