1
/* This file is part of Step.
2
Copyright (C) 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
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.
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.
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
19
#ifndef STEP_WORLDSCENE_H
20
#define STEP_WORLDSCENE_H
22
#include <QGraphicsScene>
23
#include <QGraphicsView>
27
#include "messageframe.h"
37
class WorldGraphicsItem;
38
class WorldGraphicsView;
47
/** \brief World scene class */
48
class WorldScene: public QGraphicsScene
53
typedef QList<const StepCore::MetaObject*> SnapList;
55
/** Flags for controlling item snapping behaviour */
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
64
Q_DECLARE_FLAGS(SnapFlags, SnapFlag)
66
/** Construct WorldScene */
67
explicit WorldScene(WorldModel* worldModel, QObject* parent = 0);
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;
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; }
80
/** Calculate united bounding rect of all items
81
* (not taking into account WorldSceneAxes */
82
QRectF calcItemsBoundingRect();
84
/** Highlight item at given 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);
90
/** Remove highlighting */
93
/** Attach item to another item at given 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)
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.
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);
113
/** Get associated WorldModel */
114
WorldModel* worldModel() const { return _worldModel; }
116
/** Check if scene has an active item creator */
117
bool hasItemCreator() const;
120
/** Begin adding new item. Creates appropriate ItemCreator */
121
void beginAddItem(const QString& name);
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);
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);
141
* \param id message id */
142
void closeMessage(int id) { _messageFrame->closeMessage(id); }
144
/** Reload application settings */
145
void settingsChanged();
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);
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);
159
void worldRowsInserted(const QModelIndex& parent, int start, int end);
160
void worldRowsAboutToBeRemoved(const QModelIndex& parent, int start, int end);
162
void messageLinkActivated(const QString& link);
164
void snapUpdateToolTip();
167
bool event(QEvent* event);
168
void mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent);
169
void helpEvent(QGraphicsSceneHelpEvent *helpEvent);
170
//void contextMenuEvent(QGraphicsSceneContextMenuEvent* contextMenuEvent);
172
void worldGetItemsRecursive(const QModelIndex& parent);
175
WorldModel* _worldModel;
176
WorldGraphicsView* _worldView;
177
QHash<const StepCore::Item*, WorldGraphicsItem*> _itemsHash;
178
double _currentViewScale;
179
ItemCreator* _itemCreator;
182
MessageFrame* _messageFrame;
183
WorldSceneAxes* _sceneAxes;
184
WorldGraphicsItem* _snapItem;
186
QString _snapToolTip;
189
friend class WorldGraphicsView;
192
/** \brief World view */
193
class WorldGraphicsView: public QGraphicsView
198
WorldGraphicsView(WorldScene* worldScene, QWidget* parent);
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%
206
/** Reload application settings */
207
void settingsChanged();
210
void sceneRectChanged(const QRectF& rect);
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();
219
static const int SCENE_LENGTH = 2000;
224
Q_DECLARE_OPERATORS_FOR_FLAGS(WorldScene::SnapFlags)