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