GNU Octave  4.0.0
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-2015 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 #ifdef 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 (up.get_string_vector()).join("\n"));
108 
109  connect (edit, SIGNAL (textChanged (void)),
110  SLOT (textChanged (void)));
111  connect (edit, SIGNAL (editingFinished (void)),
112  SLOT (editingFinished (void)));
113  connect (edit, SIGNAL (returnPressed (void)),
114  SLOT (returnPressed (void)));
115 }
116 
118 {
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 (up.get_string_vector()).join("\n"));
194  return true;
195 
198  if ((up.get_max () - up.get_min ()) <= 1)
199  {
200  QWidget* container = edit->parentWidget ();
201 
202  delete edit;
203  init (new QLineEdit (container), true);
204  }
205  return true;
206 
207  default:
208  break;
209  }
210 
211  return false;
212 }
213 
214 void
216 {
217  m_textChanged = true;
218 }
219 
220 void
222 {
223  QString txt = (m_multiLine
224  ? qWidget<TextEdit> ()->toPlainText ()
225  : qWidget<QLineEdit> ()->text ());
226 
227  if (m_textChanged)
228  {
229  if (m_multiLine)
230  gh_manager::post_set (m_handle, "string", Utils::toCellString(txt.split("\n")), false);
231  else
232  gh_manager::post_set (m_handle, "string", Utils::toStdString (txt), false);
233 
234  m_textChanged = false;
235  }
236 
237  if (txt.length () > 0)
238  gh_manager::post_callback (m_handle, "callback");
239 }
240 
241 void
243 {
244  if (m_textChanged)
245  {
246  QString txt = (m_multiLine
247  ? qWidget<TextEdit> ()->toPlainText ()
248  : qWidget<QLineEdit> ()->text ());
249  if (m_multiLine)
250  gh_manager::post_set (m_handle, "string", Utils::toCellString(txt.split("\n")), false);
251  else
252  gh_manager::post_set (m_handle, "string", Utils::toStdString (txt), false);
253  gh_manager::post_callback (m_handle, "callback");
254 
255  m_textChanged = false;
256  }
257 }
258 
259 }; // namespace QtHandles
260 
static void post_callback(const graphics_handle &h, const std::string &name, const octave_value &data=Matrix())
Definition: graphics.h:13329
std::string get_verticalalignment(void) const
Definition: graphics.h:11760
std::string get_horizontalalignment(void) const
Definition: graphics.h:11730
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:11746
EditControl(const graphics_object &go, QLineEdit *edit)
Definition: EditControl.cc:60
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:170
QStringList fromStringVector(const string_vector &v)
void init(QWidget *w, bool callBase=false)
Definition: BaseControl.cc:79
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
static EditControl * create(const graphics_object &go)
Definition: EditControl.cc:38
std::string get_string_string(void) const
Definition: graphics.h:11745
double get_min(void) const
Definition: graphics.h:11739
void update(int pId)
Definition: BaseControl.cc:106
Qt::Alignment fromHVAlign(const caseless_str &halign, const caseless_str &valign)
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:11737
static void post_set(const graphics_handle &h, const std::string &name, const octave_value &value, bool notify_toolkit=true)
Definition: graphics.h:13343
void editingFinished(void)
Definition: EditControl.cc:242