GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
octave-gui.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2013 Jacob Dawid
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <QApplication>
28 #include <QTextCodec>
29 #include <QThread>
30 #include <QTranslator>
31 
32 #include <iostream>
33 
34 #include <unistd.h>
35 #include <fcntl.h>
36 
37 #if defined (HAVE_SYS_IOCTL_H)
38 #include <sys/ioctl.h>
39 #endif
40 
41 #include "lo-utils.h"
42 #include "oct-env.h"
43 #include "oct-syscalls.h"
44 #include "syswait.h"
45 
46 #include "octave.h"
47 #include "sighandlers.h"
48 
49 #include "welcome-wizard.h"
50 #include "resource-manager.h"
51 #include "main-window.h"
52 #include "octave-gui.h"
53 #include "thread-manager.h"
54 
55 // Allow the Octave interpreter to start as in CLI mode with a
56 // QApplication context so that it can use Qt for things like plotting
57 // and UI widgets.
58 
59 class octave_cli_thread : public QThread
60 {
61 public:
62 
63  octave_cli_thread (int argc, char **argv)
64  : m_argc (argc), m_argv (argv), m_result (0) { }
65 
66  int result (void) const { return m_result; }
67 
68 protected:
69 
70  void run (void)
71  {
73 
75 
77 
78  QApplication::exit (m_result);
79  }
80 
81 private:
82 
83  int m_argc;
84  char** m_argv;
85  int m_result;
86 };
87 
88 
89 // Custom message handler for filtering some messages from Qt.
90 
91 void message_handler (QtMsgType type, const char *msg)
92 {
93  switch (type)
94  {
95  case QtDebugMsg:
96  if (strncmp (msg, "QFileSystemWatcher: skipping native engine",42) != 0)
97  std::cerr << "Debug: " << msg << std::endl;
98  break;
99 
100  case QtWarningMsg:
101  std::cerr << "Warning: " << msg << std::endl;
102  break;
103 
104  case QtCriticalMsg:
105  std::cerr << "Critical: " << msg << std::endl;
106  break;
107 
108  case QtFatalMsg:
109  std::cerr << "Fatal: " << msg << std::endl;
110  abort ();
111 
112  default:
113  break;
114  }
115 }
116 
117 // If START_GUI is false, we still set up the QApplication so that we
118 // can use Qt widgets for plot windows.
119 
120 int
121 octave_start_gui (int argc, char *argv[], bool start_gui)
122 {
124 
125  qInstallMsgHandler (message_handler);
126 
127  if (start_gui)
128  {
129  QApplication application (argc, argv);
130  QTranslator gui_tr, qt_tr, qsci_tr;
131 
132  // Set the codec for all strings (before wizard)
133  QTextCodec::setCodecForCStrings (QTextCodec::codecForName ("UTF-8"));
134 
135  // show wizard if this is the first run
137  {
138  resource_manager::config_translators (&qt_tr, &qsci_tr, &gui_tr); // before wizard
139  application.installTranslator (&qt_tr);
140  application.installTranslator (&qsci_tr);
141  application.installTranslator (&gui_tr);
142 
143  welcome_wizard welcomeWizard;
144 
145  if (welcomeWizard.exec () == QDialog::Rejected)
146  exit (1);
147 
148  resource_manager::reload_settings (); // install settings file
149  }
150  else
151  {
152  resource_manager::reload_settings (); // get settings file
153 
154  resource_manager::config_translators (&qt_tr, &qsci_tr, &gui_tr); // after settings
155  application.installTranslator (&qt_tr);
156  application.installTranslator (&qsci_tr);
157  application.installTranslator (&gui_tr);
158  }
159 
160  // update network-settings
162 
163 #if ! defined (__WIN32__) || defined (__CYGWIN__)
164  // If we were started from a launcher, TERM might not be
165  // defined, but we provide a terminal with xterm
166  // capabilities.
167 
168  std::string term = octave_env::getenv ("TERM");
169 
170  if (term.empty ())
171  octave_env::putenv ("TERM", "xterm");
172 #else
173  std::string term = octave_env::getenv ("TERM");
174 
175  if (term.empty ())
176  octave_env::putenv ("TERM", "cygwin");
177 #endif
178 
179  // Create and show main window.
180 
181  main_window w;
182 
183  w.read_settings ();
184 
186 
187  // Connect signals for changes in visibility not before w
188  // is shown.
189 
191 
192  return application.exec ();
193  }
194  else
195  {
196  QApplication application (argc, argv);
197 
198  octave_cli_thread main_thread (argc, argv);
199 
200  application.setQuitOnLastWindowClosed (false);
201 
202  main_thread.start ();
203 
204  return application.exec ();
205  }
206 }