DataControlPlugin.cc 16.8 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 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 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 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 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 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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
//=============================================================================
//
//                               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.
// 
//  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 Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision$
//   $Author$
//   $Date$
//
//=============================================================================





#include <QtGui>

#include "DataControlPlugin.hh"

#include <QLayout>
#include <QGridLayout> 
#include <QItemSelectionModel>
 
#include <iostream>
#include <ACG/GL/GLState.hh>
#include <QStringList>
#include <ACG/Scenegraph/BaseNode.hh>
#include <QModelIndexList>

#include <queue>

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>


void DataControlPlugin::pluginsInitialized() {
  QMenu* contextMenu = new QMenu("Object selection");
  
  //Target Objects
  QAction* hideAction = new QAction(tr("&Hide"), this);
  hideAction->setStatusTip(tr("Hide object"));
  connect(hideAction, SIGNAL(triggered()), this, SLOT(slotContextMenuHide()) );
  contextMenu->addAction(hideAction);
  
  //Target Objects
  targetAction_ = new QAction(tr("&target"), this);
  targetAction_->setCheckable(true);
  targetAction_->setStatusTip(tr("Set object as target"));
  connect(targetAction_, SIGNAL(triggered()), this, SLOT(slotContextMenuTarget()) );
  contextMenu->addAction(targetAction_);
  
  //Source Objects
  sourceAction_ = new QAction(tr("&source"), this);
  sourceAction_->setCheckable(true);
  sourceAction_->setStatusTip(tr("Set object as source"));
  connect(sourceAction_, SIGNAL(triggered()), this, SLOT(slotContextMenuSource()) );
  contextMenu->addAction(sourceAction_);  
  
  emit addContextMenu(contextMenu , DATA_ALL , CONTEXTTOPLEVELMENU);   
}

bool DataControlPlugin::initializeToolbox(QWidget*& _widget)
{
   locked = false;
   tool_ = new QWidget();  
   _widget = tool_;
   QSize size(300, 300);
   tool_->resize(size); 
   MeshDialogLayout_ = new QGridLayout( tool_); 
   
   model_ = new TreeModel( );
   
   view_ = new QTreeView(0);
   view_->setModel(model_);
   
   view_->QTreeView::resizeColumnToContents(1);
   view_->QTreeView::resizeColumnToContents(2);
   view_->QTreeView::resizeColumnToContents(3);
   
   view_->setContextMenuPolicy(Qt::CustomContextMenu);
   
   view_->setSelectionBehavior(QAbstractItemView::SelectRows);
   view_->setSelectionMode(QAbstractItemView::ExtendedSelection);
   
   connect( model_,SIGNAL(dataChanged( const QModelIndex&, const QModelIndex& ) ),
             this,SLOT(slotDataChanged( const QModelIndex&, const QModelIndex& )));
   
   connect( model_ , SIGNAL(rowsRemoved( const QModelIndex &, int , int ) ),
            this , SLOT(slotRowsRemoved ( const QModelIndex &, int , int ) ) );
   
   connect( view_,SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
            this,SLOT(slotCustomContextMenuRequested ( const QPoint & ) ));
   
   MeshDialogLayout_->addWidget( view_ , 0,0  ); 
   
   viewHeader_ = view_->header(); 
   viewHeader_->setContextMenuPolicy(Qt::CustomContextMenu);
   
   // connect the slot for the context menu
   connect( viewHeader_, SIGNAL(customContextMenuRequested ( const QPoint &  )  ),
            this,        SLOT(slotHeaderCustomContextMenuRequested ( const QPoint & ) ));
   
   return true;
}

void DataControlPlugin::slotObjectUpdated( int _identifier ) {
  model_->updatedObject( _identifier );
}

void DataControlPlugin::slotCellClicked(int _row , int _col) {
   
//    if ( _col == 0 ) {
//          QTableWidgetItem* target = objectList_->item(_row,_col);
//          if ( target->checkState() == Qt::Checked ) {
//             target->setCheckState(Qt::Unchecked);
//             (*data_)[_row]->target(false); 
//          } else { 
//             target->setCheckState(Qt::Checked);
//             (*data_)[_row]->target(true); 
//          }
//         emit activeObjectChanged();
//    }
}

