Salome HOME
017e8fada8a5cbd7139bd0b562a58c3fa2fb4842
[modules/gui.git] / src / Plot2d / Plot2d_ViewWindow.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 // Plot2d_ViewWindow.cxx: implementation of the Plot2d_ViewWindow class.
20 //
21 //////////////////////////////////////////////////////////////////////
22 #include "Plot2d_ViewWindow.h"
23 #include "Plot2d_ViewFrame.h"
24
25 #include "SUIT_ViewManager.h"
26 #include "SUIT_ResourceMgr.h"
27 #include "SUIT_Session.h"
28 #include "SUIT_ToolButton.h"
29 #include "SUIT_Desktop.h"
30
31 #include "QtxAction.h"
32
33 #include <qstatusbar.h>
34 #include <qlayout.h>
35 #include <qapplication.h>
36 #include <qpopupmenu.h>
37 #include <qimage.h>
38
39 //////////////////////////////////////////////////////////////////////
40 // Construction/Destruction
41 //////////////////////////////////////////////////////////////////////
42
43 Plot2d_ViewWindow::Plot2d_ViewWindow(SUIT_Desktop* theDesktop, Plot2d_Viewer* theModel)
44 : SUIT_ViewWindow(theDesktop)
45 {
46   myModel = theModel;
47
48   myViewFrame = new Plot2d_ViewFrame(this, "plotView");
49   setCentralWidget(myViewFrame);
50
51   myToolBar = new QToolBar(this);
52   myToolBar->setCloseMode(QDockWindow::Undocked);
53   myToolBar->setLabel(tr("LBL_TOOLBAR_LABEL"));
54   createActions();
55   createToolBar();
56
57   connect(myViewFrame, SIGNAL(vpModeHorChanged()), this, SLOT(onChangeHorMode()));
58   connect(myViewFrame, SIGNAL(vpModeVerChanged()), this, SLOT(onChangeVerMode()));
59   connect(myViewFrame, SIGNAL(vpCurveChanged()),   this, SLOT(onChangeCurveMode()));
60   connect(myViewFrame, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
61           this,        SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
62
63 }
64
65 Plot2d_ViewWindow::~Plot2d_ViewWindow()
66 {
67 }
68
69 //****************************************************************
70 void Plot2d_ViewWindow::putInfo(QString theMsg)
71 {
72   QStatusBar*   aStatusBar = myDesktop->statusBar();
73   aStatusBar->message(theMsg/*, 3000*/);
74 }
75
76 //****************************************************************
77 void Plot2d_ViewWindow::contextMenuPopup( QPopupMenu* thePopup )
78 {
79   // scaling
80   QPopupMenu* scalingPopup = new QPopupMenu( thePopup );
81   myActionsMap[ PModeXLinearId ]->addTo( scalingPopup );
82   myActionsMap[ PModeXLogarithmicId ]->addTo( scalingPopup );
83   onChangeHorMode();
84   scalingPopup->insertSeparator();
85   myActionsMap[ PModeYLinearId ]->addTo( scalingPopup );
86   myActionsMap[ PModeYLogarithmicId ]->addTo( scalingPopup );
87   thePopup->insertItem( tr( "SCALING_POPUP" ), scalingPopup );
88   onChangeVerMode();
89
90   thePopup->insertItem(tr("TOT_PLOT2D_FITDATA"), myViewFrame, SLOT(onFitData()));
91   // curve type
92   QPopupMenu* curTypePopup = new QPopupMenu( thePopup );
93   myActionsMap[ CurvPointsId ]->addTo( curTypePopup );
94   myActionsMap[ CurvLinesId ]->addTo( curTypePopup );
95   myActionsMap[ CurvSplinesId ]->addTo( curTypePopup );
96   thePopup->insertItem( tr( "CURVE_TYPE_POPUP" ), curTypePopup );
97
98   // legend
99   myActionsMap[ LegendId ]->addTo(thePopup);
100   // settings
101   myActionsMap[ CurvSettingsId ]->addTo(thePopup);
102 }
103
104 //****************************************************************
105 bool Plot2d_ViewWindow::eventFilter(QObject* watched, QEvent* e)
106 {
107   if (watched == myViewFrame) {
108     int aType = e->type();
109     switch(aType) {
110     case QEvent::MouseButtonPress:
111       emit mousePressed(this, (QMouseEvent*) e);
112       return true;
113
114     case QEvent::MouseButtonRelease:
115       emit mouseReleased(this, (QMouseEvent*) e);
116       return true;
117
118     case QEvent::MouseMove:
119       emit mouseMoving(this, (QMouseEvent*) e);
120       return true;
121
122     default:
123       break;
124     }
125   }
126   return SUIT_ViewWindow::eventFilter(watched, e);
127 }
128
129 //****************************************************************
130 void Plot2d_ViewWindow::createActions()
131 {
132   if ( !myActionsMap.isEmpty() )
133     return;
134
135   QtxAction* aAction;
136   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
137
138   // Dump view
139   aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "Plot2d", tr( "ICON_PLOT2D_DUMP" ) ),
140                            tr( "MNU_DUMP_VIEW" ), 0, this);
141   aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
142   connect(aAction, SIGNAL(activated()), this, SLOT(onDumpView()));
143   myActionsMap[ DumpId ] = aAction;
144
145   // FitAll
146   aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "Plot2d", tr( "ICON_PLOT2D_FIT_ALL" ) ),
147                            tr( "MNU_FITALL" ), 0, this);
148   aAction->setStatusTip(tr("DSC_FITALL"));
149   connect(aAction, SIGNAL(activated()), this, SLOT(onFitAll()));
150   myActionsMap[ FitAllId ] = aAction;
151
152   // FitRect
153   aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "Plot2d", tr( "ICON_PLOT2D_FIT_AREA" ) ),
154                            tr( "MNU_FITRECT" ), 0, this);
155   aAction->setStatusTip(tr("DSC_FITRECT"));
156   connect(aAction, SIGNAL(activated()), this, SLOT(onFitRect()));
157   myActionsMap[ FitRectId ] = aAction;
158
159   // Zoom
160   aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "Plot2d", tr( "ICON_PLOT2D_ZOOM" ) ),
161                            tr( "MNU_ZOOM_VIEW" ), 0, this);
162   aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
163   connect(aAction, SIGNAL(activated()), this, SLOT(onZoom()));
164   myActionsMap[ ZoomId ] = aAction;
165
166   // Panning
167   aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "Plot2d", tr( "ICON_PLOT2D_PAN" ) ),
168                            tr( "MNU_PAN_VIEW" ), 0, this);
169   aAction->setStatusTip(tr("DSC_PAN_VIEW"));
170   connect(aAction, SIGNAL(activated()), this, SLOT(onPanning()));
171   myActionsMap[ PanId ] = aAction;
172
173   // Global Panning
174   aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "Plot2d", tr( "ICON_PLOT2D_GLOBALPAN" ) ),
175                            tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
176   aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
177   connect(aAction, SIGNAL(activated()), this, SLOT(onGlobalPanning()));
178   myActionsMap[ GlobalPanId ] = aAction;
179
180   // Curve type - points
181   aAction = new QtxAction(tr("TOT_PLOT2D_CURVES_POINTS"),
182                 aResMgr->loadPixmap("Plot2d", tr("ICON_PLOT2D_CURVES_POINTS")),
183                 tr("MEN_PLOT2D_CURVES_POINTS"), 0, this);
184   aAction->setStatusTip(tr("PRP_PLOT2D_CURVES_POINTS"));
185   connect(aAction, SIGNAL(activated()), this, SLOT(onCurves()));
186   aAction->setToggleAction(true);
187   myActionsMap[ CurvPointsId ] = aAction;
188
189   // Curve type - lines
190   aAction = new QtxAction(tr("TOT_PLOT2D_CURVES_LINES"),
191                aResMgr->loadPixmap("Plot2d", tr("ICON_PLOT2D_CURVES_LINES")),
192                tr("MEN_PLOT2D_CURVES_LINES"), 0, this);
193   aAction->setStatusTip(tr("PRP_PLOT2D_CURVES_LINES"));
194   connect(aAction, SIGNAL(activated()), this, SLOT(onCurves()));
195   aAction->setToggleAction(true);
196   myActionsMap[ CurvLinesId ] = aAction;
197
198   // Curve type - splines
199   aAction = new QtxAction(tr("TOT_PLOT2D_CURVES_SPLINES"),
200                  aResMgr->loadPixmap("Plot2d", tr("ICON_PLOT2D_CURVES_SPLINES")),
201                  tr("MEN_PLOT2D_CURVES_SPLINES"), 0, this);
202   aAction->setStatusTip(tr("PRP_PLOT2D_CURVES_SPLINES"));
203   connect(aAction, SIGNAL(activated()), this, SLOT(onCurves()));
204   aAction->setToggleAction(true);
205   myActionsMap[ CurvSplinesId ] = aAction;
206
207   // Mode for X (linear or logarithmic)
208   aAction = new QtxAction(tr("TOT_PLOT2D_MODE_LINEAR_HOR"),
209                  aResMgr->loadPixmap("Plot2d", tr("ICON_PLOT2D_MODE_LINEAR_HOR")),
210                  tr("MEN_PLOT2D_MODE_LINEAR_HOR"), 0, this);
211   aAction->setStatusTip (tr("PRP_PLOT2D_MODE_LINEAR_HOR"));
212   connect(aAction, SIGNAL(activated()), this, SLOT(onViewHorMode()));
213   myActionsMap[ HorId ] = aAction;
214
215   // Mode for Y (linear or logarithmic)
216   aAction = new QtxAction(tr("TOT_PLOT2D_MODE_LINEAR_VER"),
217                  aResMgr->loadPixmap("Plot2d", tr("ICON_PLOT2D_MODE_LINEAR_VER")),
218                  tr("MEN_PLOT2D_MODE_LINEAR_VER" ), 0, this);
219   aAction->setStatusTip(tr("PRP_PLOT2D_MODE_LINEAR_VER"));
220   connect(aAction, SIGNAL(activated()), this, SLOT(onViewVerMode()));
221   myActionsMap[ VerId ] = aAction;
222
223   // Legend
224   aAction = new QtxAction(tr("TOT_PLOT2D_SHOW_LEGEND"),
225                 aResMgr->loadPixmap("Plot2d", tr("ICON_PLOT2D_SHOW_LEGEND")),
226                 tr("MEN_PLOT2D_SHOW_LEGEND"), 0, this);
227   aAction->setStatusTip(tr("PRP_PLOT2D_SHOW_LEGEND"));
228   connect(aAction, SIGNAL(activated()), this, SLOT(onLegend()));
229   aAction->setToggleAction(true);
230   myActionsMap[ LegendId ] = aAction;
231
232   // Settings
233   aAction = new QtxAction(tr( "TOT_PLOT2D_SETTINGS"),
234                 aResMgr->loadPixmap("Plot2d", tr("ICON_PLOT2D_SETTINGS")),
235                 tr("MEN_PLOT2D_SETTINGS"), 0, this);
236   aAction->setStatusTip(tr( "PRP_PLOT2D_SETTINGS"));
237   connect(aAction, SIGNAL(activated()), myViewFrame, SLOT(onSettings()));
238   myActionsMap[ CurvSettingsId ] = aAction;
239
240   // Clone
241   aAction = new QtxAction(tr("MNU_CLONE_VIEW"), aResMgr->loadPixmap( "Plot2d", tr( "ICON_PLOT2D_CLONE_VIEW" ) ),
242                            tr( "MNU_CLONE_VIEW" ), 0, this);
243   aAction->setStatusTip(tr("DSC_CLONE_VIEW"));
244   connect(aAction, SIGNAL(activated()), this, SIGNAL(cloneView()));
245   myActionsMap[ CloneId ] = aAction;
246
247   /* Popup Actions */
248   /* Linear/logarithmic mode */
249   // Horizontal axis
250   aAction = new QtxAction(tr("TOT_PLOT2D_MODE_LINEAR_HOR"),
251                  aResMgr->loadPixmap("Plot2d", tr("ICON_PLOT2D_MODE_LINEAR_HOR")),
252                  tr("MEN_PLOT2D_MODE_LINEAR_HOR"), 0, this);
253   aAction->setStatusTip (tr("PRP_PLOT2D_MODE_LINEAR_HOR"));
254   aAction->setToggleAction(true);
255   myActionsMap[PModeXLinearId] = aAction;
256   connect(aAction, SIGNAL(activated()), this, SLOT(onViewHorMode()));
257
258   aAction = new QtxAction(tr("TOT_PLOT2D_MODE_LOGARITHMIC_HOR"),
259               aResMgr->loadPixmap("Plot2d", tr("ICON_PLOT2D_MODE_LOGARITHMIC_HOR")),
260               tr("MEN_PLOT2D_MODE_LOGARITHMIC_HOR"), 0, this);
261   aAction->setStatusTip(tr("PRP_PLOT2D_MODE_LOGARITHMIC_HOR"));
262   aAction->setToggleAction(true);
263   myActionsMap[PModeXLogarithmicId] = aAction;
264   connect(aAction, SIGNAL(activated()), this, SLOT(onViewHorMode()));
265
266   // Vertical axis
267   aAction = new QtxAction(tr("TOT_PLOT2D_MODE_LINEAR_VER"),
268                  aResMgr->loadPixmap("Plot2d", tr("ICON_PLOT2D_MODE_LINEAR_VER")),
269                  tr("MEN_PLOT2D_MODE_LINEAR_VER" ), 0, this);
270   aAction->setStatusTip(tr("PRP_PLOT2D_MODE_LINEAR_VER"));
271   aAction->setToggleAction(true);
272   myActionsMap[PModeYLinearId] = aAction;
273   connect(aAction, SIGNAL(activated()), this, SLOT(onViewVerMode()));
274
275   aAction = new QtxAction(tr("TOT_PLOT2D_MODE_LOGARITHMIC_VER"),
276                  aResMgr->loadPixmap("Plot2d", tr("ICON_PLOT2D_MODE_LOGARITHMIC_VER")),
277                  tr("MEN_PLOT2D_MODE_LOGARITHMIC_VER" ), 0, this);
278   aAction->setStatusTip(tr("PRP_PLOT2D_MODE_LOGARITHMIC_VER"));
279   aAction->setToggleAction(true);
280   myActionsMap[PModeYLogarithmicId] = aAction;
281   connect(aAction, SIGNAL(activated()), this, SLOT(onViewVerMode()));
282
283 }
284
285 //****************************************************************
286 void Plot2d_ViewWindow::createToolBar()
287 {
288   myActionsMap[DumpId]->addTo(myToolBar);
289
290   SUIT_ToolButton* aScaleBtn = new SUIT_ToolButton(myToolBar);
291   aScaleBtn->AddAction(myActionsMap[FitAllId]);
292   aScaleBtn->AddAction(myActionsMap[FitRectId]);
293   aScaleBtn->AddAction(myActionsMap[ZoomId]);
294
295   SUIT_ToolButton* aPanBtn = new SUIT_ToolButton(myToolBar);
296   aPanBtn->AddAction(myActionsMap[PanId]);
297   aPanBtn->AddAction(myActionsMap[GlobalPanId]);
298
299   myCurveBtn = new SUIT_ToolButton(myToolBar);
300   myCurveBtn->AddAction(myActionsMap[CurvPointsId]);
301   myCurveBtn->AddAction(myActionsMap[CurvLinesId]);
302   myCurveBtn->AddAction(myActionsMap[CurvSplinesId]);
303   myActionsMap[CurvLinesId]->setOn(true);
304   onChangeCurveMode();
305
306   myActionsMap[HorId]->addTo(myToolBar);
307   onChangeHorMode();
308   myActionsMap[VerId]->addTo(myToolBar);
309   onChangeVerMode();
310
311   myActionsMap[LegendId]->addTo(myToolBar);
312   myActionsMap[CurvSettingsId]->addTo(myToolBar);
313   myActionsMap[CloneId]->addTo(myToolBar);
314   onChangeLegendMode();
315 }
316
317 //****************************************************************
318 void Plot2d_ViewWindow::onChangeHorMode()
319 {
320   bool aLinear = myViewFrame->isModeHorLinear();
321   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
322
323   myActionsMap[PModeXLinearId]->setOn( aLinear );
324   myActionsMap[PModeXLogarithmicId]->setOn( !aLinear );
325
326   QPixmap pix = aResMgr->loadPixmap( "Plot2d", tr( aLinear ? "ICON_PLOT2D_MODE_LOGARITHMIC_HOR" :
327                                                              "ICON_PLOT2D_MODE_LINEAR_HOR" ) );
328   myActionsMap[HorId]->setIconSet( pix );
329   myActionsMap[HorId]->setToolTip( tr( aLinear ? "TOT_PLOT2D_MODE_LOGARITHMIC_HOR" :
330                                                  "TOT_PLOT2D_MODE_LINEAR_HOR" ) );
331   myActionsMap[HorId]->setStatusTip( tr( aLinear ? "PRP_PLOT2D_MODE_LOGARITHMIC_HOR" :
332                                                    "PRP_PLOT2D_MODE_LINEAR_HOR" ) );
333
334   myActionsMap[GlobalPanId]->setEnabled( myViewFrame->isModeVerLinear() && myViewFrame->isModeHorLinear() );
335 }
336
337 //****************************************************************
338 void Plot2d_ViewWindow::onChangeVerMode()
339 {
340   bool aLinear = myViewFrame->isModeVerLinear();
341   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
342
343   myActionsMap[PModeYLinearId]->setOn( aLinear );
344   myActionsMap[PModeYLogarithmicId]->setOn( !aLinear );
345
346   QPixmap pix = aResMgr->loadPixmap( "Plot2d", tr( aLinear ? "ICON_PLOT2D_MODE_LOGARITHMIC_VER" :
347                                                              "ICON_PLOT2D_MODE_LINEAR_VER" ) );
348   myActionsMap[VerId]->setIconSet( pix );
349   myActionsMap[VerId]->setToolTip( tr( aLinear ? "TOT_PLOT2D_MODE_LOGARITHMIC_VER" :
350                                                  "TOT_PLOT2D_MODE_LINEAR_VER" ) );
351   myActionsMap[VerId]->setStatusTip( tr( aLinear ? "PRP_PLOT2D_MODE_LOGARITHMIC_VER" :
352                                                    "PRP_PLOT2D_MODE_LINEAR_VER" ) );
353
354   myActionsMap[GlobalPanId]->setEnabled( myViewFrame->isModeVerLinear() && myViewFrame->isModeHorLinear() );
355 }
356
357 //****************************************************************
358 void Plot2d_ViewWindow::onChangeCurveMode()
359 {
360   int aCurveType = myViewFrame->getCurveType();
361   myCurveBtn->SetItem(aCurveType);
362
363   myActionsMap[CurvPointsId]->setOn(aCurveType == 0);
364   myActionsMap[CurvLinesId]->setOn(aCurveType == 1);
365   myActionsMap[CurvSplinesId]->setOn(aCurveType == 2);
366 }
367
368 //****************************************************************
369 void Plot2d_ViewWindow::onChangeLegendMode()
370 {
371   myActionsMap[ LegendId ]->setOn(myViewFrame->isLegendShow());
372 }
373
374 //****************************************************************
375 void Plot2d_ViewWindow::onFitAll()
376 {
377   myViewFrame->onViewFitAll();
378 }
379
380 //****************************************************************
381 void Plot2d_ViewWindow::onFitRect()
382 {
383   myViewFrame->onViewFitArea();
384 }
385
386 //****************************************************************
387 void Plot2d_ViewWindow::onZoom()
388 {
389   myViewFrame->onViewZoom();
390 }
391
392 //****************************************************************
393 void Plot2d_ViewWindow::onPanning()
394 {
395   myViewFrame->onViewPan();
396 }
397
398 //****************************************************************
399 void Plot2d_ViewWindow::onGlobalPanning()
400 {
401   myViewFrame->onViewGlobalPan();
402 }
403
404 //****************************************************************
405 void Plot2d_ViewWindow::onViewHorMode()
406 {
407   if (myViewFrame->isModeHorLinear())
408     myViewFrame->setHorScaleMode(1);
409   else
410     myViewFrame->setHorScaleMode(0);
411 }
412
413 //****************************************************************
414 void Plot2d_ViewWindow::onViewVerMode()
415 {
416   if (myViewFrame->isModeVerLinear())
417     myViewFrame->setVerScaleMode(1);
418   else
419     myViewFrame->setVerScaleMode(0);
420 }
421
422 //****************************************************************
423 void Plot2d_ViewWindow::onLegend()
424 {
425   myViewFrame->showLegend(!myViewFrame->isLegendShow());
426   onChangeLegendMode();
427 }
428
429 //****************************************************************
430 void Plot2d_ViewWindow::onCurves()
431 {
432   QtxAction* aSender = (QtxAction*) sender();
433   if(aSender == myActionsMap[CurvPointsId])
434     myViewFrame->setCurveType(0);
435   else if(aSender == myActionsMap[CurvLinesId])
436     myViewFrame->setCurveType(1);
437   else if(aSender == myActionsMap[CurvSplinesId])
438     myViewFrame->setCurveType(2);
439 }
440  
441 //****************************************************************
442 void Plot2d_ViewWindow::onDumpView()
443 {
444   qApp->postEvent( myViewFrame, new QPaintEvent( QRect( 0, 0, myViewFrame->width(), myViewFrame->height() ), TRUE ) );
445   SUIT_ViewWindow::onDumpView();
446 }
447
448 //****************************************************************
449 QImage Plot2d_ViewWindow::dumpView()
450 {
451   QPixmap px = QPixmap::grabWindow( myViewFrame->winId() );
452   return px.convertToImage();
453 }
454
455 bool Plot2d_ViewWindow::dumpViewToFormat( const QString& fileName, const QString& format )
456 {
457   bool res = myViewFrame ? myViewFrame->print( fileName, format ) : false;
458   if( !res )
459     res = SUIT_ViewWindow::dumpViewToFormat( fileName, format );
460
461   return res;
462 }
463
464 QString Plot2d_ViewWindow::filter() const
465 {
466   return SUIT_ViewWindow::filter() + ";;" + tr( "POSTSCRIPT_FILES" );
467 }
468
469 /*! The method returns the visual parameters of this view as a formated string
470  */
471 QString Plot2d_ViewWindow::getVisualParameters()
472 {
473   return myViewFrame->getVisualParameters();
474 }
475
476 /* The method restors visual parameters of this view from a formated string
477  */
478 void Plot2d_ViewWindow::setVisualParameters( const QString& parameters )
479 {
480   myViewFrame->setVisualParameters( parameters );
481 }