Version: 8.3.0
SceneObserverItem.cxx
Go to the documentation of this file.
1 // Copyright (C) 2006-2016 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, or (at your option) any later version.
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 
20 #include "SceneObserverItem.hxx"
21 #include "QtGuiContext.hxx"
22 #include "ItemMimeData.hxx"
23 #include "Scene.hxx"
24 #include "SchemaItem.hxx"
25 #include "Resource.hxx"
26 
27 #include <QGraphicsSceneMouseEvent>
28 #include <QApplication>
29 #include <QDrag>
30 #include <QPixmap>
31 #include <QBitmap>
32 #include <QPainter>
33 
34 //#define _DEVDEBUG_
35 #include "YacsTrace.hxx"
36 
37 using namespace std;
38 using namespace YACS::ENGINE;
39 using namespace YACS::HMI;
40 
41 
42 SceneObserverItem::SceneObserverItem(QGraphicsScene *scene, SceneItem *parent,
43  QString label, Subject *subject)
44  : SceneItem(scene, parent, label), GuiObserver()
45 {
46  _subject = subject;
47  _draging = false;
48  _dragModifier = false;
49  _emphasized = false;
50  _subject->attach(this);
52 }
53 
55 {
57 }
58 
59 void SceneObserverItem::update(GuiEvent event, int type, Subject* son)
60 {
61  DEBTRACE(" SceneObserverItem::update " << eventName(event)<< " " << type << " " << son);
62  switch (event)
63  {
65  DEBTRACE("SceneObserverItem::update EMPHASIZE " << type);
66  if (type)
67  _emphasized = true;
68  else
69  _emphasized = false;
70  QGraphicsItem::update();
71  break;
72  default:
73  ;
74  }
75 }
76 
77 void SceneObserverItem::select(bool isSelected)
78 {
79  DEBTRACE("SceneObserverItem::select " << _label.toStdString() << " " << isSelected);
80  if (isSelected)
81  {
82 // _scene->clearSelection();
83  setSelected(true);
85  }
86  else setSelected(false);
87 }
88 
90 {
91  DEBTRACE("SceneObserverItem::getToolTip");
92  if (!_subject)
93  return _label;
94  if ( !QtGuiContext::getQtCurrent() || !QtGuiContext::getQtCurrent()->_mapOfSchemaItem.count(_subject))
95  return _label;
96  QString val ="";
98  val = item->data(0, Qt::ToolTipRole).toString();
99  return val;
100 }
101 
102 void SceneObserverItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
103 {
104  DEBTRACE("SceneObserverItem::mousePressEvent " << _label.toStdString()
105  << " " << acceptedMouseButtons ());
106  if (!_scene->isZooming())
107  {
108  _subject->select(true);
109  if (_dragable && (event->button() == _dragButton) && QtGuiContext::getQtCurrent()->isEdition())
110  {
111  setCursor(Qt::ClosedHandCursor);
112  _draging = true;
113  _dragModifier= event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
114  }
115  }
116 }
117 
123 void SceneObserverItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
124 {
125  if (_draging)
126  {
127  //DEBTRACE("_draging");
128  if (QLineF(event->screenPos(),
129  event->buttonDownScreenPos(_dragButton)).length()
130  < QApplication::startDragDistance())
131  {
132  return;
133  }
134  DEBTRACE("in drag");
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");
140  if(_dragModifier)
141  mime->setControl(false);
142  else
143  mime->setControl(true);
144 
145  QPixmap pixmap(34, 34);
146  pixmap.fill(Qt::white);
147 
148  QPainter painter(&pixmap);
149  painter.translate(15, 15);
150  painter.setRenderHint(QPainter::Antialiasing);
151  paint(&painter, 0, 0);
152  painter.end();
153 
154  pixmap.setMask(pixmap.createHeuristicMask());
155 
156  drag->setPixmap(pixmap);
157  drag->setHotSpot(QPoint(15, 20));
158 
159  drag->exec();
160  //restore non drag state
161  setCursor(Qt::ArrowCursor);
162  _draging = false;
163  _dragModifier = false;
164  }
165 }
166 
167 void SceneObserverItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
168 {
169  if (_draging)
170  {
171  setCursor(Qt::ArrowCursor);
172  }
173  _draging = false;
174  _dragModifier = false;
175 }
176 
178 {
179  QColor color;
180  color = _brushColor;
181  if (isSelected())
182  color = _hiBrushColor;
183  if (_emphasized)
185  if (_hover)
186  color = hoverColor(color);
187  return color;
188 }
189 
191 {
192  DEBTRACE("SceneObserverItem::activateSelection " << _label.toStdString()<< " " << selected);
193  _subject->select(true);
194 }
195 
197 {
198  return _subject;
199 }
200 
202 {
203  return "yacs/subject";
204 }