]> git.sesse.net Git - kdenlive/blob - src/clipdurationdialog.cpp
When editing clip properties, changing crop start should not move the clip end, based...
[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     }
54
55     m_crop = m_clip->cropStart().frames(m_fps);
56     m_view.clip_position->setText(tc.getTimecode(m_clip->startPos()));
57     m_view.crop_position->setText(tc.getTimecode(m_clip->cropStart()));
58     m_view.clip_duration->setText(tc.getTimecode(m_clip->cropDuration()));
59     connect(m_view.position_up, SIGNAL(clicked()), this, SLOT(slotPosUp()));
60     connect(m_view.position_down, SIGNAL(clicked()), this, SLOT(slotPosDown()));
61     connect(m_view.crop_up, SIGNAL(clicked()), this, SLOT(slotCropUp()));
62     connect(m_view.crop_down, SIGNAL(clicked()), this, SLOT(slotCropDown()));
63     connect(m_view.duration_up, SIGNAL(clicked()), this, SLOT(slotDurUp()));
64     connect(m_view.duration_down, SIGNAL(clicked()), this, SLOT(slotDurDown()));
65     connect(m_view.crop_position, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckCrop()));
66     connect(m_view.clip_duration, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckDuration()));
67     connect(m_view.clip_position, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckStart()));
68     adjustSize();
69 }
70
71 ClipDurationDialog::~ClipDurationDialog()
72 {
73 }
74
75 void ClipDurationDialog::slotCheckStart()
76 {
77     int pos = m_tc.getFrameCount(m_view.clip_position->text());
78     int dur = m_tc.getFrameCount(m_view.clip_duration->text());
79     GenTime start(pos, m_fps);
80     GenTime duration(dur, m_fps);
81     if (m_min != GenTime() && start < m_min) {
82         m_view.clip_position->setText(m_tc.getTimecode(m_min));
83     } else if (m_max != GenTime() && start + duration > m_max) {
84         m_view.clip_position->setText(m_tc.getTimecode(m_max - duration));
85     }
86 }
87
88 void ClipDurationDialog::slotCheckDuration()
89 {
90     int pos = m_tc.getFrameCount(m_view.clip_position->text());
91     int dur = m_tc.getFrameCount(m_view.clip_duration->text());
92     int crop = m_tc.getFrameCount(m_view.crop_position->text());
93     GenTime start(pos, m_fps);
94     GenTime duration(dur, m_fps);
95     GenTime cropStart(crop, m_fps);
96     GenTime maxDuration;
97     if (m_clip->maxDuration() == GenTime()) maxDuration = m_max;
98     else maxDuration = m_max == GenTime() ? start + m_clip->maxDuration() - cropStart : qMin(m_max, start + m_clip->maxDuration() - cropStart);
99     if (maxDuration != GenTime() && start + duration > maxDuration) {
100         m_view.clip_duration->blockSignals(true);
101         m_view.clip_duration->setText(m_tc.getTimecode(maxDuration - start));
102         m_view.clip_duration->blockSignals(false);
103     }
104 }
105
106 void ClipDurationDialog::slotCheckCrop()
107 {
108     int dur = m_tc.getFrameCount(m_view.clip_duration->text());
109     int crop = m_tc.getFrameCount(m_view.crop_position->text());
110     int diff = crop - m_crop;
111     if ((diff > 0 && diff < dur) || diff < 0) {
112         dur -= diff;
113     } else {
114         m_view.crop_position->setText(m_tc.getTimecode(GenTime(m_crop, m_fps)));
115         return;
116     }
117     GenTime duration(dur, m_fps);
118     GenTime cropStart(crop, m_fps);
119     GenTime maxDuration = m_clip->maxDuration();
120     if (maxDuration != GenTime() && cropStart + duration > maxDuration) {
121         m_view.crop_position->setText(m_tc.getTimecode(GenTime(m_crop, m_fps)));
122     } else {
123         m_crop = crop;
124         m_view.clip_duration->blockSignals(true);
125         m_view.clip_duration->setText(m_tc.getTimecode(duration));
126         m_view.clip_duration->blockSignals(false);
127     }
128 }
129
130 void ClipDurationDialog::slotPosUp()
131 {
132     int position = m_tc.getFrameCount(m_view.clip_position->text());
133     //if (duration >= m_clip->duration().frames(m_fps)) return;
134     position ++;
135     m_view.clip_position->setText(m_tc.getTimecode(GenTime(position, m_fps)));
136 }
137
138 void ClipDurationDialog::slotPosDown()
139 {
140     int position = m_tc.getFrameCount(m_view.clip_position->text());
141     //if (duration >= m_clip->duration().frames(m_fps)) return;
142     position --;
143     m_view.clip_position->setText(m_tc.getTimecode(GenTime(position, m_fps)));
144 }
145
146 void ClipDurationDialog::slotDurUp()
147 {
148     int duration = m_tc.getFrameCount(m_view.clip_duration->text());
149     int crop = m_tc.getFrameCount(m_view.crop_position->text());
150     if (m_clip->maxDuration() != GenTime() && duration + crop > m_clip->maxDuration().frames(m_fps)) return;
151     duration ++;
152     m_view.clip_duration->setText(m_tc.getTimecode(GenTime(duration, m_fps)));
153 }
154
155 void ClipDurationDialog::slotDurDown()
156 {
157     int duration = m_tc.getFrameCount(m_view.clip_duration->text());
158     if (duration <= 0) return;
159     duration --;
160     m_view.clip_duration->setText(m_tc.getTimecode(GenTime(duration, m_fps)));
161 }
162
163 void ClipDurationDialog::slotCropUp()
164 {
165     int crop = m_tc.getFrameCount(m_view.crop_position->text());
166     int duration = m_tc.getFrameCount(m_view.clip_duration->text());
167     if (m_clip->maxDuration() != GenTime() && duration + crop > m_clip->maxDuration().frames(m_fps)) return;
168     crop ++;
169     m_view.crop_position->setText(m_tc.getTimecode(GenTime(crop, m_fps)));
170 }
171
172 void ClipDurationDialog::slotCropDown()
173 {
174     int crop = m_tc.getFrameCount(m_view.crop_position->text());
175     if (crop <= 0) return;
176     crop --;
177     m_view.crop_position->setText(m_tc.getTimecode(GenTime(crop, m_fps)));
178 }
179
180 GenTime ClipDurationDialog::startPos() const
181 {
182     int pos = m_tc.getFrameCount(m_view.clip_position->text());
183     return GenTime(pos, m_fps);
184 }
185
186 GenTime ClipDurationDialog::cropStart() const
187 {
188     int pos = m_tc.getFrameCount(m_view.crop_position->text());
189     return GenTime(pos, m_fps);
190 }
191
192 GenTime ClipDurationDialog::duration() const
193 {
194     int pos = m_tc.getFrameCount(m_view.clip_duration->text());
195     return GenTime(pos, m_fps);
196 }
197
198 void ClipDurationDialog::wheelEvent(QWheelEvent * event)
199 {
200     if (m_view.clip_position->underMouse()) {
201         if (event->delta() > 0)
202             slotPosUp();
203         else
204             slotPosDown();
205     } else if (m_view.clip_duration->underMouse()) {
206         if (event->delta() > 0)
207             slotDurUp();
208         else
209             slotDurDown();
210     } else if (m_view.crop_position->underMouse()) {
211         if (event->delta() > 0)
212             slotCropUp();
213         else
214             slotCropDown();
215     }
216 }
217
218 #include "clipdurationdialog.moc"
219
220