textureProperties.cc 16.6 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
40
41
\*===========================================================================*/

Jan Möbius's avatar
Jan Möbius committed
42

Jan Möbius's avatar
 
Jan Möbius committed
43
44
45
46
47




#include "textureProperties.hh"
48

49
#include <OpenFlipper/common/GlobalOptions.hh>
Jan Möbius's avatar
Jan Möbius committed
50
51
#include <OpenFlipper/common/Types.hh>
#include <ObjectTypes/PolyMesh/PolyMesh.hh>
52
#include "ImageStorage.hh"
Jan Möbius's avatar
Jan Möbius committed
53

Jan Möbius's avatar
 
Jan Möbius committed
54
55
56
57
texturePropertiesWidget::texturePropertiesWidget(QWidget *parent)
    : QDialog(parent)
{
    setupUi(this);
Jan Möbius's avatar
Jan Möbius committed
58

59
    connect(buttonBox, SIGNAL( clicked(QAbstractButton*) ), this , SLOT ( slotButtonBoxClicked(QAbstractButton*) ) );
Jan Möbius's avatar
Jan Möbius committed
60
61
    connect(textureList, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(textureChanged(QTreeWidgetItem*,int)) );
    connect(textureList, SIGNAL(itemPressed(QTreeWidgetItem*,int)), this, SLOT(textureAboutToChange(QTreeWidgetItem*,int)) );
62
63
64
65
66
67
68
69
70
71
72
73

    //remember changes
    connect(repeatBox, SIGNAL( clicked() ), this , SLOT ( slotPropertiesChanged() ) );
    connect(clampBox, SIGNAL( clicked() ), this , SLOT ( slotPropertiesChanged() ) );
    connect(centerBox, SIGNAL( clicked() ), this , SLOT ( slotPropertiesChanged() ) );
    connect(absBox, SIGNAL( clicked() ), this , SLOT ( slotPropertiesChanged() ) );
    connect(scaleBox, SIGNAL( clicked() ), this , SLOT ( slotPropertiesChanged() ) );

    connect(max_val, SIGNAL( valueChanged(double) ), this , SLOT ( slotPropertiesChanged(double) ) );
    connect(clamp_min, SIGNAL( valueChanged(double) ), this , SLOT ( slotPropertiesChanged(double) ) );
    connect(clamp_max, SIGNAL( valueChanged(double) ), this , SLOT ( slotPropertiesChanged(double) ) );

74
75
    connect(changeImageButton, SIGNAL( clicked() ), this, SLOT( slotChangeImage() ) );

76
77
78

    texData_ = 0;

79
    #ifdef WITH_QWT
Dirk Wilden's avatar
Dirk Wilden committed
80
      QGridLayout* layout = new QGridLayout( originalData);
81

82
      functionPlot_ = new ACG::QwtFunctionPlot(0);
83

84
      layout->addWidget( functionPlot_ , 0,0  );
85

Dirk Wilden's avatar
Dirk Wilden committed
86
    #endif
87
88
89

}

90
91
92
93
94
95
96
void texturePropertiesWidget::show(TextureData* _texData, int _id, QString _name){

  texData_ = _texData;
  id_ = _id;

  textureList->clear();

97
98
  QTreeWidgetItem* activeItem = 0;
  
Jan Möbius's avatar
Jan Möbius committed
99
  for (unsigned int i=0; i < texData_->textures().size(); i++) {
Jan Möbius's avatar
Bugfix    
Jan Möbius committed
100
    if ( ! texData_->textures()[i].hidden() ) {
Jan Möbius's avatar
Jan Möbius committed
101
      if (  texData_->textures()[i].type() != MULTITEXTURE ) {
102
103
104
105
106
107
108
109
110
111
112
113
114
        
        QTreeWidgetItem* item = 0;
        
        if ( !texData_->textures()[i].visibleName().isEmpty() )
          item = new QTreeWidgetItem((QTreeWidget*)0, QStringList( texData_->textures()[i].visibleName() ) );
        else
          item = new QTreeWidgetItem((QTreeWidget*)0, QStringList( texData_->textures()[i].name() ) );
        
        textureList->addTopLevelItem( item );
        
        if (texData_->textures()[i].enabled())
          activeItem = item;
         
Jan Möbius's avatar
Jan Möbius committed
115
      } else {
116
117
118
119
120
121
        QTreeWidgetItem* parent = 0;
        if ( !texData_->textures()[i].visibleName().isEmpty() )
          parent = new QTreeWidgetItem((QTreeWidget*)0, QStringList( texData_->textures()[i].visibleName() ) );
        else
          parent = new QTreeWidgetItem((QTreeWidget*)0, QStringList( texData_->textures()[i].name() ) );

Jan Möbius's avatar
Jan Möbius committed
122
123
124
        textureList->addTopLevelItem( parent ) ;
        for ( int j = 0 ; j < texData_->textures()[i].multiTextureList.size() ; ++j )
          textureList->addTopLevelItem( new QTreeWidgetItem(parent, QStringList(texData_->textures()[i].multiTextureList[j] )) );
125
126
127
        
        if (texData_->textures()[i].enabled())
          activeItem = parent;
Jan Möbius's avatar
Jan Möbius committed
128
129
      }
    }
Jan Möbius's avatar
Jan Möbius committed
130
131
132
133
134
135
136
137
  }

  if ( textureList->invisibleRootItem()->childCount() == 0 )  {
    QMessageBox msgBox(this);
    msgBox.setText("Cannot show Properties. No Textures available!");
    msgBox.exec();
    return;
  }
138
139
140
141
142
143

  if (id_ == -1)
    textureLabel->setText("<B>Global Textures</B>");
  else
    textureLabel->setText("<B>Textures for object '" + _name + "'</B>");

Dirk Wilden's avatar
Dirk Wilden committed
144
145
  propChanged_ = false;

146
147
148
149
150
151
152
153
154
155
  if (activeItem == 0){
    
    textureList->setCurrentItem( textureList->topLevelItem(0) );
    textureChanged( textureList->topLevelItem(0), 0 );
    
  } else {
    textureList->setCurrentItem( activeItem );
    textureChanged( activeItem, 0 );
  }
  
156
157
158
  QDialog::show();
}

