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 #include "SUIT_ViewManager.h"
25 #include "SUIT_Desktop.h"
26 #include "SUIT_ViewModel.h"
27 #include "SUIT_ViewWindow.h"
28 #include "SUIT_Study.h"
29 #include "SUIT_Session.h"
39 QMap<QString, int> SUIT_ViewManager::_ViewMgrId;
41 /*!\class SUIT_ViewManager.
42 * Class provide manipulation with view windows.
46 SUIT_ViewManager::SUIT_ViewManager( SUIT_Study* theStudy,
47 SUIT_Desktop* theDesktop,
48 SUIT_ViewModel* theViewModel )
50 myDesktop( theDesktop ),
51 myTitle( "Default: %M - viewer %V" ),
57 setViewModel( theViewModel );
59 myId = useNewId( getType() );
63 connect( theDesktop, SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
64 this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
69 connect( myStudy, SIGNAL( destroyed() ), this, SLOT( onDeleteStudy() ) );
73 SUIT_ViewManager::~SUIT_ViewManager()
77 myViewModel->setViewManager( 0 );
82 int SUIT_ViewManager::useNewId( const QString& type )
84 if ( !_ViewMgrId.contains( type ) )
85 _ViewMgrId.insert( type, 0 );
87 int id = _ViewMgrId[type];
93 int SUIT_ViewManager::getGlobalId() const {
95 SUIT_Application* app = SUIT_Session::session()->activeApplication();
97 id = app->viewManagerId(this);
102 void SUIT_ViewManager::setTitle( const QString& theTitle )
104 if ( myTitle == theTitle )
108 for ( int i = 0; i < myViews.count(); i++ )
109 setViewName( myViews[i] );
112 void SUIT_ViewManager::setIcon( const QPixmap& theIcon )
115 for ( int i = 0; i < myViews.count(); i++ )
116 myViews[i]->setWindowIcon( QIcon( myIcon ) );
119 /*!Sets view model \a theViewModel to view manager.*/
120 void SUIT_ViewManager::setViewModel(SUIT_ViewModel* theViewModel)
122 if (myViewModel && myViewModel != theViewModel) {
123 myViewModel->setViewManager(0);
126 myViewModel = theViewModel;
128 myViewModel->setViewManager(this);
131 /*!Sets view name for view window \a theView.*/
132 void SUIT_ViewManager::setViewName( SUIT_ViewWindow* theView )
134 QString title = prepareTitle( getTitle(), myId + 1, myViews.indexOf( theView ) + 1 );
135 theView->setWindowTitle( title );
138 QString SUIT_ViewManager::prepareTitle( const QString& title, const int mId, const int vId )
141 QRegExp re( "%[%MV]" );
143 while ( ( i = re.indexIn( res, i ) ) != -1 )
146 QString str = res.mid( i, re.matchedLength() );
147 if ( str == QString( "%%" ) )
148 rplc = QString( "%" );
149 else if ( str == QString( "%M" ) )
150 rplc = QString::number( mId );
151 else if ( str == QString( "%V" ) )
152 rplc = QString::number( vId );
153 res.replace( i, re.matchedLength(), rplc );
159 /*! Creates View, adds it into list of views and returns just created view window*/
160 SUIT_ViewWindow* SUIT_ViewManager::createViewWindow()
162 SUIT_ViewWindow* aView = myViewModel->createView(myDesktop);
164 if ( !insertView( aView ) ){
169 setViewName( aView );
170 aView->setWindowIcon( QIcon( myIcon ) );
172 //myDesktop->addViewWindow( aView );
173 //it is done automatically during creation of view
175 aView->setViewManager( this );
176 emit viewCreated( aView );
178 // Special treatment for the case when <aView> is the first one in this view manager
179 // -> call onWindowActivated() directly, because somebody may always want
180 // to use getActiveView()
182 onWindowActivated( aView );
187 /*! Get identifier */
188 int SUIT_ViewManager::getId() const
193 /*!Create view window.*/
194 void SUIT_ViewManager::createView()
199 QVector<SUIT_ViewWindow*> SUIT_ViewManager::getViews() const
201 QVector<SUIT_ViewWindow*> res;
202 for ( int i = 0; i < myViews.count(); i++ )
205 res.append( myViews[i] );
211 /*!Insert view window to view manager.
212 *\retval false - if something wrong, else true.
214 bool SUIT_ViewManager::insertView(SUIT_ViewWindow* theView)
216 unsigned int aSize = myViews.size();
217 unsigned int aNbItems = myViews.count() + 1;
218 if ( aNbItems > aSize )
220 myViews.resize( aNbItems );
221 aSize = myViews.size();
224 connect(theView, SIGNAL(closing(SUIT_ViewWindow*)),
225 this, SLOT(onClosingView(SUIT_ViewWindow*)));
227 connect(theView, SIGNAL(tryClosing(SUIT_ViewWindow*)),
228 this, SIGNAL(tryCloseView(SUIT_ViewWindow*)));
230 connect(theView, SIGNAL(mousePressed(SUIT_ViewWindow*, QMouseEvent*)),
231 this, SLOT(onMousePressed(SUIT_ViewWindow*, QMouseEvent*)));
233 connect(theView, SIGNAL(mouseReleased(SUIT_ViewWindow*, QMouseEvent*)),
234 this, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
236 connect(theView, SIGNAL(mouseDoubleClicked(SUIT_ViewWindow*, QMouseEvent*)),
237 this, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
239 connect(theView, SIGNAL(mouseMoving(SUIT_ViewWindow*, QMouseEvent*)),
240 this, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)));
242 connect(theView, SIGNAL(wheeling(SUIT_ViewWindow*, QWheelEvent*)),
243 this, SIGNAL(wheel(SUIT_ViewWindow*, QWheelEvent*)));
245 connect(theView, SIGNAL(keyPressed(SUIT_ViewWindow*, QKeyEvent*)),
246 this, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)));
248 connect(theView, SIGNAL(keyReleased(SUIT_ViewWindow*, QKeyEvent*)),
249 this, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)));
251 connect(theView, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
252 this, SLOT (onContextMenuRequested( QContextMenuEvent * )));
254 for ( uint i = 0; i < aSize; i++ )
256 if ( myViews[i] == 0 )
258 myViews[i] = theView;
265 /*!Emit delete view. Remove view window \a theView from view manager.
267 void SUIT_ViewManager::onClosingView( SUIT_ViewWindow* theView )
269 closeView( theView );
273 Remove the view window \a theView from view manager and destroy it.
275 void SUIT_ViewManager::closeView( SUIT_ViewWindow* theView )
280 QPointer<SUIT_ViewWindow> view( theView );
282 view->setClosable( false );
285 if ( !view->testAttribute( Qt::WA_DeleteOnClose ) )
288 emit deleteView( view );
295 /*!Remove view window \a theView from view manager.
296 *And close the last view, if it has \a theView.
298 void SUIT_ViewManager::removeView( SUIT_ViewWindow* theView )
300 theView->disconnect( this );
301 myViews.remove( myViews.indexOf( theView ) );
302 if ( myActiveView == theView )
304 if ( !myViews.count() )
305 emit lastViewClosed( this );
309 Set or clear flag Qt::WDestructiveClose for all views
311 void SUIT_ViewManager::setDestructiveClose( const bool on )
313 for ( int i = 0; i < myViews.count(); i++ )
314 myViews[i]->setDestructiveClose( on );
318 Returns 'true' if any of views (view windows) is visible.
320 bool SUIT_ViewManager::isVisible() const
323 for ( int i = 0; i < myViews.count() && !res; i++ )
324 res = myViews[i]->isVisibleTo( myViews[i]->parentWidget() );
329 Show or hide all views (view windows)
331 void SUIT_ViewManager::setShown( const bool on )
333 for ( int i = 0; i < myViews.count(); i++ )
334 myViews.at( i )->setVisible( on );
337 /*!Emit on \a theEvent mouse pressed in \a theView.*/
338 void SUIT_ViewManager::onMousePressed(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
340 emit mousePress(theView, theEvent);
343 /*!Emit activated for view \a view.
345 void SUIT_ViewManager::onWindowActivated(SUIT_ViewWindow* view)
348 unsigned int aSize = myViews.size();
349 for (uint i = 0; i < aSize; i++) {
350 if (myViews[i] && myViews[i] == view) {
352 emit activated( this );
361 void SUIT_ViewManager::closeAllViews()
363 for ( int i = 0; i < myViews.size(); i++ )
369 *\retval QString - type of view model.
371 QString SUIT_ViewManager::getType() const
373 return (!myViewModel)? "": myViewModel->getType();
377 *\retval SUIT_Study* - current study.
379 SUIT_Study* SUIT_ViewManager::study() const
385 * Sets stydy to NULL.
387 void SUIT_ViewManager::onDeleteStudy()
392 /*! invoke method of SUIT_PopupClient, which notifies about popup*/
393 void SUIT_ViewManager::onContextMenuRequested( QContextMenuEvent* e )
395 contextMenuRequest( e );
398 /*!Context menu popup for \a popup.*/
399 void SUIT_ViewManager::contextMenuPopup( QMenu* popup )
401 SUIT_ViewModel* vm = getViewModel();
403 vm->contextMenuPopup( popup );
406 /*! option detached (false by default) to set before the viewWindow is added to Desktop TabBar */
407 void SUIT_ViewManager::setDetached(bool detached)
409 myIsDetached = detached;
412 /*! get option detached (false by default) to decide if the viewWindow is to be added to Desktop TabBar */
413 bool SUIT_ViewManager::getDetached() const