MaterialPicker.cc 22.6 KB
Newer Older
Isaak Lim's avatar
Isaak Lim committed
1 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.              *
Isaak Lim's avatar
Isaak Lim committed
39 40 41 42 43
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
Jan Möbius's avatar
Jan Möbius committed
44 45 46
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
Isaak Lim's avatar
Isaak Lim committed
47 48 49 50 51 52 53
*                                                                            *
\*===========================================================================*/


#include "MaterialPicker.hh"

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
54
#include <OpenFlipper/common/GlobalOptions.hh>
Matthias Möller's avatar
Matthias Möller committed
55
#include <ACG/QtWidgets/QtMaterialDialog.hh>
56
#include <sstream>
Isaak Lim's avatar
Isaak Lim committed
57

58 59
#include <ACG/Scenegraph/MaterialNode.hh>

60 61 62 63
#include <QSignalMapper>
#include <QMessageBox>
#include <QMenu>

Isaak Lim's avatar
Isaak Lim committed
64 65 66 67
//------------------------------------------------------------------------------
MaterialPicker::MaterialPicker()
  :
  pickModeName_("MaterialPicker"),
68
  propName_(name()+QString("/Materials")),
Isaak Lim's avatar
Isaak Lim committed
69 70 71
  pickMaterialButton_(0),
  fillMaterialButton_(0),
  materialListWidget_(0),
Matthias Möller's avatar
Matthias Möller committed
72 73 74
  materialStrings_(),
  shortKeyRow_(),
  materialNode_(),
Isaak Lim's avatar
Isaak Lim committed
75 76
  pickMaterial_(false),
  fillMaterial_(false)
Matthias Möller's avatar
Matthias Möller committed
77

Isaak Lim's avatar
Isaak Lim committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
{
}

//------------------------------------------------------------------------------

MaterialPicker::~MaterialPicker() {

}

//------------------------------------------------------------------------------

void MaterialPicker::initializePlugin() {
   QWidget* toolBox = new QWidget();

   pickMaterialButton_ = new QPushButton("&pick Material", toolBox);
   fillMaterialButton_ = new QPushButton("&fill Material", toolBox);
94 95
   QPushButton* clearListButton =  new QPushButton("Clear List", toolBox);
   QPushButton* removeItemButton = new QPushButton("Remove", toolBox);
Isaak Lim's avatar
Isaak Lim committed
96 97 98 99 100 101 102 103

   pickMaterialButton_->setCheckable(true);
   fillMaterialButton_->setCheckable(true);

   QLabel* materials = new QLabel("Materials:");

   materialListWidget_ = new QListWidget(toolBox);

104
   //load saved materials
Matthias Möller's avatar
Matthias Möller committed
105 106
   materialStrings_ = OpenFlipperSettings().value(propName_, QStringList()).toStringList();
   for (int i = 0; i < materialStrings_.size(); ++i)
107
   {
Matthias Möller's avatar
Matthias Möller committed
108
     QStringList savedString = materialStrings_[i].split(";");
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
     std::stringstream stream;
     MaterialInfo materialInfo;
     stream << savedString[1].toStdString();
     stream >> materialInfo.color_material;
     stream.str("");
     stream.clear();
     stream << savedString[2].toStdString();
     stream >> materialInfo.base_color;
     stream.str("");
     stream.clear();
     stream << savedString[3].toStdString();
     stream >> materialInfo.ambient_color;
     stream.str("");
     stream.clear();
     stream << savedString[4].toStdString();
     stream >> materialInfo.diffuse_color;
     stream.str("");
     stream.clear();
     stream << savedString[5].toStdString();
     stream >> materialInfo.specular_color;
     stream.str("");
     stream.clear();
     stream << savedString[6].toStdString();
     stream >> materialInfo.shininess;
     stream.str("");
     stream.clear();
     stream << savedString[7].toStdString();
     stream >> materialInfo.reflectance;
137 138 139
     stream.str("");
     stream.clear();
     stream << savedString[8].toStdString();
Matthias Möller's avatar
Matthias Möller committed
140
     stream >> materialInfo.key;
141

Matthias Möller's avatar
Matthias Möller committed
142 143
     if (materialInfo.key != Qt::Key_unknown)
       shortKeyRow_[materialInfo.key] = materialListWidget_->count();
144

Matthias Möller's avatar
Matthias Möller committed
145
     materialListWidget_->addItem( itemName(savedString[0],materialInfo.key) );
146 147
     materialList_.push_back(materialInfo);
   }
148 149

   //if material was saved, set first as current
Matthias Möller's avatar
Matthias Möller committed
150
   if (materialStrings_.size())
151
     materialListWidget_->setCurrentItem(materialListWidget_->item(0));
152 153 154 155 156 157 158 159 160 161
   else
     fillMaterialButton_->setEnabled(false);

   QGridLayout* removeGrid = new QGridLayout();
   removeGrid->addWidget(removeItemButton,0,0);
   removeGrid->addWidget(clearListButton,0,1);

   QGridLayout* pickGrid = new QGridLayout();
   pickGrid->addWidget(pickMaterialButton_, 0, 0);
   pickGrid->addWidget(fillMaterialButton_, 0, 1);
162

163
   QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom, toolBox);
