GNU Octave  6.2.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-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 <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 
39 namespace QtHandles
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  edit->setText (Utils::fromStdString (up.get_string_string ()));
89  edit->setAlignment (Utils::fromHVAlign (up.get_horizontalalignment (),
90  up.get_verticalalignment ()));
91 
92  connect (edit, SIGNAL (textEdited (const QString&)),
93  SLOT (textChanged (void)));
94  connect (edit, SIGNAL (editingFinished (void)),
95  SLOT (editingFinished (void)));
96  connect (edit, SIGNAL (returnPressed (void)),
97  SLOT (returnPressed (void)));
98  }
99 
101  octave::interpreter& interp,
102  const graphics_object& go, TextEdit *edit)
103  : BaseControl (oct_qobj, interp, go, edit), m_multiLine (true),
104  m_textChanged (false)
105  {
106  init (edit);
107  }
108 
109  void
110  EditControl::init (TextEdit *edit, bool callBase)
111  {
112  if (callBase)
113  BaseControl::init (edit, callBase);
114 
115  m_multiLine = true;
116  initCommon (edit);
117 
118  uicontrol::properties& up = properties<uicontrol> ();
119 
120  edit->setAcceptRichText (false);
121  edit->setPlainText (Utils::fromStringVector
122  (up.get_string_vector ()).join ("\n"));
123 
124  connect (edit, SIGNAL (textChanged (void)),
125  SLOT (textChanged (void)));
126  connect (edit, SIGNAL (editingFinished (void)),
127  SLOT (editingFinished (void)));
128  connect (edit, SIGNAL (returnPressed (void)),
129  SLOT (returnPressed (void)));
130  }
131 
133  { }
134 
135  void
137  {
138  m_textChanged = false;
139  }
140 
141  void
143  {
144  bool handled = false;
145 
146  if (m_multiLine)
147  handled = updateMultiLine (pId);
148  else
149  handled = updateSingleLine (pId);
150 
151  if (! handled)
152  {
153  switch (pId)
154  {
155  default:
156  BaseControl::update (pId);
157  break;
158  }
159  }
160  }
161 
162  bool
164  {
165  uicontrol::properties& up = properties<uicontrol> ();
166  QLineEdit *edit = qWidget<QLineEdit> ();
167 
168  switch (pId)
169  {
170  case uicontrol::properties::ID_STRING:
171  edit->setText (Utils::fromStdString (up.get_string_string ()));
172  return true;
173 
174  case uicontrol::properties::ID_HORIZONTALALIGNMENT:
175  case uicontrol::properties::ID_VERTICALALIGNMENT:
176  edit->setAlignment (Utils::fromHVAlign (up.get_horizontalalignment (),
177  up.get_verticalalignment ()));
178  return true;
179 
180  case uicontrol::properties::ID_MIN:
181  case uicontrol::properties::ID_MAX:
182  if ((up.get_max () - up.get_min ()) > 1)
183  {
184  QWidget *container = edit->parentWidget ();
185 
186  delete edit;
187  init (new TextEdit (container), true);
188  }
189  return true;
190 
191  default:
192  break;
193  }
194 
195  return false;
196  }
197 
198  bool
200  {
201  uicontrol::properties& up = properties<uicontrol> ();
202  TextEdit *edit = qWidget<TextEdit> ();
203 
204  switch (pId)
205  {
206  case uicontrol::properties::ID_STRING:
207  edit->setPlainText (Utils::fromStringVector
208  (up.get_string_vector ()).join ("\n"));
209  return true;
210 
211  case uicontrol::properties::ID_MIN:
212  case uicontrol::properties::ID_MAX:
213  if ((up.get_max () - up.get_min ()) <= 1)
214  {
215  QWidget *container = edit->parentWidget ();
216 
217  delete edit;
218  init (new QLineEdit (container), true);
219  }
220  return true;
221 
222  default:
223  break;
224  }
225 
226  return false;
227  }
228 
229  void
231  {
232  m_textChanged = true;
233  }
234 
235  void
237  {
238  QString txt = (m_multiLine
239  ? qWidget<TextEdit> ()->toPlainText ()
240  : qWidget<QLineEdit> ()->text ());
241 
242  if (m_textChanged)
243  {
244  if (m_multiLine)
245  emit gh_set_event (m_handle, "string",
246  Utils::toCellString (txt.split ("\n")), false);
247  else
248  emit gh_set_event (m_handle, "string",
249  Utils::toStdString (txt), false);
250 
251  m_textChanged = false;
252  }
253 
254  if (txt.length () > 0)
255  emit gh_callback_event (m_handle, "callback");
256  }
257 
258  void
260  {
261  if (m_textChanged)
262  {
263  QString txt = (m_multiLine
264  ? qWidget<TextEdit> ()->toPlainText ()
265  : qWidget<QLineEdit> ()->text ());
266  if (m_multiLine)
267  emit gh_set_event (m_handle, "string",
268  Utils::toCellString (txt.split ("\n")), false);
269  else
270  emit gh_set_event (m_handle, "string", Utils::toStdString (txt),
271  false);
272  emit gh_callback_event (m_handle, "callback");
273 
274  m_textChanged = false;
275  }
276  }
277 
278 }
void init(QWidget *w, bool callBase=false)
Definition: BaseControl.cc:110
void update(int pId)
Definition: BaseControl.cc:142
bool updateMultiLine(int pId)
Definition: EditControl.cc:199
bool updateSingleLine(int pId)
Definition: EditControl.cc:163
static EditControl * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
Definition: EditControl.cc:43
void update(int pId)
Definition: EditControl.cc:142
void initCommon(QWidget *widget)
Definition: EditControl.cc:136
void init(QLineEdit *edit, bool callBase=false)
Definition: EditControl.cc:78
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:259
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)
Base class for Octave interfaces that use Qt.
QStringList fromStringVector(const string_vector &v)
Qt::Alignment fromHVAlign(const std::string &halign, const std::string &valign)
Cell toCellString(const QStringList &l)
QString fromStdString(const std::string &s)
std::string toStdString(const QString &s)