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
dialog.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013-2017 John W. Eaton
4 Copyright (C) 2013-2016 Daniel J. Sebald
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include "dialog.h"
29 
30 #include <QString>
31 #include <QStringList>
32 #include <QStringListModel>
33 #include <QListView>
34 #include <QFileInfo>
35 // Could replace most of these with #include <QtGui>
36 #include <QMessageBox>
37 #include <QHBoxLayout>
38 #include <QVBoxLayout>
39 #include <QPushButton>
40 #include <QGroupBox>
41 #include <QGridLayout>
42 #include <QLabel>
43 
44 
46 
48  : QObject (), dialog_result (-1), dialog_button (),
49  string_list (new QStringList ()), list_index (new QIntList ()),
50  path_name (new QString ())
51 { }
52 
54 {
55  delete string_list;
56  delete list_index;
57  delete path_name;
58 }
59 
60 void
61 QUIWidgetCreator::dialog_button_clicked (QAbstractButton *button)
62 {
63  // Wait for link thread to go to sleep state.
64  mutex.lock ();
65 
66  // Store the value so that builtin functions can retrieve.
67  if (button)
68  dialog_button = button->text ();
69 
70  // The value should always be 1 for the Octave functions.
71  dialog_result = 1;
72 
73  mutex.unlock ();
74 
75  // Wake up Octave process so that it continues.
76  waitcondition.wakeAll ();
77 }
78 
79 void
81  int button_pressed)
82 {
83  // Wait for link thread to go to sleep state.
84  mutex.lock ();
85 
86  // Store the value so that builtin functions can retrieve.
87  *list_index = selected;
88  dialog_result = button_pressed;
89 
90  mutex.unlock ();
91 
92  // Wake up Octave process so that it continues.
93  waitcondition.wakeAll ();
94 }
95 
96 void
97 QUIWidgetCreator::input_finished (const QStringList& input, int button_pressed)
98 {
99  // Wait for link thread to go to sleep state.
100  mutex.lock ();
101 
102  // Store the value so that builtin functions can retrieve.
103  *string_list = input;
104  dialog_result = button_pressed;
105 
106  mutex.unlock ();
107 
108  // Wake up Octave process so that it continues.
109  waitcondition.wakeAll ();
110 }
111 
112 void
114  const QString& path, int filterindex)
115 {
116  // Wait for link thread to go to sleep state.
117  mutex.lock ();
118 
119  // Store the value so that builtin functions can retrieve.
120  *string_list = files;
121  dialog_result = filterindex;
122  *path_name = path;
123 
124  mutex.unlock ();
125 
126  // Wake up Octave process so that it continues.
127  waitcondition.wakeAll ();
128 }
129 
131  const QString& title,
132  const QString& qsicon,
133  const QStringList& qsbutton,
134  const QString& defbutton,
135  const QStringList& role)
136  : QMessageBox (QMessageBox::NoIcon, title.isEmpty () ? " " : title,
137  message, 0, 0)
138 {
139  // Create a NonModal message.
140  setWindowModality (Qt::NonModal);
141 
142  // Interpret the icon string, because enumeration QMessageBox::Icon can't
143  // easily be made to pass through a signal.
144  QMessageBox::Icon eicon = QMessageBox::NoIcon;
145  if (qsicon == "error")
146  eicon = QMessageBox::Critical;
147  else if (qsicon == "warn")
148  eicon = QMessageBox::Warning;
149  else if (qsicon == "help")
150  eicon = QMessageBox::Information;
151  else if (qsicon == "quest")
152  eicon = QMessageBox::Question;
153  setIcon (eicon);
154 
155  int N = qsbutton.size () < role.size () ? qsbutton.size () : role.size ();
156  if (N == 0)
157  addButton (QMessageBox::Ok);
158  else
159  {
160  for (int i = N-1; i >= 0; i--)
161  {
162  // Interpret the button role string, because enumeration
163  // QMessageBox::ButtonRole can't be made to pass through a signal.
164  QString srole = role.at (i);
165  QMessageBox::ButtonRole erole = QMessageBox::InvalidRole;
166  if (srole == "YesRole")
167  erole = QMessageBox::YesRole;
168  else if (srole == "NoRole")
169  erole = QMessageBox::NoRole;
170  else if (srole == "RejectRole")
171  erole = QMessageBox::RejectRole;
172  else if (srole == "AcceptRole")
173  erole = QMessageBox::AcceptRole;
174 
175  QPushButton *pbutton = addButton (qsbutton.at (i), erole);
176  if (qsbutton.at (i) == defbutton)
177  setDefaultButton (pbutton);
178  // Make the last button the button pressed when <esc> key activated.
179  if (i == N-1)
180  {
181 // FIXME: Why define and then immediately test value?
182 #define ACTIVE_ESCAPE 1
183 #if ACTIVE_ESCAPE
184  setEscapeButton (pbutton);
185 #else
186  setEscapeButton (0);
187 #endif
188 #undef ACTIVE_ESCAPE
189  }
190  }
191  }
192 
193  connect (this, SIGNAL (buttonClicked (QAbstractButton *)),
194  &uiwidget_creator, SLOT (dialog_button_clicked (QAbstractButton *)));
195 }
196 
197 ListDialog::ListDialog (const QStringList& list, const QString& mode,
198  int wd, int ht, const QList<int>& initial,
199  const QString& title, const QStringList& prompt,
200  const QString& ok_string, const QString& cancel_string)
201  : QDialog (), model (new QStringListModel (list))
202 {
203  QListView *view = new QListView;
204  view->setModel (model);
205 
206  if (mode == "single")
207  view->setSelectionMode (QAbstractItemView::SingleSelection);
208  else if (mode == "multiple")
209  view->setSelectionMode (QAbstractItemView::ExtendedSelection);
210  else
211  view->setSelectionMode (QAbstractItemView::NoSelection);
212 
213  selector = view->selectionModel ();
214  int i = 0;
215  for (QList<int>::const_iterator it = initial.begin ();
216  it != initial.end (); it++)
217  {
218  QModelIndex idx = model->index (initial.value (i++) - 1, 0,
219  QModelIndex ());
220  selector->select (idx, QItemSelectionModel::Select);
221  }
222 
223  bool fixed_layout = false;
224  if (wd > 0 && ht > 0)
225  {
226  view->setFixedSize (wd, ht);
227  fixed_layout = true;
228  }
229 
230  view->setEditTriggers (QAbstractItemView::NoEditTriggers);
231 
232  QVBoxLayout *listLayout = new QVBoxLayout;
233  if (! prompt.isEmpty ())
234  {
235  // For now, assume html-like Rich Text. May be incompatible
236  // with something down the road, but just testing capability.
237  QString prompt_string;
238  for (int j = 0; j < prompt.length (); j++)
239  {
240  if (j > 0)
241 // FIXME: Why define and then immediately test value?
242 #define RICH_TEXT 1
243 #if RICH_TEXT
244  prompt_string.append ("<br>");
245 #else
246  prompt_string.append ("\n");
247 #endif
248  prompt_string.append (prompt.at (j));
249  }
250  QLabel *plabel = new QLabel (prompt_string);
251 #if RICH_TEXT
252  plabel->setTextFormat (Qt::RichText);
253 #endif
254 #undef RICH_TEXT
255  listLayout->addWidget (plabel);
256  }
257  listLayout->addWidget (view);
258  QPushButton *select_all = new QPushButton (tr ("Select All"));
259  select_all->setVisible (mode == "multiple");
260  listLayout->addWidget (select_all);
261 
262  QPushButton *buttonOk = new QPushButton (ok_string);
263  QPushButton *buttonCancel = new QPushButton (cancel_string);
264  QHBoxLayout *buttonsLayout = new QHBoxLayout;
265  buttonsLayout->addStretch (1);
266  buttonsLayout->addWidget (buttonOk);
267  buttonsLayout->addWidget (buttonCancel);
268  buttonOk->setDefault (true);
269 
270  QVBoxLayout *mainLayout = new QVBoxLayout;
271  mainLayout->addLayout (listLayout);
272  mainLayout->addSpacing (12);
273  mainLayout->addLayout (buttonsLayout);
274  setLayout (mainLayout);
275  if (fixed_layout)
276  layout ()->setSizeConstraint (QLayout::SetFixedSize);
277 
278  // If empty, make blank rather than use default OS behavior.
279  setWindowTitle (title.isEmpty () ? " " : title);
280 
281  connect (select_all, SIGNAL (clicked ()),
282  view, SLOT (selectAll ()));
283 
284  connect (buttonOk, SIGNAL (clicked ()),
285  this, SLOT (buttonOk_clicked ()));
286 
287  connect (buttonCancel, SIGNAL (clicked ()),
288  this, SLOT (buttonCancel_clicked ()));
289 
290  connect (this, SIGNAL (finish_selection (const QIntList&, int)),
291  &uiwidget_creator,
292  SLOT (list_select_finished (const QIntList&, int)));
293 
294  connect (view, SIGNAL (doubleClicked (const QModelIndex&)),
295  this, SLOT (item_double_clicked (const QModelIndex&)));
296 }
297 
299 {
300  delete model;
301 }
302 
303 void
305 {
306  // Store information about what button was pressed so that builtin
307  // functions can retrieve.
308  QModelIndexList selected_index = selector->selectedIndexes ();
309  QIntList selected_int;
310 
311  for (int i = 0; i < selected_index.size (); i++)
312  selected_int << selected_index.at (i).row () + 1;
313 
314  emit finish_selection (selected_int, 1);
315 
316  done (QDialog::Accepted);
317 }
318 
319 void
321 {
322  // Store information about what button was pressed so that builtin
323  // functions can retrieve.
324  QIntList empty;
325 
326  emit finish_selection (empty, 0);
327 
328  done (QDialog::Rejected);
329 }
330 
331 void
333 {
335 }
336 
337 void
339 {
340  buttonOk_clicked ();
341 }
342 
343 InputDialog::InputDialog (const QStringList& prompt, const QString& title,
344  const QFloatList& nr, const QFloatList& nc,
345  const QStringList& defaults)
346  : QDialog ()
347 {
348 // FIXME: Why define and then immediately test value?
349 #define LINE_EDIT_FOLLOWS_PROMPT 0
350 
351 #if LINE_EDIT_FOLLOWS_PROMPT
352  // Prompt on left followed by input on right.
353  QGridLayout *promptInputLayout = new QGridLayout;
354 #else
355  // Prompt aligned above input.
356  QVBoxLayout *promptInputLayout = new QVBoxLayout;
357 #endif
358  int N_gridrows = prompt.size ();
359  for (int i = 0; i < N_gridrows; i++)
360  {
361  QLabel *label = new QLabel (prompt.at (i));
362  QLineEdit *line_edit = new QLineEdit (defaults.at (i));
363  if (nr.at (i) > 0)
364  {
365  QSize qsize = line_edit->sizeHint ();
366  int intval = qsize.height () * nr.at (i);
367  line_edit->setFixedHeight (intval);
368  if (nc.at (i) > 0)
369  {
370  intval = qsize.height () * nc.at (i) / 2;
371  line_edit->setFixedWidth (intval);
372  }
373  }
374  input_line << line_edit;
375 #if LINE_EDIT_FOLLOWS_PROMPT
376  promptInputLayout->addWidget (label, i + 1, 0);
377  promptInputLayout->addWidget (line_edit, i + 1, 1);
378 #else
379  promptInputLayout->addWidget (label);
380  promptInputLayout->addWidget (line_edit);
381 #endif
382  }
383 #undef LINE_EDIT_FOLLOWS_PROMPT
384 
385  QPushButton *buttonOk = new QPushButton ("OK");
386  QPushButton *buttonCancel = new QPushButton ("Cancel");
387  QHBoxLayout *buttonsLayout = new QHBoxLayout;
388  buttonsLayout->addStretch (1);
389  buttonsLayout->addWidget (buttonOk);
390  buttonsLayout->addWidget (buttonCancel);
391 
392  QVBoxLayout *mainLayout = new QVBoxLayout;
393  mainLayout->addLayout (promptInputLayout);
394  mainLayout->addSpacing (12);
395  mainLayout->addLayout (buttonsLayout);
396  setLayout (mainLayout);
397 
398  // If empty, make blank rather than use default OS behavior.
399  setWindowTitle (title.isEmpty () ? " " : title);
400 
401  connect (buttonOk, SIGNAL (clicked ()),
402  this, SLOT (buttonOk_clicked ()));
403 
404  connect (buttonCancel, SIGNAL (clicked ()),
405  this, SLOT (buttonCancel_clicked ()));
406 
407  connect (this, SIGNAL (finish_input (const QStringList&, int)),
408  &uiwidget_creator,
409  SLOT (input_finished (const QStringList&, int)));
410 }
411 
412 void
414 {
415  // Store information about what button was pressed so that builtin
416  // functions can retrieve.
417  QStringList string_result;
418  for (int i = 0; i < input_line.size (); i++)
419  string_result << input_line.at (i)->text ();
420  emit finish_input (string_result, 1);
421  done (QDialog::Accepted);
422 }
423 
424 void
426 {
427  // Store information about what button was pressed so that builtin
428  // functions can retrieve.
429  QStringList empty;
430  emit finish_input (empty, 0);
431  done (QDialog::Rejected);
432 }
433 
434 void
436 {
438 }
439 
440 FileDialog::FileDialog (const QStringList& name_filters, const QString& title,
441  const QString& filename, const QString& dirname,
442  const QString& multimode)
443  : QFileDialog ()
444 {
445  // Create a NonModal message.
446  setWindowModality (Qt::NonModal);
447 
448  setWindowTitle (title.isEmpty () ? " " : title);
449  setDirectory (dirname);
450 
451  if (multimode == "on") // uigetfile multiselect=on
452  {
453  setFileMode (QFileDialog::ExistingFiles);
454  setAcceptMode (QFileDialog::AcceptOpen);
455  }
456  else if (multimode == "create") // uiputfile
457  {
458  setFileMode (QFileDialog::AnyFile);
459  setAcceptMode (QFileDialog::AcceptSave);
460  setOption (QFileDialog::DontConfirmOverwrite, false);
461  setConfirmOverwrite (true);
462  }
463  else if (multimode == "dir") // uigetdir
464  {
465  setFileMode (QFileDialog::Directory);
466  setOption (QFileDialog::ShowDirsOnly, true);
467  setOption (QFileDialog::HideNameFilterDetails, true);
468  setAcceptMode (QFileDialog::AcceptOpen);
469  }
470  else // uigetfile multiselect=off
471  {
472  setFileMode (QFileDialog::ExistingFile);
473  setAcceptMode (QFileDialog::AcceptOpen);
474  }
475 
476  setNameFilters (name_filters);
477 
478  selectFile (filename);
479 
480  connect (this,
481  SIGNAL (finish_input (const QStringList&, const QString&, int)),
482  &uiwidget_creator,
483  SLOT (filedialog_finished (const QStringList&, const QString&,
484  int)));
485  connect (this, SIGNAL (accepted ()), this, SLOT (acceptSelection ()));
486  connect (this, SIGNAL (rejected ()), this, SLOT (rejectSelection ()));
487 }
488 
489 void
491 {
492  QStringList empty;
493  emit finish_input (empty, "", 0);
494 }
495 
497 {
498  QStringList string_result;
499  QString path;
500  int idx = 1;
501 
502  string_result = selectedFiles ();
503 
504  if (testOption (QFileDialog::ShowDirsOnly) == true &&
505  string_result.size () > 0)
506  {
507  path = string_result[0];
508  }
509  else
510  {
511  path = directory ().absolutePath ();
512  }
513 
514  // Matlab expects just the filename, whereas the file dialog gave us
515  // full path names, so fix it.
516 
517  for (int i = 0; i < string_result.size (); i++)
518  string_result[i] = QFileInfo (string_result[i]).fileName ();
519 
520  // if not showing only dirs, add end slash for the path component
521  if (testOption (QFileDialog::ShowDirsOnly) == false)
522  path += "/";
523 
524  // convert to native slashes
525  path = QDir::toNativeSeparators (path);
526 
527  QStringList name_filters = nameFilters ();
528  idx = name_filters.indexOf (selectedNameFilter ()) + 1;
529 
530  // send the selected info
531  emit finish_input (string_result, path, idx);
532 }
~ListDialog(void)
Definition: dialog.cc:298
void item_double_clicked(const QModelIndex &)
Definition: dialog.cc:338
~QUIWidgetCreator(void)
Definition: dialog.cc:53
void list_select_finished(const QIntList &selected, int button_pressed)
Definition: dialog.cc:80
The value of lines which begin with a space character are not saved in the history list A value of all commands are saved on the history list
Definition: oct-hist.cc:728
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6386
void buttonOk_clicked(void)
Definition: dialog.cc:304
void finish_selection(const QIntList &, int)
Definition: moc-dialog.cc:303
QItemSelectionModel * selector
Definition: dialog.h:191
std::string filename
Definition: urlwrite.cc:340
OCTAVE_EXPORT octave_value_list return the value of the option it must match the dimension of the state and the relative tolerance must also be a vector of the same length tem it must match the dimension of the state and the absolute tolerance must also be a vector of the same length The local error test applied at each integration step is xample roup calculate Y_a and Y _d item Given calculate Y nd enumerate In either initial values for the given components are input
Definition: DASPK-opts.cc:739
FileDialog(const QStringList &filters, const QString &title, const QString &filename, const QString &dirname, const QString &multimode)
Definition: dialog.cc:440
QMutex mutex
Definition: dialog.h:120
ListDialog(const QStringList &list, const QString &mode, int width, int height, const QList< int > &initial, const QString &name, const QStringList &prompt, const QString &ok_string, const QString &cancel_string)
Definition: dialog.cc:197
void rejectSelection(void)
Definition: dialog.cc:490
void filedialog_finished(const QStringList &files, const QString &path, int filterindex)
Definition: dialog.cc:113
void reject(void)
Definition: dialog.cc:435
QUIWidgetCreator uiwidget_creator
Definition: dialog.cc:45
void message(const char *name, const char *fmt,...)
Definition: error.cc:430
void reject(void)
Definition: dialog.cc:332
done
Definition: syscalls.cc:248
create a structure array and initialize its values The dimensions of each cell array of values must match Singleton cells and non cell values are repeated so that they fill the entire array If the cells are empty
Definition: ov-struct.cc:1688
QString dialog_button
Definition: dialog.h:153
QString * path_name
Definition: dialog.h:160
void dialog_button_clicked(QAbstractButton *button)
Definition: dialog.cc:61
QIntList * list_index
Definition: dialog.h:158
void buttonCancel_clicked(void)
Definition: dialog.cc:425
F77_RET_T const F77_INT & N
void input_finished(const QStringList &input, int button_pressed)
Definition: dialog.cc:97
QWaitCondition waitcondition
Definition: dialog.h:121
QStringList * string_list
Definition: dialog.h:157
void buttonOk_clicked(void)
Definition: dialog.cc:413
int dialog_result
Definition: dialog.h:152
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
MessageDialog(const QString &message, const QString &title, const QString &icon, const QStringList &button, const QString &defbutton, const QStringList &role)
Definition: dialog.cc:130
InputDialog(const QStringList &prompt, const QString &title, const QFloatList &nr, const QFloatList &nc, const QStringList &defaults)
Definition: dialog.cc:343
void finish_input(const QStringList &, const QString &, int)
Definition: moc-dialog.cc:480
QList< int > QIntList
Definition: dialog.h:38
void acceptSelection(void)
Definition: dialog.cc:496
QAbstractItemModel * model
Definition: dialog.h:218
QList< QLineEdit * > input_line
Definition: dialog.h:225
QUIWidgetCreator(void)
Definition: dialog.cc:47
void buttonCancel_clicked(void)
Definition: dialog.cc:320
void finish_input(const QStringList &, int)
Definition: moc-dialog.cc:393
OCTAVE_EXPORT octave_value_list directory
Definition: variables.cc:582