void tst_QPainterPath::angleAtPercent()
{
    for (int angle = 0; angle < 360; ++angle) {
        QLineF line = QLineF::fromPolar(100, angle);
        QPainterPath path;
        path.moveTo(line.p1());
        path.lineTo(line.p2());

        QCOMPARE(path.angleAtPercent(0.5), line.angle());
    }
}
Exemplo n.º 2
0
void Color_Wheel::mousePressEvent(QMouseEvent *ev)
{
    if ( ev->buttons() & Qt::LeftButton )
    {
        QLineF ray = line_to_point(ev->pos());
        if ( ray.length() <= inner_radius() )
            mouse_status = Drag_Square;
        else if ( ray.length() <= outer_radius() )
            mouse_status = Drag_Circle;
    }
}
Exemplo n.º 3
0
float RayDisplayScene::pointToLineDistSquared(const QPointF &point, const QLineF &line) const
{
	const float x2x1 = line.p2().x() - line.p1().x();
	const float y2y1 = line.p2().y() - line.p1().y();
	const float d = (x2x1) * (line.p1().y() - point.y()) - (line.p1().x() - point.x()) * (y2y1);
	const float dSq = d * d;
	const float y2y1Sq = y2y1 * y2y1;
	const float x2x1Sq = x2x1 * x2x1;
	const float distSq = dSq / (x2x1Sq + y2y1Sq);

	return distSq;
}
Exemplo n.º 4
0
void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
          QWidget *)
{
    if (myStartItem->collidesWithItem(myEndItem))
        return;

    QPen myPen = pen();
    myPen.setColor(Qt::black);
    qreal arrowSize = 20;
    painter->setPen(myPen);
    painter->setBrush(Qt::black);

    QLineF centerLine(myStartItem->pos(), myEndItem->pos());
    QPolygonF endPolygon = myEndItem->polygon();
    QPointF p1 = endPolygon.first() + myEndItem->pos();
    QPointF p2;
    QPointF intersectPoint;
    QLineF polyLine;
    for (int i = 1; i < endPolygon.count(); ++i) {
    p2 = endPolygon.at(i) + myEndItem->pos();
    polyLine = QLineF(p1, p2);
    QLineF::IntersectType intersectType =
        polyLine.intersect(centerLine, &intersectPoint);
    if (intersectType == QLineF::BoundedIntersection)
        break;
        p1 = p2;
    }

    setLine(QLineF(intersectPoint, myStartItem->pos()));

    double angle = ::acos(line().dx() / line().length());
    if (line().dy() >= 0)
        angle = (Pi * 2) - angle;

        QPointF arrowP1 = line().p1() + QPointF(sin(angle + Pi / 3) * arrowSize,
                                        cos(angle + Pi / 3) * arrowSize);
        QPointF arrowP2 = line().p1() + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
                                        cos(angle + Pi - Pi / 3) * arrowSize);

        arrowHead.clear();
        arrowHead << line().p1() << arrowP1 << arrowP2;

        painter->drawLine(line());
        painter->drawPolygon(arrowHead);
        if (isSelected()) {
            painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
        QLineF myLine = line();
        myLine.translate(0, 4.0);
        painter->drawLine(myLine);
        myLine.translate(0,-8.0);
        painter->drawLine(myLine);
    }
}
Exemplo n.º 5
0
QString CurveTracker::curveInfoAt( 
    const QwtPlotCurve *curve, const QPointF &pos ) const
{
    const QLineF line = curveLineAt( curve, pos.x() );
    if ( line.isNull() )
        return QString::null;

    QPointF p = line.pointAt(( pos.x() - line.p1().x() ) / line.dx());

    QString info( "<font color=""%1"">%2 / %3</font>" );
    return info.arg( curve->pen().color().name() ).arg( p.y() ).arg(p.x());
}
Exemplo n.º 6
0
void MainWindow::sample() {
    if (m_points.size() > 1) {
        for (int i = 0; i < m_lines.size(); i++)
            delete m_lines[i];
        m_lines.clear();

        float length = 0;
        QPointF *prev = m_points.value(0);
        QList<float> lens;
        for (int i = 1; i < m_points.size(); i++) {
            QLineF *line = new QLineF(*prev, *m_points.value(i));
            m_lines.push_back(line);
            lens.push_back(line->length());
            length += line->length();
            prev = m_points.value(i);
        }

        for (int i = 0; i < m_samples.size(); i++)
            delete m_samples[i];
        m_samples.clear();

        float step = 1;
        int which = 0;
        float currdelta = 0;

        while (which < m_lines.size()) {
            float alpha = currdelta / lens[which];
            if (alpha < 0 || alpha > 1)
                printf("Error: alpha is not unit valued");
            m_samples.push_back(new QPointF(m_lines[which]->pointAt(alpha)));

            currdelta += step;

            if (currdelta > lens[which]) {
                currdelta -= lens[which];
                which++;
            }
        }

        if (m_sectval)
            delete m_sectval;

        m_sectval = new FloatImage(m_hsi->bands(), m_samples.size(), 1);
        m_hsi->section(m_samples, m_sectval);
    } else {
        if (m_sectval)
            delete m_sectval;

        m_sectval = new FloatImage(1, 1, 1);
    }

    updateSect();
}
Exemplo n.º 7
0
void LineItem::setLine(const QLineF &line_) {
  setPos(line_.p1());
  setViewRect(QRectF(0.0, 0.0, 0.0, sizeOfGrip().height()));

  if (!rect().isEmpty()) {
    rotateTowards(line().p2(), line_.p2());
  }

  QRectF r = rect();
  r.setSize(QSizeF(QLineF(line().p1(), line_.p2()).length(), r.height()));
  setViewRect(r);
}
Exemplo n.º 8
0
void QgsComposerItem::setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint, bool posIncludesFrame )
{
  double upperLeftX = x;
  double upperLeftY = y;

  //store the item position mode
  mLastUsedPositionMode = itemPoint;

  //adjust x-coordinate if placement is not done to a left point
  if ( itemPoint == UpperMiddle || itemPoint == Middle || itemPoint == LowerMiddle )
  {
    upperLeftX -= width / 2.0;
  }
  else if ( itemPoint == UpperRight || itemPoint == MiddleRight || itemPoint == LowerRight )
  {
    upperLeftX -= width;
  }

  //adjust y-coordinate if placement is not done to an upper point
  if ( itemPoint == MiddleLeft || itemPoint == Middle || itemPoint == MiddleRight )
  {
    upperLeftY -= height / 2.0;
  }
  else if ( itemPoint == LowerLeft || itemPoint == LowerMiddle || itemPoint == LowerRight )
  {
    upperLeftY -= height;
  }

  if ( posIncludesFrame )
  {
    //adjust position to account for frame size

    if ( mItemRotation == 0 )
    {
      upperLeftX += estimatedFrameBleed();
      upperLeftY += estimatedFrameBleed();
    }
    else
    {
      //adjust position for item rotation
      QLineF lineToItemOrigin = QLineF( 0, 0, estimatedFrameBleed(), estimatedFrameBleed() );
      lineToItemOrigin.setAngle( -45 - mItemRotation );
      upperLeftX += lineToItemOrigin.x2();
      upperLeftY += lineToItemOrigin.y2();
    }

    width -= 2 * estimatedFrameBleed();
    height -= 2 * estimatedFrameBleed();
  }

  setSceneRect( QRectF( upperLeftX, upperLeftY, width, height ) );
}
Exemplo n.º 9
0
QPointF LineObject::objectEndPoint2() const
{
    QLineF lyne = line();
    qreal rot = radians(rotation());
    qreal cosRot = qCos(rot);
    qreal sinRot = qSin(rot);
    qreal x2 = lyne.x2()*scale();
    qreal y2 = lyne.y2()*scale();
    qreal rotEnd2X = x2*cosRot - y2*sinRot;
    qreal rotEnd2Y = x2*sinRot + y2*cosRot;

    return (scenePos() + QPointF(rotEnd2X, rotEnd2Y));
}
Exemplo n.º 10
0
void CableItem::adjust() {
  QLineF line;
  boost::shared_ptr<ToolConnection> input = this->input.lock(), output = this->output.lock();
  if (input && output)
    line = QLineF(mapFromItem(input->parent, input->attachmentPos()), mapFromItem(output->parent, output->attachmentPos()));
  else if (input && dragPoint)
    line = QLineF(mapFromItem(input->parent, input->attachmentPos()), mapFromScene(*dragPoint));
  else if (dragPoint && output)
    line = QLineF(mapFromScene(*dragPoint), mapFromItem(output->parent, output->attachmentPos()));
  prepareGeometryChange();
  sourcePoint = line.p1();
  destPoint = line.p2();
}
Exemplo n.º 11
0
    void advance()
    {
        QPointF unit = (end - start)/sqrt(QPointF::dotProduct(end - start, end - start));
        currentPos += 4.*unit;

        QLineF l = line->line();
        l.setP1(currentPos + QPoint(GameScene::tile_width/2, GameScene::tile_height/2));
        line->setLine(l);

        if ((currentPos - end).manhattanLength() < 4.) {
            finished = true;
        }
    }
