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