GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EditControl.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2017 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://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 0;
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  {
157  edit->setText (Utils::fromStdString (up.get_string_string ()));
158  return true;
159 
162  edit->setAlignment (Utils::fromHVAlign (up.get_horizontalalignment (),
163  up.get_verticalalignment ()));
164  return true;
165 
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  {
193  edit->setPlainText (Utils::fromStringVector (
194  up.get_string_vector()).join("\n"));
195  return true;
196 
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", Utils::toCellString(txt.split("\n")),
232  false);
233  else
234  gh_manager::post_set (m_handle, "string", Utils::toStdString (txt), false);
235 
236  m_textChanged = false;
237  }
238 
239  if (txt.length () > 0)
240  gh_manager::post_callback (m_handle, "callback");
241  }
242 
243  void
245  {
246  if (m_textChanged)
247  {
248  QString txt = (m_multiLine
249  ? qWidget<TextEdit> ()->toPlainText ()
250  : qWidget<QLineEdit> ()->text ());
251  if (m_multiLine)
252  gh_manager::post_set (m_handle, "string", Utils::toCellString(txt.split("\n")),
253  false);
254  else
255  gh_manager::post_set (m_handle, "string", Utils::toStdString (txt), false);
256  gh_manager::post_callback (m_handle, "callback");
257 
258  m_textChanged = false;
259  }
260  }
261 
262 }
static void post_callback(const graphics_handle &h, const std::string &name, const octave_value &data=Matrix())
Definition: graphics.h:13910
std::string get_verticalalignment(void) const
Definition: graphics.h:11992
std::string get_horizontalalignment(void) const
Definition: graphics.h:11962
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)
string_vector get_string_vector(void) const
Definition: graphics.h:11978
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:100
is false
Definition: cellfun.cc:398
static EditControl * create(const graphics_object &go)
Definition: EditControl.cc:38
std::string get_string_string(void) const
Definition: graphics.h:11977
double get_min(void) const
Definition: graphics.h:11971
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)
double get_max(void) const
Definition: graphics.h:11969
static void post_set(const graphics_handle &h, const std::string &name, const octave_value &value, bool notify_toolkit=true)
Definition: graphics.h:13924
void editingFinished(void)
Definition: EditControl.cc:244