GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
PopupMenuControl.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 <QComboBox>
28 
29 #include "Container.h"
30 #include "PopupMenuControl.h"
31 #include "QtHandlesUtils.h"
32 
33 namespace QtHandles
34 {
35 
36  PopupMenuControl*
38  {
39  Object *parent = Object::parentObject (go);
40 
41  if (parent)
42  {
43  Container *container = parent->innerContainer ();
44 
45  if (container)
46  return new PopupMenuControl (go, new QComboBox (container));
47  }
48 
49  return nullptr;
50  }
51 
53  : BaseControl (go, box), m_blockUpdate (false)
54  {
55  uicontrol::properties& up = properties<uicontrol> ();
56 
57  box->addItems (Utils::fromStdString (up.get_string_string ()).split ('|'));
58 
59  update (uicontrol::properties::ID_VALUE);
60 
61  connect (box, SIGNAL (activated (int)),
62  SLOT (currentIndexChanged (int)));
63  }
64 
66  { }
67 
68  void PopupMenuControl::update (int pId)
69  {
70  uicontrol::properties& up = properties<uicontrol> ();
71  QComboBox *box = qWidget<QComboBox> ();
72 
73  switch (pId)
74  {
75  case uicontrol::properties::ID_STRING:
76  m_blockUpdate = true;
77  {
78  int oldCurrent = box->currentIndex ();
79 
80  box->clear ();
81  box->addItems (Utils::fromStdString
82  (up.get_string_string ()).split ('|'));
83  if (box->count () > 0
84  && oldCurrent >= 0
85  && oldCurrent < box->count ())
86  {
87  box->setCurrentIndex (oldCurrent);
88  }
89  else
90  {
92  octave_value (box->count () > 0
93  ? 1.0 : 0.0),
94  false);
95  }
96  }
97  m_blockUpdate = false;
98  break;
99 
100  case uicontrol::properties::ID_VALUE:
101  m_blockUpdate = true;
102  {
103  Matrix value = up.get_value ().matrix_value ();
104 
105  if (value.numel () > 0)
106  {
107  if (value(0) != static_cast<int>(value(0)))
108  warning ("popupmenu value should be integer");
109  else
110  {
111  int newIndex = int (value(0)) - 1;
112 
113  if (newIndex >= 0 && newIndex < box->count ())
114  {
115  if (newIndex != box->currentIndex ())
116  box->setCurrentIndex (newIndex);
117  }
118  else
119  warning ("popupmenu value not within valid display range");
120  }
121  }
122  }
123  m_blockUpdate = false;
124  break;
125 
126  default:
127  BaseControl::update (pId);
128  break;
129  }
130  }
131 
132  void
134  {
135  if (! m_blockUpdate)
136  {
137  gh_manager::post_set (m_handle, "value",
138  octave_value (double (index + 1)),
139  false);
140  gh_manager::post_callback (m_handle, "callback");
141  }
142  }
143 
144 }
static PopupMenuControl * create(const graphics_object &go)
static void post_callback(const graphics_handle &h, const std::string &name, const octave_value &data=Matrix())
Definition: graphics.in.h:6214
QString fromStdString(const std::string &s)
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:165
virtual Container * innerContainer(void)=0
static octave_value box(JNIEnv *jni_env, void *jobj, void *jcls_arg=nullptr)
graphics_handle m_handle
Definition: Object.h:115
is false
Definition: cellfun.cc:400
Definition: dMatrix.h:36
PopupMenuControl(const graphics_object &go, QComboBox *box)
void warning(const char *fmt,...)
Definition: error.cc:801
void update(int pId)
Definition: BaseControl.cc:124
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