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 setConfirmOverwrite(
false);
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 ext = fi.suffix();
254 if (optionsBox_->isChecked()) {
260 for (j = 0; j < supportedTypes_.size(); ++j) {
261 if (supportedTypes_[j].name == pluginName) {
268 pluginForExtension_[ ext ] = j;
272 if ( pluginForExtension_.find( ext ) != pluginForExtension_.end() ){
273 emit load(filename, pluginForExtension_[ ext ]);
274 loadableFiles.push_back(filename);
275 pluginIds.push_back(pluginForExtension_[ext]);
278 emit loadFiles(loadableFiles, pluginIds);
284 QStringList files = selectedFiles();
286 if ( files.size() != 1 ) {
287 std::cerr <<
"Error: zero or multiple save files selected!" << std::endl;
291 QString filename = files[0];
293 if (QFileInfo(filename).completeSuffix().isEmpty()) {
295 int s = selectedNameFilter().indexOf(
"*")+1;
296 int e = selectedNameFilter().indexOf(
" ", s);
297 int e2 = selectedNameFilter().indexOf(
")", s);
298 if (e == -1 || e2 < e) e = e2;
300 QString ext = selectedNameFilter().mid(s,e-s);
305 QFileInfo fi(filename);
308 pluginForExtension_.clear();
310 for (uint t=0; t < supportedTypes_.size(); t++){
312 QString filters = supportedTypes_[t].saveFilters;
315 filters = filters.section(
"(",1).section(
")",0,0);
317 QStringList separateFilters = filters.split(
" ");
321 for (
int filterId = 0 ; filterId < separateFilters.size(); ++filterId ) {
323 if (separateFilters[filterId].endsWith(fi.suffix(),Qt::CaseInsensitive)){
324 pluginForExtension_[ fi.suffix() ] = t;
336 if ( !optionsBox_->isChecked() ){
340 connect(&options, SIGNAL(setPluginForExtension(QString,
int)),
this, SLOT(slotSetPluginForExtension(QString,
int)) );
342 if ( !options.exec() )
347 int ret = QMessageBox::warning(
this, tr(
"File exists"),tr(
"This file already exists.\n" 348 "Do you want to overwrite the file?"),QMessageBox::Yes|QMessageBox::No,QMessageBox::No);
349 if (ret == QMessageBox::No)
353 const std::map< QString, int >::iterator saving_plugin =
354 pluginForExtension_.find( fi.suffix() );
355 if ( saving_plugin != pluginForExtension_.end() ){
356 if (ids_.size() == 1)
357 emit save(ids_[0],filename, saving_plugin->second);
359 emit save(ids_ ,filename, saving_plugin->second);
367 setAcceptMode ( QFileDialog::AcceptOpen );
368 setWindowTitle(tr(
"Load Object"));
371 setFileMode(QFileDialog::ExistingFiles);
376 slotSetLoadFilters();
383 setAcceptMode ( QFileDialog::AcceptSave );
384 setFileMode( QFileDialog::AnyFile );
385 setWindowTitle(tr(
"Save Object"));
396 bool typeFound =
false;
398 for (
int i=0; i < (int)supportedTypes_.size(); i++)
399 if ( object->
dataType( supportedTypes_[i].type ) )
404 std::cerr <<
"No suitable plugin for saving this dataType." << std::endl;
405 return QDialog::Rejected;
408 slotSetSaveFilters( object->
dataType() );
411 QFileInfo fi(_filename);
412 QFile file(_filename);
415 selectFile ( fi.fileName() );
418 for (
int i=0; i < nameFilters().count(); i++){
419 int s = nameFilters()[i].indexOf(
"*")+2;
420 int e = nameFilters()[i].indexOf(
" ", s);
421 int e2 = nameFilters()[i].indexOf(
")", s);
422 if (e == -1 || e2 < e) e = e2;
424 QString ext = nameFilters()[i].mid(s,e-s);
426 if (ext == fi.completeSuffix()){
427 selectNameFilter(nameFilters()[i]);
437 setAcceptMode ( QFileDialog::AcceptSave );
438 setFileMode( QFileDialog::AnyFile );
439 setWindowTitle(tr(
"Save Objects"));
446 for (uint i=0; i < _ids.size(); i++){
451 types |=
object->dataType();
455 bool typeFound =
false;
457 for (
int i=0; i < (int)supportedTypes_.size(); i++)
458 if ( supportedTypes_[i].type.contains(types) )
463 std::cerr <<
"No suitable plugin for saving this dataType." << std::endl;
464 return QDialog::Rejected;
467 slotSetSaveFilters( types );
470 QFileInfo fi(_filename);
471 QFile file(_filename);
474 selectFile ( fi.fileName() );
477 for (
int i=0; i < nameFilters().count(); i++){
478 int s = nameFilters()[i].indexOf(
"*")+2;
479 int e = nameFilters()[i].indexOf(
" ", s);
480 int e2 = nameFilters()[i].indexOf(
")", s);
481 if (e == -1 || e2 < e) e = e2;
483 QString ext = nameFilters()[i].mid(s,e-s);
485 if (ext == fi.completeSuffix()){
486 selectNameFilter(nameFilters()[i]);
494 bool LoadWidget::validFilename() {
500 QString firstEntered = selectedFiles()[0];
503 QDir testdir(firstEntered);
504 if(testdir.exists()) {
505 setDirectory(testdir);
510 QFile file(firstEntered);
511 if(!file.exists())
return false;
518 QFileInfo firstEntered = selectedFiles()[0];
520 return QDir(firstEntered.dir()).exists();
523 void LoadWidget::accept() {
525 if( (!loadMode_) || validFilename() ) {
536 QFileDialog::accept();
540 void LoadWidget::slotSetPluginForExtension(QString _extension,
int _pluginId ){
541 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.