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
#ifndef __MainWindowUI__
#define __MainWindowUI__
 
/**
@file
Subclass of UI, which is generated by wxFormBuilder.
*/
 
#include "MainWindow.h"
 
//// end generated include
#include "ver.hpp"
#include "MainWindowSettings.h"
#include "MainWindowImageViewer.h"
#include "QueueManager.h"
 
#include <filesystem>
#include <map>
#include <thread>
#include <vector>
#include <fstream>
#include <mutex>
 
#include <fmt/format.h>
#include "utils.hpp"
#include <stable-diffusion.h>
 
#include <wx/fileconf.h>
#include <wx/stdpaths.h>
#include <wx/event.h>
#include <wx/notifmsg.h>
#include <wx/textdlg.h>
#include <wx/menu.h>
 
/** Implementing UI */
class MainWindowUI : public UI
{
    protected:
        // Handlers for UI events.
        void onSettings( wxCommandEvent& event );
        void onModelsRefresh( wxCommandEvent& event );
        void onModelSelect( wxCommandEvent& event );
        void onVaeSelect( wxCommandEvent& event );
        void onResolutionSwap( wxCommandEvent& event );
        void onJobsStart( wxCommandEvent& event );
        void onJobsPause( wxCommandEvent& event );
        void onJobsDelete( wxCommandEvent& event );
        void onJoblistItemActivated( wxDataViewEvent& event );
        void onContextMenu( wxDataViewEvent& event );
        void onJoblistSelectionChanged( wxDataViewEvent& event );
        void onGenerate( wxCommandEvent& event );
        void onSamplerSelect( wxCommandEvent& event );
        void onSavePreset( wxCommandEvent& event );
        void onLoadPreset( wxCommandEvent& event );
        void onSelectPreset( wxCommandEvent& event );
        void onDeletePreset( wxCommandEvent& event );
    public:
        /** Constructor */
        MainWindowUI( wxWindow* parent );
    //// end generated class members
    ~MainWindowUI();
    void OnThreadMessage(wxThreadEvent &e);
 
private:
    std::mutex sdMutex;
    wxFileConfig *fileConfig;
    sd_gui_utils::config *cfg;
    wxString ini_path;
    MainWindowSettings *settingsWindow;
    sd_gui_utils::SDParams *sd_params;
 
    std::map<std::string, std::string> ModelFiles;
    std::map<std::string, std::string> VaeFiles;
    std::map<std::string, sd_gui_utils::generator_preset> Presets;
 
    // the queue manager
    QM::QueueManager *qmanager;
    bool modelLoaded = false;
    std::string currentModel;
    sd_ctx_t *sd_ctx;
    std::streambuf *buffer;
    std::vector<std::thread> threads;
 
    // row,QueueItem
    std::map<int, QM::QueueItem*> JobTableItems;
    std::map<int, wxDataViewColumn *> *JobTableColumns;
 
    void initConfig();
    void loadModelList();
    void loadVaeList();
    void OnCloseSettings(wxCloseEvent &event);
    void LoadFileList(sd_gui_utils::DirTypes type = sd_gui_utils::DirTypes::CHECKPOINT);
    void LoadPresets();
 
    static void HandleSDLog(sd_log_level_t level, const char *text, void *data);
    static void HandleSDProgress(int step, int steps, float time, void *data);
 
    // load the model in a new thread
    sd_ctx_t *LoadModelv2(wxEvtHandler *eventHandler, QM::QueueItem myItem);
    // generate in another thread
    void Generate(wxEvtHandler *eventHandler, QM::QueueItem myItem);
 
    // start a thread to generate image
    void StartGeneration(QM::QueueItem myJob);
 
    // handle queue managers events, manipulate data table by events
    void OnQueueItemManagerItemAdded(QM::QueueItem item);
    void OnQueueItemManagerItemUpdated(QM::QueueItem item);
    void OnQueueItemManagerItemStatusChanged(QM::QueueItem item);
 
    wxNotificationMessage *notification;
 
 
};
 
#endif // __MainWindowUI__