GNU Octave 7.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-2022 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
40namespace octave
41{
42
43 static void
44 updateSelection (QListWidget *list, const Matrix& value)
45 {
46 octave_idx_type n = value.numel ();
47 int lc = list->count ();
48
49 list->clearSelection ();
50
51 for (octave_idx_type i = 0; i < n; i++)
52 {
53 int idx = octave::math::round (value(i));
54
55 if (1 <= idx && idx <= lc)
56 {
57 list->item (idx-1)->setSelected (true);
58 list->scrollToItem (list->item (idx-1));
59 if (i == 0
60 && list->selectionMode () == QAbstractItemView::SingleSelection)
61 break;
62 }
63 else
64 {
65 // Invalid selection.
66 list->clearSelection ();
67 break;
68 }
69 }
70 }
71
72 ListBoxControl *
74 octave::interpreter& interp,
75 const graphics_object& go)
76 {
77 Object *parent = parentObject (interp, go);
78
79 if (parent)
80 {
81 Container *container = parent->innerContainer ();
82
83 if (container)
84 return new ListBoxControl (oct_qobj, interp, go,
85 new QListWidget (container));
86 }
87
88 return nullptr;
89 }
90
92 octave::interpreter& interp,
93 const graphics_object& go, QListWidget *list)
94 : BaseControl (oct_qobj, interp, go, list), m_blockCallback (false),
95 m_selectionChanged (false)
96 {
97 uicontrol::properties& up = properties<uicontrol> ();
98
99 list->addItems (Utils::fromStringVector (up.get_string_vector ()));
100 if ((up.get_max () - up.get_min ()) > 1)
101 list->setSelectionMode (QAbstractItemView::ExtendedSelection);
102 else
103 list->setSelectionMode (QAbstractItemView::SingleSelection);
104 Matrix value = up.get_value ().matrix_value ();
105 if (value.numel () > 0)
106 {
107 octave_idx_type n = value.numel ();
108 int lc = list->count ();
109
110 for (octave_idx_type i = 0; i < n; i++)
111 {
112 int idx = octave::math::round (value(i));
113
114 if (1 <= idx && idx <= lc)
115 {
116 list->item (idx-1)->setSelected (true);
117 list->scrollToItem (list->item (idx-1));
118 if (i == 0 && (list->selectionMode ()
119 == QAbstractItemView::SingleSelection))
120 break;
121 }
122 }
123 }
124
125 list->viewport ()->installEventFilter (this);
126
127 connect (list, &QListWidget::itemSelectionChanged,
129 connect (list, &QListWidget::activated,
131 connect (list, &QListWidget::itemPressed,
133 }
134
136 { }
137
138 void
140 {
141 uicontrol::properties& up = properties<uicontrol> ();
142 QListWidget *list = qWidget<QListWidget> ();
143
144 switch (pId)
145 {
146 case uicontrol::properties::ID_STRING:
147 m_blockCallback = true;
148 list->clear ();
149 list->addItems (Utils::fromStringVector (up.get_string_vector ()));
150 updateSelection (list, up.get_value ().matrix_value ());
151 m_blockCallback = false;
152 break;
153
154 case uicontrol::properties::ID_MIN:
155 case uicontrol::properties::ID_MAX:
156 if ((up.get_max () - up.get_min ()) > 1)
157 list->setSelectionMode (QAbstractItemView::ExtendedSelection);
158 else
159 list->setSelectionMode (QAbstractItemView::SingleSelection);
160 break;
161
162 case uicontrol::properties::ID_LISTBOXTOP:
163 {
164 int idx = octave::math::fix (up.get_listboxtop ());
165 if (idx > 0)
166 list->scrollToItem (list->item (idx-1),
167 QAbstractItemView::PositionAtTop);
168 break;
169 }
170
171 case uicontrol::properties::ID_VALUE:
172 m_blockCallback = true;
173 updateSelection (list, up.get_value ().matrix_value ());
174 m_blockCallback = false;
175 break;
176
177 default:
179 break;
180 }
181 }
182
183 void
185 {
186 if (! m_blockCallback)
187 {
188 QListWidget *list = qWidget<QListWidget> ();
189
190 QModelIndexList l = list->selectionModel ()->selectedIndexes ();
191 Matrix value (dim_vector (1, l.size ()));
192 int i = 0;
193
194 for (const auto& idx : l)
195 value(i++) = idx.row () + 1;
196
197 emit gh_set_event (m_handle, "value", octave_value (value), false);
198 emit gh_callback_event (m_handle, "callback");
199 }
200
201 m_selectionChanged = false;
202 }
203
204 void
206 {
207 if (! m_blockCallback)
208 m_selectionChanged = true;
209 }
210
211 void
212 ListBoxControl::itemActivated (const QModelIndex&)
213 {
214 m_selectionChanged = true;
215 }
216 void
217 ListBoxControl::itemPressed (QListWidgetItem *)
218 {
219 m_selectionChanged = true;
220 }
221
222 bool
224 {
225 // listbox change
226 if (watched == m_qobject)
227 {
228 switch (e->type ())
229 {
230 case QEvent::KeyRelease:
233 m_selectionChanged = false;
234 break;
235
236 default:
237 break;
238 }
239
240 return Object::eventFilter (watched, e);
241 }
242 // listbox viewport
243 else
244 {
245 bool override_return = false;
246 QListWidget *list = qWidget<QListWidget> ();
247
248 switch (e->type ())
249 {
250 case QEvent::MouseButtonPress:
251 {
252 QMouseEvent *m = dynamic_cast<QMouseEvent *> (e);
253
254 if (m->button () & Qt::RightButton)
255 override_return = true;
256 else
257 {
258 if (! list->indexAt (m->pos ()).isValid ())
259 override_return = true;
260 m_selectionChanged = true;
261 }
262 break;
263 }
264 case QEvent::MouseButtonRelease:
265 {
266 QMouseEvent *m = dynamic_cast<QMouseEvent *> (e);
267
268 if (m->button () & Qt::RightButton)
269 override_return = true;
270
271 else if (! list->indexAt (m->pos ()).isValid ())
272 {
273 list->setCurrentRow (list->count () - 1);
274 override_return = true;
275 }
276
279 m_selectionChanged = false;
280
281 break;
282 }
283 default:
284 break;
285
286 }
287 return BaseControl::eventFilter (watched, e) || override_return;
288 }
289 }
290}
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:411
Definition: dMatrix.h:42
OCTAVE_API RowVector row(octave_idx_type i) const
Definition: dMatrix.cc:416
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
bool eventFilter(QObject *watched, QEvent *e)
Definition: BaseControl.cc:237
void update(int pId)
Definition: BaseControl.cc:165
void itemPressed(QListWidgetItem *)
bool eventFilter(QObject *watched, QEvent *e)
ListBoxControl(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QListWidget *list)
void itemActivated(const QModelIndex &)
static ListBoxControl * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
virtual Container * innerContainer(void)=0
void gh_callback_event(const graphics_handle &h, const std::string &name)
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
graphics_handle m_handle
Definition: Object.h:157
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:201
QObject * m_qobject
Definition: Object.h:159
Base class for Octave interfaces that use Qt.
QStringList fromStringVector(const string_vector &v)
T::properties & properties(graphics_object obj)
double fix(double x)
Definition: lo-mappers.h:118
double round(double x)
Definition: lo-mappers.h:136
static void updateSelection(QListWidget *list, const Matrix &value)