GNU Octave  8.1.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-2023 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 
41 
43 ContextMenu::create (octave::base_qobject& oct_qobj,
44  octave::interpreter& interp, const graphics_object& go)
45 {
46  Object *xparent = parentObject (interp, go);
47 
48  if (xparent)
49  {
50  QWidget *w = xparent->qWidget<QWidget> ();
51 
52  return new ContextMenu (oct_qobj, interp, go, new QMenu (w));
53  }
54 
55  return nullptr;
56 }
57 
58 ContextMenu::ContextMenu (octave::base_qobject& oct_qobj,
59  octave::interpreter& interp,
60  const graphics_object& go, QMenu *xmenu)
61  : Object (oct_qobj, interp, go, xmenu)
62 {
63  xmenu->setAutoFillBackground (true);
64 
65  connect (xmenu, &QMenu::aboutToShow, this, &ContextMenu::aboutToShow);
66  connect (xmenu, &QMenu::aboutToHide, this, &ContextMenu::aboutToHide);
67 }
68 
70 { }
71 
72 void
74 {
75  uicontextmenu::properties& up = properties<uicontextmenu> ();
76  QMenu *xmenu = qWidget<QMenu> ();
77 
78  switch (pId)
79  {
80  case base_properties::ID_VISIBLE:
81  if (up.is_visible ())
82  {
83  Matrix pos = up.get_position ().matrix_value ();
84  QWidget *parentW = xmenu->parentWidget ();
85  QPoint pt;
86 
87  pt.rx () = octave::math::round (pos(0));
88  pt.ry () = parentW->height () - octave::math::round (pos(1));
89  pt = parentW->mapToGlobal (pt);
90 
91  xmenu->popup (pt);
92  }
93  else
94  xmenu->hide ();
95  break;
96  default:
97  Object::update (pId);
98  break;
99  }
100 }
101 
102 void
104 {
105  emit gh_callback_event (m_handle, "callback");
106  emit gh_set_event (m_handle, "visible", "on", false);
107 }
108 
109 void
111 {
112  emit gh_set_event (m_handle, "visible", "off", false);
113 }
114 
115 QWidget *
117 {
118  return qWidget<QWidget> ();
119 }
120 
121 void
122 ContextMenu::executeAt (octave::interpreter& interp,
123  const base_properties& props, const QPoint& pt)
124 {
125  graphics_handle h = props.get_uicontextmenu ();
126 
127  if (h.ok ())
128  {
129  gh_manager& gh_mgr = interp.get_gh_manager ();
130  octave::autolock guard (gh_mgr.graphics_lock ());
131 
132  graphics_object go = gh_mgr.get_object (h);
133 
134  if (go.valid_object ())
135  {
136  ContextMenu *cMenu =
137  dynamic_cast<ContextMenu *> (qt_graphics_toolkit::toolkitObject (go));
138 
139  if (cMenu)
140  {
141  QMenu *menu = cMenu->qWidget<QMenu> ();
142 
143  if (menu)
144  menu->popup (pt);
145  }
146  }
147  }
148 }
149 
OCTAVE_END_NAMESPACE(octave)
~ContextMenu(void)
Definition: ContextMenu.cc:69
void aboutToHide(void)
Definition: ContextMenu.cc:110
QWidget * menu(void)
Definition: ContextMenu.cc:116
static void executeAt(octave::interpreter &interp, const base_properties &props, const QPoint &pt)
Definition: ContextMenu.cc:122
ContextMenu(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QMenu *menu)
Definition: ContextMenu.cc:58
void aboutToShow(void)
Definition: ContextMenu.cc:103
void update(int pId)
Definition: ContextMenu.cc:73
static ContextMenu * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
Definition: ContextMenu.cc:43
Definition: dMatrix.h:42
Definition: Object.h:47
void gh_callback_event(const graphics_handle &h, const std::string &name)
T * qWidget(void)
Definition: Object.h:81
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:200
graphics_handle m_handle
Definition: Object.h:153
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
virtual void update(int pId)
Definition: Object.cc:163
bool ok(void) const
Definition: oct-handle.h:113
static Object * toolkitObject(const graphics_object &go)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
double round(double x)
Definition: lo-mappers.h:136
std::complex< double > w(std::complex< double > z, double relerr=0)
T::properties & properties(graphics_object obj)