]> git.sesse.net Git - kdenlive/blob - src/clipdurationdialog.cpp
Fix adjusting position with clip dialog (double click on clip)
[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, QWidget * parent):
31         QDialog(parent),
32         m_clip(clip),
33         m_tc(tc)
34 {
35     setFont(KGlobalSettings::toolBarFont());
36     m_fps = m_tc.fps();
37     m_view.setupUi(this);
38
39     bool allowCrop = true;
40     if (clip->type() == AVWIDGET) {
41         ClipItem *item = static_cast <ClipItem *>(clip);
42         int t = item->clipType();
43         if (t == COLOR || t == IMAGE || t == TEXT) allowCrop = false;
44     }
45
46     if (!allowCrop || clip->type() == TRANSITIONWIDGET) {
47         m_view.crop_up->hide();
48         m_view.crop_down->hide();
49         m_view.crop_position->hide();
50         m_view.crop_label->hide();
51     }
52
53     m_view.clip_position->setText(tc.getTimecode(m_clip->startPos(), m_fps));
54     m_view.crop_position->setText(tc.getTimecode(m_clip->cropStart(), m_fps));
55     m_view.clip_duration->setText(tc.getTimecode(m_clip->duration(), m_fps));
56     connect(m_view.position_up, SIGNAL(clicked()), this, SLOT(slotPosUp()));
57     connect(m_view.position_down, SIGNAL(clicked()), this, SLOT(slotPosDown()));
58     connect(m_view.crop_up, SIGNAL(clicked()), this, SLOT(slotCropUp()));
59     connect(m_view.crop_down, SIGNAL(clicked()), this, SLOT(slotCropDown()));
60     connect(m_view.duration_up, SIGNAL(clicked()), this, SLOT(slotDurUp()));
61     connect(m_view.duration_down, SIGNAL(clicked()), this, SLOT(slotDurDown()));
62     connect(m_view.crop_position, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckCrop()));
63     adjustSize();
64 }
65
66 ClipDurationDialog::~ClipDurationDialog()
67 {
68 }
69
70 void ClipDurationDialog::setMargins(GenTime min, GenTime max)
71 {
72     m_min = min;
73     m_max = max;
74     connect(m_view.clip_position, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckStart()));
75     connect(m_view.clip_duration, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckDuration()));
76 }
77
78 void ClipDurationDialog::slotCheckStart()
79 {
80     int pos = m_tc.getFrameCount(m_view.clip_position->text(), m_fps);
81     int dur = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
82     GenTime start(pos, m_fps);
83     GenTime duration(dur, m_fps);
84     if (m_min != GenTime() && start < m_min) {
85         m_view.clip_position->setText(m_tc.getTimecode(m_min, m_fps));
86     } else if (m_max != GenTime() && start + duration > m_max) {
87         m_view.clip_position->setText(m_tc.getTimecode(m_max - duration, m_fps));
88     }
89 }
90
91 void ClipDurationDialog::slotCheckDuration()
92 {
93     int pos = m_tc.getFrameCount(m_view.clip_position->text(), m_fps);
94     int dur = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
95     GenTime start(pos, m_fps);
96     GenTime duration(dur, m_fps);
97     GenTime maxDuration = m_max == GenTime() ? start + m_clip->maxDuration() : qMin(m_max, start + m_clip->maxDuration());
98     if (start + duration > maxDuration) {
99         m_view.clip_duration->setText(m_tc.getTimecode(maxDuration - start, m_fps));
100     }
101 }
102
103 void ClipDurationDialog::slotCheckCrop()
104 {
105     int dur = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
106     int crop = m_tc.getFrameCount(m_view.crop_position->text(), m_fps);
107     GenTime duration(dur, m_fps);
108     GenTime cropStart(crop, m_fps);
109     GenTime maxDuration = m_clip->maxDuration();
110     if (cropStart + duration > maxDuration) {
111         m_view.crop_position->setText(m_tc.getTimecode(maxDuration - duration, m_fps));
112     }
113 }
114
115 void ClipDurationDialog::slotPosUp()
116 {
117     int position = m_tc.getFrameCount(m_view.clip_position->text(), m_fps);
118     //if (duration >= m_clip->duration().frames(m_fps)) return;
119     position ++;
120     m_view.clip_position->setText(m_tc.getTimecode(GenTime(position, m_fps), m_fps));
121 }
122
123 void ClipDurationDialog::slotPosDown()
124 {
125     int position = m_tc.getFrameCount(m_view.clip_position->text(), m_fps);
126     //if (duration >= m_clip->duration().frames(m_fps)) return;
127     position --;
128     m_view.clip_position->setText(m_tc.getTimecode(GenTime(position, m_fps), m_fps));
129 }
130
131 void ClipDurationDialog::slotDurUp()
132 {
133     int duration = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
134     int crop = m_tc.getFrameCount(m_view.crop_position->text(), m_fps);
135     if (duration + crop > m_clip->maxDuration().frames(m_fps)) return;
136     duration ++;
137     m_view.clip_duration->setText(m_tc.getTimecode(GenTime(duration, m_fps), m_fps));
138 }
139
140 void ClipDurationDialog::slotDurDown()
141 {
142     int duration = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
143     if (duration <= 0) return;
144     duration --;
145     m_view.clip_duration->setText(m_tc.getTimecode(GenTime(duration, m_fps), m_fps));
146 }
147
148 void ClipDurationDialog::slotCropUp()
149 {
150     int crop = m_tc.getFrameCount(m_view.crop_position->text(), m_fps);
151     int duration = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
152     if (duration + crop > m_clip->maxDuration().frames(m_fps)) return;
153     crop ++;
154     m_view.crop_position->setText(m_tc.getTimecode(GenTime(crop, m_fps), m_fps));
155 }
156
157 void ClipDurationDialog::slotCropDown()
158 {
159     int crop = m_tc.getFrameCount(m_view.crop_position->text(), m_fps);
160     if (crop <= 0) return;
161     crop --;
162     m_view.crop_position->setText(m_tc.getTimecode(GenTime(crop, m_fps), m_fps));
163 }
164
165 GenTime ClipDurationDialog::startPos() const
166 {
167     int pos = m_tc.getFrameCount(m_view.clip_position->text(), m_fps);
168     return GenTime(pos, m_fps);
169 }
170
171 GenTime ClipDurationDialog::cropStart() const
172 {
173     int pos = m_tc.getFrameCount(m_view.crop_position->text(), m_fps);
174     return GenTime(pos, m_fps);
175 }
176
177 GenTime ClipDurationDialog::duration() const
178 {
179     int pos = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
180     return GenTime(pos, m_fps);
181 }
182
183 void ClipDurationDialog::wheelEvent(QWheelEvent * event)
184 {
185     if (m_view.clip_position->underMouse()) {
186         if (event->delta() > 0)
187             slotPosUp();
188         else
189             slotPosDown();
190     } else if (m_view.clip_duration->underMouse()) {
191         if (event->delta() > 0)
192             slotDurUp();
193         else
194             slotDurDown();
195     } else if (m_view.crop_position->underMouse()) {
196         if (event->delta() > 0)
197             slotCropUp();
198         else
199             slotCropDown();
200     }
201 }
202
203 #include "clipdurationdialog.moc"
204
205