~ubuntu-branches/ubuntu/oneiric/step/oneiric-proposed

« back to all changes in this revision

Viewing changes to step/worldscene.h

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-13 16:03:30 UTC
  • Revision ID: james.westby@ubuntu.com-20110713160330-f9e5axd7kc76vorj
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Step.
 
2
   Copyright (C) 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
 
3
 
 
4
   Step 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 2 of the License, or
 
7
   (at your option) any later version.
 
8
 
 
9
   Step 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 Step; if not, write to the Free Software
 
16
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
*/
 
18
 
 
19
#ifndef STEP_WORLDSCENE_H
 
20
#define STEP_WORLDSCENE_H
 
21
 
 
22
#include <QGraphicsScene>
 
23
#include <QGraphicsView>
 
24
#include <QList>
 
25
#include <QHash>
 
26
 
 
27
#include "messageframe.h"
 
28
 
 
29
class KUrl;
 
30
class WorldModel;
 
31
//class ItemCreator;
 
32
class QModelIndex;
 
33
class QGraphicsItem;
 
34
class QItemSelection;
 
35
class QVBoxLayout;
 
36
class QSignalMapper;
 
37
class WorldGraphicsItem;
 
38
class WorldGraphicsView;
 
39
class ItemCreator;
 
40
class WorldSceneAxes;
 
41
 
 
42
namespace StepCore {
 
43
    class Item;
 
44
    class MetaObject;
 
45
}
 
46
 
 
47
/** \brief World scene class */
 
48
class WorldScene: public QGraphicsScene
 
49
{
 
50
    Q_OBJECT
 
51
 
 
52
public:
 
53
    typedef QList<const StepCore::MetaObject*> SnapList;
 
54
 
 
55
    /** Flags for controlling item snapping behaviour */
 
56
    enum SnapFlag {
 
57
        SnapOnCenter = 1,         ///< Snap to the center of the body
 
58
        SnapSetPosition = 2,      ///< Set position property
 
59
        SnapSetAngle = 4,         ///< Set angle property
 
60
        SnapSetLocalPosition = 8, ///< Set localPosition property
 
61
        SnapParticle = 256,         ///< Allow snapping to Particle
 
62
        SnapRigidBody = 512        ///< Allow snapping to RigidBody
 
63
    };
 
64
    Q_DECLARE_FLAGS(SnapFlags, SnapFlag)
 
65
 
 
66
    /** Construct WorldScene */
 
67
    explicit WorldScene(WorldModel* worldModel, QObject* parent = 0);
 
68
    ~WorldScene();
 
69
 
 
70
    /** Get StepCore::Item by QGraphicsItem */
 
71
    StepCore::Item* itemFromGraphics(const QGraphicsItem* graphicsItem) const;
 
72
    /** Get WorldGraphicsItem for given StepCore::Item */
 
73
    WorldGraphicsItem* graphicsFromItem(const StepCore::Item* item) const;
 
74
 
 
75
    /** Called by WorldView when view scale is updated */
 
76
    void updateViewScale(); // Qt4.3 can help here
 
77
    /** Get current view scale of the scene */
 
78
    double currentViewScale() { return _currentViewScale; }
 
79
 
 
80
    /** Calculate united bounding rect of all items
 
81
     *  (not taking into account WorldSceneAxes */
 
82
    QRectF calcItemsBoundingRect();
 
83
 
 
84
    /** Highlight item at given position
 
85
     *  \param pos position
 
86
     *  \param flags snap flags
 
87
     *  \param moreTypes additional item types to snap */
 
88
    StepCore::Item* snapHighlight(QPointF pos, SnapFlags flags, const SnapList* moreTypes = 0);
 
89
 
 
90
    /** Remove highlighting */
 
91
    void snapClear();
 
92
 
 
93
    /** Attach item to another item at given position
 
94
     *  \param pos position
 
95
     *  \param flags snap flags
 
96
     *  \param moreTypes additional item types to snap
 
97
     *  \param movingState moving state of the item
 
98
     *  \param item StepCore::Item to attach
 
99
     *  \param num Num of the end to attach (or -1)
 
100
     *
 
101
     *  If movingState equals Started or Moving this function
 
102
     *  will only highlight potential body to attach and leave current
 
103
     *  body detaches. It movingState equals Finished the function
 
104
     *  will actually attach the body.
 
105
     *
 
106
     *  This function sets "body" property of the item to snapped item
 
107
     *  and "position" and/or "localPosition" property to the position
 
108
     *  on snapped item. If num >=0 then QString::number(num) is added
 
109
     *  to property names */
 
110
    StepCore::Item* snapItem(QPointF pos, SnapFlags flags, const SnapList* moreTypes,
 
111
                                  int movingState, StepCore::Item* item, int num = -1);
 
112
 
 
113
    /** Get associated WorldModel */
 
114
    WorldModel* worldModel() const { return _worldModel; }
 
115
 
 
116
    /** Check if scene has an active item creator */
 
117
    bool hasItemCreator() const;
 
118
 
 
119
public slots:
 
120
    /** Begin adding new item. Creates appropriate ItemCreator */
 
121
    void beginAddItem(const QString& name);
 
122
 
 
123
    /** Shows a message to the user
 
124
     *  \param type message type
 
125
     *  \param text message text
 
126
     *  \param flags message flags
 
127
     *  \return message id of the created message */
 
128
    int showMessage(MessageFrame::Type type, const QString& text, MessageFrame::Flags flags = 0) {
 
129
        return _messageFrame->showMessage(type, text, flags);
 
130
    }
 
131
    /** Changed existing message
 
132
     *  \param id message id
 
133
     *  \param type message type
 
134
     *  \param text message text
 
135
     *  \param flags message flags
 
136
     *  \return new message id */
 
137
    int changeMessage(int id, MessageFrame::Type type, const QString& text, MessageFrame::Flags flags = 0) {
 
138
        return _messageFrame->changeMessage(id, type, text, flags);
 
139
    }
 
140
    /** Close message
 
141
     *  \param id message id */
 
142
    void closeMessage(int id) { _messageFrame->closeMessage(id); }
 
143
 
 
144
    /** Reload application settings */
 
145
    void settingsChanged();
 
146
    
 
147
signals:
 
148
    /** This signal is emitted when item creation is finished or canceled */
 
149
    void endAddItem(const QString& name, bool success);
 
150
    /** This signal is emitted when a link in the message is activated */
 
151
    void linkActivated(const KUrl& url);
 
152
 
 
153
protected slots:
 
154
    void worldModelReset();
 
155
    void worldDataChanged(bool dynamicOnly);
 
156
    void worldCurrentChanged(const QModelIndex& current, const QModelIndex& previous);
 
157
    void worldSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
 
158
    
 
159
    void worldRowsInserted(const QModelIndex& parent, int start, int end);
 
160
    void worldRowsAboutToBeRemoved(const QModelIndex& parent, int start, int end);
 
161
 
 
162
    void messageLinkActivated(const QString& link);
 
163
 
 
164
    void snapUpdateToolTip();
 
165
 
 
166
protected:
 
167
    bool event(QEvent* event);
 
168
    void mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent);
 
