~raimar/navitconfigurator/NavitConfigurator

« back to all changes in this revision

Viewing changes to src/navitconf/gui/mapview/BoxGraphicsItem.h

  • Committer: Raimar
  • Date: 2012-07-18 20:58:12 UTC
  • Revision ID: git-v1:da49d479d527774f27cdc54f6985f4cacecc43f3
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @see http://www.davidwdrell.net/wordpress/?page_id=46
 
3
 */
 
4
 
 
5
#ifndef BOXGRAPHICSITEM_H_
 
6
#define BOXGRAPHICSITEM_H_
 
7
 
 
8
#include <QtCore/QRectF>
 
9
#include <QtCore/QPointF>
 
10
#include <QtCore/QString>
 
11
 
 
12
#include <QtGui/QGraphicsRectItem>
 
13
#include <QtGui/QGraphicsItem>
 
14
#include <QtGui/QGraphicsSceneMouseEvent>
 
15
#include <QtGui/QPainter>
 
16
#include <QtGui/QStyleOptionGraphicsItem>
 
17
#include <QtGui/QWidget>
 
18
#include <QtGui/QImage>
 
19
 
 
20
#include <navitconf/data/TreeItem.h>
 
21
#include <navitconf/data/TreeModel.h>
 
22
 
 
23
#include "BoxGraphicsItemListener.h"
 
24
#include "BoxItemAttribute.h"
 
25
 
 
26
/**
 
27
 * An OSD node of the navit.xml file shown in the MapViewScene.
 
28
 */
 
29
class BoxGraphicsItem: public QGraphicsRectItem {
 
30
 
 
31
public:
 
32
        /**
 
33
         * Unique IDs for each box item property.
 
34
         * Attention: Must be in sync with internal list defined in method
 
35
         * {@link #init(const QModelIndex&,const TreeItem&,int,int)}
 
36
         */
 
37
        enum BoxItemAttributeId {
 
38
                OSD_TYPE_NAME,
 
39
                OSD_OSD_CONFIGURATION,
 
40
                OSD_LABEL,
 
41
                OSD_COMMAND,
 
42
                OSD_SRC,
 
43
                OSD_ICON_SRC,
 
44
                OSD_HORIZONTAL_ALIGN,
 
45
                OSD_VERTICAL_ALIGN,
 
46
                OSD_X_POSITION,
 
47
                OSD_Y_POSITION,
 
48
                OSD_WIDTH,
 
49
                OSD_HEIGHT,
 
50
                OSD_USE_SIZE,
 
51
                OSD_BACKGROUND_COLOR,
 
52
                OSD_TEXT_COLOR,
 
53
                OSD_COUNT
 
54
        };
 
55
 
 
56
        static int getAttributeCount();
 
57
        static QStringList getAttributeNames();
 
58
 
 
59
        /**
 
60
         * Method for creating an instance of this BoxGraohicsItem.
 
61
         * @param index The tree index this item is associated with.
 
62
         * @param sceneWidth The width in pixel of this scene.
 
63
         * @param sceneHeight The height in pixel of this scene.
 
64
         */
 
65
        static BoxGraphicsItem* init(const QModelIndex& index, const TreeItem& item, int sceneWidth, int sceneHeight);
 
66
        /**
 
67
         * Not valid default constructor.
 
68
         */
 
69
        BoxGraphicsItem();
 
70
        /**
 
71
         * Normal constructor sets the same valued as the inherited class {@link QGraphicsRectItem#QGraphicsRectItem()}
 
72
         */
 
73
        BoxGraphicsItem(const QModelIndex& index, int x, int y, int width, int height);
 
74
        virtual ~BoxGraphicsItem();
 
75
        /**
 
76
         * Copy assignment.
 
77
         */
 
78
        BoxGraphicsItem& operator=(const BoxGraphicsItem& other);
 
79
 
 
80
        /**
 
81
         * @return True, if this class holds valid data, i. e. width >= 0, otherwise false. Normally only used by overloaded default constructor.
 
82
         */
 
83
        bool isValid() const;
 
84
        QModelIndex getModelIndex() const;
 
85
        void setPos(const QPointF& newPos);
 
86
        void setRect(const QRectF& newRect);
 
87
 
 
88
        void setIsLeft(bool isLeft);
 
89
        void setIsTop(bool isTop);
 
90
        bool isOsdConfiguration() const;
 
91
        void setIsOsdConfiguration(bool isOsdConfiguration);
 
92
        void setAttribute(BoxItemAttributeId id, const QString& value);
 
93
        BoxItemAttribute& getAttribute(BoxItemAttributeId id);
 
94
        const BoxItemAttribute& getAttribute(BoxItemAttributeId id) const;
 
95
        void setHasSize(bool hasSize);
 
96
        void sceneSizeChanged();
 
97
        /**
 
98
         * @return True, it both items has the same row number at the index,
 
99
         *         <code>i. e. index.row() == other.index.row()</code>.
 
100
         */
 
101
        bool equalsByRow(const BoxGraphicsItem& other) const;
 
102
        /**
 
103
         * Stores the differences between the given last state and the current state into the tree model.
 
104
         */
 
105
        void storeChangedAttributes(TreeModel& model);
 
106
    void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
 
107
 
 
108
protected:
 
109
 
 
110
        /**
 
111
         * Called, when a mouse button is clicked:
 
112
         * <ol>
 
113
         *  <li>Left  mouse button: Begin moving   this item and showing moving   mouse cursor.</li>
 
114
         *  <li>Right mouse button: Begin resizing this item and showing resizing mouse cursor.</li>
 
115
         * </ol>
 
116
         */
 
117
        virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
 
118
 
 
119
        /**
 
120
         * Called, when mouse is moved after pressing left or right button
 
121
         * to move and resize interactively.
 
122
         * @see #mousePressEvent(QGraphicsSceneMouseEvent*)
 
123
         */
 
124
        virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
 
125
        /**
 
126
         * Called, when the mouse is released to show the normal mouse cursor.
 
127
         */
 
128
        virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
 
129
 
 
130
private:
 
131
        QModelIndex index;
 
132
        QList<BoxItemAttribute> attributes;
 
133
        bool isResizing;
 
134
        QPointF moveStartPos;
 
135
        QPointF resizeStartEventPos;
 
136
        QRectF resizeStartRect;
 
137
        QPointF lastPosMovingFired;
 
138
        QRectF lastRectResizingFired;
 
139
        QPointF rightBottonPos;
 
140
        QImage image;
 
141
        void setImage(const QString& filename);
 
142
        void fireItemMoving();
 
143
        void fireItemMoveFinished();
 
144
        void fireItemResizing();
 
145
        void fireItemResizeFinished();
 
146
        BoxGraphicsItemListener& getListener();
 
147
};
 
148
 
 
149
#endif /* BOXGRAPHICSITEM_H_ */