00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #ifndef ACG_QTBASEVIEWER_HH
00052 #define ACG_QTBASEVIEWER_HH
00053
00054
00055
00056
00057 #include "../GL/GLState.hh"
00058 #include "../Scenegraph/SceneGraph.hh"
00059 #include "../Scenegraph/DrawModes.hh"
00060
00061 #include <QtOpenGL/QGLFormat>
00062 #include <QBoxLayout>
00063 #include <QtNetwork/QUdpSocket>
00064 #include <QWheelEvent>
00065 #include <QKeyEvent>
00066 #include <QDropEvent>
00067 #include <QContextMenuEvent>
00068 #include <QDragEnterEvent>
00069 #include <QMouseEvent>
00070 #include <QAction>
00071 #include <QSize>
00072 #include <QMap>
00073 #include <QString>
00074 #include <QMenu>
00075 #include <QToolBar>
00076 #include <QTime>
00077
00078 #include <vector>
00079 #include <string>
00080
00081
00082
00083
00084
00085 class QPushButton;
00086 class QToolButton;
00087 class QStatusBar;
00088 class QSplitter;
00089 class QTimer;
00090 class QImage;
00091 class QSocketNotifier;
00092 class QGraphicsWidget;
00093 class QGraphicsGridLayout;
00094
00095
00096
00097
00098
00099 namespace ACG {
00100 namespace QtWidgets {
00101
00102
00103
00104
00105
00106 class QtWheel;
00107 class QtGLGraphicsScene;
00108 class QtGLGraphicsView;
00109 class QtSceneGraphDialog;
00110 class QtShiftPopupMenu;
00111
00112
00113
00114
00115
00123 class ACGDLLEXPORT QtBaseViewer : public QWidget
00124 {
00125 Q_OBJECT
00126
00127
00128 public:
00129
00133 enum Options {
00135 Nothing=0,
00136
00143 ShowPrivateStatusBar=1,
00144
00154 ShowToolBar=2,
00155
00157 ShowPickButton=4,
00159 ShowLassoButton=8,
00161 ShowQuestionButton=0x10,
00162
00164 ShowWheelX=0x20,
00166 ShowWheelY=0x40,
00170 ShowWheelZ=0x80,
00171
00173 ShowWheels=0xe0,
00174
00176 DefaultOptions = 0xffff & (~ShowPrivateStatusBar)
00177 };
00178
00179
00180
00181
00182
00183
00192 QtBaseViewer( QWidget* _parent=0,
00193 const char* _name=0,
00194 QStatusBar *_statusBar=0,
00195 const QGLFormat* _format=0,
00196 const QtBaseViewer* _share=0,
00197 Options _options=DefaultOptions );
00198
00200 virtual ~QtBaseViewer();
00201
00202
00203
00204
00205
00206
00207
00208 virtual QSize sizeHint () const;
00209
00211 QStatusBar* statusBar() { return statusbar_; }
00220 void setStatusBar(QStatusBar* _sb);
00221
00223 void applyOptions(int _options);
00225 int options() const { return options_; }
00226
00227
00229 virtual void makeCurrent();
00231 virtual void swapBuffers();
00232
00233
00235 GLState& glState() { return *glstate_; }
00236
00237
00241 void backgroundColor(const Vec4f& _color) {
00242 glstate_->set_clear_color(_color); updateGL();
00243 }
00245 Vec4f backgroundColor() { return glstate_->clear_color(); }
00246
00247
00253 virtual void lockUpdate();
00255 virtual void unlockUpdate();
00260 virtual void unlockAndUpdate();
00263 bool isUpdateLocked() const { return updateLocked_; }
00264
00265
00267 void lockProjectionUpdate( void ) { projectionUpdateLocked_ = true; }
00268
00270 void unlockProjectionUpdate( void ) { projectionUpdateLocked_ = false; }
00271
00273 bool animation() const { return animation_; }
00275 void animation(bool _b);
00276
00278 void trackMouse(bool _track);
00279
00281 void enablePopupMenu(bool _enable);
00282
00283
00284
00285
00286
00287
00289 SceneGraph::BaseNode* sceneGraph() { return sceneGraphRoot_; }
00291 const SceneGraph::BaseNode* sceneGraph() const { return sceneGraphRoot_; }
00299 void sceneGraph(SceneGraph::BaseNode* _root);
00300
00302 enum NavigationMode {
00303 NORMAL_NAVIGATION,
00304 FIRSTPERSON_NAVIGATION
00305 };
00306
00308 enum ProjectionMode {
00309 ORTHOGRAPHIC_PROJECTION,
00310 PERSPECTIVE_PROJECTION
00311 };
00312
00314 void projectionMode(ProjectionMode _p);
00316 ProjectionMode projectionMode() const { return projectionMode_; }
00317
00319 void navigationMode(NavigationMode _n);
00321 NavigationMode navigationMode() const { return navigationMode_; }
00322
00327 void setScenePos( const ACG::Vec3d& _center, double _radius, const bool _setCenter = true );
00328
00332 void setSceneCenter( const ACG::Vec3d& _center );
00333
00337 const ACG::Vec3d& scene_center() const { return scene_center_; }
00341 double scene_radius() const { return scene_radius_; }
00342
00344 void viewingDirection( const ACG::Vec3d& _dir, const ACG::Vec3d& _up );
00345
00347 enum ActionMode {
00348
00349 ExamineMode,
00353 PickingMode,
00354
00355 QuestionMode,
00356
00357 LightMode
00358 };
00359
00360
00366 void actionMode(ActionMode);
00367
00369 ActionMode lastActionMode(){ return lastActionMode_; };
00370
00372 ActionMode actionMode() const { return actionMode_; }
00373
00374
00376 enum FaceOrientation {
00377 CCW_ORIENTATION,
00378 CW_ORIENTATION
00379 };
00380
00382 void faceOrientation(FaceOrientation);
00384 FaceOrientation faceOrientation() const { return faceOrientation_; }
00385
00387 void backFaceCulling( bool _b );
00389 bool backFaceCulling() const { return backFaceCulling_; }
00390
00392 void twoSidedLighting( bool _b );
00394 bool twoSidedLighting() const { return twoSidedLighting_; }
00395
00397 enum NormalsMode {
00399 DONT_TOUCH_NORMALS,
00404 NORMALIZE_NORMALS
00405 };
00406
00408 void normalsMode(NormalsMode _mode);
00410 NormalsMode normalsMode() const { return normalsMode_; }
00411
00412
00414 GLMatrixd& light_matrix() { return light_matrix_; }
00416 void rotate_lights(Vec3d& _axis, double _angle);
00418 void update_lights();
00419
00420
00422 void copyToImage( QImage& _image, GLenum _buffer=GL_BACK) {
00423 copyToImage(_image, 0, 0, glWidth(), glHeight(), _buffer);
00424 }
00425
00427 void copyToImage( QImage& _image,
00428 unsigned int _left, unsigned int _top,
00429 unsigned int _width, unsigned int _height,
00430 GLenum _buffer );
00431
00432
00434 unsigned int glWidth() const;
00436 unsigned int glHeight() const;
00438 QSize glSize() const;
00440 QPoint glMapFromGlobal( const QPoint& _pos ) const;
00442 QPoint glMapToGlobal( const QPoint& _pos ) const;
00443
00444
00446 void drawMode(ACG::SceneGraph::DrawModes::DrawMode _mode)
00447 {
00448 curDrawMode_=_mode;
00449 updatePopupMenu();
00450 }
00451
00453 ACG::SceneGraph::DrawModes::DrawMode drawMode() { return curDrawMode_; }
00454
00456 void encodeView(QString& _view);
00461 bool decodeView(const QString& _view);
00462
00464 void initModelviewMatrix();
00465
00466
00467 void grabGLArea();
00468
00469
00470 void releaseGLArea();
00471
00473 void drawBlendedObjects(bool _status) { blending_ = _status; }
00474
00476 void translate(const Vec3d& trans);
00477
00479 void rotate(const Vec3d& axis, double angle)
00480 { rotate(axis, angle, trackball_center_); }
00481
00483 void rotate(const Vec3d& axis, double angle, const Vec3d& _center);
00484
00486 QMenu * getPickMenu() { return pickMenu_; };
00487 QMenu * getFuncMenu() { return funcMenu_; };
00488 QMenu * getDrawMenu() { return drawMenu_; };
00489
00491 QToolBar* getToolBar();
00492
00494 QToolBar* removeToolBar();
00495
00497 void moveForward();
00498
00500 void moveBack();
00501
00503 void strafeLeft();
00504
00506 void strafeRight();
00507
00508
00509 public slots:
00510
00516 virtual void drawNow();
00518 virtual void updateGL();
00519
00521 virtual void setHome();
00523 virtual void home();
00525 virtual void viewAll();
00527 virtual void flyTo(const QPoint& _pos, bool _move_back);
00529 virtual void flyTo(const QPoint& _pos) { flyTo(_pos,false); }
00531 virtual void flyFrom(const QPoint& _pos) { flyTo(_pos,true); }
00532
00538 virtual void flyTo(const Vec3d& _position,
00539 const Vec3d& _center,
00540 double _time = 1000.0);
00541
00543 virtual void examineMode() { actionMode(ExamineMode); }
00545 virtual void lightMode() { actionMode(LightMode); }
00547 virtual void questionMode() { actionMode(QuestionMode); }
00548
00550 virtual void perspectiveProjection();
00552 virtual void orthographicProjection();
00554 virtual void toggleProjectionMode();
00556 virtual void toggleNavigationMode();
00557
00558 signals:
00559
00560 void projectionModeChanged( bool _ortho );
00561 void navigationModeChanged( bool _normal );
00562
00563 public slots:
00564
00566 virtual void showSceneGraphDialog();
00567
00569 virtual void setView( const GLMatrixd& _modelview,
00570 const GLMatrixd& _inverse_modelview );
00571
00572 void actionBackground();
00573 void actionCopyView();
00574 void actionPasteView();
00575 void actionPasteDropSize();
00576 void actionSynchronize();
00577 void actionAnimation();
00578 void actionBackfaceCulling();
00579 void actionTwoSidedLighting();
00580 void actionSynchronize(bool _enable);
00581 void actionAnimation(bool _enable);
00582 void actionBackfaceCulling(bool _enable);
00583 void actionTwoSidedLighting(bool _enable);
00584
00585 void actionDrawMenu( QAction * _action );
00586 void actionPickMenu( QAction * _action );
00587
00588
00589
00590
00591
00592 signals:
00593
00595 void signalInitializeGL();
00596
00607 void signalMouseEvent(QMouseEvent*, const std::string&);
00608
00610 void signalMouseEvent(QMouseEvent*);
00611
00613 void signalWheelEvent(QWheelEvent*, const std::string&);
00614
00618 void signalMouseEventIdentify( QMouseEvent* );
00619
00620
00622 void signalSetView( const GLMatrixd& _modelview,
00623 const GLMatrixd& _inverse_modelview );
00625 void signalSceneGraphChanged(ACG::SceneGraph::BaseNode* _root);
00627 void signalNodeChanged(ACG::SceneGraph::BaseNode* _node);
00629 void signalActionModeChanged(ACG::QtWidgets::QtBaseViewer::ActionMode _m);
00631 void signalDrawScene(ACG::GLState* _state);
00632
00633
00634 signals:
00635
00640 void signalCustomContextMenuRequested ( const QPoint & );
00641
00642
00643
00644 protected:
00645
00646 friend class QtGLGraphicsScene;
00647 friend class QtGLGraphicsView;
00648
00650 virtual void initializeGL();
00652 virtual void paintGL();
00654 virtual void resizeGL(int _w, int _h);
00655
00656
00658 virtual void glMousePressEvent(QMouseEvent* _event);
00660 virtual void glMouseDoubleClickEvent(QMouseEvent* _event);
00662 virtual void glMouseMoveEvent(QMouseEvent* _event);
00664 virtual void glMouseReleaseEvent(QMouseEvent* _event);
00666 virtual void glMouseWheelEvent(QWheelEvent* _event);
00668 virtual void glContextMenuEvent(QContextMenuEvent* _event);
00669
00670
00672 virtual void viewMouseEvent( QMouseEvent* _event) = 0;
00674 virtual void viewWheelEvent(QWheelEvent* _event) = 0;
00675
00677 virtual void lightMouseEvent( QMouseEvent* ) {}
00678
00679
00680 protected:
00681
00682
00684 void updateProjectionMatrix();
00686 void updatePickMenu();
00687
00688
00689
00690
00691 protected slots:
00692
00694 virtual void sceneRectChanged(const QRectF & rect);
00695
00697 virtual void slotWheelX(double _dAngle);
00699 virtual void slotWheelY(double _dAngle);
00701 virtual void slotWheelZ(double _dist);
00702
00704 virtual void slotNodeChanged(ACG::SceneGraph::BaseNode* _node);
00705
00707 virtual void cleanupEventFilter()
00708 { removeEventFilter( sender());}
00709
00710
00711
00712
00713 private slots:
00714
00715 void hidePopupMenus();
00716
00717
00718 private:
00719
00720
00721 enum FuncMenuID {
00722 M_CopyView=0x100,
00723 M_PasteView,
00724 M_PasteDropSize,
00725 M_Animation,
00726 M_BackfaceCulling,
00727 M_TwoSidedLighting,
00728 M_Background,
00729 M_Snapshot,
00730 M_SnapshotName,
00731 M_SnapshotSavesView,
00732 M_Synchronize
00733 };
00734
00736 QtBaseViewer(const QtBaseViewer&);
00738 QtBaseViewer& operator=(const QtBaseViewer&);
00739
00740
00741 void createWidgets(const QGLFormat* _format,QStatusBar* _sb,
00742 const QtBaseViewer* _share);
00743
00744
00745
00746 void drawScene();
00747
00748 void drawScene_mono();
00749
00750 void drawScene_stereo();
00751
00752
00753
00754 void updatePopupMenu();
00755
00756
00757 protected:
00758
00759
00760
00761 Vec3d scene_center_, trackball_center_;
00762 double scene_radius_, trackball_radius_;
00763
00764
00765
00766 GLdouble orthoWidth_;
00767 GLdouble near_, far_, fovy_;
00768
00769
00770
00771 GLState *glstate_;
00772
00773
00774
00775 bool isRotating_;
00776
00777
00778
00779 QStatusBar* statusbar_;
00780
00781
00782
00783
00784 private:
00785
00786
00787
00788 Vec3d home_center_;
00789 double home_radius_;
00790 GLMatrixd home_modelview_,
00791 home_inverse_modelview_;
00792 double homeOrthoWidth_;
00793
00794
00795
00796 GLMatrixd light_matrix_;
00797
00798
00799
00800 NormalsMode normalsMode_;
00801 FaceOrientation faceOrientation_;
00802 ProjectionMode projectionMode_;
00803 NavigationMode navigationMode_;
00804
00805 ActionMode actionMode_, lastActionMode_;
00806 bool backFaceCulling_;
00807 bool twoSidedLighting_;
00808 bool animation_;
00809
00810
00811 bool trackMouse_;
00812 bool popupEnabled_;
00813 bool glareaGrabbed_;
00814 double frame_time_;
00815
00816
00817 QMenu * pickMenu_;
00818 QMenu * funcMenu_;
00819 QMenu * drawMenu_;
00820
00821
00822 SceneGraph::BaseNode* sceneGraphRoot_;
00823
00824 ACG::SceneGraph::DrawModes::DrawMode curDrawMode_,
00825 availDrawModes_;
00826
00827 bool updateLocked_;
00828 bool projectionUpdateLocked_;
00829 bool blending_;
00830
00831
00832
00833 QtSceneGraphDialog* sceneGraphDialog_;
00834
00835
00836
00837
00838 Options options_;
00839
00840 QGLWidget* glWidget_;
00841
00842 QtGLGraphicsScene* glScene_;
00843
00844 QtGLGraphicsView* glView_;
00845
00846
00847 QGraphicsWidget* glBase_;
00848
00849 QGraphicsGridLayout* glBaseLayout_;
00850
00851
00852 QStatusBar* privateStatusBar_;
00853
00854
00855 QGridLayout* glLayout_;
00856
00857
00858 QToolBar * buttonBar_;
00859
00860
00861 QToolButton* pickButton_;
00862
00863 QToolButton* moveButton_;
00864
00865 QToolButton* lightButton_;
00866
00867 QToolButton* questionButton_;
00868
00869 QToolButton* projectionButton_;
00870
00871 QToolButton* homeButton_;
00872
00873 QToolButton* setHomeButton_;
00874
00875 QToolButton* viewAllButton_;
00876
00877 QToolButton* sceneGraphButton_;
00878
00879 QToolButton* stereoButton_;
00880
00881
00882 QtWheel* wheelX_;
00883
00884 QtWheel* wheelY_;
00885
00886 QtWheel* wheelZ_;
00887
00888
00889 QMap< QString, QAction * > action_;
00890
00891
00892 std::vector< QAction * > drawMenuActions_;
00893
00894
00895 QTime redrawTime_;
00896
00897
00900
00901 signals:
00903 void viewUpdated();
00904
00906 void viewChanged();
00907
00910
00913
00914
00915 public slots:
00916
00918 virtual void toggleStereoMode();
00919
00921 virtual void setStereoMode(bool _b);
00922
00923 public:
00928 void setEyeDistance(double _distance);
00929
00931 double eyeDistance( );
00932
00937 void setFocalDistance(double _distance);
00938
00940 double focalDistance( );
00941
00942 private:
00943
00944 bool stereo_;
00945 double eyeDist_, focalDist_;
00946
00947
00951
00954
00955
00956 signals:
00963 void signalKeyPressEvent(QKeyEvent*);
00964
00965 public:
00971 void disableKeyHandling(bool _state );
00972
00976 bool keyHandlingState();
00977
00978 protected:
00979
00984 virtual void glKeyPressEvent(QKeyEvent*);
00985
00992 virtual void glKeyReleaseEvent(QKeyEvent* _event);
00993
01001 virtual bool viewKeyPressEvent(QKeyEvent* _event) = 0;
01002
01003 private:
01004 bool disableKeyHandling_;
01005
01008
01011
01012
01013 protected:
01014
01016 virtual void startDrag();
01018 virtual void glDragEnterEvent(QDragEnterEvent* _event);
01020 virtual void glDropEvent(QDropEvent* _event);
01021
01022 public:
01024 void setExternalDrag( bool _external ) { externalDrag_ = _external; };
01025
01026 signals:
01030 void startDragEvent( QMouseEvent* _event );
01031
01035 void dragEnterEvent(QDragEnterEvent* _event);
01036
01040 void dropEvent( QDropEvent* _event );
01041
01042 private:
01043
01047 bool externalDrag_;
01048
01052
01055
01056
01057 public:
01062 void sync_connect(const QtBaseViewer*);
01063
01065 void sync_disconnect(const QtBaseViewer*);
01066
01068 virtual void setSynchronization(bool _b);
01069
01071 bool add_sync_host(const QString& _name);
01073 void add_sync_host(QHostAddress& _adr);
01074
01075 bool synchronization();
01076
01077 private slots:
01078
01079 void sync_receive();
01080 void sync_send(const GLMatrixd& _modelview,
01081 const GLMatrixd& _inverse_modelview);
01082
01083 private:
01085 bool synchronized_;
01086
01088 bool skipNextSync_;
01089
01091 QUdpSocket * socket_;
01092
01094 std::vector<QHostAddress> sync_hosts_;
01095
01098
01101
01102
01103 public slots:
01115 virtual void snapshotBaseFileName(const QString& _fname);
01116
01121 virtual void snapshot();
01122
01125 void actionSnapshot();
01126
01129 void actionSnapshotName();
01130
01133 void actionSnapshotSavesView();
01134
01135 private:
01136
01137 QString snapshotName_;
01138 int snapshotCounter_;
01139 QImage* snapshot_;
01140
01143
01146
01147 public:
01161 bool pick( SceneGraph::PickTarget _pickTarget,
01162 const QPoint& _mousePos,
01163 unsigned int& _nodeIdx,
01164 unsigned int& _targetIdx,
01165 Vec3d* _hitPointPtr=0 );
01166
01172 bool fast_pick( const QPoint& _mousePos,
01173 Vec3d& _hitPoint );
01174
01181 void addPickMode(const std::string& _name,
01182 bool _mouse_tracking = false,
01183 int _pos = -1,
01184 bool _visible = true,
01185 QCursor _cursor = Qt::ArrowCursor );
01186
01189 void clearPickModes();
01190
01193 const std::string& pickMode() const;
01194
01198 void pickMode(const std::string& _name);
01199
01203 void pickMode( int _id );
01204
01209 void renderPicking(bool _renderPicking, ACG::SceneGraph::PickTarget _mode);
01210
01211 public slots:
01212
01216 virtual void pickingMode();
01217
01223 void setPickModeCursor(const std::string& _name, QCursor _cursor);
01224
01230 void setPickModeMouseTracking(const std::string& _name, bool _mouseTracking);
01231
01232 signals:
01235 void signalPickModeChanged(const std::string&);
01236
01237 private:
01238
01241 struct PickMode
01242 {
01244 PickMode(const std::string& _n, bool _t, bool _v, QCursor _c) :
01245 name(_n), tracking(_t), visible(_v), cursor(_c) {}
01246
01249 std::string name;
01250
01253 bool tracking;
01254
01257 bool visible;
01258
01261 QCursor cursor;
01262 };
01263
01266 std::vector<PickMode> pick_modes_;
01267
01270 std::string pick_mode_name_;
01271
01274 int pick_mode_idx_;
01275
01278 bool renderPicking_;
01279
01284 ACG::SceneGraph::PickTarget pickRendererMode_;
01285
01287 };
01288
01289
01290
01291 }
01292 }
01293
01294 #endif // ACG_QTBASEVIEWER_HH defined
01295