1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "SUIT_ViewManager.h"
21 #include "SUIT_Desktop.h"
22 #include "SUIT_ViewModel.h"
23 #include "SUIT_Study.h"
27 #include <qmessagebox.h>
33 QMap<QString, int> SUIT_ViewManager::_ViewMgrId;
35 /*!\class SUIT_ViewManager.
36 * Class provide manipulation with view windows.
40 SUIT_ViewManager::SUIT_ViewManager( SUIT_Study* theStudy,
41 SUIT_Desktop* theDesktop,
42 SUIT_ViewModel* theViewModel )
44 myDesktop( theDesktop ),
45 myTitle( "Default: %M - viewer %V" ),
50 setViewModel( theViewModel );
52 myId = useNewId( getType() );
54 connect( theDesktop, SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
55 this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
59 connect( myStudy, SIGNAL( destroyed() ), this, SLOT( onDeleteStudy() ) );
63 SUIT_ViewManager::~SUIT_ViewManager()
67 myViewModel->setViewManager( 0 );
72 int SUIT_ViewManager::useNewId( const QString& type )
74 if ( !_ViewMgrId.contains( type ) )
75 _ViewMgrId.insert( type, 0 );
77 int id = _ViewMgrId[type];
82 void SUIT_ViewManager::setTitle( const QString& theTitle )
84 if ( myTitle == theTitle )
88 for ( uint i = 0; i < myViews.count(); i++ )
89 setViewName( myViews[i] );
92 /*!Sets view model \a theViewModel to view manager.*/
93 void SUIT_ViewManager::setViewModel(SUIT_ViewModel* theViewModel)
95 if (myViewModel && myViewModel != theViewModel) {
96 myViewModel->setViewManager(0);
99 myViewModel = theViewModel;
101 myViewModel->setViewManager(this);
104 /*!Sets view name for view window \a theView.*/
105 void SUIT_ViewManager::setViewName( SUIT_ViewWindow* theView )
107 QString title = prepareTitle( getTitle(), myId + 1, myViews.find( theView ) + 1 );
108 theView->setCaption( title );
111 QString SUIT_ViewManager::prepareTitle( const QString& title, const int mId, const int vId )
114 QRegExp re( "%[%MV]" );
116 while ( ( i = re.search( res, i ) ) != -1 )
119 QString str = res.mid( i, re.matchedLength() );
120 if ( str == QString( "%%" ) )
121 rplc = QString( "%" );
122 else if ( str == QString( "%M" ) )
123 rplc = QString::number( mId );
124 else if ( str == QString( "%V" ) )
125 rplc = QString::number( vId );
126 res.replace( i, re.matchedLength(), rplc );
132 /*! Creates View, adds it into list of views and returns just created view window*/
133 SUIT_ViewWindow* SUIT_ViewManager::createViewWindow()
135 SUIT_ViewWindow* aView = myViewModel->createView(myDesktop);
137 if ( !insertView( aView ) ){
142 setViewName( aView );
143 //myDesktop->addViewWindow( aView );
144 //it is done automatically during creation of view
146 aView->setViewManager(this);
148 emit viewCreated(aView);
150 // Special treatment for the case when <aView> is the first one in this view manager
151 // -> call onWindowActivated() directly, because somebody may always want
152 // to use getActiveView()
154 onWindowActivated( aView );
159 /*!Create view window.*/
160 void SUIT_ViewManager::createView()
165 /*!Insert view window to view manager.
166 *\retval false - if something wrong, else true.
168 bool SUIT_ViewManager::insertView(SUIT_ViewWindow* theView)
170 unsigned int aSize = myViews.size();
171 unsigned int aNbItems = myViews.count()+1;
172 if (aNbItems > aSize) {
173 if (!myViews.resize(aNbItems)) {
174 QMessageBox::critical(myDesktop, tr("Critical error"), tr("There is no memory for the new view!!!"));
177 aSize = myViews.size();
180 connect(theView, SIGNAL(closing(SUIT_ViewWindow*)),
181 this, SLOT(onClosingView(SUIT_ViewWindow*)));
183 connect(theView, SIGNAL(mousePressed(SUIT_ViewWindow*, QMouseEvent*)),
184 this, SLOT(onMousePressed(SUIT_ViewWindow*, QMouseEvent*)));
186 connect(theView, SIGNAL(mouseReleased(SUIT_ViewWindow*, QMouseEvent*)),
187 this, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
189 connect(theView, SIGNAL(mouseDoubleClicked(SUIT_ViewWindow*, QMouseEvent*)),
190 this, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
192 connect(theView, SIGNAL(mouseMoving(SUIT_ViewWindow*, QMouseEvent*)),
193 this, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)));
195 connect(theView, SIGNAL(wheeling(SUIT_ViewWindow*, QWheelEvent*)),
196 this, SIGNAL(wheel(SUIT_ViewWindow*, QWheelEvent*)));
198 connect(theView, SIGNAL(keyPressed(SUIT_ViewWindow*, QKeyEvent*)),
199 this, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)));
201 connect(theView, SIGNAL(keyReleased(SUIT_ViewWindow*, QKeyEvent*)),
202 this, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)));
204 connect(theView, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
205 this, SLOT (onContextMenuRequested( QContextMenuEvent * )));
207 for (uint i = 0; i < aSize; i++) {
209 myViews.insert(i, theView);
216 /*!Emit delete view. Remove view window \a theView from view manager.
218 void SUIT_ViewManager::onClosingView( SUIT_ViewWindow* theView )
220 closeView( theView );
224 Remove the view window \a theView from view manager and destroy it.
226 void SUIT_ViewManager::closeView( SUIT_ViewWindow* theView )
231 QGuardedPtr<SUIT_ViewWindow> view( theView );
235 if ( !view->testWFlags( WDestructiveClose ) )
238 emit deleteView( view );
245 /*!Remove view window \a theView from view manager.
246 *And close the last view, if it has \a theView.
248 void SUIT_ViewManager::removeView(SUIT_ViewWindow* theView)
250 theView->disconnect(this);
251 myViews.remove(myViews.find(theView));
252 if (myActiveView == theView)
254 int aNumItems = myViews.count();
256 emit lastViewClosed(this);
260 Set or clear flag Qt::WDestructiveClose for all views
262 void SUIT_ViewManager::setDestructiveClose( const bool on )
264 for ( uint i = 0; i < myViews.count(); i++ )
265 myViews[i]->setDestructiveClose( on );
269 Returns 'true' if any of views (view windows) is visible.
271 bool SUIT_ViewManager::isVisible() const
274 for ( uint i = 0; i < myViews.count() && !res; i++ )
275 res = myViews[i]->isVisibleTo( myViews[i]->parentWidget() );
280 Show or hide all views (view windows)
282 void SUIT_ViewManager::setShown( const bool on )
284 for ( uint i = 0; i < myViews.count(); i++ )
285 myViews.at( i )->setShown( on );
288 /*!Emit on \a theEvent mouse pressed in \a theView.*/
289 void SUIT_ViewManager::onMousePressed(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
291 emit mousePress(theView, theEvent);
294 /*!Emit activated for view \a view.
296 void SUIT_ViewManager::onWindowActivated(SUIT_ViewWindow* view)
299 unsigned int aSize = myViews.size();
300 for (uint i = 0; i < aSize; i++) {
301 if (myViews[i] && myViews[i] == view) {
303 emit activated( this );
312 void SUIT_ViewManager::closeAllViews()
314 for ( uint i = 0; i < myViews.size(); i++ )
320 *\retval QString - type of view model.
322 QString SUIT_ViewManager::getType() const
324 return (!myViewModel)? "": myViewModel->getType();
328 *\retval SUIT_Study* - current study.
330 SUIT_Study* SUIT_ViewManager::study() const
336 * Sets stydy to NULL.
338 void SUIT_ViewManager::onDeleteStudy()
343 /*! invoke method of SUIT_PopupClient, which notifies about popup*/
344 void SUIT_ViewManager::onContextMenuRequested( QContextMenuEvent* e )
346 contextMenuRequest( e );
349 /*!Context menu popup for \a popup.*/
350 void SUIT_ViewManager::contextMenuPopup( QPopupMenu* popup )
352 SUIT_ViewModel* vm = getViewModel();
354 vm->contextMenuPopup( popup );