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
00060 #include <QtOpenGL/QGLFormat>
00061 #include <QBoxLayout>
00062 #include <QtNetwork/QUdpSocket>
00063 #include <QWheelEvent>
00064 #include <QKeyEvent>
00065 #include <QDropEvent>
00066 #include <QContextMenuEvent>
00067 #include <QDragEnterEvent>
00068 #include <QMouseEvent>
00069 #include <QAction>
00070 #include <QSize>
00071 #include <QMap>
00072 #include <QString>
00073 #include <QMenu>
00074 #include <QToolBar>
00075 #include <QTime>
00076
00077 #include <vector>
00078 #include <string>
00079
00080
00081
00082
00083
00084 class QPushButton;
00085 class QToolButton;
00086 class QStatusBar;
00087 class QSplitter;
00088 class QTimer;
00089 class QImage;
00090 class QSocketNotifier;
00091 class QGraphicsWidget;
00092 class QGraphicsGridLayout;
00093
00094
00095
00096
00097
00098 namespace ACG {
00099 namespace QtWidgets {
00100
00101
00102
00103
00104
00105 class QtWheel;
00106 class QtGLGraphicsScene;
00107 class QtGLGraphicsView;
00108 class QtSceneGraphDialog;
00109 class QtShiftPopupMenu;
00110
00111
00112
00113
00114
00122 class ACGDLLEXPORT QtBaseViewer : public QWidget
00123 {
00124 Q_OBJECT
00125
00126
00127 public:
00128
00132 enum Options {
00134 Nothing=0,
00135
00142 ShowPrivateStatusBar=1,
00143
00153 ShowToolBar=2,
00154
00156 ShowPickButton=4,
00158 ShowLassoButton=8,
00160 ShowQuestionButton=0x10,
00161
00163 ShowWheelX=0x20,
00165 ShowWheelY=0x40,
00169 ShowWheelZ=0x80,
00170
00172 ShowWheels=0xe0,
00173
00175 DefaultOptions = 0xffff & (~ShowPrivateStatusBar)
00176 };
00177
00178
00179
00180
00181
00182
00191 QtBaseViewer( QWidget* _parent=0,
00192 const char* _name=0,
00193 QStatusBar *_statusBar=0,
00194 const QGLFormat* _format=0,
00195 const QtBaseViewer* _share=0,
00196 Options _options=DefaultOptions );
00197
00199 virtual ~QtBaseViewer();
00200
00201
00202
00203
00204
00205
00206
00207 virtual QSize sizeHint () const;
00208
00210 QStatusBar* statusBar() { return statusbar_; }
00219 void setStatusBar(QStatusBar* _sb);
00220
00222 void applyOptions(int _options);
00224 int options() const { return options_; }
00225
00226
00228 virtual void makeCurrent();
00230 virtual void swapBuffers();
00231
00232
00234 GLState& glState() { return *glstate_; }
00235
00236
00240 void backgroundColor(const Vec4f& _color) {
00241 glstate_->set_clear_color(_color); updateGL();
00242 }
00244 Vec4f backgroundColor() { return glstate_->clear_color(); }
00245
00246
00252 virtual void lockUpdate();
00254 virtual void unlockUpdate();
00259 virtual void unlockAndUpdate();
00262 bool isUpdateLocked() const { return updateLocked_; }
00263
00264
00266 void lockProjectionUpdate( void ) { projectionUpdateLocked_ = true; }
00267
00269 void unlockProjectionUpdate( void ) { projectionUpdateLocked_ = false; }
00270
00272 bool animation() const { return animation_; }
00274 void animation(bool _b);
00275
00277 void trackMouse(bool _track);
00278
00280 void enablePopupMenu(bool _enable);
00281
00282
00283
00284
00285
00286
00288 SceneGraph::BaseNode* sceneGraph() { return sceneGraphRoot_; }
00290 const SceneGraph::BaseNode* sceneGraph() const { return sceneGraphRoot_; }
00298 void sceneGraph(SceneGraph::BaseNode* _root);
00299
00301 enum NavigationMode {
00302 NORMAL_NAVIGATION,
00303 FIRSTPERSON_NAVIGATION
00304 };
00305
00307 enum ProjectionMode {
00308 ORTHOGRAPHIC_PROJECTION,
00309 PERSPECTIVE_PROJECTION
00310 };
00311
00313 void projectionMode(ProjectionMode _p);
00315 ProjectionMode projectionMode() const { return projectionMode_; }
00316
00318 void navigationMode(NavigationMode _n);
00320 NavigationMode navigationMode() const { return navigationMode_; }
00321
00326 void setScenePos( const ACG::Vec3d& _center, double _radius, const bool _setCenter = true );
00327
00331 void setSceneCenter( const ACG::Vec3d& _center );
00332
00336 const ACG::Vec3d& scene_center() const { return scene_center_; }
00340 double scene_radius() const { return scene_radius_; }
00341
00343 void viewingDirection( const ACG::Vec3d& _dir, const ACG::Vec3d& _up );
00344
00346 enum ActionMode {
00347
00348 ExamineMode,
00352 PickingMode,
00353
00354 QuestionMode,
00355
00356 LightMode
00357 };
00358
00359
00365 void actionMode(ActionMode);
00366
00368 ActionMode lastActionMode(){ return lastActionMode_; };
00369
00371 ActionMode actionMode() const { return actionMode_; }
00372
00373
00375 enum FaceOrientation {
00376 CCW_ORIENTATION,
00377 CW_ORIENTATION
00378 };
00379
00381 void faceOrientation(FaceOrientation);
00383 FaceOrientation faceOrientation() const { return faceOrientation_; }
00384
00386 void backFaceCulling( bool _b );
00388 bool backFaceCulling() const { return backFaceCulling_; }
00389
00391 void twoSidedLighting( bool _b );
00393 bool twoSidedLighting() const { return twoSidedLighting_; }
00394
00396 enum NormalsMode {
00398 DONT_TOUCH_NORMALS,
00403 NORMALIZE_NORMALS
00404 };
00405
00407 void normalsMode(NormalsMode _mode);
00409 NormalsMode normalsMode() const { return normalsMode_; }
00410
00411
00413 GLMatrixd& light_matrix() { return light_matrix_; }
00415 void rotate_lights(Vec3d& _axis, double _angle);
00417 void update_lights();
00418
00419
00421 void copyToImage( QImage& _image, GLenum _buffer=GL_BACK) {
00422 copyToImage(_image, 0, 0, glWidth(), glHeight(), _buffer);
00423 }
00424
00426 void copyToImage( QImage& _image,
00427 unsigned int _left, unsigned int _top,
00428 unsigned int _width, unsigned int _height,
00429 GLenum _buffer );
00430
00431
00433 unsigned int glWidth() const;
00435 unsigned int glHeight() const;
00437 QSize glSize() const;
00439 QPoint glMapFromGlobal( const QPoint& _pos ) const;
00441 QPoint glMapToGlobal( const QPoint& _pos ) const;
00442
00443
00445 void drawMode(unsigned int _mode)
00446 {
00447 curDrawMode_=_mode;
00448 updatePopupMenu();
00449 }
00450
00452 unsigned int drawMode() { return curDrawMode_; }
00453
00455 void encodeView(QString& _view);
00460 bool decodeView(const QString& _view);
00461
00463 void initModelviewMatrix();
00464
00465
00466 void grabGLArea();
00467
00468
00469 void releaseGLArea();
00470
00472 void drawBlendedObjects(bool _status) { blending_ = _status; }
00473
00475 void translate(const Vec3d& trans);
00476
00478 void rotate(const Vec3d& axis, double angle)
00479 { rotate(axis, angle, trackball_center_); }
00480
00482 void rotate(const Vec3d& axis, double angle, const Vec3d& _center);
00483
00485 QMenu * getPickMenu() { return pickMenu_; };
00486 QMenu * getFuncMenu() { return funcMenu_; };
00487 QMenu * getDrawMenu() { return drawMenu_; };
00488
00490 QToolBar* getToolBar();
00491
00493 QToolBar* removeToolBar();
00494
00496 void moveForward();
00497
00499 void moveBack();
00500
00502 void strafeLeft();
00503
00505 void strafeRight();
00506
00507
00508 public slots:
00509
00515 virtual void drawNow();
00517 virtual void updateGL();
00518
00520 virtual void setHome();
00522 virtual void home();
00524 virtual void viewAll();
00526 virtual void flyTo(const QPoint& _pos, bool _move_back);
00528 virtual void flyTo(const QPoint& _pos) { flyTo(_pos,false); }
00530 virtual void flyFrom(const QPoint& _pos) { flyTo(_pos,true); }
00531
00537 virtual void flyTo(const Vec3d& _position,
00538 const Vec3d& _center,
00539 double _time = 1000.0);
00540
00542 virtual void examineMode() { actionMode(ExamineMode); }
00544 virtual void lightMode() { actionMode(LightMode); }
00546 virtual void questionMode() { actionMode(QuestionMode); }
00547
00549 virtual void perspectiveProjection();
00551 virtual void orthographicProjection();
00553 virtual void toggleProjectionMode();
00555 virtual void toggleNavigationMode();
00556
00557 signals:
00558
00559 void projectionModeChanged( bool _ortho );
00560 void navigationModeChanged( bool _normal );
00561
00562 public slots:
00563
00565 virtual void showSceneGraphDialog();
00566
00568 virtual void setView( const GLMatrixd& _modelview,
00569 const GLMatrixd& _inverse_modelview );
00570
00571 void actionBackground();
00572 void actionCopyView();
00573 void actionPasteView();
00574 void actionPasteDropSize();
00575 void actionSynchronize();
00576 void actionAnimation();
00577 void actionBackfaceCulling();
00578 void actionTwoSidedLighting();
00579 void actionSynchronize(bool _enable);
00580 void actionAnimation(bool _enable);
00581 void actionBackfaceCulling(bool _enable);
00582 void actionTwoSidedLighting(bool _enable);
00583
00584 void actionDrawMenu( QAction * _action );
00585 void actionPickMenu( QAction * _action );
00586
00587
00588
00589
00590
00591 signals:
00592
00594 void signalInitializeGL();
00595
00606 void signalMouseEvent(QMouseEvent*, const std::string&);
00607
00609 void signalMouseEvent(QMouseEvent*);
00610
00612 void signalWheelEvent(QWheelEvent*, const std::string&);
00613
00617 void signalMouseEventIdentify( QMouseEvent* );
00618
00619
00621 void signalSetView( const GLMatrixd& _modelview,
00622 const GLMatrixd& _inverse_modelview );
00624 void signalSceneGraphChanged(ACG::SceneGraph::BaseNode* _root);
00626 void signalNodeChanged(ACG::SceneGraph::BaseNode* _node);
00628 void signalActionModeChanged(ACG::QtWidgets::QtBaseViewer::ActionMode _m);
00630 void signalDrawScene(ACG::GLState* _state);
00631
00632
00633 signals:
00634
00639 void signalCustomContextMenuRequested ( const QPoint & );
00640
00641
00642
00643 protected:
00644
00645 friend class QtGLGraphicsScene;
00646 friend class QtGLGraphicsView;
00647
00649 virtual void initializeGL();
00651 virtual void paintGL();
00653 virtual void resizeGL(int _w, int _h);
00654
00655
00657 virtual void glMousePressEvent(QMouseEvent* _event);
00659 virtual void glMouseDoubleClickEvent(QMouseEvent* _event);
00661 virtual void glMouseMoveEvent(QMouseEvent* _event);
00663 virtual void glMouseReleaseEvent(QMouseEvent* _event);
00665 virtual void glMouseWheelEvent(QWheelEvent* _event);
00667 virtual void glContextMenuEvent(QContextMenuEvent* _event);
00668
00669
00671 virtual void viewMouseEvent( QMouseEvent* _event) = 0;
00673 virtual void viewWheelEvent(QWheelEvent* _event) = 0;
00674
00676 virtual void lightMouseEvent( QMouseEvent* ) {}
00677
00678
00679 protected:
00680
00681
00683 void updateProjectionMatrix();
00685 void updatePickMenu();
00686
00687
00688
00689
00690 protected slots:
00691
00693 virtual void sceneRectChanged(const QRectF & rect);
00694
00696 virtual void slotWheelX(double _dAngle);
00698 virtual void slotWheelY(double _dAngle);
00700 virtual void slotWheelZ(double _dist);
00701
00703 virtual void slotNodeChanged(ACG::SceneGraph::BaseNode* _node);
00704
00706 virtual void cleanupEventFilter()
00707 { removeEventFilter( sender());}
00708
00709
00710
00711
00712 private slots:
00713
00714 void hidePopupMenus();
00715
00716
00717 private:
00718
00719
00720 enum FuncMenuID {
00721 M_CopyView=0x100,
00722 M_PasteView,
00723 M_PasteDropSize,
00724 M_Animation,
00725 M_BackfaceCulling,
00726 M_TwoSidedLighting,
00727 M_Background,
00728 M_Snapshot,
00729 M_SnapshotName,
00730 M_SnapshotSavesView,
00731 M_Synchronize
00732 };
00733
00735 QtBaseViewer(const QtBaseViewer&);
00737 QtBaseViewer& operator=(const QtBaseViewer&);
00738
00739
00740 void createWidgets(const QGLFormat* _format,QStatusBar* _sb,
00741 const QtBaseViewer* _share);
00742
00743
00744
00745 void drawScene();
00746
00747 void drawScene_mono();
00748
00749 void drawScene_stereo();
00750
00751
00752
00753 void updatePopupMenu();
00754
00755
00756 protected:
00757
00758
00759
00760 Vec3d scene_center_, trackball_center_;
00761 double scene_radius_, trackball_radius_;
00762
00763
00764
00765 GLdouble orthoWidth_;
00766 GLdouble near_, far_, fovy_;
00767
00768
00769
00770 GLState *glstate_;
00771
00772
00773
00774 bool isRotating_;
00775
00776
00777
00778 QStatusBar* statusbar_;
00779
00780
00781
00782
00783 private:
00784
00785
00786
00787 Vec3d home_center_;
00788 double home_radius_;
00789 GLMatrixd home_modelview_,
00790 home_inverse_modelview_;
00791 double homeOrthoWidth_;
00792
00793
00794
00795 GLMatrixd light_matrix_;
00796
00797
00798
00799 NormalsMode normalsMode_;
00800 FaceOrientation faceOrientation_;
00801 ProjectionMode projectionMode_;
00802 NavigationMode navigationMode_;
00803
00804 ActionMode actionMode_, lastActionMode_;
00805 bool backFaceCulling_;
00806 bool twoSidedLighting_;
00807 bool animation_;
00808
00809
00810 bool trackMouse_;
00811 bool popupEnabled_;
00812 bool glareaGrabbed_;
00813 double frame_time_;
00814
00815
00816 QMenu * pickMenu_;
00817 QMenu * funcMenu_;
00818 QMenu * drawMenu_;
00819
00820
00821 SceneGraph::BaseNode* sceneGraphRoot_;
00822
00823 unsigned int curDrawMode_,
00824 availDrawModes_;
00825
00826 bool updateLocked_;
00827 bool projectionUpdateLocked_;
00828 bool blending_;
00829
00830
00831
00832 QtSceneGraphDialog* sceneGraphDialog_;
00833
00834
00835
00836
00837 Options options_;
00838
00839 QGLWidget* glWidget_;
00840
00841 QtGLGraphicsScene* glScene_;
00842
00843 QtGLGraphicsView* glView_;
00844
00845
00846 QGraphicsWidget* glBase_;
00847
00848 QGraphicsGridLayout* glBaseLayout_;
00849
00850
00851 QStatusBar* privateStatusBar_;
00852
00853
00854 QGridLayout* glLayout_;
00855
00856
00857 QToolBar * buttonBar_;
00858
00859
00860 QToolButton* pickButton_;
00861
00862 QToolButton* moveButton_;
00863
00864 QToolButton* lightButton_;
00865
00866 QToolButton* questionButton_;
00867
00868 QToolButton* projectionButton_;
00869
00870 QToolButton* homeButton_;
00871
00872 QToolButton* setHomeButton_;
00873
00874 QToolButton* viewAllButton_;
00875
00876 QToolButton* sceneGraphButton_;
00877
00878 QToolButton* stereoButton_;
00879
00880
00881 QtWheel* wheelX_;
00882
00883 QtWheel* wheelY_;
00884
00885 QtWheel* wheelZ_;
00886
00887
00888 QMap< QString, QAction * > action_;
00889
00890
00891 std::vector< QAction * > drawMenuActions_;
00892
00893
00894 QTime redrawTime_;
00895
00896
00899
00900 signals:
00902 void viewUpdated();
00903
00905 void viewChanged();
00906
00909
00912
00913
00914 public slots:
00915
00917 virtual void toggleStereoMode();
00918
00920 virtual void setStereoMode(bool _b);
00921
00922 public:
00927 void setEyeDistance(double _distance);
00928
00930 double eyeDistance( );
00931
00936 void setFocalDistance(double _distance);
00937
00939 double focalDistance( );
00940
00941 private:
00942
00943 bool stereo_;
00944 double eyeDist_, focalDist_;
00945
00946
00950
00953
00954
00955 signals:
00962 void signalKeyPressEvent(QKeyEvent*);
00963
00964 public:
00970 void disableKeyHandling(bool _state );
00971
00975 bool keyHandlingState();
00976
00977 protected:
00978
00983 virtual void glKeyPressEvent(QKeyEvent*);
00984
00991 virtual void glKeyReleaseEvent(QKeyEvent* _event);
00992
01000 virtual bool viewKeyPressEvent(QKeyEvent* _event) = 0;
01001
01002 private:
01003 bool disableKeyHandling_;
01004
01007
01010
01011
01012 protected:
01013
01015 virtual void startDrag();
01017 virtual void glDragEnterEvent(QDragEnterEvent* _event);
01019 virtual void glDropEvent(QDropEvent* _event);
01020
01021 public:
01023 void setExternalDrag( bool _external ) { externalDrag_ = _external; };
01024
01025 signals:
01029 void startDragEvent( QMouseEvent* _event );
01030
01034 void dragEnterEvent(QDragEnterEvent* _event);
01035
01039 void dropEvent( QDropEvent* _event );
01040
01041 private:
01042
01046 bool externalDrag_;
01047
01051
01054
01055
01056 public:
01061 void sync_connect(const QtBaseViewer*);
01062
01064 void sync_disconnect(const QtBaseViewer*);
01065
01067 virtual void setSynchronization(bool _b);
01068
01070 bool add_sync_host(const QString& _name);
01072 void add_sync_host(QHostAddress& _adr);
01073
01074 bool synchronization();
01075
01076 private slots:
01077
01078 void sync_receive();
01079 void sync_send(const GLMatrixd& _modelview,
01080 const GLMatrixd& _inverse_modelview);
01081
01082 private:
01084 bool synchronized_;
01085
01087 bool skipNextSync_;
01088
01090 QUdpSocket * socket_;
01091
01093 std::vector<QHostAddress> sync_hosts_;
01094
01097
01100
01101
01102 public slots:
01114 virtual void snapshotBaseFileName(const QString& _fname);
01115
01120 virtual void snapshot();
01121
01124 void actionSnapshot();
01125
01128 void actionSnapshotName();
01129
01132 void actionSnapshotSavesView();
01133
01134 private:
01135
01136 QString snapshotName_;
01137 int snapshotCounter_;
01138 QImage* snapshot_;
01139
01142
01145
01146 public:
01160 bool pick( SceneGraph::PickTarget _pickTarget,
01161 const QPoint& _mousePos,
01162 unsigned int& _nodeIdx,
01163 unsigned int& _targetIdx,
01164 Vec3d* _hitPointPtr=0 );
01165
01171 bool fast_pick( const QPoint& _mousePos,
01172 Vec3d& _hitPoint );
01173
01180 void addPickMode(const std::string& _name,
01181 bool _mouse_tracking = false,
01182 int _pos = -1,
01183 bool _visible = true,
01184 QCursor _cursor = Qt::ArrowCursor );
01185
01188 void clearPickModes();
01189
01192 const std::string& pickMode() const;
01193
01197 void pickMode(const std::string& _name);
01198
01202 void pickMode( int _id );
01203
01208 void renderPicking(bool _renderPicking, ACG::SceneGraph::PickTarget _mode);
01209
01210 public slots:
01211
01215 virtual void pickingMode();
01216
01222 void setPickModeCursor(const std::string& _name, QCursor _cursor);
01223
01229 void setPickModeMouseTracking(const std::string& _name, bool _mouseTracking);
01230
01231 signals:
01234 void signalPickModeChanged(const std::string&);
01235
01236 private:
01237
01240 struct PickMode
01241 {
01243 PickMode(const std::string& _n, bool _t, bool _v, QCursor _c) :
01244 name(_n), tracking(_t), visible(_v), cursor(_c) {}
01245
01248 std::string name;
01249
01252 bool tracking;
01253
01256 bool visible;
01257
01260 QCursor cursor;
01261 };
01262
01265 std::vector<PickMode> pick_modes_;
01266
01269 std::string pick_mode_name_;
01270
01273 int pick_mode_idx_;
01274
01277 bool renderPicking_;
01278
01283 ACG::SceneGraph::PickTarget pickRendererMode_;
01284
01286 };
01287
01288
01289
01290 }
01291 }
01292
01293 #endif // ACG_QTBASEVIEWER_HH defined
01294