loadWidget.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 8520 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2010-02-10 15:56:59 +0100 (Mi, 10. Feb 2010) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 
00046 #include "loadWidget.hh"
00047 #include <OpenFlipper/common/GlobalOptions.hh>
00048 #include "OpenFlipper/BasePlugin/PluginFunctions.hh"
00049 #include <QCompleter>
00050 #include <QMessageBox>
00051 #include <QFile>
00052 #include <QFileDialog>
00053 #include <QDir>
00054 #include <QDebug>
00055 
00056 #include <QHBoxLayout>
00057 
00058 #include "FileOptionsDialog.hh"
00059 
00060 #include <OpenFlipper/INIFile/INIFile.hh>
00061 #include <OpenFlipper/common/GlobalOptions.hh>
00062 
00063 LoadWidget::LoadWidget(std::vector<fileTypes>& _supportedTypes , QWidget *parent)
00064   : QFileDialog(parent),
00065     loadMode_(true),
00066     supportedTypes_(_supportedTypes)
00067 {
00068   setOption (QFileDialog::DontUseNativeDialog, true);
00069 
00070   // Get our layout
00071   QGridLayout *gridLayout = (QGridLayout*)layout();
00072 
00073   //supported Types
00074   optionsBox_ = new QCheckBox(tr("use defaults"), this);
00075   optionsBox_->setChecked( OpenFlipperSettings().value("Core/File/UseLoadDefaults",false).toBool() );
00076   
00077   // add the options box to the bottom
00078   gridLayout->addWidget( optionsBox_, gridLayout->rowCount() , 1 );
00079 
00080   // Add a very nice label for it
00081   QLabel* typeLabel = new QLabel(tr("Options:") , this);
00082   gridLayout->addWidget( typeLabel, gridLayout->rowCount() -1 , 0 );
00083 
00084   //overwrite dialog shouldn't be handled by the qfiledialog
00085   setConfirmOverwrite(false);
00086 
00087   setDirectory( OpenFlipperSettings().value("Core/CurrentDir").toString() );
00088 }
00089 
00091 LoadWidget::~LoadWidget()
00092 {
00093 
00094 }
00095 
00097 void LoadWidget::slotSetLoadFilters(){
00098 
00099   QStringList allFilters;
00100 
00101   //TODO All files filter
00102   
00103   for (int i=0; i < (int)supportedTypes_.size(); i++){
00104     QStringList filters = supportedTypes_[i].loadFilters.split(";;");
00105     for (int f=filters.size()-1; f >= 0;  f--){
00106       if (filters[f].trimmed() == "") { filters.removeAt(f); continue; }
00107       if (filters[f].contains( tr("All files") ) ) filters.removeAt(f);
00108     }
00109 
00110     allFilters.append( filters );
00111   }
00112 
00113   allFilters.removeDuplicates();
00114   allFilters.sort();
00115 
00116   QStringList allExt;
00117   
00118   //get all possible extensions
00119   for (int i=0; i < allFilters.size(); i++){
00120      QString ext = allFilters[i].section("(",1).section(")",0,0);
00121      allExt.append( ext.split(" ") );
00122   }
00123 
00124   for (int f=allExt.size()-1; f >= 0;  f--)
00125     if (allExt[f].trimmed() == "") allExt.removeAt(f);
00126   
00127   allExt.removeDuplicates();
00128   allExt.sort();
00129   
00130   QString allFiles = tr("All Files (");
00131   
00132   for (int i=0; i < allExt.size(); i++)
00133     allFiles += " " + allExt[i];
00134   
00135   allFiles += " )";
00136 
00137   allFilters.push_front(allFiles);
00138   
00139   setNameFilters(allFilters);
00140 }
00141 
00143 void LoadWidget::slotSetSaveFilters(DataType _type){
00144 
00145   QStringList allFilters;
00146 
00147   for (int i=0; i < (int)supportedTypes_.size(); i++)
00148     if (supportedTypes_[i].type & _type){
00149       QStringList filters = supportedTypes_[i].saveFilters.split(";;");
00150       for (int f=filters.size()-1; f >= 0;  f--){
00151         if (filters[f].trimmed() == "") { filters.removeAt(f); continue; }
00152         if (filters[f].contains( tr("All files") ) ) filters.removeAt(f);
00153       }
00154       
00155       allFilters.append( filters );
00156     }
00157 
00158   allFilters.removeDuplicates();
00159   allFilters.sort();
00160 
00161   QStringList allExt;
00162   
00163   //get all possible extensions
00164   for (int i=0; i < allFilters.size(); i++){
00165      QString ext = allFilters[i].section("(",1).section(")",0,0);
00166      allExt.append( ext.split(" ") );
00167   }
00168 
00169   for (int f=allExt.size()-1; f >= 0;  f--)
00170     if (allExt[f].trimmed() == "") allExt.removeAt(f);
00171   
00172   allExt.removeDuplicates();
00173   allExt.sort();
00174   
00175   QString allFiles = tr("All Files (");
00176   
00177   for (int i=0; i < allExt.size(); i++)
00178     allFiles += " " + allExt[i];
00179   
00180   allFiles += " )";
00181 
00182   allFilters.push_front(allFiles);
00183   
00184   setNameFilters(allFilters);
00185 }
00186 
00187 
00189 void LoadWidget::loadFile(){
00190 
00191   //get selection
00192   QStringList files = selectedFiles();
00193 
00194   bool success = false;
00195 
00196   //get all extensions
00197   QStringList ext;
00198 
00199   for (int i=0; i < files.size(); i++)
00200     ext.push_back( QFileInfo(files[i]).suffix() );
00201   
00202    
00203   //find plugins that can handle the current extensions
00204   pluginForExtension_.clear();
00205   
00206   for (int i=0; i < ext.size(); i++){
00207     for (uint t=0; t < supportedTypes_.size(); t++){
00208       
00209       QString filters = supportedTypes_[t].loadFilters;  
00210       
00211       if (filters.contains(ext[i])){
00212         pluginForExtension_[ ext[i] ] = t;
00213         break;
00214       }
00215     }
00216   }
00217   
00218   getPluginForExtensionINI(ext);
00219   
00220   // display options for all dataTypes
00221   if ( !optionsBox_->isChecked() ){
00222 
00223     FileOptionsDialog options(supportedTypes_,ext, loadMode_);
00224 
00225     connect(&options, SIGNAL(setPluginForExtension(QString,int)), this, SLOT(slotSetPluginForExtension(QString,int)) );
00226 
00227     if ( !options.exec() )
00228       return;
00229   }
00230 
00231   //load the selected files
00232   for (int i=0; i < files.size(); i++){
00233     
00234     QFileInfo fi(files[i]);
00235     QString filename = fi.absoluteFilePath();
00236     OpenFlipperSettings().setValue("Core/CurrentDir", fi.absolutePath());
00237     QFile file(filename);
00238 
00239     if (fi.isDir() || !file.exists()) continue; //do nothing if its a not a valid file
00240     QString ext = fi.suffix();
00241 
00242     //emit load signal
00243     if ( pluginForExtension_.find( fi.suffix() ) != pluginForExtension_.end() ){
00244 
00245       emit load(filename, pluginForExtension_[ fi.suffix() ]);
00246       success = true;
00247     }
00248   }
00249 }
00250 
00252 void LoadWidget::saveFile(){
00253   //get selection
00254   QStringList files = selectedFiles();
00255 
00256   if ( files.size() != 1 ) {
00257     std::cerr << "Error no or multiple save files selected" << std::endl;
00258     return;
00259   }
00260 
00261   QString filename = files[0];
00262 
00263   //get filename
00264   if (!filename.contains(".",Qt::CaseSensitive)){ //check for extension
00265 
00266     int s = selectedFilter().indexOf("*")+1;
00267     int e = selectedFilter().indexOf(" ", s);
00268     int e2 = selectedFilter().indexOf(")", s);
00269     if (e == -1 || e2 < e) e = e2;
00270 
00271     QString ext = selectedFilter().mid(s,e-s);
00272     filename += ext;
00273   }
00274 
00275   QFile      f(filename);
00276   QFileInfo fi(filename);
00277    
00278   //find plugin that can handle the current extension
00279   pluginForExtension_.clear();
00280   
00281   for (uint t=0; t < supportedTypes_.size(); t++){
00282 
00283     QString filters = supportedTypes_[t].loadFilters;  
00284 
00285     if (filters.contains( fi.suffix() )){
00286       pluginForExtension_[ fi.suffix() ] = t;
00287       break;
00288     }
00289   }
00290   
00291   getPluginForExtensionINI(QStringList(fi.suffix()));
00292   
00293   // display options for all dataTypes
00294   if ( !optionsBox_->isChecked() ){
00295 
00296     FileOptionsDialog options(supportedTypes_,QStringList(fi.suffix()), loadMode_);
00297 
00298     connect(&options, SIGNAL(setPluginForExtension(QString,int)), this, SLOT(slotSetPluginForExtension(QString,int)) );
00299 
00300     if ( !options.exec() )
00301       return;
00302   }
00303 
00304   if (f.exists()){ //check for extension
00305     int ret = QMessageBox::warning(this, tr("File exists"),tr("This file already exists.\n"
00306         "Do you want to overwrite the file?"),QMessageBox::Yes|QMessageBox::No,QMessageBox::No);
00307     if (ret == QMessageBox::No)
00308       return; //abort if users doesn't want to overwrite
00309   }
00310 
00311   if ( pluginForExtension_.find( fi.suffix() ) != pluginForExtension_.end() )
00312     emit save(id_,filename, pluginForExtension_[fi.suffix()] );
00313 
00314   OpenFlipperSettings().setValue("Core/CurrentDir", fi.absolutePath() );
00315 }
00316 
00318 int LoadWidget::showLoad(){
00319   setAcceptMode ( QFileDialog::AcceptOpen );
00320   setWindowTitle(tr("Load Object"));
00321   loadMode_ = true;
00322 
00323   setFileMode(QFileDialog::ExistingFiles);
00324 
00325   slotSetLoadFilters();
00326 
00327   return this->exec();
00328 }
00329 
00331 int LoadWidget::showSave(int _id, QString _filename){
00332   setAcceptMode ( QFileDialog::AcceptSave );
00333   setFileMode( QFileDialog::AnyFile );
00334   setWindowTitle(tr("Save Object"));
00335   loadMode_ = false;
00336 
00337   id_ = _id;
00338 
00339   //set dataType
00340   BaseObjectData* object;
00341   PluginFunctions::getObject(_id,object);
00342 
00343   //check if we can save this dataType
00344   bool typeFound = false;
00345   
00346   for (int i=0; i < (int)supportedTypes_.size(); i++)
00347     if ( object->dataType( supportedTypes_[i].type ) )
00348       typeFound = true;
00349 
00350 
00351   if (!typeFound){
00352     std::cerr << "No suitable plugin for saving this dataType." << std::endl;
00353     return QDialog::Rejected;
00354   }
00355 
00356   slotSetSaveFilters( object->dataType() );
00357 
00358   //display correct path/name
00359   QFileInfo fi(_filename);
00360   QFile file(_filename);
00361 
00362   if (file.exists()) {
00363     setDirectory( fi.absolutePath() );
00364     selectFile ( fi.fileName() );
00365   } else {
00366 //     setDirectory(OpenFlipper::Options::currentDir().absolutePath() );
00367     std::cout << "setting filename = " << _filename.toStdString() << std::endl;
00368     setDirectory( fi.absolutePath() );
00369     selectFile ( fi.fileName() );
00370   }
00371 
00372 
00373   //try to select the best fitting name filter
00374   for (int i=0; i < nameFilters().count(); i++){
00375     int s = nameFilters()[i].indexOf("*")+2;
00376     int e = nameFilters()[i].indexOf(" ", s);
00377     int e2 = nameFilters()[i].indexOf(")", s);
00378     if (e == -1 || e2 < e) e = e2;
00379 
00380     QString ext = nameFilters()[i].mid(s,e-s);
00381 
00382     if (ext == fi.completeSuffix()){
00383       selectNameFilter(nameFilters()[i]);
00384       break;
00385     }
00386   }
00387 
00388   return this->exec();
00389 }
00390 
00391 void LoadWidget::accept() {
00392   if ( loadMode_ )
00393     loadFile();
00394   else
00395     saveFile();
00396 
00397 
00398   QFileDialog::accept();
00399 }
00400 
00401 void LoadWidget::slotSetPluginForExtension(QString _extension, int _pluginId ){
00402   pluginForExtension_[ _extension ] = _pluginId;
00403 }
00404 
00405 void LoadWidget::getPluginForExtensionINI(QStringList _extensions){
00406     
00407   QString filename = OpenFlipper::Options::configDirStr() + OpenFlipper::Options::dirSeparator() + "OpenFlipper.ini";
00408   
00409   INIFile ini;
00410   
00411   if ( ! ini.connect(filename,false) ) {
00412     std::cerr << (tr("Failed to connect to ini file %1").arg(filename)).toStdString() << std::endl;
00413     return;
00414   }
00415 
00416   for (int i=0; i < _extensions.count(); i++){
00417     QString pluginName;
00418     
00419     if ( ini.get_entry(pluginName, "LoadSave" , "Extension_" + _extensions[i] ) ){
00420       
00421       for (uint t=0; t < supportedTypes_.size(); t++)
00422         if ( supportedTypes_[t].name == pluginName ){
00423           pluginForExtension_[ _extensions[i] ] = t;
00424           break;
00425         }
00426     }
00427   }
00428 
00429   // close ini file
00430   ini.disconnect();
00431 }
00432 
00433 

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .