openFunctions.cc 13.2 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
//=============================================================================
//
//                               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 "Core.hh"

#include <ACG/QtWidgets/QtFileDialog.hh>

#include "OpenFlipper/common/GlobalOptions.hh"
#include "OpenFlipper/BasePlugin/PluginFunctions.hh"

#include "OpenFlipper/widgets/loadWidget/loadWidget.hh"
#include "OpenFlipper/widgets/addEmptyWidget/addEmptyWidget.hh"

void Core::resetScenegraph() {
47
  if ( OpenFlipper::Options::gui() && !OpenFlipper::Options::loadingSettings() ) {
Jan Möbius's avatar
Jan Möbius committed
48 49 50

    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i ) {
      // update scene graph
51
      PluginFunctions::viewerProperties(i).lockUpdate();
Jan Möbius's avatar
Jan Möbius committed
52 53
      coreWidget_->examiner_widgets_[i]->sceneGraph(root_node_scenegraph_);
      coreWidget_->examiner_widgets_[i]->viewAll();
Jan Möbius's avatar
Jan Möbius committed
54 55
      coreWidget_->examiner_widgets_[i]->setScenePos( coreWidget_->examiner_widgets_[i]->scene_center() ,
                                                      coreWidget_->examiner_widgets_[i]->scene_radius() /* 10.0*/  );
Jan Möbius's avatar
Jan Möbius committed
56 57 58

      coreWidget_->examiner_widgets_[i]->setHome();
      coreWidget_->examiner_widgets_[i]->drawMode( OpenFlipper::Options::standardDrawMode() );
59
      PluginFunctions::viewerProperties(i).unLockUpdate();
Jan Möbius's avatar
Jan Möbius committed
60 61 62
      coreWidget_->examiner_widgets_[i]->updateGL();
    }

Jan Möbius's avatar
 
Jan Möbius committed
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
  }
}

//========================================================================================
// ===            Open/Add-Empty Functions                    ============================
//========================================================================================


void Core::slotGetAllFilters ( QStringList& _list){

  for (int i=0; i < (int)supportedTypes_.size(); i++){
    QString f = supportedTypes_[i].plugin->getLoadFilters();
    f = f.section(")",0,0).section("(",1,1).trimmed();
    _list << (QString::number(supportedTypes_[i].plugin->supportedType()) + " " + f);
  }
}

void Core::commandLineOpen(const char* _filename, bool asPolyMesh ){
  if (asPolyMesh)
    loadObject(DATA_POLY_MESH, _filename);
  else
    loadObject(_filename);
}

void Core::commandLineScript(const char* _filename ) {
  //check if we have scripting support:
  bool ok = false;
  slotPluginExists("scripting",ok);
  if ( ! ok ) {
    emit log(LOGERR ,"No scripting support available, please check if we load a scripting plugin");
    return;
  }

  emit executeFileScript(_filename);
}

/// Load object by guessing DataType depending on the files extension
int Core::loadObject ( QString _filename ) {
  if (_filename.endsWith(".ini")) {
102 103

    // Load all information from the given ini file
104
    openIniFile(_filename,true,true,true);
105

Jan Möbius's avatar
 
Jan Möbius committed
106 107
    if ( OpenFlipper::Options::gui() )
      coreWidget_->addRecent(_filename, DATA_NONE);
108

Jan Möbius's avatar
 
Jan Möbius committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    return -2;
  } else
  if (_filename.endsWith(".ofs")) {
     emit log(LOGINFO ,"Starting script execution of " + _filename);
     emit executeFileScript(_filename);
  } else
    return loadObject( DATA_TRIANGLE_MESH, _filename);

  return -1;
}

/// Function for loading a given file
int Core::loadObject( DataType _type, QString _filename) {

  if (_type == DATA_NONE)
    return loadObject(_filename);

  for (int i=0; i < (int)supportedTypes_.size(); i++)
    if (supportedTypes_[i].type == _type) {


      if ( OpenFlipper::Options::gui() ) {
        coreWidget_->statusMessage( "Loading " + _filename + " ...");
132
        if ( !OpenFlipper::Options::loadingSettings() )
Jan Möbius's avatar
 
Jan Möbius committed
133 134 135 136 137 138 139 140 141 142 143 144
          coreWidget_->setStatus(ApplicationStatus::PROCESSING );
      }

      //load file
      int id = supportedTypes_[i].plugin->loadObject(_filename);

      if ( OpenFlipper::Options::gui() ) {
        if ( id != -1 )
          coreWidget_->statusMessage( "Loading " + _filename + " ... done", 4000 );
        else
          coreWidget_->statusMessage( "Loading " + _filename + " ... failed!", 4000 );

145
        if ( !OpenFlipper::Options::loadingSettings() )
Jan Möbius's avatar
 
Jan Möbius committed
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
          coreWidget_->setStatus(ApplicationStatus::READY );
      }

      return id;
    }
  return -1; //no plugin found
}

