GNU Octave  6.2.0
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-2021 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 <QComboBox>
31 
32 #include "Container.h"
33 #include "PopupMenuControl.h"
34 #include "QtHandlesUtils.h"
35 
36 #include "octave-qobject.h"
37 
38 namespace QtHandles
39 {
40 
41  PopupMenuControl*
43  octave::interpreter& interp,
44  const graphics_object& go)
45  {
46  Object *parent = parentObject (interp, go);
47 
48  if (parent)
49  {
50  Container *container = parent->innerContainer ();
51 
52  if (container)
53  return new PopupMenuControl (oct_qobj, interp, go,
54  new QComboBox (container));
55  }
56 
57  return nullptr;
58  }
59 
61  octave::interpreter& interp,
62  const graphics_object& go,
63  QComboBox *box)
64  : BaseControl (oct_qobj, interp, go, box), m_blockUpdate (false)
65  {
66  uicontrol::properties& up = properties<uicontrol> ();
67 
68  box->addItems (Utils::fromStdString (up.get_string_string ()).split ('|'));
69 
70  update (uicontrol::properties::ID_VALUE);
71 
72  connect (box, SIGNAL (activated (int)),
73  SLOT (currentIndexChanged (int)));
74  }
75 
77  { }
78 
79  void PopupMenuControl::update (int pId)
80  {
81  uicontrol::properties& up = properties<uicontrol> ();
82  QComboBox *box = qWidget<QComboBox> ();
83 
84  switch (pId)
85  {
86  case uicontrol::properties::ID_STRING:
87  m_blockUpdate = true;
88  {
89  int oldCurrent = box->currentIndex ();
90 
91  box->clear ();
92  box->addItems (Utils::fromStdString
93  (up.get_string_string ()).split ('|'));
94  if (box->count () > 0
95  && oldCurrent >= 0
96  && oldCurrent < box->count ())
97  {
98  box->setCurrentIndex (oldCurrent);
99  }
100  else
101  {
102  emit gh_set_event (m_handle, "value",
103  octave_value (box->count () > 0 ? 1.0 : 0.0),
104  false);
105  }
106  }
107  m_blockUpdate = false;
108  break;
109 
110  case uicontrol::properties::ID_VALUE:
111  m_blockUpdate = true;
112  {
113  Matrix value = up.get_value ().matrix_value ();
114 
115  if (value.numel () > 0)
116  {
117  if (value(0) != static_cast<int> (value(0)))
118  warning ("popupmenu value should be integer");
119  else
120  {
121  int newIndex = int (value(0)) - 1;
122 
123  if (newIndex >= 0 && newIndex < box->count ())
124  {
125  if (newIndex != box->currentIndex ())
126  box->setCurrentIndex (newIndex);
127  }
128  else
129  warning ("popupmenu value not within valid display range");
130  }
131  }
132  }
133  m_blockUpdate = false;
134  break;
135 
136  default:
137  BaseControl::update (pId);
138  break;
139  }
140  }
141 
142  void
144  {
145  if (! m_blockUpdate)
146  {
147  emit gh_set_event (m_handle, "value", octave_value (double (index + 1)),
148  false);
149  emit gh_callback_event (m_handle, "callback");
150  }
151  }
152 
153 }
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:377
Definition: dMatrix.h:42
void update(int pId)
Definition: BaseControl.cc:142
virtual Container * innerContainer(void)=0
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:201
void gh_callback_event(const graphics_handle &h, const std::string &name)
graphics_handle m_handle
Definition: Object.h:157
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
static PopupMenuControl * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
PopupMenuControl(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QComboBox *box)
Base class for Octave interfaces that use Qt.
void warning(const char *fmt,...)
Definition: error.cc:1050
QString fromStdString(const std::string &s)
static octave_value box(JNIEnv *jni_env, void *jobj, void *jcls_arg=nullptr)