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