wxWidgets based Stable Diffusion C++ GUi
Ferenc Szontágh
2024-02-05 126c3a3510a37e21c7404745144b5872b72aafdd
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "QueueManager.h"
 
QM::QueueManager::QueueManager(wxEvtHandler *eventHandler, std::string jobsdir)
{
    // need to send events into the mainwindow by the threads...
    this->eventHandler = eventHandler;
    this->eventHandler->Bind(wxEVT_THREAD, &QueueManager::OnThreadMessage, this);
    this->jobsDir = jobsdir;
    this->QueueList = std::map<int, QM::QueueItem>();
    this->LoadJobListFromDir();
}
 
QM::QueueManager::~QueueManager()
{
}
 
int QM::QueueManager::AddItem(QM::QueueItem item)
{
    if (item.id == 0)
    {
        item.id = this->GetAnId();
    }
    if (item.created_at == 0)
    {
        item.created_at = this->GetCurrentUnixTimestamp();
    }
 
    this->QueueList[item.id] = item;
 
    this->SendEventToMainWindow(QM::QueueEvents::ITEM_ADDED, item);
    if (this->isRunning == false && item.status == QM::QueueStatus::PENDING)
    {
        this->SendEventToMainWindow(QM::QueueEvents::ITEM_START, item);
        this->isRunning = true;
    }
    this->SaveJobToFile(item);
    return item.id;
}
 
int QM::QueueManager::AddItem(sd_gui_utils::SDParams *params)
{
    QM::QueueItem item;
    item.params = *params;
    return this->AddItem(item);
}
 
int QM::QueueManager::AddItem(sd_gui_utils::SDParams params)
{
    QM::QueueItem item;
    item.params = params;
    return this->AddItem(item);
}
 
QM::QueueItem QM::QueueManager::GetItem(int id)
{
    if (this->QueueList.find(id) == this->QueueList.end())
    {
        return QM::QueueItem();
    }
    else
    {
        return this->QueueList[id];
    }
}
 
QM::QueueItem QM::QueueManager::GetItem(QM::QueueItem item)
{
    return this->GetItem(item.id);
}
 
const std::map<int, QM::QueueItem> QM::QueueManager::getList()
{
    return this->QueueList;
}
 
int QM::QueueManager::Duplicate(QM::QueueItem item)
{
    if (this->QueueList.find(item.id) == this->QueueList.end())
    {
        return -1;
    }
    else
    {
        return this->AddItem(this->QueueList.at(item.id).params);
    }
}
 
int QM::QueueManager::Duplicate(int id)
{
    return this->AddItem(this->GetItem(id).params);
}
 
void QM::QueueManager::SetStatus(QM::QueueStatus status, int id)
{
    if (this->QueueList.find(id) != this->QueueList.end())
    {
        this->QueueList[id].status = status;
        this->QueueList[id].updated_at = this->GetCurrentUnixTimestamp();
        if (status == QM::QueueStatus::DONE)
        {
            this->QueueList[id].finished_at = this->GetCurrentUnixTimestamp();
        }
        this->SaveJobToFile(this->QueueList[id]);
        this->SendEventToMainWindow(QM::QueueEvents::ITEM_STATUS_CHANGED, this->QueueList[id]);
    }
}
 
void QM::QueueManager::PauseAll()
{
    for (auto [key, value] : this->QueueList)
    {
        if (value.status == QM::QueueStatus::PENDING)
        {
            this->SetStatus(QM::PAUSED, key);
            this->SendEventToMainWindow(QM::QueueEvents::ITEM_STATUS_CHANGED);
        }
    }
}
 
void QM::QueueManager::SendEventToMainWindow(QM::QueueEvents eventType, QM::QueueItem item)
{
    wxThreadEvent *e = new wxThreadEvent();
    e->SetString(wxString::Format("QUEUE:%d", (int)eventType));
    e->SetPayload(item);
    wxQueueEvent(this->eventHandler, e);
}
 
