textureProperties.cc 13.6 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  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.
Jan Möbius's avatar
Jan Möbius committed
15
//
Jan Möbius's avatar
 
Jan Möbius committed
16 17 18 19
//  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.
Jan Möbius's avatar
Jan Möbius committed
20
//
Jan Möbius's avatar
 
Jan Möbius committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
//  You should have received a copy of the GNU Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision$
//   $Author$
//   $Date$
//
//=============================================================================




#include "textureProperties.hh"
36 37 38

#include <float.h>

Jan Möbius's avatar
 
Jan Möbius committed
39
#include <QtGui>
40
#include <OpenFlipper/common/GlobalOptions.hh>
Jan Möbius's avatar
Jan Möbius committed
41 42 43 44 45 46
#include <OpenFlipper/common/Types.hh>
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <ObjectTypes/PolyMesh/PolyMesh.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>


Jan Möbius's avatar
 
Jan Möbius committed
47 48 49 50 51

texturePropertiesWidget::texturePropertiesWidget(QWidget *parent)
    : QDialog(parent)
{
    setupUi(this);
Jan Möbius's avatar
Jan Möbius committed
52

53
    connect(buttonBox, SIGNAL( clicked(QAbstractButton*) ), this , SLOT ( slotButtonBoxClicked(QAbstractButton*) ) );
Jan Möbius's avatar
Jan Möbius committed
54 55
    connect(textureList, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(textureChanged(QTreeWidgetItem*,int)) );
    connect(textureList, SIGNAL(itemPressed(QTreeWidgetItem*,int)), this, SLOT(textureAboutToChange(QTreeWidgetItem*,int)) );
56 57 58 59 60 61 62 63 64 65 66 67

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

68 69
    connect(changeImageButton, SIGNAL( clicked() ), this, SLOT( slotChangeImage() ) );

70 71 72

    texData_ = 0;

73
    #ifdef WITH_QWT
Dirk Wilden's avatar
Dirk Wilden committed
74
      QGridLayout* layout = new QGridLayout( originalData);
75

76
      functionPlot_ = new ACG::QwtFunctionPlot(0);
77

78
      layout->addWidget( functionPlot_ , 0,0  );
79

Dirk Wilden's avatar
Dirk Wilden committed
80
    #endif
81 82 83

}

84 85 86 87 88 89 90 91
void texturePropertiesWidget::show(TextureData* _texData, int _id, QString _name){

  texData_ = _texData;
  id_ = _id;

  textureList->clear();

  for (uint i=0; i < texData_->textures().size(); i++)
Jan Möbius's avatar
Jan Möbius committed
92
    if ( ! texData_->textures()[i].hidden() ) {
Jan Möbius's avatar
Jan Möbius committed
93 94 95 96 97 98 99 100 101
      if (  texData_->textures()[i].type() != MULTITEXTURE ) {
        textureList->addTopLevelItem( new QTreeWidgetItem((QTreeWidget*)0, QStringList( texData_->textures()[i].name() ) ) );
      } else {
        QTreeWidgetItem* parent = new QTreeWidgetItem((QTreeWidget*)0, QStringList( texData_->textures()[i].name() ) );
        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] )) );
      }
    }
102

Jan Möbius's avatar
Jan Möbius committed
103 104 105 106 107 108
    if ( textureList->invisibleRootItem()->childCount() == 0 )  {
      QMessageBox msgBox(this);
      msgBox.setText("Cannot show Properties. No Textures available!");
      msgBox.exec();
      return;
    }
109 110 111 112 113 114

  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
115 116
  propChanged_ = false;

Dirk Wilden's avatar
Dirk Wilden committed
117 118
  textureList->setCurrentItem( textureList->topLevelItem(0) );
  textureChanged( textureList->topLevelItem(0), 0 );
119 120 121 122

  QDialog::show();
}

Jan Möbius's avatar
Jan Möbius committed
123
void texturePropertiesWidget::textureAboutToChange(QTreeWidgetItem* _item, int _column){
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

  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
139
      textureList->setCurrentItem( _item );
Jan Möbius's avatar
Jan Möbius committed
140
      textureChanged( _item,_column );
141 142 143 144

    } else {
      propChanged_ = false;

Dirk Wilden's avatar
Dirk Wilden committed
145
      textureList->setCurrentItem( _item );
Jan Möbius's avatar
Jan Möbius committed
146
      textureChanged( _item,_column );
147 148 149 150
    }
  }
}

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

Jan Möbius's avatar
Jan Möbius committed
153 154 155
  // ================================================================================
  // opened changes for the last texture so switch back
  // ================================================================================
156
  if ( propChanged_ ){
Dirk Wilden's avatar
Dirk Wilden committed
157
    textureList->setCurrentItem( curItem_ );
158 159 160
    return;
  }