Jan Möbius's avatar
Jan Möbius committed
159
void texturePropertiesWidget::textureAboutToChange(QTreeWidgetItem* _item, int _column){
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

  if (propChanged_){
    QMessageBox msgBox(this);
    msgBox.setText("The properties of the current texture have been changed.");
    msgBox.setInformativeText("Do you want to apply these changes?");
    msgBox.setStandardButtons(QMessageBox::Apply | QMessageBox::Discard );
    msgBox.setDefaultButton(QMessageBox::Apply);
    int ret = msgBox.exec();

    if (ret == QMessageBox::Apply){
      //just hit the applyButton ;)
      for (int i=0; i < buttonBox->buttons().count(); i++)
        if ( buttonBox->standardButton( buttonBox->buttons()[i] ) == QDialogButtonBox::Apply )
          slotButtonBoxClicked( buttonBox->buttons()[i] );

Dirk Wilden's avatar
Dirk Wilden committed
175
      textureList->setCurrentItem( _item );
Jan Möbius's avatar
Jan Möbius committed
176
      textureChanged( _item,_column );
177
178
179
180

    } else {
      propChanged_ = false;

Dirk Wilden's avatar
Dirk Wilden committed
181
      textureList->setCurrentItem( _item );
Jan Möbius's avatar
Jan Möbius committed
182
      textureChanged( _item,_column );
183
184
185
186
    }
  }
}

