GNU Octave  3.8.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
dialog.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013 John W. Eaton
4 Copyright (C) 2013 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 (octave_dialog_h)
25 #define octave_dialog_h 1
26 
27 #include <QMutex>
28 #include <QWaitCondition>
29 #include <QAbstractButton>
30 #include <QList>
31 #include <QItemSelectionModel>
32 #include <QDialog>
33 #include <QMessageBox>
34 #include <QLineEdit>
35 #include <QFileDialog>
36 
37 // Defined for purposes of sending QList<int> as part of signal.
39 
40 // Defined for purposes of sending QList<float> as part of signal.
42 
43 
44 class QUIWidgetCreator : public QObject
45 {
46  Q_OBJECT
47 
48 public:
49 
50  QUIWidgetCreator (void);
51 
52  ~QUIWidgetCreator (void);
53 
54 public:
55 
56  void signal_dialog (const QString& message, const QString& title,
57  const QString& icon, const QStringList& button,
58  const QString& defbutton, const QStringList& role)
59  {
60 
61  // Use the last button in the list as the reject result, i.e., when no
62  // button is pressed such as in the case of the upper right close tab.
63  if (!button.isEmpty ())
64  dialog_button = button.last ();
65 
66  QString xicon = icon;
67  if (xicon.isEmpty ())
68  xicon = "none";
69 
70  emit create_dialog (message, title, xicon, button, defbutton, role);
71  };
72 
73  int get_dialog_result (void) { return dialog_result; }
74 
75  QString get_dialog_button (void) { return dialog_button; }
76 
77  bool signal_listview (const QStringList& list, const QString& mode,
78  int wd, int ht, const QList<int>& initial,
79  const QString& name, const QStringList& prompt,
80  const QString& ok_string, const QString& cancel_string)
81  {
82  if (list.isEmpty ())
83  return false;
84 
85  emit create_listview (list, mode, wd, ht, initial, name,
86  prompt, ok_string, cancel_string);
87 
88  return true;
89  };
90 
91  const QIntList *get_list_index (void) { return list_index; }
92 
93  bool signal_inputlayout (const QStringList& prompt, const QString& title,
94  const QFloatList& nr, const QFloatList& nc,
95  const QStringList& defaults)
96  {
97  if (prompt.isEmpty ())
98  return false;
99 
100  emit create_inputlayout (prompt, title, nr, nc, defaults);
101 
102  return true;
103  };
104 
105  const QStringList *get_string_list (void) { return string_list; }
106 
107  bool signal_filedialog (const QStringList& filters, const QString& title,
108  const QString& filename, const QString& dirname,
109  const QString &multimode)
110  {
111  emit create_filedialog (filters, title, filename, dirname, multimode);
112  return true;
113  }
114 
115  const QString *get_dialog_path (void) { return path_name; }
116 
117  void wait (void)
118  {
119  // Wait while the user is responding to message box.
120  waitcondition.wait (&mutex);
121  }
122 
123 signals:
124 
125  void create_dialog (const QString&, const QString&, const QString&,
126  const QStringList&, const QString&, const QStringList&);
127 
128  void create_listview (const QStringList&, const QString&, int, int,
129  const QIntList&, const QString&, const QStringList&,
130  const QString&, const QString&);
131 
132  void create_inputlayout (const QStringList&, const QString&,
133  const QFloatList&, const QFloatList&,
134  const QStringList&);
135 
136  void create_filedialog (const QStringList& filters, const QString& title,
137  const QString& filename, const QString& dirname,
138  const QString& multimode);
139 public slots:
140 
141  void dialog_button_clicked (QAbstractButton *button);
142 
143  void list_select_finished (const QIntList& selected, int button_pressed);
144 
145  void input_finished (const QStringList& input, int button_pressed);
146 
147  void filedialog_finished (const QStringList& files, const QString& path,
148  int filterindex);
149 
150 private:
151 
153  QString dialog_button;
154 
155  // The list could conceivably be big. Not sure how things are
156  // stored internally, so keep off of the stack.
157  QStringList *string_list;
159 
160  QString *path_name;
161 
162  // GUI objects cannot be accessed in the non-GUI thread. However,
163  // signals can be sent to slots across threads with proper
164  // synchronization. Hence, the use of QWaitCondition.
165 
166  QMutex mutex;
167 
168  QWaitCondition waitcondition;
169 };
170 
172 
173 
175 {
176  Q_OBJECT
177 
178 public:
179 
180  explicit MessageDialog (const QString& message, const QString& title,
181  const QString& icon, const QStringList& button,
182  const QString& defbutton,
183  const QStringList& role);
184 
185 private:
186 
187  void closeEvent (QCloseEvent *)
188  {
189  // Reroute the close tab to a button click so there is only a single
190  // route to waking the wait condition.
191  emit buttonClicked (0);
192  }
193 };
194 
195 
196 class ListDialog : public QDialog
197 {
198  Q_OBJECT
199 
200  QItemSelectionModel *selector;
201 
202 public:
203 
204  explicit ListDialog (const QStringList& list, const QString& mode,
205  int width, int height, const QList<int>& initial,
206  const QString& name, const QStringList& prompt,
207  const QString& ok_string, const QString& cancel_string);
208 
209 signals:
210 
211  void finish_selection (const QIntList&, int);
212 
213 public slots:
214 
215  void buttonOk_clicked (void);
216 
217  void buttonCancel_clicked (void);
218 
219  void reject (void);
220 };
221 
222 
223 class InputDialog : public QDialog
224 {
225  Q_OBJECT
226 
228 
229 public:
230 
231  explicit InputDialog (const QStringList& prompt, const QString& title,
232  const QFloatList& nr, const QFloatList& nc,
233  const QStringList& defaults);
234 
235 signals:
236 
237  void finish_input (const QStringList&, int);
238 
239 public slots:
240 
241  void buttonOk_clicked (void);
242 
243  void buttonCancel_clicked (void);
244 
245  void reject (void);
246 };
247 
248 class FileDialog : public QFileDialog
249 {
250  Q_OBJECT
251 
252 public:
253 
254  explicit FileDialog (const QStringList& filters,
255  const QString& title, const QString& filename,
256  const QString& dirname, const QString& multimode);
257 
258 signals:
259 
260  void finish_input (const QStringList&, const QString&, int);
261 
262 private slots:
263 
264  void reject (void);
265 
266  void accept (void);
267 };
268 
269 #endif