1 // Copyright (C) 2007-2012 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.
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" ),
56 setViewModel( theViewModel );
58 myId = useNewId( getType() );
60 connect( theDesktop, SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
61 this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
65 connect( myStudy, SIGNAL( destroyed() ), this, SLOT( onDeleteStudy() ) );
69 SUIT_ViewManager::~SUIT_ViewManager()
73 myViewModel->setViewManager( 0 );
78 int SUIT_ViewManager::useNewId( const QString& type )
80 if ( !_ViewMgrId.contains( type ) )
81 _ViewMgrId.insert( type, 0 );
83 int id = _ViewMgrId[type];
89 int SUIT_ViewManager::getGlobalId() const {
91 SUIT_Application* app = SUIT_Session::session()->activeApplication();
93 id = app->viewManagerId(this);
98 void SUIT_ViewManager::setTitle( const QString& theTitle )
100 if ( myTitle == theTitle )
104 for ( int i = 0; i < myViews.count(); i++ )
105 setViewName( myViews[i] );
108 void SUIT_ViewManager::setIcon( const QPixmap& theIcon )
111 for ( int i = 0; i < myViews.count(); i++ )
112 myViews[i]->setWindowIcon( QIcon( myIcon ) );
115 /*!Sets view model \a theViewModel to view manager.*/
116 void SUIT_ViewManager::setViewModel(SUIT_ViewModel* theViewModel)
118 if (myViewModel && myViewModel != theViewModel) {
119 myViewModel->setViewManager(0);
122 myViewModel = theViewModel;
124 myViewModel->setViewManager(this);
127 /*!Sets view name for view window \a theView.*/
128 void SUIT_ViewManager::setViewName( SUIT_ViewWindow* theView )
130 QString title = prepareTitle( getTitle(), myId + 1, myViews.indexOf( theView ) + 1 );
131 theView->setWindowTitle( title );
134 QString SUIT_ViewManager::prepareTitle( const QString& title, const int mId, const int vId )
137 QRegExp re( "%[%MV]" );
139 while ( ( i = re.indexIn( res, i ) ) != -1 )
142 QString str = res.mid( i, re.matchedLength() );
143 if ( str == QString( "%%" ) )
144 rplc = QString( "%" );
145 else if ( str == QString( "%M" ) )
146 rplc = QString::number( mId );
147 else if ( str == QString( "%V" ) )
148 rplc = QString::number( vId );
149 res.replace( i, re.matchedLength(), rplc );
155 /*! Creates View, adds it into list of views and returns just created view window*/
156 SUIT_ViewWindow* SUIT_ViewManager::createViewWindow()
158 SUIT_ViewWindow* aView = myViewModel->createView(myDesktop);
160 if ( !insertView( aView ) ){
165 setViewName( aView );
166 aView->setWindowIcon( QIcon( myIcon ) );
168 //myDesktop->addViewWindow( aView );
169 //it is done automatically during creation of view
171 aView->setViewManager( this );
172 emit viewCreated( aView );
174 // Special treatment for the case when <aView> is the first one in this view manager
175 // -> call onWindowActivated() directly, because somebody may always want
176 // to use getActiveView()
178 onWindowActivated( aView );
183 /*! Get identifier */
184 int SUIT_ViewManager::getId() const
189 /*!Create view window.*/
190 void SUIT_ViewManager::createView()
195 QVector<SUIT_ViewWindow*> SUIT_ViewManager::getViews() const
197 QVector<SUIT_ViewWindow*> res;
198 for ( int i = 0; i < myViews.count(); i++ )
201 res.append( myViews[i] );
207 /*!Insert view window to view manager.
208 *\retval false - if something wrong, else true.
210 bool SUIT_ViewManager::insertView(SUIT_ViewWindow* theView)
212 unsigned int aSize = myViews.size();
213 unsigned int aNbItems = myViews.count() + 1;
214 if ( aNbItems > aSize )
216 myViews.resize( aNbItems );
217 aSize = myViews.size();
220 connect(theView, SIGNAL(closing(SUIT_ViewWindow*)),
221 this, SLOT(onClosingView(SUIT_ViewWindow*)));
223 connect(theView, SIGNAL(tryClosing(SUIT_ViewWindow*)),
224 this, SIGNAL(tryCloseView(SUIT_ViewWindow*)));
226 connect(theView, SIGNAL(mousePressed(SUIT_ViewWindow*, QMouseEvent*)),
227 this, SLOT(onMousePressed(SUIT_ViewWindow*, QMouseEvent*)));
229 connect(theView, SIGNAL(mouseReleased(SUIT_ViewWindow*, QMouseEvent*)),
230 this, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
232 connect(theView, SIGNAL(mouseDoubleClicked(SUIT_ViewWindow*, QMouseEvent*)),
233 this, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
235 connect(theView, SIGNAL(mouseMoving(SUIT_ViewWindow*, QMouseEvent*)),
236 this, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)));
238 connect(theView, SIGNAL(wheeling(SUIT_ViewWindow*, QWheelEvent*)),
239 this, SIGNAL(wheel(SUIT_ViewWindow*, QWheelEvent*)));
241 connect(theView, SIGNAL(keyPressed(SUIT_ViewWindow*, QKeyEvent*)),
242 this, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)));
244 connect(theView, SIGNAL(keyReleased(SUIT_ViewWindow*, QKeyEvent*)),
245 this, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)));
247 connect(theView, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
248 this, SLOT (onContextMenuRequested( QContextMenuEvent * )));
250 for ( uint i = 0; i < aSize; i++ )
252 if ( myViews[i] == 0 )
254 myViews[i] = theView;
261 /*!Emit delete view. Remove view window \a theView from view manager.
263 void SUIT_ViewManager::onClosingView( SUIT_ViewWindow* theView )
265 closeView( theView );
269 Remove the view window \a theView from view manager and destroy it.
271 void SUIT_ViewManager::closeView( SUIT_ViewWindow* theView )
276 QPointer<SUIT_ViewWindow> view( theView );
278 view->setClosable( false );
281 if ( !view->testAttribute( Qt::WA_DeleteOnClose ) )
284 emit deleteView( view );
291 /*!Remove view window \a theView from view manager.
292 *And close the last view, if it has \a theView.
294 void SUIT_ViewManager::removeView( SUIT_ViewWindow* theView )
296 theView->disconnect( this );
297 myViews.remove( myViews.indexOf( theView ) );
298 if ( myActiveView == theView )
300 if ( !myViews.count() )
301 emit lastViewClosed( this );
305 Set or clear flag Qt::WDestructiveClose for all views
307 void SUIT_ViewManager::setDestructiveClose( const bool on )
309 for ( int i = 0; i < myViews.count(); i++ )
310 myViews[i]->setDestructiveClose( on );
314 Returns 'true' if any of views (view windows) is visible.
316 bool SUIT_ViewManager::isVisible() const
319 for ( int i = 0; i < myViews.count() && !res; i++ )
320 res = myViews[i]->isVisibleTo( myViews[i]->parentWidget() );
325 Show or hide all views (view windows)
327 void SUIT_ViewManager::setShown( const bool on )
329 for ( int i = 0; i < myViews.count(); i++ )
330 myViews.at( i )->setShown( on );
333 /*!Emit on \a theEvent mouse pressed in \a theView.*/
334 void SUIT_ViewManager::onMousePressed(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
336 emit mousePress(theView, theEvent);
339 /*!Emit activated for view \a view.
341 void SUIT_ViewManager::onWindowActivated(SUIT_ViewWindow* view)
344 unsigned int aSize = myViews.size();
345 for (uint i = 0; i < aSize; i++) {
346 if (myViews[i] && myViews[i] == view) {
348 emit activated( this );
357 void SUIT_ViewManager::closeAllViews()
359 for ( int i = 0; i < myViews.size(); i++ )
365 *\retval QString - type of view model.
367 QString SUIT_ViewManager::getType() const
369 return (!myViewModel)? "": myViewModel->getType();
373 *\retval SUIT_Study* - current study.
375 SUIT_Study* SUIT_ViewManager::study() const
381 * Sets stydy to NULL.
383 void SUIT_ViewManager::onDeleteStudy()
388 /*! invoke method of SUIT_PopupClient, which notifies about popup*/
389 void SUIT_ViewManager::onContextMenuRequested( QContextMenuEvent* e )
391 contextMenuRequest( e );
394 /*!Context menu popup for \a popup.*/
395 void SUIT_ViewManager::contextMenuPopup( QMenu* popup )
397 SUIT_ViewModel* vm = getViewModel();
399 vm->contextMenuPopup( popup );