wxWidgets based Stable Diffusion C++ GUi
Ferenc Szontágh
2024-02-03 875477e097c353a368b5fbf5b5f048c956fc4743
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
#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;
}
 
int QM::QueueManager::AddItem(QM::QueueItem item)
{
    if (!item.id)
    {
        item.id = this->GetCurrentUnixTimestamp();
    }
    this->QueueList[item.id] = item;
    this->SendEventToMainWindow(QM::QueueEvents::ITEM_ADDED, item);
    return item.id;
}
 
int QM::QueueManager::AddItem(sd_gui_utils::SDParams *params)
{
    QM::QueueItem item;
    item.params = *params;
    item.created_at = item.id = this->GetCurrentUnixTimestamp();
    this->AddItem(item);
    return item.id;
}
 
int QM::QueueManager::AddItem(sd_gui_utils::SDParams params)
{
    QM::QueueItem item;
    item.params = params;
    item.created_at = item.id = this->GetCurrentUnixTimestamp();
    this->AddItem(item);
    return item.id;
}
 
QM::QueueItem QM::QueueManager::GetItem(int id)
{
    if (this->QueueList.find(id) == this->QueueList.end())
    {
        return QM::QueueItem();
    }
    else
    {
        return this->QueueList.at(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)
{
    // todo, start - stop - pause
    auto item = this->GetItem(id);
    item.status = status;
    this->SendEventToMainWindow(QM::QueueEvents::ITEM_STATUS_CHANGED, item);
}
 
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("MODEL_LOAD_START:%s", this->sd_params->model_path));
    e->SetString(wxString("QUEUE:" + (int)eventType));
    e->SetPayload(item);
    wxQueueEvent(this->eventHandler, e);
}
 
void QM::QueueManager::OnThreadMessage(wxThreadEvent &e)
{
    auto msg = e.GetString().ToStdString();
 
    std::string token = msg.substr(0, msg.find(":"));
    std::string content = msg.substr(msg.find(":") + 1);
    // only numbers here...
    QM::QueueEvents event = (QM::QueueEvents)std::stoi(content);
    // only handle the QUEUE messages, what this class generate
}
 
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::onItemAdded(QM::QueueItem item)
{
    // this->parent->m_joblist
    // auto dataTable = this->parent->m_joblist;
}