LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
iconlayout.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2010-2012 Hewlett-Packard Development Company, L.P.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * LICENSE@@@ */
18 
19 
20 
21 
22 #ifndef ICONLAYOUT_H_
23 #define ICONLAYOUT_H_
24 
25 #include <QObject>
26 #include <QList>
27 #include <QPointer>
28 #include <QRectF>
29 #include <QSizeF>
30 #include <QDebug>
31 #include <QTransform>
32 
33 #include "dimensionsglobal.h"
34 #include "scrollableobject.h"
35 #include "layoutitem.h"
36 
37 class IconBase;
38 class QPainter;
39 class IconRow;
40 class IconLayout;
41 class Page;
42 class PixmapObject;
43 class PixmapHugeObject;
44 
45 //utility classes, not to be used directly
46 class IconCell : public LayoutItem
47 {
48  Q_OBJECT
49 public:
50  IconCell(IconRow * p_owner);
51  virtual ~IconCell();
52  IconCell(IconRow * p_owner,IconBase * p_icon);
53  IconCell(IconRow * p_owner,IconBase * p_icon,const QSizeF& size);
54  IconCell(IconRow * p_owner,IconBase * p_icon,const QSize& size);
55 
56  virtual void clear();
57 
58  virtual QRectF geometry();
59  virtual QRectF relativeGeometry();
60  virtual QRectF untranslateGeometry(const QRectF& geom); //inverse of relativeGeometry()
61  virtual QPointF position();
62 
63  //returns the previously set icon, if any.
64  // it will call its owner IconRow's cellGeomChangedDueToNewIcon() function if the new icon's geom is incompatible
65  // with the current cell geom (since this will require re-layouts)
66  // the bool repositionIconToCell parameter, if true, will call IconBase->setPos() with the
67  // cell's position, which will instantly put the icon's position in line with the layout
68  // false will leave the icon's current position alone
69  virtual IconBase * setNewIcon(IconBase * p_newIcon,bool repositionIconToCell=false);
70 
71  //this one will set the associated icon's position to the cell's current position, which moves
72  // it back in line with the layout instantly.
73  virtual void resetIconPosition();
74 
75  //called as a part of reorder commit (the final stage of reorder) to swap the pending icon into the actual
76  //icon (m_qp_icon) variable herein. It also disables the icon's auto-paint and locks its position to the cell
77  // TAKE CARE THAT THE ICON THAT WAS PREVIOUSLY IN THIS CELL (OCCUPIED m_qp_icon) IS REFERENCED ELSEWHERE,
78  // OR ELSE IT'S GOING TO BE LOST AFTER THIS FN.
79  virtual void commitPendingIcon();
80 
81  //returns the old point/coord for convenience
82  QPointF reposition(const QPointF& newCenterPointICS);
83  qreal repositionX(const qreal x);
84  qreal repositionY(const qreal y);
85 
86  void moveBy(const QPointF& d);
87  void moveBy(const qreal dx,const qreal dy);
88 
89  virtual void paint(QPainter * painter);
90  // "clipping", but by only drawing from the source area specified. sourceRect guaranteed to be within
91  // m_geom space
92  // see IconLayout, IconRow functions of the same signature
93  virtual void paint(QPainter * painter, const QRectF& sourceRect);
94  virtual void paint(QPainter * painter, const QRectF& sourceRect,qint32 renderOpt);
95  virtual void paint(QPainter * painter, const QRectF& sourceRect,const QPointF& painterTranslate);
96  virtual void paint(QPainter * painter, const QRectF& sourceRect,const QPointF& painterTranslate,qint32 renderOpt);
97 
98  virtual void paintOffscreen(QPainter * painter);
99  virtual void paintOffscreen(PixmapObject * p_hugePmo);
100  virtual void paintOffscreen(PixmapHugeObject * p_hugePmo);
101 
102  QPointer<IconBase> m_qp_icon; //DOES NOT OWN the icon
103 
104  //this cell is the target of a reorder...the icon is referenced here while it is "in-flight"
105  // during the animation (if any). When a commit is run by the layout, this will be swapped into the
106  // main icon
107  QPointer<IconBase> m_qp_pendingReorderIcon;
108 
109  QRectF m_geom; //ICS
110  QPointF m_pos; //absolute ICS of *layout* (NOT Page!)
111 
112  QRect m_lastPaintedPixmapTargetRect; //records where the paintOffscreen functions last painted into a pixmap. This is to facilitate
113  // easy(er) retrieval of icons based on UI events (e.g. a mouse press on a pre-rendered pixmap)
114 
115  IconRow * m_p_layoutRowObject; //ptr back to the owning layout row
116 
117  friend QDataStream & operator<< (QDataStream& stream, const IconCell& s);
118  friend QDataStream & operator>> (QDataStream& stream, IconCell& s);
119  friend QDebug operator<<(QDebug dbg, const IconCell &s);
120 
121 Q_SIGNALS:
122 
123  void signalChangedIcon(const IconBase * p_icon);
124 };
125 
126 class IconRow : public LayoutItem
127 {
128  Q_OBJECT
129 public:
130  IconRow(IconLayout * p_owner);
131  IconRow(IconLayout * p_owner,const QRectF& geom);
132  virtual ~IconRow();
133 
134  QRectF m_geom; //real geometry - defined as 0,0 centered rect from icon[0].topleft to icon[last].bottomright
135  QPointF m_pos;
136 
137  QRect m_lastPaintedPixmapTargetRect; //records where the paintOffscreen functions last painted into a pixmap. This is to facilitate
138  // easy(er) retrieval of icons based on UI events (e.g. a mouse press on a pre-rendered pixmap)
139 
140  typedef QList<IconCell *> IconCellList;
141  typedef IconCellList::const_iterator IconCellListConstIter;
142  typedef IconCellList::iterator IconCellListIter;
143 
144  IconCellList m_iconList; //DOES NOT OWN the icons
145  IconLayout * m_p_layoutObject; //ptr back to owner IconLayout
146 
147  quint32 m_spaceToUpperAdjacentRow; //this is for bookkeeping. setting these doesn't auto-update positions to keep them valid
149 
150  quint32 numIcons() const;
151 
152  virtual QRectF geometry();
153  virtual QRectF relativeGeometry() const;
154  virtual void recomputeGeometry(bool adjustPosition=false);
155  virtual void repositionAboveRow(IconRow& refRow,const quint32 space);
156  virtual void repositionBelowRow(IconRow& refRow,const quint32 space);
157  virtual void repositionAboveRow(IconRow& refRow);
158  virtual void repositionBelowRow(IconRow& refRow);
159  virtual void moveBy(const QPointF& d);
160  virtual void moveBy(const qreal dx,const qreal dy);
161  virtual void appendCell(IconCell * p_cell);
162  virtual void addCell(IconCell * p_cell,const quint32 index); //index > list size = append
163  virtual void appendCellAndAlignVerticallyToCell(IconCell * p_cell,const quint32 alignToCellIndex);
164  virtual void addCellAndAlignVerticallyToCell(IconCell * p_cell,const quint32 index,const quint32 alignToCellIndex);
165  virtual void removeCell(const quint32 index); //index > list size = remove tail
166 
167  virtual void paint(QPainter * painter);
168 
169  // "clipping", but by only drawing from the source area specified. sourceRect guaranteed to be within
170  // m_geom space
171  // see IconLayout function of the same signature
172  virtual void paint(QPainter * painter, const QRectF& sourceRect);
173  virtual void paint(QPainter * painter, const QRectF& sourceRect,qint32 renderOpt);
174 
175  virtual void paint(QPainter * painter, const QRectF& sourceRect,const QPointF& painterTranslate);
176  virtual void paint(QPainter * painter, const QRectF& sourceRect,const QPointF& painterTranslate,qint32 renderOpt);
177 
178  virtual void paintOffscreen(QPainter * painter);
179  virtual void paintOffscreen(PixmapObject * p_pmo);
180  virtual void paintOffscreen(PixmapHugeObject * p_hugePmo);
181 
182  virtual void relayout(bool force=false);
183  virtual void relayout(const qreal leftEdgeX,const quint32 spacing,bool force);
184 
185  // redistributeIconsHorizontally(): will only touch horizontal positions of the icons, not the vertical.
186  //takes params to make it a little more generic, unlike relayout() which accesses owner layout vars directly
187  virtual void redistributeIconsHorizontally(const qreal leftEdgeX,const quint32 spacing);
188  virtual void alignIconsVerticallyCentered(const qreal yPosition);
189 
190  virtual void cellGeomChangedDueToNewIcon(IconCell * reportingCell);
191 
193  friend QDataStream & operator<< (QDataStream& stream, const IconRow& s);
194  friend QDataStream & operator>> (QDataStream& stream, IconRow& s);
195  friend QDebug operator<<(QDebug dbg, const IconRow &s);
196 };
197 
198 //TODO: can remove m_valid and use the Name INVALID...
200 {
201 public:
202 
203  enum Name
204  {
205  INVALID = 0,
206  Id, //special type used for synchronizing possibly interleaved op lists
210  };
211 
212  static QString Names[];
213 
214  IconOperation();
215  IconOperation(const Name op,const quint32 origin_column,const quint32 origin_row,
216  const quint32 destination_column,const quint32 destination_row);
217  IconOperation(const Name op,const quint32 origin_column,const quint32 origin_row,
218  const quint32 destination_column,const quint32 destination_row,
219  const quint32 seq);
220 
221  bool m_valid;
223  quint32 m_originRow;
224  quint32 m_originColumn;
227  quint64 m_sequenceNum; //optional, for reordering that occurs sequentially. 0 by default, > 0 if used
228  // also used for the id number when opname == Id
229  friend QDataStream & operator<< (QDataStream& stream, const IconOperation& s);
230  friend QDataStream & operator>> (QDataStream& stream, IconOperation& s);
231  friend QDebug operator<<(QDebug dbg, const IconOperation &s);
232 
233  static quint64 s_idcounter; //for id assignments
234 
235 };
236 typedef QList<IconOperation> IconOpList;
237 typedef IconOpList::const_iterator IconOpListConstIter;
238 typedef IconOpList::iterator IconOpListIter;
239 
240 class IconLayout : public LayoutItem
241 {
242  Q_OBJECT
243 public:
244 
245  friend class Page;
246 
247  IconLayout(Page * p_owner);
248  virtual ~IconLayout();
249 
250  /*
251  *
252  * This base class assumes a simple model of a fixed rectangular icon size (all icons are the same size)
253  * and fixed spacing between icons
254  *
255  */
256 
257  virtual void paint(QPainter * painter);
258  virtual void paint(const QPointF& translate,QPainter * painter);
259 
260  // (ScrollableObject-->ScrollingLayoutRenderer-->here)
261  // "clipping", but by only drawing from the source area specified. sourceRect guaranteed to be within
262  // m_geom space
263  virtual void paint(QPainter * painter, const QRectF& sourceRect);
264  virtual void paint(QPainter * painter, const QRectF& sourceRect,qint32 renderOpt);
265 
266  virtual void paint(QPainter * painter, const QRectF& sourceRect,const QPointF& painterTranslate);
267  virtual void paint(QPainter * painter, const QRectF& sourceRect,const QPointF& painterTranslate,qint32 renderOpt);
268 
269  virtual void paintOffscreen(QPainter * painter);
270  virtual void paintOffscreen(PixmapObject * p_pmo);
271  virtual void paintOffscreen(PixmapHugeObject * p_hugePmo);
272 
273  virtual void enableAutoPaint();
274  virtual void disableAutoPaint();
275 
276  virtual QRectF geometry();
277  virtual QRectF relativeGeometry();
278 
279  //relayout() sets absolute positioning within the item (Page or a Page-subclass) of layout, rows, and cells
280  virtual void relayout(bool force=false);
281  virtual void resizeWidth(const quint32 w);
282  virtual void setPosition(const QPointF& pos);
283  virtual QPointF position();
284  virtual void setUniformCellSize(const QSize& size);
285 
286  // intended to tell a cell that it is losing control of an icon (usually for moving/animating it to a new cell)
287  //TODO: make this more generic for layouts that may not use a system that can describe coordinates as x,y
288  virtual void iconCellReleaseIcon(const QPoint& cellCoordinate);
289 
290  virtual IconCell * iconCellAtLayoutCoordinate(const QPointF& coordinate);
291  virtual IconCell * iconCellAtLayoutCoordinate(const QPointF& layoutCoordinate,QPoint& r_gridCoordinate);
292 
293  //returns the rect area of the row specified, in layout CS (it's the geom of the row, translated to its layout CS position)
294  virtual QRectF rowArea(quint32 rowIndex) const;
295  virtual qint32 rowAtLayoutCoordinate(const QPointF& layoutCoordinate,bool clipMinMax=false);
296 
297  //uses the owner page as a reference CS, and maps the given point to the scene...equivalent to calling mapToScene() from inside the page, but
298  // this is a convenience pass-through so cells and rows and things that have references to the layout can use mapToScene from page's context.
299  // if there is no owner page, then the point passed in is returned unmodified
300  virtual QPointF sceneCoordinateFromLayoutCoordinate(const QPointF& layoutCoordinate);
301  // and its helper (see Page::pageCoordinateFromLayoutCoordinate)
302  virtual QPointF pageCoordinateFromLayoutCoordinate(const QPointF& layoutCoordinate);
303 
304  virtual QPointF rawPageCoordinateFromLayoutCoordinate(const QPointF& layoutCoordinate) const;
305  virtual QPointF rawLayoutCoordinateFromPageCoordinate(const QPointF& pageCoordinate) const;
306  virtual QRectF rawPageRectFromLayoutRect(const QRectF& layoutRect) const;
307  virtual QRectF rawLayoutRectFromPageRect(const QRectF& pageRect) const;
308 
309  virtual void recomputePageLayoutTransforms();
310 
311  virtual qreal verticalDistanceToNearestUpperRow(const QPointF& layoutCoordinate);
312  virtual qreal verticalDistanceToNearestLowerRow(const QPointF& layoutCoordinate);
313 
314  virtual quint32 maximumRowHeight() const;
315  virtual quint32 minimumRowHeight() const;
316 
317  // all the icon cells, starting with the leftmost cell of the top row, and ending with the rightmost cell in the bottom row
318  //WARNING: DO NOT HOLD REF TO ANYTHING FROM THE RETURN OF THIS FN. It could be invalidated at any time (whenever an add/remove or relayout happens)
319  virtual QList<IconCell *> iconCellsInFlowOrder();
320 
321  friend QDataStream & operator<< (QDataStream& stream, const IconLayout& s);
322  friend QDataStream & operator>> (QDataStream& stream, IconLayout& s);
323  friend QDebug operator<<(QDebug dbg, const IconLayout &s);
324 
325 protected:
326 
327 // static IconRow& removeIconFromIconRow(IconRow& ref_iconRow,const quint32 index); //in place remove and shift of the icon in the row
328 
329  QPointer<Page> m_qp_ownerPage;
330  QRectF m_geom;
331  QPointF m_pos; //pos is in term of Page ICS
332  QTransform m_layoutToPageTran;
333  QTransform m_pageToLayoutTran;
334 };
335 
336 QDataStream & operator<< (QDataStream& stream, const IconCell& s);
337 QDataStream & operator>> (QDataStream& stream, IconCell& s);
338 QDataStream & operator<< (QDataStream& stream, const IconRow& s);
339 QDataStream & operator>> (QDataStream& stream, IconRow& s);
340 QDataStream & operator<< (QDataStream& stream, const IconOperation& s);
341 QDataStream & operator>> (QDataStream& stream, IconOperation& s);
342 QDataStream & operator<< (QDataStream& stream, const IconLayout& s);
343 QDataStream & operator>> (QDataStream& stream, IconLayout& s);
344 
345 QDebug operator<<(QDebug dbg, const IconCell &s);
346 QDebug operator<<(QDebug dbg, const IconRow &s);
347 QDebug operator<<(QDebug dbg, const IconOperation &s);
348 QDebug operator<<(QDebug dbg, const IconLayout &s);
349 
350 #endif /* ICONLAYOUT_H_ */