Salome HOME
Merge from V6_main 11/02/2013
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewFrame.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "OCCViewer_ViewFrame.h"
24 #include "OCCViewer_ViewWindow.h"
25 #include "OCCViewer_ViewModel.h"
26
27 #include <SUIT_ViewManager.h>
28
29 #include <QFrame>
30 #include <QLayout>
31 #include <QApplication>
32
33 OCCViewer_ViewFrame::OCCViewer_ViewFrame(SUIT_Desktop* theDesktop, OCCViewer_Viewer* theModel)
34   : OCCViewer_ViewWindow( theDesktop, theModel ), myPopupRequestedView(0)
35 {
36   QFrame* centralFrame = new QFrame( this );
37   setCentralWidget( centralFrame );
38
39   OCCViewer_ViewWindow* view0 = theModel->createSubWindow();
40   updateWindowTitle( view0 );
41   view0->setParent( centralFrame );
42   myViews.append( view0 ); // MAIN_VIEW
43
44   myLayout = new QGridLayout( centralFrame );
45   myLayout->setMargin( 0 );
46   myLayout->setSpacing( 1 );
47
48   myLayout->addWidget( view0, 1, 1 );
49   connectViewSignals(view0);
50 }
51
52 OCCViewer_ViewFrame::~OCCViewer_ViewFrame()
53 {
54 }
55
56 bool OCCViewer_ViewFrame::event( QEvent* e )
57 {
58   if ( e->type() == QEvent::WindowTitleChange ) {
59     updateWindowTitle( getView( MAIN_VIEW ) );
60     updateWindowTitle( getView( BOTTOM_LEFT ) );
61     updateWindowTitle( getView( TOP_LEFT ) );
62     updateWindowTitle( getView( TOP_RIGHT ) );
63   }
64   return OCCViewer_ViewWindow::event( e );
65 }
66
67 //**************************************************************************************
68 OCCViewer_ViewWindow* OCCViewer_ViewFrame::getView( const int i ) const
69 {
70   return ( i >= 0 && i < myViews.count() ) ? myViews.at( i ) : 0 ;
71 }
72
73 //**************************************************************************************
74 void OCCViewer_ViewFrame::setViewManager( SUIT_ViewManager* theMgr )
75 {
76   OCCViewer_ViewWindow::setViewManager(theMgr);
77   foreach (OCCViewer_ViewWindow* aView, myViews) {
78     aView->setViewManager(theMgr); 
79   }
80 }
81
82
83 //**************************************************************************************
84 void OCCViewer_ViewFrame::onMaximizedView( OCCViewer_ViewWindow* theView, bool isMaximized)
85 {
86   if (isMaximized) {
87     if (myViews.count() <= 1)
88       return;
89
90     myLayout->setColumnStretch(0, 0);
91     myLayout->setColumnStretch(1, 0);
92     int i = 0;
93     OCCViewer_ViewWindow* view = 0;
94     for ( i = BOTTOM_RIGHT; i <= TOP_RIGHT; i++) {
95       view = myViews.at(i);
96       view->setVisible( view == theView );
97       view->setMaximized( view == theView, false );
98     }
99   }
100   else {
101     OCCViewer_Viewer* aModel = dynamic_cast<OCCViewer_Viewer*>(myManager->getViewModel());
102     if (!aModel) return;
103
104     myLayout->setColumnStretch(0, 10);
105     myLayout->setColumnStretch(1, 10);
106
107     int i = 0;
108     if (myViews.count() == 1) {
109       //QColor aColor = myViews.at( MAIN_VIEW )->backgroundColor();
110       OCCViewer_ViewWindow* view = 0;
111       for ( i = BOTTOM_LEFT; i <= TOP_RIGHT; i++) {
112         view = aModel->createSubWindow();
113         view->set2dMode( (Mode2dType) i );
114         view->setParent( centralWidget() );
115         view->setViewManager(myManager); 
116         updateWindowTitle( view );
117         myViews.append( view ); 
118         aModel->initView(view);
119         view->setMaximized(false, false);
120         view->setDropDownButtons( dropDownButtons() );
121         connectViewSignals(view);
122         view->setBackground(aModel->background(i));
123       }
124       myLayout->addWidget( myViews.at(BOTTOM_LEFT), 1, 0 );
125       myLayout->addWidget( myViews.at(TOP_LEFT), 0, 0 );
126       myLayout->addWidget( myViews.at(TOP_RIGHT), 0, 1 );
127     }
128     OCCViewer_ViewWindow* view = 0;
129     for ( i = BOTTOM_RIGHT; i <= TOP_RIGHT; i++) {
130       view = myViews.at(i);
131       view->show();
132       view->setMaximized( false, false );
133       ///////////////QApplication::processEvents(); // VSR: hangs up ?
134       if (view != theView)
135         view->onViewFitAll();
136     }
137   }
138   myLayout->invalidate();
139 }
140
141 OCCViewer_ViewPort3d* OCCViewer_ViewFrame::getViewPort(int theView) 
142
143   return getView(theView)? getView(theView)->getViewPort() : 0;
144 }
145   
146 void OCCViewer_ViewFrame::updateEnabledDrawMode() 
147
148   foreach (OCCViewer_ViewWindow* aView, myViews) {
149     aView->updateEnabledDrawMode(); 
150   }
151 }
152
153 void OCCViewer_ViewFrame::setCuttingPlane( bool on, const double x , const double y , const double z,
154                                            const double dx, const double dy, const double dz)  
155
156   foreach (OCCViewer_ViewWindow* aView, myViews) {
157     aView->setCuttingPlane(on, x, y, z, dx, dy, dz); 
158     aView->update();
159   }
160 }
161
162 //**************************************************************************************
163 void OCCViewer_ViewFrame::setCuttingPlane( bool on, const gp_Pln thePln ) 
164
165   foreach (OCCViewer_ViewWindow* aView, myViews) {
166     aView->setCuttingPlane(on, thePln); 
167     aView->update();
168   }
169 }
170   
171 //**************************************************************************************
172 void OCCViewer_ViewFrame::setInteractionStyle( const int i ) 
173
174   foreach (OCCViewer_ViewWindow* aView, myViews) {
175     aView->setInteractionStyle(i); 
176   }
177 }
178
179 //**************************************************************************************
180 void OCCViewer_ViewFrame::setZoomingStyle( const int i ) 
181
182   foreach (OCCViewer_ViewWindow* aView, myViews) {
183     aView->setZoomingStyle(i); 
184   }
185 }
186   
187 //**************************************************************************************
188 void OCCViewer_ViewFrame::connectViewSignals(OCCViewer_ViewWindow* theView)
189 {
190   connect( theView, SIGNAL( maximized( OCCViewer_ViewWindow*, bool ) ), 
191            this, SLOT( onMaximizedView( OCCViewer_ViewWindow*, bool ) ) );
192
193   connect( theView, SIGNAL( wheeling(SUIT_ViewWindow*, QWheelEvent*) ), 
194            this, SIGNAL( wheeling(SUIT_ViewWindow*, QWheelEvent*) ) );
195
196   connect( theView, SIGNAL( keyReleased(SUIT_ViewWindow*, QKeyEvent*) ), 
197            this, SIGNAL( keyReleased(SUIT_ViewWindow*, QKeyEvent*) ) );
198   connect( theView, SIGNAL( keyPressed(SUIT_ViewWindow*, QKeyEvent*) ), 
199            this, SIGNAL( keyPressed(SUIT_ViewWindow*, QKeyEvent*) ) );
200
201   connect( theView, SIGNAL( mouseDoubleClicked(SUIT_ViewWindow*, QMouseEvent*) ), 
202            this, SIGNAL( mouseDoubleClicked(SUIT_ViewWindow*, QMouseEvent*) ) );
203   connect( theView, SIGNAL( mousePressed(SUIT_ViewWindow*, QMouseEvent*) ), 
204            this, SIGNAL( mousePressed(SUIT_ViewWindow*, QMouseEvent*) ) );
205   connect( theView, SIGNAL( mouseReleased(SUIT_ViewWindow*, QMouseEvent*) ), 
206            this, SIGNAL( mouseReleased(SUIT_ViewWindow*, QMouseEvent*) ) );
207   connect( theView, SIGNAL( mouseMoving(SUIT_ViewWindow*, QMouseEvent*) ), 
208            this, SIGNAL( mouseMoving(SUIT_ViewWindow*, QMouseEvent*) ) );
209
210   // The signal is used to process get/set background color from popup
211   connect( theView, SIGNAL( contextMenuRequested(QContextMenuEvent*) ), 
212            this, SLOT( onContextMenuRequested(QContextMenuEvent*) ) );
213
214   connect( theView, SIGNAL( contextMenuRequested(QContextMenuEvent*) ), 
215            this, SIGNAL( contextMenuRequested(QContextMenuEvent*) ) );
216
217   connect( theView, SIGNAL( viewCloned( SUIT_ViewWindow* ) ),
218            this,    SIGNAL( viewCloned( SUIT_ViewWindow* ) ) );
219 }
220
221 // obsolete
222 void OCCViewer_ViewFrame::setBackgroundColor( const QColor& theColor )
223 {
224   if (myPopupRequestedView)
225     myPopupRequestedView->setBackgroundColor(theColor); 
226   else {
227     foreach (OCCViewer_ViewWindow* aView, myViews) {
228       if (aView->isVisible())
229         aView->setBackgroundColor(theColor); 
230     }
231   }
232 }
233 void OCCViewer_ViewFrame::setBackground( const Qtx::BackgroundData& theBackground )
234 {
235   if (myPopupRequestedView)
236     myPopupRequestedView->setBackground(theBackground); 
237   else {
238     foreach (OCCViewer_ViewWindow* aView, myViews) {
239       if (aView->isVisible())
240         aView->setBackground(theBackground); 
241     }
242   }
243 }
244
245 void OCCViewer_ViewFrame::onViewFitAll()
246 {
247   foreach (OCCViewer_ViewWindow* aView, myViews) {
248     aView->onViewFitAll(); 
249   }
250 }
251
252 void OCCViewer_ViewFrame::onFitAll()
253 {
254   foreach (OCCViewer_ViewWindow* aView, myViews) {
255     aView->onFitAll(); 
256   }
257 }
258
259 // obsolete  
260 QColor OCCViewer_ViewFrame::backgroundColor() const 
261
262   if (myPopupRequestedView)
263     return myPopupRequestedView->backgroundColor(); 
264
265   foreach (OCCViewer_ViewWindow* aView, myViews) {
266     if (aView->isVisible())
267       return aView->backgroundColor(); 
268   }
269   return getView(MAIN_VIEW)->backgroundColor(); 
270 }
271
272 Qtx::BackgroundData OCCViewer_ViewFrame::background() const 
273
274   if (myPopupRequestedView)
275     return myPopupRequestedView->background(); 
276
277   foreach (OCCViewer_ViewWindow* aView, myViews) {
278     if (aView->isVisible())
279       return aView->background(); 
280   }
281   return getView(MAIN_VIEW)->background(); 
282 }
283
284 QImage OCCViewer_ViewFrame::dumpView()
285 {
286   foreach (OCCViewer_ViewWindow* aView, myViews) {
287     if (aView->isVisible())
288       return aView->dumpView();
289   }
290   return QImage();
291 }
292
293 bool OCCViewer_ViewFrame::dumpViewToFormat( const QImage& image, const QString& fileName, const QString& format )
294 {
295   foreach (OCCViewer_ViewWindow* aView, myViews) {
296     if (aView->isVisible())
297       return aView->dumpViewToFormat( image, fileName, format );
298   }
299   return false;
300 }
301
302 void OCCViewer_ViewFrame::onContextMenuRequested(QContextMenuEvent*)
303 {
304   myPopupRequestedView = dynamic_cast<OCCViewer_ViewWindow*>(sender());
305 }
306
307 void OCCViewer_ViewFrame::onDumpView()
308 {
309   if (myPopupRequestedView) {
310     myPopupRequestedView->onDumpView(); 
311   }
312   else {
313     getView(MAIN_VIEW)->onDumpView(); 
314   }
315 }
316
317 void OCCViewer_ViewFrame::setDropDownButtons( bool on )
318 {
319   foreach( OCCViewer_ViewWindow* aView, myViews ) {
320     aView->setDropDownButtons( on );
321   }
322   OCCViewer_ViewWindow::setDropDownButtons( on );
323 }
324
325 QString OCCViewer_ViewFrame::getVisualParameters()
326 {
327   QStringList params;
328   int maximizedView = 999;
329   for ( int i = BOTTOM_RIGHT; i <= TOP_RIGHT && i < myViews.count(); i++) {
330     if ( getView(i)->isVisible() )
331       maximizedView = ( maximizedView != -1 ) ? ( maximizedView == 999 ? i : -1 ) : ( maximizedView );
332     params << getView(i)->getVisualParameters();
333   }
334   params.prepend( QString::number( maximizedView ) );
335   return params.join( "|" );
336 }
337
338 void OCCViewer_ViewFrame::setVisualParameters( const QString& parameters ) 
339 {
340   QStringList params = parameters.split( "|" );
341   if ( params.count() > 1 ) {
342     int maximizedView = params[0].toInt();
343     if ( myViews.count() < params.count()-1 )
344       onMaximizedView( getView(MAIN_VIEW), false ); // secondary views are not created yet, but should be
345     for ( int i = 1; i < params.count(); i++ ) {
346       int idx = i-1;
347       getView( idx )->setVisualParameters( params[i] );
348     }
349     onMaximizedView( getView( maximizedView ), maximizedView != -1 ); // set proper sib-window maximized 
350   }
351   else {
352     // handle obsolete versions - no parameters for xy, yz, xz views
353     getView(MAIN_VIEW)->setVisualParameters( parameters );
354   }
355 }
356
357 SUIT_CameraProperties OCCViewer_ViewFrame::cameraProperties()
358 {
359   // view frame does not have camera properties
360   return SUIT_CameraProperties();
361 }
362
363 void OCCViewer_ViewFrame::updateWindowTitle(OCCViewer_ViewWindow* theView)
364 {
365   if ( !theView )
366     return;
367   QString title;
368   switch ( theView->get2dMode() ) {
369   case No2dMode:
370     title = "3D"; break;
371   case XYPlane:
372     title = "YX"; break;
373   case XZPlane:
374     title = "XZ"; break;
375   case YZPlane:
376     title = "YZ"; break;
377   default:
378     break;
379   }
380   if ( !title.isEmpty() )
381     theView->setWindowTitle( windowTitle() + " - " + title );
382 }