Exemplo n.º 12
0
bool Meshing::intersects(QPolygonF poly, QLineF line)
{
    for(int i=0;i<poly.size()-1;i++){
        QLineF l(poly.at(i) , poly.at(i+1));
        QPointF p;
        if(l.p1()==line.p1() || l.p1()==line.p2() || l.p2()==line.p1() || l.p2()==line.p2())
            continue;
        if(l.intersect(line,&p)==QLineF::BoundedIntersection){
                return true;
        }
    }
    return false;
}
Exemplo n.º 13
0
void PathLine::paint(QPainter *poPainter, const QStyleOptionGraphicsItem *poOption, QWidget *poWidget)
{
    poPainter->setPen(QPen(Qt::black, 2, Qt::SolidLine));
    poPainter->drawLine(line());
    if (isSelected()) {
        poPainter->setPen(QPen(Qt::gray, 1, Qt::DashLine));
        QLineF oLine = line();
        oLine.translate(0, 4.0);
        poPainter->drawLine(oLine);
        oLine.translate(0,-8.0);
        poPainter->drawLine(oLine);
    }
}
void CanvasMode_EditPolygon::mouseReleaseEvent(QMouseEvent *m)
{
	const FPoint mousePointDoc = m_canvas->globalToCanvas(m->globalPos());
	m_canvas->m_viewMode.m_MouseButtonPressed = false;
	m_canvas->resetRenderMode();
	m->accept();
	PageItem *currItem = m_doc->m_Selection->itemAt(0);
	PageItem_RegularPolygon* item = currItem->asRegularPolygon();
	QTransform itemMatrix = currItem->getTransform();
	if ((m_polygonPoint == useControlInner) || (m_polygonPoint == useControlOuter) || (m_polygonPoint == useControlInnerCurve) || (m_polygonPoint == useControlOuterCurve))
	{
		double newX = mousePointDoc.x();
		double newY = mousePointDoc.y();
		QPointF cPoint = itemMatrix.map(centerPoint);
		QLineF stLinA = QLineF(cPoint, QPointF(newX, newY));
		uint cx = polyUseFactor ? polyCorners * 2 : polyCorners;
		double seg = 360.0 / cx;
		double trueLength = sqrt(pow(sin(seg / 180.0 * M_PI) * (currItem->width() / 2.0), 2) + pow(cos(seg / 180.0 * M_PI) * (currItem->height() / 2.0) + (currItem->height()/2.0) - currItem->height(), 2));
		if (m_polygonPoint == useControlInner)
		{
			polyInnerRot = stLinA.angle() - 90 - polyRotation - seg;
			polyFactor = stLinA.length() / sqrt(pow(sin(stLinA.angle() * M_PI / 180.0) * currItem->height() / 2.0, 2) + pow(cos(stLinA.angle() * M_PI / 180.0) * currItem->width() / 2.0, 2));
		}
		if (m_polygonPoint == useControlOuter)
			polyRotation = stLinA.angle() - 90;
		if (m_polygonPoint == useControlInnerCurve)
		{
			QPointF ePoint = itemMatrix.map(endPoint);
			QLineF stLinC = QLineF(ePoint, QPointF(newX, newY));
			polyCurvature = stLinC.length() / trueLength;
		}
		if (m_polygonPoint == useControlOuterCurve)
		{
			QPointF sPoint = itemMatrix.map(startPoint);
			QPointF sPoint2 = itemMatrix.map(currItem->PoLine.pointQF(6));
			QLineF stLinCo = QLineF(sPoint, QPointF(newX, newY));
			QLineF stLinCo2 = QLineF(sPoint, sPoint2);
			polyOuterCurvature = stLinCo.length() / stLinCo2.length();
		}
		item->polyFactor = polyFactor;
		item->polyRotation = polyRotation;
		item->polyCurvature = polyCurvature;
		item->polyInnerRot = polyInnerRot;
		item->polyOuterCurvature = polyOuterCurvature;
		item->recalcPath();
		VectorDialog->setValues(polyCorners, polyFactor, polyUseFactor, polyRotation, polyCurvature, polyInnerRot, polyOuterCurvature);
		if (m_transaction)
		{
			m_transaction.commit();
			m_transaction.reset();
		}
	}
	QPainterPath path = itemMatrix.map(RegularPolygonPath(item->width(), item->height(), polyCorners, polyUseFactor, polyFactor, polyRotation, polyCurvature, polyInnerRot, polyOuterCurvature));
	m_doc->regionsChanged()->update(path.boundingRect().adjusted(-5, -5, 10, 10));
}
Exemplo n.º 15
0
QLineF GroupBox::positionToPoint(int pos) const
{
	QLineF result;
	foreach (const Box *box, boxes())
	{
		if (box->containsPos(pos))
		{
			result = box->positionToPoint(pos);
		}
	}
	if (!result.isNull())
		result.translate(x(), y());
	return result;
}
Exemplo n.º 16
0
QPointF LineObject::objectMidPoint() const
{
    QLineF lyne = line();
    QPointF mp = lyne.pointAt(0.5);
    qreal rot = radians(rotation());
    qreal cosRot = qCos(rot);
    qreal sinRot = qSin(rot);
    qreal mx = mp.x()*scale();
    qreal my = mp.y()*scale();
    qreal rotMidX = mx*cosRot - my*sinRot;
    qreal rotMidY = mx*sinRot + my*cosRot;

    return (scenePos() + QPointF(rotMidX, rotMidY));
}
Exemplo n.º 17
0
void MainWindow::drawLine(double rangle, int i,int n,int x2, int y2, double length, double angle, QGraphicsScene *scene)
{
    //base case, we recurse back out once this condition reached
    if (i == n)
        return;
    else{
        //create new line, based on previously drawn line
        QLineF q2;
        q2.setP1(QPointF(x2,y2));
        q2.setLength(length*.95);
        q2.setAngle(angle +rangle);
        scene->addLine(q2);
        QLineF q3;
        q3.setP1(QPointF(x2,y2));
        q3.setLength(length*.95);
        q3.setAngle(angle -rangle);
        scene->addLine(q3);
        QLineF q;
        q.setP1(QPointF(x2,y2));
        q.setLength(length*.95);
        q.setAngle(angle);
        scene->addLine(q);
        ui->graphicsView->setScene(scene);
        x2 = q.x2();
        y2 = q.y2();
        i = i+1;
        //call drawLine again, using new values
        drawLine(rangle, i,n,x2,y2,q.length(),q.angle(), scene);
    }
}
Exemplo n.º 18
0
Ray::Ray(qreal x, qreal y, qreal angle, qreal wavelength, const Orders & orders, const QPen & pen, QList<Reflector *> const & reflectors, QGraphicsItem * parent) :
    QGraphicsLineItem(x, y, x + 1, y, parent),
    m_recursionDepth(0),
    m_wavelength(wavelength),
    m_orders(orders),
    m_reflector(nullptr),
    m_reflectors(reflectors)
{
    QLineF l = line();
    l.setAngle(angle);
    setLine(l);
    setPen(pen);
    for(int i = 0; i < Orders::Size; ++i) m_rays[i] = nullptr;
}
Exemplo n.º 19
0
void GraphViewer::onSceneMouseRelease(QGraphicsSceneMouseEvent *mouseEvent)
{

    // need to put the connecting line on the background otherwise it is detected under the mouse
    _drawingEdge->setZValue(EDGE_LINE_Z);

    QGraphicsItem * item = _scene.itemAt(mouseEvent->scenePos(), QTransform());
    if (qgraphicsitem_cast<QGraphicsEllipseItem*>(item)
            || qgraphicsitem_cast<QGraphicsSimpleTextItem*>(item))
    {
        QGraphicsEllipseItem* vertex = qgraphicsitem_cast<QGraphicsEllipseItem*>(item->parentItem());
        if (!vertex)
            vertex = qgraphicsitem_cast<QGraphicsEllipseItem*>(item);

        if (vertex)
        {
            double x = _drawingEdge->transform().dx();
            double y = _drawingEdge->transform().dy();
            _drawingEdge->setLine(0.0,0.0, vertex->scenePos().x()-x, vertex->scenePos().y()-y);
            _drawingEdge->setZValue(EDGE_LINE_Z);

            if (_drawingEdge->data(KEY_EDGE_VERTEX1) == vertex->data(KEY_EDGE_VERTEX1))
                _scene.removeItem(_drawingEdge);
            else
            {
                int defaultWeight = 1;
                _drawingEdge->setData(KEY_EDGE_VERTEX2, vertex->data(KEY_VERTEX_ID));
                _drawingEdge->setData(KEY_EDGE_WEIGHT, defaultWeight);
                // draw edge weight
                QGraphicsSimpleTextItem * edgeWeight = _scene.addSimpleText(QString("%1").arg(defaultWeight));
                edgeWeight->setParentItem(_drawingEdge);
                edgeWeight->setBrush(Qt::blue);
                QLineF line = _drawingEdge->line();
                edgeWeight->setTransform(
                            QTransform::fromScale(0.005, 0.005)
                            * QTransform::fromTranslate(line.x2()*0.5, line.y2()*0.5)
                            );
                edgeWeight->setZValue(EDGE_TEXT_Z);
                // add to graph edges
                _edges << _drawingEdge;
            }
        }
    }
    else
    {
        _scene.removeItem(_drawingEdge);
    }
    _isDrawingEdge=false;
    _drawingEdge=0;
}
Exemplo n.º 20
0
static QLineF preciselyMap( const QTransform& transform, const QLineF& line )
{
    qreal fx1 = line.x1();
    qreal fy1 = line.y1();
    qreal fx2 = line.x2();
    qreal fy2 = line.y2();

    qreal x1 = transform.m11() * fx1 + transform.m21() * fy1 + transform.dx();
    qreal y1 = transform.m12() * fx1 + transform.m22() * fy1 + transform.dy();
    qreal x2 = transform.m11() * fx2 + transform.m21() * fy2 + transform.dx();
    qreal y2 = transform.m12() * fx2 + transform.m22() * fy2 + transform.dy();

    return QLineF( x1, y1, x2, y2 );
}
Exemplo n.º 21
0
void QgsComposerItem::refreshRotation( const bool updateItem, const bool adjustPosition, const QgsExpressionContext& context )
{
  double rotation = mItemRotation;

  //data defined rotation set?
  QVariant exprVal;
  if ( dataDefinedEvaluate( QgsComposerObject::ItemRotation, exprVal, context ) )
  {
    bool ok;
    double rotD = exprVal.toDouble( &ok );
    QgsDebugMsg( QString( "exprVal Rotation:%1" ).arg( rotD ) );
    if ( ok && !exprVal.isNull() )
    {
      rotation = rotD;
    }
  }

  if ( qgsDoubleNear( rotation, mEvaluatedItemRotation ) )
  {
    return;
  }

  if ( adjustPosition )
  {
    //adjustPosition set, so shift the position of the item so that rotation occurs around item center
    //create a line from the centrepoint of the rect() to its origin, in scene coordinates
    QLineF refLine = QLineF( mapToScene( QPointF( rect().width() / 2.0, rect().height() / 2.0 ) ), mapToScene( QPointF( 0, 0 ) ) );
    //rotate this line by the current rotation angle
    refLine.setAngle( refLine.angle() - rotation + mEvaluatedItemRotation );
    //get new end point of line - this is the new item position
    QPointF rotatedReferencePoint = refLine.p2();
    setPos( rotatedReferencePoint );
    emit sizeChanged();
  }

  setTransformOriginPoint( 0, 0 );
  QGraphicsItem::setRotation( rotation );

  mEvaluatedItemRotation = rotation;

  emit itemRotationChanged( rotation );

  //update bounds of scene, since rotation may affect this
  mComposition->updateBounds();

  if ( updateItem )
  {
    update();
  }
}
Exemplo n.º 22
0
void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
                  QWidget *)
{
    if(!myStartItem || !myEndItem) {
        return;
    }

    if(myEndItem->sceneBoundingRect().contains(p1())) {
        return;
    }

    QPen myPen = pen();
    myPen.setColor(myColor);
    qreal arrowSize = 5;
    painter->setPen(myPen);
    painter->setBrush(myColor);

    QLineF centerLine(p1(), p2());
    QPolygonF endPolygon = myEndItem->mapToScene(myEndItem->shape()).toFillPolygon();
    QPointF p1 = endPolygon.first();
    QPointF p2;
    QPointF intersectPoint;
    QLineF polyLine;
    for (int i = 1; i < endPolygon.count(); ++i) {
        p2 = endPolygon.at(i);
        polyLine = QLineF(p1, p2);
        QLineF::IntersectType intersectType =
                polyLine.intersect(centerLine, &intersectPoint);
        if (intersectType == QLineF::BoundedIntersection)
            break;
        p1 = p2;
    }

    setLine(QLineF(intersectPoint, this->p1()));

    double angle = ::acos(line().dx() / line().length());
    if (line().dy() >= 0)
        angle = (Pi * 2) - angle;

    QPointF arrowP1 = line().p1() + QPointF(sin(angle + Pi / 3) * arrowSize,
                                            cos(angle + Pi / 3) * arrowSize);
    QPointF arrowP2 = line().p1() + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
                                            cos(angle + Pi - Pi / 3) * arrowSize);

    arrowHead.clear();
    arrowHead << line().p1() << arrowP1 << arrowP2;
    painter->drawLine(line());
    painter->drawPolygon(arrowHead);
}
Exemplo n.º 23
0
bool
AbstractGraphicsItem::resizeZone( const QPointF& position )
{
    // Get the current transformation of the view and invert it.
    QTransform transform = tracksView()->transform().inverted();
    // Map the RESIZE_ZONE distance from the view to the item coordinates.
    QLineF line = transform.map( QLineF( 0, 0, RESIZE_ZONE, 0 ) );

    if ( position.x() < line.x2() ||
         position.x() > ( boundingRect().width() - line.x2() ) )
    {
        return true;
    }
    return false;
}
Exemplo n.º 24
0
//This function handles the special case of an edge that connects a node
//to itself where the node graphics item has only one line segment.
void GraphicsItemEdge::makeSpecialPathConnectingNodeToSelf()
{
    QLineF nodeLine(m_startingLocation, m_endingLocation);
    QLineF normalLine = nodeLine.normalVector();
    QPointF perpendicularShift = normalLine.p2() - normalLine.p1();
    QPointF nodeMidPoint = (m_startingLocation + m_endingLocation) / 2.0;

    QPainterPath path;
    path.moveTo(m_startingLocation);

    path.cubicTo(m_controlPoint1, m_controlPoint1 + perpendicularShift, nodeMidPoint + perpendicularShift);
    path.cubicTo(m_controlPoint2 + perpendicularShift, m_controlPoint2, m_endingLocation);

    setPath(path);
}
Exemplo n.º 25
0
void PSV_ChartItem::updateCurrentMes(const QPointF &point)
{
    QLineF vLine = m_vLineItem->line();
    vLine.setP1(QPointF(point.x(),vLine.y1()));
    vLine.setP2(QPointF(point.x(),vLine.y2()));
    m_vLineItem->setLine(vLine);

    QLineF hLine = m_hLineItem->line();
    hLine.setP1(QPointF(hLine.x1(),point.y()));
    hLine.setP2(QPointF(hLine.x2(),point.y()));
    m_hLineItem->setLine(hLine);

    m_currentMesItem->setHtml(getCurrentHtmMes(point.x()));
    m_currentMesItem->setPos(point.x(),point.y()+10);
}
Exemplo n.º 26
0
/**
 * This method aligns both the \b "start" and \b "end" symbols to
 * the current angles of the \b "first" and the \b "last" line
 * segment respectively.
 */
