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