MaterialPicker.cc 22.2 KB
Newer Older
Isaak Lim's avatar
Isaak Lim committed
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
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
*                                                                            *
\*===========================================================================*/

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

Isaak Lim's avatar
Isaak Lim committed
43 44 45 46 47


#include "MaterialPicker.hh"

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
48
#include <OpenFlipper/common/GlobalOptions.hh>
Matthias Möller's avatar
Matthias Möller committed
49
#include <ACG/QtWidgets/QtMaterialDialog.hh>
50
#include <sstream>
Isaak Lim's avatar
Isaak Lim committed
51

52 53
#include <ACG/Scenegraph/MaterialNode.hh>

54 55 56 57
#include <QSignalMapper>
#include <QMessageBox>
#include <QMenu>

Isaak Lim's avatar
Isaak Lim committed
58 59 60 61
//------------------------------------------------------------------------------
MaterialPicker::MaterialPicker()
  :
  pickModeName_("MaterialPicker"),
62
  propName_(name()+QString("/Materials")),
Isaak Lim's avatar
Isaak Lim committed
63 64 65
  pickMaterialButton_(0),
  fillMaterialButton_(0),
  materialListWidget_(0),
Matthias Möller's avatar
Matthias Möller committed
66 67 68
  materialStrings_(),
  shortKeyRow_(),
  materialNode_(),
Isaak Lim's avatar
Isaak Lim committed
69 70
  pickMaterial_(false),
  fillMaterial_(false)
Matthias Möller's avatar
Matthias Möller committed
71

Isaak Lim's avatar
Isaak Lim committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
{
}

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

MaterialPicker::~MaterialPicker() {

}

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

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

   pickMaterialButton_ = new QPushButton("&pick Material", toolBox);
   fillMaterialButton_ = new QPushButton("&fill Material", toolBox);
88 89
   QPushButton* clearListButton =  new QPushButton("Clear List", toolBox);
   QPushButton* removeItemButton = new QPushButton("Remove", toolBox);
Isaak Lim's avatar
Isaak Lim committed
90 91 92 93 94 95 96 97

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

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

   materialListWidget_ = new QListWidget(toolBox);

98
   //load saved materials
Matthias Möller's avatar
Matthias Möller committed
99 100
   materialStrings_ = OpenFlipperSettings().value(propName_, QStringList()).toStringList();
   for (int i = 0; i < materialStrings_.size(); ++i)
101
   {
Matthias Möller's avatar
Matthias Möller committed
102
     QStringList savedString = materialStrings_[i].split(";");
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
     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;
131 132 133
     stream.str("");
     stream.clear();
     stream << savedString[8].toStdString();
Matthias Möller's avatar
Matthias Möller committed
134
     stream >> materialInfo.key;
135

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

Matthias Möller's avatar
Matthias Möller committed
139
     materialListWidget_->addItem( itemName(savedString[0],materialInfo.key) );
140 141
     materialList_.push_back(materialInfo);
   }
142 143

   //if material was saved, set first as current
Matthias Möller's avatar
Matthias Möller committed
144
   if (materialStrings_.size())
145
     materialListWidget_->setCurrentItem(materialListWidget_->item(0));
146 147 148 149 150 151 152 153 154 155
   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);
156

157
   QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom, toolBox);
Isaak Lim's avatar
Isaak Lim committed
158 159 160
   layout->addWidget(materials);
   layout->addWidget(materialListWidget_);

161 162
   layout->addLayout(removeGrid);
   layout->addLayout(pickGrid);
Isaak Lim's avatar
Isaak Lim committed
163 164 165 166 167

   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
168
   connect(materialListWidget_->itemDelegate(), SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)),this,SLOT(saveNewName(QWidget*, QAbstractItemDelegate::EndEditHint)));
169
   connect(removeItemButton, SIGNAL(clicked()), this, SLOT(slotRemoveCurrentItem()));
Matthias Möller's avatar
Matthias Möller committed
170
   connect(materialListWidget_,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(createContextMenu(const QPoint&)));
Isaak Lim's avatar
Isaak Lim committed
171

Matthias Möller's avatar
Matthias Möller committed
172
   materialListWidget_->setContextMenuPolicy(Qt::CustomContextMenu);
173 174
   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
175
}
176 177 178 179 180 181 182
//------------------------------------------------------------------------------

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
183
  materialStrings_.erase(materialStrings_.begin()+index);
184 185 186 187
  if (materialStrings_.isEmpty())
    OpenFlipperSettings().remove(propName_);
  else
    OpenFlipperSettings().setValue(propName_, materialStrings_);
188
  fillMaterialButton_->setEnabled(materialListWidget_->count());
189 190 191 192 193 194 195 196 197 198 199 200

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

202
}
Isaak Lim's avatar
Isaak Lim committed
203 204 205 206 207 208

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

void MaterialPicker::clearList() {
  materialListWidget_->clear();
  materialList_.clear();
Matthias Möller's avatar
Matthias Möller committed
209
  materialStrings_.clear();
210
  fillMaterialButton_->setEnabled(false);
Matthias Möller's avatar
Matthias Möller committed
211 212

  //setting value empty instead of removing will cause an error at start up
213
  OpenFlipperSettings().remove(propName_);
Isaak Lim's avatar
Isaak Lim committed
214 215 216 217
}

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

218 219
void MaterialPicker::slotRemoveCurrentItem()
{
220 221
  if (!materialListWidget_->count())
    return;
Matthias Möller's avatar
Matthias Möller committed
222

223 224 225 226 227 228 229 230 231
  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());
232 233 234
}
//------------------------------------------------------------------------------

