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
__init_qt__.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2017 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 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 <QDir>
29 #include <QFileDialog>
30 #include <QMetaType>
31 #include <QPalette>
32 #include <QRegExp>
33 
34 #include "graphics.h"
35 #include "interpreter.h"
36 #include "defun.h"
37 
38 #include "Backend.h"
39 #include "QtHandlesUtils.h"
40 
41 namespace QtHandles
42 {
43 
44  static bool qtHandlesInitialized = false;
45 
46  bool
47  __init__ (void)
48  {
49  if (! qtHandlesInitialized)
50  {
51  if (qApp)
52  {
54 
55  qRegisterMetaType<graphics_object> ("graphics_object");
56 
58 
59  graphics_toolkit tk (new Backend ());
61 
62  octave_add_atexit_function ("__shutdown_qt__");
63 
64  // Change some default settings to use Qt default colors
65  QPalette p;
67 
68  /*
69  root.set ("defaultfigurecolor",
70  octave_value (Utils::toRgb (p.color (QPalette::Window))));
71  */
72  root.set ("defaultuicontrolbackgroundcolor",
73  octave_value (Utils::toRgb (p.color (QPalette::Window))));
74  root.set ("defaultuicontrolforegroundcolor",
76  (p.color (QPalette::WindowText))));
77  root.set ("defaultuipanelbackgroundcolor",
78  octave_value (Utils::toRgb (p.color (QPalette::Window))));
79  root.set ("defaultuipanelforegroundcolor",
81  (p.color (QPalette::WindowText))));
82  root.set ("defaultuipanelhighlightcolor",
83  octave_value (Utils::toRgb (p.color (QPalette::Light))));
84  root.set ("defaultuipanelshadowcolor",
85  octave_value (Utils::toRgb (p.color (QPalette::Dark))));
86  root.set ("defaultuibuttongroupbackgroundcolor",
87  octave_value (Utils::toRgb (p.color (QPalette::Window))));
88  root.set ("defaultuibuttongroupforegroundcolor",
90  (p.color (QPalette::WindowText))));
91  root.set ("defaultuibuttongrouphighlightcolor",
92  octave_value (Utils::toRgb (p.color (QPalette::Light))));
93  root.set ("defaultuibuttongroupshadowcolor",
94  octave_value (Utils::toRgb (p.color (QPalette::Dark))));
95 
96  qtHandlesInitialized = true;
97 
98  return true;
99  }
100  else
101  error ("__init_qt__: QApplication object must exist.");
102  }
103 
104  return false;
105  }
106 
107  bool
109  {
110  if (qtHandlesInitialized)
111  {
113 
114  octave_add_atexit_function ("__shutdown_qt__");
115 
117 
119 
120  qtHandlesInitialized = false;
121 
122  return true;
123  }
124 
125  return false;
126  }
127 
128 }
129 
130 DEFUN (__init_qt__, , , "")
131 {
133 
134  return octave_value ();
135 }
136 
137 DEFUN (__shutdown_qt__, , , "")
138 {
140 
141  return octave_value ();
142 }
143 
144 void
146 {
147  install_builtin_function (F__init_qt__, "__init_qt__",
148  "__init_qt__.cc", "");
149 
150  install_builtin_function (F__shutdown_qt__, "__shutdown_qt__",
151  "__init_qt__.cc", "");
152 }
153 
154 #if 0
155 
156 static QStringList
157 makeFilterSpecs (const Cell& filters)
158 {
159  using namespace QtHandles::Utils;
160 
161  QStringList filterSpecs;
162  QRegExp parenRe (" ?\\(.*\\)\\s*$");
163 
164  for (int i = 0; i < filters.rows (); i++)
165  {
166  QStringList extList =
167  fromStdString (filters(i, 0).string_value ()).split (";");
168  QString desc = fromStdString (filters(i, 1).string_value ()).trimmed ();
169  QString specItem;
170 
171  if (desc.contains (parenRe))
172  {
173  // We need to strip any existing parenthesis and recreate it.
174  // In case the format specified in the () section is not correct,
175  // the filters won't work as expected.
176  desc.remove (parenRe);
177  }
178 
179  specItem = QString ("%1 (%2)").arg (desc).arg (extList.join (" "));
180 
181  filterSpecs.append (specItem);
182  }
183 
184  return filterSpecs;
185 }
186 
187 static QString
188 appendDirSep (const QString& d)
189 {
190  if (! d.endsWith ("/") && ! d.endsWith (QDir::separator ()))
191  return (d + "/");
192  return d;
193 }
194 
195 DEFUN (__uigetfile_qt__, args, , "")
196 {
197  using namespace QtHandles::Utils;
198 
199  // Expected arguments:
200  // args(0) : File filter as a cell array {ext1, name1; ext2, name2; ...}
201  // args(1) : Dialog title
202  // args(2) : Default filename
203  // args(3) : Dialog position [ignored]
204  // args(4) : Multiselection "on"/"off"
205  // args(5) : Default directory
206 
208 
209  QString caption = fromStdString (args(1).string_value ());
210  QString defaultDirectory = fromStdString (args(5).string_value ());
211  QString defaultFileName = fromStdString (args(2).string_value ());
212  bool isMultiSelect = (args(4).string_value () == "on");
213 
214  if (isMultiSelect)
215  retval(0) = Cell ();
216  else
217  retval(0) = "";
218  retval(1) = "";
219  retval(2) = 0.0;
220 
221  if (defaultFileName.isEmpty ())
222  defaultFileName = defaultDirectory;
223  else
224  defaultFileName = defaultDirectory + "/" + defaultFileName;
225 
226  QStringList filterSpecs = makeFilterSpecs (args(0).cell_value ());
227 
228  if (isMultiSelect)
229  {
230  QString filter;
231  QStringList files =
232  QFileDialog::getOpenFileNames (0, caption, defaultFileName,
233  filterSpecs.join (";;"), &filter, 0);
234 
235  if (! files.isEmpty ())
236  {
237  Cell cFiles (1, files.length ());
238  QString dirName;
239  int i = 0;
240 
241  foreach (const QString& s, files)
242  {
243  QFileInfo fi (s);
244 
245  if (dirName.isEmpty ())
246  dirName = appendDirSep (fi.canonicalPath ());
247  cFiles(i++) = toStdString (fi.fileName ());
248  }
249 
250  retval(0) = cFiles;
251  retval(1) = toStdString (dirName);
252  if (! filter.isEmpty ())
253  retval(2) = static_cast<double> (filterSpecs.indexOf (filter) + 1);
254  }
255  }
256  else
257  {
258  QString filter;
259  QString fileName =
260  QFileDialog::getOpenFileName (0, caption, defaultFileName,
261  filterSpecs.join (";;"), &filter, 0);
262 
263  if (! fileName.isNull ())
264  {
265  QFileInfo fi (fileName);
266 
267  retval(0) = toStdString (fi.fileName ());
268  retval(1) = toStdString (appendDirSep (fi.canonicalPath ()));
269  if (! filter.isEmpty ())
270  retval(2) = static_cast<double> (filterSpecs.indexOf (filter) + 1);
271  }
272  }
273 
274  return retval;
275 }
276 
277 DEFUN (__uiputfile_qt__, args, , "")
278 {
279  using namespace QtHandles::Utils;
280 
281  // Expected arguments:
282  // args(0) : File filter as a cell array {ext1, name1; ext2, name2; ...}
283  // args(1) : Dialog title
284  // args(2) : Default filename
285  // args(3) : Dialog position [ignored]
286  // args(4) : Tag [ignored]
287  // args(5) : Default directory
288 
290 
291  QString caption = fromStdString (args(1).string_value ());
292  QString defaultDirectory = fromStdString (args(5).string_value ());
293  QString defaultFileName = fromStdString (args(2).string_value ());
294 
295  retval(0) = "";
296  retval(1) = "";
297  retval(2) = 0.0;
298 
299  if (defaultFileName.isEmpty ())
300  defaultFileName = defaultDirectory;
301  else
302  defaultFileName = defaultDirectory + "/" + defaultFileName;
303 
304  QStringList filterSpecs = makeFilterSpecs (args(0).cell_value ());
305 
306  QString filter;
307  QString fileName =
308  QFileDialog::getSaveFileName (0, caption, defaultFileName,
309  filterSpecs.join (";;"), &filter, 0);
310 
311  if (! fileName.isNull ())
312  {
313  QFileInfo fi (fileName);
314 
315  retval(0) = toStdString (fi.fileName ());
316  if (fi.exists ())
317  retval(1) = toStdString (appendDirSep (fi.canonicalPath ()));
318  else
319  retval(1) = toStdString (appendDirSep (fi.absolutePath ()));
320  if (! filter.isEmpty ())
321  retval(2) = static_cast<double> (filterSpecs.indexOf (filter) + 1);
322  }
323 
324  return retval;
325 }
326 
327 DEFUN (__uigetdir_qt__, args, , "")
328 {
329  using namespace QtHandles::Utils;
330 
331  // Expected arguments:
332  // args(0) : Start directory
333  // args(1) : Dialog title
334 
335  octave_value retval ("");
336 
337  QString caption = fromStdString (args(1).string_value ());
338  QString defaultDirectory = fromStdString (args(0).string_value ());
339 
340  QString dirName = QFileDialog::getExistingDirectory (0, caption,
341  defaultDirectory);
342 
343  if (! dirName.isNull ())
344  retval = toStdString (dirName);
345 
346  return retval;
347 }
348 
349 #endif
Definition: Cell.h:37
OCTINTERP_API void install_builtin_function(octave_builtin::fcn f, const std::string &name, const std::string &file, const std::string &doc, bool can_hide_function=true)
Definition: defun.cc:83
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:46
void error(const char *fmt,...)
Definition: error.cc:570
QString fromStdString(const std::string &s)
static double fi[256]
Definition: randmtzig.cc:440
static void load_toolkit(const graphics_toolkit &tk)
Definition: graphics.h:2266
s
Definition: file-io.cc:2682
octave_idx_type rows(void) const
Definition: Array.h:401
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
JNIEnv void * args
Definition: ov-java.cc:67
bool __shutdown__(void)
Definition: __init_qt__.cc:108
bool __init__(void)
Definition: __init_qt__.cc:47
void install___init_qt___functions(void)
Definition: __init_qt__.cc:145
Matrix toRgb(const QColor &c)
octave_value retval
Definition: data.cc:6294
static void unload_toolkit(const std::string &name)
Definition: graphics.h:2272
static bool qtHandlesInitialized
Definition: __init_qt__.cc:44
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.h:13941
void set(const caseless_str &name, const octave_value &val)
Definition: graphics.h:3194
OCTAVE_EXPORT octave_value_list F__shutdown_qt__(const octave_value_list &, int)
Definition: __init_qt__.cc:137
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:2981
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
p
Definition: lu.cc:138
static graphics_object get_object(double val)
Definition: graphics.h:13794
OCTAVE_EXPORT octave_value_list F__init_qt__(const octave_value_list &, int)
Definition: __init_qt__.cc:130
std::string toStdString(const QString &s)
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
OCTINTERP_API void octave_add_atexit_function(const std::string &fname)