61 #include "QtExaminerViewer.hh"
64 #include <QApplication>
65 #include <QInputDialog>
85 QStatusBar *_statusBar,
86 const QGLFormat* _format,
89 QtBaseViewer(_parent, _name, _statusBar, _format, _share, _options)
93 timer_ =
new QTimer(
this );
94 connect( timer_, SIGNAL(timeout()),
this, SLOT( slotAnimation()) );
96 allowRotation_ =
true;
100 wZoomFactorShift_ = 0.2;
110 switch (_event->type())
112 case QEvent::MouseButtonPress:
115 if (_event->modifiers() & Qt::ShiftModifier)
120 trackball_center_ = c;
121 trackball_radius_ = std::max(scene_radius_, (c-
glState().eye()).norm()*0.9f);
125 lastPoint_hitSphere_ =
mapToSphere( lastPoint2D_=_event->pos(),
135 case QEvent::MouseButtonDblClick:
138 flyTo(_event->pos(), _event->button()==Qt::MidButton);
143 case QEvent::MouseMove:
147 if (_event->modifiers() == Qt::ShiftModifier)
148 factor = wZoomFactorShift_;
151 if (_event->buttons() & (Qt::LeftButton | Qt::MidButton))
153 QPoint newPoint2D = _event->pos();
155 if ( (newPoint2D.x()<0) || (newPoint2D.x() > (int)
glWidth()) ||
156 (newPoint2D.y()<0) || (newPoint2D.y() > (int)
glHeight()) )
161 bool newPoint_hitSphere =
mapToSphere( newPoint2D, newPoint3D );
166 if ( (_event->buttons() & Qt::LeftButton) &&
167 (_event->buttons() & Qt::MidButton))
173 value_y = scene_radius_ * ((newPoint2D.y() - lastPoint2D_.y())) * 3.0 / (double)
glHeight();
181 value_y = ((newPoint2D.y() - lastPoint2D_.y())) * orthoWidth_ / (double)
glHeight();
182 orthoWidth_ -= value_y * factor;
191 else if (_event->buttons() & Qt::MidButton)
194 value_x = scene_radius_ * ((newPoint2D.x() - lastPoint2D_.x())) * 2.0 / (double)
glWidth();
195 value_y = scene_radius_ * ((newPoint2D.y() - lastPoint2D_.y())) * 2.0 / (double)
glHeight();
200 else if (allowRotation_ && (_event->buttons() & Qt::LeftButton) )
202 Vec3d axis(1.0,0.0,0.0);
205 if ( lastPoint_hitSphere_ ) {
207 if ( ( newPoint_hitSphere =
mapToSphere( newPoint2D,
209 axis = lastPoint3D_ % newPoint3D;
210 double cos_angle = ( lastPoint3D_ | newPoint3D );
211 if ( fabs(cos_angle) < 1.0 ) {
212 angle = acos( cos_angle ) * 180.0 / M_PI * factor ;
221 lastRotationAxis_ = axis;
222 lastRotationAngle_ = angle;
225 lastPoint2D_ = newPoint2D;
226 lastPoint3D_ = newPoint3D;
227 lastPoint_hitSphere_ = newPoint_hitSphere;
230 lastMoveTime_.restart();
237 case QEvent::MouseButtonRelease:
239 lastPoint_hitSphere_ =
false;
243 (_event->button() == Qt::LeftButton) &&
244 (!(_event->buttons() & Qt::MidButton)) &&
245 (lastMoveTime_.elapsed() < 10) &&
267 switch (_event->type()) {
268 case QEvent::MouseButtonPress: {
269 lastPoint_hitSphere_ =
mapToSphere(lastPoint2D_ = _event->pos(), lastPoint3D_);
275 case QEvent::MouseMove: {
278 if (_event->buttons() & Qt::LeftButton) {
279 QPoint newPoint2D = _event->pos();
281 if ((newPoint2D.x() < 0) || (newPoint2D.x() > (int)
glWidth()) || (newPoint2D.y() < 0)
282 || (newPoint2D.y() > (int)
glHeight()))
286 bool newPoint_hitSphere =
mapToSphere(newPoint2D, newPoint3D);
290 if (lastPoint_hitSphere_) {
291 Vec3d axis(1.0, 0.0, 0.0);
294 if ((newPoint_hitSphere =
mapToSphere(newPoint2D, newPoint3D))) {
295 axis = lastPoint3D_ % newPoint3D;
296 double cos_angle = (lastPoint3D_ | newPoint3D);
297 if (fabs(cos_angle) < 1.0) {
298 angle = acos(cos_angle) * 180.0 / M_PI;
305 lastPoint2D_ = newPoint2D;
306 lastPoint3D_ = newPoint3D;
307 lastPoint_hitSphere_ = newPoint_hitSphere;
328 double QtExaminerViewer::wheelZoomFactorShift(){
329 return wZoomFactorShift_;
334 void QtExaminerViewer::setWheelZoomFactor(
double _factor){
335 wZoomFactor_ = _factor;
340 void QtExaminerViewer::setWheelZoomFactorShift(
double _factor){
341 wZoomFactorShift_ = _factor;
348 double factor = wZoomFactor_;
350 if (_event->modifiers() == Qt::ShiftModifier)
351 factor = wZoomFactorShift_;
357 double d = -(double)_event->delta() / 120.0 * 0.2 * factor * trackball_radius_/3;
365 double d = (double)_event->delta() / 120.0 * 0.2 * factor * orthoWidth_;
384 if ( (_v2D.x() >= 0) && (_v2D.x() < (int)
glWidth()) &&
385 (_v2D.y() >= 0) && (_v2D.y() < (int)
glHeight()) )
387 double x = (double)(_v2D.x() - ((double)
glWidth() / 2.0)) / (
double)
glWidth();
388 double y = (double)(((
double)
glHeight() / 2.0) - _v2D.y()) / (
double)
glHeight();
389 double sinx = sin(M_PI * x * 0.5);
390 double siny = sin(M_PI * y * 0.5);
391 double sinx2siny2 = sinx * sinx + siny * siny;
395 _v3D[2] = sinx2siny2 < 1.0 ? sqrt(1.0 - sinx2siny2) : 0.0;
406 void QtExaminerViewer::slotAnimation()
412 rotate( lastRotationAxis_, lastRotationAngle_ );
417 static int msecs=0, count=0;
419 msecs += t.elapsed();
421 assert(statusbar_!=0);
423 sprintf( s,
"%.3f fps", (10000.0 / (
float)msecs) );
424 statusbar_->showMessage(s,2000);
bool mapToSphere(const QPoint &_p, Vec3d &_result) const
virtual trackball: map 2D screen point to unit sphere
Namespace providing different geometric functions concerning angles.
unsigned int glWidth() const
get width of QGLWidget
double wheelZoomFactor()
Factors for zooming with the wheel.
virtual void viewWheelEvent(QWheelEvent *_event)
handle wheel events
VectorT< double, 3 > Vec3d
const GLMatrixd & modelview() const
get modelview matrix
void rotate(const Vec3d &axis, double angle)
rotate the scene (around its center) and update modelview matrix
GLState & glState()
get OpenGL state
void translate(const Vec3d &trans)
translate the scene and update modelview matrix
virtual void flyTo(const QPoint &_pos, bool _move_back)
Fly to. Get closer if _move_back=false, get more distant else.
void signalSetView(const GLMatrixd &_modelview, const GLMatrixd &_inverse_modelview)
set view, used for synchronizing (cf. slotSetView())
bool animation() const
Is animation enabled?
const GLMatrixd & inverse_modelview() const
get inverse modelview matrix
void updateProjectionMatrix()
updates projection matrix
bool fast_pick(const QPoint &_mousePos, Vec3d &_hitPoint)
virtual void makeCurrent()
Makes this widget the current widget for OpenGL operations.
virtual void viewMouseEvent(QMouseEvent *_event)
handle mouse events
QtExaminerViewer(QWidget *_parent=0, const char *_name=0, QStatusBar *_statusBar=0, const QGLFormat *_format=0, const QtBaseViewer *_share=0, Options _options=DefaultOptions)
Constructor.
bool isUpdateLocked() const
void rotate_lights(Vec3d &_axis, double _angle)
rotete light sources
virtual void lightMouseEvent(QMouseEvent *_event)
handle mouse events
ProjectionMode projectionMode() const
get current projection mode
virtual void updateGL()
Redraw scene. Triggers paint event for updating the view (cf. drawNow()).
unsigned int glHeight() const
get height of QGLWidget