void DataControlPlugin::verticalHeaderClicked( int _row ) {
//     (*data_)[_row]->target ( !(*data_)[_row]->target() );
//    QTableWidgetItem* target = objectList_->item(_row,3);
//    if ( target->checkState() == Qt::Checked ) {
//       target->setCheckState(Qt::Unchecked);
//       (*data_)[_row]->target(false); 
//    } else { 
//       target->setCheckState(Qt::Checked);
//       (*data_)[_row]->target(true); 
//    }
//    emit activeObjectChanged();
// //    QTableWidgetSelectionRange range(5,0,5,3);
// //    objectList_->setRangeSelected(range,true);
}

void DataControlPlugin::verticalCountClicked( int  id , int old , int newc ) {
//    emit log(LOGWARN,"VHeader : Count " + QString::number(id) + " " + QString::number(old) + " " + QString::number(newc));
}


void DataControlPlugin::slotActiveObjectChanged() 
{
  ///@todo set all target checkboxes right
  // hint : dont call slotObjectUpdated -1 as this will do a redraw
//   slotObjectUpdated(-1);
  update_active();
}

void DataControlPlugin::update_active( ) {
  // find changed manipulator
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; 
                                        o_it != PluginFunctions::objects_end(); ++o_it)  {
      if ( o_it->target() ) {
               o_it->materialNode()->disable_blending();
               OpenMesh::Vec4f base_color = o_it->materialNode()->base_color();
               base_color[3] = 1.0;
               o_it->materialNode()->set_base_color(base_color);
               OpenMesh::Vec4f ambient_color = o_it->materialNode()->ambient_color();
               ambient_color[3] = 1.0;
               o_it->materialNode()->set_ambient_color(ambient_color);
               OpenMesh::Vec4f diffuse_color = o_it->materialNode()->diffuse_color();
               diffuse_color[3] = 1.0;
               o_it->materialNode()->set_diffuse_color(diffuse_color);
      }  else { 
            o_it->materialNode()->enable_blending();
            OpenMesh::Vec4f base_color = o_it->materialNode()->base_color();
            base_color[3] = 0.4;
            o_it->materialNode()->set_base_color(base_color);
            OpenMesh::Vec4f ambient_color = o_it->materialNode()->ambient_color();
            ambient_color[3] = 0.4;
            o_it->materialNode()->set_ambient_color(ambient_color);
            OpenMesh::Vec4f diffuse_color = o_it->materialNode()->diffuse_color();
            diffuse_color[3] = 0.4;
            o_it->materialNode()->set_diffuse_color(diffuse_color);
      }                                     
  }
  
  emit update_view();
}

