loadWidget.cc 10.9 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
//=============================================================================
//
//                               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 "loadWidget.hh"
#include <OpenFlipper/common/GlobalOptions.hh>
#include "OpenFlipper/BasePlugin/PluginFunctions.hh"
#include <QCompleter>
#include <QMessageBox>
#include <QFile>
#include <QFileDialog>
#include <QDir>
#include <QDebug>


 #include <QHBoxLayout>

LoadWidget::LoadWidget(std::vector<fileTypes>& _supportedTypes , QWidget *parent)
  : QFileDialog(parent),
    loadMode_(true),
    supportedTypes_(_supportedTypes)
{

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

  //supported Types
  typeBox_ = new QComboBox(this);
  typeBox_->setDuplicatesEnabled(false);
  for (int i=0; i < (int)supportedTypes_.size(); i++)
    typeBox_->addItem(supportedTypes_[i].plugin->typeName(),QVariant(supportedTypes_[i].type));

  // add the type box to the bottom
  gridLayout->addWidget( typeBox_, gridLayout->rowCount() , 1 );

  // Add a nice label for it
  QLabel* typeLabel = new QLabel("Object type:" , this);
  gridLayout->addWidget( typeLabel, gridLayout->rowCount() -1 , 0 );

  // Add a frame to the right of the dialog
  box_ = new QGroupBox( this ) ;
  box_->setFlat ( false );
  box_->setSizePolicy( QSizePolicy ( QSizePolicy::Preferred , QSizePolicy::Expanding ) );
  gridLayout->addWidget( box_, 0 , gridLayout->columnCount(),gridLayout->rowCount(),1 );

  //init layout for the frame
  boxLayout_ = new QHBoxLayout();
  box_->setLayout(boxLayout_);

  //set last used DataType as currentItem
  for (int i=0; i < typeBox_->count(); i++){
    if (typeBox_->itemText(i) == OpenFlipper::Options::lastDataType()){
      typeBox_->setCurrentIndex(i);
      if (loadMode_)
        slotSetLoadFilters(i);
      else
        slotSetSaveFilters(i);
    }
  }

Dirk Wilden's avatar
Dirk Wilden committed
91
92
  connect(this, SIGNAL(filterSelected(QString)), this, SLOT(currentFilterChanged(QString)));

Jan Möbius's avatar
 
Jan Möbius committed
93
94
95
96
97
98
  //overwrite dialog shouldn't be handled by the qfiledialog
  setConfirmOverwrite(false);

  setDirectory(OpenFlipper::Options::currentDirStr());
}

99
100
101
102
103
104
105
/// Desctructor
LoadWidget::~LoadWidget()
{
  // remove the optionWidgets so they don't get destroyed
  // (the fileplugins control the optionWidgets)
  for (int i=0; i < boxWidgets_.size(); i++){
    boxLayout_->removeWidget( boxWidgets_[i] );
106
    boxWidgets_[i]->setParent(0);
107
108
109
  }
}

Dirk Wilden's avatar
Dirk Wilden committed
110
111
112
/// adjust the loadWidget / saveWidget when the selected nameFilter changed
void LoadWidget::currentFilterChanged(QString _currentFilter){

Dirk Wilden's avatar
Dirk Wilden committed
113
114
  for (int i=0; i < boxWidgets_.size(); i++){
    boxLayout_->removeWidget( boxWidgets_[i] );
115
    boxWidgets_[i]->setParent(0);
Dirk Wilden's avatar
Dirk Wilden committed
116
117
118
  }

  boxWidgets_.clear();
Dirk Wilden's avatar
Dirk Wilden committed
119
120
121
122
123
124
125
126
127
128
129
130

  const DataType type = (DataType) typeBox_->itemData( typeBox_->currentIndex() ).toInt();

  if ( loadMode_ ){
  for (int i=0; i < (int)supportedTypes_.size(); i++)
    if (supportedTypes_[i].type == type){
      //add Widget for new Filter
      QWidget* nuWidget = 0;
      nuWidget = supportedTypes_[i].plugin->loadOptionsWidget(_currentFilter);

      if (nuWidget != 0){
        boxLayout_->addWidget( nuWidget );
Dirk Wilden's avatar
Dirk Wilden committed
131
        boxWidgets_.push_back( nuWidget );
Dirk Wilden's avatar
Dirk Wilden committed
132
133
134
135
136
137
138
139
140
        box_->show();
      }
    }
  }else{
    for (int i=0; i < (int)supportedTypes_.size(); i++)
      if (supportedTypes_[i].type == type){
        //add Widget for new Filter
        QWidget* nuWidget = 0;
        nuWidget = supportedTypes_[i].plugin->saveOptionsWidget(_currentFilter);
141

Dirk Wilden's avatar
Dirk Wilden committed
142
143
        if (nuWidget != 0){
          boxLayout_->addWidget( nuWidget );
Dirk Wilden's avatar
Dirk Wilden committed
144
          boxWidgets_.push_back( nuWidget );
Dirk Wilden's avatar
Dirk Wilden committed
145
146
147
148
149
150
          box_->show();
        }
      }
  }
}

