2
Copyright (c) 2008 Ambroz Bizjak <ambro@b4ever.net>
4
This program 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.
19
* Implements "push and pull" dynamics of rectangular items in 2D space.
21
* All isolated motion folows these rules:
22
* - overlapping items stay at the same positions relative to each other
23
* - non-overlapping items stay such and do not jump one over another
25
* There are two types of motion:
26
* - forced motion: an item moves all items on its way, even if any of them
27
* would intersect the border of the working area
28
* - non-forced motion: an item moves items on its way only as much as the border
31
* Items are pushed to fit inside the working area:
32
* if an item is in the way of one of the borders of alignment, the move is forced;
33
* if an item is in the way of one of the opposite borders, the move is non-forced.
35
* An item can have a "preferred position". Such item is moved non-forced in the
36
* direction towards the preferred position.
43
void setWorkingArea(const QSizeF& area);
46
* Returns the visibility of an item at a given position.
47
* This is the part of the item inside the working area.
49
qreal positionVisibility(const QRectF& geom);
54
QPointF preferredPosition;
57
bool animateMovement : 1;
67
Q_DECLARE_FLAGS(Direction, DirectionFlag)
71
PushAwayFromPreferred = 1,
74
Q_DECLARE_FLAGS(PushPower, PushPowerFlag)
77
* Push an item group. Requires no initialization.
79
* @param groupId the index of the group
80
* @param direction in which direction pushing will be done
81
* @param amount how much to push
82
* @param power how 'powerful' the push is; what types of obstacles
83
* can be pushed or ignored
85
* @return how much the item group was really pushed
87
qreal performPush(int groupId, Direction direction, qreal amount, PushPower power);
91
* Groups will be updated to reflect the change.
93
* @param newItem the item to add; must be initialized
95
void addItem(ItemSpaceItem newItem);
98
* Removes an item by its location.
100
* @param groupIndex the index of the item's group
101
* @param itemInGroup the index of the item in its group
103
void removeItem(int groupIndex, int itemInGroup);
106
* Move the item to a new position.
108
* @param groupIndex the index of the item's group
109
* @param itemInGroup the index of the item in its group
110
* @param newGeom the new geometry of the item
112
void moveItem(int groupIndex, int itemInGroup, const QRectF& newGeom);
115
* Resize an item. The item's alignment corner will be the center of resizing.
117
* @param groupId the index of the group
118
* @param direction in which direction pushing will be done
119
* @param newSize the item's new size
121
void resizeItem(int groupId, int itemInGroup, const QSizeF& newSize);
124
* Offset the positions of all items.
126
void offsetPositions(const QPointF &offset);
129
* Find an item by its number as if we iterated over
130
* all groups and over all items in each group.
132
bool locateItemByPosition(int pos, int *groupIndex, int *itemInGroup) const;
135
* Find an item by its 'user' parameter.
137
bool locateItemByUser(QVariant user, int *groupIndex, int *itemInGroup) const;
140
* Finds an empty place for an item.
141
* Tries to stack the item vertically, starting in the corner
142
* of alignment, and advances horizontally once no more positions
145
* @param itemSize the size of the item; placementSpacing is already
147
* @param align the corner of the screen where position testing will
148
* begin (and in what directions it will advance)
149
* must be an OR of Qt::AlignLeft or Qt::AlignRight
150
* and Qt::AlignTop or Qt::AlignBottom
151
* @param limitedSpace if true, positions outside the working area
152
* will not be considered; otherwise, positions
153
* will only be limited by the borders at the
155
* @param findAll if false, searching will stop after the first valid
158
* @return all positions found
160
QList<QPointF> positionVertically(
161
const QSizeF &itemSize,
167
bool positionedProperly(const QRectF& itemGeom);
170
* Represents a group of overlapping items.
180
* Create a push request. No calculations will be performed.
182
* @param sourceGroup the group that posted the request, or
183
* -1 if it was posted manually.
184
* @param sourceGroupPushRequested how much the posting group wanted
185
* to move itself when the request was
186
* posted (if sourceGroup is -1)
187
* @param pushRequested how much the group concerned is asked to move
191
qreal sourceGroupPushRequested,
196
* Perform obstacle searching and post push request to obstacle groups.
197
* This is the main method involved in recursive push calculation.
199
* If an item is found to be in the way of any of the group's items,
200
* its ItemGroup will be created if it doesn't have one already,
201
* and a new push request will bo posted to it.
203
* If the offending group can not move as much as we need it to,
204
* we limit the amount our group wants to move, and future obstacles
205
* will be asked to move less than they would have been had there
208
* @param group the ItemGroup this push request belongs to
210
void activate(ItemSpace *itemSpace, ItemGroup *group);
212
// saved from constructor
214
qreal m_sourceGroupPushRequested;
215
qreal m_pushRequested;
217
// true if the request has already been reached by applyResults
218
// and compensated for the reduction of the requester's move
222
void resetPush(int id);
225
* Post a move request.
226
* This adds the request to the group and calls activate on it.
228
void addRequest(ItemSpace *itemSpace, const class Request &request);
231
* Apply the results of initial push calculation, moving the items.
233
* For each push request belonging to the calling/requesting group,
234
* the requesting group is checked for how much it still wants to
235
* move itself, and the value is compared to how much it wanted to
236
* when the request was posted. The amount of the request is reduced
239
* If all requests have been compensated, it updates the amount it
240
* would like to move (the maximum of all move requests) and
241
* physically moves its items. In that case it also calls applyResults
242
* on the item groups it has requested to move, which will see the new
244
* (Otherwise, another requesting group will reach it later on.)
246
void applyResults(ItemSpace *itemSpace, int cameFrom);
248
// items belonging to this group
249
QList<ItemSpaceItem> m_groupItems;
251
// the list index of this group in the calculation process
253
// the maximum of all push requests
254
qreal m_largestPushRequested;
255
// the available space calculated so-far
256
qreal m_pushAvailable;
259
// return true if the group is above this one (its requests lead here)
260
bool groupIsAbove(ItemSpace *itemSpace, QList<int> &visited, int groupId);
262
// move requests posted to this group
263
QList<Request> m_requests;
264
// groups we asked to move
265
QList<int> m_obstacles;
271
QList<ItemGroup> m_groups;
273
Qt::Alignment spaceAlignment;
276
qreal placementSpacing;
278
qreal shiftingSpacing;
282
void linkItem(ItemSpaceItem newItem);
283
void unlinkItem(int removeGroup, int removeItemInGroup);
286
* Prepare for pushing.
287
* After that, move requests can be posted to item groups
288
* with ItemGroup::addRequest and the move can be performed
289
* with ItemGroup::applyResults.
291
* @param direction in which direction pushing will be done
292
* @param power how 'powerful' the push is; what types of obstacles
293
* can be pushed or ignored
295
void preparePush(Direction direction, PushPower power);
298
* Look for items overlapping with working area borders and move them inside as much as possible.
303
* Look for items not in their preferred positions and move them back as much as possible.
305
void checkPreferredPositions();
307
QRectF itemInRegionStartingFirstVert(const QRectF ®ion) const;
308
QRectF itemInRegionEndingLastVert(const QRectF ®ion) const;
309
QRectF itemInRegionEndingFirstHoriz(const QRectF ®ion) const;
310
QRectF itemInRegionStartingLastHoriz(const QRectF ®ion) const;
312
Direction m_direction;