/// Function for adding an empty object of given DataType
int Core::addEmptyObject( DataType _type ) {
  for (int i=0; i < (int)supportedTypes_.size(); i++)
    if (supportedTypes_[i].type == _type)
      return supportedTypes_[i].plugin->addEmpty();
  return -1; //no plugin found
}

//========================================================================================
// ===             Open/Add-Empty Slots                       ============================
//========================================================================================

/// Slot for adding an empty object of given DataType
void Core::slotAddEmptyObject( DataType _type , int& _id ) {
  _id = addEmptyObject( _type );
}

/// Slot for loading a given file
void Core::slotLoad(QString _filename, DataType _type, int& _id) {
  _id = loadObject(_type,_filename);
  if ( _id < 0 )
    _id = -1;
  else
    if ( OpenFlipper::Options::gui() )
      coreWidget_->addRecent(_filename,_type);
}

/// Slot gets called after a file-plugin has opened an object
 void Core::slotObjectOpened ( int _id ) {
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

  // get the opened object
   BaseObjectData* object;
   PluginFunctions::get_object(_id,object);

   if ( OpenFlipper::Options::randomBaseColor() )
     object->setBaseColor(ACG::Vec4f((float)(rand()%255)/255.0,
                          (float)(rand()%255)/255.0,
                           (float)(rand()%255)/255.0,
                            1.0 ));
   else{
      ACG::Vec4f color;
      QColor defColor = OpenFlipper::Options::defaultBaseColor();

      color[0] = defColor.redF();
      color[1] = defColor.greenF();
      color[2] = defColor.blueF();
      color[3] = defColor.alphaF();

      object->setBaseColor( color );
   }
Jan Möbius's avatar
 
Jan Möbius committed
204 205 206 207 208 209

   resetScenegraph();

   emit openedFile( _id );

  // Tell the Plugins that the Object List and the active object have changed
210
   emit signalObjectUpdated(_id);
Jan Möbius's avatar
 
Jan Möbius committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
   emit activeObjectChanged();

   backupRequest(_id,"Original Object");

   QString filename = object->path() + OpenFlipper::Options::dirSeparator() + object->name();

   BaseObject* object2;
   PluginFunctions::get_object(_id,object2);

   if ( OpenFlipper::Options::gui() )
    coreWidget_->addRecent( filename, object2->dataType() );

  // objectRoot_->dumpTree();
 }

 /// Slot gets called after a file-plugin has opened an object
 void Core::slotEmptyObjectAdded ( int _id ) {
  // Tell the Plugins that the Object List and the active object have changed
229
   emit signalObjectUpdated(_id);
Jan Möbius's avatar
 
Jan Möbius committed
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
   emit activeObjectChanged();

   backupRequest(_id,"Original Object");

  // get the opened object
   BaseObjectData* object;
   PluginFunctions::get_object(_id,object);

   ///@todo : set a default path for new objects
//    QString filename = object->path() + OpenFlipper::Options::dirSeparator() + object->name();

//    addRecent(filename);
 }

//========================================================================================
// ===             Menu Slots                                 ============================
//========================================================================================

/// Opens AddEmpty-Object widget
void Core::slotAddEmptyObjectMenu() {
  if (supportedTypes_.size() != 0){
    static addEmptyWidget* widget = 0;
    if ( !widget ){
      std::vector< DataType > types;
      QStringList typeNames;
Jan Möbius's avatar
 
Jan Möbius committed
255 256 257 258
      for (int i=0; i < (int)supportedTypes_.size(); i++) {
        types.push_back(supportedTypes_[i].type);
        typeNames.push_back(supportedTypes_[i].plugin->typeName());
      }
Jan Möbius's avatar
 
Jan Möbius committed
259 260 261 262 263 264 265 266
      widget = new addEmptyWidget(types,typeNames);
      connect(widget,SIGNAL(chosen(DataType, int&)),this,SLOT(slotAddEmptyObject(DataType, int&)));
    }
    widget->show();
  }else
    emit log(LOGERR,"Could not show 'add Empty' dialog. Missing file-plugins.");
}

267 268 269 270
//========================================================================================
// ===             Public Slots                                 ============================
//========================================================================================

Jan Möbius's avatar
 
Jan Möbius committed
271
/// Open Load-Object Widget
272 273 274 275 276
void Core::loadObject() {

  if ( OpenFlipper::Options::gui() ){

    if (supportedTypes_.size() != 0){
277 278 279
      LoadWidget* widget = new LoadWidget(supportedTypes_);
      connect(widget,SIGNAL(load(QString, DataType, int&)),this,SLOT(slotLoad(QString, DataType, int&)));
      connect(widget,SIGNAL(save(int, QString)),this,SLOT(saveObject(int, QString)));
Jan Möbius's avatar
Jan Möbius committed
280

281
      widget->showLoad();
282 283 284 285

      widget->disconnect();
      delete widget;

286 287
    }else
      emit log(LOGERR,"Could not show 'load objects' dialog. Missing file-plugins.");
Jan Möbius's avatar
 
Jan Möbius committed
288

289
  }
Jan Möbius's avatar
 
Jan Möbius committed
290 291
}