Jan Möbius's avatar
Jan Möbius committed
161 162 163
  // ================================================================================
  // Unable to find the right texture
  // ================================================================================
Jan Möbius's avatar
Jan Möbius committed
164
  if ( !texData_->textureExists( _item->text(_column) ) )
Jan Möbius's avatar
Jan Möbius committed
165
    return;
166

Jan Möbius's avatar
Jan Möbius committed
167 168 169
  // ================================================================================
  // Set name of the texture
  // ================================================================================
Jan Möbius's avatar
Jan Möbius committed
170
  textureName_ = _item->text(_column);
171

Jan Möbius's avatar
Jan Möbius committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  // ================================================================================
  // 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
  // ================================================================================
208 209 210 211 212 213 214 215 216 217 218
  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
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  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
239 240 241 242 243 244
    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
245 246 247 248 249 250 251 252
    case UNSET:
      typeLabel->setText("Type:   Unset");
      indexLabel->setEnabled(false);
      indexBox->setEnabled(false);
      indexBox->clear();
      break;
  }

253 254 255 256
  // Show the texture Image
  imageLabel->setPixmap(QPixmap::fromImage(texture.textureImage));
  imageLabel->setScaledContents(true);

257
  if ( texture.filename().startsWith("/") )
258
    fileLabel->setText( "File: " + texture.filename() );
259
  else
260
    fileLabel->setText( "File: " + OpenFlipper::Options::textureDirStr() + QDir::separator() + texture.filename() );
261

262 263
  currentImage_ = texture.filename();

264
  // update plot only when dimension is 1
265
  if ( texture.dimension() == 1 && id_ != -1) {
266

267
    #ifdef WITH_QWT
268

Dirk Wilden's avatar
Dirk Wilden committed
269
      std::vector< double > coords;
270

Dirk Wilden's avatar
Dirk Wilden committed
271
      emit getCoordinates1D(textureName_, id_, coords);
272

Dirk Wilden's avatar
Dirk Wilden committed
273
      if ( coords.size() > 0 ){
274

Dirk Wilden's avatar
Dirk Wilden committed
275
        functionPlot_->setFunction( coords );
276

Dirk Wilden's avatar
Dirk Wilden committed
277 278 279 280 281
        functionPlot_->setParameters(repeatBox->isChecked(), max_val->value(),
                                     clampBox->isChecked(),  clamp_min->value(), clamp_max->value(),
                                     centerBox->isChecked(),
                                     absBox->isChecked(),
                                     scaleBox->isChecked());
282

Dirk Wilden's avatar
Dirk Wilden committed
283
        image_ = texture.textureImage;
284

Dirk Wilden's avatar
Dirk Wilden committed
285 286 287
        functionPlot_->setImage( &image_ );

        functionPlot_->replot();
288
      }
289

290 291 292 293
    #endif
  }

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

296
}
297 298 299

void texturePropertiesWidget::slotChangeImage() {

300
  QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),
301
                                                  OpenFlipper::Options::currentTextureDirStr(),
302 303
                                                  tr("Images (*.png *.xpm *.jpg)"));

304
  if ( QFile(fileName).exists() ){
305 306
    QFileInfo fileInfo(fileName);
    OpenFlipper::Options::currentTextureDir( fileInfo.absolutePath() );
307

308 309
    imageLabel->setPixmap( QPixmap(fileName) );
    imageLabel->setScaledContents(true);
310

311 312 313 314
    fileLabel->setText( "File: " + fileName );

    currentImage_ = fileName;

Dirk Wilden's avatar
Dirk Wilden committed
315 316
    image_ = imageLabel->pixmap()->toImage();

Jan Möbius's avatar
Jan Möbius committed
317
    #ifdef WITH_QWT
Dirk Wilden's avatar
Dirk Wilden committed
318 319
    functionPlot_->setImage( &image_ );
    functionPlot_->replot();
320
    #endif
Dirk Wilden's avatar
Dirk Wilden committed
321

322 323
    propChanged_ = true;
  }
324 325 326

}

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
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;
    }

374 375 376 377 378 379
    if ( texture.filename() != currentImage_ ){
      texture.filename( currentImage_ );
      texture.textureImage = QImage( currentImage_ );
      changed = true;
    }

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    //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
395

Jan Möbius's avatar
Jan Möbius committed
396
  #ifdef WITH_QWT
Dirk Wilden's avatar
Dirk Wilden committed
397 398 399 400 401 402 403
  functionPlot_->setParameters(repeatBox->isChecked(), max_val->value(),
                              clampBox->isChecked(),  clamp_min->value(), clamp_max->value(),
                              centerBox->isChecked(),
                              absBox->isChecked(),
                              scaleBox->isChecked());

  functionPlot_->replot();
404
  #endif
405
}