void DataControlPlugin::slotKeyEvent( QKeyEvent* _event )
{

//    QList<QTableWidgetItem *> selected ;
//    QList<QTableWidgetSelectionRange> ranges;
//    switch (_event->key()) 
//    {
//      case Qt::Key_M : 
//           for ( uint i = 0 ; i < data_->size() ; ++i ) {
//                QTableWidgetSelectionRange range(i,0,i,3);
//                objectList_->setRangeSelected(range,true);
//          }
//          return;          
//           
//       case Qt::Key_N : 
//          std::cerr << "Checking : " << std::endl;
//          for ( uint i = 0 ; i < data_->size() ; ++i ) {
//             QTableWidgetItem * item = objectList_->verticalHeaderItem ( i );
//             if ( objectList_->isItemSelected(item)  ) 
//                std::cerr << i << item->text().toStdString() << " : yes" << std::endl;
//             else {
//                std::cerr << i << item->text().toStdString() << " : no" << std::endl;
//             }
//          }
//          
//          selected = objectList_->selectedItems();
//          std::cerr << "Selected " << selected.size() << std::endl;
//          
//          ranges = objectList_->selectedRanges();
//          std::cerr << "Ranges " << ranges.size() << std::endl;
//          
//          for ( int i = 0 ; i < ranges.size(); ++i ) {
//            std::cerr << "Range " << i << " : " << ranges[i].topRow() << " .. " << ranges[i].bottomRow() << std::endl;        
//            std::cerr << "Range " << i << " : " << ranges[i].leftColumn() << " .. " << ranges[i].rightColumn() << std::endl;        
//          }
//          
//          return;
//       case Qt::Key_C : 
//           if ( _event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier) ) {
//                for ( uint i = 0 ; i < data_->size() ; ++i ) {  
//                   (*data_)[i]->source(false);
//                }
//                emit updated_objects(-1);
//               return;
//           } 
//           
//           if ( _event->modifiers() == (Qt::AltModifier | Qt::ShiftModifier) ) {
//              for ( uint i = 0 ; i < data_->size() ; ++i ) {  
//                 (*data_)[i]->target(false);
//               }
//               emit updated_objects(-1);
//              return;
//           } 
//           
//           return;
//           
//        case Qt::Key_A : 
//           if ( _event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier) ) {
//                for ( uint i = 0 ; i < data_->size() ; ++i ) {  
//                   (*data_)[i]->source(true);
//                }
//                emit updated_objects(-1);
//               return;
//           } 
//           
//           if ( _event->modifiers() == (Qt::AltModifier | Qt::ShiftModifier) ) {
//              for ( uint i = 0 ; i < data_->size() ; ++i ) {  
//                 (*data_)[i]->target(true);
//               }
//               emit updated_objects(-1);
//              return;
//           } 
//           
//           return;
//       default:
//       break;
//   }
  
  if ( _event->modifiers() == Qt::ControlModifier ) {
    switch (_event->key()) {
      case Qt::Key_A : 
          setAllTarget();
        return;
      default:
        return;
    }
  }
}

void DataControlPlugin::setAllTarget() {
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; 
                                    o_it != PluginFunctions::objects_end(); ++o_it)  
  o_it->target(true);                       
  emit activeObjectChanged();
  emit updated_objects(-1);  
}

void DataControlPlugin::setAllSource() {
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; 
                                    o_it != PluginFunctions::objects_end(); ++o_it)  
  o_it->source(true);                       
  emit updated_objects(-1);  
}

void DataControlPlugin::clearAllTarget() {
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; 
                                    o_it != PluginFunctions::objects_end(); ++o_it)  
  o_it->target(false);                       
  emit activeObjectChanged();
  emit updated_objects(-1);  
}

void DataControlPlugin::clearAllSource() {
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; 
                                    o_it != PluginFunctions::objects_end(); ++o_it)  
  o_it->source(false);                       
  emit updated_objects(-1);  
}

void DataControlPlugin::hideAll() {
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; 
                                    o_it != PluginFunctions::objects_end(); ++o_it)  
    o_it->hide();                       
  emit update_view();
}

void DataControlPlugin::showAll() {
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; 
                                    o_it != PluginFunctions::objects_end(); ++o_it)  
    o_it->show();                       
  emit update_view();
}

void DataControlPlugin::slotDataChanged ( const QModelIndex & topLeft, 
                                          const QModelIndex & bottomRight ) 
{
  
  switch (topLeft.column()) {
    case 0:
//         std::cerr << "Todo : NameChange" << std::endl;
      break;
      
    // show/hide
    case 1:
      break;
      
    // source
    case 2:
      break; 
      
    // target
    case 3:
      emit activeObjectChanged();
      break;
      
    default:
      break;
  }

  view_->expandToDepth(0);
  emit update_view();
}

void DataControlPlugin::slotRowsRemoved ( const QModelIndex & _parent, int _start, int _end ) {
  std::cerr << "Row removed! " << std::endl;
  emit update_view();
}

