Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / Plot2d / Plot2d_ViewModel.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // Plot2d_ViewModel.cxx: implementation of the Plot2d_ViewModel class.
23 //
24 #include "Plot2d_ViewModel.h"
25 #include "Plot2d_ViewWindow.h"
26 #include "Plot2d_ViewManager.h"
27 #include "Plot2d_ViewFrame.h"
28 #include "Plot2d_Prs.h"
29
30 #include <QMenu>
31 #include <QToolBar>
32 #include <QVector>
33
34 /*!
35   Constructor
36 */
37 Plot2d_Viewer::Plot2d_Viewer(bool theAutoDel)
38 :SUIT_ViewModel() 
39 {
40   myPrs = 0;
41   myAutoDel = theAutoDel;
42 }
43
44 /*!
45   Destructor
46 */
47 Plot2d_Viewer::~Plot2d_Viewer()
48 {
49   if (myPrs)
50     clearPrs();
51 }
52
53 /*!
54   Create new instance of view window on desktop \a theDesktop.
55   \retval SUIT_ViewWindow* - created view window pointer.
56 */
57 SUIT_ViewWindow* Plot2d_Viewer::createView(SUIT_Desktop* theDesktop)
58 {
59   Plot2d_ViewWindow* aPlot2dView = new Plot2d_ViewWindow(theDesktop, this);
60   if (myPrs)
61     aPlot2dView->getViewFrame()->Display(myPrs);
62   return aPlot2dView;
63 }
64
65 /*!
66   Adds custom items to popup menu
67   \param thePopup - popup menu
68 */
69 void Plot2d_Viewer::contextMenuPopup(QMenu* thePopup)
70 {
71   Plot2d_ViewWindow* aView = (Plot2d_ViewWindow*)(myViewManager->getActiveView());
72   if ( aView )
73     aView->contextMenuPopup(thePopup);
74
75   if (!thePopup->isEmpty())
76     thePopup->addSeparator();
77   thePopup->addAction( tr( "MNU_DUMP_VIEW" ),                this, SLOT(onDumpView()));
78   thePopup->addAction( tr( "MEN_PLOT2D_CHANGE_BACKGROUND" ), this, SLOT(onChangeBgColor()));
79
80   if ( aView ) {
81     if ( !aView->getToolBar()->isVisible() ) {
82       if (!thePopup->isEmpty())
83         thePopup->addSeparator();
84       thePopup->addAction("Show toolbar", this, SLOT(onShowToolbar()));
85     }
86     aView->RefreshDumpImage();
87   }
88 }
89
90 /*!
91   Sets presentation of viewer
92   \param thePrs - new presentation
93 */
94 void Plot2d_Viewer::setPrs(Plot2d_Prs* thePrs) 
95 {
96   if (myPrs)
97     clearPrs();
98   myPrs = thePrs;
99   myPrs->setAutoDel(myAutoDel);
100 }
101
102 /*!
103   Updates current viewer
104 */
105 void Plot2d_Viewer::update()
106 {
107   SUIT_ViewManager* aMgr = getViewManager();
108   QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
109   unsigned int aSize = aViews.size();
110   for (uint i = 0; i < aSize; i++) {
111     Plot2d_ViewWindow* aView = (Plot2d_ViewWindow*)aViews[i];
112     if (myPrs && aView)
113       aView->getViewFrame()->Display(myPrs);
114   }
115 }
116
117 /*!
118   Clear viewer presentation
119 */
120 void Plot2d_Viewer::clearPrs()
121 {
122   SUIT_ViewManager* aMgr = getViewManager();
123   QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
124   unsigned int aSize = aViews.size();
125   for (uint i = 0; i < aSize; i++) {
126     Plot2d_ViewWindow* aView = (Plot2d_ViewWindow*)aViews[i];
127     if (myPrs && aView)
128       aView->getViewFrame()->Erase(myPrs);
129   }
130   if (myAutoDel && myPrs) {
131     delete myPrs;
132   }
133   myPrs = 0;
134 }
135
136 /*!
137   Sets "auto delete" state of of presentation
138   \param theDel - new state
139 */
140 void Plot2d_Viewer::setAutoDel(bool theDel)
141 {
142   myAutoDel = theDel;
143   if (myPrs)
144     myPrs->setAutoDel(theDel);
145 }
146
147 /*!
148   SLOT: called when action "Change background" is activated
149 */
150 void Plot2d_Viewer::onChangeBgColor()
151 {
152   Plot2d_ViewWindow* aView = (Plot2d_ViewWindow*)(myViewManager->getActiveView());
153   if( !aView )
154     return;
155   Plot2d_ViewFrame* aViewFrame = aView->getViewFrame();
156   aViewFrame->onChangeBackground();
157 }
158
159 /*!
160   SLOT: called when action "Show toolbar" is activated
161 */
162 void Plot2d_Viewer::onShowToolbar() {
163   Plot2d_ViewWindow* aView = (Plot2d_ViewWindow*)(myViewManager->getActiveView());
164   if ( aView )
165     aView->getToolBar()->show();    
166 }
167
168 /*!
169   SLOT: called when action "Dump view" is activated
170 */
171 void Plot2d_Viewer::onDumpView()
172 {
173   Plot2d_ViewWindow* aView = (Plot2d_ViewWindow*)(myViewManager->getActiveView());
174   if ( aView )
175     aView->onDumpView();    
176 }
177
178 /*!
179   SLOT: called when action "Clone view" is activated
180 */
181 void Plot2d_Viewer::onCloneView( Plot2d_ViewFrame* clonedVF, Plot2d_ViewFrame* newVF )
182 {
183   if( !clonedVF || !newVF )
184     return;
185
186   // 1) Copy all properties of view
187
188   newVF->copyPreferences( clonedVF );
189
190   // 2) Display all curves displayed in cloned view
191
192   curveList aCurves;
193   clonedVF->getCurves( aCurves );
194   curveList::const_iterator anIt = aCurves.begin(), aLast = aCurves.end();
195
196   for( ; anIt!=aLast; anIt++ )
197     if( clonedVF->isVisible( *anIt ) )
198       newVF->displayCurve( *anIt, false );
199   newVF->Repaint();
200   
201   if ( newVF )
202   {
203     // find view window corresponding to the frame 
204     QWidget* p = newVF->parentWidget();
205     while( p && !p->inherits( "SUIT_ViewWindow" ) )
206       p = p->parentWidget();
207     
208     // emits signal
209     if ( p && p->inherits( "SUIT_ViewWindow" ) )
210       emit viewCloned( (SUIT_ViewWindow*)p );
211   }
212 }
213
214 /*
215   SLOT: called when clicked item in the legend from Plot2d_ViewManager
216  */
217 void Plot2d_Viewer::onLegendClicked( QwtPlotItem* plotItem )
218 {
219 }
220
221 /*!
222   Sets view manager
223   \param mgr - new view manager
224 */
225 void Plot2d_Viewer::setViewManager( SUIT_ViewManager* mgr )
226 {
227   SUIT_ViewModel::setViewManager( mgr );
228   if( mgr && mgr->inherits( "Plot2d_ViewManager" ) )
229   {
230     Plot2d_ViewManager* pmgr = ( Plot2d_ViewManager* )mgr;
231     connect( pmgr, SIGNAL( cloneView( Plot2d_ViewFrame*, Plot2d_ViewFrame* ) ),
232              this, SLOT( onCloneView( Plot2d_ViewFrame*, Plot2d_ViewFrame* ) ) );
233   }
234 }