GNU Octave  4.0.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
__init_qt__.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2015 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 #ifdef 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 "toplev.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 
87  qtHandlesInitialized = true;
88 
89  return true;
90  }
91  else
92  error ("__init_qt__: QApplication object must exist.");
93  }
94 
95  return false;
96 }
97 
98 bool
100 {
101  if (qtHandlesInitialized)
102  {
104 
105  octave_add_atexit_function ("__shutdown_qt__");
106 
108 
110 
111  qtHandlesInitialized = false;
112 
113  return true;
114  }
115 
116  return false;
117 }
118 
119 }; // namespace QtHandles
120 
121 DEFUN (__init_qt__, , , "")
122 {
124 
125  return octave_value ();
126 }
127 
128 DEFUN (__shutdown_qt__, , , "")
129 {
131 
132  return octave_value ();
133 }
134 
135 void
137 {
138  install_builtin_function (F__init_qt__, "__init_qt__",
139  "__init_qt__.cc", "");
140 
141  install_builtin_function (F__shutdown_qt__, "__shutdown_qt__",
142  "__init_qt__.cc", "");
143 }
144 
145 #if 0
146 
147 static QStringList
148 makeFilterSpecs (const Cell& filters)
149 {
150  using namespace QtHandles::Utils;
151 
152  QStringList filterSpecs;
153  QRegExp parenRe (" ?\\(.*\\)\\s*$");
154 
155  for (int i = 0; i < filters.rows (); i++)
156  {
157  QStringList extList =
158  fromStdString (filters(i, 0).string_value ()).split (";");
159  QString desc = fromStdString (filters(i, 1).string_value ()).trimmed ();
160  QString specItem;
161 
162  if (desc.contains (parenRe))
163  {
164  // We need to strip any existing parenthesis and recreate it.
165  // In case the format specified in the () section is not correct,
166  // the filters won't work as expected.
167  desc.remove (parenRe);
168  }
169 
170  specItem = QString ("%1 (%2)").arg (desc).arg (extList.join (" "));
171 
172  filterSpecs.append (specItem);
173  }
174 
175  return filterSpecs;
176 }
177 
178 static QString
179 appendDirSep (const QString& d)
180 {
181  if (! d.endsWith ("/") && ! d.endsWith (QDir::separator ()))
182  return (d + "/");
183  return d;
184 }
185 
186 DEFUN (__uigetfile_qt__, args, , "")
187 {
188  using namespace QtHandles::Utils;
189 
190  // Expected arguments:
191  // args(0) : File filter as a cell array {ext1, name1; ext2, name2; ...}
192  // args(1) : Dialog title
193  // args(2) : Default file name
194  // args(3) : Dialog position [ignored]
195  // args(4) : Multiselection "on"/"off"
196  // args(5) : Default directory
197 
198  octave_value_list retval (3);
199 
200  QString caption = fromStdString (args(1).string_value ());
201  QString defaultDirectory = fromStdString (args(5).string_value ());
202  QString defaultFileName = fromStdString (args(2).string_value ());
203  bool isMultiSelect = (args(4).string_value () == "on");
204 
205  if (isMultiSelect)
206  retval(0) = Cell ();
207  else
208  retval(0) = "";
209  retval(1) = "";
210  retval(2) = 0.0;
211 
212  if (defaultFileName.isEmpty ())
213  defaultFileName = defaultDirectory;
214  else
215  defaultFileName = defaultDirectory + "/" + defaultFileName;
216 
217  QStringList filterSpecs = makeFilterSpecs (args(0).cell_value ());
218 
219  if (isMultiSelect)
220  {
221  QString filter;
222  QStringList files =
223  QFileDialog::getOpenFileNames (0, caption, defaultFileName,
224  filterSpecs.join (";;"), &filter, 0);
225 
226  if (! files.isEmpty ())
227  {
228  Cell cFiles (1, files.length ());
229  QString dirName;
230  int i = 0;
231 
232  foreach (const QString& s, files)
233  {
234  QFileInfo fi (s);
235 
236  if (dirName.isEmpty ())
237  dirName = appendDirSep (fi.canonicalPath ());
238  cFiles(i++) = toStdString (fi.fileName ());
239  }
240 
241  retval(0) = cFiles;
242  retval(1) = toStdString (dirName);
243  if (! filter.isEmpty ())
244  retval(2) = static_cast<double> (filterSpecs.indexOf (filter) + 1);
245  }
246  }
247  else
248  {
249  QString filter;
250  QString fileName =
251  QFileDialog::getOpenFileName (0, caption, defaultFileName,
252  filterSpecs.join (";;"), &filter, 0);
253 
254  if (! fileName.isNull ())
255  {
256  QFileInfo fi (fileName);
257 
258  retval(0) = toStdString (fi.fileName ());
259  retval(1) = toStdString (appendDirSep (fi.canonicalPath ()));
260  if (! filter.isEmpty ())
261  retval(2) = static_cast<double> (filterSpecs.indexOf (filter) + 1);
262  }
263  }
264 
265  return retval;
266 }
267 
268 DEFUN (__uiputfile_qt__, args, , "")
269 {
270  using namespace QtHandles::Utils;
271 
272  // Expected arguments:
273  // args(0) : File filter as a cell array {ext1, name1; ext2, name2; ...}
274  // args(1) : Dialog title
275  // args(2) : Default file name
276  // args(3) : Dialog position [ignored]
277  // args(4) : Tag [ignored]
278  // args(5) : Default directory
279 
280  octave_value_list retval (3);
281 
282  QString caption = fromStdString (args(1).string_value ());
283  QString defaultDirectory = fromStdString (args(5).string_value ());
284  QString defaultFileName = fromStdString (args(2).string_value ());
285 
286  retval(0) = "";
287  retval(1) = "";
288  retval(2) = 0.0;
289 
290  if (defaultFileName.isEmpty ())
291  defaultFileName = defaultDirectory;
292  else
293  defaultFileName = defaultDirectory + "/" + defaultFileName;
294 
295  QStringList filterSpecs = makeFilterSpecs (args(0).cell_value ());
296 
297  QString filter;
298  QString fileName =
299  QFileDialog::getSaveFileName (0, caption, defaultFileName,
300  filterSpecs.join (";;"), &filter, 0);
301 
302  if (! fileName.isNull ())
303  {
304  QFileInfo fi (fileName);
305 
306  retval(0) = toStdString (fi.fileName ());
307  if (fi.exists ())
308  retval(1) = toStdString (appendDirSep (fi.canonicalPath ()));
309  else
310  retval(1) = toStdString (appendDirSep (fi.absolutePath ()));
311  if (! filter.isEmpty ())
312  retval(2) = static_cast<double> (filterSpecs.indexOf (filter) + 1);
313  }
314 
315  return retval;
316 }
317 
318 DEFUN (__uigetdir_qt__, args, , "")
319 {
320  using namespace QtHandles::Utils;
321 
322  // Expected arguments:
323  // args(0) : Start directory
324  // args(1) : Dialog title
325 
326  octave_value retval ("");
327 
328  QString caption = fromStdString (args(1).string_value ());
329  QString defaultDirectory = fromStdString (args(0).string_value ());
330 
331  QString dirName = QFileDialog::getExistingDirectory (0, caption,
332  defaultDirectory);
333 
334  if (! dirName.isNull ())
335  retval = toStdString (dirName);
336 
337  return retval;
338 }
339 
340 #endif
Definition: Cell.h:35
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:82
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:44
void error(const char *fmt,...)
Definition: error.cc:476
QString fromStdString(const std::string &s)
static void load_toolkit(const graphics_toolkit &tk)
Definition: graphics.h:2313
octave_idx_type rows(void) const
Definition: Array.h:313
F77_RET_T const double const double double * d
bool __shutdown__(void)
Definition: __init_qt__.cc:99
bool __init__(void)
Definition: __init_qt__.cc:47
void install___init_qt___functions(void)
Definition: __init_qt__.cc:136
Matrix toRgb(const QColor &c)
MArray< double > filter(MArray< double > &, MArray< double > &, MArray< double > &, int dim)
static void unload_toolkit(const std::string &name)
Definition: graphics.h:2319
static bool qtHandlesInitialized
Definition: __init_qt__.cc:44
static void enable_event_processing(bool enable=true)
Definition: graphics.h:13360
void set(const caseless_str &name, const octave_value &val)
Definition: graphics.h:3283
OCTAVE_EXPORT octave_value_list F__shutdown_qt__(const octave_value_list &, int)
Definition: __init_qt__.cc:128
void octave_add_atexit_function(const std::string &fname)
Definition: toplev.cc:1147
static double fi[256]
Definition: randmtzig.c:443
static graphics_object get_object(double val)
Definition: graphics.h:13212
OCTAVE_EXPORT octave_value_list F__init_qt__(const octave_value_list &, int)
Definition: __init_qt__.cc:121
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())? '\'': '"'))