169
    void helpEvent(QGraphicsSceneHelpEvent *helpEvent);
 
170
    //void contextMenuEvent(QGraphicsSceneContextMenuEvent* contextMenuEvent);
 
171
 
 
172
    void worldGetItemsRecursive(const QModelIndex& parent);
 
173
 
 
174
protected:
 
175
    WorldModel* _worldModel;
 
176
    WorldGraphicsView* _worldView;
 
177
    QHash<const StepCore::Item*, WorldGraphicsItem*> _itemsHash;
 
178
    double _currentViewScale;
 
179
    ItemCreator* _itemCreator;
 
180
    QRgb         _bgColor;
 
181
 
 
182
    MessageFrame*  _messageFrame;
 
183
    WorldSceneAxes* _sceneAxes;
 
184
    WorldGraphicsItem* _snapItem;
 
185
    QPointF            _snapPos;
 
186
    QString            _snapToolTip;
 
187
    QTimer*            _snapTimer;
 
188
 
 
189
    friend class WorldGraphicsView;
 
190
};
 
191
 
 
192
/** \brief World view */
 
193
class WorldGraphicsView: public QGraphicsView
 
194
{
 
195
    Q_OBJECT
 
196
 
 
197
public:
 
198
    WorldGraphicsView(WorldScene* worldScene, QWidget* parent);
 
199
 
 
200
public slots:
 
201
    void zoomIn();      ///< Zoom scene in
 
202
    void zoomOut();     ///< Zoom scene out
 
203
    void fitToPage();   ///< Ensure that all objects are visible
 
204
    void actualSize();  ///< Set zoom to 100%
 
205
 
 
206
    /** Reload application settings */
 
207
    void settingsChanged();
 
208
 
 
209
protected slots:
 
210
    void sceneRectChanged(const QRectF& rect);
 
211
    
 
212
protected:
 
213
    void mousePressEvent(QMouseEvent* e);
 
214
    void mouseReleaseEvent(QMouseEvent* e);
 
215
    void wheelEvent(QWheelEvent* e);
 
216
    void scrollContentsBy(int dx, int dy);
 
217
    void updateSceneRect();
 
218
 
 
219
    static const int SCENE_LENGTH = 2000;
 
220
    
 
221
    QRectF _sceneRect;
 
222
};
 
223
 
 
224
Q_DECLARE_OPERATORS_FOR_FLAGS(WorldScene::SnapFlags)
 
225
 
 
226
#endif
 
227