]> git.mxchange.org Git - flightgear.git/blob - src/GUI/QtFileDialog.cxx
Code cleanups, code updates and fix at least on (possible) devide-by-zero
[flightgear.git] / src / GUI / QtFileDialog.cxx
1 // QtFileDialog.cxx - Qt5 implementation of FGFileDialog
2 //
3 // Written by Rebecca Palmer, started February 2016.
4 //
5 // Copyright (C) 2015 Rebecca Palmer <rebecca_palmer@zoho.com>
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #include "QtFileDialog.hxx"
22 #include "QtLauncher.hxx"
23 #include <simgear/debug/logstream.hxx>
24
25 // Qt
26 #include <QFileDialog>
27 #include <QString>
28 #include <QStringList>
29
30 QtFileDialog::QtFileDialog(FGFileDialog::Usage use) :
31     FGFileDialog(use)
32 {
33
34 }
35
36 QtFileDialog::~QtFileDialog() {}
37
38 void QtFileDialog::exec()
39 {
40     int fakeargc = 1;
41     static char fakeargv0[] = "fgfs";
42     static char * fakeargv[2] = {fakeargv0, 0};
43     // This does nothing if it has already been run, so the fake argc/argv
44     // are only used if run without launcher
45     flightgear::initApp(fakeargc, fakeargv);
46     
47     // concatenate filter patterns, as Qt uses a single string
48     std::string filter="";
49     for( string_list::const_iterator it = _filterPatterns.begin(); it != _filterPatterns.end();++it ) {
50         if(!filter.empty()){
51             filter=filter+" ";
52         }
53         filter=filter+*it;
54     }
55     QFileDialog dlg(0,QString::fromStdString(_title),QString::fromStdString(_initialPath.str()),QString::fromStdString(filter));
56     if (_usage==USE_SAVE_FILE) {
57         dlg.setAcceptMode(QFileDialog::AcceptSave);
58     }
59     if (_usage==USE_CHOOSE_DIR) {
60         dlg.setFileMode(QFileDialog::Directory);
61     }
62     if (_usage==USE_OPEN_FILE) {
63         dlg.setFileMode(QFileDialog::ExistingFile);
64     }
65     dlg.setLabelText(QFileDialog::Accept,QString::fromStdString(_buttonText));
66     dlg.selectFile(QString::fromStdString(_placeholder));
67     if(_showHidden){
68         SG_LOG(SG_GENERAL, SG_ALERT, "QtFileDialog: can't show hidden files in Qt");
69     }
70     if(dlg.exec()){
71         QStringList result = dlg.selectedFiles();
72         if(!(result.isEmpty())){
73             _callback->onFileDialogDone(this, SGPath(result[0].toStdString()));
74         }
75     }
76 }
77
78 void QtFileDialog::close(){}
79