Developer documentation | Axl-2.5.1

axlInspectorObjectMesh.cpp
Go to the documentation of this file.
1 /* axlInspectorObjectMesh.cpp ---
2  *
3  * Author: Julien Wintz
4  * Copyright (C) 2008 - Julien Wintz, Inria.
5  * Created: Fri Mar 18 11:19:52 2011 (+0100)
6  * Version: $Id$
7  * Last-Updated: Tue Apr 3 14:21:04 2012 (+0200)
8  * By: Julien Wintz
9  * Update #: 60
10  */
11 
12 /* Commentary:
13  *
14  */
15 
16 /* Change log:
17  *
18  */
19 
20 #include "axlInspectorObjectMesh.h"
21 #include "axlParameterSpace.h"
22 
23 #include <axlCore/axlMesh.h>
27 
28 #include <dtkCoreSupport/dtkAbstractData.h>
29 
30 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
31 
32 
33 #include <dtkGuiSupport/dtkColorButton.h>
34 #include <dtkGuiSupport/dtkSplitter.h>
35 
36 #include <QtGui>
37 
38 class axlInspectorObjectMeshPrivate
39 {
40 public:
41  axlAbstractData *data;
42 
43  QSlider *sliderSize;
44 
45  dtkColorButton *colorButton;
46  QComboBox *comboBoxShader;
47 
48  QComboBox *comboInterpolation;
49 
50  QCheckBox *checkBoxShader;
51  QLineEdit *lineEditShader;
52  QPushButton *buttonShader;
53 
54  QCheckBox *checkBoxPoints;
55  QCheckBox *checkBoxUseNormals;
56  QCheckBox *checkBoxLines;
57  QCheckBox *checkBoxPoly;
58 
59  QSlider *sliderOpacity;
60 
61 };
62 
64  axlInspectorObjectInterface(parent), //QFrame(parent),
65  d(new axlInspectorObjectMeshPrivate)
66 {
67  d->data = NULL;
68  d->sliderSize = NULL;
69  d->colorButton = NULL;
70  d->comboBoxShader = NULL;
71 
72  d->comboInterpolation = NULL;
73 
74  d->checkBoxShader = NULL;
75  d->lineEditShader = NULL;
76  d->buttonShader = NULL;
77 
78  d->sliderOpacity = NULL;
79 
80 
81 }
82 
84 {
85  delete d;
86 
87  d = NULL;
88 }
89 
91 {
92  return QSize(300, 300);
93 }
94 
96 {
97  d->data = data;
98  initWidget();
99  // Detect the data composition
100  this->setObjectDataState();
101 }
102 
103 void axlInspectorObjectMesh::setObjectDataState(void)
104 {
105  axlMesh *mesh;
106 
107  mesh = d->data->mesh();
108 
109  if(!mesh){
110 
111  if(!(mesh = dynamic_cast<axlMesh *>(d->data)))
112  {
113  // we have to transform the data in a mesh
114  foreach(QString t, dtkAbstractDataFactory::instance()->converters())
115  {
116  if(!mesh)
117  {
118  axlAbstractDataConverter *converter = qobject_cast<axlAbstractDataConverter *>(dtkAbstractDataFactory::instance()->converter(t));
119  if(converter){
120  converter->setData(d->data);
121  mesh = converter->toMesh();
122  }
123  }
124  }
125  } // transform the axlAbstractData into mesh
126 
127  }
128 
129  if(mesh)
130  {
131  d->data->setMesh(mesh);
132  d->checkBoxPoly->setChecked(mesh->face_show());
133  d->checkBoxLines->setChecked(mesh->edge_show());
134  d->checkBoxPoints->setChecked(mesh->vertex_show());
135 
136  if(mesh->normal_count() == mesh->vertex_count() && mesh->normal_count() != 0)
137  {
138  if(mesh->normal_used())
139  d->checkBoxUseNormals->setChecked(true);
140  else
141  d->checkBoxUseNormals->setChecked(false);
142  }
143  else
144  {
145  d->checkBoxUseNormals->setDisabled(true);
146  d->comboInterpolation->setCurrentIndex(0);
147  d->comboInterpolation->setDisabled(true);
148  }
149 
150  }
151 
152  // because for now we don't want to edit the checkbox :
153  // d->checkBoxPoints->setDisabled(true);
154  // d->checkBoxLines->setDisabled(true);
155  // d->checkBoxPoly->setDisabled(true);
156  // d->checkBoxUseNormals->setDisabled(true);
157 
158 }
159 
160 void axlInspectorObjectMesh::initWidget()
161 {
163 
164  d->sliderSize = new QSlider(Qt::Horizontal, this);
165 
166  QHBoxLayout *layoutSize = new QHBoxLayout;
167  layoutSize->addWidget(new QLabel("Size",this));
168  layoutSize->addWidget(d->sliderSize);
169  d->sliderSize->setMinimum(-800);
170  d->sliderSize->setMaximum(500);
171  d->sliderSize->setValue(initSizeValue());
172 
174 
175  d->sliderOpacity = new QSlider(Qt::Horizontal, this);
176  d->sliderOpacity->setMaximum(100);
177  QHBoxLayout *layoutOpacity = new QHBoxLayout;
178  layoutOpacity->addWidget(new QLabel("Opacity",this));
179  layoutOpacity->addWidget(d->sliderOpacity);
180  d->sliderOpacity->setValue(initOpacityValue());
181 
183  d->comboBoxShader = new QComboBox(this);
184  d->comboBoxShader->setInsertPolicy(QComboBox::InsertAlphabetically);
185 
186  d->checkBoxShader = new QCheckBox(this);
187  d->lineEditShader = new QLineEdit(this);
188  d->buttonShader = new QPushButton(this);
189  d->buttonShader->setText("open");
190  d->lineEditShader->setText(this->initShaderValue());
191  this->initComboBoxShaderValue();
192 
193  if(d->lineEditShader->text().isEmpty())
194  {
195  d->lineEditShader->setEnabled(false);
196  d->buttonShader->setEnabled(false);
197  d->comboBoxShader->setEnabled(false);
198  }
199  else
200  d->checkBoxShader->setChecked(true);
201 
202  QVBoxLayout *layoutShader = new QVBoxLayout;
203  QHBoxLayout *layoutShader1 = new QHBoxLayout;
204 
205  QLabel *labelShader = new QLabel("Shader",this);
206  layoutShader1->addWidget(labelShader);
207  layoutShader1->addWidget(d->checkBoxShader);
208  layoutShader1->addWidget(d->comboBoxShader);
209  layoutShader1->addWidget(d->buttonShader);
210 
211  layoutShader1->setStretchFactor(labelShader, 2);
212  layoutShader1->setStretchFactor(d->checkBoxShader, 1);
213  layoutShader1->setStretchFactor(d->comboBoxShader, 4);
214  layoutShader1->setStretchFactor(d->buttonShader, 3);
215 
216  layoutShader->addLayout(layoutShader1);
217  layoutShader->addWidget(d->lineEditShader);
218 
220 
221  d->colorButton = new dtkColorButton(this);
222  QHBoxLayout *layoutColorButton = new QHBoxLayout;
223  layoutColorButton->addWidget(new QLabel("Color",this));
224  layoutColorButton->addWidget(d->colorButton);
225  d->colorButton->setColor(this->initColorValue());
226 
228  d->comboInterpolation = new QComboBox(this);
229  d->comboInterpolation->setInsertPolicy(QComboBox::InsertAlphabetically);
230  d->comboInterpolation->addItems(QStringList()<<"Flat"<<"Gouraud"<<"Phong");
231  QHBoxLayout *layoutInterpolationCombobox = new QHBoxLayout;
232  layoutInterpolationCombobox->addWidget(new QLabel("Interpolation",this));
233  layoutInterpolationCombobox->addWidget(d->comboInterpolation);
234  if (axlMesh *meshData = dynamic_cast<axlMesh *>(d->data))
235  d->comboInterpolation->setCurrentIndex(meshData->interpolation());
236 
238  d->checkBoxPoints = new QCheckBox(this);
239  d->checkBoxLines = new QCheckBox(this);
240  d->checkBoxPoly = new QCheckBox(this);
241  d->checkBoxUseNormals = new QCheckBox(this);
242 
243 
244  QHBoxLayout *layoutPoints = new QHBoxLayout;
245  layoutPoints->addWidget(new QLabel("Show Points",this));
246  layoutPoints->addWidget(d->checkBoxPoints);
247 
248  QHBoxLayout *layoutLines = new QHBoxLayout;
249  layoutLines->addWidget(new QLabel("Show Edges",this));
250  layoutLines->addWidget(d->checkBoxLines);
251 
252  QHBoxLayout *layoutPolygons = new QHBoxLayout;
253  layoutPolygons->addWidget(new QLabel("Show Faces",this));
254  layoutPolygons->addWidget(d->checkBoxPoly);
255 
256  QHBoxLayout *layoutNormals = new QHBoxLayout;
257  layoutNormals->addWidget(new QLabel("Use Normals",this));
258  layoutNormals->addWidget(d->checkBoxUseNormals);
259 
261 
262  QVBoxLayout *layoutTop = new QVBoxLayout;
263  layoutTop->addWidget(new QLabel("axlInspectorObjectMesh", this));
264  layoutTop->addLayout(layoutColorButton);
265  layoutTop->addLayout(layoutSize);
266  layoutTop->addLayout(layoutOpacity);
267  layoutTop->addLayout(layoutShader);
268  layoutTop->addLayout(layoutInterpolationCombobox);
269  layoutTop->addLayout(layoutPoints);
270  layoutTop->addLayout(layoutLines);
271  layoutTop->addLayout(layoutPolygons);
272  layoutTop->addLayout(layoutNormals);
273  layoutTop->addStretch(1);
274 
275  QWidget *top = new QWidget(this);
276  top->setLayout(layoutTop);
277  top->setMaximumWidth(295);
278 
279  dtkSplitter *splitter = new dtkSplitter(this, true);
280  splitter->setOrientation(Qt::Vertical);
281  splitter->addWidget(top);
282 
283  QVBoxLayout *layout = new QVBoxLayout(this);
284  layout->setContentsMargins(0, 0, 0, 0);
285  layout->setSpacing(0);
286  layout->addWidget(splitter);
287 
288 
289  connect(d->colorButton, SIGNAL(colorChanged(QColor)), this, SLOT(onColorChanged(QColor)));
290  connect(d->sliderSize, SIGNAL(valueChanged(int)), this, SLOT(onSizeChanged(int)));
291  connect(d->sliderOpacity, SIGNAL(valueChanged(int)), this, SLOT(onOpacityChanged(int)));
292  connect(d->comboBoxShader, SIGNAL(currentIndexChanged(QString)), this, SLOT(onLineEditShaderChanged(QString)));
293  connect(d->comboInterpolation, SIGNAL(currentIndexChanged(int)), this, SLOT(onInterpolationChanged(int)));
294  connect(d->checkBoxShader, SIGNAL(clicked(bool)), this, SLOT(onShaderStateChanged(bool)));
295  connect(d->buttonShader, SIGNAL(clicked()), this, SLOT(openShader()));
296  connect(d->lineEditShader, SIGNAL(textChanged(QString)), this, SLOT(onShaderChanged(QString)));
297 
298  connect(d->checkBoxPoints, SIGNAL(clicked()), this, SLOT(onUpdateGeometry()));
299  connect(d->checkBoxLines, SIGNAL(clicked()), this, SLOT(onUpdateGeometry()));
300  connect(d->checkBoxPoly, SIGNAL(clicked()), this, SLOT(onUpdateGeometry()));
301  connect(d->checkBoxUseNormals, SIGNAL(clicked()), this, SLOT(onUpdateGeometry()));
302 
303 }
304 
305 
306 int axlInspectorObjectMesh::initSizeValue(void)
307 {
308  double initSize = 100.0*(log(d->data->size()/0.125))/log(2.0);
309  // if(initSize > 10.0)
310  // initSize = 10;
311  // else if(initSize < 0.0)
312  // initSize = 0;
313 
314  return initSize;
315 }
316 
317 void axlInspectorObjectMesh::initComboBoxShaderValue(void)
318 {
319  if(d->comboBoxShader) {
320 
321  QStringList filters;
322  filters << "*.vs";
323 
324  // First add item of axlShader.qrc, then find shader from shader path
325  QDir dirShader( ":axlShader/shader/");
326  dirShader.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
327 
328  dirShader.setNameFilters(filters);
329 
330  QFileInfoList list = dirShader.entryInfoList();
331  // for (int i = 0; i < list.size(); ++i) {
332  // d->comboBoxShader->addItem(list.at(i).fileName());
333  // }
334 
335  QSettings settings("inria", "dtk");
336  QString defaultPath;
337  settings.beginGroup("shader");
338  QString defaultPathShader = settings.value("path", defaultPath).toString();
339  defaultPathShader.append("/");
340 
341  QDir defaultDirShader(defaultPathShader);
342  defaultDirShader.setNameFilters(filters);
343  QFileInfoList list2 = defaultDirShader.entryInfoList();
344 
345  list.append(list2);
346 
347  QStringList items;
348 
349  for (int i = 0; i < list.size(); ++i) {
350  if(!items.contains(list.at(i).fileName()))
351  items << list.at(i).fileName();
352  }
353 
354  qSort(items.begin(), items.end(), caseInsensitiveLessThan);
355  int indInitShader = -1;
356  int indCurrentShader = -1;
357 
358  foreach(QString item, items) {
359  indCurrentShader++;
360  d->comboBoxShader->addItem(item);
361 
362  QFileInfo currentFileInfo(d->lineEditShader->text());
363 
364  if(currentFileInfo.exists())
365  {
366  if(item == currentFileInfo.fileName())
367  indInitShader =indCurrentShader;
368  }
369  }
370 
371  //init the value from the lineEditShader.
372  if(indInitShader != -1)
373  d->comboBoxShader->setCurrentIndex(indInitShader);
374  }
375 }
376 
378 {
379  // First add item of axlShader.qrc, then find shader from shader path
380  QDir dirShader( ":axlShader/shader/");
381  dirShader.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
382 
383  QFileInfo currentFile(dirShader, shader);
384  if(!currentFile.exists())
385  {
386  QSettings settings("inria", "dtk");
387  QString defaultPath;
388  settings.beginGroup("shader");
389  QString defaultPathShader = settings.value("path", defaultPath).toString();
390  defaultPathShader.append("/");
391 
392  QDir defaultDirShader(defaultPathShader);
393  currentFile = QFileInfo(defaultDirShader, shader);
394 
395  }
396 
397  d->lineEditShader->setText(currentFile.absoluteFilePath());
398 }
399 
401 {
402  if (axlMesh *meshData = dynamic_cast<axlMesh *>(d->data))
403  meshData->setInterpolation(modeInterpolation);
404 
405  if (modeInterpolation == 0 && d->checkBoxUseNormals->checkState()){
406  d->checkBoxUseNormals->setChecked(false);
407  this->onUpdateGeometry();
408  } else if (modeInterpolation > 0 && (!d->checkBoxUseNormals->checkState())){
409  d->checkBoxUseNormals->setChecked(true);
410  this->onUpdateGeometry();
411  }
412 
413  emit interpolationChanded(d->data, modeInterpolation);
414  emit update();
415 }
416 
417 QString axlInspectorObjectMesh::initShaderValue(void)
418 {
419  return d->data->shader();
420 }
421 
422 
423 QColor axlInspectorObjectMesh::initColorValue(void)
424 {
425  return d->data->color();
426 }
427 
428 int axlInspectorObjectMesh::initOpacityValue(void)
429 {
430  double initOpacity = 0.0;
431  double opacity = d->data->opacity();
432  if(opacity > initOpacity)
433  initOpacity = opacity;
434 
435  return 100 * (1.0 - initOpacity);
436 }
437 
439 {
440  d->data->setColor(color);
441 
442  // emit dataChangedByColor(d->data, color.redF(), color.greenF(), color.blueF());
443 // emit modifiedProperty(d->data, 0);
444  d->data->touchProperty();
445 // emit update();
446 }
447 
449 {
450  if(d->lineEditShader->isEnabled())
451  {
452  QString fileToOpen;
453  fileToOpen = QFileDialog::getOpenFileName(this, tr("Open shader"), "", tr("vs file (*.vs)"));
454  d->lineEditShader->setText(fileToOpen);
455  }
456 }
457 
459 {
460  d->data->setShader(shader);
461 
462  // emit dataChangedByShader(d->data, d->lineEditShader->text());
463 // emit modifiedProperty(d->data, AXL_PROPERTY_SHADER);
464  d->data->touchProperty();
465 // emit update();
466 }
467 
469 {
470  if(isShader)
471  {
472  d->comboBoxShader->setEnabled(true);
473  d->lineEditShader->setEnabled(true);
474  d->buttonShader->setEnabled(true);
475  onLineEditShaderChanged(d->comboBoxShader->currentText());
476 
477  d->data->setShader(d->lineEditShader->text());
478  // emit dataChangedByShader(d->data, d->lineEditShader->text());
479  }
480  else
481  {
482  d->comboBoxShader->setEnabled(false);
483  d->lineEditShader->setEnabled(false);
484  d->buttonShader->setEnabled(false);
485 
486  d->data->setShader("");
487  // emit dataChangedByShader(d->data, "");
488  }
489 
490 // emit modifiedProperty(d->data, AXL_PROPERTY_SHADER);
491 // emit update();
492  d->data->touchProperty();
493 }
494 
496 {
497  double size_d = pow(2.0, size/100.0 - 3.0);
498  QVariant variant = d->data->QObject::property("size");
499  if(variant.isValid())
500  {
501  d->data->setSize(size_d);
502  if(d->data->mesh()){
503  d->data->mesh()->setSize(size_d);
504  }
505  }else {
506  if(d->data->mesh()){
507  d->data->mesh()->setSize(size_d);
508  }
509  }
510  emit modifiedProperty(d->data, AXL_PROPERTY_SIZE);
511 }
512 
514 {
515  double opacity_d = 1.0 - 0.01 * opacity; // range from 0.00 to 1.00
516  d->data->setOpacity(opacity_d);
517  d->data->touchProperty();
518  // emit dataChangedByOpacity(d->data, opacity_d);
519 // emit modifiedProperty(d->data, AXL_PROPERTY_OPACITY);
520 // emit update();
521 }
522 
524  axlMesh* mesh = NULL;
525 
526  if ((mesh = dynamic_cast<axlMesh*>(d->data))) {
527  mesh->vertex_show() = d->checkBoxPoints->isChecked();
528  mesh->normal_used() = d->checkBoxUseNormals->isChecked();
529  mesh->edge_show() = d->checkBoxLines->isChecked();
530  mesh->face_show() = d->checkBoxPoly->isChecked();
531  if (!mesh->normal_used())
532  d->comboInterpolation->setCurrentIndex(0);
533  else
534  if (d->comboInterpolation->currentIndex()==0)
535  d->comboInterpolation->setCurrentIndex(2);
536 
537  d->data->touchGeometry();
538  }else{
539  if(d->data->mesh()){
540  d->data->mesh()->vertex_show() = d->checkBoxPoints->isChecked();
541  d->data->mesh()->normal_used() = d->checkBoxUseNormals->isChecked();
542  d->data->mesh()->edge_show() = d->checkBoxLines->isChecked();
543  d->data->mesh()->face_show() = d->checkBoxPoly->isChecked();
544  if (!d->data->mesh()->normal_used())
545  d->comboInterpolation->setCurrentIndex(0);
546  else
547  if (d->comboInterpolation->currentIndex()==0)
548  d->comboInterpolation->setCurrentIndex(2);
549  d->data->mesh()->touchGeometry();
550  }
551  }
552 
553 // emit dataChangedByGeometry(d->data);
554 
555 // emit update();
556 }
bool vertex_show(void) const
Definition: axlMesh.cpp:159
axlInspectorObjectMesh(QWidget *parent=0)
bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
bool normal_used(void) const
Definition: axlMesh.cpp:170
void onInterpolationChanged(int modeInterpolation)
void setData(axlAbstractData *data)
bool edge_show(void) const
Definition: axlMesh.cpp:190
#define AXL_PROPERTY_SIZE
void onShaderStateChanged(bool isShader)
bool face_show(void) const
Definition: axlMesh.cpp:200
int normal_count(void) const
Definition: axlMesh.cpp:132
void modifiedProperty(dtkAbstractData *, int)
virtual axlMesh * toMesh(void)
Mesh conversion.
int vertex_count(void) const
Definition: axlMesh.cpp:122
Class axlAbstractData defines an API for all type of axel data.
void interpolationChanded(dtkAbstractData *data, int interpolation)
Class axlMesh defines a piecewise-linear 3D object.
Definition: axlMesh.h:41