GNU Octave  4.0.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
ListBoxControl.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2015 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 #ifdef 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 = xround (value(i));
47 
48  if (1 <= idx && idx <= lc)
49  {
50  list->item (idx-1)->setSelected (true);
51  if (i == 0
52  && list->selectionMode () == QAbstractItemView::SingleSelection)
53  break;
54  }
55  else
56  {
57  // Invalid selection.
58  list->clearSelection ();
59  break;
60  }
61  }
62 }
63 
64 ListBoxControl*
66 {
67  Object* parent = Object::parentObject (go);
68 
69  if (parent)
70  {
71  Container* container = parent->innerContainer ();
72 
73  if (container)
74  return new ListBoxControl (go, new QListWidget (container));
75  }
76 
77  return 0;
78 }
79 
80 ListBoxControl::ListBoxControl (const graphics_object& go, QListWidget* list)
81  : BaseControl (go, list), m_blockCallback (false)
82 {
83  uicontrol::properties& up = properties<uicontrol> ();
84 
85  list->addItems (Utils::fromStringVector (up.get_string_vector ()));
86  if ((up.get_max () - up.get_min ()) > 1)
87  list->setSelectionMode (QAbstractItemView::ExtendedSelection);
88  else
89  list->setSelectionMode (QAbstractItemView::SingleSelection);
90  Matrix value = up.get_value ().matrix_value ();
91  if (value.numel () > 0)
92  {
93  octave_idx_type n = value.numel ();
94  int lc = list->count ();
95 
96  for (octave_idx_type i = 0; i < n; i++)
97  {
98  int idx = xround (value(i));
99 
100  if (1 <= idx && idx <= lc)
101  {
102  list->item (idx-1)->setSelected (true);
103  if (i == 0 && (list->selectionMode ()
104  == QAbstractItemView::SingleSelection))
105  break;
106  }
107  }
108  }
109 
110  list->removeEventFilter (this);
111  list->viewport ()->installEventFilter (this);
112 
113  connect (list, SIGNAL (itemSelectionChanged (void)),
114  SLOT (itemSelectionChanged (void)));
115 }
116 
118 {
119 }
120 
121 void
123 {
124  uicontrol::properties& up = properties<uicontrol> ();
125  QListWidget* list = qWidget<QListWidget> ();
126 
127  switch (pId)
128  {
130  m_blockCallback = true;
131  list->clear ();
132  list->addItems (Utils::fromStringVector (up.get_string_vector ()));
133  updateSelection (list, up.get_value ().matrix_value ());
134  m_blockCallback = false;
135  break;
136 
138 
140  if ((up.get_max () - up.get_min ()) > 1)
141  list->setSelectionMode (QAbstractItemView::ExtendedSelection);
142  else
143  list->setSelectionMode (QAbstractItemView::SingleSelection);
144  break;
145 
147  m_blockCallback = true;
148  updateSelection (list, up.get_value ().matrix_value ());
149  m_blockCallback = false;
150  break;
151 
152  default:
153  BaseControl::update (pId);
154  break;
155  }
156 }
157 
158 void
160 {
161  if (! m_blockCallback)
162  {
163  QListWidget* list = qWidget<QListWidget> ();
164 
165  QModelIndexList l = list->selectionModel ()->selectedIndexes ();
166  Matrix value (dim_vector (1, l.size ()));
167  int i = 0;
168 
169  foreach (const QModelIndex& idx, l)
170  value(i++) = (idx.row () + 1);
171 
172  gh_manager::post_set (m_handle, "value", octave_value (value), false);
173  gh_manager::post_callback (m_handle, "callback");
174  }
175 }
176 
177 }; // namespace QtHandles
static void post_callback(const graphics_handle &h, const std::string &name, const octave_value &data=Matrix())
Definition: graphics.h:13329
double xround(double x)
Definition: lo-mappers.cc:63
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
string_vector get_string_vector(void) const
Definition: graphics.h:11746
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:170
ListBoxControl(const graphics_object &go, QListWidget *list)
QStringList fromStringVector(const string_vector &v)
virtual Container * innerContainer(void)=0
graphics_handle m_handle
Definition: Object.h:100
Definition: dMatrix.h:35
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:773
double get_min(void) const
Definition: graphics.h:11739
void update(int pId)
Definition: BaseControl.cc:106
static ListBoxControl * create(const graphics_object &go)
octave_value get_value(void) const
Definition: graphics.h:11757
double get_max(void) const
Definition: graphics.h:11737
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:13343