Isaak Lim's avatar
Isaak Lim committed
164 165 166
   layout->addWidget(materials);
   layout->addWidget(materialListWidget_);

167 168
   layout->addLayout(removeGrid);
   layout->addLayout(pickGrid);
Isaak Lim's avatar
Isaak Lim committed
169 170 171 172 173

   connect(pickMaterialButton_, SIGNAL(clicked()), this, SLOT(slotPickMaterialMode()));
   connect(fillMaterialButton_, SIGNAL(clicked()), this, SLOT(slotFillMaterialMode()));
   connect(clearListButton, SIGNAL(clicked()), this, SLOT(clearList()));
   connect(materialListWidget_, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(editMode(QListWidgetItem*)));
Matthias Möller's avatar
Matthias Möller committed
174
   connect(materialListWidget_->itemDelegate(), SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)),this,SLOT(saveNewName(QWidget*, QAbstractItemDelegate::EndEditHint)));
175
   connect(removeItemButton, SIGNAL(clicked()), this, SLOT(slotRemoveCurrentItem()));
Matthias Möller's avatar
Matthias Möller committed
176
   connect(materialListWidget_,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(createContextMenu(const QPoint&)));
Isaak Lim's avatar
Isaak Lim committed
177

Matthias Möller's avatar
Matthias Möller committed
178
   materialListWidget_->setContextMenuPolicy(Qt::CustomContextMenu);
Jan Möbius's avatar
Jan Möbius committed
179 180
   QIcon* toolIcon = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"material_picker.png");
   emit addToolbox( tr("Material Picker"), toolBox, toolIcon);
Isaak Lim's avatar
Isaak Lim committed
181
}
182 183 184 185 186 187 188
//------------------------------------------------------------------------------

void MaterialPicker::removeItem(QListWidgetItem* _item)
{
  unsigned index = materialListWidget_->row(_item);
  materialListWidget_->takeItem(index);
  materialList_.erase(materialList_.begin()+index);
Matthias Möller's avatar
Matthias Möller committed
189
  materialStrings_.erase(materialStrings_.begin()+index);
190 191 192 193
  if (materialStrings_.isEmpty())
    OpenFlipperSettings().remove(propName_);
  else
    OpenFlipperSettings().setValue(propName_, materialStrings_);
194
  fillMaterialButton_->setEnabled(materialListWidget_->count());
195 196 197 198 199 200 201 202 203 204 205 206

  //update hotkey table
  std::map<int,size_t>::iterator eraseIter = shortKeyRow_.end();
  for (std::map<int,size_t>::iterator iter = shortKeyRow_.begin(); iter != shortKeyRow_.end(); ++iter)
  {
    if (iter->second > index)
      --(iter->second);
    else if (iter->second == index)
      eraseIter = iter;
  }
  if (eraseIter != shortKeyRow_.end())
    shortKeyRow_.erase(eraseIter);
207

208
}
Isaak Lim's avatar
Isaak Lim committed
209 210 211 212 213 214

//------------------------------------------------------------------------------

void MaterialPicker::clearList() {
  materialListWidget_->clear();
  materialList_.clear();
Matthias Möller's avatar
Matthias Möller committed
215
  materialStrings_.clear();
216
  fillMaterialButton_->setEnabled(false);
Matthias Möller's avatar
doc  
Matthias Möller committed
217 218

  //setting value empty instead of removing will cause an error at start up
219
  OpenFlipperSettings().remove(propName_);
Isaak Lim's avatar
Isaak Lim committed
220 221 222 223
}

