343c21db99a99f4fb0c2cc842dd54dd48f9da8b5
[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_NameOfMaterial.hxx>
15 #include <OpenGl_GraphicDriver.hxx>
16 #include <TCollection_AsciiString.hxx>
17
18 // =======================================================================
19 // function : Viewer
20 // purpose  :
21 // =======================================================================
22 Handle(V3d_Viewer) DocumentCommon::Viewer (const Standard_ExtString theName,
23                                            const Standard_CString theDomain,
24                                            const Standard_Real theViewSize,
25                                            const V3d_TypeOfOrientation theViewProj,
26                                            const Standard_Boolean theComputedMode,
27                                            const Standard_Boolean theDefaultComputedMode )
28 {
29   static Handle(OpenGl_GraphicDriver) aGraphicDriver;
30
31   if (aGraphicDriver.IsNull())
32   {
33     Handle(Aspect_DisplayConnection) aDisplayConnection;
34 #if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
35     aDisplayConnection = new Aspect_DisplayConnection (qgetenv ("DISPLAY").constData());
36 #endif
37     aGraphicDriver = new OpenGl_GraphicDriver (aDisplayConnection);
38   }
39
40   return new V3d_Viewer (aGraphicDriver,
41                          theName,
42                          theDomain,
43                          theViewSize,
44                          theViewProj,
45                          Quantity_NOC_GRAY30,
46                          V3d_ZBUFFER,
47                          V3d_GOURAUD,
48                          V3d_WAIT,
49                          theComputedMode,
50                          theDefaultComputedMode,
51                          V3d_TEX_NONE);
52 }
53
54 DocumentCommon::DocumentCommon( const int theIndex, ApplicationCommonWindow* app )
55 : QObject( app ),
56 myApp( app ),
57 myIndex( theIndex ),
58 myNbViews( 0 )
59 {
60   TCollection_ExtendedString a3DName ("Visu3D");
61
62   myViewer = Viewer (a3DName.ToExtString(), "", 1000.0, V3d_XposYnegZpos, Standard_True, Standard_True);
63
64   myViewer->SetDefaultLights();
65   myViewer->SetLightOn();
66
67   myContext = new AIS_InteractiveContext (myViewer);
68 }
69
70 DocumentCommon::~DocumentCommon()
71 {
72 }
73
74 ApplicationCommonWindow* DocumentCommon::getApplication()
75 {
76   return myApp;
77 }
78
79 MDIWindow* DocumentCommon::createNewMDIWindow()
80 {
81   QMdiArea* ws = myApp->getWorkspace();
82   return new MDIWindow (this, ws, 0);
83 }
84
85 void DocumentCommon::onCreateNewView()
86 {
87   QMdiArea* ws = myApp->getWorkspace();
88   MDIWindow* w = createNewMDIWindow();
89   
90   if (!w)
91     return;
92
93   ws->addSubWindow (w);
94   myViews.append (w);
95
96   connect( w,    SIGNAL( selectionChanged() ),
97            this, SIGNAL( selectionChanged() ) );
98   connect( w, SIGNAL( message( const QString&, int ) ),
99            myApp->statusBar(), SLOT( showMessage( const QString&, int ) ) );
100   connect( w, SIGNAL( sendCloseView( MDIWindow* ) ),
101            this, SLOT( onCloseView( MDIWindow* ) ) );
102
103   QString aName;
104   w->setWindowTitle( aName.sprintf( "Document %d:%d", myIndex, ++myNbViews ) );
105   QString dir = ApplicationCommonWindow::getResourceDir() + QString( "/" );
106   
107   w->setWindowIcon( QPixmap( dir + QObject::tr("ICON_DOC") ) );
108
109   if ( ws->subWindowList().isEmpty() )
110   {
111     // Due to strange Qt4.2.3 feature the child window icon is not drawn
112     // in the main menu if showMaximized() is called for a non-visible child window
113     // Therefore calling show() first...
114     w->show();
115     w->showMaximized();
116   }
117   else
118     w->show();
119
120   w->setFocus();
121
122   getApplication()->onSelectionChanged();
123 }
124
125 void DocumentCommon::onCloseView(MDIWindow* theView)
126 {
127     removeView(theView);
128     if( countOfWindow() == 0 )
129         emit sendCloseDocument( this );
130 }
131
132 void DocumentCommon::removeView(MDIWindow* theView)
133 {
134     if ( myViews.count( theView ) )
135     {
136     myViews.removeAll(theView);
137     delete theView;
138   }
139 }
140 void DocumentCommon::removeViews()
141 {
142   while( myViews.count() )
143   {
144     removeView( myViews.first() );
145   }
146 }
147
148 int DocumentCommon::countOfWindow()
149 {
150   return myViews.count();
151 }
152
153 Handle(AIS_InteractiveContext) DocumentCommon::getContext()
154 {
155   return myContext;
156 }
157
158 void DocumentCommon::fitAll()
159 {
160   QList<MDIWindow*>::iterator i;
161   for ( i = myViews.begin(); i != myViews.end(); i++ )
162     (*i)->fitAll();
163 }
164
165 void DocumentCommon::onWireframe()
166 {
167     QApplication::setOverrideCursor( Qt::WaitCursor );
168     for( myContext->InitCurrent(); myContext->MoreCurrent(); myContext->NextCurrent() )
169         myContext->SetDisplayMode( myContext->Current(), 0, false );
170     myContext->UpdateCurrentViewer();
171     getApplication()->onSelectionChanged();
172     QApplication::restoreOverrideCursor();
173 }
174
175 void DocumentCommon::onShading()
176 {
177     QApplication::setOverrideCursor( Qt::WaitCursor );
178     for( myContext->InitCurrent(); myContext->MoreCurrent(); myContext->NextCurrent() )
179         myContext->SetDisplayMode( myContext->Current(), 1, false );
180     myContext->UpdateCurrentViewer();
181     getApplication()->onSelectionChanged();
182     QApplication::restoreOverrideCursor();
183 }
184
185 void DocumentCommon::onColor()
186 {
187     QColor aColor ;
188     Quantity_Color aShapeColor;
189     myContext->InitCurrent();
190     Handle_AIS_InteractiveObject Current = myContext->Current() ;
191     if ( Current->HasColor () )
192     {
193         aShapeColor = myContext->Color( Current );
194         aColor.setRgb( aShapeColor.Red() * 255, aShapeColor.Green() * 255, aShapeColor.Blue() * 255 );
195     }
196     else
197         aColor.setRgb( 255, 255, 255 );
198
199     QColor aRetColor = QColorDialog::getColor( aColor );
200     if ( aRetColor.isValid() )
201     {
202         Quantity_Color color( aRetColor.red() / 255., aRetColor.green() / 255.,
203                         aRetColor.blue() / 255., Quantity_TOC_RGB );
204         for (; myContext->MoreCurrent(); myContext->NextCurrent() )
205             myContext->SetColor( myContext->Current(), color.Name() );
206     }
207 }
208
209 void DocumentCommon::onMaterial( int theMaterial )
210 {
211     for ( myContext->InitCurrent(); myContext->MoreCurrent (); myContext->NextCurrent () )
212         myContext->SetMaterial( myContext->Current(), (Graphic3d_NameOfMaterial)theMaterial );
213 }
214
215 void DocumentCommon::onMaterial()
216 {
217     DialogMaterial* m = new DialogMaterial();
218     connect( m, SIGNAL( sendMaterialChanged( int ) ), this, SLOT( onMaterial( int ) ) );
219     m->exec();
220 }
221
222 void DocumentCommon::onTransparency( int theTrans )
223 {
224     for( myContext->InitCurrent(); myContext->MoreCurrent(); myContext->NextSelected() )
225         myContext->SetTransparency( myContext->Current(), ((Standard_Real)theTrans) / 10.0 );
226 }
227
228 void DocumentCommon::onTransparency()
229 {
230     DialogTransparency* aDialog = new DialogTransparency();
231     connect( aDialog, SIGNAL( sendTransparencyChanged( int ) ), this, SLOT( onTransparency( int ) ) );
232     aDialog->exec();
233 }
234
235 void DocumentCommon::onDelete()
236 {
237     for ( myContext->InitCurrent(); myContext->MoreCurrent(); myContext->NextCurrent() )
238         myContext->Erase( myContext->Current(), false);
239     myContext->ClearSelected();
240     getApplication()->onSelectionChanged();
241 }