MNE-CPP  beta 1.0
pluginitem.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "pluginitem.h"
42 #include "arrow.h"
43 
44 #include <QGraphicsScene>
45 #include <QGraphicsSceneContextMenuEvent>
46 #include <QMenu>
47 #include <QPainter>
48 
49 
50 //*************************************************************************************************************
51 //=============================================================================================================
52 // USED NAMESPACES
53 //=============================================================================================================
54 
55 using namespace MNEX;
56 
57 
58 //*************************************************************************************************************
59 //=============================================================================================================
60 // DEFINE MEMBER METHODS
61 //=============================================================================================================
62 
63 PluginItem::PluginItem(IPlugin::SPtr pPlugin, QMenu *contextMenu, QGraphicsItem *parent)
64 : QGraphicsPolygonItem(parent)
65 , m_pPlugin(pPlugin)
66 , m_iWidth(60)
67 , m_iHeight(40)
68 , m_contextMenu(contextMenu)
69 {
70  QPainterPath path;
71  m_qLinearGradientFace = QLinearGradient(m_iWidth/2, -10, m_iWidth/2, m_iHeight);
72  m_qLinearGradientFace.setColorAt(1, Qt::white);
73  m_qPolygon << QPointF(-m_iWidth/2, -m_iHeight/2) << QPointF(m_iWidth/2, -m_iHeight/2)
74  << QPointF(m_iWidth/2, m_iHeight/2) << QPointF(-m_iWidth/2, m_iHeight/2)
75  << QPointF(-m_iWidth/2, -m_iHeight/2);
76  switch (m_pPlugin->getType()) {
77 // case StartEnd:
78 // m_qColorContour = QColor(79, 136, 187);
79 // m_qLinearGradientFace.setColorAt(0, QColor(234, 239, 247));
80 // break;
82  m_qColorContour = QColor(98, 152, 61);
83  m_qLinearGradientFace.setColorAt(0, QColor(235, 241, 233));
84  break;
85  case IPlugin::_ISensor:
86  m_qColorContour = QColor(79, 136, 187);
87  m_qLinearGradientFace.setColorAt(0, QColor(234, 239, 247));
88  break;
89  case IPlugin::_IIO:
90  m_qColorContour = QColor(224, 169, 0);
91  m_qLinearGradientFace.setColorAt(0, QColor(255, 244, 231));
92  break;
93  default:
94  m_qColorContour = QColor(125, 125, 125);
95  m_qLinearGradientFace.setColorAt(0, QColor(125, 125, 125));
96  break;
97  }
98  setBrush(m_qLinearGradientFace);
99  setPen(QPen(m_qColorContour,1));
100  setPolygon(m_qPolygon);
101  setFlag(QGraphicsItem::ItemIsMovable, true);
102  setFlag(QGraphicsItem::ItemIsSelectable, true);
103  setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
104 }
105 
106 
107 //*************************************************************************************************************
108 
109 PluginItem::~PluginItem()
110 {
111  m_pPlugin->unload();
112 }
113 
114 
115 //*************************************************************************************************************
116 
117 void PluginItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
118 {
119  QGraphicsPolygonItem::paint(painter, option, widget);
120 
121 
122  painter->setPen(QPen(m_qColorContour, 1));
123 
124 // QString sKind("");
125 // switch (m_diagramType) {
126 // case StartEnd:
127 // break;
128 // case Algorithm:
129 // sKind = QString("Tool");
130 // break;
131 // case Sensor:
132 // sKind = QString("Sensor");
133 // break;
134 // default:
135 // sKind = QString("IO");
136 // break;
137 // }
138 
139  painter->drawText(-m_iWidth/2+4,-m_iHeight/2+14,m_pPlugin->getName().mid(0,8));
140 
141  painter->drawText(-m_iWidth/2+4,-m_iHeight/2+28,m_pPlugin->getName().mid(8,8));
142 
143 }
144 
145 
146 //*************************************************************************************************************
147 
148 void PluginItem::removeArrow(Arrow *arrow)
149 {
150  int index = arrows.indexOf(arrow);
151 
152  if (index != -1)
153  arrows.removeAt(index);
154 }
155 
156 
157 //*************************************************************************************************************
158 
159 void PluginItem::removeArrows()
160 {
161  foreach (Arrow *arrow, arrows) {
162  arrow->startItem()->removeArrow(arrow);
163  arrow->endItem()->removeArrow(arrow);
164  scene()->removeItem(arrow);
165  delete arrow;
166  }
167 }
168 
169 
170 //*************************************************************************************************************
171 
172 void PluginItem::addArrow(Arrow *arrow)
173 {
174  arrows.append(arrow);
175 }
176 
177 
178 //*************************************************************************************************************
179 
180 QPixmap PluginItem::image() const
181 {
182  QPixmap pixmap(m_iWidth, m_iHeight);
183  pixmap.fill(Qt::transparent);
184  QPainter painter(&pixmap);
185  painter.setPen(QPen(m_qColorContour, 2));
186  painter.translate(m_iWidth/2, m_iHeight/2);
187 
188  painter.drawPolyline(m_qPolygon);
189 
190  painter.drawText(-m_iWidth/2+4,-m_iHeight/2+14,m_pPlugin->getName().mid(0,10));
191 
192  return pixmap;
193 }
194 
195 
196 //*************************************************************************************************************
197 
198 void PluginItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
199 {
200  scene()->clearSelection();
201  setSelected(true);
202  m_contextMenu->exec(event->screenPos());
203 }
204 
205 
206 //*************************************************************************************************************
207 
208 QVariant PluginItem::itemChange(GraphicsItemChange change, const QVariant &value)
209 {
210  if (change == QGraphicsItem::ItemPositionChange) {
211  foreach (Arrow *arrow, arrows) {
212  arrow->updatePosition();
213  }
214  }
215 
216  return value;
217 }
QSharedPointer< IPlugin > SPtr
Definition: IPlugin.h:108
Definition: arrow.h:75
PluginItem class declaration.
Arrow class declaration.