Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. #include "arc.h"
  2.  
  3. Arc::Arc(int i, QPointF point1, QPointF point2, QPointF point3)
  4. {
  5. // assigns id
  6. id = i;
  7.  
  8. /**
  9. * set values of three points
  10. * and calls init() to do calculation for arc
  11. */
  12. p1 = point1;
  13. p2 = point2;
  14. p3 = point3;
  15. init();
  16. }
  17.  
  18. int Arc::type() const
  19. {
  20. // Enable the use of qgraphicsitem_cast with arc item.
  21. return Type;
  22. }
  23.  
  24. Arc::Arc(QPointF point1, QPointF point2, QPointF point3)
  25. {
  26. /**
  27. * set values of three points
  28. * and calls init() to do calculation for arc
  29. */
  30. p1 = point1;
  31. p2 = point2;
  32. p3 = point3;
  33. init();
  34. }
  35.  
  36. void Arc::init()
  37. {
  38. // Calculates startangle and spanangle
  39. lineBC = QLineF(p2, p3);
  40. lineAC = QLineF(p1, p3);
  41. lineBA = QLineF(p2, p1);
  42.  
  43. rad = qAbs(lineBC.length()/(2 * qSin(qDegreesToRadians
  44. (lineAC.angleTo(lineBA)))));
  45.  
  46. bisectorBC = QLineF(lineBC.pointAt(0.5), lineBC.p2());
  47. bisectorBC.setAngle(lineBC.normalVector().angle());
  48.  
  49. bisectorBA = QLineF(lineBA.pointAt(0.5), lineBA.p2());
  50. bisectorBA.setAngle(lineBA.normalVector().angle());
  51. bisectorBA.intersect(bisectorBC, &center);
  52.  
  53. circle = QRectF(center.x() - rad, center.y() - rad, rad * 2, rad * 2);
  54.  
  55. lineOA = QLineF(center, p1);
  56. lineOB = QLineF(center, p2);
  57. lineOC = QLineF(center, p3);
  58.  
  59. startAngle = lineOA.angle();
  60. spanAngle = lineOA.angleTo(lineOC);
  61.  
  62. /**
  63. * Make sure that the span angle covers all three points with the
  64. * second point in the middle
  65. */
  66. if (qAbs(spanAngle) < qAbs(lineOA.angleTo(lineOB)) ||
  67. qAbs(spanAngle) < qAbs(lineOB.angleTo(lineOC)))
  68. {
  69. // swap the end point and invert the spanAngle
  70. startAngle = lineOC.angle();
  71. spanAngle = 360 - spanAngle;
  72. }
  73.  
  74. int w = 10;
  75. boundingRectTemp = circle.adjusted(-w, -w, w, w);
  76. }
  77.  
  78. QRectF Arc::boundingRect() const
  79. {
  80. // bounding rectangle for arc
  81. return boundingRectTemp;
  82. }
  83.  
  84. void Arc::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
  85. QWidget *widget)
  86. {
  87. QPen paintpen;
  88. painter->setRenderHint(QPainter::Antialiasing);
  89. paintpen.setCosmetic(true);
  90. paintpen.setWidth(1);
  91.  
  92. // Draw arc
  93. if (option->state & QStyle::State_Selected)
  94. {
  95. // sets brush for end points
  96. painter->setBrush(Qt::SolidPattern);
  97. paintpen.setColor(Qt::red);
  98. painter->setPen(paintpen);
  99. painter->drawEllipse(p1, 2, 2);
  100. painter->drawEllipse(p2, 2, 2);
  101. painter->drawEllipse(p3, 2, 2);
  102.  
  103. // sets pen for arc path
  104. paintpen.setStyle(Qt::DashLine);
  105. paintpen.setColor(Qt::black);
  106. painter->setPen(paintpen);
  107. }
  108.  
  109. else
  110. {
  111. painter->setBrush(Qt::SolidPattern);
  112. paintpen.setColor(Qt::black);
  113. painter->setPen(paintpen);
  114. painter->drawEllipse(p1, 2, 2);
  115. painter->drawEllipse(p2, 2, 2);
  116. painter->drawEllipse(p3, 2, 2);
  117. }
  118.  
  119. QPainterPath path;
  120. path.arcMoveTo(circle, startAngle);
  121. path.arcTo(circle, startAngle, spanAngle);
  122. painter->setBrush(Qt::NoBrush);
  123. painter->drawPath(path);
  124. }
  125.  
  126. getEntity *Arc::clone(int i)
  127. {
  128. Arc *a = new Arc;
  129. a->id = i;
  130. a->p1 = p1;
  131. a->p2 = p2;
  132. a->p3 = p3;
  133.  
  134. a->lineAC.setLine(p1.x(), p1.y(),p3.x(), p3.y());
  135. a->lineBA.setLine(p2.x(),p2.y(),p1.x(), p1.y());
  136. a->lineBC.setLine(p2.x(),p2.y(),p3.x(), p3.y());
  137. a->lineOA.setLine(center.x(), center.y(), p1.x(), p1.y());
  138. a->lineOB.setLine(center.x(), center.y() ,p2.x(), p2.y());
  139. a->lineOC.setLine(center.x(), center.y(), p3.x(), p3.y());
  140. a->bisectorBA = bisectorBA;
  141. a->bisectorBC = bisectorBC;
  142. a->center = center;
  143. a->circle = circle;
  144. a->startAngle = startAngle;
  145. a->spanAngle = spanAngle;
  146. return a;
  147. }
  148.  
  149. #ifndef GETENTITY_H
  150. #define GETENTITY_H
  151.  
  152. #include <QGraphicsItem>
  153.  
  154. class getEntity : public QObject, public QGraphicsItem
  155. {
  156. public:
  157. getEntity(QObject *parent = 0) : QObject(parent) {}
  158. virtual ~getEntity() {}
  159.  
  160. virtual getEntity *clone(int i)
  161. {
  162. return 0;
  163. }
  164.  
  165. int id;
  166. };
  167.  
  168. #endif // GENTITY_H
  169.  
  170. #ifndef CLIPBOARDSTACK_H
  171. #define CLIPBOARDSTACK_H
  172.  
  173. #include <QStack>
  174.  
  175. #include "getEntity.h"
  176.  
  177. class clipboardStack
  178. {
  179. public:
  180. static clipboardStack *instance()
  181. {
  182. if (!inst)
  183. inst = new clipboardStack;
  184.  
  185. return inst;
  186. }
  187.  
  188. void push(getEntity *entity)
  189. {
  190. clips.push(entity);
  191. }
  192.  
  193. getEntity *pasteEntity()
  194. {
  195. if (clips.count() == 0)
  196. return 0;
  197.  
  198. return clips.last();
  199. }
  200.  
  201. getEntity *pop()
  202. {
  203. if (clips.count() == 0)
  204. return 0;
  205.  
  206. return clips.pop();
  207. }
  208.  
  209. bool isEmpty() const
  210. {
  211. return clips.empty();
  212. }
  213.  
  214. private:
  215. QStack<getEntity *> clips;
  216. static clipboardStack *inst;
  217. };
  218.  
  219. #endif // CLIPBOARDSTACK_H
  220.  
  221. if (mouseEvent->button() == Qt::RightButton)
  222. {
  223. contextItem = itemAt(mouseEvent->scenePos().toPoint(), QTransform());
  224. contextPosition = mouseEvent->scenePos();
  225.  
  226. if (!contextItem || !contextItem->isSelected())
  227. {
  228. cutAction->setEnabled(false);
  229. copyAction->setEnabled(false);
  230.  
  231. if (clipboardStack::instance()->isEmpty())
  232. {
  233. pasteAction->setEnabled(false);
  234. }
  235.  
  236. else
  237. {
  238. pasteAction->setEnabled(true);
  239. }
  240. }
  241.  
  242. else
  243. {
  244. cutAction->setEnabled(true);
  245. copyAction->setEnabled(true);
  246. pasteAction->setEnabled(false);
  247. }
  248. if (contextItem->type() == Arc::Type)
  249. {
  250. Arc *itemPtr = dynamic_cast<Arc *>(contextItem);
  251. contextItemId = itemPtr->id;
  252. }
  253. }
  254. void CadGraphicsScene::cut(getEntity *obj)
  255. {
  256. // id of item pasted is kept same as that of the item being cut
  257. removeItem(obj);
  258. clipboardStack::instance()->push(obj->clone(contextItemId));
  259. }
  260.  
  261. void CadGraphicsScene::copy(getEntity *obj)
  262. {
  263. // id of item pasted is one more than total number of items in the scene
  264. clipboardStack::instance()->push(obj->clone(++id));
  265. }
  266.  
  267. void CadGraphicsScene::paste(const QPointF &pos)
  268. {
  269. // gets the items cut/copy from clipboardStack to paste
  270. getEntity *pasteEntity = clipboardStack::instance()->pop();
  271. if (pasteEntity->type() == Arc::Type)
  272. {
  273. Arc *itemPtr = dynamic_cast<Arc *>(pasteEntity);
  274. drawEntity(itemPtr);
  275. }
  276. void CadGraphicsScene::drawEntity(QGraphicsItem *item)
  277. {
  278. if (item->type() == Arc::Type)
  279. {
  280. Arc *itemPtr = dynamic_cast<Arc *>(item);
  281. itemList.append(itemPtr);
  282. mUndoStack->push(new CadCommandAdd(this, itemPtr));
  283. }
  284. void CadGraphicsScene::menuAction(QAction *action)
  285. {
  286. if (action == cutAction)
  287. {
  288. cut(static_cast<getEntity *>(contextItem));
  289. }
  290.  
  291. else if (action == copyAction)
  292. {
  293. copy(static_cast<getEntity *>(contextItem));
  294. }
  295.  
  296. else if (action == pasteAction)
  297. {
  298. paste(contextPosition);
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement