1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-02-20 10:54:14 +01:00

MixerCurveWidget: enhance node value handling; draw node mixer value inside node;

This commit is contained in:
Mike LaBranche 2012-06-21 11:53:51 -07:00
parent 5edd952ed6
commit 7eb0105886
3 changed files with 53 additions and 50 deletions

View File

@ -43,7 +43,6 @@ Node::Node(MixerCurveWidget *graphWidget)
setCacheMode(DeviceCoordinateCache);
setZValue(-1);
vertical = false;
value = 0;
}
void Node::addEdge(Edge *edge)
@ -61,14 +60,14 @@ QList<Edge *> Node::edges() const
QRectF Node::boundingRect() const
{
qreal adjust = 2;
return QRectF(-10 - adjust, -10 - adjust,
23 + adjust, 23 + adjust);
return QRectF(-12 - adjust, -12 - adjust,
28 + adjust, 28 + adjust);
}
QPainterPath Node::shape() const
{
QPainterPath path;
path.addEllipse(-10, -10, 20, 20);
path.addEllipse(-12, -12, 25, 25);
return path;
}
@ -92,59 +91,60 @@ void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
}
painter->setBrush(gradient);
painter->setPen(QPen(Qt::black, 0));
painter->drawEllipse(-10, -10, 20, 20);
painter->drawEllipse(-12, -12, 25, 25);
painter->setPen(QPen(Qt::white, 0));
painter->drawText(-10, 3, QString().sprintf("%.2f", value()));
}
void Node::verticalMove(bool flag){
vertical = flag;
}
double Node::getValue() {
return value;
}
void Node::setValue(double val) {
value = val;
}
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
{
QPointF newPos = value.toPointF();
double Node::value() {
double h = graph->sceneRect().height();
double min = graph->getMin();
double range = graph->getMax() - min;
double ratio = (h - pos().y()) / h;
return (range * ratio ) + min;
}
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &val)
{
QPointF newPos = val.toPointF();
double h = graph->sceneRect().height();
switch (change) {
case ItemPositionChange: {
if (!vertical)
break;
// Force node to move vertically
newPos.setX(pos().x());
// Stay inside graph
if (newPos.y() < 0)
newPos.setY(0);
//qDebug() << h << " - " << newPos.y();
if (newPos.y() > h)
newPos.setY(h);
return newPos;
if (!vertical)
break;
// Force node to move vertically
newPos.setX(pos().x());
// Stay inside graph
if (newPos.y() < 0)
newPos.setY(0);
//qDebug() << h << " - " << newPos.y();
if (newPos.y() > h)
newPos.setY(h);
return newPos;
}
case ItemPositionHasChanged: {
foreach (Edge *edge, edgeList)
edge->adjust();
double min = graph->getMin();
double range = graph->getMax() - min;
double ratio = (h - newPos.y()) / h;
double val = (range * ratio ) + min;
setValue(val);
update();
graph->itemMoved(val);
graph->itemMoved(value());
break;
}
default:
break;
};
return QGraphicsItem::itemChange(change, value);
return QGraphicsItem::itemChange(change, val);
}
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)

View File

@ -55,22 +55,21 @@ public:
QPainterPath shape() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void setValue(double val);
double getValue();
double value();
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
QVariant itemChange(GraphicsItemChange change, const QVariant &val);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
private:
double value;
QList<Edge *> edgeList;
QPointF newPos;
MixerCurveWidget *graph;
bool vertical;
};
#endif // MIXERCURVEPOINT_H

View File

@ -163,6 +163,8 @@ void MixerCurveWidget::initNodes(int numPoints)
nodeList.append(node);
scene()->addItem(node);
node->setPos(0,0);
if (prevNode) {
scene()->addItem(getEdge(i, prevNode, node));
}
@ -179,7 +181,7 @@ QList<double> MixerCurveWidget::getCurve() {
QList<double> list;
foreach(Node *node, nodeList) {
list.append(node->getValue());
list.append(node->value());
}
return list;
@ -212,8 +214,10 @@ void MixerCurveWidget::setCurve(QList<double> points)
double min = curveMin + 10;
double max = curveMax + 10;
qreal w = plot->boundingRect().width()/(points.count()-1);
qreal h = plot->boundingRect().height();
QRectF plotRect = plot->boundingRect();
qreal w = plotRect.width()/(points.count()-1);
qreal h = plotRect.height();
for (int i=0; i<points.count(); i++) {
double val = points.at(i);
@ -226,12 +230,15 @@ void MixerCurveWidget::setCurve(QList<double> points)
val -= min;
val /= (max - min);
nodeList.at(i)->setPos(w*i, h - (val*h));
nodeList.at(i)->verticalMove(true);
}
Node* node = nodeList.at(i);
node->setPos(w*i, h - (val*h));
node->verticalMove(true);
}
curveUpdating = false;
update();
emit curveUpdated(points, (double)0);
}
@ -252,13 +259,10 @@ void MixerCurveWidget::resizeEvent(QResizeEvent* event)
fitInView(plot, Qt::KeepAspectRatio);
}
void MixerCurveWidget::itemMoved(double itemValue)
{
if (!curveUpdating) {
QList<double> list = getCurve();
emit curveUpdated(list, itemValue);
emit curveUpdated(getCurve(), itemValue);
}
}