Jan Möbius's avatar
Jan Möbius committed
187
void texturePropertiesWidget::textureChanged(QTreeWidgetItem* _item, int _column){
188

Jan Möbius's avatar
Jan Möbius committed
189
190
191
  // ================================================================================
  // opened changes for the last texture so switch back
  // ================================================================================
192
  if ( propChanged_ ){
Dirk Wilden's avatar
Dirk Wilden committed
193
    textureList->setCurrentItem( curItem_ );
194
195
196
    return;
  }

Jan Möbius's avatar
Jan Möbius committed
197
198
199
  // ================================================================================
  // Unable to find the right texture
  // ================================================================================
Jan Möbius's avatar
Jan Möbius committed
200
  if ( !texData_->textureExists( _item->text(_column) ) )
Jan Möbius's avatar
Jan Möbius committed
201
    return;
202

Jan Möbius's avatar
Jan Möbius committed
203
204
205
  // ================================================================================
  // Set name of the texture
  // ================================================================================
Jan Möbius's avatar
Jan Möbius committed
206
  textureName_ = _item->text(_column);
207

Jan Möbius's avatar
Jan Möbius committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
  // ================================================================================
  // Get Object to parse Properties
  // ================================================================================
//   BaseObjectData* obj;
//   if ( PluginFunctions::getObject(  id_ , obj ) ) {
//     if( obj->dataType( DATA_TRIANGLE_MESH ) ){
//       TriMesh* mesh = PluginFunctions::triMeshObject(obj)->mesh();
//       std::string fprops;
//       mesh->fprop_stats(fprops);
//       QString facePropertyString(fprops.c_str());
//       QStringList faceProperties = facePropertyString.split(QRegExp("\n"));
//
//       std::cerr << "Got : \n" ;
//       for ( int i = 0 ; i < faceProperties.size(); ++i ) {
//         faceProperties[i] = faceProperties[i].trimmed();
//         if ( ( ! faceProperties[i].size() == 0 ) && faceProperties[i] != "<fprop>" )
//           std::cerr << faceProperties[i].toStdString() << std::endl;
//         else
//           continue;
//
//         OpenMesh::FPropHandleT< int > indexPropertyTest;
//         if ( mesh->get_property_handle(indexPropertyTest,faceProperties[i].toStdString()) ) {
//           std::cerr << "Got handle : " << faceProperties[i].toStdString() << std::endl;
//         } else {
//           std::cerr << "Unable to get Handle : " << faceProperties[i].toStdString() << std::endl;
//         }
//       }
//
//     } else if( obj->dataType( DATA_POLY_MESH ) ){
//       PolyMesh* mesh = PluginFunctions::polyMeshObject(obj)->mesh();
//     }
//   }

  // ================================================================================
  // Update the dialog
  // ================================================================================
244
245
246
247
248
249
250
  Texture& texture = texData_->texture(textureName_);

  repeatBox->setChecked(texture.parameters.repeat);
  clampBox->setChecked(texture.parameters.clamp);
  centerBox->setChecked(texture.parameters.center);
  absBox->setChecked(texture.parameters.abs);

251
252
253
  max_val->setValue( texture.parameters.repeatMax );
  clamp_min->setValue( texture.parameters.clampMin );
  clamp_max->setValue( texture.parameters.clampMax );
254

Jan Möbius's avatar
Jan Möbius committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  switch (texture.type()) {
    case MULTITEXTURE:
      typeLabel->setText("Type:   MultiTexture");
      indexLabel->setEnabled(true);
      indexBox->setEnabled(true);
      indexBox->clear();
      indexBox->addItem("TODO");
      break;
    case HALFEDGEBASED:
      typeLabel->setText("Type:   HalfedgeBased");
      indexLabel->setEnabled(false);
      indexBox->setEnabled(false);
      indexBox->clear();
      break;
    case VERTEXBASED:
      typeLabel->setText("Type:   VertexBased");
      indexLabel->setEnabled(false);
      indexBox->setEnabled(false);
      indexBox->clear();
      break;
Jan Möbius's avatar
Jan Möbius committed
275
276
277
278
279
280
    case ENVIRONMENT:
      typeLabel->setText("Type:   Environment Map");
      indexLabel->setEnabled(false);
      indexBox->setEnabled(false);
      indexBox->clear();
      break;
Jan Möbius's avatar
Jan Möbius committed
281
282
283
284
285
286
287
288
    case UNSET:
      typeLabel->setText("Type:   Unset");
      indexLabel->setEnabled(false);
      indexBox->setEnabled(false);
      indexBox->clear();
      break;
  }

289
  // Show the texture Image
290
291
292
293
294
295
296
  bool ok = false;
  imageLabel->setPixmap(QPixmap::fromImage( imageStore().getImage(texture.textureImageId(),&ok) ));

  if ( !ok ) {
    std::cerr<< imageStore().error().toStdString();
  }

297
298
  imageLabel->setScaledContents(true);

299
  if ( texture.filename().startsWith("/") )
300
    fileLabel->setText( "File: " + texture.filename() );
301
  else
302
    fileLabel->setText( "File: " + OpenFlipper::Options::textureDirStr() + QDir::separator() + texture.filename() );
303

304
305
  currentImage_ = texture.filename();

306
  // update plot only when dimension is 1
307
  if ( texture.dimension() == 1 && id_ != -1) {
308

309
    #ifdef WITH_QWT
310

Dirk Wilden's avatar
Dirk Wilden committed
311
      std::vector< double > coords;
312

Dirk Wilden's avatar
Dirk Wilden committed
313
      emit getCoordinates1D(textureName_, id_, coords);
314

315
      if ( ! coords.empty() ){
316

Dirk Wilden's avatar
Dirk Wilden committed
317
        functionPlot_->setFunction( coords );
318

Dirk Wilden's avatar
Dirk Wilden committed
319
320
321
322
323
        functionPlot_->setParameters(repeatBox->isChecked(), max_val->value(),
                                     clampBox->isChecked(),  clamp_min->value(), clamp_max->value(),
                                     centerBox->isChecked(),
                                     absBox->isChecked(),
                                     scaleBox->isChecked());
324

325
326
327
328
329
330
        bool ok = false;
        image_ = imageStore().getImage(texture.textureImageId(),&ok);
        if ( !ok ) {
          std::cerr << imageStore().error().toStdString();
        }

331

Dirk Wilden's avatar
Dirk Wilden committed
332
333
334
        functionPlot_->setImage( &image_ );

        functionPlot_->replot();
335
      }
336

337
338
339
340
    #endif
  }

  propChanged_ = false;
Dirk Wilden's avatar
Dirk Wilden committed
341
  curItem_ = textureList->currentItem();
Jan Möbius's avatar
Jan Möbius committed
342

343
}
344

