Skip to content

Commit af26941

Browse files
committed
Refactoring
1 parent 2f5afba commit af26941

File tree

10 files changed

+342
-80
lines changed

10 files changed

+342
-80
lines changed

bin/toucan-view/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(HEADERS
1313
MenuBar.h
1414
PlaybackModel.h
1515
SelectionModel.h
16+
StackItem.h
1617
TimeUnitsModel.h
1718
TimeWidgets.h
1819
TimelineItem.h
@@ -39,6 +40,7 @@ set(SOURCE
3940
MenuBar.cpp
4041
PlaybackModel.cpp
4142
SelectionModel.cpp
43+
StackItem.cpp
4244
TimeUnitsModel.cpp
4345
TimeWidgets.cpp
4446
TimelineItem.cpp

bin/toucan-view/ClipItem.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
#include "ClipItem.h"
66

77
#include <dtk/ui/DrawUtil.h>
8-
#include <dtk/core/Random.h>
98
#include <dtk/core/RenderUtil.h>
109

11-
#include <opentimelineio/clip.h>
12-
1310
namespace toucan
1411
{
1512
void ClipItem::_init(
1613
const std::shared_ptr<dtk::Context>& context,
1714
const std::shared_ptr<App>& app,
1815
const OTIO_NS::SerializableObject::Retainer<OTIO_NS::Clip>& clip,
16+
const dtk::Color4F& color,
1917
const std::shared_ptr<IWidget>& parent)
2018
{
2119
auto opt = clip->trimmed_range_in_parent();
@@ -27,16 +25,13 @@ namespace toucan
2725
"toucan::ClipItem",
2826
parent);
2927

30-
setTooltip(clip->name());
3128
_setMousePressEnabled(0, 0);
3229

3330
_clip = clip;
34-
_text = clip->name();
35-
dtk::Random random;
36-
_color = dtk::Color4F(
37-
random.getF(.5F) + .2F,
38-
random.getF(.5F) + .2F,
39-
random.getF(.5F) + .2F);
31+
_text = !clip->name().empty() ? clip->name() : "Clip";
32+
_color = color;
33+
34+
setTooltip(_text);
4035
}
4136

4237
ClipItem::~ClipItem()
@@ -46,10 +41,11 @@ namespace toucan
4641
const std::shared_ptr<dtk::Context>& context,
4742
const std::shared_ptr<App>& app,
4843
const OTIO_NS::SerializableObject::Retainer<OTIO_NS::Clip>& clip,
44+
const dtk::Color4F& color,
4945
const std::shared_ptr<IWidget>& parent)
5046
{
5147
auto out = std::make_shared<ClipItem>();
52-
out->_init(context, app, clip, parent);
48+
out->_init(context, app, clip, color, parent);
5349
return out;
5450
}
5551

@@ -71,7 +67,7 @@ namespace toucan
7167
}
7268
dtk::Size2I sizeHint(
7369
_timeRange.duration().rescaled_to(1.0).value() * _scale,
74-
_size.textSize.h + _size.margin + _size.borderFocus);
70+
_size.textSize.h + _size.margin * 2 + _size.borderFocus * 2);
7571
_setSizeHint(sizeHint);
7672
}
7773

bin/toucan-view/ClipItem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace toucan
1717
const std::shared_ptr<dtk::Context>&,
1818
const std::shared_ptr<App>&,
1919
const OTIO_NS::SerializableObject::Retainer<OTIO_NS::Clip>&,
20+
const dtk::Color4F&,
2021
const std::shared_ptr<IWidget>& parent);
2122

2223
public:
@@ -26,6 +27,7 @@ namespace toucan
2627
const std::shared_ptr<dtk::Context>&,
2728
const std::shared_ptr<App>&,
2829
const OTIO_NS::SerializableObject::Retainer<OTIO_NS::Clip>&,
30+
const dtk::Color4F&,
2931
const std::shared_ptr<IWidget>& parent = nullptr);
3032

3133
void sizeHintEvent(const dtk::SizeHintEvent&) override;

bin/toucan-view/SelectionModel.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
#include "SelectionModel.h"
66

7-
#include <opentimelineio/gap.h>
8-
97
#include <set>
108