Jan Möbius's avatar
 
Jan Möbius committed
151
152
153
154
/// adjust load-filters to current datatype
void LoadWidget::slotSetLoadFilters(int _typeIndex){
  box_->setTitle("Load Options");

Dirk Wilden's avatar
Dirk Wilden committed
155
156
  for (int i=0; i < boxWidgets_.size(); i++){
    boxLayout_->removeWidget( boxWidgets_[i] );
157
    boxWidgets_[i]->setParent(0);
Jan Möbius's avatar
 
Jan Möbius committed
158
159
  }

Dirk Wilden's avatar
Dirk Wilden committed
160
  boxWidgets_.clear();
Jan Möbius's avatar
 
Jan Möbius committed
161
162
163
164
165
166
167

  const DataType type = (DataType) typeBox_->itemData(_typeIndex).toInt();
  for (int i=0; i < (int)supportedTypes_.size(); i++)
    if (supportedTypes_[i].type == type){
      QStringList filters = supportedTypes_[i].loadFilters.split(";;");
      for (int f=0; f < filters.size(); f++)
        if (filters[f].trimmed() == "") filters.removeAt(f);
Dirk Wilden's avatar
Dirk Wilden committed
168
      setNameFilters(filters);
Jan Möbius's avatar
 
Jan Möbius committed
169
170

      //add Widget for new Filter
Dirk Wilden's avatar
Dirk Wilden committed
171
172
173
174
175
176
      QWidget* nuWidget = 0;
      nuWidget = supportedTypes_[i].plugin->loadOptionsWidget("");

      if ( nuWidget != 0 ) {
        boxLayout_->addWidget( nuWidget );
        boxWidgets_.push_back( nuWidget );
Jan Möbius's avatar
 
Jan Möbius committed
177
178
179
180
181
182
183
184
185
186
187
188
        box_->show();
      }
    }

  if (box_->children().count() == 1) //only the layout is left
    box_->hide();
}

/// adjust save-filters to current datatype
void LoadWidget::slotSetSaveFilters(int _typeIndex){
  box_->setTitle("Save Options");

Dirk Wilden's avatar
Dirk Wilden committed
189
190
  for (int i=0; i < boxWidgets_.size(); i++){
    boxLayout_->removeWidget( boxWidgets_[i] );
191
    boxWidgets_[i]->setParent(0);
Jan Möbius's avatar
 
Jan Möbius committed
192
193
  }

Dirk Wilden's avatar
Dirk Wilden committed
194
195
  boxWidgets_.clear();

Jan Möbius's avatar
 
Jan Möbius committed
196
197
198
199
200
201
  const DataType type = (DataType) typeBox_->itemData(_typeIndex).toInt();
  for (int i=0; i < (int)supportedTypes_.size(); i++)
    if (supportedTypes_[i].type == type){
      QStringList filters = supportedTypes_[i].saveFilters.split(";;");
      for (int f=0; f < filters.size(); f++)
        if (filters[f].trimmed() == "") filters.removeAt(f);
Dirk Wilden's avatar
Dirk Wilden committed
202
      setNameFilters(filters);
Jan Möbius's avatar
 
Jan Möbius committed
203
204

      //add Widget for new Filter
Dirk Wilden's avatar
Dirk Wilden committed
205
206
207
208
209
210
      QWidget* nuWidget = 0;
      nuWidget = supportedTypes_[i].plugin->saveOptionsWidget("");

      if ( nuWidget != 0 ) {
        boxLayout_->addWidget( nuWidget );
        boxWidgets_.push_back( nuWidget );
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
229
230
231
232
        box_->show();
      }
    }

  if (box_->children().count() == 1) //only the layout is left
    box_->hide();
}



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

  //get selection
  QStringList files = selectedFiles();

  bool success = false;

  //iterate over selection
  for (int i=0; i < files.size(); i++){
    QFileInfo fi(files[i]);
    QString filename = fi.absoluteFilePath();
233
    OpenFlipper::Options::currentDir(fi.absolutePath());
Jan Möbius's avatar
 
Jan Möbius committed
234
    QFile file(filename);
235

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

    //emit load signal
    DataType type = (DataType) typeBox_->itemData(typeBox_->currentIndex()).toInt();
    int unused;
    emit load(filename, type, unused);
    success = true;
  }

