Vuo  2.3.2
VuoCodeEditorStages.cc
Go to the documentation of this file.
1 
10 #include "VuoCodeEditorStages.hh"
11 
12 #include "VuoCodeEditor.hh"
13 #include "VuoCodeGutter.hh"
14 #include "VuoEditor.hh"
15 #include "VuoRendererColors.hh"
16 
21  : QTabWidget(parent)
22 {
23 // vertex = new VuoCodeEditor(QString::fromStdString(shaderFile->vertexSource()));
24 // addTab(vertex, "Vertex Shader");
25  vertex = NULL;
26 
27 // if (shaderFile->typeAllowsGeometryShader())
28 // {
29 // geometry = new VuoCodeEditor(QString::fromStdString(shaderFile->geometrySource()));
30 // addTab(geometry, "Geometry Shader");
31 // }
32 // else
33  geometry = NULL;
34 
35 // if (shaderFile->typeAllowsFragmentShader())
36 // {
37  fragment = new VuoCodeEditor(QString::fromStdString(shaderFile->fragmentSource()));
38  addTab(fragment, "Fragment Shader");
39 // }
40 // else
41 // fragment = NULL;
42 
43  VuoShaderFile::Type type = shaderFile->type();
44  if (type == VuoShaderFile::GLSLImageFilter
45  || type == VuoShaderFile::GLSLImageGenerator
46  || type == VuoShaderFile::GLSLImageTransition
47  || type == VuoShaderFile::GLSLObjectRenderer)
48  {
49  setCurrentWidget(fragment);
50  fragment->setFocus();
51  }
52  else // if (type == VuoShaderFile::GLSLObjectFilter)
53  {
54  setCurrentWidget(vertex);
55  vertex->setFocus();
56  }
57 
58  if (vertex)
59  connect(vertex->document(), &QTextDocument::modificationChanged, this, &VuoCodeEditorStages::modificationMayHaveChanged);
60  if (geometry)
61  connect(geometry->document(), &QTextDocument::modificationChanged, this, &VuoCodeEditorStages::modificationMayHaveChanged);
62  if (fragment)
63  connect(fragment->document(), &QTextDocument::modificationChanged, this, &VuoCodeEditorStages::modificationMayHaveChanged);
64 
65  VuoEditor *editor = static_cast<VuoEditor *>(qApp);
66  connect(editor, &VuoEditor::darkInterfaceToggled, this, &VuoCodeEditorStages::updateColor);
67  updateColor(editor->isInterfaceDark());
68 
69  connect(this, &QTabWidget::currentChanged, this, &VuoCodeEditorStages::updatePosition);
70 }
71 
76 {
77  return vertex ? vertex : (geometry ? geometry : fragment);
78 }
79 
84 {
85  return (vertex && vertex->document()->isModified()) ||
86  (geometry && geometry->document()->isModified()) ||
87  (fragment && fragment->document()->isModified());
88 }
89 
94 {
95  if (vertex)
96  vertex->document()->setModified(false);
97  if (geometry)
98  geometry->document()->setModified(false);
99  if (fragment)
100  fragment->document()->setModified(false);
101 }
102 
107 {
108  if (vertex)
109  vertex->zoom11();
110  if (geometry)
111  geometry->zoom11();
112  if (fragment)
113  fragment->zoom11();
114 }
115 
120 {
121  if (vertex)
122  vertex->zoomIn();
123  if (geometry)
124  geometry->zoomIn();
125  if (fragment)
126  fragment->zoomIn();
127 }
128 
133 {
134  if (vertex)
135  vertex->zoomOut();
136  if (geometry)
137  geometry->zoomOut();
138  if (fragment)
139  fragment->zoomOut();
140 }
141 
142 void VuoCodeEditorStages::keyPressEvent(QKeyEvent *event)
143 {
144  int key = event->key();
145  Qt::KeyboardModifiers modifiers = event->modifiers();
146 
147  if ( (key == Qt::Key_BracketRight && modifiers & Qt::ControlModifier && modifiers & Qt::ShiftModifier) // Safari: cmd-shift-]
148  || (key == Qt::Key_Right && modifiers & Qt::ControlModifier && modifiers & Qt::AltModifier )) // Chrome: cmd-option-rightarrow
149  setCurrentIndex(currentIndex() + 1);
150  else if ( (key == Qt::Key_BracketLeft && modifiers & Qt::ControlModifier && modifiers & Qt::ShiftModifier)
151  || (key == Qt::Key_Left && modifiers & Qt::ControlModifier && modifiers & Qt::AltModifier ))
152  setCurrentIndex(currentIndex() - 1);
153  else
154  QTabWidget::keyPressEvent(event);
155 }
156 
157 void VuoCodeEditorStages::updateColor(bool isDark)
158 {
159  if (vertex)
160  vertex->updateColor(isDark);
161  if (geometry)
162  geometry->updateColor(isDark);
163  if (fragment)
164  fragment->updateColor(isDark);
165 
166  VuoCodeEditor *e = someEditor();
167 
168  VuoRendererColors colors;
169  colors.setDark(isDark);
170 
171  QColor tabBackground = e->gutterColor.lighter(isDark ? 90 : 105);
172  QColor tabBorder = e->gutterColor.lighter(isDark ? 110 : 97);
173 
174  VuoCodeEditor *codeEditor = static_cast<VuoCodeEditor *>(currentWidget());
175  int gutterWidth = codeEditor->gutter->width();
176 
177  setStyleSheet(VUO_QSTRINGIFY(
178  QTabWidget::pane {
179  background: %1;
180  }
181 
182  QTabWidget::tab-bar {
183  left: %8px;
184  }
185 
186  QTabBar::tab {
187  padding: 2px 12px 2px 12px;
188  border: 1px solid %7;
189  margin-right: -1px;
190  }
191 
192  QTabBar::tab:selected {
193  background: %3;
194  color: %4;
195  border-top: 2px solid %2;
196  border-bottom: 1px solid %3;
197  border-top-left-radius: 4px;
198  border-top-right-radius: 4px;
199  }
200 
201  QTabBar::tab:last {
202  margin-right: 0;
203  }
204 
205  QTabBar::tab:!selected {
206  margin-top: 4px;
207  background: %6;
208  color: %5;
209  }
210 
211  VuoCodeEditor {
212  border: none;
213  border-bottom: 1px solid %1;
214  })
215  .arg(e->gutterColor.name())
216  .arg(colors.selection().name())
217  .arg(colors.canvasFill().name())
218  .arg(e->palette().text().color().name())
219  .arg(colors.nodeFrame().name())
220  .arg(tabBackground.name())
221  .arg(tabBorder.name())
222  .arg(gutterWidth - 1)
223  );
224 }
225 
230 {
231  VuoEditor *editor = static_cast<VuoEditor *>(qApp);
232  // To update part of the stylesheet we unfortunately need to update the whole stylesheet.
233  updateColor(editor->isInterfaceDark());
234 }
235 
240 {
241  return static_cast<VuoCodeEditor *>(currentWidget());
242 }
243 
248 {
249  if (currentWidget() == vertex)
250  return VuoShaderFile::Vertex;
251  else if (currentWidget() == geometry)
252  return VuoShaderFile::Geometry;
253  else // if (currentWidget() == fragment)
254  return VuoShaderFile::Fragment;
255 }
256 
261 {
262  if (stage == VuoShaderFile::Vertex)
263  setCurrentWidget(vertex);
264  else if (stage == VuoShaderFile::Geometry)
265  setCurrentWidget(geometry);
266  else if (stage == VuoShaderFile::Fragment)
267  setCurrentWidget(fragment);
268 }