GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
__init_qt__.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2018 Michael Goffioul
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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave 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
15 GNU General Public License 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 <https://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 <QDir>
29 #include <QFileDialog>
30 #include <QMetaType>
31 #include <QPalette>
32 #include <QRegExp>
33 
34 #include "defun.h"
35 #include "graphics.h"
36 #include "gtk-manager.h"
37 #include "interpreter.h"
38 #include "symtab.h"
39 
40 #include "Backend.h"
41 #include "QtHandlesUtils.h"
42 #include "__init_qt__.h"
43 
44 namespace QtHandles
45 {
46 
47  static bool qtHandlesInitialized = false;
48 
49  bool
51  {
53  {
54  if (qApp)
55  {
57 
58  qRegisterMetaType<graphics_object> ("graphics_object");
59 
61 
62  octave::gtk_manager& gtk_mgr = interp.get_gtk_manager ();
63 
64  graphics_toolkit tk (new Backend ());
65  gtk_mgr.load_toolkit (tk);
66 
67  octave::interpreter::add_atexit_function ("__shutdown_qt__");
68 
69  qtHandlesInitialized = true;
70 
71  return true;
72  }
73  else
74  error ("__init_qt__: QApplication object must exist.");
75  }
76 
77  return false;
78  }
79 
80  bool
81  __shutdown__ (void)
82  {
84  {
86 
88 
89  qtHandlesInitialized = false;
90 
91  return true;
92  }
93 
94  return false;
95  }
96 }
97 
98 DEFMETHOD (__init_qt__, interp, , , "")
99 {
100  QtHandles::__init__ (interp);
101 
102  return octave_value ();
103 }
104 
105 DEFUN (__shutdown_qt__, , , "")
106 {
108 
109  return octave_value ();
110 }
111 
112 void
114 {
116  ("__init_qt__", octave_value (new octave_builtin
117  (F__init_qt__, "__init_qt__",
118  "__init_qt__.cc", "")));
119 
121  ("__shutdown_qt__", octave_value (new octave_builtin
122  (F__shutdown_qt__, "__shutdown_qt__",
123  "__init_qt__.cc", "")));
124 }
125 
126 #if 0
127 
128 static QStringList
129 makeFilterSpecs (const Cell& filters)
130 {
131  using namespace QtHandles::Utils;
132 
133  QStringList filterSpecs;
134  QRegExp parenRe (" ?\\(.*\\)\\s*$");
135 
136  for (int i = 0; i < filters.rows (); i++)
137  {
138  QStringList extList =
139  fromStdString (filters(i, 0).string_value ()).split (";");
140  QString desc = fromStdString (filters(i, 1).string_value ()).trimmed ();
141  QString specItem;
142 
143  if (desc.contains (parenRe))
144  {
145  // We need to strip any existing parenthesis and recreate it.
146  // In case the format specified in the () section is not correct,
147  // the filters won't work as expected.
148  desc.remove (parenRe);
149  }
150 
151  specItem = QString ("%1 (%2)").arg (desc).arg (extList.join (" "));
152 
153  filterSpecs.append (specItem);
154  }
155 
156  return filterSpecs;
157 }
158 
159 static QString
160 appendDirSep (const QString& d)
161 {
162  if (! d.endsWith ("/") && ! d.endsWith (QDir::separator ()))
163  return (d + '/');
164  return d;
165 }
166 
167 DEFUN (__uigetfile_qt__, args, , "")
168 {
169  using namespace QtHandles::Utils;
170 
171  // Expected arguments:
172  // args(0) : File filter as a cell array {ext1, name1; ext2, name2; ...}
173  // args(1) : Dialog title
174  // args(2) : Default filename
175  // args(3) : Dialog position [ignored]
176  // args(4) : Multiselection "on"/"off"
177  // args(5) : Default directory
178 
180 
181  QString caption = fromStdString (args(1).string_value ());
182  QString defaultDirectory = fromStdString (args(5).string_value ());
183  QString defaultFileName = fromStdString (args(2).string_value ());
184  bool isMultiSelect = (args(4).string_value () == "on");
185 
186  if (isMultiSelect)
187  retval(0) = Cell ();
188  else
189  retval(0) = "";
190  retval(1) = "";
191  retval(2) = 0.0;
192 
193  if (defaultFileName.isEmpty ())
194  defaultFileName = defaultDirectory;
195  else
196  defaultFileName = defaultDirectory + '/' + defaultFileName;
197 
198  QStringList filterSpecs = makeFilterSpecs (args(0).cell_value ());
199 
200  if (isMultiSelect)
201  {
202  QString filter;
203  QStringList files =
204  QFileDialog::getOpenFileNames (0, caption, defaultFileName,
205  filterSpecs.join (";;"), &filter, 0);
206 
207  if (! files.isEmpty ())
208  {
209  Cell cFiles (1, files.length ());
210  QString dirName;
211  int i = 0;
212 
213  foreach (const QString& s, files)
214  {
215  QFileInfo fi (s);
216 
217  if (dirName.isEmpty ())
218  dirName = appendDirSep (fi.canonicalPath ());
219  cFiles(i++) = toStdString (fi.fileName ());
220  }
221 
222  retval(0) = cFiles;
223  retval(1) = toStdString (dirName);
224  if (! filter.isEmpty ())
225  retval(2) = static_cast<double> (filterSpecs.indexOf (filter) + 1);
226  }
227  }
228  else
229  {
230  QString filter;
231  QString fileName =
232  QFileDialog::getOpenFileName (0, caption, defaultFileName,
233  filterSpecs.join (";;"), &filter, 0);
234 
235  if (! fileName.isNull ())
236  {
237  QFileInfo fi (fileName);
238 
239  retval(0) = toStdString (fi.fileName ());
240  retval(1) = toStdString (appendDirSep (fi.canonicalPath ()));
241  if (! filter.isEmpty ())
242  retval(2) = static_cast<double> (filterSpecs.indexOf (filter) + 1);
243  }
244  }
245 
246  return retval;
247 }
248 
249 DEFUN (__uiputfile_qt__, args, , "")
250 {
251  using namespace QtHandles::Utils;
252 
253  // Expected arguments:
254  // args(0) : File filter as a cell array {ext1, name1; ext2, name2; ...}
255  // args(1) : Dialog title
256  // args(2) : Default filename
257  // args(3) : Dialog position [ignored]
258  // args(4) : Tag [ignored]
259  // args(5) : Default directory
260 
262 
263  QString caption = fromStdString (args(1).string_value ());
264  QString defaultDirectory = fromStdString (args(5).string_value ());
265  QString defaultFileName = fromStdString (args(2).string_value ());
266 
267  retval(0) = "";
268  retval(1) = "";
269  retval(2) = 0.0;
270 
271  if (defaultFileName.isEmpty ())
272  defaultFileName = defaultDirectory;
273  else
274  defaultFileName = defaultDirectory + '/' + defaultFileName;
275 
276  QStringList filterSpecs = makeFilterSpecs (args(0).cell_value ());
277 
278  QString filter;
279  QString fileName =
280  QFileDialog::getSaveFileName (0, caption, defaultFileName,
281  filterSpecs.join (";;"), &filter, 0);
282 
283  if (! fileName.isNull ())
284  {
285  QFileInfo fi (fileName);
286 
287  retval(0) = toStdString (fi.fileName ());
288  if (fi.exists ())
289  retval(1) = toStdString (appendDirSep (fi.canonicalPath ()));
290  else
291  retval(1) = toStdString (appendDirSep (fi.absolutePath ()));
292  if (! filter.isEmpty ())
293  retval(2) = static_cast<double> (filterSpecs.indexOf (filter) + 1);
294  }
295 
296  return retval;
297 }
298 
299 DEFUN (__uigetdir_qt__, args, , "")
300 {
301  using namespace QtHandles::Utils;
302 
303  // Expected arguments:
304  // args(0) : Start directory
305  // args(1) : Dialog title
306 
307  octave_value retval ("");
308 
309  QString caption = fromStdString (args(1).string_value ());
310  QString defaultDirectory = fromStdString (args(0).string_value ());
311 
312  QString dirName = QFileDialog::getExistingDirectory (0, caption,
313  defaultDirectory);
314 
315  if (! dirName.isNull ())
316  retval = toStdString (dirName);
317 
318  return retval;
319 }
320 
321 #endif
octave_idx_type rows(void) const
Definition: Array.h:404
OCTAVE_EXPORT octave_value_list F__init_qt__(octave::interpreter &interp, const octave_value_list &, int)
Definition: __init_qt__.cc:98
Definition: Cell.h:37
#define DEFMETHOD(name, interp_name, args_name, nargout_name, doc)
Macro to define a builtin method.
Definition: defun.h:135
void load_toolkit(const graphics_toolkit &tk)
Definition: gtk-manager.h:54
void install_built_in_function(const std::string &name, const octave_value &fcn)
Definition: symtab.h:347
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
void error(const char *fmt,...)
Definition: error.cc:578
QString fromStdString(const std::string &s)
static double fi[256]
Definition: randmtzig.cc:435
s
Definition: file-io.cc:2729
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
gtk_manager & get_gtk_manager(void)
Definition: interpreter.h:207
bool __shutdown__(void)
Definition: __init_qt__.cc:81
void install___init_qt___functions(octave::symbol_table &symtab)
Definition: __init_qt__.cc:113
octave_value retval
Definition: data.cc:6246
static bool qtHandlesInitialized
Definition: __init_qt__.cc:47
MArray< T > filter(MArray< T > &b, MArray< T > &a, MArray< T > &x, MArray< T > &si, int dim=0)
Definition: filter.cc:43
static void enable_event_processing(bool enable=true)
Definition: graphics.in.h:6245
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
OCTAVE_EXPORT octave_value_list F__shutdown_qt__(const octave_value_list &, int)
Definition: __init_qt__.cc:105
bool __init__(octave::interpreter &interp)
Definition: __init_qt__.cc:50
otherwise an error message is printed The permission mask is a UNIX concept used when creating new objects on a file system such as files
Definition: file-io.cc:3038
static void add_atexit_function(const std::string &fname)
for i
Definition: data.cc:5264
std::string toStdString(const QString &s)