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