]> SALOME platform Git repositories - modules/kernel.git/blob - src/VTKViewer/VTKViewer_RenderWindow.cxx
Salome HOME
PR: retour au tag V1_2_1_debug2 (pb dans la branche de merge V1_2c)
[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 using namespace std;
30 #include "VTKViewer_RenderWindow.h"
31 #include "utilities.h"
32 #include "QAD_Settings.h"
33 #include "QAD_Config.h"
34 #include "QAD_Desktop.h"
35 #include "QAD_Study.h"
36 #include "QAD_Tools.h"
37 #include "SALOME_Selection.h"
38
39 #include <qcolordialog.h>
40
41 #include <stdlib.h>
42 #include <math.h>
43 #include <iostream.h>
44
45 #include <vtkRenderWindowInteractor.h>
46 #include <vtkRendererCollection.h>
47 #include <vtkXOpenGLRenderWindow.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
57 VTKViewer_RenderWindow::VTKViewer_RenderWindow(QWidget *parent, const char *name) :
58   QWidget(parent, name, 
59           Qt::WStyle_NoBorder | Qt::WDestructiveClose | 
60           Qt::WResizeNoErase | Qt::WRepaintNoErase)
61 {
62   myRW = vtkRenderWindow::New();
63   MESSAGE(QString("RenderWindow is ") + QString(myRW->GetClassName()));
64   myRW->SetDisplayId((void*)x11Display());
65   myRW->SetWindowId((void*)winId());
66   myRW->DoubleBufferOn();
67 }
68
69 VTKViewer_RenderWindow::~VTKViewer_RenderWindow() {
70   //myRW->Delete(); //BUG SAL2954, Commented by SRN, to avoid problems when using rlogin
71 }
72
73 void VTKViewer_RenderWindow::paintEvent(QPaintEvent* theEvent) {
74   myRW->Render();
75 }
76
77 void VTKViewer_RenderWindow::resizeEvent(QResizeEvent* theEvent) {
78   vtkRenderWindowInteractor* aRWI = myRW->GetInteractor();
79   if (aRWI != NULL)
80     aRWI->SetSize(width(), height());
81   myRW->SetSize(width(), height());
82 }
83
84
85 void VTKViewer_RenderWindow::mouseMoveEvent( QMouseEvent *event ) {
86   emit MouseMove(event) ;
87 }
88
89 void VTKViewer_RenderWindow::mousePressEvent( QMouseEvent *event ) {
90   emit ButtonPressed(event) ;
91   switch(event->button()) {
92   case LeftButton:
93     emit LeftButtonPressed(event) ;
94     break ;
95   case MidButton:
96     emit MiddleButtonPressed(event) ;
97     break ;
98   case RightButton:
99     if ( event->state() == Qt::ControlButton ) {
100       emit RightButtonPressed(event) ;
101     } else {
102       QPopupMenu* popup = createPopup();
103       if ( popup ) {
104         QAD_Tools::checkPopup( popup );
105         if ( popup->count()>0 ) {
106           popup->exec( QCursor::pos() );
107         }
108         destroyPopup();
109       }
110     }
111     break;
112   default:
113     break ;
114   }
115 }
116
117
118 void VTKViewer_RenderWindow::mouseReleaseEvent( QMouseEvent *event ) {
119   emit ButtonReleased(event) ;
120   switch(event->button()) {
121   case LeftButton:
122     emit LeftButtonReleased(event) ;
123     break ;
124   case MidButton:
125     emit MiddleButtonReleased(event) ;
126     break ;
127   case RightButton:
128     emit RightButtonReleased(event) ;
129     break;
130   default:
131     break ;
132   }
133 }
134
135 void VTKViewer_RenderWindow::keyPressEvent (QKeyEvent * event) {
136   emit KeyPressed(event) ;
137 }
138
139 /*!
140     Creates the popup 
141 */
142 void VTKViewer_RenderWindow::onCreatePopup() 
143 {
144   if ( myPopup ) {      
145     QAD_Desktop*     Desktop = (QAD_Desktop*) QAD_Application::getDesktop();
146     QAD_Study*   myActiveStudy = Desktop->getActiveStudy();
147     SALOME_Selection*      Sel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
148     
149     QString theContext;
150     QString theParent("Viewer");
151     QString theObject;
152     
153     Desktop->definePopup( theContext, theParent, theObject );
154     Desktop->createPopup( myPopup, theContext, theParent, theObject);
155     Desktop->customPopup( myPopup, theContext, theParent, theObject );
156
157 //    if (Sel->IObjectCount() == 0 && myPopup->count()<1) {
158     if ( myPopup->count() > 0 )
159       myIDs.append ( myPopup->insertSeparator() );      
160     int id;
161     myIDs.append ( id = myPopup->insertItem (tr ("MEN_VP3D_CHANGEBGR")) );      
162     QAD_ASSERT ( myPopup->connectItem ( id, this, SLOT(onChangeBackgroundColor())) );
163 //    }
164   }
165 }
166
167
168 void VTKViewer_RenderWindow::onChangeBackgroundColor()
169 {
170   float red, green, blue;
171   float backint[3];
172
173   vtkRendererCollection * theRenderers = myRW->GetRenderers();
174   theRenderers->InitTraversal();
175   vtkRenderer * theRenderer = theRenderers->GetNextItem();
176   theRenderer->GetBackground(backint);
177
178   QColor selColor = QColorDialog::getColor ( QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255)), NULL );     
179   if ( selColor.isValid() ) {
180     theRenderer->SetBackground( selColor.red()/255., selColor.green()/255., selColor.blue()/255. ); 
181     QAD_CONFIG->addSetting( "VTKViewer:BackgroundColorRed",   selColor.red() );
182     QAD_CONFIG->addSetting( "VTKViewer:BackgroundColorGreen", selColor.green() );
183     QAD_CONFIG->addSetting( "VTKViewer:BackgroundColorBlue",  selColor.blue() );
184   }
185 }