119
namespace OTIO_NS

bin/toucan-view/StackItem.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright (c) 2024 Darby Johnston
3+
// All rights reserved.
4+
5+
#include "StackItem.h"
6+
7+
#include "TrackItem.h"
8+
9+
#include <dtk/ui/DrawUtil.h>
10+
#include <dtk/core/RenderUtil.h>
11+
12+
namespace toucan
13+
{
14+
void StackItem::_init(
15+
const std::shared_ptr<dtk::Context>& context,
16+
const std::shared_ptr<App>& app,
17+
const OTIO_NS::SerializableObject::Retainer<OTIO_NS::Stack>& stack,
18+
const std::shared_ptr<IWidget>& parent)
19+
{
20+
IItem::_init(
21+
context,
22+
app,
23+
OTIO_NS::dynamic_retainer_cast<OTIO_NS::Item>(stack),
24+
stack->trimmed_range(),
25+
"toucan::StackItem",
26+
parent);
27+
28+
_stack = stack;
29+
_text = !stack->name().empty() ? stack->name() : "Stack";
30+
_color = dtk::Color4F(.2F, .2F, .2F);
31+
32+
for (const auto& child : stack->children())
33+
{
34+
if (auto track = OTIO_NS::dynamic_retainer_cast<OTIO_NS::Track>(child))
35+
{
36+
TrackItem::create(context, app, track, shared_from_this());
37+
}
38+
}
39+
}
40+
41+
StackItem::~StackItem()
42+
{}
43+
44+
std::shared_ptr<StackItem> StackItem::create(
45+
const std::shared_ptr<dtk::Context>& context,
46+
const std::shared_ptr<App>& app,
47+
const OTIO_NS::SerializableObject::Retainer<OTIO_NS::Stack>& stack,
48+
const std::shared_ptr<IWidget>& parent)
49+
{
50+
auto out = std::make_shared<StackItem>();
51+
out->_init(context, app, stack, parent);
52+
return out;
53+
}
54+
55+
void StackItem::setGeometry(const dtk::Box2I& value)
56+
{
57+
IItem::setGeometry(value);
58+
const dtk::Box2I g = dtk::margin(
59+
value, 0, -_size.borderFocus, 0, -_size.borderFocus);
60+
dtk::V2I pos = g.min;
61+
pos.y += _size.fontMetrics.lineHeight + _size.margin * 2;
62+
for (const auto& child : getChildren())
63+
{
64+
const dtk::Size2I& sizeHint = child->getSizeHint();
65+
child->setGeometry(dtk::Box2I(pos.x, pos.y, sizeHint.w, sizeHint.h));
66+
pos.y += sizeHint.h + _size.spacing;
67+
}
68+
}
69+
70+
void StackItem::sizeHintEvent(const dtk::SizeHintEvent& event)
71+
{
72+
IItem::sizeHintEvent(event);
73+
const bool displayScaleChanged = event.displayScale != _size.displayScale;
74+
if (_size.init || displayScaleChanged)
75+
{
76+
_size.init = false;
77+
_size.displayScale = event.displayScale;
78+
_size.margin = event.style->getSizeRole(dtk::SizeRole::MarginInside, event.displayScale);
79+
_size.spacing = event.style->getSizeRole(dtk::SizeRole::SpacingTool, event.displayScale);
80+
_size.border = event.style->getSizeRole(dtk::SizeRole::Border, event.displayScale);
81+
_size.borderFocus = event.style->getSizeRole(dtk::SizeRole::BorderFocus, event.displayScale);
82+
_size.fontInfo = event.style->getFontRole(dtk::FontRole::Label, event.displayScale);
83+
_size.fontMetrics = event.fontSystem->getMetrics(_size.fontInfo);
84+
_size.textSize = event.fontSystem->getSize(_text, _size.fontInfo);
85+
_draw.glyphs.clear();
86+
}
87+
dtk::Size2I sizeHint(
88+
_timeRange.duration().rescaled_to(1.0).value() * _scale,
89+
0);
90+
const auto& children = getChildren();
91+
for (const auto& child : children)
92+
{
93+
const dtk::Size2I& childSizeHint = child->getSizeHint();
94+
sizeHint.h += childSizeHint.h;
95+
}
96+
if (!children.empty())
97+
{
98+
sizeHint.h += _size.spacing * (children.size() - 1);
99+
}
100+
sizeHint.h += _size.textSize.h + _size.margin * 2;
101+
sizeHint.h += _size.borderFocus * 2;
102+
_setSizeHint(sizeHint);
103+
}
104+
105+
void StackItem::clipEvent(const dtk::Box2I& clipRect, bool clipped)
106+
{
107+
IItem::clipEvent(clipRect, clipped);
108+
if (clipped)
109+
{
110+
_draw.glyphs.clear();
111+
}
112+
}
113+
114+
void StackItem::drawEvent(
115+
const dtk::Box2I& drawRect,
116+
const dtk::DrawEvent& event)
117+
{
118+
IItem::drawEvent(drawRect, event);
119+
const dtk::Box2I& g = getGeometry();
120+
121+
if (_selected)
122+
{
123+
event.render->drawMesh(
124+
dtk::border(g, _size.borderFocus),
125+
event.style->getColorRole(dtk::ColorRole::KeyFocus));
126+
}
127+
128+
const dtk::Box2I g2 = dtk::margin(g, -_size.borderFocus);
129+
event.render->drawRect(g2, _color);
130+
131+
const dtk::Box2I g3 = dtk::margin(g2, -_size.margin);
132+
if (!_text.empty() && _draw.glyphs.empty())
133+
{
134+
_draw.glyphs = event.fontSystem->getGlyphs(_text, _size.fontInfo);
135+
}
136+
dtk::ClipRectEnabledState clipRectEnabledState(event.render);
137+
dtk::ClipRectState clipRectState(event.render);
138+
event.render->setClipRectEnabled(true);
139+
event.render->setClipRect(intersect(g3, drawRect));
140+
event.render->drawText(
141+
_draw.glyphs,
142+
_size.fontMetrics,
143+
g3.min,
144+
event.style->getColorRole(dtk::ColorRole::Text));
145+
}
146+
}

