]> git.cworth.org Git - vogl/blob - src/vogleditor/vogleditor_qprogramexplorer.cpp
UI: Improved support for shared contexts and viewing shared state objects
[vogl] / src / vogleditor / vogleditor_qprogramexplorer.cpp
1 #include "vogleditor_qprogramexplorer.h"
2 #include "ui_vogleditor_qprogramexplorer.h"
3
4 #include "vogl_gl_object.h"
5 #include "vogl_gl_state_snapshot.h"
6 #include "vogl_program_state.h"
7
8 Q_DECLARE_METATYPE(vogl_program_state*);
9 Q_DECLARE_METATYPE(vogl_shader_state*);
10
11 vogleditor_QProgramExplorer::vogleditor_QProgramExplorer(QWidget *parent) :
12     QWidget(parent),
13     ui(new Ui::vogleditor_QProgramExplorer)
14 {
15     ui->setupUi(this);
16
17     ui->saveShaderButton->setEnabled(false);
18 }
19
20 vogleditor_QProgramExplorer::~vogleditor_QProgramExplorer()
21 {
22     delete ui;
23 }
24
25 void vogleditor_QProgramExplorer::clear()
26 {
27     ui->programListBox->clear();
28     ui->shaderListBox->clear();
29
30     ui->shaderTextEdit->clear();
31 }
32
33 uint vogleditor_QProgramExplorer::set_program_objects(vogl::vector<vogl_context_snapshot*> sharingContexts)
34 {
35     clear();
36
37     uint programCount = 0;
38     for (uint c = 0; c < sharingContexts.size(); c++)
39     {
40         vogl_gl_object_state_ptr_vec programObjects;
41         sharingContexts[c]->get_all_objects_of_category(cGLSTProgram, programObjects);
42
43         programCount += add_program_objects(programObjects);
44     }
45
46     return programCount;
47 }
48
49 uint vogleditor_QProgramExplorer::add_program_objects(vogl_gl_object_state_ptr_vec objects)
50 {
51     m_objects.append(objects);
52
53     for (vogl_gl_object_state_ptr_vec::iterator iter = objects.begin(); iter != objects.end(); iter++)
54     {
55         if ((*iter)->get_type() == cGLSTProgram)
56         {
57             vogl_program_state* pState = static_cast<vogl_program_state*>(*iter);
58
59             QString valueStr;
60             valueStr = valueStr.sprintf("Program %" PRIu64, pState->get_snapshot_handle());
61
62             ui->programListBox->addItem(valueStr, QVariant::fromValue(pState));
63         }
64         else
65         {
66             VOGL_ASSERT(!"Unhandled object type in vogleditor_QProgramExplorer");
67         }
68     }
69
70     return objects.size();
71 }
72
73 bool vogleditor_QProgramExplorer::set_active_program(unsigned long long programHandle)
74 {
75     bool bActivated = false;
76     int index = 0;
77     for (vogl_gl_object_state_ptr_vec::iterator iter = m_objects.begin(); iter != m_objects.end(); iter++)
78     {
79         vogl_program_state* pState = static_cast<vogl_program_state*>(*iter);
80         if (pState->get_snapshot_handle() == programHandle)
81         {
82             ui->programListBox->setCurrentIndex(index);
83             bActivated = true;
84             break;
85         }
86
87         ++index;
88     }
89     return bActivated;
90 }
91
92 void vogleditor_QProgramExplorer::on_programListBox_currentIndexChanged(int index)
93 {
94     ui->shaderListBox->clear();
95
96     int count = ui->programListBox->count();
97     if (index >= 0 && index < count)
98     {
99         vogl_program_state* pObjState = ui->programListBox->itemData(index).value<vogl_program_state*>();
100         if (pObjState == NULL)
101         {
102             VOGL_ASSERT(!"NULL object type in vogleditor_QProgramExplorer");
103             return;
104         }
105
106         vogl_shader_state_vec& attachedShaderVec = pObjState->get_shaders();
107         vogl_shader_state_vec& linkedShaderVec = pObjState->get_link_time_snapshot()->get_shaders();
108
109         QString valueStr;
110         for (vogl_shader_state_vec::iterator linkedIter = linkedShaderVec.begin(); linkedIter != linkedShaderVec.end(); linkedIter++)
111         {
112             vogl_shader_state* pShaderState = linkedIter;
113             valueStr = valueStr.sprintf("Linked Shader %" PRIu64 " - %s", pShaderState->get_snapshot_handle(), g_gl_enums.find_gl_name(pShaderState->get_shader_type()));
114             ui->shaderListBox->addItem(valueStr, QVariant::fromValue(pShaderState));
115         }
116
117         for (vogl_shader_state_vec::iterator attachedIter = attachedShaderVec.begin(); attachedIter != attachedShaderVec.end(); attachedIter++)
118         {
119             vogl_shader_state* pShaderState = attachedIter;
120             valueStr = valueStr.sprintf("Attached Shader %" PRIu64 " - %s", pShaderState->get_snapshot_handle(), g_gl_enums.find_gl_name(pShaderState->get_shader_type()));
121             ui->shaderListBox->addItem(valueStr, QVariant::fromValue(pShaderState));
122         }
123     }
124 }
125
126 void vogleditor_QProgramExplorer::on_shaderListBox_currentIndexChanged(int index)
127 {
128     ui->shaderTextEdit->clear();
129
130     int count = ui->shaderListBox->count();
131     if (index >= 0 && index < count)
132     {
133         vogl_shader_state* pObjState = ui->shaderListBox->itemData(index).value<vogl_shader_state*>();
134         if (pObjState == NULL)
135         {
136             VOGL_ASSERT(!"NULL shader object type in vogleditor_QProgramExplorer");
137         }
138         else
139         {
140             ui->shaderTextEdit->setText(pObjState->get_source().c_str());
141         }
142     }
143
144     ui->saveShaderButton->setEnabled(false);
145 }
146
147 void vogleditor_QProgramExplorer::on_shaderTextEdit_textChanged()
148 {
149     ui->saveShaderButton->setEnabled(true);
150 }
151
152 void vogleditor_QProgramExplorer::on_saveShaderButton_clicked()
153 {
154     int index = ui->shaderListBox->currentIndex();
155     if (index >= 0 && ui->shaderListBox->count() > 0)
156     {
157         vogl_shader_state* pObjState = ui->shaderListBox->itemData(index).value<vogl_shader_state*>();
158         if (pObjState == NULL)
159         {
160             VOGL_ASSERT(!"NULL shader object type in vogleditor_QProgramExplorer");
161             return;
162         }
163
164         pObjState->set_source(ui->shaderTextEdit->toPlainText().toStdString().c_str());
165
166         ui->saveShaderButton->setEnabled(false);
167
168         vogl_program_state* pProgramState = ui->programListBox->itemData(index).value<vogl_program_state*>();
169         emit program_edited(pProgramState);
170     }
171 }