File indexing completed on 2024-05-19 04:55:07

0001 /*
0002     SPDX-FileCopyrightText: 2008 Jean-Baptiste Mardelle <jb@kdenlive.org>
0003 
0004 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "clipdurationdialog.h"
0008 #include "core.h"
0009 
0010 #include <QFontDatabase>
0011 
0012 #include <QWheelEvent>
0013 
0014 ClipDurationDialog::ClipDurationDialog(int clipId, int pos, int minpos, int in, int out, int length, int maxpos, QWidget *parent)
0015     : QDialog(parent)
0016     , m_clipId(clipId)
0017     , m_fps(pCore->getCurrentFps())
0018     , m_min(GenTime(minpos, m_fps))
0019     , m_max(GenTime(maxpos, m_fps))
0020     , m_length(GenTime(length, m_fps))
0021 {
0022     setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
0023     setupUi(this);
0024 
0025     bool allowCrop = length != -1;
0026 
0027     if (!allowCrop) {
0028         m_cropStart->setHidden(true);
0029         crop_label->hide();
0030         m_cropEnd->setHidden(true), end_label->hide();
0031     }
0032 
0033     m_crop = GenTime(in, m_fps);
0034 
0035     m_pos->setValue(GenTime(pos, m_fps));
0036     m_dur->setValue(GenTime(out - in, m_fps));
0037     m_cropStart->setValue(GenTime(in, m_fps));
0038     m_cropEnd->setValue(GenTime(length - out, m_fps));
0039 
0040     connect(m_pos, &TimecodeDisplay::timeCodeEditingFinished, this, &ClipDurationDialog::slotCheckStart);
0041     connect(m_dur, &TimecodeDisplay::timeCodeEditingFinished, this, &ClipDurationDialog::slotCheckDuration);
0042     connect(m_cropStart, &TimecodeDisplay::timeCodeEditingFinished, this, &ClipDurationDialog::slotCheckCrop);
0043     connect(m_cropEnd, &TimecodeDisplay::timeCodeEditingFinished, this, &ClipDurationDialog::slotCheckEnd);
0044 
0045     adjustSize();
0046 }
0047 
0048 void ClipDurationDialog::slotCheckStart()
0049 {
0050     GenTime start = m_pos->gentime();
0051     GenTime duration = m_dur->gentime();
0052     if (m_min != GenTime() && start < m_min) {
0053         m_pos->setValue(m_min);
0054     } else if (m_max != GenTime() && start + duration > m_max) {
0055         m_pos->setValue(m_max - duration);
0056     }
0057 }
0058 
0059 void ClipDurationDialog::slotCheckDuration()
0060 {
0061     GenTime start = m_pos->gentime();
0062     GenTime duration = m_dur->gentime();
0063     GenTime cropStart = m_cropStart->gentime();
0064     GenTime maxDuration;
0065 
0066     if (m_length <= GenTime()) {
0067         maxDuration = m_max;
0068     } else {
0069         maxDuration = m_max == GenTime() ? start + m_length - cropStart : qMin(m_max, start + m_length - cropStart);
0070     }
0071 
0072     if (maxDuration != GenTime() && start + duration > maxDuration) {
0073         m_dur->blockSignals(true);
0074         m_dur->setValue(maxDuration - start);
0075         m_dur->blockSignals(false);
0076     }
0077 
0078     m_cropEnd->blockSignals(true);
0079     m_cropEnd->setValue(m_length - m_dur->gentime() - cropStart);
0080     m_cropEnd->blockSignals(false);
0081 }
0082 
0083 void ClipDurationDialog::slotCheckCrop()
0084 {
0085     GenTime duration = m_dur->gentime();
0086     GenTime cropStart = m_cropStart->gentime();
0087 
0088     GenTime diff = cropStart - m_crop;
0089     if ((diff > GenTime() && diff < duration) || diff < GenTime()) {
0090         duration -= diff;
0091     } else {
0092         m_cropStart->setValue(m_crop);
0093         return;
0094     }
0095 
0096     if (m_length > GenTime() && cropStart + duration > m_length) {
0097         m_cropStart->setValue(m_crop);
0098     } else {
0099         m_crop = cropStart;
0100         m_dur->blockSignals(true);
0101         m_dur->setValue(duration);
0102         m_dur->blockSignals(false);
0103     }
0104 }
0105 
0106 void ClipDurationDialog::slotCheckEnd()
0107 {
0108     GenTime cropStart = m_cropStart->gentime();
0109     GenTime cropEnd = m_cropEnd->gentime();
0110     GenTime duration = m_length - cropEnd - cropStart;
0111 
0112     if (duration >= GenTime()) {
0113         m_dur->setValue(duration);
0114         slotCheckDuration();
0115     } else {
0116         m_cropEnd->blockSignals(true);
0117         m_cropEnd->setValue(m_length - m_dur->gentime() - cropStart);
0118         m_cropEnd->blockSignals(false);
0119     }
0120 }
0121 
0122 GenTime ClipDurationDialog::startPos() const
0123 {
0124     return m_pos->gentime();
0125 }
0126 
0127 GenTime ClipDurationDialog::cropStart() const
0128 {
0129     return m_cropStart->gentime();
0130 }
0131 
0132 GenTime ClipDurationDialog::duration() const
0133 {
0134     return m_dur->gentime();
0135 }