loadWidget.cc 12.8 KB
Newer Older
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
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           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/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                         *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55




#include "loadWidget.hh"
#include <OpenFlipper/common/GlobalOptions.hh>
#include "OpenFlipper/BasePlugin/PluginFunctions.hh"
#include <QCompleter>
#include <QMessageBox>
#include <QFile>
#include <QFileDialog>
#include <QDir>
#include <QDebug>

Dirk Wilden's avatar
Dirk Wilden committed
56
#include <QHBoxLayout>
Jan Möbius's avatar
 
Jan Möbius committed
57

Dirk Wilden's avatar
Dirk Wilden committed
58
59
60
61
#include "FileOptionsDialog.hh"

#include <OpenFlipper/INIFile/INIFile.hh>
#include <OpenFlipper/common/GlobalOptions.hh>
Jan Möbius's avatar
 
Jan Möbius committed
62
63
64
65
66
67

LoadWidget::LoadWidget(std::vector<fileTypes>& _supportedTypes , QWidget *parent)
  : QFileDialog(parent),
    loadMode_(true),
    supportedTypes_(_supportedTypes)
{
68
  setOption (QFileDialog::DontUseNativeDialog, true);
Jan Möbius's avatar
 
Jan Möbius committed
69
70
71
72
73

  // Get our layout
  QGridLayout *gridLayout = (QGridLayout*)layout();

  //supported Types
Dirk Wilden's avatar
Dirk Wilden committed
74
  optionsBox_ = new QCheckBox(tr("use defaults"), this);
Jan Möbius's avatar
Jan Möbius committed
75
  optionsBox_->setChecked( OpenFlipperSettings().value("Core/File/UseLoadDefaults",false).toBool() );
Dirk Wilden's avatar
Dirk Wilden committed
76
  
Dirk Wilden's avatar
Dirk Wilden committed
77
78
  // add the options box to the bottom
  gridLayout->addWidget( optionsBox_, gridLayout->rowCount() , 1 );
Jan Möbius's avatar
 
Jan Möbius committed
79

Dirk Wilden's avatar
Dirk Wilden committed
80
81
  // Add a very nice label for it
  QLabel* typeLabel = new QLabel(tr("Options:") , this);
Jan Möbius's avatar
 
Jan Möbius committed
82
83
84
85
86
  gridLayout->addWidget( typeLabel, gridLayout->rowCount() -1 , 0 );

  //overwrite dialog shouldn't be handled by the qfiledialog
  setConfirmOverwrite(false);

87
  setDirectory( OpenFlipperSettings().value("Core/CurrentDir").toString() );
Jan Möbius's avatar
 
Jan Möbius committed
88
89
}

90
91
92
/// Desctructor
LoadWidget::~LoadWidget()
{
Dirk Wilden's avatar
Dirk Wilden committed
93

Dirk Wilden's avatar
Dirk Wilden committed
94
}
Dirk Wilden's avatar
Dirk Wilden committed
95

Dirk Wilden's avatar
Dirk Wilden committed
96
97
/// adjust load-filters to current datatype
void LoadWidget::slotSetLoadFilters(){
Dirk Wilden's avatar
Dirk Wilden committed
98

Dirk Wilden's avatar
Dirk Wilden committed
99
  QStringList allFilters;
Dirk Wilden's avatar
Dirk Wilden committed
100

Dirk Wilden's avatar
Dirk Wilden committed
101
102
103
104
105
106
107
  //TODO All files filter
  
  for (int i=0; i < (int)supportedTypes_.size(); i++){
    QStringList filters = supportedTypes_[i].loadFilters.split(";;");
    for (int f=filters.size()-1; f >= 0;  f--){
      if (filters[f].trimmed() == "") { filters.removeAt(f); continue; }
      if (filters[f].contains( tr("All files") ) ) filters.removeAt(f);
Dirk Wilden's avatar
Dirk Wilden committed
108
109
    }

Dirk Wilden's avatar
Dirk Wilden committed
110
    allFilters.append( filters );
Jan Möbius's avatar
 
Jan Möbius committed
111
112
  }

Dirk Wilden's avatar
Dirk Wilden committed
113
114
  allFilters.removeDuplicates();
  allFilters.sort();
Jan Möbius's avatar
 
Jan Möbius committed
115

Dirk Wilden's avatar
Dirk Wilden committed
116
117
118
119
120
121
122
  QStringList allExt;
  
  //get all possible extensions
  for (int i=0; i < allFilters.size(); i++){
     QString ext = allFilters[i].section("(",1).section(")",0,0);
     allExt.append( ext.split(" ") );
  }
Jan Möbius's avatar
 
Jan Möbius committed
123

Dirk Wilden's avatar
Dirk Wilden committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  for (int f=allExt.size()-1; f >= 0;  f--)
    if (allExt[f].trimmed() == "") allExt.removeAt(f);
  
  allExt.removeDuplicates();
  allExt.sort();
  
  QString allFiles = tr("All Files (");
  
  for (int i=0; i < allExt.size(); i++)
    allFiles += " " + allExt[i];
  
  allFiles += " )";

  allFilters.push_front(allFiles);
  
  setNameFilters(allFilters);
Jan Möbius's avatar
 
Jan Möbius committed
140
141
142
}

