Zrythm v2.0.0-alpha.1
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
change_qobject_property_command.h
1// SPDX-FileCopyrightText: © 2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <utility>
7
8#include <QObject>
9#include <QUndoCommand>
10#include <QVariant>
11
12namespace zrythm::commands
13{
14
25class ChangeQObjectPropertyCommand : public QUndoCommand
26{
27public:
28 ChangeQObjectPropertyCommand (
29 QObject &object,
30 QString property_name,
31 QVariant value)
32 : object_ (object), property_name_ (property_name),
33 original_value_ (object.property (property_name.toLatin1 ().data ())),
34 value_ (std::move (value))
35 {
36 setText (QObject::tr ("Change %1").arg (property_name_));
37 }
38
39 int id () const override { return 1762957285; }
40 bool mergeWith (const QUndoCommand * other) override
41 {
42 if (other->id () != id ())
43 return false;
44
45 // only merge if other command was made in quick succession of this
46 // command's redo() and operates on the same objects
47 const auto * other_cmd =
48 dynamic_cast<const ChangeQObjectPropertyCommand *> (other);
49 const auto cur_time = std::chrono::steady_clock::now ();
50 const auto duration = cur_time - last_redo_timestamp_;
51 if (
52 std::chrono::duration_cast<std::chrono::milliseconds> (duration).count ()
53 > 1'000)
54 {
55 return false;
56 }
57
58 // Check if we're operating on the same object and property
59 if (
60 &object_ != &other_cmd->object_
61 || property_name_ != other_cmd->property_name_)
62 return false;
63
64 last_redo_timestamp_ = cur_time;
65 value_ = other_cmd->value_;
66 return true;
67 }
68
69 void undo () override
70 {
71 object_.setProperty (property_name_.toLatin1 ().data (), original_value_);
72 }
73
74 void redo () override
75 {
76 object_.setProperty (property_name_.toLatin1 ().data (), value_);
77 last_redo_timestamp_ = std::chrono::steady_clock::now ();
78 }
79
80private:
81 QObject &object_;
82 QString property_name_;
83 QVariant original_value_;
84 QVariant value_;
85 std::chrono::time_point<std::chrono::steady_clock> last_redo_timestamp_;
86};
87
89 : public ChangeQObjectPropertyCommand
90{
91public:
92 static constexpr int CommandId = 1762957664;
93 using ChangeQObjectPropertyCommand::ChangeQObjectPropertyCommand;
94
95 int id () const override { return CommandId; }
96};
97
98} // namespace zrythm::commands