GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ContextMenu.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2011-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <QMenu>
31 
32 #include "ContextMenu.h"
33 #include "QtHandlesUtils.h"
34 #include "qt-graphics-toolkit.h"
35 
36 #include "octave-qobject.h"
37 
38 #include "interpreter.h"
39 
40 namespace QtHandles
41 {
42 
43  ContextMenu*
45  octave::interpreter& interp, const graphics_object& go)
46  {
47  Object *xparent = parentObject (interp, go);
48 
49  if (xparent)
50  {
51  QWidget *w = xparent->qWidget<QWidget> ();
52 
53  return new ContextMenu (oct_qobj, interp, go, new QMenu (w));
54  }
55 
56  return nullptr;
57  }
58 
60  octave::interpreter& interp,
61  const graphics_object& go, QMenu *xmenu)
62  : Object (oct_qobj, interp, go, xmenu)
63  {
64  xmenu->setAutoFillBackground (true);
65 
66  connect (xmenu, SIGNAL (aboutToShow (void)), SLOT (aboutToShow (void)));
67  connect (xmenu, SIGNAL (aboutToHide (void)), SLOT (aboutToHide (void)));
68  }
69 
71  { }
72 
73  void
75  {
76  uicontextmenu::properties& up = properties<uicontextmenu> ();
77  QMenu *xmenu = qWidget<QMenu> ();
78 
79  switch (pId)
80  {
81  case base_properties::ID_VISIBLE:
82  if (up.is_visible ())
83  {
84  Matrix pos = up.get_position ().matrix_value ();
85  QWidget *parentW = xmenu->parentWidget ();
86  QPoint pt;
87 
88  pt.rx () = octave::math::round (pos(0));
89  pt.ry () = parentW->height () - octave::math::round (pos(1));
90  pt = parentW->mapToGlobal (pt);
91 
92  xmenu->popup (pt);
93  }
94  else
95  xmenu->hide ();
96  break;
97  default:
98  Object::update (pId);
99  break;
100  }
101  }
102 
103  void
105  {
106  emit gh_callback_event (m_handle, "callback");
107  emit gh_set_event (m_handle, "visible", "on", false);
108  }
109 
110  void
112  {
113  emit gh_set_event (m_handle, "visible", "off", false);
114  }
115 
116  QWidget*
118  {
119  return qWidget<QWidget> ();
120  }
121 
122  void
124  const base_properties& props, const QPoint& pt)
125  {
126  graphics_handle h = props.get_uicontextmenu ();
127 
128  if (h.ok ())
129  {
130  gh_manager& gh_mgr = interp.get_gh_manager ();
131  octave::autolock guard (gh_mgr.graphics_lock ());
132 
133  graphics_object go = gh_mgr.get_object (h);
134 
135  if (go.valid_object ())
136  {
137  ContextMenu *cMenu =
138  dynamic_cast<ContextMenu *> (qt_graphics_toolkit::toolkitObject (go));
139 
140  if (cMenu)
141  {
142  QMenu *menu = cMenu->qWidget<QMenu> ();
143 
144  if (menu)
145  menu->popup (pt);
146  }
147  }
148  }
149  }
150 
151 }
Definition: dMatrix.h:42
QWidget * menu(void)
Definition: ContextMenu.cc:117
void update(int pId)
Definition: ContextMenu.cc:74
static ContextMenu * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
Definition: ContextMenu.cc:44
static void executeAt(octave::interpreter &interp, const base_properties &props, const QPoint &pt)
Definition: ContextMenu.cc:123
ContextMenu(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QMenu *menu)
Definition: ContextMenu.cc:59
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:201
void gh_callback_event(const graphics_handle &h, const std::string &name)
virtual void update(int pId)
Definition: Object.cc:164
graphics_handle m_handle
Definition: Object.h:157
T * qWidget(void)
Definition: Object.h:85
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
static Object * toolkitObject(const graphics_object &go)
graphics_object get_object(double val) const
Definition: graphics.in.h:6260
octave::mutex graphics_lock(void)
Definition: graphics.in.h:6393
bool valid_object(void) const
Definition: graphics.in.h:2847
Base class for Octave interfaces that use Qt.
gh_manager & get_gh_manager(void)
Definition: interpreter.h:295
bool ok(void) const
Definition: oct-handle.h:113
std::complex< double > w(std::complex< double > z, double relerr=0)
double round(double x)
Definition: lo-mappers.h:136