Isaak Lim's avatar
Isaak Lim committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
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
261 262
  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
263 264 265 266 267 268 269 270 271
}

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

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

  if (_event->type() == QEvent::MouseButtonPress) {
272
    size_t          node_idx, target_idx;
Isaak Lim's avatar
Isaak Lim committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    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
296 297
            materialInfo.key = Qt::Key_unknown;
            if (shortKeyRow_.size() < supportedKeys_)
298
            {
299
              materialInfo.key = Qt::Key_1+(int)shortKeyRow_.size();
Matthias Möller's avatar
Matthias Möller committed
300
              shortKeyRow_[materialInfo.key] = materialListWidget_->count();
301 302
            }

Isaak Lim's avatar
Isaak Lim committed
303
            // update list widget and material list
304
            QString name = QString("material id: %1").arg(material->id());
Matthias Möller's avatar
Matthias Möller committed
305
            materialListWidget_->addItem( itemName(name,materialInfo.key) );
Isaak Lim's avatar
Isaak Lim committed
306 307 308 309
            materialListWidget_->setCurrentItem( materialListWidget_->item(materialListWidget_->count() - 1) );

            materialList_.push_back(materialInfo);

Matthias Möller's avatar
Matthias Möller committed
310 311 312 313 314
            //save material
            QString matStr = materialString(materialInfo,name);
            materialStrings_.push_back(matStr);
            OpenFlipperSettings().setValue(propName_, materialStrings_);

315
            fillMaterialButton_->setEnabled(true);
Matthias Möller's avatar
Matthias Möller committed
316
            OpenFlipperSettings().setValue(propName_, materialStrings_);
Isaak Lim's avatar
Isaak Lim committed
317 318 319 320 321 322 323 324
          }

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

          if (material) {

325 326 327 328 329 330 331 332 333 334 335
            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
336 337 338 339 340 341 342 343 344 345

          }
        }
      }
    }
  }
}

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

Matthias Möller's avatar
Matthias Möller committed
346 347 348 349 350 351
void MaterialPicker::editModeCurrent()
{
  editMode(materialListWidget_->currentItem());
}

//------------------------------------------------------------------------------
Isaak Lim's avatar
Isaak Lim committed
352 353 354
void MaterialPicker::editMode(QListWidgetItem* _item) {
  _item->setFlags(_item->flags() | Qt::ItemIsEditable);
  materialListWidget_->editItem(_item);
Matthias Möller's avatar
Matthias Möller committed
355
  _item->setText( plainName(_item->text(),materialListWidget_->row(_item)));
Isaak Lim's avatar
Isaak Lim committed
356 357
}

358
//------------------------------------------------------------------------------
Matthias Möller's avatar
Matthias Möller committed
359 360 361 362 363 364 365 366 367
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
Matthias Möller committed
368

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

  //pass name
380
  strList[0] = _item->text();
Matthias Möller's avatar
Matthias Möller committed
381 382 383 384
  //highlight hotkey support
  if (materialList_[index].key != Qt::Key_unknown)
    _item->setText( itemName(strList[0], materialList_[index].key) );

385 386

  //create new String to save
387 388 389 390
  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
391 392 393 394
  materialStrings_[index] = str;
  OpenFlipperSettings().setValue(propName_, materialStrings_);
}
//------------------------------------------------------------------------------
395

Matthias Möller's avatar
Matthias Möller committed
396 397 398 399 400
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;
401
}
Matthias Möller's avatar
Matthias Möller committed
402

Isaak Lim's avatar
Isaak Lim committed
403 404 405 406 407 408 409
//------------------------------------------------------------------------------

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

430 431 432
  if (iter != shortKeyRow_.end())
  {
    //remove old key
433
    int oldIndex = (int)iter->second;
434 435 436 437 438 439 440
    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
441 442

  //set the new item (save and hint)
443
  int newIndex = materialListWidget_->currentRow();
Matthias Möller's avatar
Matthias Möller committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  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
474
  //QListWidgetItem* item = materialListWidget_->currentItem();
Matthias Möller's avatar
Matthias Möller committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
  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)));
492
  connect(dialog,SIGNAL(accepted()),this,SLOT(slotMaterialChanged()));
Matthias Möller's avatar
Matthias Möller committed
493 494 495 496 497 498 499

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

  dialog->show();

}
//------------------------------------------------------------------------------
500
void MaterialPicker::slotMaterialChanged()
Matthias Möller's avatar
Matthias Möller committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
{
  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)
527
    slotMaterialChanged();
Matthias Möller's avatar
Matthias Möller committed
528 529 530 531 532 533 534 535
  materialNode_.reset();
}

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

536
  QAction* action = menu->addAction(tr("Material Properties"));
Matthias Möller's avatar
Matthias Möller committed
537 538 539 540 541 542
  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()));

543 544 545 546 547 548
  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
549 550
  menu->addSeparator();

551
  //add hotkey selectors
Matthias Möller's avatar
Matthias Möller committed
552 553
  QSignalMapper* signalMapper = new QSignalMapper(menu);
  for (unsigned i = 0; i < supportedKeys_; ++i)
554
  {
Matthias Möller's avatar
Matthias Möller committed
555 556 557 558
    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);
559 560

    //Disable already selected hotkey number
Matthias Möller's avatar
Matthias Möller committed
561 562
    if (iter != shortKeyRow_.end() && iter->second == static_cast<size_t>(materialListWidget_->currentRow()))
      action->setDisabled(true);
563
  }
Matthias Möller's avatar
Matthias Möller committed
564 565 566 567 568

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


569
}
570 571