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
BaseControl.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 <QEvent>
28 #include <QKeyEvent>
29 #include <QMouseEvent>
30 #include <QWidget>
31 
32 #include "BaseControl.h"
33 #include "ContextMenu.h"
34 #include "QtHandlesUtils.h"
35 
36 namespace QtHandles
37 {
38 
39 static void
41 {
42  QPalette p = w->palette ();
43 
44  if (props.style_is ("edit")
45  || props.style_is ("listbox")
46  || props.style_is ("popupmenu"))
47  {
48  p.setColor (QPalette::Base,
50  p.setColor (QPalette::Text,
52  }
53  else if (props.style_is ("pushbutton")
54  || props.style_is ("togglebutton"))
55  {
56  p.setColor (QPalette::Button,
58  p.setColor (QPalette::ButtonText,
60  }
61  else
62  {
63  p.setColor (QPalette::Window,
65  p.setColor (QPalette::WindowText,
67  }
68 
69  w->setPalette (p);
70 }
71 
73  : Object (go, w), m_normalizedFont (false), m_keyPressHandlerDefined (false)
74 {
75  init (w);
76 }
77 
78 void
79 BaseControl::init (QWidget* w, bool callBase)
80 {
81  if (callBase)
82  Object::init (w, callBase);
83 
84  uicontrol::properties& up = properties<uicontrol> ();
85 
86  Matrix bb = up.get_boundingbox (false);
87  w->setGeometry (xround (bb(0)), xround (bb(1)),
88  xround (bb(2)), xround (bb(3)));
89  w->setFont (Utils::computeFont<uicontrol> (up, bb(3)));
90  updatePalette (up, w);
91  w->setEnabled (up.enable_is ("on"));
92  w->setToolTip (Utils::fromStdString (up.get_tooltipstring ()));
93  w->setVisible (up.is_visible ());
95 
96  w->installEventFilter (this);
97 
98  m_normalizedFont = up.fontunits_is ("normalized");
99 }
100 
102 {
103 }
104 
105 void
107 {
108  uicontrol::properties& up = properties<uicontrol> ();
109  QWidget* w = qWidget<QWidget> ();
110 
111  switch (pId)
112  {
114  {
115  Matrix bb = up.get_boundingbox (false);
116  w->setGeometry (xround (bb(0)), xround (bb(1)),
117  xround (bb(2)), xround (bb(3)));
118  }
119  break;
120 
125  w->setFont (Utils::computeFont<uicontrol> (up));
126  break;
127 
129  // FIXME: We shouldn't have to do anything, octave should update
130  // the "fontsize" property automatically to the new units.
131  // Hence the actual font used shouldn't change.
132  m_normalizedFont = up.fontunits_is ("normalized");
133  break;
134 
137  updatePalette (up, w);
138  break;
139 
141  w->setEnabled (up.enable_is ("on"));
142  break;
143 
145  w->setToolTip (Utils::fromStdString (up.get_tooltipstring ()));
146  break;
147 
149  w->setVisible (up.is_visible ());
150  break;
151 
154  break;
155 
156  default:
157  break;
158  }
159 }
160 
161 bool
162 BaseControl::eventFilter (QObject* watched, QEvent* xevent)
163 {
164  switch (xevent->type ())
165  {
166  case QEvent::Resize:
167  if (m_normalizedFont)
168  {
170 
171  qWidget<QWidget> ()->setFont (Utils::computeFont<uicontrol>
172  (properties<uicontrol> ()));
173  }
174  break;
175 
176  case QEvent::MouseButtonPress:
177  {
179 
180  QMouseEvent* m = dynamic_cast<QMouseEvent*> (xevent);
181  graphics_object go = object ();
182  uicontrol::properties& up = Utils::properties<uicontrol> (go);
183  graphics_object fig = go.get_ancestor ("figure");
184 
185  if (m->button () != Qt::LeftButton
186  || ! up.enable_is ("on"))
187  {
188  gh_manager::post_set (fig.get_handle (), "selectiontype",
189  Utils::figureSelectionType (m), false);
190  gh_manager::post_set (fig.get_handle (), "currentpoint",
191  Utils::figureCurrentPoint (fig, m),
192  false);
193  gh_manager::post_callback (fig.get_handle (),
194  "windowbuttondownfcn");
195  gh_manager::post_callback (m_handle, "buttondownfcn");
196 
197  if (m->button () == Qt::RightButton)
198  ContextMenu::executeAt (up, m->globalPos ());
199  }
200  else
201  {
202  if (up.style_is ("listbox"))
203  gh_manager::post_set (fig.get_handle (), "selectiontype",
204  Utils::figureSelectionType (m), false);
205  else
206  gh_manager::post_set (fig.get_handle (), "selectiontype",
207  octave_value ("normal"), false);
208  }
209  }
210  break;
211 
212  case QEvent::MouseMove:
213  if (qWidget<QWidget> ()->hasMouseTracking ())
214  {
216 
217  QMouseEvent* m = dynamic_cast<QMouseEvent*> (xevent);
218  graphics_object go = object ();
219  graphics_object fig = go.get_ancestor ("figure");
220 
221  gh_manager::post_set (fig.get_handle (), "currentpoint",
222  Utils::figureCurrentPoint (fig, m), false);
223  gh_manager::post_callback (fig.get_handle (), "windowbuttonmotionfcn");
224  }
225  break;
226 
227  case QEvent::KeyPress:
229  {
231 
232  octave_scalar_map keyData =
233  Utils::makeKeyEventStruct (dynamic_cast<QKeyEvent*> (xevent));
234  graphics_object fig = object ().get_ancestor ("figure");
235 
236  gh_manager::post_set (fig.get_handle (), "currentcharacter",
237  keyData.getfield ("Character"), false);
238  gh_manager::post_callback (m_handle, "keypressfcn", keyData);
239  }
240  break;
241 
242  default:
243  break;
244  }
245 
246  return Object::eventFilter (watched, xevent);
247 }
248 
249 }; // namespace QtHandles
static void post_callback(const graphics_handle &h, const std::string &name, const octave_value &data=Matrix())
Definition: graphics.h:13329
bool enable_is(const std::string &v) const
Definition: graphics.h:11706
bool is_visible(void) const
Definition: graphics.h:2758
double xround(double x)
Definition: lo-mappers.cc:63
Matrix get_boundingbox(bool internal=false, const Matrix &parent_pix_size=Matrix()) const
Definition: graphics.cc:8961
std::string get_tooltipstring(void) const
Definition: graphics.h:11752
QString fromStdString(const std::string &s)
bool eventFilter(QObject *watched, QEvent *e)
Definition: BaseControl.cc:162
bool fontunits_is(const std::string &v) const
Definition: graphics.h:11718
void init(QWidget *w, bool callBase=false)
Definition: BaseControl.cc:79
Matrix figureCurrentPoint(const graphics_object &fig, QMouseEvent *event)
Matrix get_backgroundcolor_rgb(void) const
Definition: graphics.h:11695
template QFont computeFont< uicontrol >(const uicontrol::properties &props, int height)
static void executeAt(const base_properties &props, const QPoint &pt)
Definition: ContextMenu.cc:114
QColor fromRgb(const Matrix &rgb)
static void updatePalette(const uicontrol::properties &props, QWidget *w)
Definition: BaseControl.cc:40
octave_scalar_map makeKeyEventStruct(QKeyEvent *event)
std::complex< double > w(std::complex< double > z, double relerr=0)
void init(QObject *obj, bool callBase=false)
Definition: Object.cc:51
graphics_handle m_handle
Definition: Object.h:100
Matrix get_foregroundcolor_rgb(void) const
Definition: graphics.h:11726
Definition: dMatrix.h:35
graphics_object get_ancestor(const std::string &type) const
Definition: graphics.cc:3394
octave_value get_keypressfcn(void) const
Definition: graphics.h:11733
graphics_object object(void) const
Definition: Object.cc:73
bool is_empty(void) const
Definition: ov.h:526
void update(int pId)
Definition: BaseControl.cc:106
std::string figureSelectionType(QMouseEvent *event, bool isDoubleClick)
BaseControl(const graphics_object &go, QWidget *w)
Definition: BaseControl.cc:72
octave_value getfield(const std::string &key) const
Definition: oct-map.cc:164
graphics_handle get_handle(void) const
Definition: graphics.h:3363
bool style_is(const std::string &v) const
Definition: graphics.h:11749
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
static void post_set(const graphics_handle &h, const std::string &name, const octave_value &value, bool notify_toolkit=true)
Definition: graphics.h:13343