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
56
57
58
59
60
61
62
63




#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)
{
64
  setOption (QFileDialog::DontUseNativeDialog, true);
Jan Möbius's avatar
 
Jan Möbius committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78

  // 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
79
  QLabel* typeLabel = new QLabel(tr("Object type:") , this);
Jan Möbius's avatar
 
Jan Möbius committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  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
103
104
  connect(this, SIGNAL(filterSelected(QString)), this, SLOT(currentFilterChanged(QString)));

Jan Möbius's avatar
 
Jan Möbius committed
105
106
107
108
109
110
  //overwrite dialog shouldn't be handled by the qfiledialog
  setConfirmOverwrite(false);

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

111
112
113
114
115
116
117
/// 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] );
118
    boxWidgets_[i]->setParent(0);
119
120
121
  }
}

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

Dirk Wilden's avatar
Dirk Wilden committed
125
126
  for (int i=0; i < boxWidgets_.size(); i++){
    boxLayout_->removeWidget( boxWidgets_[i] );
127
    boxWidgets_[i]->setParent(0);
Dirk Wilden's avatar
Dirk Wilden committed
128
129
130
  }

  boxWidgets_.clear();
Dirk Wilden's avatar
Dirk Wilden committed
131
132
133
134
135
136
137
138
139
140
141
142

  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
143
        boxWidgets_.push_back( nuWidget );
Dirk Wilden's avatar
Dirk Wilden committed
144
145
146
147
148
149
150
151
152
        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);
153

Dirk Wilden's avatar
Dirk Wilden committed
154
155
        if (nuWidget != 0){
          boxLayout_->addWidget( nuWidget );
Dirk Wilden's avatar
Dirk Wilden committed
156
          boxWidgets_.push_back( nuWidget );
Dirk Wilden's avatar
Dirk Wilden committed
157
158
159
160
161
162
          box_->show();
        }
      }
  }
}

Jan Möbius's avatar
 
Jan Möbius committed
163
164
/// adjust load-filters to current datatype
void LoadWidget::slotSetLoadFilters(int _typeIndex){
165
  box_->setTitle(tr("Load Options"));
Jan Möbius's avatar
 
Jan Möbius committed
166

Dirk Wilden's avatar
Dirk Wilden committed
167
168
  for (int i=0; i < boxWidgets_.size(); i++){
    boxLayout_->removeWidget( boxWidgets_[i] );
169
    boxWidgets_[i]->setParent(0);
Jan Möbius's avatar
 
Jan Möbius committed
170
171
  }

Dirk Wilden's avatar
Dirk Wilden committed
172
  boxWidgets_.clear();
Jan Möbius's avatar
 
Jan Möbius committed
173
174
175
176
177
178
179

  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
180
      setNameFilters(filters);
Jan Möbius's avatar
 
Jan Möbius committed
181
182

      //add Widget for new Filter
Dirk Wilden's avatar
Dirk Wilden committed
183
184
185
186
187
188
      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
189
190
191
192
193
194
195
196
197
198
        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){
199
  box_->setTitle(tr("Save Options"));
Jan Möbius's avatar
 
Jan Möbius committed
200

Dirk Wilden's avatar
Dirk Wilden committed
201
202
  for (int i=0; i < boxWidgets_.size(); i++){
    boxLayout_->removeWidget( boxWidgets_[i] );
203
    boxWidgets_[i]->setParent(0);
Jan Möbius's avatar
 
Jan Möbius committed
204
205
  }

Dirk Wilden's avatar
Dirk Wilden committed
206
207
  boxWidgets_.clear();

Jan Möbius's avatar
 
Jan Möbius committed
208
209
210
211
212
213
  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
214
      setNameFilters(filters);
Jan Möbius's avatar
 
Jan Möbius committed
215
216

      //add Widget for new Filter
Dirk Wilden's avatar
Dirk Wilden committed
217
218
219
220
221
222
      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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
        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();
245
    OpenFlipper::Options::currentDir(fi.absolutePath());
Jan Möbius's avatar
 
Jan Möbius committed
246
    QFile file(filename);
247

Jan Möbius's avatar
 
Jan Möbius committed
248
249
250
251
252
253
254
255
256
257
    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;
  }

258
259


Jan Möbius's avatar
 
Jan Möbius committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  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
281
282
283
    int e2 = selectedFilter().indexOf(")", s);
    if (e == -1 || e2 < e) e = e2;

Jan Möbius's avatar
 
Jan Möbius committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
    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 );
308
  setWindowTitle(tr("Load Object"));
Jan Möbius's avatar
 
Jan Möbius committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
  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 );
325
  setWindowTitle(tr("Save Object"));
Jan Möbius's avatar
 
Jan Möbius committed
326
327
328
329
330
331
332
  typeBox_->setEnabled(false);
  loadMode_ = false;

  id_ = _id;

  //set dataType
  BaseObjectData* object;
Jan Möbius's avatar
   
Jan Möbius committed
333
  PluginFunctions::getObject(_id,object);
Jan Möbius's avatar
 
Jan Möbius committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

  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
350
351
352
353
354
  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
355
356
357
  //display correct path/name
  QFileInfo fi(_filename);
  QFile file(_filename);
358

Jan Möbius's avatar
 
Jan Möbius committed
359
360
  if (file.exists()) {
    setDirectory( fi.absolutePath() );
Dirk Wilden's avatar
Dirk Wilden committed
361
    selectFile ( fi.fileName() );
Jan Möbius's avatar
 
Jan Möbius committed
362
363
364
365
366
367
368
  } 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
369

Dirk Wilden's avatar
Dirk Wilden committed
370
371
372
373
  //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
374
375
376
    int e2 = nameFilters()[i].indexOf(")", s);
    if (e == -1 || e2 < e) e = e2;

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

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

  return this->exec();
}

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


  QFileDialog::accept();
}