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