dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
mixercurvepoint.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 
28 #include <QGraphicsScene>
29 #include <QGraphicsSceneMouseEvent>
30 #include <QPainter>
31 #include <QStyleOption>
32 #include <QColor>
33 #include <QDebug>
34 
35 #include "mixercurveline.h"
36 #include "mixercurvepoint.h"
37 #include "mixercurvewidget.h"
38 
39 MixerNode::MixerNode(MixerCurveWidget *graphWidget)
40  : graph(graphWidget)
41 {
42  setFlag(ItemIsMovable);
43  setFlag(ItemSendsGeometryChanges);
44  setCacheMode(DeviceCoordinateCache);
45  setZValue(-1);
46  cmdActive = false;
47  vertical = false;
48  cmdNode = false;
49  cmdToggle = true;
50  drawNode = true;
51  drawText = true;
52 
53  posColor0 = "#1c870b"; // greenish?
54  posColor1 = "#116703"; // greenish?
55  negColor0 = "#aa0000"; // red
56  negColor1 = "#aa0000"; // red
57 }
58 
59 void MixerNode::addEdge(Edge *edge)
60 {
61  edgeList << edge;
62  edge->adjust();
63 }
64 
65 QList<Edge *> MixerNode::edges() const
66 {
67  return edgeList;
68 }
69 
70 QRectF MixerNode::boundingRect() const
71 {
72  return cmdNode ? QRectF(-4, -4, 15, 10) : QRectF(-13, -13, 26, 26);
73 }
74 
75 QPainterPath MixerNode::shape() const
76 {
77  QPainterPath path;
78  path.addEllipse(boundingRect());
79  return path;
80 }
81 
82 void MixerNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
83 {
84  QString text = cmdNode ? cmdText : QString().sprintf("%.2f", value());
85  painter->setFont(graph->font());
86  if (drawNode) {
87  QRadialGradient gradient(-3, -3, 10);
88  if (option->state & QStyle::State_Sunken) {
89  gradient.setCenter(3, 3);
90  gradient.setFocalPoint(3, 3);
91 
92  gradient.setColorAt(1, Qt::darkBlue);
93  gradient.setColorAt(0, Qt::darkBlue);
94  } else {
95  if (cmdNode) {
96  gradient.setColorAt(0, cmdActive ? posColor0 : negColor0);
97  gradient.setColorAt(1, cmdActive ? posColor1 : negColor1);
98  } else {
99  if (value() < 0) {
100  gradient.setColorAt(0, negColor0);
101  gradient.setColorAt(1, negColor1);
102  } else {
103  gradient.setColorAt(0, posColor0);
104  gradient.setColorAt(1, posColor1);
105  }
106  }
107  }
108  painter->setBrush(gradient);
109  painter->setPen(QPen(Qt::black, 0));
110  painter->drawEllipse(boundingRect());
111 
112  if (!image.isNull())
113  painter->drawImage(boundingRect().adjusted(1, 1, -1, -1), image);
114  }
115 
116  if (drawText) {
117  painter->setPen(QPen(drawNode ? Qt::white : Qt::black, 0));
118  if (cmdNode) {
119  painter->drawText(0, 4, text);
120  } else {
121  painter->drawText((value() < 0) ? -13 : -11, 4, text);
122  }
123  }
124 }
125 
126 void MixerNode::verticalMove(bool flag)
127 {
128  vertical = flag;
129 }
130 
131 void MixerNode::commandNode(bool enable)
132 {
133  cmdNode = enable;
134 }
135 void MixerNode::commandText(QString text)
136 {
137  cmdText = text;
138 }
139 
140 double MixerNode::value()
141 {
142  double h = graph->sceneRect().height();
143  double ratio = (h - pos().y()) / h;
144  return ((graph->getMax() - graph->getMin()) * ratio) + graph->getMin();
145 }
146 
147 QVariant MixerNode::itemChange(GraphicsItemChange change, const QVariant &val)
148 {
149  QPointF newPos = val.toPointF();
150  double h = graph->sceneRect().height();
151 
152  switch (change) {
153  case ItemPositionChange: {
154 
155  if (!vertical)
156  break;
157 
158  // Force node to move vertically
159  newPos.setX(pos().x());
160 
161  // Stay inside graph
162  if (newPos.y() < 0)
163  newPos.setY(0);
164  // qDebug() << h << " - " << newPos.y();
165  if (newPos.y() > h)
166  newPos.setY(h);
167 
168  return newPos;
169  }
170  case ItemPositionHasChanged: {
171  foreach (Edge *edge, edgeList)
172  edge->adjust();
173 
174  update();
175 
176  graph->itemMoved(value());
177  break;
178  }
179  default:
180  break;
181  };
182 
183  return QGraphicsItem::itemChange(change, val);
184 }
185 
186 void MixerNode::mousePressEvent(QGraphicsSceneMouseEvent *event)
187 {
188  if (cmdNode) {
189  graph->cmdActivated(this);
190  // return;
191  }
192  update();
193  QGraphicsItem::mousePressEvent(event);
194 }
195 
196 void MixerNode::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
197 {
198  update();
199  QGraphicsItem::mouseReleaseEvent(event);
200 }
void mousePressEvent(QGraphicsSceneMouseEvent *event)
void adjust()
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
void itemMoved(double itemValue)
QVariant itemChange(GraphicsItemChange change, const QVariant &val)
void cmdActivated(MixerNode *node)
x
Definition: OPPlots.m:100