53 #include "loadWidget.hh" 55 #include "FileOptionsDialog.hh" 59 LoadWidget::LoadWidget(std::vector<fileTypes>& _supportedTypes , QWidget *parent)
60 : QFileDialog(parent),
62 supportedTypes_(_supportedTypes)
64 setOption (QFileDialog::DontUseNativeDialog,
true);
67 QGridLayout *gridLayout = (QGridLayout*)layout();
70 optionsBox_ =
new QCheckBox(tr(
"use defaults"),
this);
71 optionsBox_->setChecked(
OpenFlipperSettings().value(
"Core/File/UseLoadDefaults",
false).toBool() );
74 gridLayout->addWidget( optionsBox_, gridLayout->rowCount() , 1 );
77 QLabel* typeLabel =
new QLabel(tr(
"Options:") ,
this);
78 gridLayout->addWidget( typeLabel, gridLayout->rowCount() -1 , 0 );
81 setConfirmOverwrite(
false);
95 QStringList allFilters;
99 for (
int i=0; i < (int)supportedTypes_.size(); i++){
100 QStringList filters = supportedTypes_[i].loadFilters.split(
";;");
101 for (
int f=filters.size()-1; f >= 0; f--){
102 if (filters[f].trimmed() ==
"") { filters.removeAt(f);
continue; }
103 if (filters[f].contains( tr(
"All files") ) ) filters.removeAt(f);
106 allFilters.append( filters );
109 allFilters.removeDuplicates();
115 for (
int i=0; i < allFilters.size(); i++){
116 QString ext = allFilters[i].section(
"(",1).section(
")",0,0);
117 allExt.append( ext.split(
" ") );
120 for (
int f=allExt.size()-1; f >= 0; f--)
121 if (allExt[f].trimmed() ==
"") allExt.removeAt(f);
123 allExt.removeDuplicates();
126 QString allFiles = tr(
"All Files (");
128 for (
int i=0; i < allExt.size(); i++)
129 allFiles +=
" " + allExt[i];
133 allFilters.push_front(allFiles);
135 setNameFilters(allFilters);
141 QStringList allFilters;
143 bool multipleFiles = (ids_.size() > 1);
145 for (
int i=0; i < (int)supportedTypes_.size(); i++)
146 if ( supportedTypes_[i].type.contains(_type) ){
148 if ( multipleFiles && !supportedTypes_[i].saveMultipleObjects)
151 QStringList filters = supportedTypes_[i].saveFilters.split(
";;");
152 for (
int f=filters.size()-1; f >= 0; f--){
153 if (filters[f].trimmed() ==
"") { filters.removeAt(f);
continue; }
154 if (filters[f].contains( tr(
"All files") ) ) filters.removeAt(f);
157 allFilters.append( filters );
160 allFilters.removeDuplicates();
166 for (
int i=0; i < allFilters.size(); i++){
167 QString ext = allFilters[i].section(
"(",1).section(
")",0,0);
168 allExt.append( ext.split(
" ") );
171 for (
int f=allExt.size()-1; f >= 0; f--)
172 if (allExt[f].trimmed() ==
"") allExt.removeAt(f);
174 allExt.removeDuplicates();
177 QString allFiles = tr(
"All Files (");
179 for (
int i=0; i < allExt.size(); i++)
180 allFiles +=
" " + allExt[i];
184 allFilters.push_front(allFiles);
186 setNameFilters(allFilters);
194 QStringList files = selectedFiles();
199 for (
int i=0; i < files.size(); i++)
200 ext.push_back( QFileInfo(files[i]).suffix() );
204 pluginForExtension_.clear();
206 for (
int i=0; i < ext.size(); i++){
208 for (uint t=0; t < supportedTypes_.size(); t++){
210 QString filters = supportedTypes_[t].loadFilters;
213 filters = filters.section(
"(",1).section(
")",0,0);
215 QStringList separateFilters = filters.split(
" ");
219 for (
int filterId = 0 ; filterId < separateFilters.size(); ++filterId ) {
221 if (separateFilters[filterId].endsWith(ext[i],Qt::CaseInsensitive)){
222 pluginForExtension_[ ext[i] ] = t;
235 if ( !optionsBox_->isChecked() ){
239 connect(&options, SIGNAL(setPluginForExtension(QString,
int)),
this, SLOT(slotSetPluginForExtension(QString,
int)) );
241 if ( !options.exec() )
247 QStringList loadableFiles;
249 for (
int i=0; i < files.size(); i++){
251 QFileInfo fi(files[i]);
252 QString filename = fi.absoluteFilePath();
254 QFile file(filename);
256 if (fi.isDir() || !file.exists())
continue;
257 QString ext = fi.suffix();
260 if (optionsBox_->isChecked()) {
266 for (j = 0; j < supportedTypes_.size(); ++j) {
267 if (supportedTypes_[j].name == pluginName) {
274 pluginForExtension_[ ext ] = j;
278 if ( pluginForExtension_.find( ext ) != pluginForExtension_.end() ){
279 emit load(filename, pluginForExtension_[ ext ]);
280 loadableFiles.push_back(filename);
281 pluginIds.push_back(pluginForExtension_[ext]);
284 emit loadFiles(loadableFiles, pluginIds);
290 QStringList files = selectedFiles();
292 if ( files.size() != 1 ) {
293 std::cerr <<
"Error: zero or multiple save files selected!" << std::endl;
297 QString filename = files[0];
299 if (QFileInfo(filename).completeSuffix().isEmpty()) {
301 int s = selectedNameFilter().indexOf(
"*")+1;
302 int e = selectedNameFilter().indexOf(
" ", s);
303 int e2 = selectedNameFilter().indexOf(
")", s);
304 if (e == -1 || e2 < e) e = e2;
306 QString ext = selectedNameFilter().mid(s,e-s);
311 QFileInfo fi(filename);
314 pluginForExtension_.clear();
316 for (uint t=0; t < supportedTypes_.size(); t++){
318 QString filters = supportedTypes_[t].saveFilters;
321 filters = filters.section(
"(",1).section(
")",0,0);
323 QStringList separateFilters = filters.split(
" ");
327 for (
int filterId = 0 ; filterId < separateFilters.size(); ++filterId ) {
329 if (separateFilters[filterId].endsWith(fi.suffix(),Qt::CaseInsensitive)){
330 pluginForExtension_[ fi.suffix() ] = t;
342 if ( !optionsBox_->isChecked() ){
346 connect(&options, SIGNAL(setPluginForExtension(QString,
int)),
this, SLOT(slotSetPluginForExtension(QString,
int)) );
348 if ( !options.exec() )
353 int ret = QMessageBox::warning(
this, tr(
"File exists"),tr(
"This file already exists.\n" 354 "Do you want to overwrite the file?"),QMessageBox::Yes|QMessageBox::No,QMessageBox::No);
355 if (ret == QMessageBox::No)
359 const std::map< QString, int >::iterator saving_plugin =
360 pluginForExtension_.find( fi.suffix() );
361 if ( saving_plugin != pluginForExtension_.end() ){
362 if (ids_.size() == 1)
363 emit save(ids_[0],filename, saving_plugin->second);
365 emit save(ids_ ,filename, saving_plugin->second);
373 setAcceptMode ( QFileDialog::AcceptOpen );
374 setWindowTitle(tr(
"Load Object"));
377 setFileMode(QFileDialog::ExistingFiles);
382 slotSetLoadFilters();
389 setAcceptMode ( QFileDialog::AcceptSave );
390 setFileMode( QFileDialog::AnyFile );
391 setWindowTitle(tr(
"Save Object"));
402 bool typeFound =
false;
404 for (
int i=0; i < (int)supportedTypes_.size(); i++)
405 if ( object->
dataType( supportedTypes_[i].type ) )
410 std::cerr <<
"No suitable plugin for saving this dataType." << std::endl;
411 return QDialog::Rejected;
414 slotSetSaveFilters( object->
dataType() );
417 QFileInfo fi(_filename);
418 QFile file(_filename);
421 selectFile ( fi.fileName() );
424 for (
int i=0; i < nameFilters().count(); i++){
425 int s = nameFilters()[i].indexOf(
"*")+2;
426 int e = nameFilters()[i].indexOf(
" ", s);
427 int e2 = nameFilters()[i].indexOf(
")", s);
428 if (e == -1 || e2 < e) e = e2;
430 QString ext = nameFilters()[i].mid(s,e-s);
432 if (ext == fi.completeSuffix()){
433 selectNameFilter(nameFilters()[i]);
443 setAcceptMode ( QFileDialog::AcceptSave );
444 setFileMode( QFileDialog::AnyFile );
445 setWindowTitle(tr(
"Save Objects"));
452 for (uint i=0; i < _ids.size(); i++){
457 types |=
object->dataType();
461 bool typeFound =
false;
463 for (
int i=0; i < (int)supportedTypes_.size(); i++)
464 if ( supportedTypes_[i].type.contains(types) )
469 std::cerr <<
"No suitable plugin for saving this dataType." << std::endl;
470 return QDialog::Rejected;
473 slotSetSaveFilters( types );
476 QFileInfo fi(_filename);
477 QFile file(_filename);
480 selectFile ( fi.fileName() );
483 for (
int i=0; i < nameFilters().count(); i++){
484 int s = nameFilters()[i].indexOf(
"*")+2;
485 int e = nameFilters()[i].indexOf(
" ", s);
486 int e2 = nameFilters()[i].indexOf(
")", s);
487 if (e == -1 || e2 < e) e = e2;
489 QString ext = nameFilters()[i].mid(s,e-s);
491 if (ext == fi.completeSuffix()){
492 selectNameFilter(nameFilters()[i]);
500 bool LoadWidget::validFilename() {
506 QString firstEntered = selectedFiles()[0];
509 QDir testdir(firstEntered);
510 if(testdir.exists()) {
511 setDirectory(testdir);
516 QFile file(firstEntered);
517 if(!file.exists())
return false;
524 QFileInfo firstEntered = selectedFiles()[0];
526 return QDir(firstEntered.dir()).exists();
529 void LoadWidget::accept() {
531 if( (!loadMode_) || validFilename() ) {
542 QFileDialog::accept();
546 void LoadWidget::slotSetPluginForExtension(QString _extension,
int _pluginId ){
547 pluginForExtension_[ _extension ] = _pluginId;
int showLoad()
show Widget for loading Files
void slotSetSaveFilters(DataType _type)
adjust save-filters to current datatype
int showSave(int _id, QString _filename)
show Widget for saving Files
bool getObject(int _identifier, BSplineCurveObject *&_object)
bool dataType(DataType _type) const
void slotSetLoadFilters()
adjust load-filters to current datatype
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.
void setValue(const QString &key, const QVariant &value)
Wrapper function which makes it possible to enable Debugging output with -DOPENFLIPPER_SETTINGS_DEBUG...
void loadFile()
find suitable plugin for loading current file
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
std::vector< int > IdList
Standard Type for id Lists used for scripting.
void saveFile()
find suitable plugin for saving current file
bool dirExists()
returns true, if the directory of the specified filename (user input via widget) exists ...