/// adjust save-filters to current datatype
Dirk Wilden's avatar
Dirk Wilden committed
143
void LoadWidget::slotSetSaveFilters(DataType _type){
Jan Möbius's avatar
 
Jan Möbius committed
144

Dirk Wilden's avatar
Dirk Wilden committed
145
  QStringList allFilters;
Dirk Wilden's avatar
Dirk Wilden committed
146

Jan Möbius's avatar
 
Jan Möbius committed
147
  for (int i=0; i < (int)supportedTypes_.size(); i++)
148
    if (supportedTypes_[i].type & _type){
Jan Möbius's avatar
 
Jan Möbius committed
149
      QStringList filters = supportedTypes_[i].saveFilters.split(";;");
Dirk Wilden's avatar
Dirk Wilden committed
150
151
152
      for (int f=filters.size()-1; f >= 0;  f--){
        if (filters[f].trimmed() == "") { filters.removeAt(f); continue; }
        if (filters[f].contains( tr("All files") ) ) filters.removeAt(f);
Jan Möbius's avatar
 
Jan Möbius committed
153
      }
154
      
Dirk Wilden's avatar
Dirk Wilden committed
155
      allFilters.append( filters );
Jan Möbius's avatar
 
Jan Möbius committed
156
157
    }

Dirk Wilden's avatar
Dirk Wilden committed
158
159
  allFilters.removeDuplicates();
  allFilters.sort();
Jan Möbius's avatar
 
Jan Möbius committed
160

Dirk Wilden's avatar
Dirk Wilden committed
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
  QStringList allExt;
  
  //get all possible extensions
  for (int i=0; i < allFilters.size(); i++){
     QString ext = allFilters[i].section("(",1).section(")",0,0);
     allExt.append( ext.split(" ") );
  }

  for (int f=allExt.size()-1; f >= 0;  f--)
    if (allExt[f].trimmed() == "") allExt.removeAt(f);
  
  allExt.removeDuplicates();
  allExt.sort();
  
  QString allFiles = tr("All Files (");
  
  for (int i=0; i < allExt.size(); i++)
    allFiles += " " + allExt[i];
  
  allFiles += " )";

  allFilters.push_front(allFiles);
  
  setNameFilters(allFilters);
}
Jan Möbius's avatar
 
Jan Möbius committed
186
187
188
189
190
191
192
193
194
195


/// find suitable plugin for loading current file
void LoadWidget::loadFile(){

  //get selection
  QStringList files = selectedFiles();

  bool success = false;

Dirk Wilden's avatar
Dirk Wilden committed
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
  //get all extensions
  QStringList ext;

  for (int i=0; i < files.size(); i++)
    ext.push_back( QFileInfo(files[i]).suffix() );
  
   
  //find plugins that can handle the current extensions
  pluginForExtension_.clear();
  
  for (int i=0; i < ext.size(); i++){
    for (uint t=0; t < supportedTypes_.size(); t++){
      
      QString filters = supportedTypes_[t].loadFilters;  
      
      if (filters.contains(ext[i])){
        pluginForExtension_[ ext[i] ] = t;
        break;
      }
    }
  }
  
  getPluginForExtensionINI(ext);
  
  // display options for all dataTypes
  if ( !optionsBox_->isChecked() ){

    FileOptionsDialog options(supportedTypes_,ext, loadMode_);

    connect(&options, SIGNAL(setPluginForExtension(QString,int)), this, SLOT(slotSetPluginForExtension(QString,int)) );

    if ( !options.exec() )
      return;
  }

  //load the selected files
Jan Möbius's avatar
 
Jan Möbius committed
232
  for (int i=0; i < files.size(); i++){
Dirk Wilden's avatar
Dirk Wilden committed
233
    
Jan Möbius's avatar
 
Jan Möbius committed
234
235
    QFileInfo fi(files[i]);
    QString filename = fi.absoluteFilePath();
236
    OpenFlipperSettings().setValue("Core/CurrentDir", fi.absolutePath());
Jan Möbius's avatar
 
Jan Möbius committed
237
    QFile file(filename);
238

Jan Möbius's avatar
 
Jan Möbius committed
239
240
241
242
    if (fi.isDir() || !file.exists()) continue; //do nothing if its a not a valid file
    QString ext = fi.suffix();

    //emit load signal
Dirk Wilden's avatar
Dirk Wilden committed
243
    if ( pluginForExtension_.find( fi.suffix() ) != pluginForExtension_.end() ){
244

Dirk Wilden's avatar
Dirk Wilden committed
245
246
247
248
      emit load(filename, pluginForExtension_[ fi.suffix() ]);
      success = true;
    }
  }
Jan Möbius's avatar
 
Jan Möbius committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
}

