Adding svn:eol-style=LF property
[occt.git] / samples / qt / Graphic3dDemo / src / ViewOperations.cxx
CommitLineData
7fd59977 1#include "ViewOperations.h"
2#include "global.h"
3//#include "Material.h"
4//#include "Transparency.h"
5#include "Application.h"
6#include "Document.h"
7
8#include <qcolordialog.h>
9#include <qapplication.h>
10#include <qsignalmapper.h>
11#include <qworkspace.h>
12#include <qpixmap.h>
13#include <qcursor.h>
14#include <qaction.h>
15
16#include <V3d_TypeOfView.hxx>
17#include <AIS_InteractiveObject.hxx>
18#include <AIS_ListOfInteractive.hxx>
19#include <AIS_ListIteratorOfListOfInteractive.hxx>
20
21#include <Quantity_Color.hxx>
22
23
24 static QCursor* defCursor = NULL;
25 static QCursor* handCursor = NULL;
26 static QCursor* panCursor = NULL;
27 static QCursor* zoomCursor = NULL;
28 static QCursor* rotCursor = NULL;
29
30ViewOperations::ViewOperations( QObject* parent)
31: QObject( parent )
32{
33 myContext = NULL;
34}
35
36ViewOperations::ViewOperations( Handle(AIS_InteractiveContext) theContext,
37 QObject* parent)
38: QObject( parent )
39{
40 myContext = theContext;
41 initActions();
42 initViewActions();
43}
44
45ViewOperations::~ViewOperations()
46{
47 myContext = NULL;
48}
49
50void ViewOperations::onWireframe()
51{
52 AIS_ListOfInteractive displayed;
53 myContext->DisplayedObjects( displayed );
54 AIS_ListIteratorOfListOfInteractive it( displayed );
55 for ( ; it.More(); it.Next() )
56 myContext->SetDisplayMode( it.Value(), 0 );
57#ifdef a
58 for(myContext->InitCurrent();myContext->MoreCurrent();myContext->NextCurrent())
59 myContext->SetDisplayMode(myContext->Current(),0);
60#endif
61}
62
63void ViewOperations::onShading()
64{
65 QApplication::setOverrideCursor( Qt::WaitCursor );
66
67 AIS_ListOfInteractive displayed;
68 myContext->DisplayedObjects( displayed );
69 AIS_ListIteratorOfListOfInteractive it( displayed );
70 for ( ; it.More(); it.Next() )
71 myContext->SetDisplayMode( it.Value(), 1 );
72
73#ifdef a
74 for(myContext->InitCurrent();myContext->MoreCurrent();myContext->NextCurrent())
75 myContext->SetDisplayMode(myContext->Current(),1);
76#endif
77 QApplication::restoreOverrideCursor();
78}
79
80void ViewOperations::initActions()
81{
82 initCursors();
83 initToolActions();
84}
85
86void ViewOperations::initCursors()
87{
88 if( !defCursor )
89 defCursor = new QCursor( Qt::ArrowCursor );
90 if( !handCursor )
91 handCursor = new QCursor( Qt::PointingHandCursor );
92 if( !panCursor )
93 panCursor = new QCursor( Qt::SizeAllCursor );
94
95 QString dir( Application::getResourceDir() );
96 if( !zoomCursor )
97 zoomCursor = new QCursor( QPixmap( dir + tr( "ICON_CURSOR_ZOOM" ) ) );
98 if( !rotCursor )
99 rotCursor = new QCursor( QPixmap( dir + tr( "ICON_CURSOR_ROTATE" ) ) );
100}
101
102QList<QAction*> ViewOperations::getViewActions()
103{
104 return myViewActions;
105}
106
107void ViewOperations::initViewActions()
108{
c6c9371f 109 if ( myViewActions.count() )
7fd59977 110 return;
111
112 QString dir = Application::getResourceDir();
113
114 QAction* a;
115
c6c9371f 116 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_FITALL" ) ), "", this );
7fd59977 117 a->setToolTip( tr( "TBR_FITALL" ) );
118 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
119 myViewActions.insert( ViewFitAllId, a );
120
c6c9371f 121 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_FITAREA" ) ), "", this );
7fd59977 122 a->setToolTip( tr( "TBR_FITAREA" ) );
123 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
124 a->setCheckable( true );
125 verify( connect( a, SIGNAL( toggled( bool ) ), SLOT( updateToggled( bool ) ) ) );
126 myViewActions.insert( ViewFitAreaId, a );
127
c6c9371f 128 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_ZOOM" ) ), "", this );
7fd59977 129 a->setToolTip( tr( "TBR_ZOOM" ) );
130 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
131 a->setCheckable( true );
132 verify( connect( a, SIGNAL( toggled( bool ) ), SLOT( updateToggled( bool ) ) ) );
133 myViewActions.insert( ViewZoomId, a );
134
c6c9371f 135 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_PAN" ) ), "", this );
7fd59977 136 a->setToolTip( tr( "TBR_PAN" ) );
137 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
138 a->setCheckable( true );
139 verify( connect( a, SIGNAL( toggled( bool ) ), SLOT( updateToggled( bool ) ) ) );
140 myViewActions.insert( ViewPanId, a );
141
c6c9371f 142 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_GLOBALPAN" ) ), "", this );
7fd59977 143 a->setToolTip( tr( "TBR_GLOBALPAN" ) );
144 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
145 a->setCheckable( true );
146 verify( connect( a, SIGNAL( toggled( bool ) ), SLOT( updateToggled( bool ) ) ) );
147 myViewActions.insert( ViewGlobalPanId, a );
148
c6c9371f 149 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_FRONT" ) ), "", this );
7fd59977 150 a->setToolTip( tr( "TBR_FRONT" ) );
151 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
152 myViewActions.insert( ViewFrontId, a );
153
c6c9371f 154 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_BACK" ) ), "", this );
7fd59977 155 a->setToolTip( tr( "TBR_BACK" ) );
156 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
157 myViewActions.insert( ViewBackId, a );
158
c6c9371f 159 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_TOP" ) ), "", this );
7fd59977 160 a->setToolTip( tr( "TBR_TOP" ) );
161 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
162 myViewActions.insert( ViewTopId, a );
163
c6c9371f 164 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_BOTTOM" ) ), "", this );
7fd59977 165 a->setToolTip( tr( "TBR_BOTTOM" ) );
166 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
167 myViewActions.insert( ViewBottomId, a );
168
c6c9371f 169 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_LEFT" ) ), "", this );
7fd59977 170 a->setToolTip( tr( "TBR_LEFT" ) );
171 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
172 myViewActions.insert( ViewLeftId, a );
173
c6c9371f 174 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_RIGHT" ) ), "", this );
7fd59977 175 a->setToolTip( tr( "TBR_RIGHT" ) );
176 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
177 myViewActions.insert( ViewRightId, a );
178
c6c9371f 179 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_AXO" ) ), "", this );
7fd59977 180 a->setToolTip( tr( "TBR_AXO" ) );
181 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
182 myViewActions.insert( ViewAxoId, a );
183
c6c9371f 184 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_ROTATION" ) ), "", this );
7fd59977 185 a->setToolTip( tr( "TBR_ROTATION" ) );
186 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
187 a->setCheckable( true );
188 verify( connect( a, SIGNAL( toggled( bool ) ), SLOT( updateToggled( bool ) ) ) );
189 myViewActions.insert( ViewRotationId, a );
190
c6c9371f 191 a = new QAction( QPixmap( dir + tr( "ICON_VIEW_RESET" ) ), "", this );
192 a->setToolTip( tr( "TBR_RESET" ) );
7fd59977 193 verify( connect( a, SIGNAL( activated() ), SLOT( onActionActivated() ) ) );
194 myViewActions.insert( ViewResetId, a );
195}
196
197void ViewOperations::updateToggled( bool isOn )
198{
199 if( !isOn )
200 return;
201
202 QAction* sentBy = (QAction*) sender();
203
204 for ( int i = ViewFitAllId;i <= ViewResetId ; i++)
205 {
206 QAction* anAction = myViewActions.at(i);
207 if( (anAction == myViewActions.at(ViewFitAreaId)) ||
208 (anAction == myViewActions.at(ViewZoomId)) ||
209 (anAction == myViewActions.at(ViewPanId)) ||
210 (anAction == myViewActions.at(ViewGlobalPanId)) ||
211 (anAction == myViewActions.at(ViewRotationId))) {
212 if (anAction && (anAction != sentBy)) {
213 anAction->setCheckable(true);
214 anAction->setChecked(false);
215 } else {
216 if(sentBy == myViewActions.at(ViewFitAreaId))
217 emit setCursor(*handCursor);
218 else if (sentBy == myViewActions.at(ViewZoomId))
219 emit setCursor(*zoomCursor);
220 else if (sentBy == myViewActions.at(ViewPanId))
221 emit setCursor(*panCursor);
222 else if (sentBy == myViewActions.at(ViewGlobalPanId))
223 emit setCursor(*defCursor);
224 else if (sentBy == myViewActions.at(ViewRotationId))
225 emit setCursor(*rotCursor);
226 sentBy->setCheckable(false);
227 }
228 }
229 }
230}
231
232QList<QAction*> ViewOperations::getToolActions()
233{
234 initToolActions();
235 return myToolActions;
236}
237
238void ViewOperations::initToolActions()
239{
240 if( myToolActions.count() )
241 return;
242
243 QString dir = Application::getResourceDir();
244
245 //QAction* a = new QAction( tr( "TBR_TOOL_MAGVIEW" ), QPixmap( dir + tr( "ICON_TOOL_WIRE" ) ),
246 // "", 0, this );
247 //verify( connect( a, SIGNAL( activated() ), SLOT( onToolAction() ) ) );
248 //myToolActions->insert( MagViewId, a );
249
250// QAction* a = new QAction( tr( "TBR_TOOL_AXOVIEW" ), QPixmap( dir + tr( "ICON_TOOL_WIRE" ) ),
251// "", 0, this );
252// verify( connect( a, SIGNAL( activated() ), SLOT( onToolAction() ) ) );
253// myToolActions->insert( AxoViewId, a );
254
255// a = new QAction( tr( "TBR_TOOL_PERVIEW" ), QPixmap( dir + tr( "ICON_TOOL_WIRE" ) ),
256// "", 0, this );
257// verify( connect( a, SIGNAL( activated() ), SLOT( onToolAction() ) ) );
258// myToolActions->insert( PerViewId, a );
259
c6c9371f 260 QAction* a = new QAction( QPixmap( dir + tr( "ICON_TOOL_WIRE" ) ), "", this );
7fd59977 261 a->setToolTip( tr( "TBR_TOOL_WIRE" ) );
262 a->setCheckable( true );
263 verify( connect( a, SIGNAL( activated() ), SLOT( onToolAction() ) ) );
264 myToolActions.insert( ToolWireframeId, a );
265
266 a = new QAction( QPixmap( dir + tr( "ICON_TOOL_SHAD") ), "", this );
c6c9371f 267 a->setToolTip( tr( "TBR_TOOL_SHAD" ) );
7fd59977 268 a->setCheckable( true );
269 a->setChecked( true );
270 verify( connect( a, SIGNAL( activated() ), SLOT( onToolAction() ) ) );
271 myToolActions.insert( ToolShadingId, a );
272}
273
274void ViewOperations::onToolAction()
275{
276 QAction* sentBy = (QAction*) sender();
277 //MDIWindow* win = (MDIWindow*) Application::getWorkspace()->activeWindow();
278 MDIWindow* win = (MDIWindow*)Application::getApplication()->getActiveMDI();
279 Document* doc = win->getDocument();
280
281// if( sentBy == myToolActions->at( MagViewId ) )
282// win->defineMagView();
283
284// if( sentBy == myToolActions->at( PerViewId ) )
285// doc->createNewView( V3d_PERSPECTIVE );
286
287// if( sentBy == myToolActions->at( AxoViewId ) )
288// doc->createNewView();
289
290 if( sentBy == myToolActions.at( ToolWireframeId ) )
291 doc->onWireframe();
292
293 if( sentBy == myToolActions.at( ToolShadingId ) )
294 doc->onShading();
295}
296
297int ViewOperations::getDisplayMode()
298{
299 bool on = myToolActions.at( ToolWireframeId )->isChecked();
300 return on ? 0 : 1;
301}
302
303void ViewOperations::onNoActiveActions()
304{
305 for ( int i = ViewFitAllId;i <= ViewResetId ; i++ ) {
306 QAction* anAction = myViewActions.at( i );
307 if( ( anAction == myViewActions.at( ViewFitAreaId ) ) ||
308 ( anAction == myViewActions.at( ViewZoomId ) ) ||
309 ( anAction == myViewActions.at( ViewPanId ) ) ||
310 ( anAction == myViewActions.at( ViewGlobalPanId ) ) ||
311 ( anAction == myViewActions.at( ViewRotationId ) ) ) {
312
313 emit setCursor( *defCursor );
314 anAction->setCheckable( true );
315 anAction->setChecked( false );
316 }
317 }
318}
319
320void ViewOperations::onActionActivated()
321{
322 QAction* a = (QAction*) sender();
323 //MDIWindow* win = (MDIWindow*) Application::getWorkspace()->activeWindow();
324 MDIWindow* win = (MDIWindow*) Application::getApplication()->getActiveMDI();
325 int index = myViewActions.indexOf( a );
326 if ( index != -1 )
327 win->activateAction( (Action)index );
328}