Files
oak-editor/app/widget/timebased/timebasedviewselectionmanager.h
T
Mike-Solar bb40b4923e style: unify identifier naming per updated conventions
Automated with clang-tidy readability-identifier-naming (config added to
.clang-tidy) plus scripted passes, per the updated rules now documented
in CONTRIBUTING.md:

- types (class/struct/enum/alias/template params): PascalCase
- functions, variables, members: snake_case (incl. rational -> Rational)
- private/protected members: trailing underscore; static member
  variables likewise (instance_, available_themes_)
- constants and enum values: snake_case (kLinear -> k_linear,
  F32P -> f32p); ALL_CAPS reserved for macros
- macros: OAK_ prefix (OLIVE_ADD_TEST/OLIVE_ASSERT/OLIVE_CONFIG ->
  OAK_ADD_TEST/OAK_ASSERT/OAK_CONFIG, GL_PREAMBLE -> OAK_GL_PREAMBLE,
  include guards -> OAK_*)
- file names: all lowercase (Current/Plugin/OliveHost/OliveClip/
  OlivePluginInstance -> current/plugin/olivehost/oliveclip/
  oliveplugininstance)
- getters share the member name sans underscore, setters set_foo()
- Qt and third-party (OpenFX) virtual overrides and framework callbacks
  keep their original names (exempt in .clang-tidy)

Manual follow-ups required where automation could not reach:
- string-based QMetaObject/SIGNAL/SLOT references updated to renamed
  methods (AddTask, CreatedFile, DeleteSpecificFile, moveSelectionUp, ...)
- macro bodies referencing renamed methods (OLIVE_CONFIG,
  NODE_DEFAULT_DESTRUCTOR, MANAGEDDISPLAYWIDGET_*)
- self-shadowing locals renamed where signals/methods became same-named
  (size_changed, worker_count, selected_items, import param, filters)
- third_party OFX member/namespace usages restored (OFX::Host::*,
  _created, _clipPrefsDirty, createInstance, clearPersistentMessage)
- STL protocol aliases restored (const_iterator) with .clang-tidy
  ignore rules; qHash overloads restored

Full build and test suite pass: ctest 4/4, ~1960 gtest cases green.
2026-07-19 16:10:54 +08:00

493 lines
11 KiB
C++

