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