loadWidget.cc 14.3 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
  //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++){
213

Dirk Wilden's avatar
Dirk Wilden committed
214
215
216
    for (uint t=0; t < supportedTypes_.size(); t++){
      
      QString filters = supportedTypes_[t].loadFilters;  
217

218
      if (filters.contains(ext[i],Qt::CaseInsensitive)){
Dirk Wilden's avatar
Dirk Wilden committed
219
220
221
222
223
        pluginForExtension_[ ext[i] ] = t;
        break;
      }
    }
  }
Jan Möbius's avatar
Jan Möbius committed
224

Dirk Wilden's avatar
Dirk Wilden committed
225
226
227
228
229
230
231
232
233
234
235
236
  // 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
237
  for (int i=0; i < files.size(); i++){
Dirk Wilden's avatar
Dirk Wilden committed
238
    
Jan Möbius's avatar
 
Jan Möbius committed
239
240
    QFileInfo fi(files[i]);
    QString filename = fi.absoluteFilePath();
241
    OpenFlipperSettings().setValue("Core/CurrentDir", fi.absolutePath());
Jan Möbius's avatar
 
Jan Möbius committed
242
    QFile file(filename);
243

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

247

Jan Möbius's avatar
 
Jan Möbius committed
248
    //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
    if (filters.contains( fi.suffix(),Qt::CaseInsensitive )){
Dirk Wilden's avatar
Dirk Wilden committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
      pluginForExtension_[ fi.suffix() ] = t;
      break;
    }
  }
  
  // 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
308
309
310
311
312
313
314
  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
  }

315
316
317
318
319
320
  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
321

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

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

  setFileMode(QFileDialog::ExistingFiles);

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

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

  return this->exec();
}

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

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

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

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

Jan Möbius's avatar
 
Jan Möbius committed
362
363
364
365
366
367

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

Dirk Wilden's avatar
Dirk Wilden committed
368
  slotSetSaveFilters( object->dataType() );
Dirk Wilden's avatar
Dirk Wilden committed
369

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

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

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

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

  return this->exec();
}

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
/// 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
433
434
  setDirectory(OpenFlipperSettings().value("Core/CurrentDir").toString() );
  selectFile ( fi.fileName() );
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453

  //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();
}

454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
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
475

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

485
486
    QFileDialog::accept();
  }
Jan Möbius's avatar
 
Jan Möbius committed
487
}
Dirk Wilden's avatar
Dirk Wilden committed
488
489
490
491
492
493

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