1 // Copyright (C) 2007-2016 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 "SUIT_FileDlg.h"
36 #include "QtxActionToolMgr.h"
37 #include "QtxMultiAction.h"
42 #include <QApplication>
43 #include <QContextMenuEvent>
45 /*!\class SUIT_ViewWindow
46 * Class provide view window.
49 /*! Dump view custom event*/
50 const int DUMP_EVENT = QEvent::User + 123;
53 SUIT_ViewWindow::SUIT_ViewWindow( SUIT_Desktop* theDesktop )
54 : QMainWindow( theDesktop ), myManager( 0 ), myIsDropDown( true ), mySyncAction( 0 )
56 myDesktop = theDesktop;
58 setWindowIcon( myDesktop ? myDesktop->windowIcon() : QApplication::windowIcon() );
60 setAttribute( Qt::WA_DeleteOnClose );
62 myToolMgr = new QtxActionToolMgr( this );
64 setProperty( "VectorsMode", false );
68 SUIT_ViewWindow::~SUIT_ViewWindow()
73 Sets new view manager for window
74 \param theManager - new view manager
76 void SUIT_ViewWindow::setViewManager( SUIT_ViewManager* theManager )
78 myManager = theManager;
82 \return view manager of window
84 SUIT_ViewManager* SUIT_ViewWindow::getViewManager() const
90 \return QImage, containing all scene rendering in window
92 QImage SUIT_ViewWindow::dumpView()
97 bool SUIT_ViewWindow::dumpViewToPSFormat(const QString& fileName)
103 Saves image to file according to the format
105 \param fileName - name of file
106 \param format - string contains name of format (for example, "BMP"(default) or "JPEG", "JPG")
108 bool SUIT_ViewWindow::dumpViewToFormat( const QImage& img, const QString& fileName, const QString& format )
113 QString fmt = format;
115 fmt = QString( "BMP" ); // default format
116 else if( fmt == "JPG" )
119 QApplication::setOverrideCursor( Qt::WaitCursor );
120 bool res = img.save( fileName, fmt.toLatin1() );
121 QApplication::restoreOverrideCursor();
126 Saves scene rendering in window to file
127 \param fileName - name of file
128 \param format - string contains name of format (for example, "BMP"(default) or "JPEG", "JPG")
130 bool SUIT_ViewWindow::dumpViewToFormat( const QString& fileName, const QString& format )
133 return dumpViewToFormat( dumpView(), fileName, format );
137 Set or clear flag Qt::WDestructiveClose
139 void SUIT_ViewWindow::setDestructiveClose( const bool on )
141 setAttribute( Qt::WA_DeleteOnClose, on );
144 /*! Close event \a theEvent.
146 void SUIT_ViewWindow::closeEvent( QCloseEvent* e )
149 emit tryClosing( this );
150 if ( closable() ) emit closing( this );
153 /*! Context menu requested for event \a e.
155 void SUIT_ViewWindow::contextMenuEvent( QContextMenuEvent* e )
159 QMainWindow::contextMenuEvent( e );
161 if ( e->isAccepted() )
164 if ( e->reason() != QContextMenuEvent::Mouse )
165 emit contextMenuRequested( e );
168 /*! Post events on dump view.
170 void SUIT_ViewWindow::onDumpView()
172 // VSV (TRIPOLI dev): next line commented: causes error messages
173 //QApplication::postEvent( this, new QPaintEvent( QRect( 0, 0, width(), height() ) ) );
174 QApplication::postEvent( this, new QEvent( (QEvent::Type)DUMP_EVENT ) );
178 \return filters for image files
180 QString SUIT_ViewWindow::filter(bool includePS) const
182 QString aFilter = tr( "TLT_IMAGE_FILES" );
187 aFilter+=";;"+tr( "POSTSCRIPT_FILES" );
192 /*! Reaction view window on event \a e.
194 bool SUIT_ViewWindow::event( QEvent* e )
196 if ( e->type() == DUMP_EVENT )
199 SUIT_Application* app = NULL;
200 if (myManager && myManager->study() && myManager->study()->application())
201 app = myManager->study()->application();
202 bool IncludePs = true;
205 fileName = app->getFileName( false, QString(), filter(IncludePs), tr( "TLT_DUMP_VIEW" ), 0 ); //old way
208 QStringList fls = filter(IncludePs).split( ";;", QString::SkipEmptyParts );
209 fileName = SUIT_FileDlg::getFileName( NULL, QString(), fls, tr( "TLT_DUMP_VIEW" ), false, true );
211 if ( !fileName.isEmpty() )
213 QString fmt = SUIT_Tools::extension( fileName ).toUpper();
214 if (fmt == "PS" || fmt == "EPS" )
215 bOk = dumpViewToPSFormat(fileName);
218 QImage im = dumpView();
220 bOk = dumpViewToFormat( im, fileName, fmt );
224 bOk = true; // cancelled
227 SUIT_MessageBox::critical( this, tr( "ERROR" ), tr( "ERR_CANT_DUMP_VIEW" ) );
231 return QMainWindow::event( e );
234 /*! Called by SUIT_Accel::onActivated() when a key accelerator was activated and this window was active
236 bool SUIT_ViewWindow::onAccelAction( int _action )
238 return action( _action );
241 /*! action handle standard action (zoom, pan) or custom action. to be redefined in successors.
243 bool SUIT_ViewWindow::action( const int )
248 /*! Returns \c true if view window can be closed by the user
250 bool SUIT_ViewWindow::closable() const
252 QVariant val = property( "closable" );
253 return !val.isValid() || val.toBool();
256 /*! Set / reset "closable" option of the view window
258 bool SUIT_ViewWindow::setClosable( const bool on )
260 bool prev = closable();
261 setProperty( "closable", on );
266 \return string containing visual parameters of window
268 QString SUIT_ViewWindow::getVisualParameters()
274 Sets visual parameters of window by its string representation
275 \param parameters - string with visual parameters
277 void SUIT_ViewWindow::setVisualParameters( const QString& /*parameters*/ )
282 \return associated tool bar manager
284 QtxActionToolMgr* SUIT_ViewWindow::toolMgr() const
290 \brief Set buttons mode to drop-down (\a on = \c true) or ligned (\a on = \c false)
291 \param on new buttons mode
292 \sa dropDownButtons()
294 void SUIT_ViewWindow::setDropDownButtons( bool on )
296 if ( myIsDropDown != on ) {
298 if ( myIsDropDown ) {
299 ActionsMap::const_iterator it;
300 for( it = myMultiActions.constBegin(); it != myMultiActions.constEnd(); ++it )
303 const QList<QtxMultiAction*>& mlist = it.value();
304 QList<QtxMultiAction*>::const_iterator mit;
305 for ( mit = mlist.constBegin(); mit != mlist.constEnd(); ++mit )
307 QtxMultiAction* ma = *mit;
308 const QList<QAction*> alist = ma->actions();
309 if ( alist.isEmpty() ) continue;
310 int idx = toolMgr()->index( toolMgr()->actionId( alist[0] ), tid );
311 if ( idx == -1 ) continue;
312 foreach ( QAction* a, alist ) toolMgr()->remove( toolMgr()->actionId( a ), tid );
313 toolMgr()->insert( ma, tid, idx );
316 myMultiActions.clear();
319 QIntList tblist = toolMgr()->toolBarsIds();
320 QIntList alist = toolMgr()->idList();
321 foreach( int aid, alist )
323 QtxMultiAction* ma = qobject_cast<QtxMultiAction*>( toolMgr()->action( aid ) );
325 foreach( int tid, tblist )
327 int idx = toolMgr()->index( aid, tid );
330 myMultiActions[ tid ].append( ma );
331 toolMgr()->remove( aid, tid );
332 foreach( QAction* a, ma->actions() ) toolMgr()->insert( a, tid, idx++ );
341 \brief Get current buttons mode
342 \return current buttons mode
343 \sa setDropDownButtons()
345 bool SUIT_ViewWindow::dropDownButtons() const
351 \return window unique identifier
353 int SUIT_ViewWindow::getId() const
355 return int(long(this));
359 Get camera properties for the view window.
360 \return shared pointer on camera properties. Base implementation
361 returns null properties.
363 SUIT_CameraProperties SUIT_ViewWindow::cameraProperties()
365 return SUIT_CameraProperties();
369 Synchronize this view window's camera properties with specified
372 This method is a part of general views synchronization mechanism.
373 It should be redefined in successors. Base imlementation does nothing.
375 \param otherWindow other view window
377 void SUIT_ViewWindow::synchronize( SUIT_ViewWindow* /*otherWindow*/ )
379 // base implementation does nothing
383 Get action for views syncronization.
385 This method is a part of general views synchronization mechanism.
386 It creates an action that can be inserted, for instance, to the toolbar.
388 \return action for views synchronization
390 QAction* SUIT_ViewWindow::synchronizeAction()
392 if ( !mySyncAction ) {
393 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
394 mySyncAction = new QtxAction( tr( "MNU_SYNCHRONIZE_VIEW" ),
395 resMgr->loadPixmap( "SUIT", tr( "ICON_VIEW_SYNC" ) ),
396 tr( "MNU_SYNCHRONIZE_VIEW" ), 0, this );
397 mySyncAction->setStatusTip( tr( "DSC_SYNCHRONIZE_VIEW" ) );
398 mySyncAction->setMenu( new QMenu( this ) );
399 mySyncAction->setCheckable( true );
400 connect( mySyncAction->menu(), SIGNAL( aboutToShow() ), this, SLOT( updateSyncViews() ) );
401 connect( mySyncAction, SIGNAL( triggered( bool ) ), this, SLOT( onSynchronizeView( bool ) ) );
407 Emit notification signal that the view is transformed.
408 Other views can use the signal for synchronization.
410 void SUIT_ViewWindow::emitViewModified()
412 emit viewModified( this );
416 Update list of available view for the "Synchronize View" action
418 void SUIT_ViewWindow::updateSyncViews()
420 SUIT_CameraProperties props = cameraProperties();
421 if ( !props.isValid() )
424 QAction* anAction = synchronizeAction();
425 if ( anAction && anAction->menu() ) {
426 int currentId = anAction->data().toInt();
427 anAction->menu()->clear();
428 SUIT_Application* app = SUIT_Session::session()->activeApplication();
430 SUIT_Desktop* d = app->desktop();
431 QList<SUIT_ViewWindow*> allViews = d->findChildren<SUIT_ViewWindow*>();
432 foreach( SUIT_ViewWindow* vw, allViews ) {
433 if ( !vw || vw == this ) continue; // skip invalid views and this one
434 SUIT_CameraProperties otherProps = vw->cameraProperties();
435 if ( otherProps.isCompatible( props ) ) {
436 QAction* a = anAction->menu()->addAction( vw->windowTitle() );
437 if ( vw->getId() == currentId ) {
442 a->setData( vw->getId() );
443 connect( a, SIGNAL( triggered( bool ) ), this, SLOT( onSynchronizeView( bool ) ) );
445 else if ( vw->getId() == currentId ) {
446 // other view, this one is being currently synchronized to, seems has become incompatible
447 // we have to break synchronization
448 vw->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), this, SLOT( synchronize( SUIT_ViewWindow* ) ) );
449 this->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), vw, SLOT( synchronize( SUIT_ViewWindow* ) ) );
451 bool blocked = anAction->blockSignals( true );
452 anAction->setChecked( false );
453 anAction->blockSignals( blocked );
454 anAction->setData( 0 );
456 QAction* a = vw->synchronizeAction();
458 blocked = a->blockSignals( true );
459 a->setChecked( false );
460 a->blockSignals( blocked );
465 if ( anAction->menu()->actions().isEmpty() ) {
466 anAction->setData( 0 );
467 anAction->menu()->addAction( tr( "MNU_SYNC_NO_VIEW" ) );
473 "Synchronize View" action slot.
475 void SUIT_ViewWindow::onSynchronizeView( bool checked )
477 QAction* a = qobject_cast<QAction*>( sender() );
479 synchronizeView( this, a->data().toInt() );
484 Synchronize camera properties of view \a viewWindow with
485 camera properties of view specified via \a id
487 void SUIT_ViewWindow::synchronizeView( SUIT_ViewWindow* viewWindow, int id )
489 SUIT_ViewWindow* sourceView = 0;
490 QList<SUIT_ViewWindow*> otherViews;
492 bool isSync = viewWindow->synchronizeAction() && viewWindow->synchronizeAction()->isChecked();
494 int vwid = viewWindow->getId();
496 SUIT_Application* app = SUIT_Session::session()->activeApplication();
498 SUIT_Desktop* d = app->desktop();
501 QList<SUIT_ViewWindow*> allViews = d->findChildren<SUIT_ViewWindow*>();
502 foreach( SUIT_ViewWindow* vw, allViews ) {
503 if ( !vw->cameraProperties().isValid() )
504 continue; // omit views not supporting camera properties
505 if ( vw->getId() == id )
506 sourceView = vw; // remember source view
507 else if ( vw != viewWindow )
508 otherViews.append( vw ); // collect all remaining views
511 if ( isSync && id ) {
512 // remove all possible disconnections
513 foreach( SUIT_ViewWindow* vw, otherViews ) {
514 // disconnect target view
515 vw->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), viewWindow, SLOT( synchronize( SUIT_ViewWindow* ) ) );
516 viewWindow->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), vw, SLOT( synchronize( SUIT_ViewWindow* ) ) );
518 // disconnect source view
519 vw->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), sourceView, SLOT( synchronize( SUIT_ViewWindow* ) ) );
520 sourceView->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), vw, SLOT( synchronize( SUIT_ViewWindow* ) ) );
522 QAction* a = vw->synchronizeAction();
524 int anid = a->data().toInt();
525 if ( a->isChecked() && ( anid == id || anid == vwid ) ) {
526 bool blocked = a->blockSignals( true );
527 a->setChecked( false );
528 a->blockSignals( blocked );
533 // reconnect source and target views
534 sourceView->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), viewWindow, SLOT( synchronize( SUIT_ViewWindow* ) ) );
535 viewWindow->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), sourceView, SLOT( synchronize( SUIT_ViewWindow* ) ) );
536 sourceView->connect( viewWindow, SIGNAL( viewModified( SUIT_ViewWindow* ) ), SLOT( synchronize( SUIT_ViewWindow* ) ) );
537 viewWindow->connect( sourceView, SIGNAL( viewModified( SUIT_ViewWindow* ) ), SLOT( synchronize( SUIT_ViewWindow* ) ) );
538 // synchronize target view with source view
539 viewWindow->synchronize( sourceView );
540 if ( viewWindow->synchronizeAction() )
541 viewWindow->synchronizeAction()->setData( sourceView->getId() );
542 QAction* sourceAction = sourceView->synchronizeAction();
543 if ( sourceAction ) {
544 sourceAction->setData( viewWindow->getId() );
545 if ( !sourceAction->isChecked() ) {
546 bool blocked = sourceAction->blockSignals( true );
547 sourceAction->setChecked( true );
548 sourceAction->blockSignals( blocked );
553 else if ( sourceView ) {
554 // reconnect source and target view
555 sourceView->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), viewWindow, SLOT( synchronize( SUIT_ViewWindow* ) ) );
556 viewWindow->disconnect( SIGNAL( viewModified( SUIT_ViewWindow* ) ), sourceView, SLOT( synchronize( SUIT_ViewWindow* ) ) );
557 viewWindow->synchronize( sourceView );
558 if ( viewWindow->synchronizeAction() )
559 viewWindow->synchronizeAction() ->setData( sourceView->getId() );
560 QAction* sourceAction = sourceView->synchronizeAction();
561 if ( sourceAction ) {
562 if ( sourceAction->data().toInt() == viewWindow->getId() && sourceAction->isChecked() ) {
563 bool blocked = sourceAction->blockSignals( true );
564 sourceAction->setChecked( false );
565 sourceAction->blockSignals( blocked );