ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ccMouseCircle.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "../include/ccMouseCircle.h"
9 
10 // Qt
11 #include <QWheelEvent>
12 
13 // System
14 #include <cmath>
15 
17 struct Circle {
18  Circle() {
19  // setup unit circle
20  for (int n = 0; n < Resolution; n++) {
21  double heading = n * (2 * M_PI / Resolution); // heading in radians
22  vertices[n][0] = std::cos(heading);
23  vertices[n][1] = std::sin(heading);
24  }
25  }
26 
27  static const int Resolution = 100;
28  double vertices[Resolution][2];
29 };
31 
33  QWidget* owner,
34  QString name)
35  : cc2DViewportObject(name.isEmpty() ? "label" : name),
36  m_app(appInterface),
37  m_radius(50),
38  m_radiusStep(4),
39  m_allowScroll(true) {
40  setVisible(true);
41  setEnabled(false);
42 
43  // attach to owner
44  assert(owner); // check valid pointer
45  ccMouseCircle::m_owner = owner;
46  m_owner->installEventFilter(this);
47  ecvDisplayTools::AddToOwnDB(this, true);
48 }
49 
51  // cleanup event listner
52  if (m_owner) {
53  m_owner->removeEventFilter(this);
55  }
56 }
57 
58 // get the circle radius in world coordinates
60  float r = getRadiusPx() * m_pixelSize;
61  CVLog::Print(QString("Radius_w = %1 (= %2 x %3)")
62  .arg(r)
63  .arg(getRadiusPx())
64  .arg(m_pixelSize));
65  return r;
66 }
67 
68 // override draw function
70  if (!m_owner) {
71  assert(false);
72  return;
73  }
74 
75  // only draw when visible
76  if (!ccMouseCircle::isVisible()) {
77  return;
78  }
79 
80  // only draw in 2D foreground mode
82  return;
83  }
84 
85  // test viewport parameters
88 
89  // CVLog::Print(QString("WidthAtFocalDist = %1 (= %2 x
90  // %3)").arg(params.computeWidthAtFocalDist()).arg(params.computeDistanceToWidthRatio()).arg(params.getFocalDistance()));
91  m_pixelSize =
92  (context.glW != 0 ? params.computeWidthAtFocalDist() / context.glW
93  : 0);
94 
95  // get mouse position
96  QPoint p = m_owner->mapFromGlobal(QCursor::pos());
97  int mx = p.x(); // mouse x-coord
98  int my = context.glH - 1 - p.y(); // mouse y-coord in OpenGL coordinates
99  // (origin at bottom left, not top left)
100 
101  // calculate circle location
102  int cx = mx - context.glW / 2;
103  int cy = my - context.glH / 2;
104 
106  //{
107  // // thick dotted line
108  // {
109  // glFunc->glPushAttrib(GL_LINE_BIT);
110  // glFunc->glLineWidth(2);
111  // glFunc->glLineStipple(1, 0xAAAA);
112  // glFunc->glEnable(GL_LINE_STIPPLE);
113  // }
114  // glFunc->glColor4ubv(ecvColor::red.rgba);
115  // glFunc->glBegin(GL_LINE_LOOP);
116  // // glFunc->glBegin(GL_POLYGON);
117  // for (int n = 0; n < Circle::Resolution; n++) {
118  // glFunc->glVertex2d(s_unitCircle.vertices[n][0] * m_radius + cx,
119  // s_unitCircle.vertices[n][1] * m_radius + cy);
120  // }
121 
122  // glFunc->glEnd();
123  // glFunc->glPopAttrib();
124  //}
125 }
126 
127 // get mouse move events
128 bool ccMouseCircle::eventFilter(QObject* obj, QEvent* event) {
129  // only process events when visible
130  if (!ccMouseCircle::isVisible()) return false;
131 
132  if (event->type() == QEvent::MouseMove) {
133  if (m_owner) {
134  ecvDisplayTools::RedrawDisplay(true, false); // redraw 2D graphics
135  }
136  }
137 
138  if (event->type() == QEvent::Wheel && m_allowScroll) {
139  QWheelEvent* wheelEvent = static_cast<QWheelEvent*>(event);
140 
141  // adjust radius (+ avoid really small radius)
142  double delta = qtCompatWheelEventDelta(wheelEvent);
143  m_radius = std::max(
144  m_radiusStep,
145  m_radius - static_cast<int>(m_radiusStep * (delta / 100.0)));
146 
147  // repaint
148  ecvDisplayTools::RedrawDisplay(true, false);
149  }
150  return false; // pass event to other listeners
151 }
MouseEvent event
constexpr double M_PI
Pi.
Definition: CVConst.h:19
std::string name
double qtCompatWheelEventDelta(const QWheelEvent *event) noexcept
Definition: QtCompat.h:751
cmdLineReadable * params[]
static bool Print(const char *format,...)
Prints out a formatted message in console.
Definition: CVLog.cpp:113
2D viewport object
virtual bool isVisible() const
Returns whether entity is visible or not.
virtual void setVisible(bool state)
Sets entity visibility.
void draw(CC_DRAW_CONTEXT &context) override
Draws entity and its children.
ccMouseCircle(ecvMainAppInterface *appInterface, QWidget *owner, QString name=QString("MouseCircle"))
float getRadiusWorld()
int getRadiusPx() const
Definition: ccMouseCircle.h:35
virtual void setEnabled(bool state)
Sets the "enabled" property.
Definition: ecvObject.h:102
static void RemoveFromOwnDB(ccHObject *obj)
Removes an entity from window own DB.
static void AddToOwnDB(ccHObject *obj, bool noDependency=true)
Adds an entity to window own DB.
static const ecvViewportParameters & GetViewportParameters()
static void RedrawDisplay(bool only2D=false, bool forceRedraw=true)
Main application interface (for plugins)
Standard parameters for GL displays/viewports.
int max(int a, int b)
Definition: cutil_math.h:48
#define MACRO_Draw2D(context)
#define MACRO_Foreground(context)
ImGuiContext * context
Definition: Window.cpp:76
static Circle s_unitCircle
Unit circle.
double vertices[Resolution][2]
static const int Resolution
Display context.