GNU Octave 7.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-2022 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
39namespace octave
40{
41
42 PopupMenuControl *
44 octave::interpreter& interp,
45 const graphics_object& go)
46 {
47 Object *parent = parentObject (interp, go);
48
49 if (parent)
50 {
51 Container *container = parent->innerContainer ();
52
53 if (container)
54 return new PopupMenuControl (oct_qobj, interp, go,
55 new QComboBox (container));
56 }
57
58 return nullptr;
59 }
60
62 octave::interpreter& interp,
63 const graphics_object& go,
64 QComboBox *box)
65 : BaseControl (oct_qobj, interp, go, box), m_blockUpdate (false)
66 {
67 uicontrol::properties& up = properties<uicontrol> ();
68
69 box->addItems (Utils::fromStdString (up.get_string_string ()).split ('|'));
70
71 update (uicontrol::properties::ID_VALUE);
72
73 connect (box, QOverload<int>::of (&QComboBox::activated),
75 }
76
78 { }
79
81 {
82 uicontrol::properties& up = properties<uicontrol> ();
83 QComboBox *box = qWidget<QComboBox> ();
84
85 switch (pId)
86 {
87 case uicontrol::properties::ID_STRING:
88 m_blockUpdate = true;
89 {
90 int oldCurrent = box->currentIndex ();
91
92 box->clear ();
93 box->addItems (Utils::fromStdString
94 (up.get_string_string ()).split ('|'));
95 if (box->count () > 0
96 && oldCurrent >= 0
97 && oldCurrent < box->count ())
98 {
99 box->setCurrentIndex (oldCurrent);
100 }
101 else
102 {
103 emit gh_set_event (m_handle, "value",
104 octave_value (box->count () > 0 ? 1.0 : 0.0),
105 false);
106 }
107 }
108 m_blockUpdate = false;
109 break;
110
111 case uicontrol::properties::ID_VALUE:
112 m_blockUpdate = true;
113 {
114 Matrix value = up.get_value ().matrix_value ();
115
116 if (value.numel () > 0)
117 {
118 if (value(0) != static_cast<int> (value(0)))
119 warning ("popupmenu value should be integer");
120 else
121 {
122 int newIndex = int (value(0)) - 1;
123
124 if (newIndex >= 0 && newIndex < box->count ())
125 {
126 if (newIndex != box->currentIndex ())
127 box->setCurrentIndex (newIndex);
128 }
129 else
130 warning ("popupmenu value not within valid display range");
131 }
132 }
133 }
134 m_blockUpdate = false;
135 break;
136
137 default:
139 break;
140 }
141 }
142
143 void
145 {
146 if (! m_blockUpdate)
147 {
148 emit gh_set_event (m_handle, "value", octave_value (double (index + 1)),
149 false);
150 emit gh_callback_event (m_handle, "callback");
151 }
152 }
153
154}
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:411
Definition: dMatrix.h:42
void update(int pId)
Definition: BaseControl.cc:165
virtual Container * innerContainer(void)=0
void gh_callback_event(const graphics_handle &h, const std::string &name)
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
graphics_handle m_handle
Definition: Object.h:157
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:201
static PopupMenuControl * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
void currentIndexChanged(int index)
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:1055
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)
Convert the Java object pointed to by jobj_arg with class jcls_arg to an Octave value.
Definition: ov-java.cc:1386