GNU Octave  8.1.0
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-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <QListWidget>
31 #include <QEvent>
32 #include <QMouseEvent>
33 
34 #include "Container.h"
35 #include "ListBoxControl.h"
36 #include "QtHandlesUtils.h"
37 
38 #include "octave-qobject.h"
39 
41 
42 static void
43 updateSelection (QListWidget *list, const Matrix& value)
44 {
45  octave_idx_type n = value.numel ();
46  int lc = list->count ();
47 
48  list->clearSelection ();
49 
50  for (octave_idx_type i = 0; i < n; i++)
51  {
52  int idx = octave::math::round (value(i));
53 
54  if (1 <= idx && idx <= lc)
55  {
56  list->item (idx-1)->setSelected (true);
57  list->scrollToItem (list->item (idx-1));
58  if (i == 0
59  && list->selectionMode () == QAbstractItemView::SingleSelection)
60  break;
61  }
62  else
63  {
64  // Invalid selection.
65  list->clearSelection ();
66  break;
67  }
68  }
69 }
70 
72 ListBoxControl::create (octave::base_qobject& oct_qobj,
73  octave::interpreter& interp,
74  const graphics_object& go)
75 {
76  Object *parent = parentObject (interp, go);
77 
78  if (parent)
79  {
80  Container *container = parent->innerContainer ();
81 
82  if (container)
83  return new ListBoxControl (oct_qobj, interp, go,
84  new QListWidget (container));
85  }
86 
87  return nullptr;
88 }
89 
90 ListBoxControl::ListBoxControl (octave::base_qobject& oct_qobj,
91  octave::interpreter& interp,
92  const graphics_object& go, QListWidget *list)
93  : BaseControl (oct_qobj, interp, go, list), m_blockCallback (false),
94  m_selectionChanged (false)
95 {
96  uicontrol::properties& up = properties<uicontrol> ();
97 
98  list->addItems (Utils::fromStringVector (up.get_string_vector ()));
99  if ((up.get_max () - up.get_min ()) > 1)
100  list->setSelectionMode (QAbstractItemView::ExtendedSelection);
101  else
102  list->setSelectionMode (QAbstractItemView::SingleSelection);
103  Matrix value = up.get_value ().matrix_value ();
104  if (value.numel () > 0)
105  {
106  octave_idx_type n = value.numel ();
107  int lc = list->count ();
108 
109  for (octave_idx_type i = 0; i < n; i++)
110  {
111  int idx = octave::math::round (value(i));
112 
113  if (1 <= idx && idx <= lc)
114  {
115  list->item (idx-1)->setSelected (true);
116  list->scrollToItem (list->item (idx-1));
117  if (i == 0 && (list->selectionMode ()
118  == QAbstractItemView::SingleSelection))
119  break;
120  }
121  }
122  }
123 
124  list->viewport ()->installEventFilter (this);
125 
126  connect (list, &QListWidget::itemSelectionChanged,
128  connect (list, &QListWidget::activated,
130  connect (list, &QListWidget::itemPressed,
132 }
133 
135 { }
136 
137 void
139 {
140  uicontrol::properties& up = properties<uicontrol> ();
141  QListWidget *list = qWidget<QListWidget> ();
142 
143  switch (pId)
144  {
145  case uicontrol::properties::ID_STRING:
146  m_blockCallback = true;
147  list->clear ();
148  list->addItems (Utils::fromStringVector (up.get_string_vector ()));
149  updateSelection (list, up.get_value ().matrix_value ());
150  m_blockCallback = false;
151  break;
152 
153  case uicontrol::properties::ID_MIN:
154  case uicontrol::properties::ID_MAX:
155  if ((up.get_max () - up.get_min ()) > 1)
156  list->setSelectionMode (QAbstractItemView::ExtendedSelection);
157  else
158  list->setSelectionMode (QAbstractItemView::SingleSelection);
159  break;
160 
161  case uicontrol::properties::ID_LISTBOXTOP:
162  {
163  int idx = octave::math::fix (up.get_listboxtop ());
164  if (idx > 0)
165  list->scrollToItem (list->item (idx-1),
166  QAbstractItemView::PositionAtTop);
167  break;
168  }
169 
170  case uicontrol::properties::ID_VALUE:
171  m_blockCallback = true;
172  updateSelection (list, up.get_value ().matrix_value ());
173  m_blockCallback = false;
174  break;
175 
176  default:
177  BaseControl::update (pId);
178  break;
179  }
180 }
181 
182 void
184 {
185  if (! m_blockCallback)
186  {
187  QListWidget *list = qWidget<QListWidget> ();
188 
189  QModelIndexList l = list->selectionModel ()->selectedIndexes ();
190  Matrix value (dim_vector (1, l.size ()));
191  int i = 0;
192 
193  for (const auto& idx : l)
194  value(i++) = idx.row () + 1;
195 
196  emit gh_set_event (m_handle, "value", octave_value (value), false);
197  emit gh_callback_event (m_handle, "callback");
198  }
199 
200  m_selectionChanged = false;
201 }
202 
203 void
205 {
206  if (! m_blockCallback)
207  m_selectionChanged = true;
208 }
209 
210 void
211 ListBoxControl::itemActivated (const QModelIndex&)
212 {
213  m_selectionChanged = true;
214 }
215 void
216 ListBoxControl::itemPressed (QListWidgetItem *)
217 {
218  m_selectionChanged = true;
219 }
220 
221 bool
223 {
224  // listbox change
225  if (watched == m_qobject)
226  {
227  switch (e->type ())
228  {
229  case QEvent::KeyRelease:
230  if (m_selectionChanged)
232  m_selectionChanged = false;
233  break;
234 
235  default:
236  break;
237  }
238 
239  return Object::eventFilter (watched, e);
240  }
241  // listbox viewport
242  else
243  {
244  bool override_return = false;
245  QListWidget *list = qWidget<QListWidget> ();
246 
247  switch (e->type ())
248  {
249  case QEvent::MouseButtonPress:
250  {
251  QMouseEvent *m = dynamic_cast<QMouseEvent *> (e);
252 
253  if (m->button () & Qt::RightButton)
254  override_return = true;
255  else
256  {
257  if (! list->indexAt (m->pos ()).isValid ())
258  override_return = true;
259  m_selectionChanged = true;
260  }
261  break;
262  }
263  case QEvent::MouseButtonRelease:
264  {
265  QMouseEvent *m = dynamic_cast<QMouseEvent *> (e);
266 
267  if (m->button () & Qt::RightButton)
268  override_return = true;
269 
270  else if (! list->indexAt (m->pos ()).isValid ())
271  {
272  list->setCurrentRow (list->count () - 1);
273  override_return = true;
274  }
275 
276  if (m_selectionChanged)
278  m_selectionChanged = false;
279 
280  break;
281  }
282  default:
283  break;
284 
285  }
286  return BaseControl::eventFilter (watched, e) || override_return;
287  }
288 }
289 
OCTAVE_END_NAMESPACE(octave)
static void updateSelection(QListWidget *list, const Matrix &value)
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:414
bool eventFilter(QObject *watched, QEvent *e)
Definition: BaseControl.cc:236
void update(int pId)
Definition: BaseControl.cc:164
void itemSelectionChanged(void)
void update(int pId)
ListBoxControl(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QListWidget *list)
void itemPressed(QListWidgetItem *)
void itemActivated(const QModelIndex &)
static ListBoxControl * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
bool eventFilter(QObject *watched, QEvent *e)
void sendSelectionChange()
Definition: dMatrix.h:42
OCTAVE_API RowVector row(octave_idx_type i) const
Definition: dMatrix.cc:416
Definition: Object.h:47
void gh_callback_event(const graphics_handle &h, const std::string &name)
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:200
graphics_handle m_handle
Definition: Object.h:153
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
QObject * m_qobject
Definition: Object.h:155
virtual Container * innerContainer(void)=0
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
double round(double x)
Definition: lo-mappers.h:136
double fix(double x)
Definition: lo-mappers.h:118
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753
QStringList fromStringVector(const string_vector &v)
T::properties & properties(graphics_object obj)