wxWidgets based Stable Diffusion C++ GUi
Ferenc Szontágh
2024-02-04 1683c8a090c3efc51c43107d5ede0dcd5d506e3b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef __SD_GUI_QUEUE_MANAGER
#define __SD_GUI_QUEUE_MANAGER
 
#include <fstream>
#include <map>
#include <chrono>
#include <nlohmann/json.hpp>
 
#include "utils.hpp"
 
#include <wx/event.h>
#include <wx/window.h>
 
namespace QM
{
    enum QueueStatus
    {
        PENDING,
        RUNNING,
        PAUSED,
        FAILED,
        MODEL_LOADING,
        DONE
    };
    inline const char *QueueStatus_str[] = {
        "pending",
        "running",
        "paused",
        "failed",
        "model loading...",
        "finished"};
 
    enum QueueEvents
    {
        ITEM_DELETED,
        ITEM_ADDED,
        ITEM_STATUS_CHANGED,
        ITEM_UPDATED,
        ITEM_START,
        ITEM_FINISHED
    };
    struct QueueItem
    {
        QueueItem() = default;
        QueueItem(const QueueItem &other)
            : id(other.id), created_at(other.created_at), updated_at(other.updated_at),
              finished_at(other.finished_at), params(other.params), status(other.status), images(other.images), step(other.step), steps(other.steps), time(other.time), model(other.model) {}
 
        QueueItem &operator=(const QueueItem &other)
        {
            if (this != &other)
            {
                id = other.id;
                created_at = other.created_at;
                updated_at = other.updated_at;
                finished_at = other.finished_at;
                images = other.images;
                params = other.params;
                status = other.status;
                step = other.step;
                steps = other.steps;
                time = other.time;
                model = other.model;
            }
            return *this;
        }
        int id = 0, created_at = 0, updated_at = 0, finished_at = 0;
        int step = 0, steps = 0;
        float time = 0;
        sd_gui_utils::SDParams params;
        QM::QueueStatus status = QM::QueueStatus::PENDING;
        std::vector<std::string> images;
        std::string model;
    };
 
    inline void to_json(nlohmann::json &j, const QueueItem &p)
    {
        j = nlohmann::json{
            {"id", p.id},
            {"created_at", p.created_at},
            {"updated_at", p.updated_at},
            {"finished_at", p.finished_at},
            {"images", p.images},
            {"status", (int)p.status},
            {"model", p.model},
            {"params", p.params},
 
        };
    }
 
    inline void from_json(const nlohmann::json &j, QueueItem &p)
    {
        j.at("id").get_to(p.id);
        j.at("created_at").get_to(p.created_at);
        j.at("updated_at").get_to(p.updated_at);
        j.at("images").get_to(p.images);
        j.at("finished_at").get_to(p.finished_at);
        j.at("model").get_to(p.model);
        j.at("params").get_to(p.params);
        p.status = j.at("status").get<QM::QueueStatus>();
    }
 
    class QueueManager
    {
    public:
        QueueManager(wxEvtHandler *eventHandler, std::string jobsdir);
        ~QueueManager();
        int AddItem(QM::QueueItem item);
        int AddItem(sd_gui_utils::SDParams *params);
        int AddItem(sd_gui_utils::SDParams params);
        QM::QueueItem GetItem(int id);
        QM::QueueItem GetItem(QM::QueueItem item);
        const std::map<int, QM::QueueItem> getList();
        int Duplicate(QM::QueueItem item);
        int Duplicate(int id);
        void SetStatus(QM::QueueStatus status, int id);
        void PauseAll();
        void SendEventToMainWindow(QM::QueueEvents eventType, QM::QueueItem item = QM::QueueItem());
        void OnThreadMessage(wxThreadEvent &e);
        void SaveJobToFile(int id);
        void SaveJobToFile(QM::QueueItem item);
 
    private:
        int GetCurrentUnixTimestamp();
        void LoadJobListFromDir();
        std::string jobsDir;
        int lastId = 0;
        int GetAnId();
        // thread events handler, toupdate main window data table
        void onItemAdded(QM::QueueItem item);
 
        // @brief check if something is running or not
        bool isRunning = false;
 
        wxEvtHandler *eventHandler;
        wxWindow *parent;
        std::map<int, QM::QueueItem> QueueList;
    };
 
};
 
#endif