Developer Documentation
QtShaderDialog.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 
43 
44 
45 
46 //=============================================================================
47 //
48 // CLASS QtShaderDialog - IMPLEMENTATION
49 //
50 //=============================================================================
51 
52 
53 //== INCLUDES =================================================================
54 
55 #include "QtShaderDialog.hh"
56 #include "../Scenegraph/ShaderNode.hh"
57 #include "../Scenegraph/DrawModes.hh"
58 #include <QFileInfo>
59 #include <QDir>
60 
61 //== NAMESPACES ==============================================================
62 
63 
64 namespace ACG {
65 namespace QtWidgets {
66 
67 
68 //== IMPLEMENTATION ==========================================================
69 
70 
71 QtShaderDialog::QtShaderDialog( QWidget * _parent,
72  SceneGraph::ShaderNode * _node )
73  : QDialog( _parent ),
74  node_(_node)
75 {
76  ui_.setupUi( this );
77 
79 
80  while ( drawmode != ACG::SceneGraph::DrawModes::UNUSED )
81  {
82  ui_.drawModeBox->addItem( QString( drawmode.description().c_str() ) );
83  ++drawmode ;
84  }
85 
86  ui_.shaderDir->setText( QString(_node->shaderDir().c_str()) );
87  ui_.vertexShader->setText( QString(_node->vertexShaderName(ACG::SceneGraph::DrawModes::DEFAULT).c_str()) );
88  ui_.fragmentShader->setText( QString(_node->fragmentShaderName(ACG::SceneGraph::DrawModes::DEFAULT).c_str()) );
89 
90  connect( ui_.okButton, SIGNAL( clicked() ),
91  this, SLOT( applyChanges() ) );
92  connect( ui_.cancelButton, SIGNAL( clicked() ),
93  this, SLOT( reject() ) );
94 
95  connect( ui_.drawModeBox, SIGNAL( currentIndexChanged( int ) ),
96  this, SLOT( comboChanged( int ) ) );
97 }
98 
99 
100 //-----------------------------------------------------------------------------
101 
102 
103 void QtShaderDialog::reject()
104 {
105  std::cerr << "reject" << std::endl;
106  undoChanges();
107  QDialog::reject();
108 }
109 
110 
111 //-----------------------------------------------------------------------------
112 
113 
114 void QtShaderDialog::applyChanges()
115 {
116  // Get and test shader directory
117  std::string shaderDirectory("");
118 
119  QString shaderDir = ui_.shaderDir->text();
120  QDir dir(shaderDir);
121 
122  if ( dir.exists() ) {
123  if ( ! shaderDir.endsWith('/' ) && ! shaderDir.endsWith( '\\' ) ) {
124  shaderDir += "/";
125  }
126 
127  shaderDirectory = std::string( shaderDir.toUtf8() );
128  node_->setShaderDir( shaderDirectory );
129 
130  } else {
131  std::cerr << "Shader directory does not exist" << std::string( shaderDir.toUtf8() ) << std::endl;
132  return;
133  }
134 
135  SceneGraph::DrawModes::DrawMode drawMode = SceneGraph::DrawModes::DrawMode(1);
136 
137  for ( int i = 0 ; i < ui_.drawModeBox->currentIndex() ; ++i )
138  ++drawMode;
139 
140  node_->setShader(drawMode,
141  std::string( ui_.vertexShader->text().toUtf8() ),
142  std::string( ui_.fragmentShader->text().toUtf8() ) );
143 
144  emit signalNodeChanged(node_);
145 
146  accept();
147 }
148 
149 
150 //-----------------------------------------------------------------------------
151 
152 
153 void QtShaderDialog::undoChanges()
154 {
155  std::cerr << "undo" << std::endl;
156  emit signalNodeChanged(node_);
157 }
158 
159 void QtShaderDialog::comboChanged ( int index ) {
161 
162  for ( int i = 0 ; i < index; ++i )
163  ++drawMode;
164 
165  QString vertexShader(node_->vertexShaderName(drawMode).c_str());
166  QString fragmentShader(node_->fragmentShaderName(drawMode).c_str());
167 
168 
169  QString shaderDir( node_->shaderDir().c_str() );
170  vertexShader = vertexShader.remove( shaderDir );
171  fragmentShader = fragmentShader.remove( shaderDir );
172 
173  ui_.vertexShader->setText( vertexShader );
174  ui_.fragmentShader->setText( fragmentShader );
175 }
176 
177 
178 //=============================================================================
179 } // namespace QtWidgets
180 } // namespace ACG
181 //=============================================================================
ACG::SceneGraph::ShaderNode ShaderNode
Simple Name for ShaderNode.
Namespace providing different geometric functions concerning angles.
DrawMode DEFAULT
use the default (global) draw mode and not the node&#39;s own.
Definition: DrawModes.cc:72
DrawMode UNUSED
marks the last used ID
Definition: DrawModes.cc:105
ACG::SceneGraph::DrawModes::DrawMode drawMode(int _viewer)
Get the current draw Mode of a Viewer.
void signalNodeChanged(ACG::SceneGraph::BaseNode *_node)