| 1 |
//########################################################################## |
| 2 |
//# # |
| 3 |
//# CLOUDCOMPARE PLUGIN: ccCompass # |
| 4 |
//# # |
| 5 |
//# This program is free software; you can redistribute it and/or modify # |
| 6 |
//# it under the terms of the GNU General Public License as published by # |
| 7 |
//# the Free Software Foundation; version 2 of the License. # |
| 8 |
//# # |
| 9 |
//# This program is distributed in the hope that it will be useful, # |
| 10 |
//# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 11 |
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 12 |
//# GNU General Public License for more details. # |
| 13 |
//# # |
| 14 |
//# COPYRIGHT: Sam Thiele 2017 # |
| 15 |
//# # |
| 16 |
//######################################################################### |
| 17 |
|
| 18 |
#include "../include/ccMouseCircle.h" |
| 19 |
|
| 20 |
//Qt |
| 21 |
#include <QCursor> |
| 22 |
#include <QWidget> |
| 23 |
#include <QWheelEvent> |
| 24 |
|
| 25 |
//System |
| 26 |
#include <cmath> |
| 27 |
|
| 28 |
//! Unit circle |
| 29 |
struct Circle |
| 30 |
{ |
| 31 |
Circle() |
| 32 |
{ |
| 33 |
//setup unit circle |
| 34 |
for (int n = 0; n < Resolution; n++) |
| 35 |
{ |
| 36 |
double heading = n * (2 * M_PI / Resolution); //heading in radians |
| 37 |
vertices[n][0] = std::cos(heading); |
| 38 |
vertices[n][1] = std::sin(heading); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
static const int Resolution = 100; |
| 43 |
double vertices[Resolution][2]; |
| 44 |
}; |
| 45 |
static Circle s_unitCircle; |
| 46 |
|
| 47 |
ccMouseCircle::ccMouseCircle(ccGLWindowInterface* owner, QString name) |
| 48 |
: cc2DViewportObject(name.isEmpty() ? "label" : name) |
| 49 |
, m_owner(owner) |
| 50 |
, m_pixelSize(0.0f) |
| 51 |
, m_radius(50) |
| 52 |
, m_radiusStep(4) |
| 53 |
, m_allowScroll(true) |
| 54 |
{ |
| 55 |
setVisible(true); |
| 56 |
setEnabled(false); |
| 57 |
|
| 58 |
//attach to owner |
| 59 |
if (nullptr != m_owner) |
| 60 |
{ |
| 61 |
m_owner->asQObject()->installEventFilter(this); |
| 62 |
m_owner->addToOwnDB(this, true); |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
assert(false); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
ccMouseCircle::~ccMouseCircle() |
| 71 |
{ |
| 72 |
//cleanup event listner |
| 73 |
if (m_owner) |
| 74 |
{ |
| 75 |
m_owner->asQObject()->removeEventFilter(this); |
| 76 |
m_owner->removeFromOwnDB(this); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
//override draw function |
| 81 |
void ccMouseCircle::draw(CC_DRAW_CONTEXT& context) |
| 82 |
{ |
| 83 |
if (!m_owner) |
| 84 |
{ |
| 85 |
assert(false); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
//only draw when visible |
| 90 |
if (!ccMouseCircle::isVisible()) |
| 91 |
{ |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
//only draw in 2D foreground mode |
| 96 |
if (!MACRO_Foreground(context) || !MACRO_Draw2D(context)) |
| 97 |
{ |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
//get the set of OpenGL functions (version 2.1) |
| 102 |
QOpenGLFunctions_2_1 *glFunc = context.glFunctions<QOpenGLFunctions_2_1>(); |
| 103 |
assert(glFunc != nullptr); |
| 104 |
if (glFunc == nullptr) |
| 105 |
{ |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
//test viewport parameters |
| 110 |
const ccViewportParameters& params = context.display->getViewportParameters(); |
| 111 |
|
| 112 |
//ccLog::Print(QString("WidthAtFocalDist = %1 (= %2 x %3)").arg(params.computeWidthAtFocalDist()).arg(params.computeDistanceToWidthRatio()).arg(params.getFocalDistance())); |
| 113 |
m_pixelSize = (context.glW != 0 ? params.computeWidthAtFocalDist() / context.glW : 0); |
| 114 |
|
| 115 |
//get mouse position |
| 116 |
QPoint p = m_owner->asWidget()->mapFromGlobal(QCursor::pos()); |
| 117 |
int mx = p.x(); //mouse x-coord |
| 118 |
int my = context.glH - 1 - p.y(); //mouse y-coord in OpenGL coordinates (origin at bottom left, not top left) |
| 119 |
|
| 120 |
//calculate circle location |
| 121 |
int cx = mx - context.glW / 2; |
| 122 |
int cy = my - context.glH / 2; |
| 123 |
|
| 124 |
//draw circle |
| 125 |
{ |
| 126 |
//thick dotted line |
| 127 |
{ |
| 128 |
glFunc->glPushAttrib(GL_LINE_BIT); |
| 129 |
glFunc->glLineWidth(2); |
| 130 |
glFunc->glLineStipple(1, 0xAAAA); |
| 131 |
glFunc->glEnable(GL_LINE_STIPPLE); |
| 132 |
} |
| 133 |
ccGL::Color(glFunc, ccColor::red); |
| 134 |
glFunc->glBegin(GL_LINE_LOOP); |
| 135 |
//glFunc->glBegin(GL_POLYGON); |
| 136 |
for (int n = 0; n < Circle::Resolution; n++) |
| 137 |
{ |
| 138 |
glFunc->glVertex2d(s_unitCircle.vertices[n][0] * m_radius + cx, s_unitCircle.vertices[n][1] * m_radius + cy); |
| 139 |
} |
| 140 |
|
| 141 |
glFunc->glEnd(); |
| 142 |
glFunc->glPopAttrib(); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
//get mouse move events |
| 147 |
bool ccMouseCircle::eventFilter(QObject* obj, QEvent* event) |
| 148 |
{ |
| 149 |
//only process events when visible |
| 150 |
if (!ccMouseCircle::isVisible()) |
| 151 |
return false; |
| 152 |
|
| 153 |
if (event->type() == QEvent::MouseMove) |
| 154 |
{ |
| 155 |
if (m_owner) |
| 156 |
{ |
| 157 |
m_owner->redraw(true, false); //redraw 2D graphics |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if (event->type() == QEvent::Wheel && m_allowScroll) |
| 162 |
{ |
| 163 |
QWheelEvent* wheelEvent = static_cast<QWheelEvent *>(event); |
| 164 |
|
| 165 |
//adjust radius (+ avoid really small radius) |
| 166 |
m_radius = std::max(m_radiusStep, m_radius - static_cast<int>(m_radiusStep * (wheelEvent->delta() / 100.0))); |
| 167 |
|
| 168 |
//repaint |
| 169 |
m_owner->redraw(true, false); |
| 170 |
} |
| 171 |
return false; //pass event to other listeners |
| 172 |
} |