Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
expandable_tick_range.h
1// SPDX-FileCopyrightText: © 2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <QObject>
7
8#include <fmt/format.h>
9#include <fmt/ranges.h>
10
11namespace zrythm::utils
12{
13class ExpandableTickRange
14{
15public:
16 ExpandableTickRange (
17 std::optional<std::pair<double, double>> range = std::nullopt)
18 {
19 if (range.has_value ())
20 {
21 assert (range->second >= range->first);
22 start_ = range->first;
23 end_ = range->second;
24 is_full_content_ = false;
25 }
26 }
27
32 bool is_full_content () const { return is_full_content_; }
33
37 void expand (std::pair<double, double> range_to_add)
38 {
39 // full content takes precedence
40 if (is_full_content ())
41 return;
42
43 assert (range_to_add.second >= range_to_add.first);
44 start_ = std::min (start_, range_to_add.first);
45 end_ = std::max (end_, range_to_add.second);
46 }
47
48 void expand_to_full ()
49 {
50 is_full_content_ = true;
51 start_ = 0;
52 end_ = 0;
53 }
54
55 void expand (const ExpandableTickRange &range_to_add)
56 {
57 if (range_to_add.is_full_content ())
58 {
59 expand_to_full ();
60 }
61 else
62 {
63 expand (range_to_add.range ().value ());
64 }
65 }
66
70 auto range () const -> std::optional<std::pair<double, double>>
71 {
72 if (is_full_content ())
73 {
74 return std::nullopt;
75 }
76
77 return std::make_pair (start_, end_);
78 }
79
80private:
81 double start_{};
82 double end_{};
83 bool is_full_content_{ true };
84};
85
86inline auto
87format_as (const ExpandableTickRange &range) -> std::string
88{
89 if (range.is_full_content ())
90 {
91 return "AffectedTickRange: (full content)";
92 }
93 const auto tick_range = range.range ().value ();
94 return fmt::format ("AffectedTickRange: {}", tick_range);
95}
96}
97
98Q_DECLARE_METATYPE (zrythm::utils::ExpandableTickRange)
bool is_full_content() const
Returns whether the range is the full content (ie, there is no range).
void expand(std::pair< double, double > range_to_add)
Expands to the given range.
auto range() const -> std::optional< std::pair< double, double > >
Returns the range, or nullopt if the full content is covered.
String utilities.
Definition algorithms.h:12