1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
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, or (at your option) any later version.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GraphicsView.hxx"
21 #include "SchemaModel.hxx"
22 #include "SceneItem.hxx"
23 #include "SceneTextItem.hxx"
25 #include "QtGuiContext.hxx"
26 #include "guiObservers.hxx"
30 #include <QGraphicsView>
31 #include <QWheelEvent>
36 #include "YacsTrace.hxx"
39 using namespace YACS::HMI;
41 GraphicsView::GraphicsView(QWidget *parent)
42 : WrapGraphicsView(parent)
48 setTransformationAnchor(QGraphicsView::AnchorViewCenter);
51 GraphicsView::~GraphicsView()
55 void GraphicsView::onViewFitAll()
57 DEBTRACE("GraphicsView::onViewFitAll");
58 SubjectProc *sProc = QtGuiContext::getQtCurrent()->getSubjectProc();
59 SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[sProc];
60 //SceneProcItem *procItem = dynamic_cast<SceneProcItem*>(item);
61 fitInView(item->boundingRect(), Qt::KeepAspectRatio);
64 void GraphicsView::onViewFitArea()
66 DEBTRACE("GraphicsView::onViewFitArea");
67 Scene* myScene = dynamic_cast<Scene*>(scene());
68 myScene->setZoom(true);
72 void GraphicsView::onViewZoom()
74 DEBTRACE("GraphicsView::onViewZoom");
75 Scene* myScene = dynamic_cast<Scene*>(scene());
76 myScene->setZoom(true);
81 void GraphicsView::onViewPan()
83 DEBTRACE("GraphicsView::onViewPan");
84 Scene* myScene = dynamic_cast<Scene*>(scene());
85 myScene->setZoom(true);
87 //setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
88 setTransformationAnchor(QGraphicsView::NoAnchor);
91 void GraphicsView::onViewGlobalPan()
93 DEBTRACE("GraphicsView::onViewGlobalPan");
96 void GraphicsView::onViewReset()
98 DEBTRACE("GraphicsView::onViewReset");
103 void GraphicsView::contextMenuEvent(QContextMenuEvent *event)
105 QGraphicsItem *qgitem = itemAt(event->pos());
108 SceneItem *item = dynamic_cast<SceneItem*>(qgitem);
111 QPointF point = mapToScene(event->pos());
112 item->setEventPos(point);
113 item->popupMenu(this, event->globalPos());
117 SceneTextItem *item = dynamic_cast<SceneTextItem*>(qgitem);
120 QPointF point = mapToScene(event->pos());
121 item->setEventPos(point);
122 item->popupMenu(this, event->globalPos());
128 void GraphicsView::mouseMoveEvent(QMouseEvent *e)
130 WrapGraphicsView::mouseMoveEvent(e);
131 if (e->buttons()==Qt::LeftButton)
133 QPoint current = e->pos();
136 qreal currentX = e->globalX();
137 qreal currentY = e->globalY();
138 qreal delta = sqrt((currentX - _prevX)*(currentX - _prevX) + (currentY - _prevY)*(currentY - _prevY));
139 if ((currentX*currentX + currentY*currentY) < (_prevX*_prevX + _prevY*_prevY))
143 // if (delta < -30) delta = -30;
144 // if (delta > 30) delta = 30;
145 double deltax = delta/900.;
146 double zoom = exp(deltax);
147 _scale = _scale*zoom;
149 //DEBTRACE("move zooming " << delta << " " << deltax << " " << zoom);
153 translate(current.x() - _prevPos.x(), current.y() - _prevPos.y());
154 //DEBTRACE(current.x()<<"-"<<_prevPos.x()<<" "<<current.y()<<"-"<<_prevPos.y());
157 else if (_fittingArea)
161 _rect = scene()->addRect(QGraphicsView::mapToScene(QRect(_prevPos, current)).boundingRect());
162 _rect->setZValue(100000);
163 _rect->setParentItem(0);
167 _rect->setRect(QGraphicsView::mapToScene(QRect(_prevPos, current)).boundingRect());
174 void GraphicsView::mousePressEvent(QMouseEvent *e)
176 DEBTRACE("GraphicsView::mousePressEvent");
177 if (QtGuiContext::getQtCurrent()->getView() != this)
179 DEBTRACE("Switch context before selection");
180 QtGuiContext::getQtCurrent()->getGMain()->switchContext(this->parentWidget());
182 WrapGraphicsView::mousePressEvent(e);
185 _prevX = e->globalX();
186 _prevY = e->globalY();
188 else if (_fittingArea)
195 //setDragMode(QGraphicsView::ScrollHandDrag);
199 void GraphicsView::mouseReleaseEvent(QMouseEvent *e)
203 setDragMode(QGraphicsView::NoDrag);
204 setTransformationAnchor(QGraphicsView::AnchorViewCenter);
207 _fittingArea = false;
208 QPoint current = e->pos();
209 fitInView(QGraphicsView::mapToScene(QRect(_prevPos, current)).boundingRect(), Qt::KeepAspectRatio);
214 QTransform q = transform();
215 DEBTRACE(q.m11()<<" "<<q.m12()<<" "<<q.m21()<<" "<<q.m22()<<" "<<q.dx()<<" "<<q.dy());
216 WrapGraphicsView::mouseReleaseEvent(e);
219 void GraphicsView::wheelEvent(QWheelEvent *e)
221 DEBTRACE("GraphicsView::wheelEvent " << e->delta());
222 double zoom = exp(0.1*e->delta()/120);
223 _scale = _scale*zoom;
224 setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
228 void GraphicsView::onZoomToBloc()
230 Subject *sub = QtGuiContext::getQtCurrent()->getSelectedSubject();
231 SubjectNode *snode = dynamic_cast<SubjectNode*>(sub);
234 SubjectComposedNode *scnode = dynamic_cast<SubjectComposedNode*>(sub);
236 scnode = dynamic_cast<SubjectComposedNode*>(snode->getParent());
238 SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[scnode];
240 QRectF rect = item->mapToScene(item->boundingRect().toRect()).boundingRect();
241 fitInView(rect.toRect(), Qt::KeepAspectRatio);
244 void GraphicsView::onCenterOnNode()
246 Subject *sub =QtGuiContext::getQtCurrent()->getSelectedSubject();
247 SubjectNode *snode = dynamic_cast<SubjectNode*>(sub);
250 SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[sub];