47 #include "loadWidget.hh" 49 #include "FileOptionsDialog.hh" 53 LoadWidget::LoadWidget(std::vector<fileTypes>& _supportedTypes , QWidget *parent)
54 : QFileDialog(parent),
56 supportedTypes_(_supportedTypes)
58 setOption (QFileDialog::DontUseNativeDialog,
true);
61 QGridLayout *gridLayout = (QGridLayout*)layout();
64 optionsBox_ =
new QCheckBox(tr(
"use defaults"),
this);
65 optionsBox_->setChecked(
OpenFlipperSettings().value(
"Core/File/UseLoadDefaults",
false).toBool() );
68 gridLayout->addWidget( optionsBox_, gridLayout->rowCount() , 1 );
71 QLabel* typeLabel =
new QLabel(tr(
"Options:") ,
this);
72 gridLayout->addWidget( typeLabel, gridLayout->rowCount() -1 , 0 );
75 setOption(DontConfirmOverwrite,
true);
89 QStringList allFilters;
93 for (
int i=0; i < (int)supportedTypes_.size(); i++){
94 QStringList filters = supportedTypes_[i].loadFilters.split(
";;");
95 for (
int f=filters.size()-1; f >= 0; f--){
96 if (filters[f].trimmed() ==
"") { filters.removeAt(f);
continue; }
97 if (filters[f].contains( tr(
"All files") ) ) filters.removeAt(f);
100 allFilters.append( filters );
103 allFilters.removeDuplicates();
109 for (
int i=0; i < allFilters.size(); i++){
110 QString ext = allFilters[i].section(
"(",1).section(
")",0,0);
111 allExt.append( ext.split(
" ") );
114 for (
int f=allExt.size()-1; f >= 0; f--)
115 if (allExt[f].trimmed() ==
"") allExt.removeAt(f);
117 allExt.removeDuplicates();
120 QString allFiles = tr(
"All Files (");
122 for (
int i=0; i < allExt.size(); i++)
123 allFiles +=
" " + allExt[i];
127 allFilters.push_front(allFiles);
129 setNameFilters(allFilters);
135 QStringList allFilters;
137 bool multipleFiles = (ids_.size() > 1);
139 for (
int i=0; i < (int)supportedTypes_.size(); i++)
140 if ( supportedTypes_[i].type.contains(_type) ){
142 if ( multipleFiles && !supportedTypes_[i].saveMultipleObjects)
145 QStringList filters = supportedTypes_[i].saveFilters.split(
";;");
146 for (
int f=filters.size()-1; f >= 0; f--){
147 if (filters[f].trimmed() ==
"") { filters.removeAt(f);
continue; }
148 if (filters[f].contains( tr(
"All files") ) ) filters.removeAt(f);
151 allFilters.append( filters );
154 allFilters.removeDuplicates();
160 for (
int i=0; i < allFilters.size(); i++){
161 QString ext = allFilters[i].section(
"(",1).section(
")",0,0);
162 allExt.append( ext.split(
" ") );
165 for (
int f=allExt.size()-1; f >= 0; f--)
166 if (allExt[f].trimmed() ==
"") allExt.removeAt(f);
168 allExt.removeDuplicates();
171 QString allFiles = tr(
"All Files (");
173 for (
int i=0; i < allExt.size(); i++)
174 allFiles +=
" " + allExt[i];
178 allFilters.push_front(allFiles);
180 setNameFilters(allFilters);
188 QStringList files = selectedFiles();
193 for (
int i=0; i < files.size(); i++)
194 ext.push_back( QFileInfo(files[i]).suffix() );
198 pluginForExtension_.clear();
200 for (
int i=0; i < ext.size(); i++){
202 for (uint t=0; t < supportedTypes_.size(); t++){
204 QString filters = supportedTypes_[t].loadFilters;
207 filters = filters.section(
"(",1).section(
")",0,0);
209 QStringList separateFilters = filters.split(
" ");
213 for (
int filterId = 0 ; filterId < separateFilters.size(); ++filterId ) {
215 if (separateFilters[filterId].endsWith(
"*." + ext[i],Qt::CaseInsensitive)){
216 pluginForExtension_[ ext[i] ] = t;
229 if ( !optionsBox_->isChecked() ){
233 connect(&options, SIGNAL(setPluginForExtension(QString,
int)),
this, SLOT(slotSetPluginForExtension(QString,
int)) );
235 if ( !options.exec() )
241 QStringList loadableFiles;
243 for (
int i=0; i < files.size(); i++){
245 QFileInfo fi(files[i]);
246 QString filename = fi.absoluteFilePath();
248 QFile file(filename);
250 if (fi.isDir() || !file.exists())
continue;
251 QString ext1 = fi.suffix();
254 if (optionsBox_->isChecked()) {
260 for (j = 0; j < supportedTypes_.size(); ++j) {
261 if (supportedTypes_[j].name == pluginName) {
268 pluginForExtension_[ ext1 ] = j;
274 if ( pluginForExtension_.find( ext1 ) != pluginForExtension_.end() ){
275 emit load(filename, pluginForExtension_[ ext1 ]);
276 loadableFiles.push_back(filename);
277 pluginIds.push_back(pluginForExtension_[ext1]);
280 emit loadFiles(loadableFiles, pluginIds);
286 QStringList files = selectedFiles();
288 if ( files.size() != 1 ) {
289 std::cerr <<
"Error: zero or multiple save files selected!" << std::endl;
293 QString filename = files[0];
295 if (QFileInfo(filename).completeSuffix().isEmpty()) {
297 int s = selectedNameFilter().indexOf(
"*")+1;
298 int e = selectedNameFilter().indexOf(
" ", s);
299 int e2 = selectedNameFilter().indexOf(
")", s);
300 if (e == -1 || e2 < e) e = e2;
302 QString ext = selectedNameFilter().mid(s,e-s);
307 QFileInfo fi(filename);
310 pluginForExtension_.clear();
312 for (uint t=0; t < supportedTypes_.size(); t++){
314 QString filters = supportedTypes_[t].saveFilters;
317 filters = filters.section(
"(",1).section(
")",0,0);
319 QStringList separateFilters = filters.split(
" ");
323 for (
int filterId = 0 ; filterId < separateFilters.size(); ++filterId ) {
325 if (separateFilters[filterId].endsWith(fi.suffix(),Qt::CaseInsensitive)){
326 pluginForExtension_[ fi.suffix() ] = t;
338 if ( !optionsBox_->isChecked() ){
342 connect(&options, SIGNAL(setPluginForExtension(QString,
int)),
this, SLOT(slotSetPluginForExtension(QString,
int)) );
344 if ( !options.exec() )
349 int ret = QMessageBox::warning(
this, tr(
"File exists"),tr(
"This file already exists.\n" 350 "Do you want to overwrite the file?"),QMessageBox::Yes|QMessageBox::No,QMessageBox::No);
351 if (ret == QMessageBox::No)
357 const std::map< QString, int >::iterator saving_plugin =
358 pluginForExtension_.find( fi.suffix() );
359 if ( saving_plugin != pluginForExtension_.end() ){
360 if (ids_.size() == 1)
361 emit save(ids_[0],filename, saving_plugin->second);
363 emit save(ids_ ,filename, saving_plugin->second);
371 setAcceptMode ( QFileDialog::AcceptOpen );
372 setWindowTitle(tr(
"Load Object"));
375 setFileMode(QFileDialog::ExistingFiles);
380 slotSetLoadFilters();
387 setAcceptMode ( QFileDialog::AcceptSave );
388 setFileMode( QFileDialog::AnyFile );
389 setWindowTitle(tr(
"Save Object"));
400 bool typeFound =
false;
402 for (
int i=0; i < (int)supportedTypes_.size(); i++)
403 if ( object->
dataType( supportedTypes_[i].type ) )
408 std::cerr <<
"No suitable plugin for saving this dataType." << std::endl;
409 return QDialog::Rejected;
412 slotSetSaveFilters( object->
dataType() );
415 QFileInfo fi(_filename);
416 QFile file(_filename);
419 selectFile ( fi.fileName() );
422 for (
int i=0; i < nameFilters().count(); i++){
423 int s = nameFilters()[i].indexOf(
"*")+2;
424 int e = nameFilters()[i].indexOf(
" ", s);
425 int e2 = nameFilters()[i].indexOf(
")", s);
426 if (e == -1 || e2 < e) e = e2;
428 QString ext = nameFilters()[i].mid(s,e-s);
430 if (ext == fi.completeSuffix()){
431 selectNameFilter(nameFilters()[i]);
441 setAcceptMode ( QFileDialog::AcceptSave );
442 setFileMode( QFileDialog::AnyFile );
443 setWindowTitle(tr(
"Save Objects"));
450 for (uint i=0; i < _ids.size(); i++){
455 types |=
object->dataType();
459 bool typeFound =
false;
461 for (
int i=0; i < (int)supportedTypes_.size(); i++)
462 if ( supportedTypes_[i].type.contains(types) )
467 std::cerr <<
"No suitable plugin for saving this dataType." << std::endl;
468 return QDialog::Rejected;
471 slotSetSaveFilters( types );
474 QFileInfo fi(_filename);
475 QFile file(_filename);
478 selectFile ( fi.fileName() );
481 for (
int i=0; i < nameFilters().count(); i++){
482 int s = nameFilters()[i].indexOf(
"*")+2;
483 int e = nameFilters()[i].indexOf(
" ", s);
484 int e2 = nameFilters()[i].indexOf(
")", s);
485 if (e == -1 || e2 < e) e = e2;
487 QString ext = nameFilters()[i].mid(s,e-s);
489 if (ext == fi.completeSuffix()){
490 selectNameFilter(nameFilters()[i]);
498 bool LoadWidget::validFilename() {
504 QString firstEntered = selectedFiles()[0];
507 QDir testdir(firstEntered);
508 if(testdir.exists()) {
509 setDirectory(testdir);
514 QFile file(firstEntered);
515 if(!file.exists())
return false;
522 QFileInfo firstEntered = selectedFiles()[0];
524 return QDir(firstEntered.dir()).exists();
527 void LoadWidget::accept() {
529 if( (!loadMode_) || validFilename() ) {
540 QFileDialog::accept();
544 void LoadWidget::slotSetPluginForExtension(QString _extension,
int _pluginId ){
545 pluginForExtension_[ _extension ] = _pluginId;
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
bool dataType(DataType _type) const
std::vector< int > IdList
Standard Type for id Lists used for scripting.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
void setValue(const QString &key, const QVariant &value)
Wrapper function which makes it possible to enable Debugging output with -DOPENFLIPPER_SETTINGS_DEBUG...
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.