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