Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
colored_object.h
1// SPDX-FileCopyrightText: © 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "utils/color.h"
7#include "utils/icloneable.h"
8
9#include <QtQmlIntegration/qqmlintegration.h>
10
11namespace zrythm::structure::arrangement
12{
13
14class ArrangerObjectColor : public QObject
15{
16 Q_OBJECT
17 Q_PROPERTY (bool useColor READ useColor NOTIFY useColorChanged)
18 Q_PROPERTY (QColor color READ color WRITE setColor NOTIFY colorChanged)
19 QML_ELEMENT
20 QML_UNCREATABLE ("")
21
22public:
23 ArrangerObjectColor (QObject * parent = nullptr) noexcept : QObject (parent)
24 {
25 }
26 ~ArrangerObjectColor () noexcept override = default;
27 Z_DISABLE_COPY_MOVE (ArrangerObjectColor)
28
29 using Color = zrythm::utils::Color;
30
31 // ========================================================================
32 // QML Interface
33 // ========================================================================
34
35 bool useColor () const { return color_.has_value (); }
36
37 QColor color () const
38 {
39 if (useColor ())
40 {
41 return color_->to_qcolor ();
42 }
43 return {};
44 }
45 void setColor (QColor color)
46 {
47 if (useColor () && color_.value ().to_qcolor () == color)
48 return;
49
50 if (!color.isValid ())
51 return;
52
53 const bool emit_use_color_changed = !useColor ();
54 color_ = color;
55 Q_EMIT colorChanged (color);
56 if (emit_use_color_changed)
57 Q_EMIT useColorChanged (true);
58 }
59
60 Q_INVOKABLE void unsetColor ()
61 {
62 color_.reset ();
63 Q_EMIT useColorChanged (false);
64 }
65
66 Q_SIGNAL void colorChanged (QColor color);
67 Q_SIGNAL void useColorChanged (bool use_color);
68
69 // ========================================================================
70
71private:
72 friend void init_from (
73 ArrangerObjectColor &obj,
74 const ArrangerObjectColor &other,
75 utils::ObjectCloneType clone_type)
76 {
77 obj.color_ = other.color_;
78 }
79
80 static constexpr std::string_view kColorKey = "color";
81 friend void to_json (nlohmann::json &j, const ArrangerObjectColor &obj)
82 {
83 j[kColorKey] = obj.color_;
84 }
85 friend void from_json (const nlohmann::json &j, ArrangerObjectColor &obj)
86 {
87 j.at (kColorKey).get_to (obj.color_);
88 }
89
90private:
96 std::optional<Color> color_;
97
98 BOOST_DESCRIBE_CLASS (ArrangerObjectColor, (), (), (), (color_))
99};
100
101} // namespace zrythm::structure::arrangement