/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
Modifications Copyright (C) 2025 mikesolar
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef OAK_TIMEBASEDVIEWSELECTIONMANAGER_H
#define OAK_TIMEBASEDVIEWSELECTIONMANAGER_H
#include <QGraphicsView>
#include <QMouseEvent>
#include <QRubberBand>
#include <QToolTip>
#include "common/qtutils.h"
#include "timebasedview.h"
#include "timebasedwidget.h"
#include "widget/timetarget/timetarget.h"
namespace olive
{
template <typename T> class TimeBasedViewSelectionManager {
public:
TimeBasedViewSelectionManager(TimeBasedView *view)
: view_(view)
, rubberband_(nullptr)
, snap_mask_(TimeBasedWidget::k_snap_all)
{
}
void set_snap_mask(TimeBasedWidget::SnapMask e)
{
snap_mask_ = e;
}
void clear_drawn_objects()
{
drawn_objects_.clear();
}
void declare_drawn_object(T *object, const QRectF &rect)
{
QRectF r(view_->unscale_point(rect.topLeft()),
view_->unscale_point(rect.bottomRight()));
drawn_objects_.push_back({ object, r });
}
bool select(T *key)
{
Q_ASSERT(key);
if (!is_selected(key)) {
selected_.push_back(key);
return true;
}
return false;
}
bool deselect(T *key)
{
Q_ASSERT(key);
auto it = std::find(selected_.cbegin(), selected_.cend(), key);
if (it == selected_.cend()) {
return false;
} else {
selected_.erase(it);
return true;
}
}
void clear_selection()
{
selected_.clear();
}
bool is_selected(T *key) const
{
return std::find(selected_.cbegin(), selected_.cend(), key) !=
selected_.cend();
}
const std::vector<T *> &get_selected_objects() const
{
return selected_;
}
void set_timebase(const Rational &tb)
{
timebase_ = tb;
}
T *get_object_at_point(const QPointF &scene_pt)
{
// Iterate in reverse order because the objects drawn later will appear on top to the user
QPointF unscaled = view_->unscale_point(scene_pt);
for (auto it = drawn_objects_.crbegin(); it != drawn_objects_.crend();
it++) {
const DrawnObject &kp = *it;
if (kp.second.contains(unscaled)) {
return kp.first;
}
}
return nullptr;
}
T *get_object_at_point(const QPoint &pt)
{
return get_object_at_point(view_->mapToScene(pt));
}
T *mouse_press(QMouseEvent *event)
{
T *key_under_cursor = nullptr;
if (event->button() == Qt::LeftButton ||
event->button() == Qt::RightButton) {
// See if there's a keyframe in this position
key_under_cursor = get_object_at_point(event->pos());
bool holding_shift = event->modifiers() & Qt::ShiftModifier;
if (!key_under_cursor || !is_selected(key_under_cursor)) {
if (!holding_shift) {
// If not already selecting and not holding shift, clear the current selection
clear_selection();
}
// Add item to selection, either nothing if shift wasn't held, or the existing selection
if (key_under_cursor) {
select(key_under_cursor);
view_->SelectionManagerSelectEvent(key_under_cursor);
}
} else if (holding_shift) {
// If selected and holding shift, de-select this item but do nothing else
deselect(key_under_cursor);
view_->SelectionManagerDeselectEvent(key_under_cursor);
key_under_cursor = nullptr;
}
}
return key_under_cursor;
}
bool is_dragging() const
{
return !dragging_.empty();
}
void drag_start(T *initial_item, QMouseEvent *event,
TimeTargetObject *target = nullptr)
{
if (event->button() != Qt::LeftButton) {
return;
}
time_target_ = target;
initial_drag_item_ = initial_item;
dragging_.resize(selected_.size());
if constexpr (std::is_same_v<T, TimelineMarker>) {
snap_points_.resize(selected_.size() * 2);
} else {
snap_points_.resize(selected_.size());
}
if (target) {
time_targets_.resize(snap_points_.size());
memset(time_targets_.data(), 0,
time_targets_.size() * sizeof(Node *));
} else {
time_targets_.clear();
}
for (size_t i = 0; i < selected_.size(); i++) {
T *obj = selected_.at(i);
if constexpr (std::is_same_v<T, TimelineMarker>) {
dragging_[i] = obj->time().in();
snap_points_[i] = obj->time().in();
snap_points_[i + selected_.size()] = obj->time().out();
if (target) {
time_targets_[i] = time_targets_[i + selected_.size()] =
QtUtils::get_parent_of_type<Node>(obj);
}
} else {
dragging_[i] = obj->time();
snap_points_[i] = obj->time();
if (target) {
time_targets_[i] = QtUtils::get_parent_of_type<Node>(obj);
}
}
}
drag_mouse_start_ =
view_->unscale_point(view_->mapToScene(event->pos()));
}
void snap_points(Rational *movement)
{
std::vector<Rational> copy = snap_points_;
if (time_target_) {
for (size_t i = 0; i < copy.size(); i++) {
if (Node *parent = time_targets_[i]) {
copy[i] = time_target_->get_adjusted_time(
parent, time_target_->get_time_target(), copy[i],
Node::k_transform_towards_output);
}
}
}
if (Core::instance()->snapping() && view_->get_snap_service()) {
view_->get_snap_service()->snap_point(copy, movement, snap_mask_);
}
}
void unsnap()
{
if (view_->get_snap_service()) {
view_->get_snap_service()->hide_snaps();
}
}
void drag_move(const QPoint &local_pos,
const QString &tip_format = QString())
{
Rational time_diff =
view_->scene_to_time_no_grid(view_->mapToScene(local_pos).x() -
view_->scale_point(drag_mouse_start_).x());
// Snap points
Rational presnap_time_diff = time_diff;
snap_points(&time_diff);
// Validate snapping
if (Core::instance()->snapping() && view_->get_snap_service()) {
for (size_t i = 0; i < selected_.size(); i++) {
Rational proposed_time = dragging_.at(i) + time_diff;
T *sel = selected_.at(i);
if (sel->has_sibling_at_time(proposed_time)) {
// Unsnap
time_diff = presnap_time_diff;
if (view_->get_snap_service()) {
view_->get_snap_service()->hide_snaps();
}
break;
}
}
}
// Validate movement
for (size_t i = 0; i < selected_.size(); i++) {
Rational proposed_time = dragging_.at(i) + time_diff;
T *sel = selected_.at(i);
// Magic number: use interval of 1ms to avoid collisions
Rational adj(1, 1000);
if (dragging_.at(i) < proposed_time) {
// Negate adjustment value if origin is less than proposed time
adj = -adj;
}
bool loop;
do {
loop = false;
while (sel->has_sibling_at_time(proposed_time)) {
proposed_time += adj;
unsnap();
}
if (proposed_time < 0) {
// Prevent any object from going below zero
proposed_time = 0;
unsnap();
// Setting our proposed time to zero may (re)introduce a conflict that we just avoided
// with the sibling check above, so we request it to happen again. To avoid a negative
// adj bringing us back below zero, we force adj to positive so it'll only nudge higher
adj = qAbs(adj);
loop = true;
}
} while (loop);
time_diff = proposed_time - dragging_.at(i);
}
// Apply movement
for (size_t i = 0; i < selected_.size(); i++) {
selected_.at(i)->set_time(dragging_.at(i) + time_diff);
}
// Show information about this keyframe
Rational display_time;
if constexpr (std::is_same_v<T, TimelineMarker>) {
display_time = initial_drag_item_->time().in();
} else {
display_time = initial_drag_item_->time();
}
QString tip = QString::fromStdString(Timecode::time_to_timecode(
display_time, timebase_, Core::instance()->get_timecode_display(),
false));
last_used_tip_format_ = tip_format;
if (!tip_format.isEmpty()) {
tip = tip_format.arg(tip);
}
QToolTip::hideText();
QToolTip::showText(QCursor::pos(), tip);
}
void drag_stop(MultiUndoCommand *command)
{
QToolTip::hideText();
for (size_t i = 0; i < selected_.size(); i++) {
Rational current;
if constexpr (std::is_same_v<T, TimelineMarker>) {
current = selected_.at(i)->time().in();
} else {
current = selected_.at(i)->time();
}
command->add_child(
new SetTimeCommand(selected_.at(i), current, dragging_.at(i)));
}
dragging_.clear();
unsnap();
}
void rubber_band_start(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton ||
event->button() == Qt::RightButton) {
rubberband_scene_start_ =
view_->unscale_point(view_->mapToScene(event->pos()));
rubberband_ = new QRubberBand(QRubberBand::Rectangle, view_);
rubberband_->setGeometry(
QRect(event->pos().x(), event->pos().y(), 0, 0));
rubberband_->show();
rubberband_preselected_ = selected_;
}
}
void rubber_band_move(const QPoint &pos)
{
if (is_rubber_banding()) {
QRectF band_rect = QRectF(view_->mapFromScene(view_->scale_point(
rubberband_scene_start_)),
pos)
.normalized();
rubberband_->setGeometry(band_rect.toRect());
QPointF current = view_->unscale_point(view_->mapToScene(pos));
QRectF scene_rect =
QRectF(rubberband_scene_start_, current).normalized();
selected_ = rubberband_preselected_;
foreach (const DrawnObject &kp, drawn_objects_) {
if (scene_rect.intersects(kp.second)) {
select(kp.first);
}
}
}
}
void rubber_band_stop()
{
if (is_rubber_banding()) {
delete rubberband_;
rubberband_ = nullptr;
}
}
bool is_rubber_banding() const
{
return rubberband_;
}
void force_drag_update()
{
if (is_rubber_banding() || is_dragging()) {
QPoint local_pos = view_->viewport()->mapFromGlobal(QCursor::pos());
if (is_rubber_banding()) {
rubber_band_move(local_pos);
} else {
drag_move(local_pos, last_used_tip_format_);
}
}
}
private:
class SetTimeCommand : public UndoCommand {
public:
SetTimeCommand(T *key, const Rational &time)
{
key_ = key;
new_time_ = time;
old_time_ = key_->time();
}
SetTimeCommand(T *key, const Rational &new_time,
const Rational &old_time)
{
key_ = key;
new_time_ = new_time;
old_time_ = old_time;
}
virtual Project *get_relevant_project() const override
{
return Project::get_project_from_object(key_);
}
protected:
virtual void redo() override
{
key_->set_time(new_time_);
}
virtual void undo() override
{
key_->set_time(old_time_);
}
private:
T *key_;
Rational old_time_;
Rational new_time_;
};
TimeBasedView *view_;
using DrawnObject = QPair<T *, QRectF>;
std::vector<DrawnObject> drawn_objects_;
std::vector<T *> selected_;
std::vector<Rational> dragging_;
std::vector<Rational> snap_points_;
std::vector<Node *> time_targets_;
T *initial_drag_item_;
QPointF drag_mouse_start_;
Rational timebase_;
QRubberBand *rubberband_;
QPointF rubberband_scene_start_;
std::vector<T *> rubberband_preselected_;
TimeBasedWidget::SnapMask snap_mask_;
TimeTargetObject *time_target_;
QString last_used_tip_format_;
};
}
#endif // OAK_TIMEBASEDVIEWSELECTIONMANAGER_H