1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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, or (at your option) any later version.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SUIT_ViewWindow.cxx: implementation of the SUIT_ViewWindow class.
25 #include "SUIT_ViewWindow.h"
27 #include "SUIT_Tools.h"
28 #include "SUIT_Session.h"
29 #include "SUIT_Study.h"
30 #include "SUIT_Desktop.h"
31 #include "SUIT_MessageBox.h"
32 #include "SUIT_Application.h"
33 #include "SUIT_ViewManager.h"
34 #include "SUIT_ResourceMgr.h"
35 #include "QtxActionToolMgr.h"
36 #include "QtxMultiAction.h"
41 #include <QApplication>
42 #include <QContextMenuEvent>
44 /*!\class SUIT_ViewWindow
45 * Class provide view window.
48 /*! Dump view custom event*/
49 const int DUMP_EVENT = QEvent::User + 123;
52 SUIT_ViewWindow::SUIT_ViewWindow( SUIT_Desktop* theDesktop )
53 : QMainWindow( theDesktop ), myManager( 0 ), myIsDropDown( true ), mySyncAction( 0 )
55 myDesktop = theDesktop;
57 setWindowIcon( myDesktop ? myDesktop->windowIcon() : QApplication::windowIcon() );
59 setAttribute( Qt::WA_DeleteOnClose );
61 myToolMgr = new QtxActionToolMgr( this );
63 setProperty( "VectorsMode", false );
67 SUIT_ViewWindow::~SUIT_ViewWindow()
72 Sets new view manager for window
73 \param theManager - new view manager
75 void SUIT_ViewWindow::setViewManager( SUIT_ViewManager* theManager )
77 myManager = theManager;
81 \return view manager of window
83 SUIT_ViewManager* SUIT_ViewWindow::getViewManager() const
89 \return QImage, containing all scene rendering in window
91 QImage SUIT_ViewWindow::dumpView()
97 Saves image to file according to the format
99 \param fileName - name of file
100 \param format - string contains name of format (for example, "BMP"(default) or "JPEG", "JPG")
102 bool SUIT_ViewWindow::dumpViewToFormat( const QImage& img, const QString& fileName, const QString& format )
107 QString fmt = format;
109 fmt = QString( "BMP" ); // default format
110 else if( fmt == "JPG" )
113 QApplication::setOverrideCursor( Qt::WaitCursor );
114 bool res = img.save( fileName, fmt.toLatin1() );
115 QApplication::restoreOverrideCursor();
120 Saves scene rendering in window to file
121 \param fileName - name of file
122 \param format - string contains name of format (for example, "BMP"(default) or "JPEG", "JPG")
124 bool SUIT_ViewWindow::dumpViewToFormat( const QString& fileName, const QString& format )
127 return dumpViewToFormat( dumpView(), fileName, format );
131 Set or clear flag Qt::WDestructiveClose
133 void SUIT_ViewWindow::setDestructiveClose( const bool on )
135 setAttribute( Qt::WA_DeleteOnClose, on );
138 /*! Close event \a theEvent.
140 void SUIT_ViewWindow::closeEvent( QCloseEvent* e )
143 emit tryClosing( this );
144 if ( closable() ) emit closing( this );
147 /*! Context menu requested for event \a e.
149 void SUIT_ViewWindow::contextMenuEvent( QContextMenuEvent* e )
153 QMainWindow::contextMenuEvent( e );
155 if ( e->isAccepted() )
158 if ( e->reason() != QContextMenuEvent::Mouse )
159 emit contextMenuRequested( e );
162 /*! Post events on dump view.
164 void SUIT_ViewWindow::onDumpView()
166 // VSV (TRIPOLI dev): next line commented: causes error messages
167 //QApplication::postEvent( this, new QPaintEvent( QRect( 0, 0, width(), height() ) ) );
168 QApplication::postEvent( this, new QEvent( (QEvent::Type)DUMP_EVENT ) );
172 \return filters for image files
174 QString SUIT_ViewWindow::filter() const
176 return tr( "TLT_IMAGE_FILES" );
179 /*! Reaction view window on event \a e.
181 bool SUIT_ViewWindow::event( QEvent* e )
183 if ( e->type() == DUMP_EVENT )
186 if ( myManager && myManager->study() && myManager->study()->application() )
189 SUIT_Application* app = myManager->study()->application();
190 QString fileName = app->getFileName( false, QString(), filter(), tr( "TLT_DUMP_VIEW" ), 0 );
191 if ( !fileName.isEmpty() )
193 QImage im = dumpView();
194 QString fmt = SUIT_Tools::extension( fileName ).toUpper();
196 bOk = dumpViewToFormat( im, fileName, fmt );
199 bOk = true; // cancelled
202 SUIT_MessageBox::critical( this, tr( "ERROR" ), tr( "ERR_CANT_DUMP_VIEW" ) );
206 return QMainWindow::event( e );
209 /*! Called by SUIT_Accel::onActivated() when a key accelerator was activated and this window was active
211 bool SUIT_ViewWindow::onAccelAction( int _action )
213 return action( _action );
216 /*! action handle standard action (zoom, pan) or custom action. to be redefined in successors.
218 bool SUIT_ViewWindow::action( const int )
223 /*! Returns \c true if view window can be closed by the user
225 bool SUIT_ViewWindow::closable() const
227 QVariant val = property( "closable" );
228 return !val.isValid() || val.toBool();
231 /*! Set / reset "closable" option of the view window
233 bool SUIT_ViewWindow::setClosable( const bool on )
235 bool prev = closable();
236 setProperty( "closable", on );
241 \return string containing visual parameters of window
243 QString SUIT_ViewWindow::getVisualParameters()
249 Sets visual parameters of window by its string representation
250 \param parameters - string with visual parameters
252 void SUIT_ViewWindow::setVisualParameters( const QString& /*parameters*/ )
257 \return associated tool bar manager
259 QtxActionToolMgr* SUIT_ViewWindow::toolMgr() const
265 \brief Set buttons mode to drop-down (\a on = \c true) or ligned (\a on = \c false)
266 \param on new buttons mode
267 \sa dropDownButtons()
269 void SUIT_ViewWindow::setDropDownButtons( bool on )
271 if ( myIsDropDown != on ) {
273 if ( myIsDropDown ) {
274 ActionsMap::const_iterator it;
275 for( it = myMultiActions.constBegin(); it != myMultiActions.constEnd(); ++it )
278 const QList<QtxMultiAction*>& mlist = it.value();
279 QList<QtxMultiAction*>::const_iterator mit;
280 for ( mit = mlist.constBegin(); mit != mlist.constEnd(); ++mit )
282 QtxMultiAction* ma = *mit;
283 const QList<QAction*> alist = ma->actions();
284 if ( alist.isEmpty() ) continue;
285 int idx = toolMgr()->index( toolMgr()->actionId( alist[0] ), tid );
286 if ( idx == -1 ) continue;
287 foreach ( QAction* a, alist ) toolMgr()->remove( toolMgr()->actionId( a ), tid );
288 toolMgr()->insert( ma, tid, idx );
291 myMultiActions.clear();
294 QIntList tblist = toolMgr()->toolBarsIds();
295 QIntList alist = toolMgr()->idList();
296 foreach( int aid, alist )
298 QtxMultiAction* ma = qobject_cast<QtxMultiAction*>( toolMgr()->action( aid ) );
300 foreach( int tid, tblist )
302 int idx = toolMgr()->index( aid, tid );
305 myMultiActions[ tid ].append( ma );
306 toolMgr()->remove( aid, tid );
307 foreach( QAction* a, ma->actions() ) toolMgr()->insert( a, tid, idx++ );
316 \brief Get current buttons mode
317 \return current buttons mode
318 \sa setDropDownButtons()
320 bool SUIT_ViewWindow::dropDownButtons() const
326 \return window unique identifier
328 int SUIT_ViewWindow::getId() const
330 return int(long(this));
334 Get camera properties for the view window.
335 \return shared pointer on camera properties. Base implementation
336 returns null properties.
338 SUIT_CameraProperties SUIT_ViewWindow::cameraProperties()
340 return SUIT_CameraProperties();
344 Synchronize this view window's camera properties with specified
347 This method is a part of general views synchronization mechanism.
348 It should be redefined in successors. Base imlementation does nothing.
350 \param otherWindow other view window
352 void SUIT_ViewWindow::synchronize( SUIT_ViewWindow* /*otherWindow*/ )
354 // base implementation does nothing
358 Get action for views syncronization.
360 This method is a part of general views synchronization mechanism.
361 It creates an action that can be inserted, for instance, to the toolbar.
363 \return action for views synchronization
365 QAction* SUIT_ViewWindow::synchronizeAction()
367 if ( !mySyncAction ) {
368 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
369 mySyncAction = new QtxAction( tr( "MNU_SYNCHRONIZE_VIEW" ),
370 resMgr->loadPixmap( "SUIT", tr( "ICON_VIEW_SYNC" ) ),
371 tr( "MNU_SYNCHRONIZE_VIEW" ), 0, this );
372 mySyncAction->setStatusTip( tr( "DSC_SYNCHRONIZE_VIEW" ) );
373 mySyncAction->setMenu( new QMenu( this ) );
374 mySyncAction->setCheckable( true );
375 connect( mySyncAction->menu(), SIGNAL( aboutToShow() ), this, SLOT( updateSyncViews() ) );
376 connect( mySyncAction, SIGNAL( triggered( bool ) ), this, SLOT( onSynchronizeView( bool ) ) );
382 Emit notification signal that the view is transformed.
383 Other views can use the signal for synchronization.
385 void SUIT_ViewWindow::emitViewModified()
387 emit viewModified( this );
391 Update list of available view for the "Synchronize View" action
393 void SUIT_ViewWindow::updateSyncViews()
395 SUIT_CameraProperties props = cameraProperties();
396 if ( !props.isValid() )
399 QAction* anAction = synchronizeAction();
400 if ( anAction && anAction->menu() ) {
401 int currentId = anAction->data().toInt();
402 anAction->menu()->clear();
403 SUIT_Application* app = SUIT_Session::session()->activeApplication();
405 SUIT_Desktop* d = app->desktop();
406 QList<SUIT_ViewWindow*> allViews = qFindChildren<SUIT_ViewWindow*>( d );
407 foreach( SUIT_ViewWindow* vw, allViews ) {
408 if ( !vw || vw == this ) continue; // skip invalid views and this one
409 SUIT_CameraProperties otherProps = vw->cameraProperties();
410 if ( otherProps.isCompatible( props ) ) {
411 QAction* a = anAction->menu()->addAction( vw->windowTitle() );
412 if ( vw->getId() == currentId ) {
417 a->setData( vw->getId() );
418 connect( a, SIGNAL( triggered( bool ) ), this, SLOT( onSynchronizeView( bool ) ) );
420 else if ( vw->getId() == currentId ) {
421 // other view, this one is being currently synchronized to, seems has become incompatible
422 // we have to break synchronization
423 vw->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), this, SLOT( synchronize( SUIT_ViewWindow* ) ) );
424 this->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), vw, SLOT( synchronize( SUIT_ViewWindow* ) ) );
426 bool blocked = anAction->blockSignals( true );
427 anAction->setChecked( false );
428 anAction->blockSignals( blocked );
429 anAction->setData( 0 );
431 QAction* a = vw->synchronizeAction();
433 blocked = a->blockSignals( true );
434 a->setChecked( false );
435 a->blockSignals( blocked );
440 if ( anAction->menu()->actions().isEmpty() ) {
441 anAction->setData( 0 );
442 anAction->menu()->addAction( tr( "MNU_SYNC_NO_VIEW" ) );
448 "Synchronize View" action slot.
450 void SUIT_ViewWindow::onSynchronizeView( bool checked )
452 QAction* a = qobject_cast<QAction*>( sender() );
454 synchronizeView( this, a->data().toInt() );
459 Synchronize camera properties of view \a viewWindow with
460 camera properties of view specified via \a id
462 void SUIT_ViewWindow::synchronizeView( SUIT_ViewWindow* viewWindow, int id )
464 SUIT_ViewWindow* sourceView = 0;
465 QList<SUIT_ViewWindow*> otherViews;
467 bool isSync = viewWindow->synchronizeAction() && viewWindow->synchronizeAction()->isChecked();
469 int vwid = viewWindow->getId();
471 SUIT_Application* app = SUIT_Session::session()->activeApplication();
473 SUIT_Desktop* d = app->desktop();
476 QList<SUIT_ViewWindow*> allViews = qFindChildren<SUIT_ViewWindow*>( d );
477 foreach( SUIT_ViewWindow* vw, allViews ) {
478 if ( !vw->cameraProperties().isValid() )
479 continue; // omit views not supporting camera properties
480 if ( vw->getId() == id )
481 sourceView = vw; // remember source view
482 else if ( vw != viewWindow )
483 otherViews.append( vw ); // collect all remaining views
486 if ( isSync && id ) {
487 // remove all possible disconnections
488 foreach( SUIT_ViewWindow* vw, otherViews ) {
489 // disconnect target view
490 vw->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), viewWindow, SLOT( synchronize( SUIT_ViewWindow* ) ) );
491 viewWindow->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), vw, SLOT( synchronize( SUIT_ViewWindow* ) ) );
493 // disconnect source view
494 vw->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), sourceView, SLOT( synchronize( SUIT_ViewWindow* ) ) );
495 sourceView->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), vw, SLOT( synchronize( SUIT_ViewWindow* ) ) );
497 QAction* a = vw->synchronizeAction();
499 int anid = a->data().toInt();
500 if ( a->isChecked() && ( anid == id || anid == vwid ) ) {
501 bool blocked = a->blockSignals( true );
502 a->setChecked( false );
503 a->blockSignals( blocked );
508 // reconnect source and target views
509 sourceView->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), viewWindow, SLOT( synchronize( SUIT_ViewWindow* ) ) );
510 viewWindow->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), sourceView, SLOT( synchronize( SUIT_ViewWindow* ) ) );
511 sourceView->connect( viewWindow, SIGNAL( viewModified( SUIT_ViewWindow* ) ), SLOT( synchronize( SUIT_ViewWindow* ) ) );
512 viewWindow->connect( sourceView, SIGNAL( viewModified( SUIT_ViewWindow* ) ), SLOT( synchronize( SUIT_ViewWindow* ) ) );
513 // synchronize target view with source view
514 viewWindow->synchronize( sourceView );
515 if ( viewWindow->synchronizeAction() )
516 viewWindow->synchronizeAction()->setData( sourceView->getId() );
517 QAction* sourceAction = sourceView->synchronizeAction();
518 if ( sourceAction ) {
519 sourceAction->setData( viewWindow->getId() );
520 if ( !sourceAction->isChecked() ) {
521 bool blocked = sourceAction->blockSignals( true );
522 sourceAction->setChecked( true );
523 sourceAction->blockSignals( blocked );
528 else if ( sourceView ) {
529 // reconnect source and target view
530 sourceView->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), viewWindow, SLOT( synchronize( SUIT_ViewWindow* ) ) );
531 viewWindow->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), sourceView, SLOT( synchronize( SUIT_ViewWindow* ) ) );
532 viewWindow->synchronize( sourceView );
533 if ( viewWindow->synchronizeAction() )
534 viewWindow->synchronizeAction() ->setData( sourceView->getId() );
535 QAction* sourceAction = sourceView->synchronizeAction();
536 if ( sourceAction ) {
537 if ( sourceAction->data().toInt() == viewWindow->getId() && sourceAction->isChecked() ) {
538 bool blocked = sourceAction->blockSignals( true );
539 sourceAction->setChecked( false );
540 sourceAction->blockSignals( blocked );