1/***************************************************************************
2 * Copyright 2008, 2009, 2010 Stefan Majewsky <majewsky@gmx.net>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (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, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 ***************************************************************************/
18
19#ifndef KOLF_OVERLAY_H
20#define KOLF_OVERLAY_H
21
22#include <QGraphicsItem>
23
24namespace Utils
25{
26 class AnimatedItem;
27}
28
29class CanvasItem;
30
31namespace Kolf
32{
33 //This can be used by Kolf::Overlay subclasses for modifying the physical appearance of an object.
34 class OverlayHandle : public QObject, public QGraphicsPathItem
35 {
36 Q_OBJECT
37 public:
38 enum Shape
39 {
40 SquareShape,
41 CircleShape,
42 TriangleShape
43 };
44
45 OverlayHandle(Shape shape, QGraphicsItem* parent);
46 Q_SIGNALS:
47 void moveStarted();
48 void moveRequest(const QPointF& targetScenePos);
49 void moveEnded();
50 protected:
51 virtual void hoverEnterEvent(QGraphicsSceneHoverEvent* event);
52 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent* event);
53 virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
54 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
55 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
56 };
57
58 //This is used by Kolf::Overlay to paint the various outlines of an item.
59 class OverlayAreaItem : public QObject, public QGraphicsPathItem
60 {
61 Q_OBJECT
62 public:
63 enum Feature
64 {
65 Draggable = 1 << 0,
66 Clickable = 1 << 1,
67 Hoverable = 1 << 2
68 };
69 Q_DECLARE_FLAGS(Features, Feature)
70
71 OverlayAreaItem(Features features, QGraphicsItem* parent);
72 Q_SIGNALS:
73 void clicked(int button);
74 void dragged(const QPointF& distance);
75 void hoverEntered();
76 void hoverLeft();
77 protected:
78 virtual void hoverEnterEvent(QGraphicsSceneHoverEvent* event);
79 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent* event);
80 virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
81 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
82 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
83 private:
84 Features m_features;
85 };
86
87 /**
88 * \class Overlay
89 * \since 2.0
90 *
91 * Overlays are used by the editor to provide the on-screen editing interface. The most wellknown overlay is probably the rectangle with grips at every corner and edge. Overlays react on changes in the property set of their object and change specific properties through user interaction.
92 *
93 * By default, the Overlay renders interaction and activation outlines and allows to change the object's position; subclasses have to add handles to manipulate geometrical properties of the object. (This class may become pure virtual in the future, we therefore recommend to not instance this class. If you want an overlay without handles, use Kolf::EmptyOverlay instead.)
94 *
95 * \warning Do not add any handles or additional interface items through setParentItem() et al. Use the addHandle() method to ensure that handles are correctly shown and hidden.
96 * \sa Kolf::Object
97 */
98 class Overlay : public QObject, public QGraphicsItem
99 {
100 Q_OBJECT
101 Q_INTERFACES(QGraphicsItem)
102 public:
103 ///This enum specifies states that an overlay can occupy.
104 enum State
105 {
106 Passive = 0, ///< The overlay is invisible (not as in isVisible() == false, but all components are transparent or invisible).
107 Hovered = 1, ///< The overlay's activation area has been hovered, and the interactor area is shown (though very translucent).
108 Active = 2 ///< The interaction area is shown at a high opacity, and the overlay's handles are visible.
109 };
110
111 Overlay(CanvasItem* citem, QGraphicsItem* qitem, bool hack_addQitemShapeToOutlines = false);
112 ///Returns a reference to the CanvasItem that this overlay is associated with.
113 CanvasItem* citem() const;
114 ///Returns a reference to the QGraphicsItem that is the CanvasItem that this overlay is associated with.
115 QGraphicsItem* qitem() const;
116
117 ///Returns the current state of this overlay.
118 State state() const;
119 ///Performs a state transition from the current towards the given \a state.
120 void setState(State state);
121 ///Lets the overlay initialize its properties or react on property changes in the base object. Subclasses of Kolf::Overlay need to reimplement this to read all important properties of their object (aside from calling the base class implementation!).
122 virtual void update();
123
124 ///Overlays should not allow to decrease an object's dimensions below this level, for the sake of usability.
125 static const qreal MinimumObjectDimension;
126
127 virtual QRectF boundingRect() const;
128 virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
129 Q_SIGNALS:
130 ///This signal is emitted if the overlay's state changes.
131 void stateChanged();
132 protected:
133 ///Adds a handle to the overlay interface. Handles are only shown if the overlay is visible and activated.
134 void addHandle(QGraphicsItem* handle);
135 private Q_SLOTS:
136 void activatorEntered();
137 void activatorLeft();
138 void activatorClicked(int button);
139 void interactorDragged(const QPointF& distance);
140 private:
141 CanvasItem* m_citem;
142 QGraphicsItem* m_qitem;
143 Kolf::Overlay::State m_state;
144 bool m_addQitemShapeToOutlines;
145 //pre-defined handles and area items
146 Kolf::OverlayAreaItem* m_activatorItem;
147 Utils::AnimatedItem* m_interactorAnimator;
148 Kolf::OverlayAreaItem* m_interactorItem;
149 Utils::AnimatedItem* m_handleAnimator;
150 };
151}
152
153Q_DECLARE_OPERATORS_FOR_FLAGS(Kolf::OverlayAreaItem::Features)
154
155#endif // KOLF_OVERLAY_H
156