Salome HOME
9653aeacf11aa2686004790210b42f842454c0c2
[modules/gui.git] / src / VTKViewer / VTKViewer_ViewModel.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "VTKViewer_ViewModel.h"
20 #include "VTKViewer_ViewWindow.h"
21 #include "VTKViewer_ViewManager.h"
22 #include "VTKViewer_RenderWindowInteractor.h"
23
24 #include "SUIT_ViewWindow.h"
25 #include "SUIT_Desktop.h"
26 #include "SUIT_Session.h"
27
28 #include <qpainter.h>
29 #include <qapplication.h>
30 #include <qcolordialog.h>
31 #include <qpalette.h>
32 #include <qpopupmenu.h>
33
34 /*!Constructor.Sets background color to black.*/
35 VTKViewer_Viewer::VTKViewer_Viewer()
36 : SUIT_ViewModel(),
37 myBgColor( Qt::black )
38 {
39 }
40
41 /*!Destructor.*/
42 VTKViewer_Viewer::~VTKViewer_Viewer() 
43 {
44 }
45
46 /*!Gets background color.*/
47 QColor VTKViewer_Viewer::backgroundColor() const
48 {
49   return myBgColor;
50 }
51
52 /*!Sets background color.*/
53 void VTKViewer_Viewer::setBackgroundColor( const QColor& c )
54 {
55   if ( c.isValid() )
56     myBgColor = c;
57 }
58
59 /*!Create new instance of VTKViewer_ViewWindow, sets background color and return pointer to it.*/
60 SUIT_ViewWindow* VTKViewer_Viewer::createView( SUIT_Desktop* theDesktop )
61 {
62   VTKViewer_ViewWindow* vw = new VTKViewer_ViewWindow( theDesktop, this );
63   vw->setBackgroundColor( myBgColor );
64   return vw;
65 }
66
67 /*!Sets view manager and connect slots.*/
68 void VTKViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
69 {
70   SUIT_ViewModel::setViewManager(theViewManager);
71   connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
72           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
73
74   connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
75           this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
76
77   connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
78           this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
79 }
80
81 /*!Insert context into popup menu.*/
82 void VTKViewer_Viewer::contextMenuPopup(QPopupMenu* thePopup)
83 {
84   thePopup->insertItem( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
85   thePopup->insertItem( tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
86
87   thePopup->insertSeparator();
88
89   VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
90   if ( aView && !aView->getToolBar()->isVisible() )
91     thePopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
92 }
93
94 /*!On mouse press event.*/
95 void VTKViewer_Viewer::onMousePress(SUIT_ViewWindow* vw, QMouseEvent* event)
96 {
97   VTKViewer_RenderWindowInteractor* rwi = 0;
98   if ( vw && vw->inherits( "VTKViewer_ViewWindow" ) )
99     rwi = ((VTKViewer_ViewWindow*)vw)->getRWInteractor();
100   if ( !rwi )
101     return;
102
103   switch(event->button()) {
104   case LeftButton:
105     rwi->LeftButtonPressed(event) ;
106     break ;
107   case MidButton:
108     rwi->MiddleButtonPressed(event) ;
109     break ;
110   case RightButton:
111     rwi->RightButtonPressed(event) ;
112     break;
113   default:
114     break ;
115   }
116 }
117
118 /*!On mouse move event.*/
119 void VTKViewer_Viewer::onMouseMove(SUIT_ViewWindow* vw, QMouseEvent* event)
120 {
121   VTKViewer_RenderWindowInteractor* rwi = 0;
122   if ( vw && vw->inherits( "VTKViewer_ViewWindow" ) )
123     rwi = ((VTKViewer_ViewWindow*)vw)->getRWInteractor();
124   if ( rwi )
125     rwi->MouseMove( event );
126 }
127
128 /*!On mouse release event.*/
129 void VTKViewer_Viewer::onMouseRelease(SUIT_ViewWindow* vw, QMouseEvent* event)
130 {
131   VTKViewer_RenderWindowInteractor* rwi = 0;
132   if ( vw && vw->inherits( "VTKViewer_ViewWindow" ) )
133     rwi = ((VTKViewer_ViewWindow*)vw)->getRWInteractor();
134   if ( !rwi )
135     return;
136
137   switch(event->button()) {
138   case LeftButton:
139     rwi->LeftButtonReleased(event) ;
140     break ;
141   case MidButton:
142     rwi->MiddleButtonReleased(event) ;
143     break ;
144   case RightButton:
145     rwi->RightButtonReleased(event) ;
146     break;
147   default:
148     break ;
149   }
150 }
151
152 /*!Sets flag to enable selection \a isEnable.*/
153 void VTKViewer_Viewer::enableSelection(bool isEnabled)
154 {
155   mySelectionEnabled = isEnabled;
156   //!! To be done for view windows
157 }
158
159 /*!Sets flag to multi selection enable \a isEnable.*/
160 void VTKViewer_Viewer::enableMultiselection(bool isEnable)
161 {
162   myMultiSelectionEnabled = isEnable;
163   //!! To be done for view windows
164 }
165
166 /*!On dump view event.*/
167 void VTKViewer_Viewer::onDumpView()
168 {
169   VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
170   if ( aView )
171     aView->onDumpView();
172 }
173
174 /*!On change back ground color event.*/
175 void VTKViewer_Viewer::onChangeBgColor()
176 {
177   VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
178   if ( !aView )
179     return;
180
181   QColor aColor = QColorDialog::getColor( aView->backgroundColor(), aView);
182   if ( aColor.isValid() )
183     aView->setBackgroundColor(aColor);
184 }
185
186 /*!On show tool bar event.*/
187 void VTKViewer_Viewer::onShowToolbar() {
188   VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
189   if ( aView )
190     aView->getToolBar()->show();    
191 }