void CanvasMode_EditArc::applyValues(double start, double end, double height, double width) { PageItem *currItem = m_doc->m_Selection->itemAt(0); PageItem_Arc *item = currItem->asArc(); QPointF mPoint = item->PoLine.pointQF(0); QRectF upRect = QRectF(QPointF(0, 0), QPointF(currItem->width(), currItem->height())).normalized(); QRectF upRect2 = QRectF(mPoint.x() - item->arcWidth / 2.0, mPoint.y() - item->arcHeight / 2.0, item->arcWidth, item->arcHeight); upRect = upRect2.united(upRect); upRect.translate(currItem->xPos(), currItem->yPos()); QTransform bb; bb.scale(height / width, 1.0); QLineF inp = QLineF(QPointF(width / 2.0, height / 2.0), QPointF(width, height / 2.0)); inp.setAngle(start); QLineF res = bb.map(inp); inp.setAngle(end); QLineF ena = bb.map(inp); startAngle = res.angle(); endAngle = ena.angle(); double nSweep = endAngle - startAngle; if (nSweep < 0) nSweep += 360; QPainterPath pp; pp.moveTo(mPoint); pp.arcTo(QRectF(mPoint.x() - width / 2.0, mPoint.y() - height / 2.0, width, height), startAngle, nSweep); pp.closeSubpath(); currItem->PoLine.fromQPainterPath(pp); m_doc->AdjustItemSize(currItem); if(UndoManager::undoEnabled()) { ScItemState<QPair<FPointArray, FPointArray> > *ss = new ScItemState<QPair<FPointArray, FPointArray> >(Um::EditArc,"",Um::IPolygon); FPointArray old = item->PoLine; ss->set("ARC","arc"); ss->set("OLD_WIDTH",item->arcWidth); ss->set("NEW_WIDTH",width); ss->set("OLD_XPOS",item->xPos()); ss->set("OLD_YPOS",item->yPos()); ss->set("OLD_HEIGHT",item->arcHeight); ss->set("NEW_HEIGHT",height); ss->set("OLD_START",item->arcStartAngle); ss->set("NEW_START",startAngle); ss->set("OLD_SWEEP",item->arcSweepAngle); ss->set("NEW_SWEEP",nSweep); ss->setItem(qMakePair(old,item->PoLine)); ss->set("NEW_XPOS",item->xPos()); ss->set("NEW_YPOS",item->yPos()); undoManager->action(currItem,ss); } item->arcStartAngle = startAngle; item->arcSweepAngle = nSweep; item->arcWidth = width; item->arcHeight = height; startPoint = currItem->PoLine.pointQF(2); endPoint = currItem->PoLine.pointQF(currItem->PoLine.size() - 4); centerPoint = currItem->PoLine.pointQF(0); widthPoint = QPointF(centerPoint.x() - item->arcWidth / 2.0, centerPoint.y()); heightPoint = QPointF(centerPoint.x(), centerPoint.y() - item->arcHeight / 2.0); QTransform itemMatrix = currItem->getTransform(); m_doc->regionsChanged()->update(itemMatrix.mapRect(QRectF(0, 0, currItem->width(), currItem->height())).adjusted(-currItem->width() / 2.0, -currItem->height() / 2.0, currItem->width(), currItem->height())); }
void QgsComposerItem::setItemRotation( double r, bool adjustPosition ) { 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() - r + mItemRotation ); //get new end point of line - this is the new item position QPointF rotatedReferencePoint = refLine.p2(); setPos( rotatedReferencePoint ); emit sizeChanged(); } if ( r > 360 ) { mItemRotation = (( int )r ) % 360; } else { mItemRotation = r; } setTransformOriginPoint( 0, 0 ); QGraphicsItem::setRotation( mItemRotation ); emit itemRotationChanged( r ); update(); }
void CanvasMode_EditPolygon::updateFromItem() { if (updateFromItemBlocked()) return; PageItem *currItem = m_doc->m_Selection->itemAt(0); PageItem_RegularPolygon* item = currItem->asRegularPolygon(); centerPoint = QPointF(currItem->width() / 2.0, currItem->height() / 2.0); startPoint = currItem->PoLine.pointQF(0); endPoint = currItem->PoLine.pointQF(2); polyCorners = item->polyCorners; polyUseFactor = item->polyUseFactor; polyFactor = item->polyFactor; polyRotation = item->polyRotation; polyCurvature = item->polyCurvature; polyInnerRot = item->polyInnerRot; polyOuterCurvature = item->polyOuterCurvature; VectorDialog->polyWidget->blockSignals(true); VectorDialog->setValues(polyCorners, polyFactor, polyUseFactor, polyRotation, polyCurvature, polyInnerRot, polyOuterCurvature); VectorDialog->polyWidget->blockSignals(false); uint cx = polyUseFactor ? polyCorners * 2 : polyCorners; double seg = 360.0 / cx; double trueLength = sqrt(pow(sin(seg / 180.0 * M_PI) * (item->width() / 2.0), 2) + pow(cos(seg / 180.0 * M_PI) * (item->height() / 2.0) + (item->height()/2.0) - item->height(), 2)); QLineF innerLine = QLineF(endPoint, centerPoint); innerLine.setAngle(innerLine.angle() + 90); innerLine.setLength(trueLength * polyCurvature); innerCPoint = innerLine.p2(); QLineF outerLine = QLineF(startPoint, currItem->PoLine.pointQF(6)); outerLine.setLength(outerLine.length() * polyOuterCurvature); outerCPoint = outerLine.p2(); m_view->update(); }
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); } }
void UBEditableGraphicsLineItem::forcePointPosition(const QPointF& pos, PointPosition pointPosition, int amplitude) { QLineF line; int angles[] = {0, 45, 90, 135, 180, 225, 270, 315}; int size = sizeof(angles) / sizeof(int); if(pointPosition == Start) { line.setP1(pos); line.setP2(path().elementAt(1)); } else { line.setP1(path().elementAt(0)); line.setP2(pos); } int angle = line.angle(); const float PI_2 = 4*atan(1.f)*2; //for each angle we compute the left and right angle //then compute the distance between both for(int i = 0; i < size; i++) { //use the modulo operator to force the angle to stay in [0, 360] int leftAmplitude = (angles[i] + amplitude) % 360; int rightAmplitude = (angles[i] - amplitude + 360) % 360; int leftDist = (leftAmplitude - angle + 360) % 360; int rightDist = (angle - rightAmplitude + 360) % 360; if(leftDist <= amplitude || rightDist <= amplitude) { if(pointPosition == End) { line.setAngle(angles[i]); } else { //compute the position of p1 by hand float angleInRadians = angles[i]*PI_2/360; qreal l = line.length(); const qreal dx = -cos(angleInRadians)*l; const qreal dy = sin(angleInRadians)*l; line.setP1(QPointF(dx + line.p2().x(), dy + line.p2().y())); } break; } } QPainterPath p; p.moveTo(line.p1()); p.lineTo(line.p2()); setPath(p); mHandles.at(0)->setPos(line.p1().x(), line.p1().y()); mHandles.at(1)->setPos(line.p2().x(), line.p2().y()); }
void QgsComposerItem::setItemPosition( double x, double y, double width, double height, ItemPositionMode itemPoint, bool posIncludesFrame, int page ) { double upperLeftX = x; double upperLeftY = y; if ( page > 0 ) { double h = composition()->paperHeight() + composition()->spaceBetweenPages(); upperLeftY += ( page - 1 ) * h; } //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 ) ); }
void BPoint::setH2(QPointF handle2) { h2 = handle2; if (handlesLinked) { qreal angle = QLineF(h2, p).angle(); QLineF l = QLineF(p, h1); l.setAngle(angle); h1 = l.p2(); } }
void BPoint::setH1(QPointF handle1) { h1 = handle1; if (handlesLinked) { qreal angle = QLineF(h1, p).angle(); QLineF l = QLineF(p, h2); l.setAngle(angle); h2 = l.p2(); } }
void CanvasMode_EditArc::applyValues(double start, double end, double height, double width) { PageItem *currItem = m_doc->m_Selection->itemAt(0); PageItem_Arc *item = currItem->asArc(); QPointF mPoint = item->PoLine.pointQF(0); QRectF upRect = QRectF(QPointF(0, 0), QPointF(currItem->width(), currItem->height())).normalized(); QRectF upRect2 = QRectF(mPoint.x() - item->arcWidth / 2.0, mPoint.y() - item->arcHeight / 2.0, item->arcWidth, item->arcHeight); upRect = upRect2.united(upRect); upRect.translate(currItem->xPos(), currItem->yPos()); item->arcWidth = width; item->arcHeight = height; QTransform bb; bb.scale(item->arcHeight / item->arcWidth, 1.0); QLineF inp = QLineF(QPointF(width / 2.0, height / 2.0), QPointF(width, height / 2.0)); inp.setAngle(start); QLineF res = bb.map(inp); inp.setAngle(end); QLineF ena = bb.map(inp); startAngle = res.angle(); endAngle = ena.angle(); double nSweep = endAngle - startAngle; if (nSweep < 0) nSweep += 360; QPainterPath pp; pp.moveTo(mPoint); pp.arcTo(QRectF(mPoint.x() - item->arcWidth / 2.0, mPoint.y() - item->arcHeight / 2.0, item->arcWidth, item->arcHeight), startAngle, nSweep); pp.closeSubpath(); currItem->PoLine.fromQPainterPath(pp); m_doc->AdjustItemSize(currItem); item->arcStartAngle = startAngle; item->arcSweepAngle = endAngle - startAngle; startPoint = currItem->PoLine.pointQF(2); endPoint = currItem->PoLine.pointQF(currItem->PoLine.size() - 4); centerPoint = currItem->PoLine.pointQF(0); widthPoint = QPointF(centerPoint.x() - item->arcWidth / 2.0, centerPoint.y()); heightPoint = QPointF(centerPoint.x(), centerPoint.y() - item->arcHeight / 2.0); m_doc->regionsChanged()->update(upRect.adjusted(-10.0 - currItem->width() / 2.0, -10.0 - currItem->height() / 2.0, 10.0 + currItem->width() / 2.0, 10.0 + currItem->height() / 2.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; }
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(); } }
void ArcItem::updateArrowPath(){ QPainterPath path; if((_endItem && _startItem->primaryShape().intersects(_endItem->primaryShape())) || (!_endItem && _startItem->primaryShape().contains(_end))){ _cachedArrowPath = path; return; } QPointF start(0,0), point = _end - pos(); //The arrow line and reverse liune QLineF revline(point, start); //Compute various points QLineF s = revline.normalVector(); s.setAngle(revline.angle() - 45); s.setLength(ARROW_SIZE); QPointF side1 = s.p2(); s = revline.normalVector(); s.setAngle(revline.angle() + 45); s.setLength(ARROW_SIZE); QPointF side2 = s.p2(); s = QLineF(side1, side2); QPointF head = point; s.intersect(revline, &head); path.moveTo(start); path.lineTo(head); path.lineTo(side1); path.lineTo(point); path.lineTo(side2); path.lineTo(head); _cachedArrowPath = path; }
void CanvasMode_EditPolygon::activate(bool fromGesture) { m_polygonPoint = noPointDefined; m_canvas->m_viewMode.m_MouseButtonPressed = false; m_canvas->resetRenderMode(); m_doc->DragP = false; m_doc->leaveDrag = false; m_canvas->m_viewMode.operItemMoving = false; m_canvas->m_viewMode.operItemResizing = false; m_view->MidButt = false; Mxp = Myp = -1; PageItem *currItem = m_doc->m_Selection->itemAt(0); PageItem_RegularPolygon* item = currItem->asRegularPolygon(); centerPoint = QPointF(currItem->width() / 2.0, currItem->height() / 2.0); startPoint = currItem->PoLine.pointQF(0); endPoint = currItem->PoLine.pointQF(2); polyCorners = item->polyCorners; polyUseFactor = item->polyUseFactor; polyFactor = item->polyFactor; polyRotation = item->polyRotation; polyCurvature = item->polyCurvature; polyInnerRot = item->polyInnerRot; polyOuterCurvature = item->polyOuterCurvature; VectorDialog = new PolyVectorDialog(m_ScMW, polyCorners, polyFactor, polyUseFactor, polyRotation, polyCurvature, polyInnerRot, polyOuterCurvature); VectorDialog->show(); uint cx = polyUseFactor ? polyCorners * 2 : polyCorners; double seg = 360.0 / cx; double trueLength = sqrt(pow(sin(seg / 180.0 * M_PI) * (item->width() / 2.0), 2) + pow(cos(seg / 180.0 * M_PI) * (item->height() / 2.0) + (item->height()/2.0) - item->height(), 2)); QLineF innerLine = QLineF(endPoint, centerPoint); innerLine.setAngle(innerLine.angle() + 90); innerLine.setLength(trueLength * polyCurvature); innerCPoint = innerLine.p2(); QLineF outerLine = QLineF(startPoint, currItem->PoLine.pointQF(6)); outerLine.setLength(outerLine.length() * polyOuterCurvature); outerCPoint = outerLine.p2(); setModeCursor(); if (fromGesture) m_view->update(); connect(m_doc, SIGNAL(docChanged()), this, SLOT(updateFromItem())); connect(VectorDialog, SIGNAL(NewVectors(int, double, bool, double, double, double, double)), this, SLOT(applyValues(int, double, bool, double, double, double, double))); connect(VectorDialog, SIGNAL(endEdit()), this, SLOT(endEditing())); connect(VectorDialog, SIGNAL(paletteShown(bool)), this, SLOT(endEditing(bool))); }
QVector<QPointF> RoundedRectItem::calculatePoints(QPointF start_pnt, double start_angle, double end_angle) { QVector<QPointF> points; QLineF lin; double inc=(start_angle > end_angle ? -10 : 10), ang=start_angle; bool end=false; while(!end) { lin.setP1(start_pnt); lin.setLength(radius); lin.setAngle(ang); points.append(lin.p2()); ang+=inc; end=((inc > 0 && ang > end_angle) || (inc < 0 && ang < end_angle)); } return(points); }
void QgsComposerView::updateRubberBandLine( const QPointF &pos, const bool constrainAngles ) { if ( !mRubberBandLineItem ) { return; } //snap to grid QPointF snappedScenePoint = composition()->snapPointToGrid( pos ); QLineF newLine = QLineF( mRubberBandStartPos, snappedScenePoint ); if ( constrainAngles ) { //movement is contrained to 45 degree angles double angle = QgsComposerUtils::snappedAngle( newLine.angle() ); newLine.setAngle( angle ); } mRubberBandLineItem->setLine( newLine ); }
void CanvasMode_EditArc::mouseReleaseEvent(QMouseEvent *m) { m_canvas->m_viewMode.m_MouseButtonPressed = false; m_canvas->resetRenderMode(); m->accept(); PageItem *currItem = m_doc->m_Selection->itemAt(0); PageItem_Arc* item = currItem->asArc(); QPointF mPoint = item->PoLine.pointQF(0); if ((m_arcPoint == useControlStart) || (m_arcPoint == useControlSweep) || (m_arcPoint == useControlHeight) || (m_arcPoint == useControlWidth)) { QTransform bb; bb.scale(item->arcHeight / item->arcWidth, 1.0); QLineF inp = QLineF(QPointF(item->arcWidth / 2.0, item->arcHeight / 2.0), QPointF(item->arcWidth, item->arcHeight / 2.0)); double start = inp.angleTo(QLineF(QPointF(item->arcWidth / 2.0, item->arcHeight / 2.0),startPoint)); inp.setAngle(start); double end = inp.angleTo(QLineF(QPointF(item->arcWidth / 2.0, item->arcHeight / 2.0),endPoint)); double nWidth = mPoint.x() - widthPoint.x(); double nHeight = mPoint.y() - heightPoint.y(); applyValues(start,end + start, 2.0 * nHeight, 2.0 * nWidth); } QTransform itemMatrix = currItem->getTransform(); m_doc->regionsChanged()->update(itemMatrix.mapRect(QRectF(0, 0, currItem->width(), currItem->height())).adjusted(-currItem->width() / 2.0, -currItem->height() / 2.0, currItem->width(), currItem->height())); }
double CanvasMode_EditSpiral::computeRealAngle(double angle, bool fromDia) { PageItem *currItem = m_doc->m_Selection->itemAt(0); double ret = angle; int rev = static_cast<int>(angle / 360.0); double part = angle - (rev * 360); QTransform bb; bb.scale(currItem->width() / currItem->height(), 1.0); QLineF inp = QLineF(QPointF(currItem->width() / 2.0, currItem->height() / 2.0), QPointF(currItem->width(), currItem->height() / 2.0)); inp.setAngle(part); if (fromDia) { QLineF res = bb.map(inp); ret = res.angle(); } else { QTransform bt = bb.inverted(); QLineF res = bt.map(inp); ret = res.angle(); } ret += rev * 360; return ret; }
void PolygonObject::updateRubber(QPainter* painter) { int rubberMode = objectRubberMode(); if(rubberMode == OBJ_RUBBER_POLYGON) { setObjectPos(objectRubberPoint("POLYGON_POINT_0")); bool ok = false; QString numStr = objectRubberText("POLYGON_NUM_POINTS"); if(numStr.isNull()) return; int num = numStr.toInt(&ok); if(!ok) return; QString appendStr; QPainterPath rubberPath; rubberPath.moveTo(mapFromScene(objectRubberPoint("POLYGON_POINT_0"))); for(int i = 1; i <= num; i++) { appendStr = "POLYGON_POINT_" + QString().setNum(i); QPointF appendPoint = mapFromScene(objectRubberPoint(appendStr)); rubberPath.lineTo(appendPoint); } //rubberPath.lineTo(0,0); updatePath(rubberPath); //Ensure the path isn't updated until the number of points is changed again setObjectRubberText("POLYGON_NUM_POINTS", QString()); } else if(rubberMode == OBJ_RUBBER_POLYGON_INSCRIBE) { setObjectPos(objectRubberPoint("POLYGON_CENTER")); quint16 numSides = objectRubberPoint("POLYGON_NUM_SIDES").x(); QPointF inscribePoint = mapFromScene(objectRubberPoint("POLYGON_INSCRIBE_POINT")); QLineF inscribeLine = QLineF(QPointF(0,0), inscribePoint); qreal inscribeAngle = inscribeLine.angle(); qreal inscribeInc = 360.0/numSides; if(painter) drawRubberLine(inscribeLine, painter, VIEW_COLOR_CROSSHAIR); QPainterPath inscribePath; //First Point inscribePath.moveTo(inscribePoint); //Remaining Points for(int i = 1; i < numSides; i++) { inscribeLine.setAngle(inscribeAngle + inscribeInc*i); inscribePath.lineTo(inscribeLine.p2()); } updatePath(inscribePath); } else if(rubberMode == OBJ_RUBBER_POLYGON_CIRCUMSCRIBE) { setObjectPos(objectRubberPoint("POLYGON_CENTER")); quint16 numSides = objectRubberPoint("POLYGON_NUM_SIDES").x(); QPointF circumscribePoint = mapFromScene(objectRubberPoint("POLYGON_CIRCUMSCRIBE_POINT")); QLineF circumscribeLine = QLineF(QPointF(0,0), circumscribePoint); qreal circumscribeAngle = circumscribeLine.angle(); qreal circumscribeInc = 360.0/numSides; if(painter) drawRubberLine(circumscribeLine, painter, VIEW_COLOR_CROSSHAIR); QPainterPath circumscribePath; //First Point QLineF prev(circumscribeLine.p2(), QPointF(0,0)); prev = prev.normalVector(); circumscribeLine.setAngle(circumscribeAngle + circumscribeInc); QLineF perp(circumscribeLine.p2(), QPointF(0,0)); perp = perp.normalVector(); QPointF iPoint; perp.intersect(prev, &iPoint); circumscribePath.moveTo(iPoint); //Remaining Points for(int i = 2; i <= numSides; i++) { prev = perp; circumscribeLine.setAngle(circumscribeAngle + circumscribeInc*i); perp = QLineF(circumscribeLine.p2(), QPointF(0,0)); perp = perp.normalVector(); perp.intersect(prev, &iPoint); circumscribePath.lineTo(iPoint); } updatePath(circumscribePath); } else if(rubberMode == OBJ_RUBBER_GRIP) { if(painter) { int elemCount = normalPath.elementCount(); QPointF gripPoint = objectRubberPoint("GRIP_POINT"); if(gripIndex == -1) gripIndex = findIndex(gripPoint); if(gripIndex == -1) return; int m = 0; int n = 0; if(!gripIndex) { m = elemCount-1; n = 1; } else if(gripIndex == elemCount-1) { m = elemCount-2; n = 0; } else { m = gripIndex-1; n = gripIndex+1; } QPainterPath::Element em = normalPath.elementAt(m); QPainterPath::Element en = normalPath.elementAt(n); QPointF emPoint = QPointF(em.x, em.y); QPointF enPoint = QPointF(en.x, en.y); painter->drawLine(emPoint, mapFromScene(objectRubberPoint(QString()))); painter->drawLine(enPoint, mapFromScene(objectRubberPoint(QString()))); QLineF rubLine(mapFromScene(gripPoint), mapFromScene(objectRubberPoint(QString()))); drawRubberLine(rubLine, painter, VIEW_COLOR_CROSSHAIR); } } }
void CanvasMode_EditPolygon::mouseMoveEvent(QMouseEvent *m) { const FPoint mousePointDoc = m_canvas->globalToCanvas(m->globalPos()); m->accept(); double newX = mousePointDoc.x(); double newY = mousePointDoc.y(); if (m_canvas->m_viewMode.m_MouseButtonPressed && m_view->moveTimerElapsed()) { PageItem *currItem = m_doc->m_Selection->itemAt(0); QTransform itemMatrix = currItem->getTransform(); 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; double factor = 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)); int maxF = qRound(getUserValFromFactor(factor)); if (maxF <= 100) polyFactor = factor; } if (m_polygonPoint == useControlOuter) { polyRotation = stLinA.angle() - 90; if (polyRotation < -180) polyRotation += 360; if (polyRotation > 180) polyRotation -= 360; } 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(); } QPainterPath path = RegularPolygonPath(currItem->width(), currItem->height(), polyCorners, polyUseFactor, polyFactor, polyRotation, polyCurvature, polyInnerRot, polyOuterCurvature); FPointArray ar; ar.fromQPainterPath(path); endPoint = ar.pointQF(2); startPoint = ar.pointQF(0); QLineF innerLine = QLineF(endPoint, centerPoint); innerLine.setAngle(innerLine.angle() + 90); innerLine.setLength(trueLength * polyCurvature); innerCPoint = innerLine.p2(); QLineF outerLine = QLineF(startPoint, ar.pointQF(6)); outerLine.setLength(outerLine.length() * polyOuterCurvature); outerCPoint = outerLine.p2(); VectorDialog->setValues(polyCorners, polyFactor, polyUseFactor, polyRotation, polyCurvature, polyInnerRot, polyOuterCurvature); blockUpdateFromItem(true); currItem->update(); blockUpdateFromItem(false); path = itemMatrix.map(path); m_doc->regionsChanged()->update(path.boundingRect().adjusted(-5, -5, 10, 10)); } Mxp = newX; Myp = newY; }
void CanvasMode_EditArc::applyValues(double start, double end, double height, double width) { PageItem *currItem = m_doc->m_Selection->itemAt(0); QRectF oldR = currItem->getBoundingRect().adjusted(-5, -5, 10, 10); PageItem_Arc *item = currItem->asArc(); QTransform bb; bb.scale(height / width, 1.0); QLineF inp = QLineF(QPointF(width / 2.0, height / 2.0), QPointF(width, height / 2.0)); inp.setAngle(start); QLineF res = bb.map(inp); inp.setAngle(end); QLineF ena = bb.map(inp); m_startAngle = res.angle(); m_endAngle = ena.angle(); double nSweep = m_endAngle - m_startAngle; if (nSweep < 0) nSweep += 360; double oldX = currItem->xPos(); double oldY = currItem->yPos(); double newX = oldX + m_centerPoint.x() - (width / 2.0); double newY = oldY + m_centerPoint.y() - (height / 2.0); item->setXYPos(newX, newY, true); FPointArray old = item->PoLine; QPainterPath pp; pp.moveTo(width / 2.0, height / 2.0); pp.arcTo(QRectF(0, 0, width, height), m_startAngle, nSweep); pp.closeSubpath(); currItem->PoLine.fromQPainterPath(pp, true); FPoint wh = getMaxClipF(&currItem->PoLine); currItem->setWidthHeight(wh.x(),wh.y()); m_doc->adjustItemSize(currItem); currItem->OldB2 = currItem->width(); currItem->OldH2 = currItem->height(); if (UndoManager::undoEnabled()) { ScItemState<QPair<FPointArray, FPointArray> > *ss = new ScItemState<QPair<FPointArray, FPointArray> >(Um::EditArc,"",Um::IPolygon); ss->set("ARC"); ss->set("OLD_WIDTH",item->arcWidth); ss->set("NEW_WIDTH",width); ss->set("OLD_XPOS",oldX); ss->set("OLD_YPOS",oldY); ss->set("OLD_HEIGHT",item->arcHeight); ss->set("NEW_HEIGHT",height); ss->set("OLD_START",item->arcStartAngle); ss->set("NEW_START",m_startAngle); ss->set("OLD_SWEEP",item->arcSweepAngle); ss->set("NEW_SWEEP",nSweep); ss->setItem(qMakePair(old,item->PoLine)); ss->set("NEW_XPOS",item->xPos()); ss->set("NEW_YPOS",item->yPos()); undoManager->action(currItem,ss); } item->arcStartAngle = m_startAngle; item->arcSweepAngle = nSweep; item->arcWidth = width; item->arcHeight = height; m_startPoint = currItem->PoLine.pointQF(2); m_endPoint = currItem->PoLine.pointQF(currItem->PoLine.size() - 4); m_centerPoint = currItem->PoLine.pointQF(0); m_widthPoint = QPointF(m_centerPoint.x() - item->arcWidth / 2.0, m_centerPoint.y()); m_heightPoint = QPointF(m_centerPoint.x(), m_centerPoint.y() - item->arcHeight / 2.0); m_doc->setRedrawBounding(currItem); QRectF newR(currItem->getBoundingRect()); m_doc->regionsChanged()->update(newR.united(oldR)); // QTransform itemMatrix = currItem->getTransform(); // m_doc->regionsChanged()->update(itemMatrix.mapRect(QRectF(0, 0, currItem->width(), currItem->height())).adjusted(-currItem->width() / 2.0, -currItem->height() / 2.0, currItem->width(), currItem->height())); }
void QgsColorWheel::paintEvent( QPaintEvent *event ) { Q_UNUSED( event ); QPainter painter( this ); //draw a frame QStyleOptionFrameV3 option = QStyleOptionFrameV3(); option.initFrom( this ); option.state = this->hasFocus() ? QStyle::State_Active : QStyle::State_None; style()->drawPrimitive( QStyle::PE_Frame, &option, &painter ); if ( !mWidgetImage || !mWheelImage || !mTriangleImage ) { createImages( size() ); } //draw everything in an image mWidgetImage->fill( Qt::transparent ); QPainter imagePainter( mWidgetImage ); imagePainter.setRenderHint( QPainter::Antialiasing ); if ( mWheelDirty ) { //need to redraw the wheel image createWheel(); } //draw wheel centered on widget QPointF center = QPointF( width() / 2.0, height() / 2.0 ); imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mWheelImage ); //draw hue marker int h = hue(); double length = mWheelImage->width() / 2.0; QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() ); hueMarkerLine.setAngle( h ); imagePainter.save(); //use sourceIn mode for nicer antialiasing imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn ); QPen pen; pen.setWidth( 2 ); //adapt pen color for hue pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white ); imagePainter.setPen( pen ); imagePainter.drawLine( hueMarkerLine ); imagePainter.restore(); //draw triangle if ( mTriangleDirty ) { createTriangle(); } imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mTriangleImage ); //draw current color marker double triangleRadius = length - mWheelThickness - 1; //adapted from equations at https://github.com/timjb/colortriangle/blob/master/colortriangle.js by Tim Baumann double lightness = mCurrentColor.lightnessF(); double hueRadians = ( h * M_PI / 180.0 ); double hx = cos( hueRadians ) * triangleRadius; double hy = -sin( hueRadians ) * triangleRadius; double sx = -cos( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius; double sy = -sin( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius; double vx = -cos( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius; double vy = sin( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius; double mx = ( sx + vx ) / 2.0; double my = ( sy + vy ) / 2.0; double a = ( 1 - 2.0 * fabs( lightness - 0.5 ) ) * mCurrentColor.hslSaturationF(); double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a; double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a; //adapt pen color for lightness pen.setColor( lightness > 0.7 ? Qt::black : Qt::white ); imagePainter.setPen( pen ); imagePainter.setBrush( Qt::NoBrush ); imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0, 4.0 ); imagePainter.end(); //draw image onto widget painter.drawImage( QPoint( 0, 0 ), *mWidgetImage ); painter.end(); }