]> SALOME platform Git repositories - modules/kernel.git/blob - src/VTKViewer/VTKViewer_RenderWindow.cxx
Salome HOME
PR : merge branch V1_2c dans branche principale pour V1_3_0_b1
[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   // Uncommented because of bug SAL3913
72 }
73
74 void VTKViewer_RenderWindow::paintEvent(QPaintEvent* theEvent) {
75   myRW->Render();
76 }
77
78 void VTKViewer_RenderWindow::resizeEvent(QResizeEvent* theEvent) {
79   vtkRenderWindowInteractor* aRWI = myRW->GetInteractor();
80   if (aRWI != NULL)
81     aRWI->SetSize(width(), height());
82   myRW->SetSize(width(), height());
83 }
84
85
86 void VTKViewer_RenderWindow::mouseMoveEvent( QMouseEvent *event ) {
87   emit MouseMove(event) ;
88 }
89
90 void VTKViewer_RenderWindow::mousePressEvent( QMouseEvent *event ) {
91   emit ButtonPressed(event) ;
92   switch(event->button()) {
93   case LeftButton:
94     emit LeftButtonPressed(event) ;
95     break ;
96   case MidButton:
97     emit MiddleButtonPressed(event) ;
98     break ;
99   case RightButton:
100     if ( event->state() == Qt::ControlButton ) {
101       emit RightButtonPressed(event) ;
102     } else {
103       QPopupMenu* popup = createPopup();
104       if ( popup ) {
105         QAD_Tools::checkPopup( popup );
106         if ( popup->count()>0 ) {
107           popup->exec( QCursor::pos() );
108         }
109         destroyPopup();
110       }
111     }
112     break;
113   default:
114     break ;
115   }
116 }
117
118
119 void VTKViewer_RenderWindow::mouseReleaseEvent( QMouseEvent *event ) {
120   emit ButtonReleased(event) ;
121   switch(event->button()) {
122   case LeftButton:
123     emit LeftButtonReleased(event) ;
124     break ;
125   case MidButton:
126     emit MiddleButtonReleased(event) ;
127     break ;
128   case RightButton:
129     emit RightButtonReleased(event) ;
130     break;
131   default:
132     break ;
133   }
134 }
135
136 void VTKViewer_RenderWindow::keyPressEvent (QKeyEvent * event) {
137   emit KeyPressed(event) ;
138 }
139
140 /*!
141     Creates the popup 
142 */
143 void VTKViewer_RenderWindow::onCreatePopup() 
144 {
145   if ( myPopup ) {      
146     QAD_Desktop*     Desktop = (QAD_Desktop*) QAD_Application::getDesktop();
147     QAD_Study*   myActiveStudy = Desktop->getActiveStudy();
148     SALOME_Selection*      Sel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
149     
150     QString theContext;
151     QString theParent("Viewer");
152     QString theObject;
153     
154     Desktop->definePopup( theContext, theParent, theObject );
155     Desktop->createPopup( myPopup, theContext, theParent, theObject);
156     Desktop->customPopup( myPopup, theContext, theParent, theObject );
157
158 //    if (Sel->IObjectCount() == 0 && myPopup->count()<1) {
159     if ( myPopup->count() > 0 )
160       myIDs.append ( myPopup->insertSeparator() );      
161     int id;
162     myIDs.append ( id = myPopup->insertItem (tr ("MEN_VP3D_CHANGEBGR")) );      
163     QAD_ASSERT ( myPopup->connectItem ( id, this, SLOT(onChangeBackgroundColor())) );
164 //    }
165   }
166 }
167
168
169 void VTKViewer_RenderWindow::onChangeBackgroundColor()
170 {
171   float red, green, blue;
172   float backint[3];
173
174   vtkRendererCollection * theRenderers = myRW->GetRenderers();
175   theRenderers->InitTraversal();
176   vtkRenderer * theRenderer = theRenderers->GetNextItem();
177   theRenderer->GetBackground(backint);
178
179   QColor selColor = QColorDialog::getColor ( QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255)), NULL );     
180   if ( selColor.isValid() ) {
181     theRenderer->SetBackground( selColor.red()/255., selColor.green()/255., selColor.blue()/255. ); 
182     QAD_CONFIG->addSetting( "VTKViewer:BackgroundColorRed",   selColor.red() );
183     QAD_CONFIG->addSetting( "VTKViewer:BackgroundColorGreen", selColor.green() );
184     QAD_CONFIG->addSetting( "VTKViewer:BackgroundColorBlue",  selColor.blue() );
185   }
186 }