//------------------------------------------------------------------------------

224 225
void MaterialPicker::slotRemoveCurrentItem()
{
226 227
  if (!materialListWidget_->count())
    return;
Matthias Möller's avatar
doc  
Matthias Möller committed
228

229 230 231 232 233 234 235 236 237
  QMessageBox msgBox;
  QListWidgetItem* item = materialListWidget_->currentItem();
  msgBox.setText(tr("Remove ")+plainName(item->text(),materialListWidget_->currentRow())+tr("?"));
  msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
  msgBox.setDefaultButton(QMessageBox::Ok);
  int ret = msgBox.exec();

  if (ret == QMessageBox::Ok)
  removeItem(materialListWidget_->currentItem());
238 239 240
}
//------------------------------------------------------------------------------

Isaak Lim's avatar
Isaak Lim committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
void MaterialPicker::slotPickMaterialMode() {
  pickMaterialButton_->setChecked(true);
  fillMaterialButton_->setChecked(false);
  pickMaterial_ = true;
  fillMaterial_ = false;

  PluginFunctions::actionMode( Viewer::PickingMode );
  PluginFunctions::pickMode(pickModeName_);
}

//------------------------------------------------------------------------------

void MaterialPicker::slotFillMaterialMode() {
  fillMaterialButton_->setChecked(true);
  pickMaterialButton_->setChecked(false);
  fillMaterial_ = true;
  pickMaterial_ = false;

  PluginFunctions::actionMode( Viewer::PickingMode );
  PluginFunctions::pickMode(pickModeName_);
}

//------------------------------------------------------------------------------

void MaterialPicker::pluginsInitialized() {
  emit addPickMode(pickModeName_);
Matthias Möller's avatar
Matthias Möller committed
267 268
  for (unsigned i = 0; i < supportedKeys_; ++i)
    emit registerKey (Qt::Key_1+i, Qt::ControlModifier, QString(tr("Material %1")).arg(i+1), false);
Isaak Lim's avatar
Isaak Lim committed
269 270 271 272 273 274 275 276 277
}

//------------------------------------------------------------------------------

void MaterialPicker::slotMouseEvent(QMouseEvent* _event) {
  if ( PluginFunctions::pickMode() != pickModeName_)
    return;

  if (_event->type() == QEvent::MouseButtonPress) {
278
    size_t          node_idx, target_idx;
Isaak Lim's avatar
Isaak Lim committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    OpenMesh::Vec3d hitPoint;

    // Get picked object's identifier by picking in scenegraph
    if ( PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING ,_event->pos(), node_idx, target_idx, &hitPoint) ){
      BaseObjectData* object;
      if ( PluginFunctions::getPickedObject(node_idx, object) ) {

        // pick material
        if ( pickMaterial_ && !fillMaterial_ ) {
          MaterialNode* material = object->materialNode();

          if (material) {

            // store the material information
            MaterialInfo materialInfo;
            materialInfo.color_material = material->colorMaterial();
            materialInfo.base_color = material->base_color();
            materialInfo.ambient_color = material->ambient_color();
            materialInfo.diffuse_color = material->diffuse_color();
            materialInfo.specular_color = material->specular_color();
            materialInfo.shininess = material->shininess();
            materialInfo.reflectance = material->reflectance();

Matthias Möller's avatar
Matthias Möller committed
302 303
            materialInfo.key = Qt::Key_unknown;
            if (shortKeyRow_.size() < supportedKeys_)
304
            {
305
              materialInfo.key = Qt::Key_1+(int)shortKeyRow_.size();
Matthias Möller's avatar
Matthias Möller committed
306
              shortKeyRow_[materialInfo.key] = materialListWidget_->count();
307 308
            }

Isaak Lim's avatar
Isaak Lim committed
309
            // update list widget and material list
310
            QString name = QString("material id: %1").arg(material->id());
Matthias Möller's avatar
Matthias Möller committed
311
            materialListWidget_->addItem( itemName(name,materialInfo.key) );
Isaak Lim's avatar
Isaak Lim committed
312 313 314 315
            materialListWidget_->setCurrentItem( materialListWidget_->item(materialListWidget_->count() - 1) );

            materialList_.push_back(materialInfo);

Matthias Möller's avatar
Matthias Möller committed
316 317 318 319 320
            //save material
            QString matStr = materialString(materialInfo,name);
            materialStrings_.push_back(matStr);
            OpenFlipperSettings().setValue(propName_, materialStrings_);

321
            fillMaterialButton_->setEnabled(true);
Matthias Möller's avatar
Matthias Möller committed
322
            OpenFlipperSettings().setValue(propName_, materialStrings_);
Isaak Lim's avatar
Isaak Lim committed
323 324 325 326 327 328 329 330
          }

        // apply material from current item in list widget to picked object
        } else if ( fillMaterial_ && !pickMaterial_ ){
          MaterialNode* material = object->materialNode();

          if (material) {

331 332 333 334 335 336 337 338 339 340 341
            if (materialListWidget_->count() > 0)
            {
              int row = materialListWidget_->currentRow();
              material->colorMaterial(materialList_[row].color_material);
              material->set_base_color(materialList_[row].base_color);
              material->set_ambient_color(materialList_[row].ambient_color);
              material->set_diffuse_color(materialList_[row].diffuse_color);
              material->set_specular_color(materialList_[row].specular_color);
              material->set_shininess(materialList_[row].shininess);
              material->set_reflectance(materialList_[row].reflectance);
            }
Isaak Lim's avatar
Isaak Lim committed
342 343 344 345 346 347 348 349 350 351

          }
        }
      }
    }
  }
}

