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