Atlas Game Manager
A game manager for f95 and dlsite written in c++
Loading...
Searching...
No Matches
ProgressNotification.hpp
Go to the documentation of this file.
1//
2// Created by kj16609 on 7/21/23.
3//
4
5#pragma once
6#ifndef ATLASGAMEMANAGER_PROGRESSNOTIFICATION_HPP
7#define ATLASGAMEMANAGER_PROGRESSNOTIFICATION_HPP
8
9#include <QFuture>
10#include <QFutureWatcher>
11#include <QMetaObject>
12
13#include "Notification.hpp"
15
16QT_BEGIN_NAMESPACE
17
18namespace Ui
19{
21}
22
23QT_END_NAMESPACE
24
26{
27 Q_OBJECT
28 Q_DISABLE_COPY_MOVE( ProgressNotification )
29
30 public:
31
32 explicit ProgressNotification( QWidget* parent = nullptr );
33 ~ProgressNotification() override;
34
35 friend class ProgressSignaler;
36
37 private:
38
39 Ui::ProgressNotification* ui;
40};
41
43{
44 Q_OBJECT
45 Q_DISABLE_COPY_MOVE( ProgressSignaler )
46
47 std::uint64_t counter { 0 };
48 std::uint64_t max { 0 };
49
50 public:
51
53 ProgressSignaler( QString str );
54
55 void setMax( int i );
56
57 template < typename T >
58 requires std::is_integral_v< T >
59 void setProgress( T i )
60 {
61 if constexpr ( std::is_same_v< T, int > )
62 {
63 emit progressChanged( i );
64 }
65 else
66 emit progressChanged( static_cast< int >( i ) );
67
68 if constexpr ( std::is_same_v< T, decltype( counter ) > )
69 counter = i;
70 else
71 counter = static_cast< decltype( counter ) >( i );
72 }
73
74 void setSubMessage( const QString str );
75 void setMessage( const QString str );
76
78
79 private:
80
82
83 public slots:
84
86 {
87 if ( counter == max ) emit selfClose();
88 }
89
90 signals:
91 void progressChanged( int i );
92 void maxChanged( int i );
93 void messageChanged( const QString str );
94 void subMessageChanged( const QString str );
95 void selfClose();
96};
97
98template < typename T >
99class FutureWatcherSignaler final : public QFuture< T >
100{
101 Q_DISABLE_COPY_MOVE( FutureWatcherSignaler )
102
103 using Future = QFuture< T >;
104 using Watcher = QFutureWatcher< T >;
105
109
110 std::vector< QMetaObject::Connection > m_signals;
111
112 public:
113
115
116 FutureWatcherSignaler( QFuture< T > future ) : m_future( std::move( future ) ), m_signaler()
117 {
118 m_signals.emplace_back( connect(
119 &m_watcher,
120 &Watcher::progressValueChanged,
121 [ this ]( const int progress ) { m_signaler.setProgress( progress ); } ) );
122
123 m_signals.emplace_back( connect(
124 &m_watcher,
125 &Watcher::progressRangeChanged,
126 [ this ]( [[maybe_unused]] const int min, const int max ) { m_signaler.setMax( max ); } ) );
127
128 m_signals.emplace_back( connect(
129 &m_watcher,
130 &Watcher::finished,
131 [ this ]()
132 {
133 m_signaler.setSubMessage( "Finished" );
134 m_signaler.triggerSelfClose();
135 } ) );
136
137 m_signals.emplace_back(
138 connect( &m_watcher, &Watcher::started, [ this ]() { m_signaler.setSubMessage( "Started" ); } ) );
139
140 m_watcher.setFuture( m_future );
141 }
142
144 {
145 for ( auto& signal : m_signals )
146 {
147 m_watcher.disconnect( signal );
148 }
149 }
150};
151
152#endif //ATLASGAMEMANAGER_PROGRESSNOTIFICATION_HPP
std::vector< QMetaObject::Connection > m_signals
Definition ProgressNotification.hpp:110
FutureWatcherSignaler(QFuture< T > future)
Definition ProgressNotification.hpp:116
QFuture< T > Future
Definition ProgressNotification.hpp:103
~FutureWatcherSignaler()
Definition ProgressNotification.hpp:143
Watcher m_watcher
Definition ProgressNotification.hpp:107
FutureWatcherSignaler()=delete
QFutureWatcher< T > Watcher
Definition ProgressNotification.hpp:104
Future m_future
Definition ProgressNotification.hpp:106
ProgressSignaler m_signaler
Definition ProgressNotification.hpp:108
Notification(QWidget *parent=nullptr)
Definition Notification.cpp:22
Definition ProgressNotification.hpp:26
friend class ProgressSignaler
Definition ProgressNotification.hpp:35
ProgressNotification(QWidget *parent=nullptr)
Definition ProgressNotification.cpp:17
~ProgressNotification() override
Definition ProgressNotification.cpp:24
Ui::ProgressNotification * ui
Definition ProgressNotification.hpp:39
Definition ProgressNotification.hpp:43
void subMessageChanged(const QString str)
ProgressSignaler()
Definition ProgressNotification.cpp:56
void setMessage(const QString str)
Definition ProgressNotification.cpp:51
void hookSignaler(ProgressNotification *notif)
Definition ProgressNotification.cpp:29
void maxChanged(int i)
void setProgress(T i)
Definition ProgressNotification.hpp:59
std::uint64_t max
Definition ProgressNotification.hpp:48
void messageChanged(const QString str)
std::uint64_t counter
Definition ProgressNotification.hpp:47
void triggerSelfClose()
Definition ProgressNotification.hpp:85
void setSubMessage(const QString str)
Definition ProgressNotification.cpp:46
void setMax(int i)
Definition ProgressNotification.cpp:40
void progressChanged(int i)
~ProgressSignaler()
Definition ProgressNotification.cpp:74
Definition Game.hpp:17
Definition NotificationSignalers.hpp:15
Definition AboutAtlas.hpp:9