bin/toucan-view/StackItem.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright (c) 2024 Darby Johnston
3+
// All rights reserved.
4+
5+
#pragma once
6+
7+
#include "IItem.h"
8+
9+
#include <opentimelineio/stack.h>
10+
11+
namespace toucan
12+
{
13+
class StackItem : public IItem
14+
{
15+
protected:
16+
void _init(
17+
const std::shared_ptr<dtk::Context>&,
18+
const std::shared_ptr<App>&,
19+
const OTIO_NS::SerializableObject::Retainer<OTIO_NS::Stack>&,
20+
const std::shared_ptr<IWidget>& parent);
21+
22+
public:
23+
virtual ~StackItem();
24+
25+
static std::shared_ptr<StackItem> create(
26+
const std::shared_ptr<dtk::Context>&,
27+
const std::shared_ptr<App>&,
28+
const OTIO_NS::SerializableObject::Retainer<OTIO_NS::Stack>&,
29+
const std::shared_ptr<IWidget>& parent = nullptr);
30+
31+
void setGeometry(const dtk::Box2I&) override;
32+
void sizeHintEvent(const dtk::SizeHintEvent&) override;
33+
void clipEvent(const dtk::Box2I&, bool) override;
34+
void drawEvent(const dtk::Box2I&, const dtk::DrawEvent&) override;
35+
36+
private:
37+
OTIO_NS::SerializableObject::Retainer<OTIO_NS::Stack> _stack;
38+
std::string _text;
39+
dtk::Color4F _color;
40+
41+
struct SizeData
42+
{
43+
bool init = true;
44+
float displayScale = 0.F;
45+
int margin = 0;
46+
int spacing = 0;
47+
int border = 0;
48+
int borderFocus = 0;
49+
dtk::FontInfo fontInfo;
50+
dtk::FontMetrics fontMetrics;
51+
dtk::Size2I textSize;
52+
};
53+
SizeData _size;
54+
55+
struct DrawData
56+
{
57+
std::vector<std::shared_ptr<dtk::Glyph> > glyphs;
58+
};
59+
DrawData _draw;
60+
};
61+
}

0 commit comments

Comments
 (0)