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() );
61 connect( theDesktop, SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
62 this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
66 connect( myStudy, SIGNAL( destroyed() ), this, SLOT( onDeleteStudy() ) );
70 SUIT_ViewManager::~SUIT_ViewManager()
74 myViewModel->setViewManager( 0 );
79 int SUIT_ViewManager::useNewId( const QString& type )
81 if ( !_ViewMgrId.contains( type ) )
82 _ViewMgrId.insert( type, 0 );
84 int id = _ViewMgrId[type];
90 int SUIT_ViewManager::getGlobalId() const {
92 SUIT_Application* app = SUIT_Session::session()->activeApplication();
94 id = app->viewManagerId(this);
99 void SUIT_ViewManager::setTitle( const QString& theTitle )
101 if ( myTitle == theTitle )
105 for ( int i = 0; i < myViews.count(); i++ )
106 setViewName( myViews[i] );
109 void SUIT_ViewManager::setIcon( const QPixmap& theIcon )
112 for ( int i = 0; i < myViews.count(); i++ )
113 myViews[i]->setWindowIcon( QIcon( myIcon ) );
116 /*!Sets view model \a theViewModel to view manager.*/
117 void SUIT_ViewManager::setViewModel(SUIT_ViewModel* theViewModel)
119 if (myViewModel && myViewModel != theViewModel) {
120 myViewModel->setViewManager(0);
123 myViewModel = theViewModel;
125 myViewModel->setViewManager(this);
128 /*!Sets view name for view window \a theView.*/
129 void SUIT_ViewManager::setViewName( SUIT_ViewWindow* theView )
131 QString title = prepareTitle( getTitle(), myId + 1, myViews.indexOf( theView ) + 1 );
132 theView->setWindowTitle( title );
135 QString SUIT_ViewManager::prepareTitle( const QString& title, const int mId, const int vId )
138 QRegExp re( "%[%MV]" );
140 while ( ( i = re.indexIn( res, i ) ) != -1 )
143 QString str = res.mid( i, re.matchedLength() );
144 if ( str == QString( "%%" ) )
145 rplc = QString( "%" );
146 else if ( str == QString( "%M" ) )
147 rplc = QString::number( mId );
148 else if ( str == QString( "%V" ) )
149 rplc = QString::number( vId );
150 res.replace( i, re.matchedLength(), rplc );
156 /*! Creates View, adds it into list of views and returns just created view window*/
157 SUIT_ViewWindow* SUIT_ViewManager::createViewWindow()
159 SUIT_ViewWindow* aView = myViewModel->createView(myDesktop);
161 if ( !insertView( aView ) ){
166 setViewName( aView );
167 aView->setWindowIcon( QIcon( myIcon ) );
169 //myDesktop->addViewWindow( aView );
170 //it is done automatically during creation of view
172 aView->setViewManager( this );
173 emit viewCreated( aView );
175 // Special treatment for the case when <aView> is the first one in this view manager
176 // -> call onWindowActivated() directly, because somebody may always want
177 // to use getActiveView()
179 onWindowActivated( aView );
184 /*! Get identifier */
185 int SUIT_ViewManager::getId() const
190 /*!Create view window.*/
191 void SUIT_ViewManager::createView()
196 QVector<SUIT_ViewWindow*> SUIT_ViewManager::getViews() const
198 QVector<SUIT_ViewWindow*> res;
199 for ( int i = 0; i < myViews.count(); i++ )
202 res.append( myViews[i] );
208 /*!Insert view window to view manager.
209 *\retval false - if something wrong, else true.
211 bool SUIT_ViewManager::insertView(SUIT_ViewWindow* theView)
213 unsigned int aSize = myViews.size();
214 unsigned int aNbItems = myViews.count() + 1;
215 if ( aNbItems > aSize )
217 myViews.resize( aNbItems );
218 aSize = myViews.size();
221 connect(theView, SIGNAL(closing(SUIT_ViewWindow*)),
222 this, SLOT(onClosingView(SUIT_ViewWindow*)));
224 connect(theView, SIGNAL(tryClosing(SUIT_ViewWindow*)),
225 this, SIGNAL(tryCloseView(SUIT_ViewWindow*)));
227 connect(theView, SIGNAL(mousePressed(SUIT_ViewWindow*, QMouseEvent*)),
228 this, SLOT(onMousePressed(SUIT_ViewWindow*, QMouseEvent*)));
230 connect(theView, SIGNAL(mouseReleased(SUIT_ViewWindow*, QMouseEvent*)),
231 this, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
233 connect(theView, SIGNAL(mouseDoubleClicked(SUIT_ViewWindow*, QMouseEvent*)),
234 this, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
236 connect(theView, SIGNAL(mouseMoving(SUIT_ViewWindow*, QMouseEvent*)),
237 this, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)));
239 connect(theView, SIGNAL(wheeling(SUIT_ViewWindow*, QWheelEvent*)),
240 this, SIGNAL(wheel(SUIT_ViewWindow*, QWheelEvent*)));
242 connect(theView, SIGNAL(keyPressed(SUIT_ViewWindow*, QKeyEvent*)),
243 this, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)));
245 connect(theView, SIGNAL(keyReleased(SUIT_ViewWindow*, QKeyEvent*)),
246 this, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)));
248 connect(theView, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
249 this, SLOT (onContextMenuRequested( QContextMenuEvent * )));
251 for ( uint i = 0; i < aSize; i++ )
253 if ( myViews[i] == 0 )
255 myViews[i] = theView;
262 /*!Emit delete view. Remove view window \a theView from view manager.
264 void SUIT_ViewManager::onClosingView( SUIT_ViewWindow* theView )
266 closeView( theView );
270 Remove the view window \a theView from view manager and destroy it.
272 void SUIT_ViewManager::closeView( SUIT_ViewWindow* theView )
277 QPointer<SUIT_ViewWindow> view( theView );
279 view->setClosable( false );
282 if ( !view->testAttribute( Qt::WA_DeleteOnClose ) )
285 emit deleteView( view );
292 /*!Remove view window \a theView from view manager.
293 *And close the last view, if it has \a theView.
295 void SUIT_ViewManager::removeView( SUIT_ViewWindow* theView )
297 theView->disconnect( this );
298 myViews.remove( myViews.indexOf( theView ) );
299 if ( myActiveView == theView )
301 if ( !myViews.count() )
302 emit lastViewClosed( this );
306 Set or clear flag Qt::WDestructiveClose for all views
308 void SUIT_ViewManager::setDestructiveClose( const bool on )
310 for ( int i = 0; i < myViews.count(); i++ )
311 myViews[i]->setDestructiveClose( on );
315 Returns 'true' if any of views (view windows) is visible.
317 bool SUIT_ViewManager::isVisible() const
320 for ( int i = 0; i < myViews.count() && !res; i++ )
321 res = myViews[i]->isVisibleTo( myViews[i]->parentWidget() );
326 Show or hide all views (view windows)
328 void SUIT_ViewManager::setShown( const bool on )
330 for ( int i = 0; i < myViews.count(); i++ )
331 myViews.at( i )->setVisible( on );
334 /*!Emit on \a theEvent mouse pressed in \a theView.*/
335 void SUIT_ViewManager::onMousePressed(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
337 emit mousePress(theView, theEvent);
340 /*!Emit activated for view \a view.
342 void SUIT_ViewManager::onWindowActivated(SUIT_ViewWindow* view)
345 unsigned int aSize = myViews.size();
346 for (uint i = 0; i < aSize; i++) {
347 if (myViews[i] && myViews[i] == view) {
349 emit activated( this );
358 void SUIT_ViewManager::closeAllViews()
360 for ( int i = 0; i < myViews.size(); i++ )
366 *\retval QString - type of view model.
368 QString SUIT_ViewManager::getType() const
370 return (!myViewModel)? "": myViewModel->getType();
374 *\retval SUIT_Study* - current study.
376 SUIT_Study* SUIT_ViewManager::study() const
382 * Sets stydy to NULL.
384 void SUIT_ViewManager::onDeleteStudy()
389 /*! invoke method of SUIT_PopupClient, which notifies about popup*/
390 void SUIT_ViewManager::onContextMenuRequested( QContextMenuEvent* e )
392 contextMenuRequest( e );
395 /*!Context menu popup for \a popup.*/
396 void SUIT_ViewManager::contextMenuPopup( QMenu* popup )
398 SUIT_ViewModel* vm = getViewModel();
400 vm->contextMenuPopup( popup );
403 /*! option detached (false by default) to set before the viewWindow is added to Desktop TabBar */
404 void SUIT_ViewManager::setDetached(bool detached)
406 myIsDetached = detached;
409 /*! get option detached (false by default) to decide if the viewWindow is to be added to Desktop TabBar */
410 bool SUIT_ViewManager::getDetached() const