Update of testing case due to changes in issue 24370
[occt.git] / samples / qt / Common / src / DocumentCommon.cxx
CommitLineData
7fd59977 1#include "DocumentCommon.h"
2
3#include "ApplicationCommon.h"
4#include "Transparency.h"
5#include "Material.h"
6
7#include <QStatusBar>
8#include <QApplication>
9#include <QColor>
10#include <QColorDialog>
11
dc3fe572 12#include <Aspect_DisplayConnection.hxx>
7fd59977 13#include <AIS_InteractiveObject.hxx>
dc3fe572 14#include <Graphic3d.hxx>
7fd59977 15#include <Graphic3d_NameOfMaterial.hxx>
65993a95 16#include <OpenGl_GraphicDriver.hxx>
dc3fe572 17#include <TCollection_AsciiString.hxx>
7fd59977 18
19Handle(V3d_Viewer) DocumentCommon::Viewer( const Standard_CString aDisplay,
20 const Standard_ExtString aName,
21 const Standard_CString aDomain,
22 const Standard_Real ViewSize,
23 const V3d_TypeOfOrientation ViewProj,
24 const Standard_Boolean ComputedMode,
25 const Standard_Boolean aDefaultComputedMode )
26{
65993a95 27 static Handle(OpenGl_GraphicDriver) aGraphicDriver;
dc3fe572 28
29 if (aGraphicDriver.IsNull())
30 {
31 Handle(Aspect_DisplayConnection) aDisplayConnection;
32#if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
33 aDisplayConnection = new Aspect_DisplayConnection (aDisplay);
7fd59977 34#endif
65993a95 35 aGraphicDriver = new OpenGl_GraphicDriver (aDisplayConnection);
dc3fe572 36 }
37
38 return new V3d_Viewer(aGraphicDriver,aName,aDomain,ViewSize,ViewProj,
39 Quantity_NOC_GRAY30,V3d_ZBUFFER,V3d_GOURAUD,V3d_WAIT,
40 ComputedMode,aDefaultComputedMode,V3d_TEX_NONE);
7fd59977 41}
42
43DocumentCommon::DocumentCommon( const int theIndex, ApplicationCommonWindow* app )
44: QObject( app ),
45myApp( app ),
46myIndex( theIndex ),
47myNbViews( 0 )
48{
49 TCollection_ExtendedString a3DName("Visu3D");
50 myViewer = Viewer( getenv("DISPLAY"), a3DName.ToExtString(), "", 1000.0,
51 V3d_XposYnegZpos, Standard_True, Standard_True );
52
7fd59977 53 myViewer->SetDefaultLights();
54 myViewer->SetLightOn();
55
e276548b 56 myContext =new AIS_InteractiveContext(myViewer);
7fd59977 57}
58
59DocumentCommon::~DocumentCommon()
60{
61}
62
63ApplicationCommonWindow* DocumentCommon::getApplication()
64{
65 return myApp;
66}
67
e276548b 68MDIWindow* DocumentCommon::createNewMDIWindow( bool theRT )
7fd59977 69{
70 QWorkspace* ws = myApp->getWorkspace();
e276548b 71 return new MDIWindow( this, ws, 0, theRT );
7fd59977 72}
e276548b 73
74void DocumentCommon::onCreateNewView( bool theRT )
7fd59977 75{
76 QWorkspace* ws = myApp->getWorkspace();
e276548b 77 MDIWindow* w = createNewMDIWindow( theRT );
7fd59977 78
79 if( !w )
80 return;
81
82 ws->addWindow( w );
83 myViews.append(w);
84
85 connect( w, SIGNAL( selectionChanged() ),
86 this, SIGNAL( selectionChanged() ) );
87 connect( w, SIGNAL( message( const QString&, int ) ),
3cb77da4 88 myApp->statusBar(), SLOT( showMessage( const QString&, int ) ) );
7fd59977 89 connect( w, SIGNAL( sendCloseView( MDIWindow* ) ),
90 this, SLOT( onCloseView( MDIWindow* ) ) );
91
92 QString aName;
93 w->setWindowTitle( aName.sprintf( "Document %d:%d", myIndex, ++myNbViews ) );
94 QString dir = ApplicationCommonWindow::getResourceDir() + QString( "/" );
95
96 w->setWindowIcon( QPixmap( dir + QObject::tr("ICON_DOC") ) );
97
98 if ( ws->windowList().isEmpty() )
99 {
100 // Due to strange Qt4.2.3 feature the child window icon is not drawn
101 // in the main menu if showMaximized() is called for a non-visible child window
102 // Therefore calling show() first...
103 w->show();
104 w->showMaximized();
105 }
106 else
107 w->show();
108
109 w->setFocus();
110
111 getApplication()->onSelectionChanged();
112}
113
114void DocumentCommon::onCloseView(MDIWindow* theView)
115{
116 removeView(theView);
117 if( countOfWindow() == 0 )
118 emit sendCloseDocument( this );
119}
120
121void DocumentCommon::removeView(MDIWindow* theView)
122{
123 if ( myViews.count( theView ) )
124 {
125 myViews.removeAll(theView);
126 delete theView;
127 }
128}
129void DocumentCommon::removeViews()
130{
131 while( myViews.count() )
132 removeView( (MDIWindow*)myViews.first() );
133}
134
135int DocumentCommon::countOfWindow()
136{
137 return myViews.count();
138}
139
140Handle(AIS_InteractiveContext) DocumentCommon::getContext()
141{
142 return myContext;
143}
144
145void DocumentCommon::fitAll()
146{
147 QList<MDIWindow*>::iterator i;
148 for ( i = myViews.begin(); i != myViews.end(); i++ )
149 (*i)->fitAll();
150}
151
152void DocumentCommon::onWireframe()
153{
154 QApplication::setOverrideCursor( Qt::WaitCursor );
155 for( myContext->InitCurrent(); myContext->MoreCurrent(); myContext->NextCurrent() )
156 myContext->SetDisplayMode( myContext->Current(), 0, false );
157 myContext->UpdateCurrentViewer();
158 getApplication()->onSelectionChanged();
159 QApplication::restoreOverrideCursor();
160}
161
162void DocumentCommon::onShading()
163{
164 QApplication::setOverrideCursor( Qt::WaitCursor );
165 for( myContext->InitCurrent(); myContext->MoreCurrent(); myContext->NextCurrent() )
166 myContext->SetDisplayMode( myContext->Current(), 1, false );
167 myContext->UpdateCurrentViewer();
168 getApplication()->onSelectionChanged();
169 QApplication::restoreOverrideCursor();
170}
171
172void DocumentCommon::onColor()
173{
174 QColor aColor ;
175 Quantity_Color aShapeColor;
176 myContext->InitCurrent();
177 Handle_AIS_InteractiveObject Current = myContext->Current() ;
178 if ( Current->HasColor () )
179 {
180 aShapeColor = myContext->Color( Current );
181 aColor.setRgb( aShapeColor.Red() * 255, aShapeColor.Green() * 255, aShapeColor.Blue() * 255 );
182 }
183 else
184 aColor.setRgb( 255, 255, 255 );
185
186 QColor aRetColor = QColorDialog::getColor( aColor );
187 if ( aRetColor.isValid() )
188 {
189 Quantity_Color color( aRetColor.red() / 255., aRetColor.green() / 255.,
190 aRetColor.blue() / 255., Quantity_TOC_RGB );
191 for (; myContext->MoreCurrent(); myContext->NextCurrent() )
192 myContext->SetColor( myContext->Current(), color.Name() );
193 }
194}
195
196void DocumentCommon::onMaterial( int theMaterial )
197{
198 for ( myContext->InitCurrent(); myContext->MoreCurrent (); myContext->NextCurrent () )
199 myContext->SetMaterial( myContext->Current(), (Graphic3d_NameOfMaterial)theMaterial );
200}
201
202void DocumentCommon::onMaterial()
203{
204 DialogMaterial* m = new DialogMaterial();
205 connect( m, SIGNAL( sendMaterialChanged( int ) ), this, SLOT( onMaterial( int ) ) );
206 m->exec();
207}
208
209void DocumentCommon::onTransparency( int theTrans )
210{
211 for( myContext->InitCurrent(); myContext->MoreCurrent(); myContext->NextSelected() )
212 myContext->SetTransparency( myContext->Current(), ((Standard_Real)theTrans) / 10.0 );
213}
214
215void DocumentCommon::onTransparency()
216{
217 DialogTransparency* aDialog = new DialogTransparency();
218 connect( aDialog, SIGNAL( sendTransparencyChanged( int ) ), this, SLOT( onTransparency( int ) ) );
219 aDialog->exec();
220}
221
222void DocumentCommon::onDelete()
223{
224 for ( myContext->InitCurrent(); myContext->MoreCurrent(); myContext->NextCurrent() )
eb4320f2 225 myContext->Erase( myContext->Current(), false);
7fd59977 226 myContext->ClearSelected();
227 getApplication()->onSelectionChanged();
228}
229
e276548b 230#ifdef HAVE_OPENCL
231
232void DocumentCommon::onShadows( int state )
233{
234 QWorkspace* ws = ApplicationCommonWindow::getWorkspace();
235
236 MDIWindow* window = qobject_cast< MDIWindow* >( ws->activeWindow() );
237
238 if( window == NULL )
239 return;
240
241 window->setRaytracedShadows( state );
242
243 myContext->UpdateCurrentViewer();
244}
245
246void DocumentCommon::onReflections( int state )
247{
248 QWorkspace* ws = ApplicationCommonWindow::getWorkspace();
249
250 MDIWindow* window = qobject_cast< MDIWindow* >( ws->activeWindow() );
251
252 if( window == NULL )
253 return;
7fd59977 254
e276548b 255 window->setRaytracedReflections( state );
256
257 myContext->UpdateCurrentViewer();
258}
259
260void DocumentCommon::onAntialiasing( int state )
261{
262 QWorkspace* ws = ApplicationCommonWindow::getWorkspace();
263
264 MDIWindow* window = qobject_cast< MDIWindow* >( ws->activeWindow() );
265
266 if( window == NULL )
267 return;
268
269 window->setRaytracedAntialiasing( state );
270
271 myContext->UpdateCurrentViewer();
272}
273
274#endif