void DataControlPlugin::loadIniFileOptions( INIFile& _ini ) {
  if ( !_ini.section_exists( "Groups" ) ) 
    return;
  
  // Names of all groups
  QStringList groupNames;
  
  // names of the primary groups
  QStringList rootGroup;
  
  // Get the list of group names to the file
  _ini.get_entry(groupNames,"Groups","groups");
  
  // Get the primary group names to the file
  _ini.get_entry(rootGroup,"Groups","rootGroup");
  
  // Go over one level of the groups
  while ( rootGroup.size() > 0 ) {
    QString current = rootGroup[0];
    rootGroup.removeFirst();
    
    QStringList groupChildren;
    QStringList elementChildren;
    
    _ini.get_entry(elementChildren ,current,"children");
    _ini.get_entry(groupChildren ,current,"subgroups");
    
    // if we get a parent item, scan the tree for it or use the root node otherwise
    BaseObject* parentItem;
    QString parentName;
    if ( _ini.get_entry(parentName,current,"parent") ) {
      parentItem = PluginFunctions::objectRoot()->childExists(parentName);
      if ( parentItem == 0 )
        parentItem = PluginFunctions::objectRoot(); 
    } else 
      parentItem = PluginFunctions::objectRoot(); 
    
    rootGroup << groupChildren;      
      
    // check if this group already exists
    BaseObject* group =  PluginFunctions::objectRoot()->childExists( current );
    
    // group does not exist
    if ( !group ) {
      
      group = dynamic_cast< BaseObject* >( new GroupObject( current, dynamic_cast< GroupObject* >(parentItem ) ) );
      
      parentItem->appendChild(group);
      group->setParent(parentItem);
    }
    
    // process children
    for ( int i = 0 ; i < elementChildren.size() ; ++i ) {
      BaseObject* childItem =  PluginFunctions::objectRoot()->childExists( elementChildren[i] );
      if ( childItem ) {
        childItem->parent()->removeChild(childItem);
        childItem->setParent(group);
        group->appendChild(childItem);
      }
    }
  }
    
  emit updated_objects(-1);   
}

void DataControlPlugin::saveIniFileOptions( INIFile& _ini ) {
  if ( !_ini.section_exists( "Groups" ) ) 
    _ini.add_section("Groups"); 
  
  std::queue< BaseObject* > children;
  children.push( PluginFunctions::objectRoot() );
  
  std::vector< BaseObject* > groups;
  
  // Get all groups from the tree
  while ( children.size() > 0 ) {
    BaseObject* item = children.front();
    children.pop();
    
    for ( int i = 0 ; i < item->childCount(); ++i ) 
      if ( item->child(i)->dataType(DATA_GROUP)) 
        children.push( item->child(i) ); 
    
    if ( item->dataType(DATA_GROUP) && (item != PluginFunctions::objectRoot() ) )
      groups.push_back(item);
  }
  
  // Names of all groups
  QStringList groupNames;
  
  // names of the primary groups
  QStringList rootGroup;
  
  for ( uint i = 0 ; i < groups.size() ; ++i ) {
    groupNames.push_back( groups[i]->name() );  
    
    if ( !_ini.section_exists( groups[i]->name() ) ) 
      _ini.add_section(groups[i]->name()); 
    
    _ini.add_entry(groups[i]->name(),"groupname",groups[i]->name());
    
    // write the name of the parent 
    if ( ( groups[i]->parent() != 0 ) && ( groups[i]->parent() != PluginFunctions::objectRoot() ) )
      _ini.add_entry(groups[i]->name(),"parent",groups[i]->parent()->name());
    
    if ( groups[i]->parent() == PluginFunctions::objectRoot() )
      rootGroup.push_back( groups[i]->name() );
    
    // Write a list of this groups children
    QStringList groupchildren;
    QStringList elementchildren;
    for ( int j = 0 ; j < groups[i]->childCount(); ++j ) {
      if  ( groups[i]->child(j)->dataType(DATA_GROUP) )
        groupchildren.push_back( groups[i]->child(j)->name() );
      else
        elementchildren.push_back( groups[i]->child(j)->name() );
    }
    
    _ini.add_entry(groups[i]->name(),"subgroups",groupchildren);
    _ini.add_entry(groups[i]->name(),"children",elementchildren);
  }
  
  // Write the list of group names to the file
  _ini.add_entry("Groups","groups",groupNames);
  
  // Write the primary group names to the file
  _ini.add_entry("Groups","rootGroup",rootGroup);
}


Q_EXPORT_PLUGIN2( datacontrolplugin , DataControlPlugin );