GNU Octave  8.1.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-2023 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 #include "octave-qtutils.h"
38 
40 
42 PopupMenuControl::create (octave::base_qobject& oct_qobj,
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 
60 PopupMenuControl::PopupMenuControl (octave::base_qobject& oct_qobj,
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, QOverload<int>::of (&QComboBox::activated),
74 }
75 
77 { }
78 
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 
OCTAVE_END_NAMESPACE(octave)
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:414
void update(int pId)
Definition: BaseControl.cc:164
Definition: dMatrix.h:42
Definition: Object.h:47
void gh_callback_event(const graphics_handle &h, const std::string &name)
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:200
graphics_handle m_handle
Definition: Object.h:153
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
virtual Container * innerContainer(void)=0
PopupMenuControl(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QComboBox *box)
void update(int pId)
void currentIndexChanged(int index)
static PopupMenuControl * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void warning(const char *fmt,...)
Definition: error.cc:1054
QString fromStdString(const std::string &s)
T::properties & properties(graphics_object obj)
static octave_value box(JNIEnv *jni_env, void *jobj, void *jcls_arg=nullptr)