File indexing completed on 2024-04-28 04:52:26
0001 /* 0002 SPDX-FileCopyrightText: 2019 Jean-Baptiste Mardelle 0003 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0004 */ 0005 0006 #include "clipsnapmodel.hpp" 0007 #include "bin/model/markerlistmodel.hpp" 0008 #include <climits> 0009 #include <cstdlib> 0010 #include <memory> 0011 0012 ClipSnapModel::ClipSnapModel() = default; 0013 0014 void ClipSnapModel::addPoint(int position) 0015 { 0016 m_snapPoints.insert(position); 0017 if (position < m_inPoint * m_speed || position >= m_outPoint * m_speed) { 0018 return; 0019 } 0020 if (auto ptr = m_registeredSnap.lock()) { 0021 ptr->addPoint(m_speed < 0 ? int(ceil(m_outPoint + m_position + position / m_speed - m_inPoint)) 0022 : int(ceil(m_position + position / m_speed - m_inPoint))); 0023 } 0024 } 0025 0026 void ClipSnapModel::removePoint(int position) 0027 { 0028 m_snapPoints.erase(position); 0029 if (position < m_inPoint * m_speed || position >= m_outPoint * m_speed) { 0030 return; 0031 } 0032 if (auto ptr = m_registeredSnap.lock()) { 0033 ptr->removePoint(m_speed < 0 ? int(ceil(m_outPoint + m_position + position / m_speed - m_inPoint)) 0034 : int(ceil(m_position + position / m_speed - m_inPoint))); 0035 } 0036 } 0037 0038 void ClipSnapModel::updateSnapModelPos(int newPos) 0039 { 0040 if (newPos == m_position) { 0041 return; 0042 } 0043 removeAllSnaps(); 0044 m_position = newPos; 0045 addAllSnaps(); 0046 } 0047 0048 void ClipSnapModel::updateSnapModelInOut(std::vector<int> borderSnaps) 0049 { 0050 removeAllSnaps(); 0051 m_inPoint = borderSnaps.at(0); 0052 m_outPoint = borderSnaps.at(1); 0053 m_mixPoint = borderSnaps.at(2); 0054 addAllSnaps(); 0055 } 0056 0057 void ClipSnapModel::updateSnapMixPosition(int mixPos) 0058 { 0059 removeAllSnaps(); 0060 m_mixPoint = mixPos; 0061 addAllSnaps(); 0062 } 0063 0064 void ClipSnapModel::addAllSnaps() 0065 { 0066 if (auto ptr = m_registeredSnap.lock()) { 0067 for (const auto &snap : m_snapPoints) { 0068 if (snap >= m_inPoint * m_speed && snap < m_outPoint * m_speed) { 0069 ptr->addPoint(m_speed < 0 ? int(ceil(m_outPoint + m_position + snap / m_speed - m_inPoint)) 0070 : int(ceil(m_position + snap / m_speed - m_inPoint))); 0071 } 0072 } 0073 if (m_mixPoint > 0) { 0074 ptr->addPoint(int(ceil(m_position + m_mixPoint))); 0075 } 0076 } 0077 } 0078 0079 void ClipSnapModel::removeAllSnaps() 0080 { 0081 if (auto ptr = m_registeredSnap.lock()) { 0082 for (const auto &snap : m_snapPoints) { 0083 if (snap >= m_inPoint * m_speed && snap < m_outPoint * m_speed) { 0084 ptr->removePoint(m_speed < 0 ? int(ceil(m_outPoint + m_position + snap / m_speed - m_inPoint)) 0085 : int(ceil(m_position + snap / m_speed - m_inPoint))); 0086 } 0087 } 0088 if (m_mixPoint > 0) { 0089 ptr->removePoint(int(ceil(m_position + m_mixPoint))); 0090 } 0091 } 0092 } 0093 0094 void ClipSnapModel::allSnaps(std::vector<int> &snaps, int offset) const 0095 { 0096 snaps.push_back(m_position - offset); 0097 if (auto ptr = m_registeredSnap.lock()) { 0098 for (const auto &snap : m_snapPoints) { 0099 if (snap >= m_inPoint * m_speed && snap < m_outPoint * m_speed) { 0100 snaps.push_back(m_speed < 0 ? int(ceil(m_outPoint + m_position + snap / m_speed - m_inPoint - offset)) 0101 : int(ceil(m_position + snap / m_speed - m_inPoint - offset))); 0102 } 0103 } 0104 } 0105 if (m_mixPoint > 0) { 0106 snaps.push_back(m_position + m_mixPoint - offset); 0107 } 0108 snaps.push_back(m_position + m_outPoint - m_inPoint + 1 - offset); 0109 } 0110 0111 void ClipSnapModel::registerSnapModel(const std::weak_ptr<SnapModel> &snapModel, int position, int in, int out, double speed) 0112 { 0113 // make sure ptr is valid 0114 m_inPoint = in; 0115 m_outPoint = out; 0116 m_speed = speed; 0117 m_position = qMax(0, position); 0118 m_registeredSnap = snapModel; 0119 addAllSnaps(); 0120 } 0121 0122 void ClipSnapModel::deregisterSnapModel() 0123 { 0124 // make sure ptr is valid 0125 removeAllSnaps(); 0126 m_registeredSnap.reset(); 0127 } 0128 0129 void ClipSnapModel::setReferenceModel(const std::weak_ptr<MarkerListModel> &markerModel, double speed) 0130 { 0131 m_parentModel = markerModel; 0132 m_speed = speed; 0133 if (auto ptr = m_parentModel.lock()) { 0134 ptr->registerSnapModel(std::static_pointer_cast<SnapInterface>(shared_from_this())); 0135 } 0136 }