2016-02-10 22:58:56 +00:00
|
|
|
// QtFileDialog.cxx - Qt5 implementation of FGFileDialog
|
|
|
|
//
|
|
|
|
// Written by Rebecca Palmer, started February 2016.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2015 Rebecca Palmer <rebecca_palmer@zoho.com>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program 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
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
#include "QtFileDialog.hxx"
|
|
|
|
#include "QtLauncher.hxx"
|
|
|
|
#include <simgear/debug/logstream.hxx>
|
|
|
|
|
|
|
|
// Qt
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
|
|
|
|
QtFileDialog::QtFileDialog(FGFileDialog::Usage use) :
|
|
|
|
FGFileDialog(use)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QtFileDialog::~QtFileDialog() {}
|
|
|
|
|
|
|
|
void QtFileDialog::exec()
|
|
|
|
{
|
|
|
|
int fakeargc = 1;
|
|
|
|
static char fakeargv0[] = "fgfs";
|
|
|
|
static char * fakeargv[2] = {fakeargv0, 0};
|
Split flightgear::initApp() to make it usable even without FGGlobals initialized
- Add an optional argument to flightgear::initApp(): doInitQSettings.
This argument defaults to true, preserving initApp()'s behavior in
this respect. If this argument is set to false, FGGlobals doesn't have
to be initialized.
- New function flightgear::initQSettings(), called by
flightgear::initApp() when its 'doInitQSettings' argument is true.
This allows initializing the QSettings exactly when it is needed.
- New function flightgear::checkKeyboardModifiersForSettingFGRoot().
The code it contains used to be run from initApp(), which is
undesirable because:
1) initApp() is not only called at FG initialization (fgMainInit()),
but also from QtMessageBox(), from QtFileDialog::exec() and twice
from Options::setupRoot(). However, checking the Alt and Shift
modifiers to set 'fg-root' in QSettings to the special value
"!ask" only makes sense in fgMainInit(), not in these other
places.
2) This code relies on the QSettings to be set up, and therefore on
FGGlobals. Thus, freeing initApp() of its dependency on FGGlobals
requires splitting this keyboard modifiers checking code out of
initApp().
2016-12-26 20:45:44 +00:00
|
|
|
// This does nothing if it has already been run, so the fake argc/argv are
|
|
|
|
// only used if run without launcher. Don't attempt to initialize the
|
|
|
|
// QSettings, because this would require FGGlobals to be initialized (for
|
|
|
|
// globals->get_fg_home()), which would prevent using this function at
|
|
|
|
// early startup.
|
|
|
|
flightgear::initApp(fakeargc, fakeargv, false /* doInitQSettings */);
|
2016-02-10 22:58:56 +00:00
|
|
|
|
|
|
|
// concatenate filter patterns, as Qt uses a single string
|
|
|
|
std::string filter="";
|
|
|
|
for( string_list::const_iterator it = _filterPatterns.begin(); it != _filterPatterns.end();++it ) {
|
|
|
|
if(!filter.empty()){
|
|
|
|
filter=filter+" ";
|
|
|
|
}
|
|
|
|
filter=filter+*it;
|
|
|
|
}
|
2016-06-21 11:28:35 +00:00
|
|
|
QFileDialog dlg(0,QString::fromStdString(_title),QString::fromStdString(_initialPath.utf8Str()),QString::fromStdString(filter));
|
2016-02-10 22:58:56 +00:00
|
|
|
if (_usage==USE_SAVE_FILE) {
|
|
|
|
dlg.setAcceptMode(QFileDialog::AcceptSave);
|
|
|
|
}
|
|
|
|
if (_usage==USE_CHOOSE_DIR) {
|
|
|
|
dlg.setFileMode(QFileDialog::Directory);
|
|
|
|
}
|
|
|
|
if (_usage==USE_OPEN_FILE) {
|
|
|
|
dlg.setFileMode(QFileDialog::ExistingFile);
|
|
|
|
}
|
|
|
|
dlg.setLabelText(QFileDialog::Accept,QString::fromStdString(_buttonText));
|
|
|
|
dlg.selectFile(QString::fromStdString(_placeholder));
|
|
|
|
if(_showHidden){
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "QtFileDialog: can't show hidden files in Qt");
|
|
|
|
}
|
|
|
|
if(dlg.exec()){
|
|
|
|
QStringList result = dlg.selectedFiles();
|
|
|
|
if(!(result.isEmpty())){
|
|
|
|
_callback->onFileDialogDone(this, SGPath(result[0].toStdString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QtFileDialog::close(){}
|
|
|
|
|