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

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
38
39
40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
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
/// Destructor
91
92
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

147
148
  bool multipleFiles = (ids_.size() > 1);
  
Jan Möbius's avatar
 
Jan Möbius committed
149
  for (int i=0; i < (int)supportedTypes_.size(); i++)
150
151
152
153
154
     if ( supportedTypes_[i].type.contains(_type) ){
       
       if ( multipleFiles && !supportedTypes_[i].saveMultipleObjects)
         continue;

Jan Möbius's avatar
 
Jan Möbius committed
155
      QStringList filters = supportedTypes_[i].saveFilters.split(";;");
Dirk Wilden's avatar
Dirk Wilden committed
156
157
158
      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
159
      }
160
      
Dirk Wilden's avatar
Dirk Wilden committed
161
      allFilters.append( filters );
Jan Möbius's avatar
 
Jan Möbius committed
162
163
    }

Dirk Wilden's avatar
Dirk Wilden committed
164
165
  allFilters.removeDuplicates();
  allFilters.sort();
Jan Möbius's avatar
 
Jan Möbius committed
166

Dirk Wilden's avatar
Dirk Wilden committed
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
  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
192
193
194
195


/// find suitable plugin for loading current file
void LoadWidget::loadFile(){
196
  
Jan Möbius's avatar
 
Jan Möbius committed
197
198
199
200
201
  //get selection
  QStringList files = selectedFiles();

  bool success = false;

Dirk Wilden's avatar
Dirk Wilden committed
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
  //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
238
  for (int i=0; i < files.size(); i++){
Dirk Wilden's avatar
Dirk Wilden committed
239
    
Jan Möbius's avatar
 
Jan Möbius committed
240
241
    QFileInfo fi(files[i]);
    QString filename = fi.absoluteFilePath();
242
    OpenFlipperSettings().setValue("Core/CurrentDir", fi.absolutePath());
Jan Möbius's avatar
 
Jan Möbius committed
243
    QFile file(filename);
244

Jan Möbius's avatar
 
Jan Möbius committed
245
246
247
248
    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
249
    if ( pluginForExtension_.find( fi.suffix() ) != pluginForExtension_.end() ){
250

Dirk Wilden's avatar
Dirk Wilden committed
251
252
253
254
      emit load(filename, pluginForExtension_[ fi.suffix() ]);
      success = true;
    }
  }
Jan Möbius's avatar
 
Jan Möbius committed
255
256
257
258
259
260
261
262
}

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

  if ( files.size() != 1 ) {
Marlin Frickenschmidt's avatar
Marlin Frickenschmidt committed
263
    std::cerr << "Error: zero or multiple save files selected!" << std::endl;
Jan Möbius's avatar
 
Jan Möbius committed
264
265
266
267
268
269
270
271
272
273
    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
274
275
276
    int e2 = selectedFilter().indexOf(")", s);
    if (e == -1 || e2 < e) e = e2;

Jan Möbius's avatar
 
Jan Möbius committed
277
278
279
280
    QString ext = selectedFilter().mid(s,e-s);
    filename += ext;
  }

Dirk Wilden's avatar
Dirk Wilden committed
281
282
283
284
285
286
287
288
  QFile      f(filename);
  QFileInfo fi(filename);
   
  //find plugin that can handle the current extension
  pluginForExtension_.clear();
  
  for (uint t=0; t < supportedTypes_.size(); t++){

289
    QString filters = supportedTypes_[t].saveFilters;  
Dirk Wilden's avatar
Dirk Wilden committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

    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
310
311
312
313
314
315
316
  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
  }

317
318
319
320
321
322
  if ( pluginForExtension_.find( fi.suffix() ) != pluginForExtension_.end() ){
    if (ids_.size() == 1)
      emit save(ids_[0],filename, pluginForExtension_[fi.suffix()] );
    else
      emit save(ids_   ,filename, pluginForExtension_[fi.suffix()] );
  }
Jan Möbius's avatar
 
Jan Möbius committed
323

324
  OpenFlipperSettings().setValue("Core/CurrentDir", fi.absolutePath() );
Jan Möbius's avatar
 
Jan Möbius committed
325
326
327
328
329
}

/// show Widget for loading Files
int LoadWidget::showLoad(){
  setAcceptMode ( QFileDialog::AcceptOpen );
330
  setWindowTitle(tr("Load Object"));
Jan Möbius's avatar
 
Jan Möbius committed
331
332
333
334
  loadMode_ = true;

  setFileMode(QFileDialog::ExistingFiles);

335
336
337
  // Set directory to last Directory used for Opening Files
  setDirectory(OpenFlipperSettings().value("Core/CurrentDir").toString() );

Dirk Wilden's avatar
Dirk Wilden committed
338
  slotSetLoadFilters();
Jan Möbius's avatar
 
Jan Möbius committed
339
340
341
342
343
344
345
346

  return this->exec();
}

