GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ListBoxControl.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 <QListWidget>
28 #include <QEvent>
29 #include <QMouseEvent>
30 
31 #include "Container.h"
32 #include "ListBoxControl.h"
33 #include "QtHandlesUtils.h"
34 
35 namespace QtHandles
36 {
37 
38  static void
39  updateSelection (QListWidget *list, const Matrix& value)
40  {
41  octave_idx_type n = value.numel ();
42  int lc = list->count ();
43 
44  list->clearSelection ();
45 
46  for (octave_idx_type i = 0; i < n; i++)
47  {
48  int idx = octave::math::round (value(i));
49 
50  if (1 <= idx && idx <= lc)
51  {
52  list->item (idx-1)->setSelected (true);
53  list->scrollToItem (list->item (idx-1));
54  if (i == 0
55  && list->selectionMode () == QAbstractItemView::SingleSelection)
56  break;
57  }
58  else
59  {
60  // Invalid selection.
61  list->clearSelection ();
62  break;
63  }
64  }
65  }
66 
67  ListBoxControl*
69  {
70  Object *parent = Object::parentObject (go);
71 
72  if (parent)
73  {
74  Container *container = parent->innerContainer ();
75 
76  if (container)
77  return new ListBoxControl (go, new QListWidget (container));
78  }
79 
80  return nullptr;
81  }
82 
84  : BaseControl (go, list), m_blockCallback (false), m_selectionChanged (false)
85  {
86  uicontrol::properties& up = properties<uicontrol> ();
87 
88  list->addItems (Utils::fromStringVector (up.get_string_vector ()));
89  if ((up.get_max () - up.get_min ()) > 1)
90  list->setSelectionMode (QAbstractItemView::ExtendedSelection);
91  else
92  list->setSelectionMode (QAbstractItemView::SingleSelection);
93  Matrix value = up.get_value ().matrix_value ();
94  if (value.numel () > 0)
95  {
96  octave_idx_type n = value.numel ();
97  int lc = list->count ();
98 
99  for (octave_idx_type i = 0; i < n; i++)
100  {
101  int idx = octave::math::round (value(i));
102 
103  if (1 <= idx && idx <= lc)
104  {
105  list->item (idx-1)->setSelected (true);
106  list->scrollToItem (list->item (idx-1));
107  if (i == 0 && (list->selectionMode ()
108  == QAbstractItemView::SingleSelection))
109  break;
110  }
111  }
112  }
113 
114  list->viewport ()->installEventFilter (this);
115 
116  connect (list, SIGNAL (itemSelectionChanged (void)),
117  SLOT (itemSelectionChanged (void)));
118  connect (list, SIGNAL (activated (const QModelIndex &)),
119  SLOT (itemActivated (const QModelIndex &)));
120  connect (list, SIGNAL (itemPressed (QListWidgetItem*)),
121  SLOT (itemPressed (QListWidgetItem*)));
122  }
123 
125  { }
126 
127  void
129  {
130  uicontrol::properties& up = properties<uicontrol> ();
131  QListWidget *list = qWidget<QListWidget> ();
132 
133  switch (pId)
134  {
135  case uicontrol::properties::ID_STRING:
136  m_blockCallback = true;
137  list->clear ();
138  list->addItems (Utils::fromStringVector (up.get_string_vector ()));
139  updateSelection (list, up.get_value ().matrix_value ());
140  m_blockCallback = false;
141  break;
142 
143  case uicontrol::properties::ID_MIN:
144 
145  case uicontrol::properties::ID_MAX:
146  if ((up.get_max () - up.get_min ()) > 1)
147  list->setSelectionMode (QAbstractItemView::ExtendedSelection);
148  else
149  list->setSelectionMode (QAbstractItemView::SingleSelection);
150  break;
151 
152  case uicontrol::properties::ID_VALUE:
153  m_blockCallback = true;
154  updateSelection (list, up.get_value ().matrix_value ());
155  m_blockCallback = false;
156  break;
157 
158  default:
159  BaseControl::update (pId);
160  break;
161  }
162  }
163 
164  void
166  {
167  if (! m_blockCallback)
168  {
169  QListWidget *list = qWidget<QListWidget> ();
170 
171  QModelIndexList l = list->selectionModel ()->selectedIndexes ();
172  Matrix value (dim_vector (1, l.size ()));
173  int i = 0;
174 
175  foreach (const QModelIndex& idx, l)
176  value(i++) = idx.row () + 1;
177 
178  gh_manager::post_set (m_handle, "value", octave_value (value), false);
179  gh_manager::post_callback (m_handle, "callback");
180  }
181 
182  m_selectionChanged = false;
183  }
184 
185  void
187  {
188  if (! m_blockCallback)
189  m_selectionChanged = true;
190  }
191 
192  void
193  ListBoxControl::itemActivated (const QModelIndex &)
194  {
195  m_selectionChanged = true;
196  }
197  void
198  ListBoxControl::itemPressed (QListWidgetItem*)
199  {
200  m_selectionChanged = true;
201  }
202 
203  bool
205  {
206  // listbox change
207  if (watched == m_qobject)
208  {
209  switch (e->type ())
210  {
211  case QEvent::KeyRelease:
212  if (m_selectionChanged)
214  m_selectionChanged = false;
215  break;
216 
217  default:
218  break;
219  }
220 
221  return Object::eventFilter (watched, e);
222  }
223  // listbox viewport
224  else
225  {
226  bool override_return = false;
227  QListWidget *list = qWidget<QListWidget> ();
228 
229  switch (e->type ())
230  {
231  case QEvent::MouseButtonPress:
232  {
233  QMouseEvent *m = dynamic_cast<QMouseEvent *> (e);
234 
235  if (m->button () & Qt::RightButton)
236  override_return = true;
237  else
238  {
239  if (! list->indexAt (m->pos ()).isValid ())
240  override_return = true;
241  m_selectionChanged = true;
242  }
243  break;
244  }
245  case QEvent::MouseButtonRelease:
246  {
247  QMouseEvent *m = dynamic_cast<QMouseEvent *> (e);
248 
249  if (m->button () & Qt::RightButton)
250  override_return = true;
251 
252  else if (! list->indexAt (m->pos ()).isValid ())
253  {
254  list->setCurrentRow (list->count () - 1);
255  override_return = true;
256  }
257 
258  if (m_selectionChanged)
260  m_selectionChanged = false;
261 
262  break;
263  }
264  default:
265  break;
266 
267  }
268  return BaseControl::eventFilter (watched, e) || override_return;
269  }
270  }
271 }
static void post_callback(const graphics_handle &h, const std::string &name, const octave_value &data=Matrix())
Definition: graphics.in.h:6214
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:734
bool eventFilter(QObject *watched, QEvent *e)
Definition: BaseControl.cc:180
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:165
ListBoxControl(const graphics_object &go, QListWidget *list)
QStringList fromStringVector(const string_vector &v)
virtual Container * innerContainer(void)=0
i e
Definition: data.cc:2591
void itemActivated(const QModelIndex &)
graphics_handle m_handle
Definition: Object.h:115
is false
Definition: cellfun.cc:400
QObject * m_qobject
Definition: Object.h:117
Definition: dMatrix.h:36
void update(int pId)
Definition: BaseControl.cc:124
static ListBoxControl * create(const graphics_object &go)
bool eventFilter(QObject *watched, QEvent *e)
double round(double x)
Definition: lo-mappers.h:145
void itemPressed(QListWidgetItem *)
for i
Definition: data.cc:5264
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
static void updateSelection(QListWidget *list, const Matrix &value)
nd group nd example For each display the value
Definition: sysdep.cc:866
static void post_set(const graphics_handle &h, const std::string &name, const octave_value &value, bool notify_toolkit=true)
Definition: graphics.in.h:6228