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 "SceneObserverItem.hxx"
20 #include "QtGuiContext.hxx"
21 #include "ItemMimeData.hxx"
24 #include <QGraphicsSceneMouseEvent>
25 #include <QApplication>
32 #include "YacsTrace.hxx"
35 using namespace YACS::ENGINE;
36 using namespace YACS::HMI;
39 SceneObserverItem::SceneObserverItem(QGraphicsScene *scene, SceneItem *parent,
40 QString label, Subject *subject)
41 : SceneItem(scene, parent, label), GuiObserver()
45 _subject->attach(this);
46 QtGuiContext::getQtCurrent()->_mapOfSceneItem[_subject]=this;
49 SceneObserverItem::~SceneObserverItem()
51 QtGuiContext::getQtCurrent()->_mapOfSceneItem.erase(_subject);
54 void SceneObserverItem::update(GuiEvent event, int type, Subject* son)
56 DEBTRACE(" SceneObserverItem::update " << eventName(event)<< " " << type << " " << son);
59 void SceneObserverItem::select(bool isSelected)
61 DEBTRACE("SceneObserverItem::select " << _label.toStdString() << " " << isSelected);
64 // _scene->clearSelection();
66 QtGuiContext::getQtCurrent()->setSelectedSubject(_subject);
68 else setSelected(false);
71 void SceneObserverItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
73 DEBTRACE("SceneObserverItem::mousePressEvent " << _label.toStdString()
74 << " " << acceptedMouseButtons ());
75 if (!_scene->isZooming())
77 _subject->select(true);
78 if (_dragable && (event->button() == _dragButton))
80 setCursor(Qt::ClosedHandCursor);
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
91 void SceneObserverItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
95 //DEBTRACE("_draging");
96 if (QLineF(event->screenPos(),
97 event->buttonDownScreenPos(_dragButton)).length()
98 < QApplication::startDragDistance())
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");
109 QPixmap pixmap(34, 34);
110 pixmap.fill(Qt::white);
112 QPainter painter(&pixmap);
113 painter.translate(15, 15);
114 painter.setRenderHint(QPainter::Antialiasing);
115 paint(&painter, 0, 0);
118 pixmap.setMask(pixmap.createHeuristicMask());
120 drag->setPixmap(pixmap);
121 drag->setHotSpot(QPoint(15, 20));
124 setCursor(Qt::OpenHandCursor);
128 void SceneObserverItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
132 setCursor(Qt::ArrowCursor);
137 void SceneObserverItem::activateSelection(bool selected)
139 DEBTRACE("SceneObserverItem::activateSelection " << _label.toStdString()<< " " << selected);
140 _subject->select(true);
143 Subject* SceneObserverItem::getSubject()
148 QString SceneObserverItem::getMimeFormat()
150 return "yacs/subject";