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  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // CLASS QtShaderDialog - IMPLEMENTATION
55 //
56 //=============================================================================
57 
58 
59 //== INCLUDES =================================================================
60 
61 #include "QtShaderDialog.hh"
62 #include "../Scenegraph/ShaderNode.hh"
63 #include <QFileInfo>
64 #include <QDir>
65 
66 //== NAMESPACES ==============================================================
67 
68 
69 namespace ACG {
70 namespace QtWidgets {
71 
72 
73 //== IMPLEMENTATION ==========================================================
74 
75 
76 QtShaderDialog::QtShaderDialog( QWidget * _parent,
77  SceneGraph::ShaderNode * _node )
78  : QDialog( _parent ),
79  node_(_node)
80 {
81  ui_.setupUi( this );
82 
84 
85  while ( drawmode != ACG::SceneGraph::DrawModes::UNUSED )
86  {
87  ui_.drawModeBox->addItem( QString( drawmode.description().c_str() ) );
88  ++drawmode ;
89  }
90 
91  ui_.shaderDir->setText( QString(_node->shaderDir().c_str()) );
92  ui_.vertexShader->setText( QString(_node->vertexShaderName(ACG::SceneGraph::DrawModes::DEFAULT).c_str()) );
93  ui_.fragmentShader->setText( QString(_node->fragmentShaderName(ACG::SceneGraph::DrawModes::DEFAULT).c_str()) );
94 
95  connect( ui_.okButton, SIGNAL( clicked() ),
96  this, SLOT( applyChanges() ) );
97  connect( ui_.cancelButton, SIGNAL( clicked() ),
98  this, SLOT( reject() ) );
99 
100  connect( ui_.drawModeBox, SIGNAL( currentIndexChanged( int ) ),
101  this, SLOT( comboChanged( int ) ) );
102 }
103 
104 
105 //-----------------------------------------------------------------------------
106 
107 
108 void QtShaderDialog::reject()
109 {
110  std::cerr << "reject" << std::endl;
111  undoChanges();
112  QDialog::reject();
113 }
114 
115 
116 //-----------------------------------------------------------------------------
117 
118 
119 void QtShaderDialog::applyChanges()
120 {
121  // Get and test shader directory
122  std::string shaderDirectory("");
123 
124  QString shaderDir = ui_.shaderDir->text();
125  QDir dir(shaderDir);
126 
127  if ( dir.exists() ) {
128  if ( ! shaderDir.endsWith('/' ) && ! shaderDir.endsWith( '\\' ) ) {
129  shaderDir += "/";
130  }
131 
132  shaderDirectory = std::string( shaderDir.toUtf8() );
133  node_->setShaderDir( shaderDirectory );
134 
135  } else {
136  std::cerr << "Shader directory does not exist" << std::string( shaderDir.toUtf8() ) << std::endl;
137  return;
138  }
139 
140  unsigned int drawMode = 1;
141 
142  for ( int i = 0 ; i < ui_.drawModeBox->currentIndex() ; ++i )
143  drawMode *= 2;
144 
145  node_->setShader(drawMode,
146  std::string( ui_.vertexShader->text().toUtf8() ),
147  std::string( ui_.fragmentShader->text().toUtf8() ) );
148 
149  emit signalNodeChanged(node_);
150 
151  accept();
152 }
153 
154 
155 //-----------------------------------------------------------------------------
156 
157 
158 void QtShaderDialog::undoChanges()
159 {
160  std::cerr << "undo" << std::endl;
161  emit signalNodeChanged(node_);
162 }
163 
164 void QtShaderDialog::comboChanged ( int index ) {
165  unsigned int drawMode = 1;
166 
167  for ( int i = 0 ; i < index; ++i )
168  drawMode *= 2;
169 
170  QString vertexShader(node_->vertexShaderName(drawMode).c_str());
171  QString fragmentShader(node_->fragmentShaderName(drawMode).c_str());
172 
173 
174  QString shaderDir( node_->shaderDir().c_str() );
175  vertexShader = vertexShader.remove( shaderDir );
176  fragmentShader = fragmentShader.remove( shaderDir );
177 
178  ui_.vertexShader->setText( vertexShader );
179  ui_.fragmentShader->setText( fragmentShader );
180 }
181 
182 
183 //=============================================================================
184 } // namespace QtWidgets
185 } // namespace ACG
186 //=============================================================================
DrawMode DEFAULT
use the default (global) draw mode and not the node&#39;s own.
Definition: DrawModes.cc:78
void signalNodeChanged(ACG::SceneGraph::BaseNode *_node)
ACG::SceneGraph::ShaderNode ShaderNode
Simple Name for ShaderNode.
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
DrawMode UNUSED
marks the last used ID
Definition: DrawModes.cc:111
ACG::SceneGraph::DrawModes::DrawMode drawMode(int _viewer)
Get the current draw Mode of a Viewer.