9b8f02c42c66e4d45a2d891aa98271db99cb3c02
[occt.git] / samples / qt / Common / src / DocumentCommon.cxx
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
12 #include <Aspect_DisplayConnection.hxx>
13 #include <AIS_InteractiveObject.hxx>
14 #include <Graphic3d.hxx>
15 #include <Graphic3d_NameOfMaterial.hxx>
16 #include <OpenGl_GraphicDriver.hxx>
17 #include <TCollection_AsciiString.hxx>
18
19 Handle(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 {
27   static Handle(OpenGl_GraphicDriver) aGraphicDriver;
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);
34 #endif
35     aGraphicDriver = new OpenGl_GraphicDriver (aDisplayConnection);
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);
41 }
42
43 DocumentCommon::DocumentCommon( const int theIndex, ApplicationCommonWindow* app )
44 : QObject( app ),
45 myApp( app ),
46 myIndex( theIndex ),
47 myNbViews( 0 )
48 {
49   TCollection_ExtendedString a3DName("Visu3D");
50   myViewer = Viewer( getenv("DISPLAY"), a3DName.ToExtString(), "", 1000.0,
51                                V3d_XposYnegZpos, Standard_True, Standard_True );
52
53         myViewer->SetDefaultLights();
54         myViewer->SetLightOn();
55
56         myContext =new AIS_InteractiveContext(myViewer);
57 }
58
59 DocumentCommon::~DocumentCommon()
60 {
61 }
62
63 ApplicationCommonWindow* DocumentCommon::getApplication()
64 {
65         return myApp;
66 }
67
68 MDIWindow* DocumentCommon::createNewMDIWindow( bool theRT )
69 {
70   QWorkspace* ws = myApp->getWorkspace();
71   return new MDIWindow( this, ws, 0, theRT );
72 }
73
74 void DocumentCommon::onCreateNewView( bool theRT ) 
75 {
76         QWorkspace* ws = myApp->getWorkspace();
77   MDIWindow* w = createNewMDIWindow( theRT );
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 ) ),
88                  myApp->statusBar(), SLOT( showMessage( const QString&, int ) ) );
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
114 void DocumentCommon::onCloseView(MDIWindow* theView)
115 {
116     removeView(theView);
117     if( countOfWindow() == 0 )
118         emit sendCloseDocument( this );
119 }
120
121 void DocumentCommon::removeView(MDIWindow* theView)
122 {
123     if ( myViews.count( theView ) )
124     {
125                 myViews.removeAll(theView);
126                 delete theView;
127         }
128 }
129 void DocumentCommon::removeViews()
130 {
131   while( myViews.count() )
132     removeView( (MDIWindow*)myViews.first() );
133 }
134
135 int DocumentCommon::countOfWindow()
136 {
137         return myViews.count();
138 }
139
140 Handle(AIS_InteractiveContext) DocumentCommon::getContext()
141 {
142         return myContext;
143 }
144
145 void DocumentCommon::fitAll()
146 {
147         QList<MDIWindow*>::iterator i;
148         for ( i = myViews.begin(); i != myViews.end(); i++ )
149                 (*i)->fitAll();
150 }
151
152 void 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
162 void 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
172 void 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
196 void DocumentCommon::onMaterial( int theMaterial )
197 {
198     for ( myContext->InitCurrent(); myContext->MoreCurrent (); myContext->NextCurrent () )
199         myContext->SetMaterial( myContext->Current(), (Graphic3d_NameOfMaterial)theMaterial );
200 }
201
202 void DocumentCommon::onMaterial()
203 {
204     DialogMaterial* m = new DialogMaterial();
205     connect( m, SIGNAL( sendMaterialChanged( int ) ), this, SLOT( onMaterial( int ) ) );
206     m->exec();
207 }
208
209 void 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
215 void DocumentCommon::onTransparency()
216 {
217     DialogTransparency* aDialog = new DialogTransparency();
218     connect( aDialog, SIGNAL( sendTransparencyChanged( int ) ), this, SLOT( onTransparency( int ) ) );
219     aDialog->exec();
220 }
221
222 void DocumentCommon::onDelete()
223 {
224     for ( myContext->InitCurrent(); myContext->MoreCurrent(); myContext->NextCurrent() )
225         myContext->Erase( myContext->Current(), false);
226     myContext->ClearSelected();
227     getApplication()->onSelectionChanged();
228 }
229
230 #ifdef HAVE_OPENCL
231
232 void 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
246 void 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;
254
255   window->setRaytracedReflections( state );
256
257   myContext->UpdateCurrentViewer();
258 }
259
260 void 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