Salome HOME
Copyrights update
[modules/gui.git] / src / SUIT / SUIT_ViewManager.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "SUIT_ViewManager.h"
20
21 #include "SUIT_Desktop.h"
22 #include "SUIT_ViewModel.h"
23 #include "SUIT_Study.h"
24
25 #include <qcursor.h>
26 #include <qmessagebox.h>
27
28 #ifdef WNT
29 #include <windows.h>
30 #endif
31
32 /*!\class SUIT_ViewManager.
33  * Class provide manipulation with view windows.
34  */
35
36 /*!Constructor.*/
37 SUIT_ViewManager::SUIT_ViewManager( SUIT_Study* theStudy,
38                                     SUIT_Desktop* theDesktop,
39                                     SUIT_ViewModel* theViewModel )
40 : QObject( 0 ),
41 myDesktop( theDesktop ),
42 myTitle( "Default viewer" ),
43 myStudy( NULL )
44 {
45   myViewModel = 0;
46   myActiveView = 0;
47   setViewModel(theViewModel);
48   connect(theDesktop, SIGNAL(windowActivated(SUIT_ViewWindow*)), 
49           this,       SLOT(onWindowActivated(SUIT_ViewWindow*)));
50
51   myStudy = theStudy;
52   if( myStudy )
53     connect( myStudy, SIGNAL( destroyed() ), this, SLOT( onDeleteStudy() ) );
54 }
55
56 /*!Destructor.*/
57 SUIT_ViewManager::~SUIT_ViewManager()
58 {
59   if ( myViewModel )
60   {
61     myViewModel->setViewManager( 0 );
62     delete myViewModel;
63   }
64 }
65
66 /*!Sets view model \a theViewModel to view manager.*/
67 void SUIT_ViewManager::setViewModel(SUIT_ViewModel* theViewModel) 
68 {
69   if (myViewModel && myViewModel != theViewModel) {
70     myViewModel->setViewManager(0);
71     delete myViewModel;
72   }
73   myViewModel = theViewModel;
74   if (myViewModel)
75     myViewModel->setViewManager(this);
76 }
77
78 /*!Sets view name for view window \a theView.*/
79 void SUIT_ViewManager::setViewName(SUIT_ViewWindow* theView)
80 {
81   int aPos = myViews.find(theView);
82   theView->setCaption(myTitle + QString(":%1").arg(aPos+1));
83 }
84
85 /*! Creates View, adds it into list of views and returns just created view window*/
86 SUIT_ViewWindow* SUIT_ViewManager::createViewWindow()
87 {
88   SUIT_ViewWindow* aView = myViewModel->createView(myDesktop);
89
90   if ( !insertView( aView ) ){
91     delete aView;
92     return 0;
93   }
94   
95   setViewName( aView );
96   //myDesktop->addViewWindow( aView );
97   //it is done automatically during creation of view
98
99   aView->setViewManager(this);
100
101   emit viewCreated(aView);
102
103   // Special treatment for the case when <aView> is the first one in this view manager
104   // -> call onWindowActivated() directly, because somebody may always want
105   // to use getActiveView()
106   if ( !myActiveView )
107     onWindowActivated( aView );
108
109   return aView;
110 }
111
112 /*!Create view window.*/
113 void SUIT_ViewManager::createView()
114 {
115   createViewWindow();
116 }
117
118 /*!Insert view window to view manager.
119  *\retval false - if something wrong, else true.
120  */
121 bool SUIT_ViewManager::insertView(SUIT_ViewWindow* theView)
122 {
123   unsigned int aSize = myViews.size();
124   unsigned int aNbItems = myViews.count()+1;
125   if (aNbItems > aSize) {
126     if (!myViews.resize(aNbItems)) {
127       QMessageBox::critical(myDesktop, tr("Critical error"), tr("There is no memory for the new view!!!"));
128       return false;
129     }
130     aSize = myViews.size();
131   }
132   
133   connect(theView, SIGNAL(closing(SUIT_ViewWindow*)),
134           this,    SLOT(onDeleteView(SUIT_ViewWindow*)));
135
136   connect(theView, SIGNAL(mousePressed(SUIT_ViewWindow*, QMouseEvent*)),
137           this,    SLOT(onMousePressed(SUIT_ViewWindow*, QMouseEvent*)));
138
139   connect(theView, SIGNAL(mouseReleased(SUIT_ViewWindow*, QMouseEvent*)),
140           this,    SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
141
142   connect(theView, SIGNAL(mouseDoubleClicked(SUIT_ViewWindow*, QMouseEvent*)),
143           this,    SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
144
145   connect(theView, SIGNAL(mouseMoving(SUIT_ViewWindow*, QMouseEvent*)),
146           this,    SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)));
147
148   connect(theView, SIGNAL(wheeling(SUIT_ViewWindow*, QWheelEvent*)),
149           this,    SIGNAL(wheel(SUIT_ViewWindow*, QWheelEvent*)));
150
151   connect(theView, SIGNAL(keyPressed(SUIT_ViewWindow*, QKeyEvent*)),
152           this,    SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)));
153
154   connect(theView, SIGNAL(keyReleased(SUIT_ViewWindow*, QKeyEvent*)),
155           this,    SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)));
156
157   connect(theView, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
158           this,    SLOT  (onContextMenuRequested( QContextMenuEvent * )));
159
160   for (uint i = 0; i < aSize; i++) {
161     if (myViews[i]==0) {
162       myViews.insert(i, theView);
163       return true;
164     }
165   }
166   return false;
167 }
168
169 /*!Emit delete view. Remove view window \a theView from view manager.
170 */
171 void SUIT_ViewManager::onDeleteView(SUIT_ViewWindow* theView)
172 {
173   emit deleteView(theView);
174   removeView(theView);
175 }
176
177 /*!Remove view window \a theView from view manager.
178  *And close the last view, if it has \a theView.
179 */
180 void SUIT_ViewManager::removeView(SUIT_ViewWindow* theView) 
181 {
182   theView->disconnect(this);
183   myViews.remove(myViews.find(theView));
184   if (myActiveView == theView)
185     myActiveView = 0;
186   int aNumItems = myViews.count();
187   if (aNumItems == 0)
188     emit lastViewClosed(this);
189 }
190
191 /*!
192   Show or hide all views (view windows)
193 */
194 void SUIT_ViewManager::setShown( const bool on )
195 {
196   for ( uint i = 0; i < myViews.count(); i++ )
197     myViews.at( i )->setShown( on );
198 }
199
200 /*!Emit on \a theEvent mouse pressed in \a theView.*/
201 void SUIT_ViewManager::onMousePressed(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
202 {
203   emit mousePress(theView, theEvent);
204 }
205
206 /*!Emit activated for view \a view.
207 */
208 void SUIT_ViewManager::onWindowActivated(SUIT_ViewWindow* view)
209 {
210   if (view) {
211     unsigned int aSize = myViews.size();
212     for (uint i = 0; i < aSize; i++) {
213       if (myViews[i] && myViews[i] == view) {
214         myActiveView = view;
215         emit activated( this );
216         return;
217       }
218     }
219   }
220 }
221
222 /*!Close all views.
223 */
224 void SUIT_ViewManager::closeAllViews()
225 {
226   unsigned int aSize = myViews.size();
227   for (uint i = 0; i < aSize; i++) {
228     if (myViews[i])
229       myViews[i]->close();
230   }
231 }
232
233 /*!
234  *\retval QString - type of view model.
235  */
236 QString  SUIT_ViewManager::getType() const 
237
238   return (!myViewModel)? "": myViewModel->getType(); 
239 }
240
241 /*!
242  *\retval SUIT_Study* - current study.
243  */
244 SUIT_Study* SUIT_ViewManager::study() const
245 {
246     return myStudy;
247 }
248
249 /*!
250  * Sets stydy to NULL.
251  */
252 void SUIT_ViewManager::onDeleteStudy()
253 {
254     myStudy = NULL;
255 }
256
257 void SUIT_ViewManager::onContextMenuRequested( QContextMenuEvent* e )
258 {
259   /*! invoke method of SUIT_PopupClient, which notifies about popup*/
260   contextMenuRequest( e );
261 }
262
263 /*!Context menu popup for \a popup.*/
264 void SUIT_ViewManager::contextMenuPopup( QPopupMenu* popup )
265 {
266   SUIT_ViewModel* vm = getViewModel();
267   if ( vm )
268     vm->contextMenuPopup( popup );
269 }