/// find suitable plugin for saving current file
void LoadWidget::saveFile(){
  //get selection
  QStringList files = selectedFiles();

  if ( files.size() != 1 ) {
    std::cerr << "Error no or multiple save files selected" << std::endl;
    return;
  }

  QString filename = files[0];

  //get filename
  if (!filename.contains(".",Qt::CaseSensitive)){ //check for extension

    int s = selectedFilter().indexOf("*")+1;
    int e = selectedFilter().indexOf(" ", s);
Dirk Wilden's avatar
Dirk Wilden committed
268
269
270
    int e2 = selectedFilter().indexOf(")", s);
    if (e == -1 || e2 < e) e = e2;

Jan Möbius's avatar
 
Jan Möbius committed
271
272
273
274
    QString ext = selectedFilter().mid(s,e-s);
    filename += ext;
  }

Dirk Wilden's avatar
Dirk Wilden committed
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
  QFile      f(filename);
  QFileInfo fi(filename);
   
  //find plugin that can handle the current extension
  pluginForExtension_.clear();
  
  for (uint t=0; t < supportedTypes_.size(); t++){

    QString filters = supportedTypes_[t].loadFilters;  

    if (filters.contains( fi.suffix() )){
      pluginForExtension_[ fi.suffix() ] = t;
      break;
    }
  }
  
  getPluginForExtensionINI(QStringList(fi.suffix()));
  
  // display options for all dataTypes
  if ( !optionsBox_->isChecked() ){

    FileOptionsDialog options(supportedTypes_,QStringList(fi.suffix()), loadMode_);

    connect(&options, SIGNAL(setPluginForExtension(QString,int)), this, SLOT(slotSetPluginForExtension(QString,int)) );

    if ( !options.exec() )
      return;
  }

Jan Möbius's avatar
 
Jan Möbius committed
304
305
306
307
308
309
310
  if (f.exists()){ //check for extension
    int ret = QMessageBox::warning(this, tr("File exists"),tr("This file already exists.\n"
        "Do you want to overwrite the file?"),QMessageBox::Yes|QMessageBox::No,QMessageBox::No);
    if (ret == QMessageBox::No)
      return; //abort if users doesn't want to overwrite
  }

Dirk Wilden's avatar
Dirk Wilden committed
311
312
  if ( pluginForExtension_.find( fi.suffix() ) != pluginForExtension_.end() )
    emit save(id_,filename, pluginForExtension_[fi.suffix()] );
Jan Möbius's avatar
 
Jan Möbius committed
313

314
  OpenFlipperSettings().setValue("Core/CurrentDir", fi.absolutePath() );
Jan Möbius's avatar
 
Jan Möbius committed
315
316
317
318
319
}

/// show Widget for loading Files
int LoadWidget::showLoad(){
  setAcceptMode ( QFileDialog::AcceptOpen );
320
  setWindowTitle(tr("Load Object"));
Jan Möbius's avatar
 
Jan Möbius committed
321
322
323
324
  loadMode_ = true;

  setFileMode(QFileDialog::ExistingFiles);

Dirk Wilden's avatar
Dirk Wilden committed
325
  slotSetLoadFilters();
Jan Möbius's avatar
 
Jan Möbius committed
326
327
328
329
330
331
332
333

  return this->exec();
}

