GNU Octave 7.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-2022 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
40namespace octave
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, &QMenu::aboutToShow, this, &ContextMenu::aboutToShow);
67 connect (xmenu, &QMenu::aboutToHide, this, &ContextMenu::aboutToHide);
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
123 ContextMenu::executeAt (octave::interpreter& interp,
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
void aboutToHide(void)
Definition: ContextMenu.cc:111
void update(int pId)
Definition: ContextMenu.cc:74
static void executeAt(octave::interpreter &interp, const base_properties &props, const QPoint &pt)
Definition: ContextMenu.cc:123
static ContextMenu * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
Definition: ContextMenu.cc:44
QWidget * menu(void)
Definition: ContextMenu.cc:117
void aboutToShow(void)
Definition: ContextMenu.cc:104
ContextMenu(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QMenu *menu)
Definition: ContextMenu.cc:59
T * qWidget(void)
Definition: Object.h:85
void gh_callback_event(const graphics_handle &h, const std::string &name)
virtual void update(int pId)
Definition: Object.cc:164
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
graphics_handle m_handle
Definition: Object.h:157
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:201
Base class for Octave interfaces that use Qt.
static Object * toolkitObject(const graphics_object &go)
bool ok(void) const
Definition: oct-handle.h:113
std::complex< double > w(std::complex< double > z, double relerr=0)
T::properties & properties(graphics_object obj)
double round(double x)
Definition: lo-mappers.h:136