GNU Octave  4.2.1
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-2017 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 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <QApplication>
28 #include <QTextCodec>
29 #include <QThread>
30 #include <QTranslator>
31 #include <QtGlobal>
32 #include <QStyleFactory>
33 
34 #include <cstdio>
35 
36 #include <iostream>
37 
38 #if defined (HAVE_SYS_IOCTL_H)
39 # include <sys/ioctl.h>
40 #endif
41 
42 #include "lo-utils.h"
43 #include "oct-env.h"
44 #include "oct-syscalls.h"
45 
46 #include "builtin-defun-decls.h"
47 #include "display.h"
48 #if defined (HAVE_QT_GRAPHICS)
49 # include "__init_qt__.h"
50 #endif
51 #include "octave.h"
52 #include "sysdep.h"
53 #include "unistd-wrappers.h"
54 
55 #include "main-window.h"
56 #include "octave-gui.h"
57 #include "resource-manager.h"
58 #include "shortcut-manager.h"
59 #include "thread-manager.h"
60 #include "welcome-wizard.h"
61 
62 // Disable all Qt messages by default.
63 
64 static void
65 #if defined (HAVE_QT4)
66 message_handler (QtMsgType, const char *)
67 #else
68 message_handler (QtMsgType, const QMessageLogContext &, const QString &)
69 #endif
70 { }
71 
72 namespace octave
73 {
74  bool gui_application::start_gui_p (void) const
75  {
76  if (m_options.no_window_system ())
77  return false;
78 
79  std::string err_msg;
80  if (! display_info::display_available (err_msg))
81  {
82  if (! (m_options.inhibit_startup_message () || err_msg.empty ()))
83  warning (err_msg.c_str ());
84 
85  return false;
86  }
87 
88  if (! m_options.line_editing ())
89  {
90  if (! (m_options.inhibit_startup_message ()
91  || m_options.no_gui ()))
92  warning ("--no-line-editing option given, disabling GUI");
93 
94  return false;
95  }
96 
97  if (m_options.force_gui ())
98  return true;
99 
100  if (m_options.no_gui ())
101  return false;
102 
103  if (m_options.persist ())
104  return true;
105 
106  // If stdin is not a tty, then assume we are reading commands from a
107  // pipe or a redirected file and the GUI should not start. If this
108  // is not the case (for example, starting from a desktop "launcher"
109  // with no terminal) and you want to start the GUI, you may use the
110  // --force-gui option to start the GUI.
111 
112  if (! octave_isatty_wrapper (fileno (stdin)))
113  return false;
114 
115  // If we have code to eval or execute from a file, and we are going
116  // to exit immediately after executing it, don't start the gui.
117 
118  std::string code_to_eval = m_options.code_to_eval ();
119  if (! code_to_eval.empty () || m_have_script_file)
120  return false;
121 
122  return true;
123  }
124 
126  {
128 
130 
131  std::string show_gui_msgs =
132  octave::sys::env::getenv ("OCTAVE_SHOW_GUI_MESSAGES");
133 
134  // Installing our handler suppresses the messages.
135 
136  if (show_gui_msgs.empty ())
137  {
138 #if defined (HAVE_QT4)
139  qInstallMsgHandler (message_handler);
140 #else
141  qInstallMessageHandler (message_handler);
142 #endif
143  }
144 
145 #if defined (HAVE_QT_GRAPHICS)
147 
149 #endif
150 
151  // If START_GUI is false, we still set up the QApplication so that
152  // we can use Qt widgets for plot windows.
153 
154  QApplication qt_app (m_argc, m_argv);
155  QTranslator gui_tr, qt_tr, qsci_tr;
156 
157  // Set the codec for all strings (before wizard or any GUI object)
158 #if ! defined (Q_OS_WIN32)
159  QTextCodec::setCodecForLocale (QTextCodec::codecForName ("UTF-8"));
160 #endif
161 
162 #if defined (HAVE_QT4)
163  QTextCodec::setCodecForCStrings (QTextCodec::codecForName ("UTF-8"));
164 #endif
165 
166  // set windows style for windows
167 #if defined (Q_OS_WIN32)
168  qt_app.setStyle(QStyleFactory::create("Windows"));
169 #endif
170 
171  bool start_gui = start_gui_p ();
172 
173  // Show welcome wizard if this is the first run.
174 
175  if (resource_manager::is_first_run () && start_gui)
176  {
177  // Before wizard.
178  resource_manager::config_translators (&qt_tr, &qsci_tr, &gui_tr);
179 
180  qt_app.installTranslator (&qt_tr);
181  qt_app.installTranslator (&gui_tr);
182  qt_app.installTranslator (&qsci_tr);
183 
184  welcome_wizard welcomeWizard;
185 
186  if (welcomeWizard.exec () == QDialog::Rejected)
187  exit (1);
188 
189  // Install settings file.
191  }
192  else
193  {
194  // Get settings file.
196 
197  // After settings.
198  resource_manager::config_translators (&qt_tr, &qsci_tr, &gui_tr);
199 
200  qt_app.installTranslator (&qt_tr);
201  qt_app.installTranslator (&gui_tr);
202 
203  if (start_gui)
204  qt_app.installTranslator (&qsci_tr);
205  }
206 
207  if (start_gui)
208  {
210 
211  // We provide specific terminal capabilities, so ensure that
212  // TERM is always set appropriately.
213 
214 #if defined (OCTAVE_USE_WINDOWS_API)
215  octave::sys::env::putenv ("TERM", "cygwin");
216 #else
217  octave::sys::env::putenv ("TERM", "xterm");
218 #endif
219 
221  }
222 
223  // Force left-to-right alignment (see bug #46204)
224  qt_app.setLayoutDirection (Qt::LeftToRight);
225 
226  // Create and show main window.
227 
228  main_window w (0, this);
229 
230  if (start_gui)
231  {
232  w.read_settings ();
233 
234  w.init_terminal_size ();
235 
236  // Connect signals for changes in visibility not before w
237  // is shown.
238 
240 
242 
243  gui_running (true);
244  }
245  else
246  qt_app.setQuitOnLastWindowClosed (false);
247 
248  return qt_app.exec ();
249  }
250 }
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
static void putenv(const std::string &name, const std::string &value)
Definition: oct-env.cc:242
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
void set_application_id(void)
Definition: sysdep.cc:184
void read_settings(void)
bool gui_running(void) const
Definition: octave-gui.h:44
static void message_handler(QtMsgType, const QMessageLogContext &, const QString &)
Definition: octave-gui.cc:68
static void config_translators(QTranslator *, QTranslator *, QTranslator *)
static std::string getenv(const std::string &name)
Definition: oct-env.cc:235
int octave_isatty_wrapper(int fd)
void install___init_qt___functions(void)
Definition: __init_qt__.cc:145
std::complex< double > w(std::complex< double > z, double relerr=0)
void connect_visibility_changed(void)
bool start_gui_p(void) const
Definition: octave-gui.cc:74
void warning(const char *fmt,...)
Definition: error.cc:788
void focus_command_window(void)
Definition: main-window.cc:228
static bool display_available(void)
Definition: display.h:72
OCTINTERP_API octave_value_list Fregister_graphics_toolkit(const octave_value_list &=octave_value_list(), int=0)
Definition: graphics.cc:10991
static void update_network_settings(void)
void init_terminal_size(void)
static bool is_first_run(void)
static void block_interrupt_signal(void)
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
static void reload_settings(void)
static void init_data()