//------------------------------------------------------------------------------

Matthias Möller's avatar
Matthias Möller committed
352 353 354 355 356 357
void MaterialPicker::editModeCurrent()
{
  editMode(materialListWidget_->currentItem());
}

//------------------------------------------------------------------------------
Isaak Lim's avatar
Isaak Lim committed
358 359 360
void MaterialPicker::editMode(QListWidgetItem* _item) {
  _item->setFlags(_item->flags() | Qt::ItemIsEditable);
  materialListWidget_->editItem(_item);
Matthias Möller's avatar
Matthias Möller committed
361
  _item->setText( plainName(_item->text(),materialListWidget_->row(_item)));
Isaak Lim's avatar
Isaak Lim committed
362 363
}

364
//------------------------------------------------------------------------------
Matthias Möller's avatar
Matthias Möller committed
365 366 367 368 369 370 371 372 373
void MaterialPicker::saveNewName ( QWidget * _editor, QAbstractItemDelegate::EndEditHint _hint )
{
  saveNewName(materialListWidget_->currentItem());
}
//------------------------------------------------------------------------------
QString MaterialPicker::plainName(const QString &string, int index)
{
  if (materialList_[index].key == Qt::Key_unknown)
    return string;
Matthias Möller's avatar
doc  
Matthias Möller committed
374

Matthias Möller's avatar
Matthias Möller committed
375 376 377 378 379
  QString str(string);
  return str.remove(0,4);
}
//------------------------------------------------------------------------------
void MaterialPicker::saveNewName (QListWidgetItem* _item)
380 381
{
  unsigned index = materialListWidget_->row(_item);
Matthias Möller's avatar
Matthias Möller committed
382
  QString str = materialStrings_[index];
383
  QStringList strList = str.split(";");
384 385

  //pass name
386
  strList[0] = _item->text();
Matthias Möller's avatar
Matthias Möller committed
387 388 389 390
  //highlight hotkey support
  if (materialList_[index].key != Qt::Key_unknown)
    _item->setText( itemName(strList[0], materialList_[index].key) );

391 392

  //create new String to save
393 394 395 396
  str = "";
  for (int i = 0; i < strList.size()-1; ++i)
    str += strList[i] + ";";
  str += strList[strList.size()-1];
Matthias Möller's avatar
Matthias Möller committed
397 398 399 400
  materialStrings_[index] = str;
  OpenFlipperSettings().setValue(propName_, materialStrings_);
}
//------------------------------------------------------------------------------
401

Matthias Möller's avatar
Matthias Möller committed
402 403 404 405 406
QString MaterialPicker::itemName(const QString &_name, int _key)
{
  if (_key == Qt::Key_unknown)
    return _name;
  return QString(tr("(%1) ")).arg(QString::number(_key-Qt::Key_1+1)) +_name;
407
}
Matthias Möller's avatar
Matthias Möller committed
408

