Salome HOME
Porting to Mandrake 10.1 and new products:
[modules/kernel.git] / src / VTKViewer / VTKViewer_RenderWindow.cxx
1 //  SALOME VTKViewer : build VTK viewer into Salome desktop
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : VTKViewer_RenderWindow.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 #include "VTKViewer_RenderWindow.h"
30 #include "utilities.h"
31 #include "QAD_Settings.h"
32 #include "QAD_Config.h"
33 #include "QAD_Desktop.h"
34 #include "QAD_Study.h"
35 #include "QAD_Tools.h"
36 #include "SALOME_Selection.h"
37
38 #include <qcolordialog.h>
39
40 #include <stdlib.h>
41 #include <math.h>
42 #include <iostream.h>
43
44 #include <vtkRenderWindowInteractor.h>
45 #include <vtkRendererCollection.h>
46 #include <vtkXOpenGLRenderWindow.h>
47 #include <vtkCamera.h>
48
49 //#include <GL/gl.h>
50 //#include <GL/glu.h>
51 //#include <qgl.h>
52
53 #if QT_VERSION > 300
54 #include <qcursor.h>
55 #endif
56 using namespace std;
57
58 VTKViewer_RenderWindow::VTKViewer_RenderWindow(QWidget *parent, const char *name) :
59   QWidget(parent, name, 
60           Qt::WStyle_NoBorder | Qt::WDestructiveClose | 
61           Qt::WResizeNoErase | Qt::WRepaintNoErase)
62 {
63   myRW = vtkRenderWindow::New();
64   MESSAGE(QString("RenderWindow is ") + QString(myRW->GetClassName()));
65   myRW->SetDisplayId((void*)x11Display());
66   myRW->SetWindowId((void*)winId());
67   myRW->DoubleBufferOn();
68   setMouseTracking(true);
69 }
70
71 VTKViewer_RenderWindow::~VTKViewer_RenderWindow() {
72   myRW->Delete(); //BUG SAL2954, Commented by SRN, to avoid problems when using rlogin
73   // Uncommented because of bug SAL3913
74 }
75
76 void VTKViewer_RenderWindow::paintEvent(QPaintEvent* theEvent) {
77   myRW->Render();
78 }
79
80 void VTKViewer_RenderWindow::resizeEvent(QResizeEvent* theEvent) {
81   int aWidth = myRW->GetSize()[0], aHeight = myRW->GetSize()[1];
82   if(vtkRenderWindowInteractor* aRWI = myRW->GetInteractor())
83     aRWI->UpdateSize(width(), height());
84   if(aWidth != width() || aHeight != height()){
85     vtkRendererCollection * aRenderers = myRW->GetRenderers();
86     aRenderers->InitTraversal();
87     double aCoeff = 1.0;
88     if(vtkRenderer *aRenderer = aRenderers->GetNextItem()){
89       vtkCamera *aCamera = aRenderer->GetActiveCamera();
90       double aScale = aCamera->GetParallelScale();
91       if((aWidth - width())*(aHeight - height()) > 0)
92         aCoeff = sqrt(double(aWidth)/double(width())*double(height())/double(aHeight));
93       else
94         aCoeff = double(aWidth)/double(width());
95       aCamera->SetParallelScale(aScale*aCoeff);
96     }
97   }
98 }
99
100
101 void VTKViewer_RenderWindow::mouseMoveEvent( QMouseEvent *event ) {
102   emit MouseMove(event) ;
103 }
104
105 void VTKViewer_RenderWindow::mousePressEvent( QMouseEvent *event ) {
106   emit ButtonPressed(event) ;
107   switch(event->button()) {
108   case LeftButton:
109     emit LeftButtonPressed(event) ;
110     break ;
111   case MidButton:
112     emit MiddleButtonPressed(event) ;
113     break ;
114   case RightButton:
115     if ( event->state() == Qt::ControlButton ) {
116       emit RightButtonPressed(event) ;
117     } else {
118       QPopupMenu* popup = createPopup();
119       if ( popup ) {
120         QAD_Tools::checkPopup( popup );
121         if ( popup->count()>0 ) {
122           popup->exec( QCursor::pos() );
123         }
124         destroyPopup();
125       }
126     }
127     break;
128   default:
129     break ;
130   }
131 }
132
133
134 void VTKViewer_RenderWindow::mouseReleaseEvent( QMouseEvent *event ) {
135   emit ButtonReleased(event) ;
136   switch(event->button()) {
137   case LeftButton:
138     emit LeftButtonReleased(event) ;
139     break ;
140   case MidButton:
141     emit MiddleButtonReleased(event) ;
142     break ;
143   case RightButton:
144     emit RightButtonReleased(event) ;
145     break;
146   default:
147     break ;
148   }
149 }
150
151 void VTKViewer_RenderWindow::keyPressEvent (QKeyEvent * event) {
152   emit KeyPressed(event) ;
153 }
154
155 /*!
156     Creates the popup 
157 */
158 void VTKViewer_RenderWindow::onCreatePopup() 
159 {
160   if ( myPopup ) {      
161     QAD_Desktop*     Desktop = (QAD_Desktop*) QAD_Application::getDesktop();
162     QAD_Study*   myActiveStudy = Desktop->getActiveStudy();
163     SALOME_Selection*      Sel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
164     
165     QString theContext;
166     QString theParent("Viewer");
167     QString theObject;
168     
169     Desktop->definePopup( theContext, theParent, theObject );
170     Desktop->createPopup( myPopup, theContext, theParent, theObject);
171     Desktop->customPopup( myPopup, theContext, theParent, theObject );
172
173 //    if (Sel->IObjectCount() == 0 && myPopup->count()<1) {
174     if ( myPopup->count() > 0 )
175       myIDs.append ( myPopup->insertSeparator() );      
176     int id;
177     myIDs.append ( id = myPopup->insertItem (tr ("MEN_VP3D_CHANGEBGR")) );      
178     QAD_ASSERT ( myPopup->connectItem ( id, this, SLOT(onChangeBackgroundColor())) );
179     myIDs.append ( id = myPopup->insertItem (tr ("MEN_VP3D_DUMPVIEW")) );       
180     QAD_ASSERT ( myPopup->connectItem ( id, this, SIGNAL(DumpView())) );
181 //    }
182   }
183 }
184
185
186 void VTKViewer_RenderWindow::onChangeBackgroundColor()
187 {
188   float red, green, blue;
189   float backint[3];
190
191   vtkRendererCollection * theRenderers = myRW->GetRenderers();
192   theRenderers->InitTraversal();
193   vtkRenderer * theRenderer = theRenderers->GetNextItem();
194   theRenderer->GetBackground(backint);
195
196   QColor selColor = QColorDialog::getColor ( QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255)), NULL );     
197   if ( selColor.isValid() ) {
198     theRenderer->SetBackground( selColor.red()/255., selColor.green()/255., selColor.blue()/255. ); 
199     /* VSR : PAL5420 ---------------------------------------------------
200     QAD_CONFIG->addSetting( "VTKViewer:BackgroundColorRed",   selColor.red() );
201     QAD_CONFIG->addSetting( "VTKViewer:BackgroundColorGreen", selColor.green() );
202     QAD_CONFIG->addSetting( "VTKViewer:BackgroundColorBlue",  selColor.blue() );
203     VSR : PAL5420 --------------------------------------------------- */
204   }
205 }