Salome HOME
0022256: [CEA 866] Add item "Developers" in menu "Help"
[modules/gui.git] / src / QxGraph / QxGraph_ViewWindow.cxx
1 // Copyright (C) 2007-2013  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
23 //  SALOME QxGraph : build Supervisor viewer into desktop
24 //
25 #include "QxGraph_ViewWindow.h"
26 #include "QxGraph_Def.h"
27 #include "QxGraph_Canvas.h"
28 #include "QxGraph_CanvasView.h"
29
30 #include <SUIT_ResourceMgr.h>
31 #include <SUIT_Session.h>
32 #include <SUIT_ToolButton.h>
33
34 //QT Include
35 #include <qlayout.h>
36 #include <qcolor.h>
37
38 /*!
39     Constructor
40 */
41 QxGraph_ViewWindow::QxGraph_ViewWindow( SUIT_Desktop* theDesktop, QxGraph_Viewer* theModel)
42   : SUIT_ViewWindow( theDesktop )
43 {
44   printf("Construct QxGraph_ViewWindow\n");
45   myViewModel = theModel;
46 }
47
48 /*!
49   Initialization
50 */
51 void QxGraph_ViewWindow::initLayout()
52 {
53   initCanvas();
54   initCanvasViews();
55
56   myToolBar = new QToolBar(this);
57   myToolBar->setCloseMode(QDockWindow::Undocked);
58   myToolBar->setLabel(tr("LBL_TOOLBAR_LABEL"));
59   createActions();
60   createToolBar();
61 }
62
63 /*!
64   Canvas initialization
65 */
66 void QxGraph_ViewWindow::initCanvas()
67 {
68   if ( myViewModel )
69     myViewModel->setCanvas( new QxGraph_Canvas(resMgr()) );
70     //myViewModel->addCanvas( new QxGraph_Canvas(resMgr()) );
71 }
72
73 /*!
74   Canvas views initialization
75 */
76 void QxGraph_ViewWindow::initCanvasViews()
77 {
78   if ( myViewModel )
79   {
80     for (int i = 0; i < 2; i++)
81     {
82       //QxGraph_Canvas* aCanvas = new QxGraph_Canvas(resMgr());
83       //myViewModel->addCanvas( aCanvas );
84
85       QxGraph_CanvasView* aCanvasView = new QxGraph_CanvasView( myViewModel->getCanvas(), this );
86       //QxGraph_CanvasView* aCanvasView = new QxGraph_CanvasView( aCanvas, this );
87       myViewModel->addView(aCanvasView);
88
89       aCanvasView->hide();
90     }
91       
92     // the first view is shown and is the current
93     myViewModel->getCanvasViews().first()->show();
94     myViewModel->setCurrentView( myViewModel->getCanvasViews().first() );
95     
96     /* test for 1 view -->
97     QxGraph_CanvasView* aCanvasView = new QxGraph_CanvasView( myViewModel->getCanvas(), this );
98     myViewModel->addView(aCanvasView);
99     myViewModel->setCurrentView(aCanvasView);
100     test for 1 view <-- */
101
102     setCentralWidget( myViewModel->getCurrentView() );
103     
104     /*if ( inherits( "QMainWindow" ) ) {
105       printf("== > 1\n");
106       ( ( QMainWindow* )this )->setCentralWidget( myViewModel->getCurrentView() );
107       }
108       else {
109       printf("== > 2\n");
110       QBoxLayout* layout = new QVBoxLayout( this );
111       layout->addWidget( myViewModel->getCurrentView() );
112       }*/
113   }
114 }
115
116 /*!
117   Creates actions of QxGraph view window
118 */
119 void QxGraph_ViewWindow::createActions()
120 {
121   if (!myActionsMap.isEmpty()) return;
122   SUIT_ResourceMgr* aResMgr = resMgr();
123   QtxAction* aAction;
124
125   // FitAll
126   aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "QxGraph", tr( "ICON_QXGRAPH_FITALL" ) ),
127                           tr( "MNU_FITALL" ), 0, this);
128   aAction->setStatusTip(tr("DSC_FITALL"));
129   connect(aAction, SIGNAL(activated()), this, SLOT(onViewFitAll()));
130   myActionsMap[ FitAllId ] = aAction;
131
132   // FitRect
133   aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "QxGraph", tr( "ICON_QXGRAPH_FITAREA" ) ),
134                           tr( "MNU_FITRECT" ), 0, this);
135   aAction->setStatusTip(tr("DSC_FITRECT"));
136   connect(aAction, SIGNAL(activated()), this, SLOT(onViewFitArea()));
137   myActionsMap[ FitRectId ] = aAction;
138
139   // Zoom
140   aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "QxGraph", tr( "ICON_QXGRAPH_ZOOM" ) ),
141                           tr( "MNU_ZOOM_VIEW" ), 0, this);
142   aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
143   connect(aAction, SIGNAL(activated()), this, SLOT(onViewZoom()));
144   myActionsMap[ ZoomId ] = aAction;
145
146   // Panning
147   aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "QxGraph", tr( "ICON_QXGRAPH_PAN" ) ),
148                           tr( "MNU_PAN_VIEW" ), 0, this);
149   aAction->setStatusTip(tr("DSC_PAN_VIEW"));
150   connect(aAction, SIGNAL(activated()), this, SLOT(onViewPan()));
151   myActionsMap[ PanId ] = aAction;
152
153   // Global Panning
154   aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "QxGraph", tr( "ICON_QXGRAPH_GLOBALPAN" ) ),
155                           tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
156   aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
157   connect(aAction, SIGNAL(activated()), this, SLOT(onViewGlobalPan()));
158   myActionsMap[ GlobalPanId ] = aAction;
159
160   // Reset
161   aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "QxGraph", tr( "ICON_QXGRAPH_RESET" ) ),
162                           tr( "MNU_RESET_VIEW" ), 0, this);
163   aAction->setStatusTip(tr("DSC_RESET_VIEW"));
164   connect(aAction, SIGNAL(activated()), this, SLOT(onViewReset()));
165   myActionsMap[ ResetId ] = aAction;
166 }
167
168 /*!
169   Creates toolbar of QxGraph view window
170 */
171 void QxGraph_ViewWindow::createToolBar()
172 {
173   SUIT_ToolButton* aScaleBtn = new SUIT_ToolButton(myToolBar, "scale");
174   aScaleBtn->AddAction(myActionsMap[FitAllId]);
175   aScaleBtn->AddAction(myActionsMap[FitRectId]);
176   aScaleBtn->AddAction(myActionsMap[ZoomId]);
177
178   SUIT_ToolButton* aPanningBtn = new SUIT_ToolButton(myToolBar, "pan");
179   aPanningBtn->AddAction(myActionsMap[PanId]);
180   aPanningBtn->AddAction(myActionsMap[GlobalPanId]);
181
182   myActionsMap[ResetId]->addTo(myToolBar);
183 }
184
185 /*!
186   Destructor
187 */
188 QxGraph_ViewWindow::~QxGraph_ViewWindow() {}
189
190 /*!
191   Reset the active view
192 */
193 void QxGraph_ViewWindow::onViewReset()    
194 {
195   printf( "QxGraph_ViewWindow::onViewReset\n" );
196   if ( myViewModel && myViewModel->getCurrentView() )
197     myViewModel->getCurrentView()->activateReset();
198 }
199
200 /*!
201   Sets a new center of the active view
202 */
203 void QxGraph_ViewWindow::onViewGlobalPan()
204 {
205   printf( "QxGraph_ViewWindow::onViewGlobalPan\n" );
206   if ( myViewModel && myViewModel->getCurrentView() )
207     myViewModel->getCurrentView()->activateGlobalPanning();
208 }
209
210 /*!
211   Zooms the active view
212 */
213 void QxGraph_ViewWindow::onViewZoom()
214 {
215   printf( "QxGraph_ViewWindow::onViewZoom\n" );
216   if ( myViewModel && myViewModel->getCurrentView() )
217     myViewModel->getCurrentView()->activateZoom();
218 }
219
220 /*!
221   Moves the active view
222 */
223 void QxGraph_ViewWindow::onViewPan()
224 {
225   printf( "QxGraph_ViewWindow::onViewPan\n" );
226   if ( myViewModel && myViewModel->getCurrentView() )
227     myViewModel->getCurrentView()->activatePanning();
228 }
229
230 /*!
231   Fits all obejcts within a rectangular area of the active view
232 */
233 void QxGraph_ViewWindow::onViewFitArea()
234 {
235   printf( "QxGraph_ViewWindow::onViewFitArea\n" );
236   if ( myViewModel && myViewModel->getCurrentView() )
237     myViewModel->getCurrentView()->activateFitRect();
238 }
239
240 /*!
241   Fits all objects in the active view
242 */
243 void QxGraph_ViewWindow::onViewFitAll()
244 {
245   printf( "QxGraph_ViewWindow::onViewFitAll\n" );
246   if ( myViewModel && myViewModel->getCurrentView() )
247     myViewModel->getCurrentView()->activateFitAll();
248 }
249
250 /*!
251     Set background of the viewport
252 */
253 void QxGraph_ViewWindow::setBackgroundColor( const QColor& color )
254 {
255   if ( myViewModel && myViewModel->getCurrentView()) {
256     myViewModel->getCurrentView()->canvas()->setBackgroundColor(color);
257     myViewModel->getCurrentView()->setPaletteBackgroundColor(color.light(120));
258   }
259 }
260
261 /*!
262     Returns background of the viewport
263 */
264 QColor QxGraph_ViewWindow::backgroundColor() const
265 {
266   if ( myViewModel && myViewModel->getCurrentView())
267     return myViewModel->getCurrentView()->canvas()->backgroundColor();
268   return QColor();
269 }
270
271 /*!
272   Custom resize event handler
273 */
274 void QxGraph_ViewWindow::resizeEvent( QResizeEvent* theEvent )
275 {
276   //  QMainWindow::resizeEvent( theEvent );
277   //if ( myView ) myView->resizeView( theEvent );
278 }
279
280 /*!
281   Get resource manager
282 */
283 SUIT_ResourceMgr* QxGraph_ViewWindow::resMgr() const
284
285   return SUIT_Session::session()->resourceMgr(); 
286 }
287