1 // Copyright (C) 2006-2008 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.
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
19 #include "SceneCtrlPortItem.hxx"
20 #include "SceneTextItem.hxx"
21 #include "SceneNodeItem.hxx"
23 #include "ItemMimeData.hxx"
25 // #include "QtGuiContext.hxx"
27 #include <QGraphicsSceneMouseEvent>
28 #include <QApplication>
32 // #include <QGraphicsSceneHoverEvent>
38 #include "YacsTrace.hxx"
41 using namespace YACS::ENGINE;
42 using namespace YACS::HMI;
44 const int SceneCtrlPortItem::_portWidth = 85;
45 const int SceneCtrlPortItem::_portHeight = 25;
47 SceneCtrlPortItem::SceneCtrlPortItem(QGraphicsScene *scene, SceneItem *parent,
49 : SceneItem(scene, parent, label), ScenePortItem(label)
52 _height = _portHeight;
54 _brushColor = QColor(205,210,227);
55 _hiBrushColor = QColor(161,176,227);
56 _penColor = QColor(120,120,120);
57 _hiPenColor = QColor( 60, 60, 60);
60 SceneCtrlPortItem::~SceneCtrlPortItem()
64 void SceneCtrlPortItem::paint(QPainter *painter,
65 const QStyleOptionGraphicsItem *option,
68 //DEBTRACE("ScenePortItem::paint");
70 painter->setPen(getPenColor());
71 painter->setBrush(getBrushColor());
72 painter->drawRoundRect(QRectF(0, 0, _width, _height), 33*_height/_width, 33);
76 void SceneCtrlPortItem::setText(QString label)
79 _text = new SceneTextItem(_scene,
83 _text->setPlainText(label);
86 SceneNodeItem* SceneCtrlPortItem::getParentNode()
89 return dynamic_cast<SceneNodeItem*>(_parent->getParent());
94 SubjectNode* SceneCtrlPortItem::getSubjectNode()
96 SceneNodeItem* nodeItem = getParentNode();
97 if (!nodeItem) return 0;
98 return dynamic_cast<SubjectNode*>(nodeItem->getSubject());
101 QString SceneCtrlPortItem::getMimeFormat()
103 return "yacs/subjectGate";
106 void SceneCtrlPortItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
108 DEBTRACE("SceneCtrlPortItem::mousePressEvent " << _label.toStdString()
109 << " " << acceptedMouseButtons ());
110 if (!_scene->isZooming())
112 if (_dragable && (event->button() == _dragButton))
114 setCursor(Qt::ClosedHandCursor);
121 * creation of mime data if drag detected.
122 * setData mime type must be coherent with SchemaModel::mimeTypes to allow drop
123 * on port item in tree view
125 void SceneCtrlPortItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
129 //DEBTRACE("_draging");
130 if (QLineF(event->screenPos(),
131 event->buttonDownScreenPos(_dragButton)).length()
132 < QApplication::startDragDistance())
137 QDrag *drag = new QDrag(event->widget());
138 ItemMimeData *mime = new ItemMimeData;
139 drag->setMimeData(mime);
140 mime->setSubject(getSubjectNode());
141 mime->setData(getMimeFormat(), "_subject");
143 QPixmap pixmap(34, 34);
144 pixmap.fill(Qt::white);
146 QPainter painter(&pixmap);
147 painter.translate(15, 15);
148 painter.setRenderHint(QPainter::Antialiasing);
149 paint(&painter, 0, 0);
152 pixmap.setMask(pixmap.createHeuristicMask());
154 drag->setPixmap(pixmap);
155 drag->setHotSpot(QPoint(15, 20));
158 setCursor(Qt::OpenHandCursor);
162 void SceneCtrlPortItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
166 setCursor(Qt::ArrowCursor);
172 int SceneCtrlPortItem::getPortWidth()
177 int SceneCtrlPortItem::getPortHeight()