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
ListBoxControl.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2017 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <QListWidget>
28 
29 #include "Container.h"
30 #include "ListBoxControl.h"
31 #include "QtHandlesUtils.h"
32 
33 namespace QtHandles
34 {
35 
36  static void
37  updateSelection (QListWidget* list, const Matrix& value)
38  {
39  octave_idx_type n = value.numel ();
40  int lc = list->count ();
41 
42  list->clearSelection ();
43 
44  for (octave_idx_type i = 0; i < n; i++)
45  {
46  int idx = octave::math::round (value(i));
47 
48  if (1 <= idx && idx <= lc)
49  {
50  list->item (idx-1)->setSelected (true);
51  list->scrollToItem (list->item (idx-1));
52  if (i == 0
53  && list->selectionMode () == QAbstractItemView::SingleSelection)
54  break;
55  }
56  else
57  {
58  // Invalid selection.
59  list->clearSelection ();
60  break;
61  }
62  }
63  }
64 
65  ListBoxControl*
67  {
68  Object* parent = Object::parentObject (go);
69 
70  if (parent)
71  {
72  Container* container = parent->innerContainer ();
73 
74  if (container)
75  return new ListBoxControl (go, new QListWidget (container));
76  }
77 
78  return 0;
79  }
80 
82  : BaseControl (go, list), m_blockCallback (false)
83  {
84  uicontrol::properties& up = properties<uicontrol> ();
85 
86  list->addItems (Utils::fromStringVector (up.get_string_vector ()));
87  if ((up.get_max () - up.get_min ()) > 1)
88  list->setSelectionMode (QAbstractItemView::ExtendedSelection);
89  else
90  list->setSelectionMode (QAbstractItemView::SingleSelection);
92  if (value.numel () > 0)
93  {
94  octave_idx_type n = value.numel ();
95  int lc = list->count ();
96 
97  for (octave_idx_type i = 0; i < n; i++)
98  {
99  int idx = octave::math::round (value(i));
100 
101  if (1 <= idx && idx <= lc)
102  {
103  list->item (idx-1)->setSelected (true);
104  list->scrollToItem (list->item (idx-1));
105  if (i == 0 && (list->selectionMode ()
106  == QAbstractItemView::SingleSelection))
107  break;
108  }
109  }
110  }
111 
112  list->removeEventFilter (this);
113  list->viewport ()->installEventFilter (this);
114 
115  connect (list, SIGNAL (itemSelectionChanged (void)),
116  SLOT (itemSelectionChanged (void)));
117  }
118 
120  { }
121 
122  void
124  {
125  uicontrol::properties& up = properties<uicontrol> ();
126  QListWidget* list = qWidget<QListWidget> ();
127 
128  switch (pId)
129  {
131  m_blockCallback = true;
132  list->clear ();
133  list->addItems (Utils::fromStringVector (up.get_string_vector ()));
134  updateSelection (list, up.get_value ().matrix_value ());
135  m_blockCallback = false;
136  break;
137 
139 
141  if ((up.get_max () - up.get_min ()) > 1)
142  list->setSelectionMode (QAbstractItemView::ExtendedSelection);
143  else
144  list->setSelectionMode (QAbstractItemView::SingleSelection);
145  break;
146 
148  m_blockCallback = true;
149  updateSelection (list, up.get_value ().matrix_value ());
150  m_blockCallback = false;
151  break;
152 
153  default:
154  BaseControl::update (pId);
155  break;
156  }
157  }
158 
159  void
161  {
162  if (! m_blockCallback)
163  {
164  QListWidget* list = qWidget<QListWidget> ();
165 
166  QModelIndexList l = list->selectionModel ()->selectedIndexes ();
167  Matrix value (dim_vector (1, l.size ()));
168  int i = 0;
169 
170  foreach (const QModelIndex& idx, l)
171  value(i++) = (idx.row () + 1);
172 
173  gh_manager::post_set (m_handle, "value", octave_value (value), false);
174  gh_manager::post_callback (m_handle, "callback");
175  }
176  }
177 
178 }
static void post_callback(const graphics_handle &h, const std::string &name, const octave_value &data=Matrix())
Definition: graphics.h:13910
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
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
string_vector get_string_vector(void) const
Definition: graphics.h:11978
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
double round(double x)
Definition: lo-mappers.cc:333
graphics_handle m_handle
Definition: Object.h:100
is false
Definition: cellfun.cc:398
Definition: dMatrix.h:37
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:787
double get_min(void) const
Definition: graphics.h:11971
void update(int pId)
Definition: BaseControl.cc:124
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the IEEE symbol zero divided by nd tex zero divided by nd ifnottex and any operation involving another NaN value(5+NaN).Note that NaN always compares not equal to NaN(NaN!
static ListBoxControl * create(const graphics_object &go)
octave_value get_value(void) const
Definition: graphics.h:11989
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
double get_max(void) const
Definition: graphics.h:11969
static void updateSelection(QListWidget *list, const Matrix &value)
static void post_set(const graphics_handle &h, const std::string &name, const octave_value &value, bool notify_toolkit=true)
Definition: graphics.h:13924