292 293 294 295 296 297
/// Load settings from file
void Core::loadSettings(){

  if ( OpenFlipper::Options::gui() ){

    QString complete_name;
Jan Möbius's avatar
Jan Möbius committed
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

    QFileDialog fileDialog( coreWidget_,
                            tr("Load Settings"),
                            OpenFlipper::Options::currentDirStr(),
                            tr("INI files (*.ini);;OBJ files (*.obj )") );

    fileDialog.setAcceptMode ( QFileDialog::AcceptOpen );
    fileDialog.setFileMode ( QFileDialog::AnyFile );

    QGridLayout *layout = (QGridLayout*)fileDialog.layout();

    QGroupBox* optionsBox = new QGroupBox( &fileDialog ) ;
    optionsBox->setSizePolicy( QSizePolicy ( QSizePolicy::Expanding , QSizePolicy::Preferred ) );
    optionsBox->setTitle("Options");
    layout->addWidget( optionsBox, layout->rowCount() , 0 , 1,layout->columnCount() );

    QCheckBox *loadProgramSettings = new QCheckBox(optionsBox);
    loadProgramSettings->setText("Load program settings");
    loadProgramSettings->setToolTip("Load all current program settings from the file ( This will include view settings, colors,...) ");
    loadProgramSettings->setCheckState( Qt::Unchecked );

    QCheckBox *loadPluginSettings = new QCheckBox(optionsBox);
    loadPluginSettings->setText("Load per Plugin Settings");
    loadPluginSettings->setToolTip("Plugins should load their current global settings from the file");
    loadPluginSettings->setCheckState( Qt::Checked );

    QCheckBox *loadObjectInfo = new QCheckBox(optionsBox);
    loadObjectInfo->setText("Load all objects defined in the file");
    loadObjectInfo->setToolTip("Load all objects which are defined in the file");
    loadObjectInfo->setCheckState( Qt::Checked );

    QBoxLayout* frameLayout = new QBoxLayout(QBoxLayout::TopToBottom,optionsBox);
    frameLayout->addWidget( loadProgramSettings , 0 , 0);
    frameLayout->addWidget( loadPluginSettings  , 1 , 0);
    frameLayout->addWidget( loadObjectInfo      , 2 , 0);
    frameLayout->addStretch();

Jan Möbius's avatar
Jan Möbius committed
336 337
    fileDialog.resize(550 ,500);

338 339 340 341 342 343 344 345 346 347 348 349
    // ========================================================================================
    // show the saveSettings-Dialog and get the target file
    // ========================================================================================
    QStringList fileNames;
    if (fileDialog.exec()) {
      fileNames = fileDialog.selectedFiles();
    } else {
      return;
    }

    if ( fileNames.size() > 1 ) {
      std::cerr << "Too many save filenames selected" << std::endl;
350
      return;
351 352 353 354
    }

    complete_name = fileNames[0];

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

356 357
    QString newpath = complete_name.section(OpenFlipper::Options::dirSeparator(),0,-2);
    OpenFlipper::Options::currentDir(newpath);
Jan Möbius's avatar
Jan Möbius committed
358

359
    if ( complete_name.endsWith("ini") ) {
360 361 362 363
      openIniFile( complete_name,
                   loadProgramSettings->isChecked(),
                   loadPluginSettings->isChecked(),
                   loadObjectInfo->isChecked());
364 365
      if ( loadProgramSettings->isChecked() )
        applyOptions();
366 367
    } else if ( complete_name.endsWith("obj") ) {
      openObjFile(complete_name);
368 369
      if ( loadProgramSettings->isChecked() )
        applyOptions();
370
    }
Jan Möbius's avatar
Jan Möbius committed
371

372 373 374
    coreWidget_->addRecent(complete_name, DATA_NONE);
  }
}
Jan Möbius's avatar
 
Jan Möbius committed
375

376 377
/// Load settings from file
void Core::loadSettings(QString _filename){
Jan Möbius's avatar
Jan Möbius committed
378

379
  if ( !QFile(_filename).exists() )
Jan Möbius's avatar
 
Jan Möbius committed
380 381
    return;

382
  QString newpath = _filename.section(OpenFlipper::Options::dirSeparator(),0,-2);
Jan Möbius's avatar
 
Jan Möbius committed
383 384
  OpenFlipper::Options::currentDir(newpath);

385
  if ( _filename.endsWith("ini") ) {
386
    // Loaded function for recent files. Load everything.
387
    openIniFile(_filename,true,true,true);
Jan Möbius's avatar
 
Jan Möbius committed
388
    applyOptions();
389 390
  } else if ( _filename.endsWith("obj") ) {
    openObjFile(_filename);
Jan Möbius's avatar
 
Jan Möbius committed
391 392 393 394
    applyOptions();
  }

}