LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
alphabeticonlayout.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 ALPHABETICONLAYOUT_H_
23 #define ALPHABETICONLAYOUT_H_
24 
25 #include "iconlayout.h"
26 #include "icon.h"
27 
28 #include <QList>
29 #include <QString>
30 #include <QChar>
31 #include <QDebug>
32 #include <QMap>
33 #include <QSet>
34 #include <QPair>
35 
36 class AlphabetIconLayout;
37 class AlphabetPage;
38 class PixmapObject;
39 class HorizontalDivider;
42 class QStateMachine;
43 class QState;
44 class QAbstractAnimation;
45 class QPropertyAnimation;
46 
48 {
49 public:
50  quint32 m_startRowNum;
51  quint32 m_endRowNum;
52 };
53 class IconRowAlpha : public IconRow
54 {
55 public:
57  IconRowAlpha(AlphabetIconLayout * p_owner,const QString& alphaDesignator);
58  IconRowAlpha(AlphabetIconLayout * p_owner,const QChar& alphaDesignator);
59  virtual ~IconRowAlpha();
60 
61  virtual void appendCell(IconCell * p_cell);
62  virtual void addCell(IconCell * p_cell,const quint32 index); //index > list size = append
63  virtual void appendCellAndAlignVerticallyToCell(IconCell * p_cell,const quint32 alignToCellIndex);
64  virtual void addCellAndAlignVerticallyToCell(IconCell * p_cell,const quint32 index,const quint32 alignToCellIndex);
65 
66  virtual void paintOffscreen(PixmapHugeObject * p_hugePmo);
67 
68  //remove() is done by the IconRow base class...remove fn is the same regardless of alpha nature of this row
69 
70  static bool iconCellLessThan(const IconCell * p_a, const IconCell * p_b);
71 
72  virtual void relayout(bool force=false);
73 
75  bool m_layoutSync; //true if no append/add/remove has occured since last relayout of the row and cell geoms
76  // false if a relayout is needed due to a mod of the icon list
77  // expected to be used by the AlphabetIconLayout class to keep track of batched mods
78 
79  friend QDataStream & operator<< (QDataStream& stream, const IconRowAlpha& s);
80  friend QDataStream & operator>> (QDataStream& stream, IconRowAlpha& s);
81  friend QDebug operator<<(QDebug dbg, const IconRowAlpha &s);
82 };
83 
85 {
86  Q_OBJECT
87 public:
88 
89  friend class IconRowAlpha;
90  friend class AlphabetPage;
91 
93  virtual ~AlphabetIconLayout();
94 
95  QRectF constGeometry() const { return m_geom; }
96 
99  const IconList iconList);
100 
101  //this version disregards m_intraAlphaRowSpace and m_interAlphaRowSpace and resets them to rowSpace
102  virtual void setLayoutUniformRowSpacing(const qreal rowSpace,const quint32 anchorRowNum=0);
103  //variant that allows indiv. spec
104  virtual void setLayoutRowSpacing(const qreal intraAlphaRowSpace,
105  const qreal interAlphaRowSpace,
106  const quint32 anchorRowNum=0);
107 
108  //this version does it according to whatever is stored in IconLayoutSettings
110 
111  //each returns a list of operations needed to move the OTHER icons around (icons already in the layout)
112  virtual QList<IconOperation> addIconAt(quint32 row,quint32 column);
113  virtual QList<IconOperation> removeIconFrom(quint32 row,quint32 column);
114 
115  virtual QList<quint32> rowListForAlpha(const QString& alphaDesignator);
116 
117  virtual void paint(QPainter * painter);
118  virtual void paint(const QPointF& translate,QPainter * painter);
119  // (ScrollableObject-->ScrollingLayoutRenderer-->here)
120  // "clipping", but by only drawing from the source area specified. sourceRect guaranteed to be within
121  // m_geom space
122  virtual void paint(QPainter * painter, const QRectF& sourceRect);
123  virtual void paint(QPainter * painter, const QRectF& sourceRect,qint32 renderOpt);
124 
125  virtual void paintOffscreen(QPainter * painter);
126  virtual void paintOffscreen(PixmapObject * p_pmo);
127  virtual void paintOffscreen(PixmapHugeObject * p_hugePmo);
128 
129  virtual void enableAutoPaint();
130  virtual void disableAutoPaint();
131 
132  virtual void relayout(bool force=false);
133  virtual void resizeWidth(const quint32 w);
134  virtual void setPosition(const QPointF& pos);
135  virtual void setUniformCellSize(const QSize& size);
136 
137  virtual IconCell * iconCellAtLayoutCoordinate(const QPointF& coordinate);
138  virtual IconCell * iconCellAtLayoutCoordinate(const QPointF& layoutCoordinate,QPoint& r_gridCoordinate);
139 
140  virtual IconCell * findIconByUid(const QUuid& iconUid,QPoint& r_gridCoordinate);
141 
142  virtual qreal verticalDistanceToNearestUpperRow(const QPointF& layoutCoordinate);
143  virtual qreal verticalDistanceToNearestLowerRow(const QPointF& layoutCoordinate);
144 
145  virtual quint32 maximumRowHeight() const;
146  virtual quint32 minimumRowHeight() const;
147 
148  friend QDataStream & operator<< (QDataStream& stream, const AlphabetIconLayout& s);
149  friend QDataStream & operator>> (QDataStream& stream, AlphabetIconLayout& s);
150  friend QDebug operator<<(QDebug dbg, const AlphabetIconLayout &s);
151 
153 
154  // returns false if can't track at the moment
155  // if return == true, then the r_iconUid param holds value1 = ORIGINAL icon uid, and value2 = COPY (floating icon; the one being
156  // designed so that iconCellAtLayoutCoordinate() result can be passed right in
157  // (it will correctly handle null (not found) icon cell)
158  virtual bool startTrackingIcon(const QPointF& layoutCoordinate,QPair<QUuid,QUuid>& r_iconUids);
159  //trackedIconLeavingLayout(): used instead of stopTrackingIcon() when the icon has changed hands;
160  // e.g. been moved to another page/layout
161  virtual bool trackedIconLeavingLayout(const QUuid& trackedIconUid);
162  //stopTrackingIcon(): used when the icon has stayed in the page/layout, but just been dropped
163  // by the user
164  virtual void stopTrackingIcon(const QUuid& trackedIconUid);
165  //provide the TRACKED icon's uid, not the original's uid
166  virtual IconBase * getTrackedIcon(const QUuid& trackedIconUid);
167 
168  virtual void stopTrackingAll();
169  virtual bool commitTracked(const QUuid& iconUid);
170 
171  //animationForTrackedIconFinal(): create and return an animation that is made to "finalize" the moved icon. In the case of an alpha layout,
172  // this will simply make the icon disappear.
173  // NOTE: when the icon is actually sent to another page, than THAT page will handle its final animation,
174  // so it would not be correct to create animations here that do this type of thing.
175  virtual QAbstractAnimation * animationForTrackedIconFinal(const QUuid& trackedIconUid);
176 
177  //Just like stopTracking__() and commit___() operate under normal circumstances and try to let animations finish,
178  //cancelAllReorder() kills all animations instantly and immediately locks all icons into their final state, as if
179  // the normal reorders were allowed to finish. This is useful in case of some drastic UI change, like an incoming call,
180  // a rotation of the screen, the launcher being dismissed, apps being deleted, etc...
181  // the function has no return value because it cannot fail; if it fails to do anything, then a crash will likely follow
182  // (or at the very least, a very inconsistent, mostly unusable launcher UI state)
183  virtual void cancelAllReorder();
184  virtual void commitTrackedImmediately(const QUuid& iconUid);
185 
186  virtual bool removeIconCell(const QPoint& gridCoordinate);
187 
188 protected:
189  typedef QList<IconRowAlpha *> IconRowAlphaList;
190  typedef IconRowAlphaList::const_iterator IconRowConstIter;
191  typedef IconRowAlphaList::iterator IconRowIter;
192  IconRowAlphaList m_iconRows; //always sorted in order of IconRow::m_logicalRowNumber,
193  //which for the alpha layout is also alpha order
194 
195  typedef QMap<QString,HorizontalDivider *> RowDividerMap;
196  typedef RowDividerMap::const_iterator RowDividerMapConstIter;
197  typedef RowDividerMap::iterator RowDividerMapIter;
199 
200  bool m_layoutSync; //true if no append/add/remove has occured since last relayout of the row and cell geoms
201  // false if a relayout is needed due to a mod of the icon list
202  // expected to be used by owner Page to keep track of batched mods
203  // (see IconRowAlpha)
204  quint32 m_relayoutCount; //every time relayout() is run, this gets incremented. It's used for both positioning initially,
205  // when no anchor row has a valid position, and also for statistics and performance monitoring
206 
207  bool m_disabledPaint; //keeps the layout from painting itself (see paint())
208 
210 
211  //all units here in pixels
215 
216  // Layout adjustments - all of this comes from IconLayoutSettings
222 
223 
224  quint32 m_intraAlphaRowSpace; //between two rows in the same alpha designator
225  quint32 m_interAlphaRowSpace; //between two rows in adjacent alpha designators;
226  //make sure to leave room for separator bar, etc
227 
228  quint32 m_anchorRow; //the current anchor row (from which (re)layouts are performed)
229 
230  QSize m_iconCellSize; //calculated from page size and m_maxIconsPerRow...see initLayout...()
231  quint32 m_horizIconSpacing; //(ditto)...takes into account m_horizIconSpacingAdjust;
232 
233  QSize m_layoutSizeInPixels; //for rendering offscreen
234 
235  quint32 m_minRowHeight;
236  quint32 m_maxRowHeight;
237 
238 protected:
239 
240  //disposes of the icon copy made while tracking a floating icon ("reorder", 'cept alphalayouts don't really reorder)
241  virtual void destroyTrackedIcon(const QUuid& iconUid);
242 
243  //deletes all the rows (but not the cells contained) and re-inits my state variables to reflect the change
245  virtual void destroyAllRows();
246 
247  // all the icon cells, starting with the leftmost cell of the top row, and ending with the rightmost cell in the bottom row
248  //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)
249  virtual QList<IconCell *> iconCellsInFlowOrder();
250 
251  //TODO: return an error code as appropriate; calculations can fail if either the page width is too small or icon size if too great
253 
254  /*
255  * when resizing the layout when it has already been done, there is a potential that rows may need to be added
256  * (when the width shrinks) or rows may need to be removed (when the width expands)
257  *
258  * This function will handle these adds and removes. It is a counterpart to relayout(); that function is kind of a misnomer
259  * since it doesn't really RE-layout...it is more for initial layouts
260  *
261  * TODO: initially, this will delete all the rows but keep the cells, and then recreate the rows, adding/removing as necessary.
262  * This can be made more efficient, but I don't have time right now, due to Demo-Demo-Demo!
263  */
264  virtual void relayoutExisting();
265 
266  static bool iconRowLessThan(const IconRowAlpha * p_a,const IconRowAlpha * p_b);
267 
268  /*
269  * TODO: a higher level function that does true alpha insert (see caution)
270  *
271  * CAUTION: not a true alphabetic insert! It only inserts into the proper alpha ROW. It will NOT re-alpha-sort the icons within the row
272  * and more importantly, it will not swap between rows that are the same Alpha, in the case where an extra row of the same Alpha was added
273  * because the max number of icons per row were reached in the existing ones.
274  * The way to use this function to maintain true alphabetic order within rows and between rows is to pre-alphasort a list of icons to be inserted
275  * and then call this function sequentially on the list
276  *
277  * Inserts the icon into the row where it alphabetically belongs.
278  * This is totally different than the addIconAt function which isn't "smart"; this function actually
279  * chooses the row and column where the icon belongs.
280  * Mostly useful for initializing a layout initially. Generally NOT useful at user-visible runtime since
281  * it will not calculate any of the animations needed to display the "add"
282  *
283  * Also, it will not automatically adjust the layout geoms of the icon cells. It is expected a caller
284  * will be handling this.
285  *
286  * returns either 0 if it inserted into an existing row or took no action, or (row index)+1 if it had to insert a new row
287  * this value helps the caller determine if it needs to run a relayout of rows, and possibly optimize the relayout
288  * over only some rows
289  *
290  * It also sets the m_layoutSync variable if any layout state was modified (this disambiguates return=0)
291  *
292  */
293  virtual quint32 insertAlphabetically(IconBase * p_icon);
294 
295  //this is its counterpart, used for relayoutExisting()
296  virtual quint32 insertAlphabetically(IconCell * p_cell);
297 
298  virtual HorizontalLabeledDivider * createNewDivider(const QString& alphaLabel);
299  virtual QSize horizontalDividerSize();
300 
301  QPropertyAnimation * fadeAnimationForTrackedIcon(IconBase * p_icon);
302 
303 Q_SIGNALS:
304 
305  // These two signals are the overall indicators of when a reordering begins and ends.
306  // reordering here means more than just "reorder pending" (vs moving/float icon tracking),
307  // it means *overall* reorder , i.e. the state is not consistent from the POV of the outside world
308  void signalReorderStarted();
309  void signalReorderEnded();
310 
311  // these are for the FSM !ONLY!
314  void signalFSMLastTrackEndedTrigger(); //TODO: see slotTrackForIconEnded() below
317 
318 protected Q_SLOTS:
319 
320  virtual void slotTrackedIconAnimationFinished();
321 
322  //TODO: TEMP: the FSM should actually count the number of in-flight trackings and switch states accordingly
323  // Qt's guarded (conditional) transitions are kind of weird so i'm holding off on implementing this for now
324  // this function will check the number of in-flights and emit the signalFSMLastTrackEndedTrigger as appropriate
325  // I'm weaving it this way with a signal->slot inside this (same) class - usually a bad idea - to minimize the
326  // changes needed when this gets done correctly
327  virtual void slotTrackForIconEnded();
328 
329  virtual void dbg_reorderFSMStateEntered();
330 
331 protected:
332 
333  virtual void setupReorderFSM();
334  virtual void startReorderFSM();
335  virtual void stopReorderFSM();
336 
337  virtual bool isReorderStateConsistent() const;
338 
339  virtual void switchIconsToReorderGraphics();
340  virtual void switchIconsToNormalGraphics();
341  virtual void switchIconToReorderGraphics(IconCell * p_iconCell);
342  virtual void switchIconToNormalGraphics(IconCell * p_iconCell);
343  virtual void switchIconToReorderGraphics(IconBase * p_icon);
344  virtual void switchIconToNormalGraphics(IconBase * p_icon);
345 
346 protected:
347 
348  quint32 m_reorderEventSampleRate; //determines when to check a moving icon against the layout to see what
349  // if anything, needs to be reordered. For now, it will count instances
350  // of "move" events (e.g. mouseMove) but I'll add other ways to threshold
351  // This is useful because checking boundaries on icons and rows in very large
352  // layouts could be expensive, as is generating operation lists and animations
353 
355  QMap<QUuid,QPointer<QAbstractAnimation> > m_trackedIconAnimations; //key = the icon being animated, which is the COPY in this case
356  QMap<QUuid,QPointer<IconBase> > m_trackedIcons; //key = uid of the COPY of the icon on the layout that was "picked up"
357  // value = the COPY that's being moved around
358  //The map of the icon back to the ORIGINAL icon on the layout is not needed, because the icon heap holds all
359  //this info already - IconHeap::getIconEx() will get this info in the attributes. Use the copy hint for slightly faster access
360  // TODO: MAJOR-MOD-WARNING: if the icon heap is no longer holding ALL of the icon refs for the system and pages and layouts are allowed
361  // to have their own icon copies, then this has to change
362 
363 
365 
369 };
370 
371 QDataStream & operator<< (QDataStream& stream, const IconRowAlpha& s);
372 QDataStream & operator>> (QDataStream& stream, IconRowAlpha& s);
373 QDataStream & operator<< (QDataStream& stream, const AlphabetIconLayout& s);
374 QDataStream & operator>> (QDataStream& stream, AlphabetIconLayout& s);
375 QDebug operator<<(QDebug dbg, const IconRowAlpha &c);
376 QDebug operator<<(QDebug dbg, const AlphabetIconLayout &s);
377 
378 #endif /* ALPHABETICONLAYOUT_H_ */