/// show Widget for saving Files
int LoadWidget::showSave(int _id, QString _filename){
  setAcceptMode ( QFileDialog::AcceptSave );
  setFileMode( QFileDialog::AnyFile );
334
  setWindowTitle(tr("Save Object"));
Jan Möbius's avatar
 
Jan Möbius committed
335
336
337
338
339
340
  loadMode_ = false;

  id_ = _id;

  //set dataType
  BaseObjectData* object;
Jan Möbius's avatar
   
Jan Möbius committed
341
  PluginFunctions::getObject(_id,object);
Jan Möbius's avatar
 
Jan Möbius committed
342

Dirk Wilden's avatar
Dirk Wilden committed
343
  //check if we can save this dataType
Jan Möbius's avatar
 
Jan Möbius committed
344
  bool typeFound = false;
Dirk Wilden's avatar
Dirk Wilden committed
345
346
347
  
  for (int i=0; i < (int)supportedTypes_.size(); i++)
    if ( object->dataType( supportedTypes_[i].type ) )
Jan Möbius's avatar
 
Jan Möbius committed
348
      typeFound = true;
Dirk Wilden's avatar
Dirk Wilden committed
349

Jan Möbius's avatar
 
Jan Möbius committed
350
351
352
353
354
355

  if (!typeFound){
    std::cerr << "No suitable plugin for saving this dataType." << std::endl;
    return QDialog::Rejected;
  }

Dirk Wilden's avatar
Dirk Wilden committed
356
  slotSetSaveFilters( object->dataType() );
Dirk Wilden's avatar
Dirk Wilden committed
357

Jan Möbius's avatar
 
Jan Möbius committed
358
359
360
  //display correct path/name
  QFileInfo fi(_filename);
  QFile file(_filename);
361

Jan Möbius's avatar
 
Jan Möbius committed
362
363
  if (file.exists()) {
    setDirectory( fi.absolutePath() );
Dirk Wilden's avatar
Dirk Wilden committed
364
    selectFile ( fi.fileName() );
Jan Möbius's avatar
 
Jan Möbius committed
365
366
367
368
369
370
371
  } else {
//     setDirectory(OpenFlipper::Options::currentDir().absolutePath() );
    std::cout << "setting filename = " << _filename.toStdString() << std::endl;
    setDirectory( fi.absolutePath() );
    selectFile ( fi.fileName() );
  }

Dirk Wilden's avatar
Dirk Wilden committed
372

Dirk Wilden's avatar
Dirk Wilden committed
373
374
375
376
  //try to select the best fitting name filter
  for (int i=0; i < nameFilters().count(); i++){
    int s = nameFilters()[i].indexOf("*")+2;
    int e = nameFilters()[i].indexOf(" ", s);
Dirk Wilden's avatar
Dirk Wilden committed
377
378
379
    int e2 = nameFilters()[i].indexOf(")", s);
    if (e == -1 || e2 < e) e = e2;

Dirk Wilden's avatar
Dirk Wilden committed
380
    QString ext = nameFilters()[i].mid(s,e-s);
Jan Möbius's avatar
 
Jan Möbius committed
381

Dirk Wilden's avatar
Dirk Wilden committed
382
383
384
385
386
    if (ext == fi.completeSuffix()){
      selectNameFilter(nameFilters()[i]);
      break;
    }
  }
Jan Möbius's avatar
 
Jan Möbius committed
387
388
389
390
391
392
393
394
395
396
397
398
399

  return this->exec();
}

void LoadWidget::accept() {
  if ( loadMode_ )
    loadFile();
  else
    saveFile();


  QFileDialog::accept();
}
Dirk Wilden's avatar
Dirk Wilden committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

void LoadWidget::slotSetPluginForExtension(QString _extension, int _pluginId ){
  pluginForExtension_[ _extension ] = _pluginId;
}

void LoadWidget::getPluginForExtensionINI(QStringList _extensions){
    
  QString filename = OpenFlipper::Options::configDirStr() + OpenFlipper::Options::dirSeparator() + "OpenFlipper.ini";
  
  INIFile ini;
  
  if ( ! ini.connect(filename,false) ) {
    std::cerr << (tr("Failed to connect to ini file %1").arg(filename)).toStdString() << std::endl;
    return;
  }

  for (int i=0; i < _extensions.count(); i++){
    QString pluginName;
    
Dirk Wilden's avatar
Dirk Wilden committed
419
    if ( ini.get_entry(pluginName, "LoadSave" , "Extension_" + _extensions[i] ) ){
Dirk Wilden's avatar
Dirk Wilden committed
420
421
422
423
424
425
426
427
      
      for (uint t=0; t < supportedTypes_.size(); t++)
        if ( supportedTypes_[t].name == pluginName ){
          pluginForExtension_[ _extensions[i] ] = t;
          break;
        }
    }
  }
Dirk Wilden's avatar
Dirk Wilden committed
428

Dirk Wilden's avatar
Dirk Wilden committed
429
430
431
432
433
  // close ini file
  ini.disconnect();
}