345
346
void texturePropertiesWidget::slotChangeImage()
{
347

348
349
  QString fileName = QFileDialog::getOpenFileName(this,
                                                  tr("Open Image"),
350
                                                  OpenFlipper::Options::currentTextureDirStr(),
351
                                                  tr("Images (*.png *.xpm *.jpg *.tga *.tif *.tiff *.bmp);;All Files (*.*)"));
352

353
  if (QFile(fileName).exists()) {
354
    QFileInfo fileInfo(fileName);
355
    OpenFlipper::Options::currentTextureDir(fileInfo.absolutePath());
356

357
    imageLabel->setPixmap(fileName);
358
    imageLabel->setScaledContents(true);
359

360
    fileLabel->setText("File: " + fileName);
361
362

    currentImage_ = fileName;
Dirk Wilden's avatar
Dirk Wilden committed
363
    image_ = imageLabel->pixmap()->toImage();
364

Jan Möbius's avatar
Jan Möbius committed
365
    #ifdef WITH_QWT
366
367
      functionPlot_->setImage(&image_);
      functionPlot_->replot();
368
    #endif
Dirk Wilden's avatar
Dirk Wilden committed
369

370
371
    propChanged_ = true;
  }
372
373
374

}

375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
void texturePropertiesWidget::slotButtonBoxClicked(QAbstractButton* _button){

  QDialogButtonBox::StandardButton btn = buttonBox->standardButton(_button);

  if ( btn == QDialogButtonBox::Apply || btn == QDialogButtonBox::Ok){

    //applySettings
    bool changed = false;

    Texture& texture = texData_->texture(textureName_);

    if ( texture.parameters.repeat != repeatBox->isChecked() ){
      texture.parameters.repeat=repeatBox->isChecked();
      changed = true;
    }
    if ( texture.parameters.clamp != clampBox->isChecked() ){
      texture.parameters.clamp=clampBox->isChecked();
      changed = true;
    }
    if ( texture.parameters.center != centerBox->isChecked() ){
      texture.parameters.center=centerBox->isChecked();
      changed = true;
    }
    if ( texture.parameters.abs != absBox->isChecked() ){
      texture.parameters.abs=absBox->isChecked();
      changed = true;
    }
    if ( texture.parameters.scale != scaleBox->isChecked() ){
      texture.parameters.scale=scaleBox->isChecked();
      changed = true;
    }

407
408
    if ( texture.parameters.repeatMax != max_val->value() ){
      texture.parameters.repeatMax = max_val->value();
409
410
411
      changed = true;
    }

412
413
    if ( texture.parameters.clampMin != clamp_min->value() ){
      texture.parameters.clampMin = clamp_min->value();
414
415
416
      changed = true;
    }

417
418
    if ( texture.parameters.clampMax != clamp_max->value() ){
      texture.parameters.clampMax = clamp_max->value();
419
420
421
      changed = true;
    }

422
    if ( texture.filename() != currentImage_ ){
423
      // Set the new filename of the image
424
      texture.filename( currentImage_ );
425
426
427
428

      // Add it to the imagestore and set the index in the texture description
      texture.textureImageId( imageStore().addImageFile(currentImage_) );

429
430
431
      changed = true;
    }

432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
    //inform the plugin about the update
    if (changed)
      emit applyProperties(texData_, textureName_, id_ );

    propChanged_ = false;
  }

  if ( btn == QDialogButtonBox::Apply )
    return;
  else
    hide();
}

void texturePropertiesWidget::slotPropertiesChanged(double /*_value*/){
  propChanged_ = true;
Dirk Wilden's avatar
Dirk Wilden committed
447

Jan Möbius's avatar
Jan Möbius committed
448
  #ifdef WITH_QWT
Dirk Wilden's avatar
Dirk Wilden committed
449
450
451
452
453
454
455
  functionPlot_->setParameters(repeatBox->isChecked(), max_val->value(),
                              clampBox->isChecked(),  clamp_min->value(), clamp_max->value(),
                              centerBox->isChecked(),
                              absBox->isChecked(),
                              scaleBox->isChecked());

  functionPlot_->replot();
456
  #endif
457
}