Fork me on GitHub
2.15.20
FGx
A cross-platform launcher for FlightGear
mainobject.cpp
Go to the documentation of this file.
1 // -=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-
2 // FGx FlightGear Launcher // mainobject.cpp
3 // -=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-
4 // (c) 2010-2012
5 // Yves Sablonier, Pete Morgan
6 // Geoff McLane
7 // GNU GPLv2, see main.cpp and shipped licence.txt for further information
8 
9 #include <QCoreApplication>
10 #include <QApplication>
11 #include <QTimer>
12 
13 #include <QStyleFactory>
14 
15 #include <QDesktopServices>
16 #include <QAction>
17 #include <QMenu>
18 #include <QMenuBar>
19 #include <QCursor>
20 #include <QMenuBar>
21 
22 #include "xobjects/mainobject.h"
24 
26 #include "utilities/utilities.h"
28 
29 
31  QObject(parent)
32 {
33 
34  // TODO Geoff to change this to object even static
35  //= init the LOG file
36  util_setStdLogFile();
37 
38  //= Preferences Object
39  settings = new XSettings();
40  debug_mode = settings->value("DEBUG_MODE","0").toBool();
41 
42  //= Settings Model
43  X = new XSettingsModel(this);
44  connect(X, SIGNAL(upx(QString,bool,QString)),
45  this, SLOT(on_upx(QString,bool,QString))
46  );
47 
48 
49  //================================================================
50  //= Processes - the nub..
51  //================================================================
52  processFgFs = new XProcess(this, "fgfs");
53  processTerraSync = new XProcess(this, "terrasync");
54 
55 
56 
57  //====================================
58  //= Set GLobal style
59  QApplication::setStyle( QStyleFactory::create(settings->style_current()) );
60  //QApplication::setQuitOnLastWindowClosed(false);
61 
62 
63 
64  //=====================================================================================
65  //== Tray Icon
66  trayIcon = new QSystemTrayIcon(QIcon(":/icon/favicon"), this);
67  trayIcon->setToolTip("FlightGear Launcher");
68  trayIcon->setVisible(true);
69  connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
70  this, SLOT(on_tray_icon(QSystemTrayIcon::ActivationReason)));
71 
72 
73  //== Tray Menu and actions
74  popupMenu = new QMenu();
75  trayIcon->setContextMenu(popupMenu);
76 
77  //== Callsign - label in a widget..
78  actionCallsign = new QWidgetAction(this);
79  lblCallsign = new QLabel();
80  lblCallsign->setText("(your callsign)");
81  lblCallsign->setStyleSheet("color: #000099; padding: 4px; font-weight: bold; background-color: white; border: 1px outset #cccccc;");
82  actionCallsign->setDefaultWidget(lblCallsign);
83  popupMenu->addAction(actionCallsign);
84 
85  //= Launcher Action
86  actionLauncher = popupMenu->addAction(QIcon(":icon/favicon"), tr("Open Launcher..."));
87  actionLauncher->setIconVisibleInMenu(true);
88  connect(actionLauncher, SIGNAL(triggered()), this, SLOT(on_launcher()) );
89 
90  //= Browser MpMap action
91  actionBrowserMap = popupMenu->addAction(QIcon(":icon/mpmap"), tr("Open Browser Map ..."));
92  actionBrowserMap->setIconVisibleInMenu(true);
93  connect(actionBrowserMap, SIGNAL(triggered()), this, SLOT(on_browsermap()));
94 
95 
96  //== Properties browseer
97  actionPropsBrowser = new QAction(this);
98  actionPropsBrowser->setIcon(QIcon(":/icon/props"));
99  actionPropsBrowser->setText(tr("Properties Browser..."));
100  actionPropsBrowser->setIconVisibleInMenu(true);
101  popupMenu->addAction(actionPropsBrowser);
102  connect(actionPropsBrowser, SIGNAL(triggered()),
103  this, SLOT(on_properties_browser())
104  );
105 
106  popupMenu->addSeparator();
107 
108 
109  //== View Logs
110  actionViewLogs = new QAction(this);
111  actionViewLogs->setIcon(QIcon(":/icon/log"));
112  actionViewLogs->setText(tr("View Logs"));
113  actionViewLogs->setIconVisibleInMenu(true);
114  popupMenu->addAction(actionViewLogs);
115  connect(actionViewLogs, SIGNAL(triggered()),
116  this, SLOT(on_view_logs())
117  );
118 
119  //== Fgx Debug Logs
120  actionViewFgxDebug = new QAction(this);
121  actionViewFgxDebug->setIcon(QIcon(":/icon/log"));
122  actionViewFgxDebug->setText(tr("FGx Debug"));
123  actionViewFgxDebug->setIconVisibleInMenu(true);
124  popupMenu->addAction(actionViewFgxDebug);
125  connect(actionViewFgxDebug, SIGNAL(triggered()),
126  this, SLOT(on_view_fgx_debug())
127  );
128 
129  //== Debug Enabled
130  /*actionDebugMode = new QAction(this);
131  actionDebugMode->setText(tr("Debug Mode"));
132  actionDebugMode->setCheckable(true);
133  actionDebugMode->setChecked(debug_mode);
134  popupMenu->addAction(actionDebugMode);
135  connect(actionDebugMode, SIGNAL(triggered()),
136  this, SLOT(set_debug_mode())
137  );*/
138 
139 
140  popupMenu->addSeparator();
141 
142 
143  //== Web Links
144  QMenu *menuHelp = new QMenu(tr("Help"));
145  popupMenu->addMenu(menuHelp);
146 
147  QActionGroup *actionGroupUrls = new QActionGroup(this);
148  connect(actionGroupUrls, SIGNAL(triggered(QAction*)), this, SLOT(on_action_open_url(QAction*)));
149 
150  QAction *act = menuHelp->addAction(tr("Project Page"));
151  act->setProperty("url", "http://code.google.com/p/fgx");
152  actionGroupUrls->addAction(act);
153 
154  act = menuHelp->addAction(tr("Bugs and Issues"));
155  act->setProperty("url", "http://code.google.com/p/fgx/issues/list");
156  actionGroupUrls->addAction(act);
157 
158  act = menuHelp->addAction(tr("Source Code"));
159  act->setProperty("url", "https://gitorious.org/fgx/fgx/");
160  actionGroupUrls->addAction(act);
161 
162  menuHelp->addSeparator();
163 
164  menuHelp->addAction(tr("About FGx"), this, SLOT(on_about_fgx()));
165  menuHelp->addAction(tr("About Qt"), this, SLOT(on_about_qt()));
166 
167 
168  popupMenu->addSeparator();
169 
170 
171  //== Quit
172  actionQuit = popupMenu->addAction(QIcon(":/icon/quit"), tr("Quit"));
173  actionQuit->setIconVisibleInMenu(true);
174  connect(actionQuit, SIGNAL(triggered()), this, SLOT(on_quit()));
175 
176  //==================
177  trayIcon->show();
178 
179  //connect(this, SIGNAL(show_settings(int)), this, SLOT(on_settings(int)));
180 
181  //== Props tree
183  propertiesBrowser->hide();
184 
185 
186  //= Log Viewer is hidden
187  viewLogsWidget = new ViewLogsWidget(this);
188  viewLogsWidget->hide();
189 
190 
191  //= FGx Debug Widget
192  fgxDebugWidget = new FgxDebugWidget(this);
193  if(settings->value("fgx_debug_show_on_startup",0).toBool()){
194  fgxDebugWidget->show();
195  }
196 
197  launcherWindow = new LauncherWindow(this);
198  //launcherWindow->hide();
199 
200 
201  //== initialise after initial show so UI dont look frozen while cache loading etc
202  QTimer::singleShot(300, this, SLOT(initialize()));
203 
204 
205 }
206 
208 {
209  outLog(util_getDateTimestg()+" - Application close");
210 }
211 
212 
213 //============================================================================
214 //= Initialize
216  on_launcher();
217 
218 
219 }
220 
221 //============================================================================
222 //= Launcher window
224 
225  QSettings firstsettings;
226  if(!firstsettings.value("firststartup").toBool()){
227  launcherWindow->move(0,0);
228  }
229  launcherWindow->show();
230  launcherWindow->raise();
231 
232 }
233 
234 
235 //****************************************************************************
236 //** Browser Map
238  QUrl mapUrl(X->getx("show_mpmap"));
239  if (X->get_ena("show_mpmap")) {
240  mapUrl.addQueryItem("follow", X->getx("--callsign="));
241  QDesktopServices::openUrl(mapUrl);
242  outLog("Opening browser map: "+mapUrl.toString());
243  }
244 }
245 
246 //======================================
247 //== Propertes Browser
249  propertiesBrowser->show();
250  propertiesBrowser->setFocus();
251 }
252 
253 
254 //=================================================
255 //== Tray Icon Clicked
256 void MainObject::on_tray_icon(QSystemTrayIcon::ActivationReason reason){
257  //= Right click will show the context Menu above system tray
258  //= Following will popup menu with single click on Top LEFT ??
259  if(reason == QSystemTrayIcon::Trigger){
260  QPoint p = QCursor::pos();
261  trayIcon->contextMenu()->popup(p);
262  }
263 }
264 
265 //=============================================================================
266 //== Logs
267 //=============================================================================
269  viewLogsWidget->show();
270 }
271 
272 void MainObject::clear_log(QString log_name){
273  viewLogsWidget->clear_log(log_name);
274 }
275 
276 void MainObject::add_log(QString log_name, QString data){
277  viewLogsWidget->add_log(log_name, data);
278 }
279 
280 
281 
282 //========================================================
283 //== Start All
285  start_fgfs();
286 
287  // Does not need to start when not checked in settings
288  if (X->terrasync_enabled()){
289  start_terrasync();
290  }
291 
292 }
293 
294 //========================================================
295 //== Stop All
297  processFgFs->stop();
298 
299 
300  if (X->terrasync_enabled()){
302  }
303 }
304 
305 
306 
307 //========================================================
308 //== Start FGFS
311  on_browsermap();
312 }
313 
314 //========================================================
315 //== Start TerraSync
317 
318  QStringList terraargs;
319  terraargs << "-p" << "5505" << "-S" << "-d" << X->terrasync_data_path();
320 
321  QString terra_command_line = X->getx("terrasync_exe_path");
322  terra_command_line.append(" ").append( terraargs.join(" ") );
323 
324  processTerraSync->start(terra_command_line, QStringList());
325 }
326 
327 
328 
330 
331  stop_all();
332 
337 
338  settings->sync();
339 
340  QApplication::quit();
341 }
342 
343 
344 //==========================================================
345 //== Debug Related
346 //==========================================================
348  fgxDebugWidget->show();
349 }
350 
351 
352 // This is set by the menu widget now
353 void MainObject::set_debug_mode(bool menu_mode)
354 {
355  debug_mode = menu_mode;
356  settings->setValue("DEBUG_MODE",debug_mode);
358 }
359 
360 //===========================================================================
361 //** OS detection
362 //===========================================================================
363 /** \brief What OS is running
364  *
365  * \return a OS enum value
366  */
368  #ifdef Q_WS_X11
369  return OS_LINUX;
370  #endif
371 
372  #ifdef Q_WS_MAC
373  return OS_MAC;
374  #endif
375 
376  #ifdef Q_WS_WIN
377  return OS_WINDOWS;
378  #endif
379 
380  return OS_UNKNOWN;
381 }
382 
383 
384 /** \brief Log File
385  *
386  * \return Absolute path to log file
387  */
389  if(runningOs() == OS_WINDOWS){
390  return temp_dir("/fgx-log.txt");
391 
392  }else if(runningOs() == OS_MAC){
393  return QDir::homePath().append("/Library/Logs/fgx.log");
394 
395  }else if(runningOs() == OS_LINUX){
396  return temp_dir("/fgx.log");
397 
398  }else{
399  return "UNKNOWN log_file_path()";
400  }
401 }
402 
403 
404 
405 
406 //===========================================================================
407 //** temp
408 //===========================================================================
409 /** \brief location if temp directoty , os specific
410  *
411  * Shortcut method for Qt's storageLocation()
412  * \return Absolute path
413  */
415  return QDir(QDesktopServices::storageLocation(QDesktopServices::TempLocation)).absolutePath();
416 }
417 /** \brief location if temp directoty , os specific with appended file/path
418  *
419  * Shortcut method for Qt's storageLocation()
420  * \return Absolute path with appended paths.
421  */
422 QString MainObject::temp_dir(QString append_path){
423  return temp_dir().append(append_path);
424 }
425 
426 
427 
428 
429 
430 
431 //===========================================================================
432 //** Data File eg airports.txt
433 //===========================================================================
434 /** \brief Path to a data file eg data_file("airports.txt")
435  *
436  * \return Absolute path to the file
437  */
438 QString MainObject::data_file(QString file_name){
439  QString storedir = QDir(QDesktopServices::storageLocation(QDesktopServices::DataLocation)).absolutePath();
440 
441  // create path is not exist
442  if(!QFile::exists(storedir)){
443  QDir *dir = new QDir("");
444  dir->mkpath(storedir);
445  }
446  return storedir.append("/").append(file_name);
447 }
448 
449 
450 //=======================================================================================================================
451 //* Help Menu Events
452 //=======================================================================================================================
453 
455 {
456  QString txt;
457  txt.append("<html><body><p>FGx FlightGear Launcher</b></p>");
458  txt.append("<p>&copy; 2011 Yves Sablonier, Pete Morgan, Geoff McLane</p>");
459  txt.append("<p><a href='http://www.gnu.org/licenses/gpl-2.0.txt'>GPLv2 and later</a></p>");
460  txt.append("<p><a href='http://wiki.flightgear.org'>FlightGear Wiki/Help</a></p>");
461  txt.append("<p><a href='http://www.flightgear.org/forums'>FlightGear Forums</a></p>");
462  txt.append("<pre>\"Rock on and avoid mountains near Zurich, Paris and EGFF ...\", said Pete.</pre></body></html>");
463  QMessageBox::about(0, "About FGx", txt);
464 }
465 
467 {
468  QMessageBox::aboutQt(0, "About Qt");
469 }
470 
472 {
473  QUrl url(act->property("url").toString());
474  QDesktopServices::openUrl( url );
475 }
476 
477 
478 void MainObject::on_upx(QString option, bool enabled, QString value)
479 {
480  Q_UNUSED(enabled);
481  if(option == "--callsign="){
482  lblCallsign->setText(value);
483  }
484 }
void on_view_fgx_debug()
Definition: mainobject.cpp:347
void on_properties_browser()
Definition: mainobject.cpp:248
QAction * actionPropsBrowser
Definition: mainobject.h:109
QSystemTrayIcon * trayIcon
Definition: mainobject.h:101
static QString log_file_path()
Log File.
Definition: mainobject.cpp:388
void clear_log(QString log_name)
Definition: mainobject.cpp:272
QString style_current()
return the currently selected style or the default
Definition: xsettings.cpp:39
QWidgetAction * actionCallsign
Definition: mainobject.h:104
The XSettings is an extended QSetttings, but also includes most to the FGx configuration.
Definition: xsettings.h:15
XProcess * processFgFs
Definition: mainobject.h:97
static int runningOs()
What OS is running.
Definition: mainobject.cpp:367
ViewLogsWidget * viewLogsWidget
Definition: mainobject.h:93
void on_upx(QString option, bool enabled, QString value)
Definition: mainobject.cpp:478
void start(QString command_line, QStringList user_env)
Definition: xprocess.cpp:41
void on_about_fgx()
Definition: mainobject.cpp:454
void clear_log(QString log_name)
The XSettingsModel is an extended QStandardItemModel, and the main "setting and state" for FGx...
void saveWindow(QWidget *widget)
Saves a window's size and position.
Definition: xsettings.cpp:90
static QString temp_dir()
location if temp directoty , os specific
Definition: mainobject.cpp:414
XProcess * processTerraSync
Definition: mainobject.h:98
QAction * actionViewLogs
Definition: mainobject.h:112
void initialize()
Definition: mainobject.cpp:215
QAction * actionLauncher
Definition: mainobject.h:107
QString get_fgfs_command_string()
bool debug_mode
Definition: mainobject.h:75
void stop()
Definition: xprocess.cpp:60
void on_about_qt()
Definition: mainobject.cpp:466
PropsTreeWidget * propertiesBrowser
Definition: mainobject.h:94
void stop_all()
Definition: mainobject.cpp:296
void on_quit()
Definition: mainobject.cpp:329
The LauncherWindow is raised and controlled by MainObject.
QString data_file(QString file_name)
Path to a data file eg data_file("airports.txt")
Definition: mainobject.cpp:438
FgxDebugWidget * fgxDebugWidget
Definition: mainobject.h:95
bool get_ena(QString option)
void set_debug_mode(bool)
Definition: mainobject.cpp:353
QAction * actionQuit
Definition: mainobject.h:116
QStringList get_fgfs_env()
XSettingsModel * X
Definition: mainobject.h:86
void on_browsermap()
Definition: mainobject.cpp:237
void add_log(QString log_name, QString log)
void on_action_open_url(QAction *)
Definition: mainobject.cpp:471
void on_launcher()
Definition: mainobject.cpp:223
void start_all()
Definition: mainobject.cpp:284
LauncherWindow * launcherWindow
Definition: mainobject.h:89
void start_fgfs()
Definition: mainobject.cpp:309
MainObject(QObject *parent=0)
Definition: mainobject.cpp:30
QMenu * popupMenu
Definition: mainobject.h:102
void on_tray_icon(QSystemTrayIcon::ActivationReason reason)
Definition: mainobject.cpp:256
bool terrasync_enabled()
Sert terrasync enabled/disabled.
void on_debug_mode(bool enabled)
QString terrasync_data_path()
Path to terrasync executable.
QAction * actionBrowserMap
Definition: mainobject.h:114
XSettings * settings
Definition: mainobject.h:85
void start_terrasync()
Definition: mainobject.cpp:316
void add_log(QString log_name, QString data)
Definition: mainobject.cpp:276
QString getx(QString option)
QAction * actionViewFgxDebug
Definition: mainobject.h:113
QLabel * lblCallsign
Definition: mainobject.h:105
void on_view_logs()
Definition: mainobject.cpp:268