point_item.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* point_item.cpp - point item reporting its position changes
  2. * Copyright (C) 2017-2018 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <QGuiApplication>
  18. #include <2geom/point.h>
  19. #include <widgets/canvas.h>
  20. #include <util/pen.h>
  21. #include "point_item.h"
  22. namespace rainynite::studio {
  23. AbstractPointItem::AbstractPointItem() :
  24. QGraphicsEllipseItem(-radius, -radius, radius*2, radius*2)
  25. {
  26. setPen(pens::cosmetic_solid());
  27. setBrush(QGuiApplication::palette().text());
  28. }
  29. void AbstractPointItem::paint(QPainter* painter, QStyleOptionGraphicsItem const* option, QWidget* widget) {
  30. // Set correct size if view was zooomed..
  31. // NOTE: this dirty hack relies on the scene being used only by one view
  32. auto t = painter->transform();
  33. auto rx = radius / std::sqrt(t.m11()*t.m11() + t.m21()*t.m21());
  34. auto ry = radius / std::sqrt(t.m22()*t.m22() + t.m12()*t.m12());
  35. setRect(-rx, -ry, rx*2, ry*2);
  36. QGraphicsEllipseItem::paint(painter, option, widget);
  37. }
  38. QVariant AbstractPointItem::itemChange(GraphicsItemChange change, QVariant const& value) {
  39. if (change == ItemPositionChange && !recursion_protection) {
  40. recursion_protection = true;
  41. auto new_pos = value.toPointF();
  42. if (new_pos != pos())
  43. moved(new_pos);
  44. } else if (change == ItemPositionHasChanged) {
  45. recursion_protection = false;
  46. }
  47. return QGraphicsItem::itemChange(change, value);
  48. }
  49. void AbstractPointItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {
  50. QGraphicsEllipseItem::mouseReleaseEvent(event);
  51. stopped_moving();
  52. }
  53. void AbstractPointItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) {
  54. QGraphicsEllipseItem::mouseDoubleClickEvent(event);
  55. double_clicked();
  56. }
  57. void AbstractPointItem::set_readonly(bool ro) {
  58. setFlag(QGraphicsItem::ItemIsMovable, !ro);
  59. setFlag(QGraphicsItem::ItemIsSelectable, !ro);
  60. setFlag(QGraphicsItem::ItemSendsGeometryChanges, !ro);
  61. }
  62. void AbstractPointItem::set_pos(double x, double y) {
  63. setPos(x, y);
  64. }
  65. void AbstractPointItem::set_color(QColor const& color) {
  66. setBrush(color);
  67. }
  68. } // namespace rainynite::studio