GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
EditControl.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 <QLineEdit>
31
32#include "Container.h"
33#include "EditControl.h"
34#include "TextEdit.h"
35#include "QtHandlesUtils.h"
36
37#include "octave-qobject.h"
38
39namespace octave
40{
41
42 EditControl *
44 octave::interpreter& interp, 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 {
54 uicontrol::properties& up = Utils::properties<uicontrol> (go);
55
56 if ((up.get_max () - up.get_min ()) > 1)
57 return new EditControl (oct_qobj, interp, go,
58 new TextEdit (container));
59 else
60 return new EditControl (oct_qobj, interp, go,
61 new QLineEdit (container));
62 }
63 }
64
65 return nullptr;
66 }
67
69 octave::interpreter& interp,
70 const graphics_object& go, QLineEdit *edit)
71 : BaseControl (oct_qobj, interp, go, edit), m_multiLine (false),
72 m_textChanged (false)
73 {
74 init (edit);
75 }
76
77 void
78 EditControl::init (QLineEdit *edit, bool callBase)
79 {
80 if (callBase)
81 BaseControl::init (edit, callBase);
82
83 m_multiLine = false;
84 initCommon (edit);
85
86 uicontrol::properties& up = properties<uicontrol> ();
87
88 if (up.enable_is ("inactive"))
89 edit->setReadOnly (true);
90 else
91 edit->setEnabled (up.enable_is ("on"));
92 edit->setText (Utils::fromStdString (up.get_string_string ()));
93 edit->setAlignment (Utils::fromHVAlign (up.get_horizontalalignment (),
94 up.get_verticalalignment ()));
95
96 connect (edit, &QLineEdit::textEdited,
98 connect (edit, &QLineEdit::editingFinished,
100 connect (edit, &QLineEdit::returnPressed,
102 }
103
105 octave::interpreter& interp,
106 const graphics_object& go, TextEdit *edit)
107 : BaseControl (oct_qobj, interp, go, edit), m_multiLine (true),
108 m_textChanged (false)
109 {
110 init (edit);
111 }
112
113 void
114 EditControl::init (TextEdit *edit, bool callBase)
115 {
116 if (callBase)
117 BaseControl::init (edit, callBase);
118
119 m_multiLine = true;
120 initCommon (edit);
121
122 uicontrol::properties& up = properties<uicontrol> ();
123
124 if (up.enable_is ("inactive"))
125 edit->setReadOnly (true);
126 else
127 edit->setEnabled (up.enable_is ("on"));
128 edit->setAcceptRichText (false);
129 edit->setPlainText (Utils::fromStringVector
130 (up.get_string_vector ()).join ("\n"));
131 edit->setAlignment (Utils::fromHVAlign (up.get_horizontalalignment (),
132 up.get_verticalalignment ()));
133
134 connect (edit, &TextEdit::textChanged,
136 connect (edit, &TextEdit::editingFinished,
138 connect (edit, &TextEdit::returnPressed,
140 }
141
143 { }
144
145 void
147 {
148 m_textChanged = false;
149 }
150
151 void
153 {
154 bool handled = false;
155
156 if (m_multiLine)
157 handled = updateMultiLine (pId);
158 else
159 handled = updateSingleLine (pId);
160
161 if (! handled)
162 {
163 switch (pId)
164 {
165 default:
167 break;
168 }
169 }
170 }
171
172 bool
174 {
175 uicontrol::properties& up = properties<uicontrol> ();
176 QLineEdit *edit = qWidget<QLineEdit> ();
177
178 switch (pId)
179 {
180 case uicontrol::properties::ID_STRING:
181 edit->setText (Utils::fromStdString (up.get_string_string ()));
182 return true;
183
184 case uicontrol::properties::ID_HORIZONTALALIGNMENT:
185 case uicontrol::properties::ID_VERTICALALIGNMENT:
186 edit->setAlignment (Utils::fromHVAlign (up.get_horizontalalignment (),
187 up.get_verticalalignment ()));
188 return true;
189
190 case uicontrol::properties::ID_ENABLE:
191 if (up.enable_is ("inactive"))
192 edit->setReadOnly (true);
193 else
194 {
195 edit->setReadOnly (false);
196 edit->setEnabled (up.enable_is ("on"));
197 }
198 return true;
199
200 case uicontrol::properties::ID_MIN:
201 case uicontrol::properties::ID_MAX:
202 if ((up.get_max () - up.get_min ()) > 1)
203 {
204 QWidget *container = edit->parentWidget ();
205
206 delete edit;
207 init (new TextEdit (container), true);
208 }
209 return true;
210
211 default:
212 break;
213 }
214
215 return false;
216 }
217
218 bool
220 {
221 uicontrol::properties& up = properties<uicontrol> ();
222 TextEdit *edit = qWidget<TextEdit> ();
223
224 switch (pId)
225 {
226 case uicontrol::properties::ID_STRING:
227 edit->setPlainText (Utils::fromStringVector
228 (up.get_string_vector ()).join ("\n"));
229 return true;
230
231 case uicontrol::properties::ID_HORIZONTALALIGNMENT:
232 case uicontrol::properties::ID_VERTICALALIGNMENT:
233 edit->setAlignment (Utils::fromHVAlign (up.get_horizontalalignment (),
234 up.get_verticalalignment ()));
235 return true;
236
237 case uicontrol::properties::ID_ENABLE:
238 if (up.enable_is ("inactive"))
239 edit->setReadOnly (true);
240 else
241 {
242 edit->setReadOnly (false);
243 edit->setEnabled (up.enable_is ("on"));
244 }
245 return true;
246
247 case uicontrol::properties::ID_MIN:
248 case uicontrol::properties::ID_MAX:
249 if ((up.get_max () - up.get_min ()) <= 1)
250 {
251 QWidget *container = edit->parentWidget ();
252
253 delete edit;
254 init (new QLineEdit (container), true);
255 }
256 return true;
257
258 default:
259 break;
260 }
261
262 return false;
263 }
264
265 void
267 {
268 m_textChanged = true;
269 }
270
271 void
273 {
274 QString txt = (m_multiLine
275 ? qWidget<TextEdit> ()->toPlainText ()
276 : qWidget<QLineEdit> ()->text ());
277
278 if (m_textChanged)
279 {
280 if (m_multiLine)
281 emit gh_set_event (m_handle, "string",
282 Utils::toCellString (txt.split ("\n")), false);
283 else
284 emit gh_set_event (m_handle, "string",
285 Utils::toStdString (txt), false);
286
287 m_textChanged = false;
288 }
289
290 if (txt.length () > 0)
291 emit gh_callback_event (m_handle, "callback");
292 }
293
294 void
296 {
297 if (m_textChanged)
298 {
299 QString txt = (m_multiLine
300 ? qWidget<TextEdit> ()->toPlainText ()
301 : qWidget<QLineEdit> ()->text ());
302 if (m_multiLine)
303 emit gh_set_event (m_handle, "string",
304 Utils::toCellString (txt.split ("\n")), false);
305 else
306 emit gh_set_event (m_handle, "string", Utils::toStdString (txt),
307 false);
308 emit gh_callback_event (m_handle, "callback");
309
310 m_textChanged = false;
311 }
312 }
313
314}
void update(int pId)
Definition: BaseControl.cc:165
void init(QWidget *w, bool callBase=false)
Definition: BaseControl.cc:130
bool updateMultiLine(int pId)
Definition: EditControl.cc:219
void textChanged(void)
Definition: EditControl.cc:266
EditControl(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QLineEdit *edit)
Definition: EditControl.cc:68
void editingFinished(void)
Definition: EditControl.cc:295
void returnPressed(void)
Definition: EditControl.cc:272
static EditControl * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
Definition: EditControl.cc:43
void initCommon(QWidget *widget)
Definition: EditControl.cc:146
void update(int pId)
Definition: EditControl.cc:152
void init(QLineEdit *edit, bool callBase=false)
Definition: EditControl.cc:78
bool updateSingleLine(int pId)
Definition: EditControl.cc:173
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
void editingFinished(void)
void returnPressed(void)
Base class for Octave interfaces that use Qt.
QStringList fromStringVector(const string_vector &v)
QString fromStdString(const std::string &s)
Qt::Alignment fromHVAlign(const std::string &halign, const std::string &valign)
T::properties & properties(graphics_object obj)
Cell toCellString(const QStringList &l)
std::string toStdString(const QString &s)