textureProperties.cc 16.6 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
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
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
33 34 35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36 37 38 39 40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42 43 44 45 46




#include "textureProperties.hh"
47 48 49

#include <float.h>

Jan Möbius's avatar
 
Jan Möbius committed
50
#include <QtGui>
51
#include <OpenFlipper/common/GlobalOptions.hh>
Jan Möbius's avatar
Jan Möbius committed
52 53 54 55
#include <OpenFlipper/common/Types.hh>
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <ObjectTypes/PolyMesh/PolyMesh.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>
56
#include "ImageStorage.hh"
Jan Möbius's avatar
Jan Möbius committed
57

Jan Möbius's avatar
 
Jan Möbius committed
58 59 60 61
texturePropertiesWidget::texturePropertiesWidget(QWidget *parent)
    : QDialog(parent)
{
    setupUi(this);
Jan Möbius's avatar
Jan Möbius committed
62

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

    //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) ) );

78 79
    connect(changeImageButton, SIGNAL( clicked() ), this, SLOT( slotChangeImage() ) );

80 81 82

    texData_ = 0;

83
    #ifdef WITH_QWT
Dirk Wilden's avatar
Dirk Wilden committed
84
      QGridLayout* layout = new QGridLayout( originalData);
85

86
      functionPlot_ = new ACG::QwtFunctionPlot(0);
87

88
      layout->addWidget( functionPlot_ , 0,0  );
89

Dirk Wilden's avatar
Dirk Wilden committed
90
    #endif
91 92 93

}

94 95 96 97 98 99 100
void texturePropertiesWidget::show(TextureData* _texData, int _id, QString _name){

  texData_ = _texData;
  id_ = _id;

  textureList->clear();

101 102
  QTreeWidgetItem* activeItem = 0;
  
103
  for (uint i=0; i < texData_->textures().size(); i++)
Jan Möbius's avatar
Bugfix  
Jan Möbius committed
104
    if ( ! texData_->textures()[i].hidden() ) {
Jan Möbius's avatar
Jan Möbius committed
105
      if (  texData_->textures()[i].type() != MULTITEXTURE ) {
106 107 108 109 110 111 112 113 114 115 116 117 118
        
        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
119
      } else {
120 121 122 123 124 125
        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
126 127 128
        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] )) );
129 130 131
        
        if (texData_->textures()[i].enabled())
          activeItem = parent;
Jan Möbius's avatar
Jan Möbius committed
132 133
      }
    }
134

Jan Möbius's avatar
Jan Möbius committed
135 136 137 138 139 140
    if ( textureList->invisibleRootItem()->childCount() == 0 )  {
      QMessageBox msgBox(this);
      msgBox.setText("Cannot show Properties. No Textures available!");
      msgBox.exec();
      return;
    }
141 142 143 144 145 146

  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
147 148
  propChanged_ = false;

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

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

  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
178
      textureList->setCurrentItem( _item );
Jan Möbius's avatar
Jan Möbius committed
179
      textureChanged( _item,_column );
180 181 182 183

    } else {
      propChanged_ = false;

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

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

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

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

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

Jan Möbius's avatar
Jan Möbius committed
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 244 245 246
  // ================================================================================
  // 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
  // ================================================================================
247 248 249 250 251 252 253 254 255 256 257
  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);

  max_val->setValue( texture.parameters.max_val );
  clamp_min->setValue( texture.parameters.clamp_min );
  clamp_max->setValue( texture.parameters.clamp_max );

Jan Möbius's avatar
Jan Möbius committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  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
278 279 280 281 282 283
    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
284 285 286 287 288 289 290 291
    case UNSET:
      typeLabel->setText("Type:   Unset");
      indexLabel->setEnabled(false);
      indexBox->setEnabled(false);
      indexBox->clear();
      break;
  }

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

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

300 301
  imageLabel->setScaledContents(true);

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

307 308
  currentImage_ = texture.filename();

309
  // update plot only when dimension is 1
310
  if ( texture.dimension() == 1 && id_ != -1) {
311

312
    #ifdef WITH_QWT
313

Dirk Wilden's avatar
Dirk Wilden committed
314
      std::vector< double > coords;
315

Dirk Wilden's avatar
Dirk Wilden committed
316
      emit getCoordinates1D(textureName_, id_, coords);
317

318
      if ( ! coords.empty() ){
319

Dirk Wilden's avatar
Dirk Wilden committed
320
        functionPlot_->setFunction( coords );
321

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

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

334

Dirk Wilden's avatar
Dirk Wilden committed
335 336 337
        functionPlot_->setImage( &image_ );

        functionPlot_->replot();
338
      }
339

340 341 342 343
    #endif
  }

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

346
}
347 348 349

void texturePropertiesWidget::slotChangeImage() {

350
  QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),
351
                                                  OpenFlipper::Options::currentTextureDirStr(),
352
                                                  tr("Images (*.png *.xpm *.jpg, *.tga)"));
353

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

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

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

    currentImage_ = fileName;
Dirk Wilden's avatar
Dirk Wilden committed
364
    image_ = imageLabel->pixmap()->toImage();
365
    
Jan Möbius's avatar
Jan Möbius committed
366
    #ifdef WITH_QWT
Dirk Wilden's avatar
Dirk Wilden committed
367 368
    functionPlot_->setImage( &image_ );
    functionPlot_->replot();
369
    #endif
Dirk Wilden's avatar
Dirk Wilden committed
370

371 372
    propChanged_ = true;
  }
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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
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;
    }

    if ( texture.parameters.max_val != max_val->value() ){
      texture.parameters.max_val = max_val->value();
      changed = true;
    }

    if ( texture.parameters.clamp_min != clamp_min->value() ){
      texture.parameters.clamp_min = clamp_min->value();
      changed = true;
    }

    if ( texture.parameters.clamp_max != clamp_max->value() ){
      texture.parameters.clamp_max = clamp_max->value();
      changed = true;
    }

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

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

430 431 432
      changed = true;
    }

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    //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
448

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

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