Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "arc.h"
- Arc::Arc(int i, QPointF point1, QPointF point2, QPointF point3)
- {
- // assigns id
- id = i;
- /**
- * set values of three points
- * and calls init() to do calculation for arc
- */
- p1 = point1;
- p2 = point2;
- p3 = point3;
- init();
- }
- int Arc::type() const
- {
- // Enable the use of qgraphicsitem_cast with arc item.
- return Type;
- }
- Arc::Arc(QPointF point1, QPointF point2, QPointF point3)
- {
- /**
- * set values of three points
- * and calls init() to do calculation for arc
- */
- p1 = point1;
- p2 = point2;
- p3 = point3;
- init();
- }
- void Arc::init()
- {
- // Calculates startangle and spanangle
- lineBC = QLineF(p2, p3);
- lineAC = QLineF(p1, p3);
- lineBA = QLineF(p2, p1);
- rad = qAbs(lineBC.length()/(2 * qSin(qDegreesToRadians
- (lineAC.angleTo(lineBA)))));
- bisectorBC = QLineF(lineBC.pointAt(0.5), lineBC.p2());
- bisectorBC.setAngle(lineBC.normalVector().angle());
- bisectorBA = QLineF(lineBA.pointAt(0.5), lineBA.p2());
- bisectorBA.setAngle(lineBA.normalVector().angle());
- bisectorBA.intersect(bisectorBC, ¢er);
- circle = QRectF(center.x() - rad, center.y() - rad, rad * 2, rad * 2);
- lineOA = QLineF(center, p1);
- lineOB = QLineF(center, p2);
- lineOC = QLineF(center, p3);
- startAngle = lineOA.angle();
- spanAngle = lineOA.angleTo(lineOC);
- /**
- * Make sure that the span angle covers all three points with the
- * second point in the middle
- */
- if (qAbs(spanAngle) < qAbs(lineOA.angleTo(lineOB)) ||
- qAbs(spanAngle) < qAbs(lineOB.angleTo(lineOC)))
- {
- // swap the end point and invert the spanAngle
- startAngle = lineOC.angle();
- spanAngle = 360 - spanAngle;
- }
- int w = 10;
- boundingRectTemp = circle.adjusted(-w, -w, w, w);
- }
- QRectF Arc::boundingRect() const
- {
- // bounding rectangle for arc
- return boundingRectTemp;
- }
- void Arc::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
- QWidget *widget)
- {
- QPen paintpen;
- painter->setRenderHint(QPainter::Antialiasing);
- paintpen.setCosmetic(true);
- paintpen.setWidth(1);
- // Draw arc
- if (option->state & QStyle::State_Selected)
- {
- // sets brush for end points
- painter->setBrush(Qt::SolidPattern);
- paintpen.setColor(Qt::red);
- painter->setPen(paintpen);
- painter->drawEllipse(p1, 2, 2);
- painter->drawEllipse(p2, 2, 2);
- painter->drawEllipse(p3, 2, 2);
- // sets pen for arc path
- paintpen.setStyle(Qt::DashLine);
- paintpen.setColor(Qt::black);
- painter->setPen(paintpen);
- }
- else
- {
- painter->setBrush(Qt::SolidPattern);
- paintpen.setColor(Qt::black);
- painter->setPen(paintpen);
- painter->drawEllipse(p1, 2, 2);
- painter->drawEllipse(p2, 2, 2);
- painter->drawEllipse(p3, 2, 2);
- }
- QPainterPath path;
- path.arcMoveTo(circle, startAngle);
- path.arcTo(circle, startAngle, spanAngle);
- painter->setBrush(Qt::NoBrush);
- painter->drawPath(path);
- }
- getEntity *Arc::clone(int i)
- {
- Arc *a = new Arc;
- a->id = i;
- a->p1 = p1;
- a->p2 = p2;
- a->p3 = p3;
- a->lineAC.setLine(p1.x(), p1.y(),p3.x(), p3.y());
- a->lineBA.setLine(p2.x(),p2.y(),p1.x(), p1.y());
- a->lineBC.setLine(p2.x(),p2.y(),p3.x(), p3.y());
- a->lineOA.setLine(center.x(), center.y(), p1.x(), p1.y());
- a->lineOB.setLine(center.x(), center.y() ,p2.x(), p2.y());
- a->lineOC.setLine(center.x(), center.y(), p3.x(), p3.y());
- a->bisectorBA = bisectorBA;
- a->bisectorBC = bisectorBC;
- a->center = center;
- a->circle = circle;
- a->startAngle = startAngle;
- a->spanAngle = spanAngle;
- return a;
- }
- #ifndef GETENTITY_H
- #define GETENTITY_H
- #include <QGraphicsItem>
- class getEntity : public QObject, public QGraphicsItem
- {
- public:
- getEntity(QObject *parent = 0) : QObject(parent) {}
- virtual ~getEntity() {}
- virtual getEntity *clone(int i)
- {
- return 0;
- }
- int id;
- };
- #endif // GENTITY_H
- #ifndef CLIPBOARDSTACK_H
- #define CLIPBOARDSTACK_H
- #include <QStack>
- #include "getEntity.h"
- class clipboardStack
- {
- public:
- static clipboardStack *instance()
- {
- if (!inst)
- inst = new clipboardStack;
- return inst;
- }
- void push(getEntity *entity)
- {
- clips.push(entity);
- }
- getEntity *pasteEntity()
- {
- if (clips.count() == 0)
- return 0;
- return clips.last();
- }
- getEntity *pop()
- {
- if (clips.count() == 0)
- return 0;
- return clips.pop();
- }
- bool isEmpty() const
- {
- return clips.empty();
- }
- private:
- QStack<getEntity *> clips;
- static clipboardStack *inst;
- };
- #endif // CLIPBOARDSTACK_H
- if (mouseEvent->button() == Qt::RightButton)
- {
- contextItem = itemAt(mouseEvent->scenePos().toPoint(), QTransform());
- contextPosition = mouseEvent->scenePos();
- if (!contextItem || !contextItem->isSelected())
- {
- cutAction->setEnabled(false);
- copyAction->setEnabled(false);
- if (clipboardStack::instance()->isEmpty())
- {
- pasteAction->setEnabled(false);
- }
- else
- {
- pasteAction->setEnabled(true);
- }
- }
- else
- {
- cutAction->setEnabled(true);
- copyAction->setEnabled(true);
- pasteAction->setEnabled(false);
- }
- if (contextItem->type() == Arc::Type)
- {
- Arc *itemPtr = dynamic_cast<Arc *>(contextItem);
- contextItemId = itemPtr->id;
- }
- }
- void CadGraphicsScene::cut(getEntity *obj)
- {
- // id of item pasted is kept same as that of the item being cut
- removeItem(obj);
- clipboardStack::instance()->push(obj->clone(contextItemId));
- }
- void CadGraphicsScene::copy(getEntity *obj)
- {
- // id of item pasted is one more than total number of items in the scene
- clipboardStack::instance()->push(obj->clone(++id));
- }
- void CadGraphicsScene::paste(const QPointF &pos)
- {
- // gets the items cut/copy from clipboardStack to paste
- getEntity *pasteEntity = clipboardStack::instance()->pop();
- if (pasteEntity->type() == Arc::Type)
- {
- Arc *itemPtr = dynamic_cast<Arc *>(pasteEntity);
- drawEntity(itemPtr);
- }
- void CadGraphicsScene::drawEntity(QGraphicsItem *item)
- {
- if (item->type() == Arc::Type)
- {
- Arc *itemPtr = dynamic_cast<Arc *>(item);
- itemList.append(itemPtr);
- mUndoStack->push(new CadCommandAdd(this, itemPtr));
- }
- void CadGraphicsScene::menuAction(QAction *action)
- {
- if (action == cutAction)
- {
- cut(static_cast<getEntity *>(contextItem));
- }
- else if (action == copyAction)
- {
- copy(static_cast<getEntity *>(contextItem));
- }
- else if (action == pasteAction)
- {
- paste(contextPosition);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement