|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright Contributors to the toucan project. |
| 3 | + |
| 4 | +#include "TimeLayout.h" |
| 5 | + |
| 6 | +namespace toucan |
| 7 | +{ |
| 8 | + void ITimeWidget::_init( |
| 9 | + const std::shared_ptr<dtk::Context>& context, |
| 10 | + const OTIO_NS::TimeRange& timeRange, |
| 11 | + const std::string& objectName, |
| 12 | + const std::shared_ptr<dtk::IWidget>& parent) |
| 13 | + { |
| 14 | + dtk::IWidget::_init(context, objectName, parent); |
| 15 | + _timeRange = timeRange; |
| 16 | + } |
| 17 | + |
| 18 | + ITimeWidget::~ITimeWidget() |
| 19 | + {} |
| 20 | + |
| 21 | + const OTIO_NS::TimeRange& ITimeWidget::getTimeRange() |
| 22 | + { |
| 23 | + return _timeRange; |
| 24 | + } |
| 25 | + |
| 26 | + double ITimeWidget::getScale() const |
| 27 | + { |
| 28 | + return _scale; |
| 29 | + } |
| 30 | + |
| 31 | + void ITimeWidget::setScale(double value) |
| 32 | + { |
| 33 | + if (value == _scale) |
| 34 | + return; |
| 35 | + _scale = value; |
| 36 | + for (const auto& child : getChildren()) |
| 37 | + { |
| 38 | + if (auto item = std::dynamic_pointer_cast<ITimeWidget>(child)) |
| 39 | + { |
| 40 | + item->setScale(value); |
| 41 | + } |
| 42 | + } |
| 43 | + _setSizeUpdate(); |
| 44 | + } |
| 45 | + |
| 46 | + int ITimeWidget::getMinWidth() const |
| 47 | + { |
| 48 | + return _minWidth; |
| 49 | + } |
| 50 | + |
| 51 | + OTIO_NS::RationalTime ITimeWidget::posToTime(double value) const |
| 52 | + { |
| 53 | + OTIO_NS::RationalTime out; |
| 54 | + const dtk::Box2I& g = getGeometry(); |
| 55 | + if (g.w() > 0) |
| 56 | + { |
| 57 | + const double n = (value - g.min.x) / static_cast<double>(g.w()); |
| 58 | + out = OTIO_NS::RationalTime( |
| 59 | + _timeRange.start_time() + |
| 60 | + OTIO_NS::RationalTime( |
| 61 | + _timeRange.duration().value() * n, |
| 62 | + _timeRange.duration().rate())). |
| 63 | + round(); |
| 64 | + out = dtk::clamp( |
| 65 | + out, |
| 66 | + _timeRange.start_time(), |
| 67 | + _timeRange.end_time_inclusive()); |
| 68 | + } |
| 69 | + return out; |
| 70 | + } |
| 71 | + |
| 72 | + int ITimeWidget::timeToPos(const OTIO_NS::RationalTime& value) const |
| 73 | + { |
| 74 | + int out = 0; |
| 75 | + const dtk::Box2I& g = getGeometry(); |
| 76 | + if (_timeRange.duration().value() > 0.0) |
| 77 | + { |
| 78 | + const OTIO_NS::RationalTime t = value - _timeRange.start_time(); |
| 79 | + const double n = t.value() / _timeRange.duration().rescaled_to(t).value(); |
| 80 | + out = g.min.x + g.w() * n; |
| 81 | + } |
| 82 | + return out; |
| 83 | + } |
| 84 | + |
| 85 | + void TimeLayout::_init( |
| 86 | + const std::shared_ptr<dtk::Context>& context, |
| 87 | + const OTIO_NS::TimeRange& timeRange, |
| 88 | + const std::shared_ptr<dtk::IWidget>& parent) |
| 89 | + { |
| 90 | + ITimeWidget::_init(context, timeRange, "toucan::TimeLayout", parent); |
| 91 | + } |
| 92 | + |
| 93 | + TimeLayout::~TimeLayout() |
| 94 | + {} |
| 95 | + |
| 96 | + std::shared_ptr<TimeLayout> TimeLayout::create( |
| 97 | + const std::shared_ptr<dtk::Context>& context, |
| 98 | + const OTIO_NS::TimeRange& timeRange, |
| 99 | + const std::shared_ptr<dtk::IWidget>& parent) |
| 100 | + { |
| 101 | + auto out = std::shared_ptr<TimeLayout>(new TimeLayout); |
| 102 | + out->_init(context, timeRange, parent); |
| 103 | + return out; |
| 104 | + } |
| 105 | + |
| 106 | + void TimeLayout::setGeometry(const dtk::Box2I& value) |
| 107 | + { |
| 108 | + ITimeWidget::setGeometry(value); |
| 109 | + for (const auto& child : getChildren()) |
| 110 | + { |
| 111 | + if (auto timeWidget = std::dynamic_pointer_cast<ITimeWidget>(child)) |
| 112 | + { |
| 113 | + const OTIO_NS::TimeRange& timeRange = timeWidget->getTimeRange(); |
| 114 | + const int t0 = timeToPos(timeRange.start_time()); |
| 115 | + const int t1 = timeToPos(timeRange.end_time_exclusive()); |
| 116 | + const dtk::Size2I& childSizeHint = child->getSizeHint(); |
| 117 | + child->setGeometry(dtk::Box2I( |
| 118 | + t0, |
| 119 | + value.min.y, |
| 120 | + std::max(t1 - t0, timeWidget->getMinWidth()), |
| 121 | + childSizeHint.h)); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + void TimeLayout::sizeHintEvent(const dtk::SizeHintEvent& event) |
| 127 | + { |
| 128 | + ITimeWidget::sizeHintEvent(event); |
| 129 | + dtk::Size2I sizeHint; |
| 130 | + for (const auto& child : getChildren()) |
| 131 | + { |
| 132 | + if (auto timeWidget = std::dynamic_pointer_cast<ITimeWidget>(child)) |
| 133 | + { |
| 134 | + const dtk::Size2I& childSizeHint = timeWidget->getSizeHint(); |
| 135 | + sizeHint.h = std::max(sizeHint.h, childSizeHint.h); |
| 136 | + } |
| 137 | + } |
| 138 | + sizeHint.w = _timeRange.duration().rescaled_to(1.0).value() * _scale; |
| 139 | + _setSizeHint(sizeHint); |
| 140 | + } |
| 141 | + |
| 142 | + void TimeStackLayout::_init( |
| 143 | + const std::shared_ptr<dtk::Context>& context, |
| 144 | + const OTIO_NS::TimeRange& timeRange, |
| 145 | + const std::shared_ptr<dtk::IWidget>& parent) |
| 146 | + { |
| 147 | + ITimeWidget::_init(context, timeRange, "toucan::TimeStackLayout", parent); |
| 148 | + } |
| 149 | + |
| 150 | + TimeStackLayout::~TimeStackLayout() |
| 151 | + {} |
| 152 | + |
| 153 | + std::shared_ptr<TimeStackLayout> TimeStackLayout::create( |
| 154 | + const std::shared_ptr<dtk::Context>& context, |
| 155 | + const OTIO_NS::TimeRange& timeRange, |
| 156 | + const std::shared_ptr<dtk::IWidget>& parent) |
| 157 | + { |
| 158 | + auto out = std::shared_ptr<TimeStackLayout>(new TimeStackLayout); |
| 159 | + out->_init(context, timeRange, parent); |
| 160 | + return out; |
| 161 | + } |
| 162 | + |
| 163 | + void TimeStackLayout::setGeometry(const dtk::Box2I& value) |
| 164 | + { |
| 165 | + ITimeWidget::setGeometry(value); |
| 166 | + int y = value.min.y; |
| 167 | + for (const auto& child : getChildren()) |
| 168 | + { |
| 169 | + if (auto timeWidget = std::dynamic_pointer_cast<ITimeWidget>(child)) |
| 170 | + { |
| 171 | + const OTIO_NS::TimeRange& timeRange = timeWidget->getTimeRange(); |
| 172 | + const double t0 = timeToPos(timeRange.start_time()); |
| 173 | + const double t1 = timeToPos(timeRange.end_time_exclusive()); |
| 174 | + const dtk::Size2I& childSizeHint = child->getSizeHint(); |
| 175 | + child->setGeometry(dtk::Box2I(t0, y, t1 - t0, childSizeHint.h)); |
| 176 | + y += childSizeHint.h + _size.spacing; |
| 177 | + }; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + void TimeStackLayout::sizeHintEvent(const dtk::SizeHintEvent& event) |
| 182 | + { |
| 183 | + ITimeWidget::sizeHintEvent(event); |
| 184 | + const bool displayScaleChanged = event.displayScale != _size.displayScale; |
| 185 | + if (_size.init || displayScaleChanged) |
| 186 | + { |
| 187 | + _size.init = false; |
| 188 | + _size.displayScale = event.displayScale; |
| 189 | + _size.spacing = event.style->getSizeRole(dtk::SizeRole::SpacingTool, event.displayScale); |
| 190 | + } |
| 191 | + |
| 192 | + dtk::Size2I sizeHint; |
| 193 | + const auto& children = getChildren(); |
| 194 | + if (!children.empty()) |
| 195 | + { |
| 196 | + for (const auto& child : children) |
| 197 | + { |
| 198 | + if (auto timeWidget = std::dynamic_pointer_cast<ITimeWidget>(child)) |
| 199 | + { |
| 200 | + const dtk::Size2I& childSizeHint = timeWidget->getSizeHint(); |
| 201 | + sizeHint.h += childSizeHint.h; |
| 202 | + } |
| 203 | + } |
| 204 | + sizeHint.h += (children.size() - 1) * _size.spacing; |
| 205 | + } |
| 206 | + sizeHint.w = _timeRange.duration().rescaled_to(1.0).value() * _scale; |
| 207 | + _setSizeHint(sizeHint); |
| 208 | + } |
| 209 | +} |
0 commit comments