Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / genericgui / SceneObserverItem.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 "SceneObserverItem.hxx"
20 #include "QtGuiContext.hxx"
21 #include "ItemMimeData.hxx"
22 #include "Scene.hxx"
23
24 #include <QGraphicsSceneMouseEvent>
25 #include <QApplication>
26 #include <QDrag>
27 #include <QPixmap>
28 #include <QBitmap>
29 #include <QPainter>
30
31 //#define _DEVDEBUG_
32 #include "YacsTrace.hxx"
33
34 using namespace std;
35 using namespace YACS::ENGINE;
36 using namespace YACS::HMI;
37
38
39 SceneObserverItem::SceneObserverItem(QGraphicsScene *scene, SceneItem *parent,
40                                      QString label, Subject *subject)
41   : SceneItem(scene, parent, label), GuiObserver()
42 {
43   _subject = subject;
44   _draging = false;
45   _subject->attach(this);
46   QtGuiContext::getQtCurrent()->_mapOfSceneItem[_subject]=this;
47 }
48
49 SceneObserverItem::~SceneObserverItem()
50 {
51   QtGuiContext::getQtCurrent()->_mapOfSceneItem.erase(_subject);
52 }
53
54 void SceneObserverItem::update(GuiEvent event, int type, Subject* son)
55 {
56   DEBTRACE(" SceneObserverItem::update " << eventName(event)<< " " << type << " " << son);
57 }
58
59 void SceneObserverItem::select(bool isSelected)
60 {
61   DEBTRACE("SceneObserverItem::select "  << _label.toStdString() << " " << isSelected);
62   if (isSelected)
63     {
64 //       _scene->clearSelection();
65       setSelected(true);
66       QtGuiContext::getQtCurrent()->setSelectedSubject(_subject);
67     }
68   else setSelected(false);
69 }
70
71 void SceneObserverItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
72 {
73   DEBTRACE("SceneObserverItem::mousePressEvent " << _label.toStdString()
74            << " " << acceptedMouseButtons ());
75   if (!_scene->isZooming())
76     {
77       _subject->select(true);
78       if (_dragable && (event->button() == _dragButton))
79         {
80           setCursor(Qt::ClosedHandCursor);
81           _draging = true;
82         }
83     }
84 }
85
86 /*!
87  * creation of mime data if drag detected.
88  * setData mime type must be coherent with SchemaModel::mimeTypes to allow drop 
89  * on port item in tree view
90  */
91 void SceneObserverItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
92 {
93   if (_draging)
94     {
95       //DEBTRACE("_draging");
96       if (QLineF(event->screenPos(), 
97                  event->buttonDownScreenPos(_dragButton)).length()
98           < QApplication::startDragDistance())
99         {
100           return;
101         }
102       DEBTRACE("in drag");
103       QDrag *drag = new QDrag(event->widget());
104       ItemMimeData *mime = new ItemMimeData;
105       drag->setMimeData(mime);
106       mime->setSubject(_subject);
107       mime->setData(getMimeFormat(), "_subject");
108   
109       QPixmap pixmap(34, 34);
110       pixmap.fill(Qt::white);
111       
112       QPainter painter(&pixmap);
113       painter.translate(15, 15);
114       painter.setRenderHint(QPainter::Antialiasing);
115       paint(&painter, 0, 0);
116       painter.end();
117       
118       pixmap.setMask(pixmap.createHeuristicMask());
119       
120       drag->setPixmap(pixmap);
121       drag->setHotSpot(QPoint(15, 20));
122       
123       drag->exec();
124       setCursor(Qt::OpenHandCursor);
125     }
126 }
127
128 void SceneObserverItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
129 {
130   if (_draging)
131     {
132       setCursor(Qt::ArrowCursor);
133     }
134   _draging = false;
135 }
136
137 void SceneObserverItem::activateSelection(bool selected)
138 {
139   DEBTRACE("SceneObserverItem::activateSelection " << _label.toStdString()<< " " << selected);
140   _subject->select(true);
141 }
142
143 Subject* SceneObserverItem::getSubject()
144 {
145   return _subject;
146 }
147
148 QString SceneObserverItem::getMimeFormat()
149 {
150   return "yacs/subject";
151 }