]> git.sesse.net Git - kdenlive/blob - src/clipdurationdialog.cpp
Cleaning code style of Definitions.
[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
24 #include <KDebug>
25 #include <KMessageBox>
26 #include <KGlobalSettings>
27
28 #include <QWheelEvent>
29
30 ClipDurationDialog::ClipDurationDialog(AbstractClipItem *clip, const Timecode &tc, const GenTime &min, const GenTime &max, QWidget * parent):
31         QDialog(parent),
32         m_clip(clip),
33         m_min(min),
34         m_max(max)
35 {
36     setFont(KGlobalSettings::toolBarFont());
37     setupUi(this);
38
39     m_pos = new TimecodeDisplay(tc);
40     m_cropStart = new TimecodeDisplay(tc);
41     m_dur = new TimecodeDisplay(tc);
42     m_cropEnd = new TimecodeDisplay(tc);
43     
44     clip_position_box->addWidget(m_pos);
45     crop_start_box->addWidget(m_cropStart);
46     clip_duration_box->addWidget(m_dur);
47     crop_end_box->addWidget(m_cropEnd);
48
49     bool allowCrop = true;
50     if (clip->type() == AVWidget) {
51         ClipItem *item = static_cast <ClipItem *>(clip);
52         const int t = item->clipType();
53         if (t == Color || t == Image || t == Text)
54             allowCrop = false;
55     }
56
57     if (!allowCrop || clip->type() == TransitionWidget) {
58         m_cropStart->setHidden(true);
59         crop_label->hide();
60         m_cropEnd->setHidden(true),
61         end_label->hide();
62     }
63
64     m_crop = m_clip->cropStart();
65
66     m_pos->setValue(m_clip->startPos());
67     m_dur->setValue(m_clip->cropDuration());
68     m_cropStart->setValue(m_clip->cropStart());
69     m_cropEnd->setValue(m_clip->maxDuration() - m_clip->cropDuration() - m_clip->cropStart());
70
71     connect(m_pos,       SIGNAL(timeCodeEditingFinished()), this, SLOT(slotCheckStart()));
72     connect(m_dur,       SIGNAL(timeCodeEditingFinished()), this, SLOT(slotCheckDuration()));
73     connect(m_cropStart, SIGNAL(timeCodeEditingFinished()), this, SLOT(slotCheckCrop()));
74     connect(m_cropEnd,   SIGNAL(timeCodeEditingFinished()), this, SLOT(slotCheckEnd()));
75
76     adjustSize();
77 }
78
79 ClipDurationDialog::~ClipDurationDialog()
80 {
81     delete m_pos;
82     delete m_dur;
83     delete m_cropStart;
84     delete m_cropEnd;
85 }
86
87 void ClipDurationDialog::slotCheckStart()
88 {
89     GenTime start = m_pos->gentime();
90     GenTime duration = m_dur->gentime();
91     if (m_min != GenTime() && start < m_min)
92         m_pos->setValue(m_min);
93     else if (m_max != GenTime() && start + duration > m_max)
94         m_pos->setValue(m_max - duration);
95 }
96
97 void ClipDurationDialog::slotCheckDuration()
98 {
99     GenTime start = m_pos->gentime();
100     GenTime duration = m_dur->gentime();
101     GenTime cropStart = m_cropStart->gentime();
102     GenTime maxDuration;
103
104     if (m_clip->maxDuration() == GenTime())
105         maxDuration = m_max;
106     else
107         maxDuration = m_max == GenTime() ? start + m_clip->maxDuration() - cropStart : qMin(m_max, start + m_clip->maxDuration() - cropStart);
108
109     if (maxDuration != GenTime() && start + duration > maxDuration) {
110         m_dur->blockSignals(true);
111         m_dur->setValue(maxDuration - start);
112         m_dur->blockSignals(false);
113     }
114
115     m_cropEnd->blockSignals(true);
116     m_cropEnd->setValue(m_clip->maxDuration() - m_dur->gentime() - cropStart);
117     m_cropEnd->blockSignals(false);
118 }
119
120 void ClipDurationDialog::slotCheckCrop()
121 {
122     GenTime duration = m_dur->gentime();
123     GenTime cropStart = m_cropStart->gentime();
124     GenTime maxDuration = m_clip->maxDuration();
125
126     GenTime diff = cropStart - m_crop;
127     if ((diff > GenTime() && diff < duration) || diff < GenTime()) {
128         duration -= diff;
129     } else {
130         m_cropStart->setValue(m_crop);
131         return;
132     }
133
134     if (maxDuration != GenTime() && cropStart + duration > maxDuration) {
135         m_cropStart->setValue(m_crop);
136     } else {
137         m_crop = cropStart;
138         m_dur->blockSignals(true);
139         m_dur->setValue(duration);
140         m_dur->blockSignals(false);
141     }
142 }
143
144 void ClipDurationDialog::slotCheckEnd()
145 {
146     GenTime cropStart = m_cropStart->gentime();
147     GenTime cropEnd = m_cropEnd->gentime();
148     GenTime duration = m_clip->maxDuration() - cropEnd - cropStart;
149
150     if (duration >= GenTime()) {
151         m_dur->setValue(duration);
152         slotCheckDuration();
153     } else {
154         m_cropEnd->blockSignals(true);
155         m_cropEnd->setValue(m_clip->maxDuration() - m_dur->gentime() - cropStart);
156         m_cropEnd->blockSignals(false);
157     }
158 }
159
160 GenTime ClipDurationDialog::startPos() const
161 {
162     return m_pos->gentime();
163 }
164
165 GenTime ClipDurationDialog::cropStart() const
166 {
167     return m_cropStart->gentime();
168 }
169
170 GenTime ClipDurationDialog::duration() const
171 {
172     return m_dur->gentime();
173 }
174
175 #include "clipdurationdialog.moc"
176
177