246
247


Jan Möbius's avatar
 
Jan Möbius committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
  if (success) //at least one item was loaded successfully
    OpenFlipper::Options::lastDataType(typeBox_->currentText());
}

/// 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
269
270
271
    int e2 = selectedFilter().indexOf(")", s);
    if (e == -1 || e2 < e) e = e2;

Jan Möbius's avatar
 
Jan Möbius committed
272
273
274
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
    QString ext = selectedFilter().mid(s,e-s);
    filename += ext;
  }

  QFile f(filename);
  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
  }
  QFileInfo fi(filename);
  QString ext = fi.suffix();

  emit save(id_,filename);
  OpenFlipper::Options::lastDataType(typeBox_->currentText());

  OpenFlipper::Options::currentDir(fi.absolutePath());

}

/// show Widget for loading Files
int LoadWidget::showLoad(){
  setAcceptMode ( QFileDialog::AcceptOpen );
  setWindowTitle("Load Object");
  typeBox_->setEnabled(true);
  loadMode_ = true;

  setFileMode(QFileDialog::ExistingFiles);
  disconnect(typeBox_, SIGNAL(activated(int)), 0, 0);
  connect(typeBox_,SIGNAL(activated(int)),this,SLOT(slotSetLoadFilters(int)));

  slotSetLoadFilters(typeBox_->currentIndex());

  return this->exec();
}

/// show Widget for saving Files
int LoadWidget::showSave(int _id, QString _filename){
  setAcceptMode ( QFileDialog::AcceptSave );
  setFileMode( QFileDialog::AnyFile );
  setWindowTitle("Save Object");
  typeBox_->setEnabled(false);
  loadMode_ = false;

  id_ = _id;

  //set dataType
  BaseObjectData* object;
Jan Möbius's avatar
   
Jan Möbius committed
321
  PluginFunctions::getObject(_id,object);
Jan Möbius's avatar
 
Jan Möbius committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337

  bool typeFound = false;
  for (int i=0; i < typeBox_->count(); i++){
    DataType type = (DataType) typeBox_->itemData(i).toInt();
    if (object->dataType(type)){
      typeBox_->setCurrentIndex(i);
      slotSetSaveFilters(i);
      typeFound = true;
    }
  }

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

Dirk Wilden's avatar
Dirk Wilden committed
338
339
340
341
342
  disconnect(typeBox_, SIGNAL(activated(int)), 0, 0);
  connect(typeBox_,SIGNAL(activated(int)),this,SLOT(slotSetSaveFilters(int)));

  slotSetSaveFilters(typeBox_->currentIndex());

Jan Möbius's avatar
 
Jan Möbius committed
343
344
345
  //display correct path/name
  QFileInfo fi(_filename);
  QFile file(_filename);
346

Jan Möbius's avatar
 
Jan Möbius committed
347
348
  if (file.exists()) {
    setDirectory( fi.absolutePath() );
Dirk Wilden's avatar
Dirk Wilden committed
349
    selectFile ( fi.fileName() );
Jan Möbius's avatar
 
Jan Möbius committed
350
351
352
353
354
355
356
  } 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
357

Dirk Wilden's avatar
Dirk Wilden committed
358
359
360
361
  //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
362
363
364
    int e2 = nameFilters()[i].indexOf(")", s);
    if (e == -1 || e2 < e) e = e2;

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

Dirk Wilden's avatar
Dirk Wilden committed
367
368
369
370
371
    if (ext == fi.completeSuffix()){
      selectNameFilter(nameFilters()[i]);
      break;
    }
  }
Jan Möbius's avatar
 
Jan Möbius committed
372
373
374
375
376
377
378
379
380
381
382
383
384

  return this->exec();
}

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


  QFileDialog::accept();
}