]> SALOME platform Git repositories - modules/yacs.git/blob - src/genericgui/SceneCtrlPortItem.cxx
Salome HOME
56ab32601793b6f4c0168c3c85902b7ec979b8cb
[modules/yacs.git] / src / genericgui / SceneCtrlPortItem.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 "SceneCtrlPortItem.hxx"
20 #include "SceneTextItem.hxx"
21 #include "SceneNodeItem.hxx"
22 #include "Scene.hxx"
23 #include "ItemMimeData.hxx"
24
25 // #include "QtGuiContext.hxx"
26 #include "Menus.hxx"
27 #include <QGraphicsSceneMouseEvent>
28 #include <QApplication>
29 #include <QDrag>
30 #include <QPixmap>
31 #include <QBitmap>
32 // #include <QGraphicsSceneHoverEvent>
33 // #include <QPointF>
34
35 // #include <cassert>
36
37 //#define _DEVDEBUG_
38 #include "YacsTrace.hxx"
39
40 using namespace std;
41 using namespace YACS::ENGINE;
42 using namespace YACS::HMI;
43
44 const int SceneCtrlPortItem::_portWidth  = 85;
45 const int SceneCtrlPortItem::_portHeight = 25;
46
47 SceneCtrlPortItem::SceneCtrlPortItem(QGraphicsScene *scene, SceneItem *parent,
48                                      QString label)
49   : SceneItem(scene, parent, label), ScenePortItem(label)
50 {
51   _width  = _portWidth;
52   _height = _portHeight;
53   setText(label);
54   _brushColor   = QColor(205,210,227);
55   _hiBrushColor = QColor(161,176,227);
56   _penColor     = QColor(120,120,120);
57   _hiPenColor   = QColor( 60, 60, 60);
58 }
59
60 SceneCtrlPortItem::~SceneCtrlPortItem()
61 {
62 }
63
64 void SceneCtrlPortItem::paint(QPainter *painter,
65                           const QStyleOptionGraphicsItem *option,
66                           QWidget *widget)
67 {
68   //DEBTRACE("ScenePortItem::paint");
69   painter->save();
70   painter->setPen(getPenColor());
71   painter->setBrush(getBrushColor());
72   painter->drawRoundRect(QRectF(0, 0, _width, _height), 33*_height/_width, 33);
73   painter->restore();
74 }
75
76 void SceneCtrlPortItem::setText(QString label)
77 {
78   if (!_text)
79     _text = new SceneTextItem(_scene,
80                               this,
81                               label);
82   else
83     _text->setPlainText(label);
84 }
85
86 SceneNodeItem* SceneCtrlPortItem::getParentNode()
87 {
88   if (_parent)
89     return dynamic_cast<SceneNodeItem*>(_parent->getParent());
90   else
91     return 0;
92 }
93
94 SubjectNode* SceneCtrlPortItem::getSubjectNode()
95 {
96   SceneNodeItem* nodeItem = getParentNode();
97   if (!nodeItem) return 0;
98   return dynamic_cast<SubjectNode*>(nodeItem->getSubject());
99 }
100
101 QString SceneCtrlPortItem::getMimeFormat()
102 {
103   return "yacs/subjectGate";
104 }
105
106 void SceneCtrlPortItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
107 {
108   DEBTRACE("SceneCtrlPortItem::mousePressEvent " << _label.toStdString()
109            << " " << acceptedMouseButtons ());
110   if (!_scene->isZooming())
111     {
112       if (_dragable && (event->button() == _dragButton))
113         {
114           setCursor(Qt::ClosedHandCursor);
115           _draging = true;
116         }
117     }
118 }
119
120 /*!
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
124  */
125 void SceneCtrlPortItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
126 {
127   if (_draging)
128     {
129       //DEBTRACE("_draging");
130       if (QLineF(event->screenPos(), 
131                  event->buttonDownScreenPos(_dragButton)).length()
132           < QApplication::startDragDistance())
133         {
134           return;
135         }
136       DEBTRACE("in drag");
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");
142   
143       QPixmap pixmap(34, 34);
144       pixmap.fill(Qt::white);
145       
146       QPainter painter(&pixmap);
147       painter.translate(15, 15);
148       painter.setRenderHint(QPainter::Antialiasing);
149       paint(&painter, 0, 0);
150       painter.end();
151       
152       pixmap.setMask(pixmap.createHeuristicMask());
153       
154       drag->setPixmap(pixmap);
155       drag->setHotSpot(QPoint(15, 20));
156       
157       drag->exec();
158       setCursor(Qt::OpenHandCursor);
159     }
160 }
161
162 void SceneCtrlPortItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
163 {
164   if (_draging)
165     {
166       setCursor(Qt::ArrowCursor);
167     }
168   _draging = false;
169 }
170
171
172 int SceneCtrlPortItem::getPortWidth()
173 {
174   return _portWidth;
175 }
176
177 int SceneCtrlPortItem::getPortHeight()
178 {
179   return _portHeight;
180 }
181