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
Menu.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 <QAction>
28 #include <QMainWindow>
29 #include <QMenu>
30 #include <QMenuBar>
31 
32 #include "Figure.h"
33 #include "Menu.h"
34 #include "QtHandlesUtils.h"
35 
36 namespace QtHandles
37 {
38 
39 static QKeySequence
41 {
42  std::string s (up.get_accelerator ());
43 
44  if (! s.empty ())
45  {
46  char c = s[0];
47  int keyMod = Qt::CTRL;
48 
49  if (c >= 'A' && c <= 'Z')
50  keyMod |= Qt::SHIFT;
51  if (c >= 'a' && c <= 'z')
52  c -= ('a' - 'A');
53  if (c >= 'A' && c <= 'Z')
54  return QKeySequence (keyMod | static_cast<int> (c));
55  }
56 
57  return QKeySequence ();
58 }
59 
60 Menu*
62 {
63  Object* parent_obj = Object::parentObject (go);
64 
65  if (parent_obj)
66  {
67  QObject* qObj = parent_obj->qObject ();
68 
69  if (qObj)
70  return new Menu (go, new QAction (qObj), parent_obj);
71  }
72 
73  return 0;
74 }
75 
76 Menu::Menu (const graphics_object& go, QAction* action, Object* xparent)
77  : Object (go, action), m_parent (0), m_separator (0)
78 {
79  uimenu::properties& up = properties<uimenu> ();
80 
81  action->setText (Utils::fromStdString (up.get_label ()));
82 
83  if (up.is_checked ())
84  {
85  action->setCheckable (true);
86  action->setChecked (up.is_checked ());
87  }
88 
89  action->setEnabled (up.is_enable ());
90  action->setShortcut (accelSequence (up));
91  action->setVisible (up.is_visible ());
92 
93  if (up.is_separator ())
94  {
95  m_separator = new QAction (action);
96  m_separator->setSeparator (true);
97  m_separator->setVisible (up.is_visible ());
98  }
99 
100  MenuContainer* menuContainer = dynamic_cast<MenuContainer*> (xparent);
101 
102  if (menuContainer)
103  m_parent = menuContainer->menu ();
104 
105  if (m_parent)
106  {
107  int pos = static_cast<int> (up.get_position ());
108 
109  if (pos <= 0)
110  {
111  if (m_separator)
112  m_parent->insertAction (0, m_separator);
113  m_parent->insertAction (0, action);
114 
115  int count = 0;
116 
117  foreach (QAction* a, m_parent->actions ())
118  if (! a->isSeparator () && a->objectName () != "builtinMenu")
119  count++;
120  up.get_property ("position").set
121  (octave_value (static_cast<double> (count)), true, false);
122  }
123  else
124  {
125 
126  int count = 0;
127  QAction* before = 0;
128 
129  foreach (QAction* a, m_parent->actions ())
130  if (! a->isSeparator () && a->objectName () != "builtinMenu")
131  {
132  count++;
133  if (pos <= count)
134  {
135  before = a;
136  break;
137  }
138  }
139 
140  if (m_separator)
141  m_parent->insertAction (before, m_separator);
142  m_parent->insertAction (before, action);
143 
144  if (before)
146  else
147  up.get_property ("position").set
148  (octave_value (static_cast<double> (count+1)), true, false);
149  }
150  }
151 
152  connect (action, SIGNAL (triggered (bool)), SLOT (actionTriggered (void)));
153 }
154 
156 {
157 }
158 
159 void
160 Menu::update (int pId)
161 {
162  uimenu::properties& up = properties<uimenu> ();
163  QAction* action = qWidget<QAction> ();
164 
165  switch (pId)
166  {
168  action->setText (Utils::fromStdString (up.get_label ()));
169  break;
170 
172  if (up.is_checked ())
173  {
174  action->setCheckable (true);
175  action->setChecked (up.is_checked ());
176  }
177  else
178  {
179  action->setChecked (false);
180  action->setCheckable (false);
181  }
182  break;
183 
185  action->setEnabled (up.is_enable ());
186  break;
187 
189  if (! action->menu ())
190  action->setShortcut (accelSequence (up));
191  break;
192 
194  if (up.is_separator ())
195  {
196  if (! m_separator)
197  {
198  m_separator = new QAction (action);
199  m_separator->setSeparator (true);
200  m_separator->setVisible (up.is_visible ());
201  if (m_parent)
202  m_parent->insertAction (action, m_separator);
203  }
204  }
205  else
206  {
207  if (m_separator)
208  delete m_separator;
209  m_separator = 0;
210  }
211  break;
212 
214  action->setVisible (up.is_visible ());
215  if (m_separator)
216  m_separator->setVisible (up.is_visible ());
217  break;
218 
220  {
221  if (m_separator)
222  m_parent->removeAction (m_separator);
223 
224  m_parent->removeAction (action);
225 
226  int pos = static_cast<int> (up.get_position ());
227  QAction* before = 0;
228 
229  if (pos > 0)
230  {
231  int count = 0;
232 
233  foreach (QAction* a, m_parent->actions ())
234  if (! a->isSeparator () && a->objectName () != "builtinMenu")
235  {
236  count++;
237  if (pos <= count)
238  {
239  before = a;
240  break;
241  }
242  }
243  }
244 
245  if (m_separator)
246  m_parent->insertAction (before, m_separator);
247 
248  m_parent->insertAction (before, action);
249 
251  }
252  break;
253 
254  default:
255  Object::update (pId);
256  break;
257  }
258 }
259 
260 QWidget*
262 {
263  QAction* action = qWidget<QAction> ();
264  QMenu* _menu = action->menu ();
265 
266  if (! _menu)
267  {
268  _menu = new QMenu (action->parentWidget ());
269  action->setMenu (_menu);
270  action->setShortcut (QKeySequence ());
271  connect (_menu, SIGNAL (aboutToShow (void)),
272  this, SLOT (actionHovered (void)));
273  }
274 
275  return _menu;
276 }
277 
278 void
280 {
281  QAction* action = qWidget<QAction> ();
282 
283  if (action->isCheckable ())
284  action->setChecked (! action->isChecked ());
285  gh_manager::post_callback (m_handle, "callback");
286 }
287 
288 void
290 {
291  gh_manager::post_callback (m_handle, "callback");
292 }
293 
294 void
296 {
297  if (m_parent)
298  {
299  double count = 1.0;
300 
301  foreach (QAction* a, m_parent->actions ())
302  {
303  if (! a->isSeparator () && a->objectName () != "builtinMenu")
304  {
305  Object* aObj = Object::fromQObject (a);
306 
307  if (aObj)
308  {
309  graphics_object go = aObj->object ();
310 
311  // Probably overkill as a uimenu child can only be another
312  // uimenu object.
313  if (go.isa ("uimenu"))
314  {
315  uimenu::properties& up = Utils::properties<uimenu> (go);
316 
317  up.get_property ("position").set
318  (octave_value (count), true, false);
319  }
320  }
321 
322  count++;
323  }
324  }
325  }
326 }
327 
328 }; // namespace QtHandles
static void post_callback(const graphics_handle &h, const std::string &name, const octave_value &data=Matrix())
Definition: graphics.h:13329
bool is_visible(void) const
Definition: graphics.h:2758
bool isa(const std::string &go_name) const
Definition: graphics.h:3375
#define CTRL(x)
Definition: kpty.cpp:143
~Menu(void)
Definition: Menu.cc:155
QString fromStdString(const std::string &s)
void actionHovered(void)
Definition: Menu.cc:289
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:170
QAction * m_separator
Definition: Menu.h:62
void update(int pId)
Definition: Menu.cc:160
QWidget * m_parent
Definition: Menu.h:61
bool is_separator(void) const
Definition: graphics.h:11258
static Menu * create(const graphics_object &go)
Definition: Menu.cc:61
QWidget * menu(void)
Definition: Menu.cc:261
property get_property(const caseless_str &pname)
Menu(const graphics_object &go, QAction *action, Object *parent)
Definition: Menu.cc:76
bool set(const octave_value &val, bool do_run=true, bool do_notify_toolkit=true)
Definition: graphics.h:1933
graphics_handle m_handle
Definition: Object.h:100
bool is_enable(void) const
Definition: graphics.h:11246
static Object * fromQObject(QObject *obj)
Definition: Object.cc:181
void updateSiblingPositions(void)
Definition: Menu.cc:295
std::string get_accelerator(void) const
Definition: graphics.h:11238
std::string get_label(void) const
Definition: graphics.h:11254
virtual QObject * qObject(void)
Definition: Object.h:71
graphics_object object(void) const
Definition: Object.cc:73
bool is_checked(void) const
Definition: graphics.h:11243
double get_position(void) const
Definition: graphics.h:11256
virtual void update(int pId)
Definition: Object.cc:133
void actionTriggered(void)
Definition: Menu.cc:279
static QKeySequence accelSequence(const uimenu::properties &up)
Definition: Menu.cc:40
virtual QWidget * menu(void)=0