Isaak Lim's avatar
Isaak Lim committed
409 410 411 412 413 414 415
//------------------------------------------------------------------------------

void MaterialPicker::slotPickModeChanged(const std::string& _mode) {
  pickMaterialButton_->setChecked( _mode == pickModeName_ && pickMaterial_ );
  fillMaterialButton_->setChecked( _mode == pickModeName_ && fillMaterial_ );
}

Matthias Möller's avatar
Matthias Möller committed
416
//------------------------------------------------------------------------------
417 418
void MaterialPicker::slotKeyEvent(QKeyEvent* _event)
{
Matthias Möller's avatar
Matthias Möller committed
419
  for (unsigned i = 0; i < supportedKeys_; ++i)
420
  {
Matthias Möller's avatar
Matthias Möller committed
421 422 423 424 425 426
    int key = Qt::Key_1+i;
    if (_event->key() == key && _event->modifiers() == Qt::ControlModifier)
    {
      if (shortKeyRow_.find(key) == shortKeyRow_.end())
        return;
      slotFillMaterialMode();
427
      materialListWidget_->setCurrentRow((int)shortKeyRow_[key]);
Matthias Möller's avatar
Matthias Möller committed
428
    }
429
  }
Matthias Möller's avatar
Matthias Möller committed
430 431
}
//------------------------------------------------------------------------------
432
void MaterialPicker::changeHotKey(const int _key)
Matthias Möller's avatar
Matthias Möller committed
433 434 435
{
  std::map<int,size_t>::iterator iter = shortKeyRow_.find(_key);

436 437 438
  if (iter != shortKeyRow_.end())
  {
    //remove old key
439
    int oldIndex = (int)iter->second;
440 441 442 443 444 445 446
    QListWidgetItem* oldItem = materialListWidget_->item(oldIndex);
    //remove name
    oldItem->setText( plainName(oldItem->text(),oldIndex) );
    materialList_[oldIndex].key = Qt::Key_unknown; //unregister key after rename, otherwise the renaming will fail
    materialStrings_[oldIndex] = materialString(materialList_[oldIndex],oldItem->text());
    saveNewName(oldItem);
  }
Matthias Möller's avatar
Matthias Möller committed
447 448

  //set the new item (save and hint)
449
  int newIndex = materialListWidget_->currentRow();
Matthias Möller's avatar
Matthias Möller committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
  QListWidgetItem* newItem = materialListWidget_->item(newIndex);
  materialList_[newIndex].key = _key;

  materialStrings_[newIndex] = materialString(materialList_[newIndex],newItem->text());
  saveNewName(newItem);

  shortKeyRow_[_key] = newIndex;
}
//------------------------------------------------------------------------------
QString MaterialPicker::materialString(const MaterialInfo& _mat, const QString &_name)
{
  std::stringstream stream;
  stream << _name.toStdString();
  stream << ";" << _mat.color_material;
  stream << ";" << _mat.base_color;
  stream << ";" << _mat.ambient_color;
  stream << ";" << _mat.diffuse_color;
  stream << ";" << _mat.specular_color;
  stream << ";" << _mat.shininess;
  stream << ";" << _mat.reflectance;
  stream << ";" << _mat.key;

  return QString(stream.str().c_str());
}
//------------------------------------------------------------------------------
void MaterialPicker::slotMaterialProperties()
{
  if (materialNode_)
    return;

Jan Möbius's avatar
Jan Möbius committed
480
  //QListWidgetItem* item = materialListWidget_->currentItem();
Matthias Möller's avatar
Matthias Möller committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  materialListWidget_->setDisabled(true);

  materialNode_.reset(new MaterialNode());
  int row = materialListWidget_->currentRow();
  materialNode_->colorMaterial(materialList_[row].color_material);
  materialNode_->set_base_color(materialList_[row].base_color);
  materialNode_->set_ambient_color(materialList_[row].ambient_color);
  materialNode_->set_diffuse_color(materialList_[row].diffuse_color);
  materialNode_->set_specular_color(materialList_[row].specular_color);
  materialNode_->set_shininess(materialList_[row].shininess);
  materialNode_->set_reflectance(materialList_[row].reflectance);

  ACG::QtWidgets::QtMaterialDialog* dialog = new ACG::QtWidgets::QtMaterialDialog( 0, materialNode_.get() );

  dialog->setWindowFlags(dialog->windowFlags() | Qt::WindowStaysOnTopHint);

  connect(dialog,SIGNAL(finished(int)),this,SLOT(slotEnableListWidget(int)));
498
  connect(dialog,SIGNAL(accepted()),this,SLOT(slotMaterialChanged()));
Matthias Möller's avatar
Matthias Möller committed
499 500 501 502 503 504 505

  dialog->setWindowIcon( QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-material.png"));

  dialog->show();

}
//------------------------------------------------------------------------------
506
void MaterialPicker::slotMaterialChanged()
Matthias Möller's avatar
Matthias Möller committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
{
  if (materialNode_)
    {
      int index = materialListWidget_->currentRow();
      // store the material information
      MaterialInfo materialInfo;
      materialInfo.color_material = materialNode_->colorMaterial();
      materialInfo.base_color = materialNode_->base_color();
      materialInfo.ambient_color = materialNode_->ambient_color();
      materialInfo.diffuse_color = materialNode_->diffuse_color();
      materialInfo.specular_color = materialNode_->specular_color();
      materialInfo.shininess = materialNode_->shininess();
      materialInfo.reflectance = materialNode_->reflectance();
      materialInfo.key = materialList_[index].key;
      QString name = plainName(materialListWidget_->currentItem()->text(),materialListWidget_->currentRow());
      materialStrings_[index] = materialString(materialInfo,name);
      materialList_[index] = materialInfo;
      OpenFlipperSettings().setValue(propName_, materialStrings_);
    }
  OpenFlipperSettings().setValue(propName_, materialStrings_);
}

//------------------------------------------------------------------------------
void MaterialPicker::slotEnableListWidget(int _save){
  materialListWidget_->setEnabled(true);
  if (_save == QDialog::Accepted)
533
    slotMaterialChanged();
Matthias Möller's avatar
Matthias Möller committed
534 535 536 537 538 539 540 541
  materialNode_.reset();
}

//------------------------------------------------------------------------------
void MaterialPicker::createContextMenu(const QPoint& _point)
{
  QMenu *menu = new QMenu(materialListWidget_);

542
  QAction* action = menu->addAction(tr("Material Properties"));
Matthias Möller's avatar
Matthias Möller committed
543 544 545 546 547 548
  QIcon icon;
  icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"datacontrol-material.png");
  action->setIcon(icon);
  action->setEnabled(true);
  connect(action,SIGNAL(triggered(bool)),this,SLOT(slotMaterialProperties()));

549 550 551 552 553 554
  action = menu->addAction(tr("Rename"));
  connect(action,SIGNAL(triggered(bool)),this,SLOT(editModeCurrent()));

  action = menu->addAction(tr("Remove"));
  connect(action, SIGNAL(triggered(bool)),this,SLOT(slotRemoveCurrentItem()));

Matthias Möller's avatar
Matthias Möller committed
555 556
  menu->addSeparator();

557
  //add hotkey selectors
Matthias Möller's avatar
Matthias Möller committed
558 559
  QSignalMapper* signalMapper = new QSignalMapper(menu);
  for (unsigned i = 0; i < supportedKeys_; ++i)
560
  {
Matthias Möller's avatar
Matthias Möller committed
561 562 563 564
    QAction* action = menu->addAction(tr("Key %1").arg(i+1));
    connect(action,SIGNAL(triggered(bool)),signalMapper,SLOT(map()));
    signalMapper->setMapping(action,Qt::Key_1+i);
    std::map<int,size_t>::iterator iter = shortKeyRow_.find(Qt::Key_1+i);
565 566

    //Disable already selected hotkey number
Matthias Möller's avatar
Matthias Möller committed
567 568
    if (iter != shortKeyRow_.end() && iter->second == static_cast<size_t>(materialListWidget_->currentRow()))
      action->setDisabled(true);
569
  }
Matthias Möller's avatar
Matthias Möller committed
570 571 572 573 574

  connect(signalMapper, SIGNAL(mapped(const int &)),this, SLOT(changeHotKey(const int &)));
  menu->exec(materialListWidget_->mapToGlobal(_point),0);


575
}
Matthias Möller's avatar
Matthias Möller committed
576 577