Schim
gobject.h
1 #ifndef GOBJECT_H
2 #define GOBJECT_H
3 
4 #include "model/object.h"
5 #include "model/line.h"
6 #include "ui/sheetscene.h"
7 
8 #include <QGraphicsItem>
9 #include <QGraphicsRectItem>
10 #include <QPen>
11 #include <QBrush>
12 #include <QPainter>
13 #include <QStyleOptionGraphicsItem>
14 
15 class GObjectHandle;
16 class GCompositeObject;
17 
24 class GObject : public QGraphicsObject
25 {
26 public:
27  // CONSTRUCTORS
34  explicit GObject(Object *obj);
36  virtual ~GObject();
37 
38  // GETTERS
45  virtual Object *get();
46  virtual const Object *get() const;
47  Entity *getModelParent() const;
51  bool isHovered() const;
52  GObject *getOldestParent();
53  SheetScene *getSheetScene() const;
54 
55  // SETTERS
62  virtual void setCosmetic(bool cosmetic);
63 
64  // OBJECT EDITING
72  virtual void reloadFromModel();
79  virtual void applyToModel();
92  virtual void showHandles(bool show = true);
99  virtual void handleChanged(GObjectHandle *handle);
100 
101  // STATIC
110  static GObject *assign(Object *obj);
111 
112  // EVENTS
116  void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
120  void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
121 protected:
126  void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
127  void keyPressEvent(QKeyEvent *event) override;
145  QVariant itemChange(GraphicsItemChange change, const QVariant &value)
146  override;
147 
148 public:
149  // OVERRIDE QGraphicsItem
153  QRectF boundingRect() const override;
161  void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) override;
165  GCompositeObject *parentItem() const;
166 private:
167  // HELPER METHODS
172  void moveHandlesAbove();
173 
174 protected:
175  // ATTRIBUTES
179  QList<GObjectHandle*> *handles{};
180  // TODO Squeeze into one flag variable
181  bool cosmetic = false;
182  bool hovered = false;
183 
184  // FRIENDS
185  friend class GObjectHandle;
186 };
187 
194 class GObjectHandle : public QGraphicsRectItem
195 {
196 public:
203  explicit GObjectHandle(GObject *obj);
204 
206  SheetScene *scene();
207 
208 protected:
209  // EVENTS
211  QVariant itemChange(QGraphicsItem::GraphicsItemChange change,
212  const QVariant &value) override;
219  void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
228  void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
229 
230 private:
231  // ATTRIBUTES
232  GObject *obj;
233  QPointF _dragStartPos;
234 };
235 
236 #endif // GOBJECT_H
The abstract base class for all objects in a sheet.
Definition: object.h:21
virtual void reloadFromModel()
Update the graphical representation to match the object from the model.
virtual ~GObject()
virtual void handleChanged(GObjectHandle *handle)
Called when a handle&#39;s position has changed.
A specialized QGraphicsScene containing a sheet of paper.
Definition: sheetscene.h:27
static GObject * assign(Object *obj)
Return a dynamically allocated GObject wrapping the specified object.
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
Process item changes that should behave uniformly across different object types.
GCompositeObject * parentItem() const
Return the parent item cast to a GObject*.
virtual void setCosmetic(bool cosmetic)
Make the object&#39;s pen independent of any transformations.
Object * obj
The object that is being wrapped.
Definition: gobject.h:177
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override
QRectF boundingRect() const override
Default implementation that returns childrenBoundingRect().
bool isHovered() const
Return whether the mouse is over this object.
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override
Disable the bool hovered so that it can be used by paint.
A graphical object that wraps a CompositeObject.
Definition: gcompositeobject.h:11
Base class for entities.
Definition: entity.h:32
The base class of all objects represented in a graphical scene.
Definition: gobject.h:24
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override
Set hovered=true so that it can be used by paint.
virtual void applyToModel()
Apply changes to the underlying model object.
The base class for object handles.
Definition: gobject.h:194
QList< GObjectHandle * > * handles
Dynamically allocated list of handles.
Definition: gobject.h:179
GObject(Object *obj)
Construct a wrapper around obj.
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
virtual void showHandles(bool show=true)
Display/hide the handles for this item.