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