1 // Copyright (C) 2006-2016 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, or (at your option) any later version.
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
20 #include "SceneObserverItem.hxx"
21 #include "QtGuiContext.hxx"
22 #include "ItemMimeData.hxx"
24 #include "SchemaItem.hxx"
25 #include "Resource.hxx"
27 #include <QGraphicsSceneMouseEvent>
28 #include <QApplication>
35 #include "YacsTrace.hxx"
38 using namespace YACS::ENGINE;
39 using namespace YACS::HMI;
42 SceneObserverItem::SceneObserverItem(QGraphicsScene *scene, SceneItem *parent,
43 QString label, Subject *subject)
44 : SceneItem(scene, parent, label), GuiObserver()
48 _dragModifier = false;
50 _subject->attach(this);
51 QtGuiContext::getQtCurrent()->_mapOfSceneItem[_subject]=this;
54 SceneObserverItem::~SceneObserverItem()
56 QtGuiContext::getQtCurrent()->_mapOfSceneItem.erase(_subject);
59 void SceneObserverItem::update(GuiEvent event, int type, Subject* son)
61 DEBTRACE(" SceneObserverItem::update " << eventName(event)<< " " << type << " " << son);
64 case YACS::HMI::EMPHASIZE:
65 DEBTRACE("SceneObserverItem::update EMPHASIZE " << type);
70 QGraphicsItem::update();
77 void SceneObserverItem::select(bool isSelected)
79 DEBTRACE("SceneObserverItem::select " << _label.toStdString() << " " << isSelected);
82 // _scene->clearSelection();
84 QtGuiContext::getQtCurrent()->setSelectedSubject(_subject);
86 else setSelected(false);
89 QString SceneObserverItem::getToolTip()
91 DEBTRACE("SceneObserverItem::getToolTip");
94 if ( !QtGuiContext::getQtCurrent() || !QtGuiContext::getQtCurrent()->_mapOfSchemaItem.count(_subject))
97 SchemaItem * item = QtGuiContext::getQtCurrent()->_mapOfSchemaItem[_subject];
98 val = item->data(0, Qt::ToolTipRole).toString();
102 void SceneObserverItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
104 DEBTRACE("SceneObserverItem::mousePressEvent " << _label.toStdString()
105 << " " << acceptedMouseButtons ());
106 if (!_scene->isZooming())
108 _subject->select(true);
109 if (_dragable && (event->button() == _dragButton) && QtGuiContext::getQtCurrent()->isEdition())
111 setCursor(Qt::ClosedHandCursor);
113 _dragModifier= event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
119 * creation of mime data if drag detected.
120 * setData mime type must be coherent with SchemaModel::mimeTypes to allow drop
121 * on port item in tree view
123 void SceneObserverItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
127 //DEBTRACE("_draging");
128 if (QLineF(event->screenPos(),
129 event->buttonDownScreenPos(_dragButton)).length()
130 < QApplication::startDragDistance())
135 QDrag *drag = new QDrag(event->widget());
136 ItemMimeData *mime = new ItemMimeData;
137 drag->setMimeData(mime);
138 mime->setSubject(_subject);
139 mime->setData(getMimeFormat(), "_subject");
141 mime->setControl(false);
143 mime->setControl(true);
145 QPixmap pixmap(34, 34);
146 pixmap.fill(Qt::white);
148 QPainter painter(&pixmap);
149 painter.translate(15, 15);
150 painter.setRenderHint(QPainter::Antialiasing);
151 paint(&painter, 0, 0);
154 pixmap.setMask(pixmap.createHeuristicMask());
156 drag->setPixmap(pixmap);
157 drag->setHotSpot(QPoint(15, 20));
160 //restore non drag state
161 setCursor(Qt::ArrowCursor);
163 _dragModifier = false;
167 void SceneObserverItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
171 setCursor(Qt::ArrowCursor);
174 _dragModifier = false;
177 QColor SceneObserverItem::getBrushColor()
182 color = _hiBrushColor;
184 color = Resource::emphasizeBrushColor;
186 color = hoverColor(color);
190 void SceneObserverItem::activateSelection(bool selected)
192 DEBTRACE("SceneObserverItem::activateSelection " << _label.toStdString()<< " " << selected);
193 _subject->select(true);
196 Subject* SceneObserverItem::getSubject()
201 QString SceneObserverItem::getMimeFormat()
203 return "yacs/subject";