void AssociationLine::alignSymbols()
{
    const int sz = m_points.size();
    if (sz < 2) {
        // cannot align if there is no line (one line = 2 points)
        return;
    }

    QList<QPolygonF> polygons = path().toSubpathPolygons();

    if (m_startSymbol) {
        QPolygonF firstLine = polygons.first();
        QLineF segment(firstLine.at(1), firstLine.at(0));
        m_startSymbol->alignTo(segment);
    }

    if (m_endSymbol) {
        QPolygonF lastLine = polygons.last();
        int maxIndex = lastLine.size();
        QLineF segment(lastLine.at(maxIndex-2), lastLine.at(maxIndex-1));
        m_endSymbol->alignTo(segment);
    }

    if (m_subsetSymbol) {
        QPointF p1 = path().pointAtPercent(0.4);
        QPointF p2 = path().pointAtPercent(0.5);
        QLineF segment(p1, p2);
        m_subsetSymbol->alignTo(segment);
    }

    if (m_collaborationLineItem) {
        const qreal distance = 10;
        const int midSegmentIndex = (sz - 1) / 2;

        const QPointF a = m_points.at(midSegmentIndex);
        const QPointF b = m_points.at(midSegmentIndex + 1);

        if (a == b)
            return;

        const QPointF p1 = (a + b) / 2.0;
        const QPointF p2 = (p1 + b) / 2.0;

        // Reversed line as we want normal in opposite direction.
        QLineF segment(p2, p1);
        QLineF normal = segment.normalVector().unitVector();
        normal.setLength(distance);

        QLineF actualLine;
        actualLine.setP2(normal.p2());

        normal.translate(p1 - p2);
        actualLine.setP1(normal.p2());

        m_collaborationLineItem->setLine(actualLine);
        m_collaborationLineHead->alignTo(actualLine);
    }
}
Exemplo n.º 27
0
/*!
    Show the snap line. fade-in animation is started on the line if the line is positioned at a different place.
    Before starting the fade-in animation, the fade-out animation is stoped if it is running.
*/
void HsSnapLine::showLine(const QLineF &snapLine)
{
    QLineF displayLine = snapLine;
    qreal angle = displayLine.angle();
    if (qAbs(angle) == 0.0 || qAbs(angle) == 180.0) { //this is a horizontal line
        //adding 1 is required below, as the line is 3 pixels wide and is translated by 1 point before displaying
        if (displayLine.y1() != (line().y1()+1.0) ) { //this horizontal line is at new position horizontally
            if (isFadeOutAnimationRunning()) { //if fade-out animation is running, stop it, animation is running at old position
                stopFadeOutAnimation();
            }
            //start fade-in animation at new position.
            startFadeInAnimation();
        }
        else { //this horizontal line is at the old position horizontally
            if (isFadeOutAnimationRunning()) { //if fade-out animation is running, stop it, animation is running at old position
                stopFadeOutAnimation();
                //start fade-in animation at the old position
                startFadeInAnimation();
            }
        }
        displayLine.translate(0.0, -1.0);
    }
    if (qAbs(angle) == 90.0 || qAbs(angle) == 270.0) { //this is a vertical line
        if (displayLine.x1() != (line().x1()+1)) { //this Vertical line is at different position vertically
            if (isFadeOutAnimationRunning()) {
                stopFadeOutAnimation();
            }
            startFadeInAnimation();
        }
        else {
            if (isFadeOutAnimationRunning()) {
                stopFadeOutAnimation();
                startFadeInAnimation();
            }
        }
        displayLine.translate(-1.0, 0.0);
    }

    QLinearGradient gradient(displayLine.p1(), displayLine.p2());
    gradient.setColorAt(0.0, Qt::white);
    QColor snapLineColor = HbColorScheme::color("qtc_hs_snapguide");
    if (!snapLineColor.isValid()) {
        //if valid color is not loaded from the theme, the darkCyan color is used as a backup.color
        snapLineColor = Qt::darkCyan;
    }
    gradient.setColorAt(0.4, snapLineColor);
    gradient.setColorAt(0.6, snapLineColor);
    gradient.setColorAt(1.0, Qt::white);
    QBrush brush(gradient);
    QPen pen;
    pen.setWidth(3);
    pen.setCapStyle(Qt::RoundCap);
    pen.setBrush(brush);
    setPen(pen);

    setLine(displayLine);
    show();
}
Exemplo n.º 28
0
/*!
  \fn qreal QLineF::angleTo(const QLineF &line) const

  \since 4.4

  Returns the angle (in degrees) from this line to the given \a
  line, taking the direction of the lines into account. If the lines
  do not intersect within their range, it is the intersection point of
  the extended lines that serves as origin (see
  QLineF::UnboundedIntersection).

  The returned value represents the number of degrees you need to add
  to this line to make it have the same angle as the given \a line,
  going counter-clockwise.

  \sa intersect()
*/
qreal QLineF::angleTo(const QLineF &l) const
{
    if (isNull() || l.isNull())
        return 0;

    const qreal a1 = angle();
    const qreal a2 = l.angle();

    const qreal delta = a2 - a1;
    const qreal delta_normalized = delta < 0 ? delta + 360 : delta;

    if (qFuzzyCompare(delta, qreal(360)))
        return 0;
    else
        return delta_normalized;
}
Exemplo n.º 29
0
QList<QPointF> Geometry::intersection(QLineF const &line, QPainterPath const &path, qreal eps)
{
	QList<QPointF> result;
	QPointF startPoint;
	QPointF endPoint;

	for (int i = 0; i < path.elementCount(); ++i) {
		QPainterPath::Element const element = path.elementAt(i);

		// Checking that element belongs to the wall path
		if (element.isMoveTo()) {
			endPoint = QPointF(element.x, element.y);
			continue;
		}

		startPoint = endPoint;
		endPoint = QPointF(element.x, element.y);
		QLineF currentLine(startPoint, endPoint);
		QPointF intersectionPoint;
		// TODO: consider curve cases
		if (line.intersect(currentLine, &intersectionPoint) != QLineF::NoIntersection
				&& belongs(intersectionPoint, currentLine, eps))
		{
			result << intersectionPoint;
		}
	}

	return result;
}
Exemplo n.º 30
0
void Edge::adjust()
{
    if (!source_ || !target_)
        return;

	const QRectF source_rect(source_->boundingRect());
	const QRectF target_rect(target_->boundingRect());

    const QLineF line(
		mapFromItem(source_, source_rect.center()), 
		mapFromItem(target_, target_rect.center()));
       
    prepareGeometryChange();
    source_point_ = line.p1();
    target_point_ = line.p2();
}