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