Salome HOME
1d5b37c3b398acad3d94fb987ca5dac515ec6b09
[modules/yacs.git] / src / genericgui / GraphicsView.cxx
1 //  Copyright (C) 2006-2008  CEA/DEN, EDF 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/ or email : webmaster.salome@opencascade.com
18 //
19 #include "GraphicsView.hxx"
20 #include "SchemaModel.hxx"
21 #include "SceneItem.hxx"
22 #include "Scene.hxx"
23 #include "QtGuiContext.hxx"
24
25 #include <cmath>
26 #include <QMenu>
27 #include <QGraphicsView>
28
29 //#define _DEVDEBUG_
30 #include "YacsTrace.hxx"
31
32 using namespace std;
33 using namespace YACS::HMI;
34
35 GraphicsView::GraphicsView(QWidget *parent)
36   : WrapGraphicsView(parent)
37 {
38   _zooming = false;
39   _fittingArea = false;
40   _panning = false;
41   setTransformationAnchor(QGraphicsView::AnchorViewCenter);
42 }
43
44 GraphicsView::~GraphicsView()
45 {
46 }
47
48 void GraphicsView::onViewFitAll()
49 {
50   DEBTRACE("GraphicsView::onViewFitAll");
51   SubjectProc *sProc = QtGuiContext::getQtCurrent()->getSubjectProc();
52   SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[sProc];
53   //SceneProcItem *procItem = dynamic_cast<SceneProcItem*>(item);
54   fitInView(item->boundingRect(),  Qt::KeepAspectRatio);
55 }
56
57 void GraphicsView::onViewFitArea()
58 {
59   DEBTRACE("GraphicsView::onViewFitArea");
60   Scene* myScene = dynamic_cast<Scene*>(scene());
61   myScene->setZoom(true);
62   _fittingArea = true;
63 }
64
65 void GraphicsView::onViewZoom()
66 {
67   DEBTRACE("GraphicsView::onViewZoom");
68   Scene* myScene = dynamic_cast<Scene*>(scene());
69   myScene->setZoom(true);
70   _zooming =true;
71   _scale = 1;
72 }
73
74 void GraphicsView::onViewPan()
75 {
76   DEBTRACE("GraphicsView::onViewPan");
77   Scene* myScene = dynamic_cast<Scene*>(scene());
78   myScene->setZoom(true);
79   _panning = true;
80   //setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
81   setTransformationAnchor(QGraphicsView::NoAnchor);
82 }
83
84 void GraphicsView::onViewGlobalPan()
85 {
86   DEBTRACE("GraphicsView::onViewGlobalPan");
87 }
88
89 void GraphicsView::onViewReset()
90 {
91   DEBTRACE("GraphicsView::onViewReset");
92   resetTransform();
93 }
94
95
96 void GraphicsView::contextMenuEvent(QContextMenuEvent *event)
97 {
98   QGraphicsItem *qgitem = itemAt(event->pos());
99   if (qgitem)
100     {
101       AbstractSceneItem *item = dynamic_cast<AbstractSceneItem*>(qgitem);
102       if (item)
103         item->popupMenu(this, event->globalPos());
104     }
105 }
106
107 void GraphicsView::mouseMoveEvent(QMouseEvent *e)
108 {
109   WrapGraphicsView::mouseMoveEvent(e);
110   if (e->buttons()==Qt::LeftButton)
111     {
112       if (_zooming)
113         {
114           qreal currentX = e->globalX();
115           qreal delta = currentX - _prevX;
116           _prevX = currentX;
117           //       if (delta < -30) delta = -30;
118           //       if (delta >  30) delta =  30;
119           double deltax = delta/900.;
120           double zoom = exp(deltax);
121           _scale = _scale*zoom;
122           scale(zoom,zoom);
123           //DEBTRACE("move zooming " << delta << " " << deltax << " " << zoom);
124         }
125       else if (_panning)
126         {
127           QPoint current = e->pos();
128           translate(current.x() - _prevPos.x(), current.y() - _prevPos.y());
129           //DEBTRACE(current.x()<<"-"<<_prevPos.x()<<" "<<current.y()<<"-"<<_prevPos.y());
130           _prevPos = current;
131         }
132     }
133 }
134
135 void GraphicsView::mousePressEvent(QMouseEvent *e)
136 {
137   WrapGraphicsView::mousePressEvent(e);
138   if (_zooming)
139     {
140       _prevX = e->globalX();
141     }
142   else if (_fittingArea)
143     {
144       _prevPos = e->pos();
145     }
146     else if (_panning)
147       {
148         _prevPos = e->pos();
149         //setDragMode(QGraphicsView::ScrollHandDrag);
150       }
151 }
152
153 void GraphicsView::mouseReleaseEvent(QMouseEvent *e)
154 {
155   _zooming = false;
156   _panning = false;
157   setDragMode(QGraphicsView::NoDrag);
158   setTransformationAnchor(QGraphicsView::AnchorViewCenter);
159   if( _fittingArea)
160     {
161       _fittingArea = false;
162       QPoint current = e->pos();
163       fitInView(QRect(_prevPos, current),  Qt::KeepAspectRatio);
164     }
165
166   QTransform q = transform();
167   DEBTRACE(q.m11()<<" "<<q.m12()<<" "<<q.m21()<<" "<<q.m22()<<" "<<q.dx()<<" "<<q.dy());
168   WrapGraphicsView::mouseReleaseEvent(e);
169 }