/// show Widget for saving Files
int LoadWidget::showSave(int _id, QString _filename){
  setAcceptMode ( QFileDialog::AcceptSave );
  setFileMode( QFileDialog::AnyFile );
347
  setWindowTitle(tr("Save Object"));
Jan Möbius's avatar
 
Jan Möbius committed
348
349
  loadMode_ = false;

350
351
  ids_.clear();
  ids_.push_back(_id);
Jan Möbius's avatar
 
Jan Möbius committed
352
353
354

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

Dirk Wilden's avatar
Dirk Wilden committed
357
  //check if we can save this dataType
Jan Möbius's avatar
 
Jan Möbius committed
358
  bool typeFound = false;
Dirk Wilden's avatar
Dirk Wilden committed
359
360
361
  
  for (int i=0; i < (int)supportedTypes_.size(); i++)
    if ( object->dataType( supportedTypes_[i].type ) )
Jan Möbius's avatar
 
Jan Möbius committed
362
      typeFound = true;
Dirk Wilden's avatar
Dirk Wilden committed
363

Jan Möbius's avatar
 
Jan Möbius committed
364
365
366
367
368
369

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

Dirk Wilden's avatar
Dirk Wilden committed
370
  slotSetSaveFilters( object->dataType() );
Dirk Wilden's avatar
Dirk Wilden committed
371

Jan Möbius's avatar
 
Jan Möbius committed
372
373
374
  //display correct path/name
  QFileInfo fi(_filename);
  QFile file(_filename);
Marlin Frickenschmidt's avatar
Marlin Frickenschmidt committed
375
376
377
  
  setDirectory(OpenFlipperSettings().value("Core/CurrentDir").toString() );
  selectFile ( fi.fileName() );
Dirk Wilden's avatar
Dirk Wilden committed
378

Dirk Wilden's avatar
Dirk Wilden committed
379
380
381
382
  //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
383
384
385
    int e2 = nameFilters()[i].indexOf(")", s);
    if (e == -1 || e2 < e) e = e2;

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

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

  return this->exec();
}

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
/// show Widget for saving Files
int LoadWidget::showSave(IdList _ids, QString _filename){
  setAcceptMode ( QFileDialog::AcceptSave );
  setFileMode( QFileDialog::AnyFile );
  setWindowTitle(tr("Save Objects"));
  loadMode_ = false;

  ids_ = _ids;

  DataType types = 0;
  
  for (uint i=0; i < _ids.size(); i++){
    
    BaseObjectData* object;
    PluginFunctions::getObject(_ids[i], object);
    
    types |= object->dataType();
  }
  
  //check if we can save this dataType
  bool typeFound = false;
  
  for (int i=0; i < (int)supportedTypes_.size(); i++)
    if ( supportedTypes_[i].type.contains(types) )
      typeFound = true;


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

  slotSetSaveFilters( types );

  //display correct path/name
  QFileInfo fi(_filename);
  QFile file(_filename);

Marlin Frickenschmidt's avatar
Marlin Frickenschmidt committed
435
436
  setDirectory(OpenFlipperSettings().value("Core/CurrentDir").toString() );
  selectFile ( fi.fileName() );
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455

  //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);
    int e2 = nameFilters()[i].indexOf(")", s);
    if (e == -1 || e2 < e) e = e2;

    QString ext = nameFilters()[i].mid(s,e-s);

    if (ext == fi.completeSuffix()){
      selectNameFilter(nameFilters()[i]);
      break;
    }
  }

  return this->exec();
}

456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
bool LoadWidget::validFilename() {
    
  // Only proceed if selected file is REALLY a file
  // Consider two cases:
  // Case 1: Filename is neither a valid file nor a directory -> continue and wait for valid file
  // Case 2: Entered filename is a directory -> Change to directory and wait for valid file
  QString firstEntered = selectedFiles()[0];
  
  // Test if directory exists
  QDir testdir(firstEntered);
  if(testdir.exists()) {
      setDirectory(testdir);
      return false;
  }
  
  // Test if file exists
  QFile file(firstEntered);
  if(!file.exists()) return false;
  
  return true;
}
Jan Möbius's avatar
 
Jan Möbius committed
477

478
479
void LoadWidget::accept() {
    
480
  if( !loadMode_ | validFilename() ) {
481
482
483
484
485
    
    if ( loadMode_ )
      loadFile();
    else
      saveFile();
Jan Möbius's avatar
 
Jan Möbius committed
486

487
488
    QFileDialog::accept();
  }
Jan Möbius's avatar
 
Jan Möbius committed
489
}
Dirk Wilden's avatar
Dirk Wilden committed
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508

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
509
    if ( ini.get_entry(pluginName, "LoadSave" , "Extension_" + _extensions[i] ) ){
Dirk Wilden's avatar
Dirk Wilden committed
510
511
512
513
514
515
516
517
      
      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
518

Dirk Wilden's avatar
Dirk Wilden committed
519
520
521
522
523
  // close ini file
  ini.disconnect();
}