GNU Octave  4.4.1
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-2018 Michael Goffioul
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <QLineEdit>
28 
29 #include "Container.h"
30 #include "EditControl.h"
31 #include "TextEdit.h"
32 #include "QtHandlesUtils.h"
33 
34 namespace QtHandles
35 {
36 
37  EditControl*
39  {
40  Object *parent = Object::parentObject (go);
41 
42  if (parent)
43  {
44  Container *container = parent->innerContainer ();
45 
46  if (container)
47  {
48  uicontrol::properties& up = Utils::properties<uicontrol> (go);
49 
50  if ((up.get_max () - up.get_min ()) > 1)
51  return new EditControl (go, new TextEdit (container));
52  else
53  return new EditControl (go, new QLineEdit (container));
54  }
55  }
56 
57  return nullptr;
58  }
59 
61  : BaseControl (go, edit), m_multiLine (false), m_textChanged (false)
62  {
63  init (edit);
64  }
65 
66  void
67  EditControl::init (QLineEdit *edit, bool callBase)
68  {
69  if (callBase)
70  BaseControl::init (edit, callBase);
71 
72  m_multiLine = false;
73  initCommon (edit);
74 
75  uicontrol::properties& up = properties<uicontrol> ();
76 
77  edit->setText (Utils::fromStdString (up.get_string_string ()));
78  edit->setAlignment (Utils::fromHVAlign (up.get_horizontalalignment (),
79  up.get_verticalalignment ()));
80 
81  connect (edit, SIGNAL (textEdited (const QString&)),
82  SLOT (textChanged (void)));
83  connect (edit, SIGNAL (editingFinished (void)),
84  SLOT (editingFinished (void)));
85  connect (edit, SIGNAL (returnPressed (void)),
86  SLOT (returnPressed (void)));
87  }
88 
90  : BaseControl (go, edit), m_multiLine (true), m_textChanged (false)
91  {
92  init (edit);
93  }
94 
95  void
96  EditControl::init (TextEdit *edit, bool callBase)
97  {
98  if (callBase)
99  BaseControl::init (edit, callBase);
100 
101  m_multiLine = true;
102  initCommon (edit);
103 
104  uicontrol::properties& up = properties<uicontrol> ();
105 
106  edit->setAcceptRichText (false);
107  edit->setPlainText (Utils::fromStringVector (
108  up.get_string_vector ()).join ("\n"));
109 
110  connect (edit, SIGNAL (textChanged (void)),
111  SLOT (textChanged (void)));
112  connect (edit, SIGNAL (editingFinished (void)),
113  SLOT (editingFinished (void)));
114  connect (edit, SIGNAL (returnPressed (void)),
115  SLOT (returnPressed (void)));
116  }
117 
119  { }
120 
121  void
123  {
124  m_textChanged = false;
125  }
126 
127  void
129  {
130  bool handled = false;
131 
132  if (m_multiLine)
133  handled = updateMultiLine (pId);
134  else
135  handled = updateSingleLine (pId);
136 
137  if (! handled)
138  {
139  switch (pId)
140  {
141  default:
142  BaseControl::update (pId);
143  break;
144  }
145  }
146  }
147 
148  bool
150  {
151  uicontrol::properties& up = properties<uicontrol> ();
152  QLineEdit *edit = qWidget<QLineEdit> ();
153 
154  switch (pId)
155  {
156  case uicontrol::properties::ID_STRING:
157  edit->setText (Utils::fromStdString (up.get_string_string ()));
158  return true;
159 
160  case uicontrol::properties::ID_HORIZONTALALIGNMENT:
161  case uicontrol::properties::ID_VERTICALALIGNMENT:
162  edit->setAlignment (Utils::fromHVAlign (up.get_horizontalalignment (),
163  up.get_verticalalignment ()));
164  return true;
165 
166  case uicontrol::properties::ID_MIN:
167  case uicontrol::properties::ID_MAX:
168  if ((up.get_max () - up.get_min ()) > 1)
169  {
170  QWidget *container = edit->parentWidget ();
171 
172  delete edit;
173  init (new TextEdit (container), true);
174  }
175  return true;
176 
177  default:
178  break;
179  }
180 
181  return false;
182  }
183 
184  bool
186  {
187  uicontrol::properties& up = properties<uicontrol> ();
188  TextEdit *edit = qWidget<TextEdit> ();
189 
190  switch (pId)
191  {
192  case uicontrol::properties::ID_STRING:
193  edit->setPlainText (Utils::fromStringVector (
194  up.get_string_vector ()).join ("\n"));
195  return true;
196 
197  case uicontrol::properties::ID_MIN:
198  case uicontrol::properties::ID_MAX:
199  if ((up.get_max () - up.get_min ()) <= 1)
200  {
201  QWidget *container = edit->parentWidget ();
202 
203  delete edit;
204  init (new QLineEdit (container), true);
205  }
206  return true;
207 
208  default:
209  break;
210  }
211 
212  return false;
213  }
214 
215  void
217  {
218  m_textChanged = true;
219  }
220 
221  void
223  {
224  QString txt = (m_multiLine
225  ? qWidget<TextEdit> ()->toPlainText ()
226  : qWidget<QLineEdit> ()->text ());
227 
228  if (m_textChanged)
229  {
230  if (m_multiLine)
231  gh_manager::post_set (m_handle, "string",
232  Utils::toCellString (txt.split ("\n")), false);
233  else
234  gh_manager::post_set (m_handle, "string",
235  Utils::toStdString (txt), false);
236 
237  m_textChanged = false;
238  }
239 
240  if (txt.length () > 0)
241  gh_manager::post_callback (m_handle, "callback");
242  }
243 
244  void
246  {
247  if (m_textChanged)
248  {
249  QString txt = (m_multiLine
250  ? qWidget<TextEdit> ()->toPlainText ()
251  : qWidget<QLineEdit> ()->text ());
252  if (m_multiLine)
253  gh_manager::post_set (m_handle, "string",
254  Utils::toCellString (txt.split ("\n")), false);
255  else
256  gh_manager::post_set (m_handle, "string",
257  Utils::toStdString (txt), false);
258  gh_manager::post_callback (m_handle, "callback");
259 
260  m_textChanged = false;
261  }
262  }
263 
264 }
static void post_callback(const graphics_handle &h, const std::string &name, const octave_value &data=Matrix())
Definition: graphics.in.h:6214
Qt::Alignment fromHVAlign(const std::string &halign, const std::string &valign)
void update(int pId)
Definition: EditControl.cc:128
bool updateSingleLine(int pId)
Definition: EditControl.cc:149
QString fromStdString(const std::string &s)
EditControl(const graphics_object &go, QLineEdit *edit)
Definition: EditControl.cc:60
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:165
QStringList fromStringVector(const string_vector &v)
void init(QWidget *w, bool callBase=false)
Definition: BaseControl.cc:98
virtual Container * innerContainer(void)=0
bool updateMultiLine(int pId)
Definition: EditControl.cc:185
void init(QLineEdit *edit, bool callBase=false)
Definition: EditControl.cc:67
graphics_handle m_handle
Definition: Object.h:115
is false
Definition: cellfun.cc:400
static EditControl * create(const graphics_object &go)
Definition: EditControl.cc:38
void update(int pId)
Definition: BaseControl.cc:124
void initCommon(QWidget *widget)
Definition: EditControl.cc:122
Cell toCellString(const QStringList &l)
std::string toStdString(const QString &s)
static void post_set(const graphics_handle &h, const std::string &name, const octave_value &value, bool notify_toolkit=true)
Definition: graphics.in.h:6228
void editingFinished(void)
Definition: EditControl.cc:245