Fork me on GitHub
2.15.20
FGx
A cross-platform launcher for FlightGear
fgexepage.cpp
Go to the documentation of this file.
1 
2 //#include <QtCore/QProcess>
3 
4 #include <QtGui/QToolButton>
5 #include <QtGui/QLabel>
6 #include <QtGui/QGridLayout>
7 
8 #include <QtGui/QLabel>
9 #include <QtGui/QMenu>
10 
11 #include <QtGui/QFileDialog>
12 
13 #include "fgexepage.h"
14 #include "xobjects/xopt.h"
15 
17  QWizardPage(parent)
18 {
19  mainObject = mob;
20 
21  setTitle("Select FlightGear exectable");
22  setSubTitle("Select the default or a custom path");
23 
24 
25  QGridLayout *layoutExe = new QGridLayout();
26  setLayout(layoutExe);
27 
28  //= Default Path
29  int row = 0;
30  radioDefault = new QRadioButton();
31  radioDefault->setText("Use default path");
32  layoutExe->addWidget(radioDefault, row, 0, 1, 3);
33  connect(radioDefault, SIGNAL(toggled(bool)), this, SLOT(on_default_toggled(bool)));
34 
35  //= default help label
36  row++;
37  lblDefault = new QLabel("--");
38  layoutExe->addWidget(lblDefault, row, 1, 1, 2);
39 
40  //= Custom path
41  row++;
42  radioCustom = new QRadioButton();
43  radioCustom->setText("Select custom path");
44  radioCustom->setChecked(true);
45  layoutExe->addWidget(radioCustom, row, 0, 1, 3);
46 
47  //= select custom path
48  row++;
49  txtFgfs = new QLineEdit("");
50  layoutExe->addWidget(txtFgfs, row, 1, 1, 1);
51  connect(txtFgfs, SIGNAL(textChanged(QString)), this, SLOT(check_paths()) );
52 
53  //= Dropdown button for path
54  buttExecutable = new QToolButton();
55  layoutExe->addWidget(buttExecutable, row, 2);
56  buttExecutable->setIcon(QIcon(":/icon/black"));
57  buttExecutable->setPopupMode(QToolButton::InstantPopup);
58 
59  QMenu *menuFgfs = new QMenu();
60  buttExecutable->setMenu(menuFgfs);
61 
62  QAction *actionFgfsSelectPath = new QAction(menuFgfs);
63  menuFgfs->addAction(actionFgfsSelectPath);
64  actionFgfsSelectPath->setText(tr("Select path ..."));
65  connect(actionFgfsSelectPath, SIGNAL(triggered()), this, SLOT(on_select_fgfs_path()));
66 
67  // does not work for win/osx
68  if(mainObject->runningOs() == XSettings::LINUX){
69  QAction *actionFgfsAutoSelect = new QAction(menuFgfs);
70  menuFgfs->addAction(actionFgfsAutoSelect);
71  actionFgfsAutoSelect->setText(tr("Autodetect"));
72  connect(actionFgfsAutoSelect, SIGNAL(triggered()), this, SLOT(on_fgfs_autodetect()));
73  //actionFgfsAutoSelect->setVisible( mainObject->settings->runningOs() != XSettings::WINDOWS );
74  }
75 
76 
77  //= custom help label
78  row++;
79  lblCustom = new QLabel("--");
80  layoutExe->addWidget(lblCustom, row, 1, 1, 2);
81 
82  layoutExe->setColumnStretch(0,1);
83  layoutExe->setColumnStretch(1,6);
84  layoutExe->setColumnStretch(2,0);
85 
86  registerField("fgfs_use_default", radioDefault);
87  registerField("fgfs_use_custom", radioCustom);
88  registerField("fgfs_custom_path", txtFgfs);
89 
90 }
91 
92 //=================================================================================
93 //* Autodetect fgfs - this wont work on windows probably
95 
96  // if(mainLayout->settings->runningOs() == )
97  // TODO implem,ent Geoffs patch
98  QString command = "which fgfs";
99 
100  QProcess *process = new QProcess(this);
101  process->start(command);
102 
103  if(process->waitForStarted()){
104 
105  process->waitForFinished();
106  QByteArray result = process->readAllStandardOutput();
107  //QByteArray errorResult = process->readAllStandardError();
108  QString exe = QString(result).trimmed();
109 
110  if(exe.length() == 0){
111  // none found
112  }else{
113  txtFgfs->setText(exe);
114  }
115  }
116 }
117 
118 
119 //=================================================================================
120 //= Select FGFS Dialog
122  QString filePath = QFileDialog::getOpenFileName(this, tr("Select FGFS binary"),
123  txtFgfs->text());
124  if(filePath.length() > 0){
125  txtFgfs->setText(filePath);
126  }
127  check_paths();
128 }
129 
130 //=================================================================================
131 //= Check paths and color labels
133 {
134  //= Check the default path
135  QString default_path = mainObject->X->fgfs_default_path();
136  bool default_exists = QFile::exists(default_path);
137  QString lbl_default(default_path);
138  lbl_default.append( default_exists ? " - Ok" : " - Not Found" );
139  QString style_default("");
140 
141  if(radioDefault->isChecked()){
142  style_default.append(default_exists ? "color:green;" : "color:#990000;");
143  }else{
144  style_default.append("color:#444444;");
145  }
146  lblDefault->setText(lbl_default);
147  lblDefault->setStyleSheet(style_default);
148 
149  //= Check Custom Path
150  if(radioCustom->isChecked()){
151  QString custom_path = txtFgfs->text().trimmed();
152  if(custom_path.size() == 0){
153  lblCustom->setText("Select a path");
154  lblCustom->setStyleSheet("color: #000099;");
155  }else{
156  bool custom_exists = QFile::exists(custom_path);
157  lblCustom->setText(custom_exists ? "Ok" : "Not found");
158  lblCustom->setStyleSheet(custom_exists ? "color: green;" : "color:#990000;");
159  if(custom_exists){
160  QFileInfo fInfo(custom_path);
161  if(fInfo.isDir()){
162  lblCustom->setText("Need a file path, not a directory");
163  lblCustom->setStyleSheet("color:#990000;");
164  }else{
165  if(!fInfo.isExecutable()){
166  lblCustom->setText("Not executable");
167  lblCustom->setStyleSheet("color:#990000;");
168  }
169  }
170  }
171  }
172  }else{
173  lblCustom->setText("");
174  }
175 }
176 
177 
178 
179 //===================================================
180 //= initializePage
182 {
183  XOpt opt = mainObject->X->get_opt("fgfs_custom_path");
184  radioDefault->setChecked( opt.enabled == false );
185  radioCustom->setChecked( opt.enabled == true );
186 
187  lblDefault->setText( QString("Default: ").append(mainObject->X->fgfs_default_path()) );
188  txtFgfs->setText( opt.value );
189  check_paths();
190 }
191 
192 
193 
194 //====================================================
195 //= ValidatePage
197 {
198  check_paths();
199  if(radioDefault->isChecked()){
200  QString exFile = mainObject->X->fgfs_default_path();
201  if(QFile::exists(exFile)){
202  QFileInfo fInfo(exFile);
203  if(fInfo.isFile() && fInfo.isExecutable()){
204  return true;
205  }else{
206  return false;
207  }
208  }else{
209  return false;
210  }
211  }
212 
213  if(QFile::exists(txtFgfs->text())){
214  // TODO - check its executable
215  //perms = QFile::permissions(txtFgfs->text())
216  return true;
217  }else{
218  txtFgfs->setFocus();
219  }
220  return false;
221 }
222 
223 
225  Q_UNUSED(state);
226 
227  bool def = radioDefault->isChecked();
228 
229  txtFgfs->setDisabled(def);
230  buttExecutable->setDisabled( def);
231  if(def == false){
232  if(txtFgfs->text().length() == 0){
233  lblCustom->setText("Select path to exacutable");
234  }
235  txtFgfs->setFocus();
236  }
237 
238  check_paths();
239 }
QRadioButton * radioCustom
Definition: fgexepage.h:20
QLabel * lblCustom
Definition: fgexepage.h:24
QString value
Definition: xopt.h:19
The MainObject is an extended QObject, and the main "controller" for FGx.
Definition: mainobject.h:66
QToolButton * buttExecutable
Definition: fgexepage.h:21
static int runningOs()
What OS is running.
Definition: mainobject.cpp:367
void on_fgfs_autodetect()
Definition: fgexepage.cpp:94
MainObject * mainObject
Definition: fgexepage.h:17
FgExePage(MainObject *mainObject, QWidget *parent=0)
Definition: fgexepage.cpp:16
void initializePage()
Definition: fgexepage.cpp:181
Definition: xopt.h:14
XSettingsModel * X
Definition: mainobject.h:86
bool validatePage()
Definition: fgexepage.cpp:196
void on_select_fgfs_path()
Definition: fgexepage.cpp:121
void on_default_toggled(bool)
Definition: fgexepage.cpp:224
QLabel * lblDefault
Definition: fgexepage.h:23
void check_paths()
Definition: fgexepage.cpp:132
XOpt get_opt(QString option)
QRadioButton * radioDefault
Definition: fgexepage.h:19
QLineEdit * txtFgfs
Definition: fgexepage.h:25
bool enabled
Definition: xopt.h:20