Zrythm v2.0.0-DEV
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{
14class ChangeQObjectPropertyCommand : public QUndoCommand
15{
16public:
17 ChangeQObjectPropertyCommand (
18 QObject &object,
19 QString property_name,
20 QVariant value)
21 : object_ (object), property_name_ (property_name),
22 original_value_ (object.property (property_name.toLatin1 ().data ())),
23 value_ (std::move (value))
24 {
25 setText (QObject::tr ("Change %1").arg (property_name_));
26 }
27
28 int id () const override { return 1762957285; }
29 bool mergeWith (const QUndoCommand * other) override
30 {
31 if (other->id () != id ())
32 return false;
33
34 // only merge if other command was made in quick succession of this
35 // command's redo() and operates on the same objects
36 const auto * other_cmd =
37 dynamic_cast<const ChangeQObjectPropertyCommand *> (other);
38 const auto cur_time = std::chrono::steady_clock::now ();
39 const auto duration = cur_time - last_redo_timestamp_;
40 if (
41 std::chrono::duration_cast<std::chrono::milliseconds> (duration).count ()
42 > 1'000)
43 {
44 return false;
45 }
46
47 // Check if we're operating on the same object and property
48 if (
49 &object_ != &other_cmd->object_
50 || property_name_ != other_cmd->property_name_)
51 return false;
52
53 last_redo_timestamp_ = cur_time;
54 value_ = other_cmd->value_;
55 return true;
56 }
57
58 void undo () override
59 {
60 object_.setProperty (property_name_.toLatin1 ().data (), original_value_);
61 }
62
63 void redo () override
64 {
65 object_.setProperty (property_name_.toLatin1 ().data (), value_);
66 last_redo_timestamp_ = std::chrono::steady_clock::now ();
67 }
68
69private:
70 QObject &object_;
71 QString property_name_;
72 QVariant original_value_;
73 QVariant value_;
74 std::chrono::time_point<std::chrono::steady_clock> last_redo_timestamp_;
75};
76
78 : public ChangeQObjectPropertyCommand
79{
80public:
81 static constexpr int CommandId = 1762957664;
82 using ChangeQObjectPropertyCommand::ChangeQObjectPropertyCommand;
83
84 int id () const override { return CommandId; }
85};
86
87} // namespace zrythm::commands