Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
qfuture_qml_wrapper.h
1// SPDX-FileCopyrightText: © 2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <QFutureWatcher>
7#include <QtQmlIntegration/qqmlintegration.h>
8
9namespace zrythm::gui::qquick
10{
15class QFutureQmlWrapper : public QObject
16{
17 Q_OBJECT
18 Q_PROPERTY (
19 int progressMinimum READ progressMinimum NOTIFY progressRangeChanged)
20 Q_PROPERTY (
21 int progressMaximum READ progressMaximum NOTIFY progressRangeChanged)
22 Q_PROPERTY (int progressValue READ progressValue NOTIFY progressValueChanged)
23 Q_PROPERTY (QString progressText READ progressText NOTIFY progressTextChanged)
24 QML_ELEMENT
25 QML_UNCREATABLE ("")
26
27public:
28 ~QFutureQmlWrapper () override = default;
29
30 virtual int progressMinimum () const = 0;
31 virtual int progressMaximum () const = 0;
32 virtual int progressValue () const = 0;
33 virtual QString progressText () const = 0;
34 Q_INVOKABLE virtual QVariant resultVar () const = 0;
35 Q_INVOKABLE virtual void cancel () = 0;
36 Q_INVOKABLE virtual bool isCanceled () const = 0;
37 Q_INVOKABLE virtual bool isValid () const = 0;
38
39Q_SIGNALS:
40 void progressValueChanged (int value);
41 void progressRangeChanged (int minimum, int maximum);
42 void progressTextChanged (const QString &text);
43 void finished ();
44
45protected:
46 void setup (QFutureWatcherBase &watcher) const
47 {
48 QObject::connect (
49 &watcher, &QFutureWatcherBase::progressRangeChanged, this,
50 &QFutureQmlWrapper::progressRangeChanged);
51 QObject::connect (
52 &watcher, &QFutureWatcherBase::progressValueChanged, this,
53 &QFutureQmlWrapper::progressValueChanged);
54 QObject::connect (
55 &watcher, &QFutureWatcherBase::progressTextChanged, this,
56 &QFutureQmlWrapper::progressTextChanged);
57 QObject::connect (
58 &watcher, &QFutureWatcherBase::finished, this,
59 &QFutureQmlWrapper::finished);
60 }
61};
62
63template <typename T> class QFutureQmlWrapperT : public QFutureQmlWrapper
64{
65public:
66 QFutureQmlWrapperT (QFuture<T> future) : future_ (future)
67 {
68 watcher_.setFuture (future_);
69 setup (watcher_);
70 }
71
72 int progressMinimum () const override { return watcher_.progressMinimum (); }
73 int progressMaximum () const override { return watcher_.progressMaximum (); }
74 int progressValue () const override { return watcher_.progressValue (); }
75 QString progressText () const override { return watcher_.progressText (); }
76 QVariant resultVar () const override
77 {
78 if constexpr (std::is_same_v<T, void>)
79 {
80 return QVariant{};
81 }
82 else
83 {
84 assert (future_.isResultReadyAt (0));
85 return QVariant::fromValue (watcher_.result ());
86 }
87 }
88
89 void cancel () override { future_.cancel (); }
90 bool isCanceled () const override { return future_.isCanceled (); }
91 bool isValid () const override { return future_.isValid (); }
92
93private:
94 QFuture<T> future_;
95 QFutureWatcher<T> watcher_;
96};
97}
QML-exposed wrapper over a QFuture that provides properties for binding.