Version: 8.3.0
SceneCtrlPortItem.cxx
Go to the documentation of this file.
1 // Copyright (C) 2006-2016 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, or (at your option) any later version.
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 
20 #include "SceneCtrlPortItem.hxx"
21 #include "SceneTextItem.hxx"
22 #include "SceneNodeItem.hxx"
23 #include "SceneHeaderNodeItem.hxx"
24 #include "Scene.hxx"
25 #include "ItemMimeData.hxx"
26 #include "QtGuiContext.hxx"
27 
28 // #include "QtGuiContext.hxx"
29 #include "Menus.hxx"
30 #include <QGraphicsSceneMouseEvent>
31 #include <QApplication>
32 #include <QDrag>
33 #include <QPixmap>
34 #include <QBitmap>
35 // #include <QGraphicsSceneHoverEvent>
36 // #include <QPointF>
37 
38 // #include <cassert>
39 
40 #include "Resource.hxx"
41 
42 //#define _DEVDEBUG_
43 #include "YacsTrace.hxx"
44 
45 using namespace std;
46 using namespace YACS::ENGINE;
47 using namespace YACS::HMI;
48 
49 SceneCtrlPortItem::SceneCtrlPortItem(QGraphicsScene *scene, SceneItem *parent,
50  QString label)
51  : SceneItem(scene, parent, label), ScenePortItem(label)
52 {
53  setText(label);
60 }
61 
63 {
64 }
65 
66 void SceneCtrlPortItem::paint(QPainter *painter,
67  const QStyleOptionGraphicsItem *option,
68  QWidget *widget)
69 {
70  //DEBTRACE("ScenePortItem::paint");
71  painter->save();
72 
73  QPen pen(getPenColor());
74  pen.setWidth(Resource::Thickness);
75  painter->setPen(pen);
76  SceneHeaderNodeItem* hd = dynamic_cast<SceneHeaderNodeItem*>(_parent);
77  painter->setBrush(hd->getValidColor());
79 
80  painter->restore();
81 }
82 
83 void SceneCtrlPortItem::setText(QString label)
84 {
85  if (!_text)
87  this,
88  label, true );
89 }
90 
92 {
93  if (_parent)
94  return dynamic_cast<SceneNodeItem*>(_parent->getParent());
95  else
96  return 0;
97 }
98 
100 {
101  SceneNodeItem* nodeItem = getParentNode();
102  if (!nodeItem) return 0;
103  return dynamic_cast<SubjectNode*>(nodeItem->getSubject());
104 }
105 
107 {
108  return "yacs/subjectGate";
109 }
110 
111 void SceneCtrlPortItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
112 {
113  DEBTRACE("SceneCtrlPortItem::mousePressEvent " << _label.toStdString()
114  << " " << acceptedMouseButtons ());
115  if (!_scene->isZooming())
116  {
117  getSubjectNode()->select(true);
118  if (_dragable && (event->button() == _dragButton) && QtGuiContext::getQtCurrent()->isEdition())
119  {
120  setCursor(Qt::ClosedHandCursor);
121  _draging = true;
122  _dragModifier= event->modifiers() & Qt::ControlModifier;
123  }
124  }
125 }
126 
132 void SceneCtrlPortItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
133 {
134  if (_draging)
135  {
136  //DEBTRACE("_draging");
137  if (QLineF(event->screenPos(),
138  event->buttonDownScreenPos(_dragButton)).length()
139  < QApplication::startDragDistance())
140  {
141  return;
142  }
143  DEBTRACE("in drag");
144  QDrag *drag = new QDrag(event->widget());
145  ItemMimeData *mime = new ItemMimeData;
146  drag->setMimeData(mime);
147  mime->setSubject(getSubjectNode());
148  mime->setData(getMimeFormat(), "_subject");
149 
150  QPixmap pixmap(34, 34);
151  pixmap.fill(Qt::white);
152 
153  QPainter painter(&pixmap);
154  painter.translate(15, 15);
155  painter.setRenderHint(QPainter::Antialiasing);
156  paint(&painter, 0, 0);
157  painter.end();
158 
159  pixmap.setMask(pixmap.createHeuristicMask());
160 
161  drag->setPixmap(pixmap);
162  drag->setHotSpot(QPoint(15, 20));
163 
164  drag->exec();
165  setCursor(Qt::ArrowCursor);
166  _draging = false;
167  _dragModifier= false;
168  }
169 }
170 
171 void SceneCtrlPortItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
172 {
173  if (_draging)
174  {
175  setCursor(Qt::ArrowCursor);
176  }
177  _draging = false;
178  _dragModifier= false;
179 }