void QM::QueueManager::OnThreadMessage(wxThreadEvent &e)
{
 
    if (e.GetSkipped() == false)
    {
        e.Skip();
    }
    auto msg = e.GetString().ToStdString();
 
    std::string token = msg.substr(0, msg.find(":"));
    std::string content = msg.substr(msg.find(":") + 1);
    // only numbers here...
 
    // only handle the QUEUE messages, what this class generate
    if (token == "QUEUE")
    {
        QM::QueueEvents event = (QM::QueueEvents)std::stoi(content);
        auto payload = e.GetPayload<QM::QueueItem>();
        if (event == QM::QueueEvents::ITEM_START)
        {
            this->SetStatus(QM::QueueStatus::RUNNING, payload.id);
            this->isRunning = true;
            return;
        }
        if (event == QM::QueueEvents::ITEM_FINISHED)
        {
            this->SetStatus(QM::QueueStatus::DONE, payload.id);
            this->isRunning = false;
            // jump to the next item in queue
            // find waiting jobs
            for (auto job : this->QueueList)
            {
                if (job.second.status == QM::QueueStatus::PENDING)
                {
                    if (this->isRunning == false)
                    {
                        this->SendEventToMainWindow(QM::QueueEvents::ITEM_START, job.second);
                        this->isRunning = true;
                    }
                    break;
                }
            }
            return;
        }
    }
 
    if (token == "MODEL_LOAD_START")
    {
        auto payload = e.GetPayload<QM::QueueItem>();
        this->SetStatus(QM::QueueStatus::MODEL_LOADING, payload.id);
    }
 
    // this state can not usable at here, because the payload is the sd_ctx* pointer here..
    // we can't identify the current running job here... (we can maybe guess it, but not needed)
    // see  GENERATION_START
    if (token == "MODEL_LOAD_DONE")
    {
        // auto payload = e.GetPayload<QM::QueueItem>();
        // this->SetStatus(QM::QueueStatus::RUNNING, payload.id);
    }
    if (token == "MODEL_LOAD_ERROR" || token == "GENERATION_ERROR")
    {
        auto payload = e.GetPayload<QM::QueueItem>();
        this->SetStatus(QM::QueueStatus::FAILED, payload.id);
        this->isRunning = false;
    }
    if (token == "GENERATION_START")
    {
        auto payload = e.GetPayload<QM::QueueItem>();
        this->SetStatus(QM::QueueStatus::RUNNING, payload.id);
        this->isRunning = true;
    }
    // nothing to todo here, the payload is the generated image list, we can't find whitch item was it...
    // TODO: use struct to payload, and store multiple items in it...
    if (token == "GENERATION_DONE")
    {
    }
}
 
void QM::QueueManager::SaveJobToFile(int id)
{
    auto item = this->GetItem(id);
    this->SaveJobToFile(item);
}
 
void QM::QueueManager::SaveJobToFile(QM::QueueItem item)
{
    nlohmann::json jsonfile(item);
    std::string filename = this->jobsDir + "/" + std::to_string(item.id) + ".json";
    std::ofstream file(filename);
    file << jsonfile;
    file.close();
}
 
int QM::QueueManager::GetCurrentUnixTimestamp()
{
    const auto p1 = std::chrono::system_clock::now();
    return static_cast<int>(std::chrono::duration_cast<std::chrono::seconds>(p1.time_since_epoch()).count());
}
 
void QM::QueueManager::LoadJobListFromDir()
{
 
    if (!std::filesystem::exists(this->jobsDir))
    {
        std::filesystem::create_directories(this->jobsDir);
    }
 
    int i = 0;
    for (auto const &dir_entry : std::filesystem::recursive_directory_iterator(this->jobsDir))
    {
        if (!dir_entry.exists() || !dir_entry.is_regular_file() || !dir_entry.path().has_extension())
        {
            continue;
        }
 
        std::filesystem::path path = dir_entry.path();
 
        std::string ext = path.extension().string();
 
        if (ext != ".json")
        {
            continue;
        }
 
        std::string name = path.filename().replace_extension("").string();
 
        std::ifstream f(path.string());
        try
        {
            nlohmann::json data = nlohmann::json::parse(f);
            auto item = data.get<QM::QueueItem>();
            if (item.status == QM::QueueStatus::RUNNING)
            {
                item.status = QM::QueueStatus::FAILED;
            }
            if (item.status == QM::QueueStatus::MODEL_LOADING)
            {
                item.status = QM::QueueStatus::FAILED;
            }
            this->AddItem(item);
        }
        catch (const std::exception &e)
        {
            std::cerr << e.what() << '\n';
        }
    }
}
 
int QM::QueueManager::GetAnId()
{
    int id = this->GetCurrentUnixTimestamp();
    while (id <= this->lastId)
    {
        id++;
    }
    this->lastId = id;
    return id;
}
 
void QM::QueueManager::onItemAdded(QM::QueueItem item)
{
    // this->parent->m_joblist
    // auto dataTable = this->parent->m_joblist;
}