MaterialPicker.cc 13.1 KB
Newer Older
Isaak Lim's avatar
Isaak Lim committed
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2012 by Computer Graphics Group, RWTH Aachen       *
Isaak Lim's avatar
Isaak Lim 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 33 34 35 36
*                           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/>.                                       *
*                                                                            *
\*===========================================================================*/

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


#include "MaterialPicker.hh"

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
47 48
#include <OpenFlipper/common/GlobalOptions.hh>
#include <sstream>
Isaak Lim's avatar
Isaak Lim committed
49 50 51 52 53

//------------------------------------------------------------------------------
MaterialPicker::MaterialPicker()
  :
  pickModeName_("MaterialPicker"),
54
  propName_("MaterialPicker/Materials"),
Isaak Lim's avatar
Isaak Lim committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  pickMaterialButton_(0),
  fillMaterialButton_(0),
  materialListWidget_(0),
  pickMaterial_(false),
  fillMaterial_(false)
{
}

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

MaterialPicker::~MaterialPicker() {

}

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

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

   pickMaterialButton_ = new QPushButton("&pick Material", toolBox);
   fillMaterialButton_ = new QPushButton("&fill Material", toolBox);
76 77
   QPushButton* clearListButton =  new QPushButton("Clear List", toolBox);
   QPushButton* removeItemButton = new QPushButton("Remove", toolBox);
Isaak Lim's avatar
Isaak Lim committed
78 79 80 81 82 83 84 85

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

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

   materialListWidget_ = new QListWidget(toolBox);

86 87
   //load saved materials
   materialString_ = OpenFlipperSettings().value("MaterialPickerPlugin/Materials", QStringList()).toStringList();
88

89
   for (int i = 0; i < materialString_.size(); ++i)
90
   {
91
     QStringList savedString = materialString_[i].split(";");
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
     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;


     materialListWidget_->addItem( savedString[0] );
     materialList_.push_back(materialInfo);
   }
125 126 127

   //if material was saved, set first as current
   if (materialString_.size())
128
     materialListWidget_->setCurrentItem(materialListWidget_->item(0));
129 130 131 132 133 134 135 136 137 138
   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);
139

140
   QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom, toolBox);
Isaak Lim's avatar
Isaak Lim committed
141 142 143
   layout->addWidget(materials);
   layout->addWidget(materialListWidget_);

144 145
   layout->addLayout(removeGrid);
   layout->addLayout(pickGrid);
Isaak Lim's avatar
Isaak Lim committed
146 147 148 149 150

   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*)));
151
   connect(materialListWidget_,SIGNAL(itemChanged ( QListWidgetItem*)), this, SLOT(assignNewName(QListWidgetItem*)));
152
   connect(removeItemButton, SIGNAL(clicked()), this, SLOT(slotRemoveCurrentItem()));
Isaak Lim's avatar
Isaak Lim committed
153

Jan Möbius's avatar
Jan Möbius committed
154 155
   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
156
}
157 158 159 160 161 162 163 164 165 166 167
//------------------------------------------------------------------------------

void MaterialPicker::removeItem(QListWidgetItem* _item)
{
  unsigned index = materialListWidget_->row(_item);
  materialListWidget_->takeItem(index);
  materialList_.erase(materialList_.begin()+index);
  materialString_.erase(materialString_.begin()+index);
  OpenFlipperSettings().setValue(propName_, materialString_);
  fillMaterialButton_->setEnabled(materialListWidget_->count());
}
Isaak Lim's avatar
Isaak Lim committed
168 169 170 171 172 173

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

void MaterialPicker::clearList() {
  materialListWidget_->clear();
  materialList_.clear();
174 175 176
  materialString_.clear();
  OpenFlipperSettings().setValue(propName_, materialString_);
  fillMaterialButton_->setEnabled(false);
Isaak Lim's avatar
Isaak Lim committed
177 178 179 180
}

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

181 182 183 184 185 186 187
void MaterialPicker::slotRemoveCurrentItem()
{
  if (materialListWidget_->count())
    removeItem(materialListWidget_->currentItem());
}
//------------------------------------------------------------------------------

Isaak Lim's avatar
Isaak Lim committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
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_);
}

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

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

  if (_event->type() == QEvent::MouseButtonPress) {
    unsigned int node_idx, target_idx;
    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();

            // update list widget and material list
248 249
            QString name = QString("material id: %1").arg(material->id());
            materialListWidget_->addItem( name );
Isaak Lim's avatar
Isaak Lim committed
250 251 252 253
            materialListWidget_->setCurrentItem( materialListWidget_->item(materialListWidget_->count() - 1) );

            materialList_.push_back(materialInfo);

254
            //save new material
255 256 257 258 259 260 261 262 263 264 265
            std::stringstream stream;
            stream << name.toStdString();
            stream << ";" << materialInfo.color_material;
            stream << ";" << materialInfo.base_color;
            stream << ";" << materialInfo.ambient_color;
            stream << ";" << materialInfo.diffuse_color;
            stream << ";" << materialInfo.specular_color;
            stream << ";" << materialInfo.shininess;
            stream << ";" << materialInfo.reflectance;

            QString materialString = stream.str().c_str();
266 267 268
            materialString_.push_back(materialString);
            fillMaterialButton_->setEnabled(true);
            OpenFlipperSettings().setValue(propName_, materialString_);
Isaak Lim's avatar
Isaak Lim committed
269 270 271 272 273 274 275 276
          }

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

          if (material) {

277 278 279 280 281 282 283 284 285 286 287
            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
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

          }
        }
      }
    }
  }
}

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

void MaterialPicker::editMode(QListWidgetItem* _item) {
  _item->setFlags(_item->flags() | Qt::ItemIsEditable);
  materialListWidget_->editItem(_item);
}

303 304 305 306
//------------------------------------------------------------------------------
void MaterialPicker::assignNewName(QListWidgetItem* _item)
{
  unsigned index = materialListWidget_->row(_item);
307
  QString str = materialString_[index];
308 309 310 311 312 313
  QStringList strList = str.split(";");
  strList[0] = _item->text();
  str = "";
  for (int i = 0; i < strList.size()-1; ++i)
    str += strList[i] + ";";
  str += strList[strList.size()-1];
314 315
  materialString_[index] = str;
  OpenFlipperSettings().setValue(propName_, materialString_);
316 317

}
Isaak Lim's avatar
Isaak Lim committed
318 319 320 321 322 323 324 325
//------------------------------------------------------------------------------

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

Q_EXPORT_PLUGIN2( materialPicker , MaterialPicker );