LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
icon.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 ICON_H_
23 #define ICON_H_
24 
25 #include <QUuid>
26 #include <QRectF>
27 #include <QRect>
28 #include <QPointer>
29 #include <QString>
30 #include <QTimer>
31 #include <QList>
32 #include <QFont>
33 #include <QTextLayout>
34 #include <QStateMachine>
35 
36 #include "thingpaintable.h"
37 #include "pixmapobject.h"
38 #include "icondecorator.h"
39 #include "renderopts.h"
40 
41 class DimensionsUI;
42 class LauncherObject;
43 class QGraphicsSceneMouseEvent;
44 class QGesture;
45 class QGestureEvent;
46 class QTouchEvent;
47 class QPanGesture;
48 class QSwipeGesture;
49 class QPinchGesture;
50 class QTapAndHoldGesture;
51 class QTapGesture;
52 class FlickGesture;
53 class QState;
54 class QStateMachine;
55 class Page;
56 class LayoutItem;
57 class IconBase;
58 
59 typedef QList<IconBase *> IconList;
60 typedef QList<IconBase *>::const_iterator IconListConstIter;
61 typedef QList<IconBase *>::iterator IconListIter;
62 
63 namespace DecoratorDesignation
64 {
65  enum Enum
66  {
71  };
72 }
73 
74 namespace RemoveDeleteDecoratorSelector
75 {
76  enum Enum
77  {
82  };
83 }
84 
85 namespace RemoveDeleteDecoratorState
86 {
87  enum Enum
88  {
92  };
93 }
94 
95 namespace IconActionRequest
96 {
97  enum Enum
98  {
99  INVALID = 0,
100  RequestRemoveDelete, //the Remove/Delete area of the icon was triggered; icon won't take action itself on this - it's a signaling to an upper level to take action
101  LAST_INVALID //(used because of blind casts from int...see usages of this Enum in the rest of the code)
102  };
103 }
104 
105 namespace IconInternalHitAreas
106 {
107  enum Enum
108  {
109  INVALID = 0,
111  Other //TODO: extend to describe other parts of the icon
112  };
113 }
114 
115 class IconBase : public ThingPaintable
116 {
117  Q_OBJECT
118  Q_INTERFACES(QGraphicsItem)
119 
120  Q_PROPERTY(QString iconlabel READ iconLabel WRITE setIconLabel RESET resetIconLabel NOTIFY signalIconLabelChanged)
123 
124 public:
125 
126  friend class Page;
127  friend class IconCell;
128 
129  static const char * IconLabelPropertyName; //keep in sync with Q_PROPERTY for the icon label
131  static const char * IconTransferContextPropertyName; //TODO: this should be changed into a better way to communicate context for offer/take/taken sequences involving icons
132  // (but the Demo must go on...so this is what it is for now)
133  static IconBase * iconFromPix(PixmapObject * p_framePix, PixmapObject * p_mainPix, PixmapObject * p_feedbackPix,Page * p_belongsTo);
134 
135  IconBase(const QRectF& iconGeometry,Page * p_belongsTo);
136  IconBase(const QRectF& iconGeometry,PixmapObject * p_framePix, PixmapObject * p_mainPix,PixmapObject * p_feedbackPix,Page * p_belongsTo);
137 
138  virtual IconBase * clone();
139 
140  //returns the master icon of this icon, or 0 if this is the master icon
141  virtual IconBase * cloneOf() const;
142  //convenience of the above; returns the master icon, or this if it is the master (never 0)
143  virtual IconBase * master();
144 
145  virtual ~IconBase();
146 
147  virtual bool stateRemoveDeleteActive() const;
148  virtual void setStateRemoveDeleteActive(bool v);
149 
150  virtual Page * owningPage() const;
151  virtual LauncherObject * owningLauncher() const; //this is only valid if the icon is in the launcher's Limbo
152 
153  QString iconLabel() const;
154  void setIconLabel(const QString& v);
155  void resetIconLabel();
156  void setIconLabelVisibility(bool visible);
157  void setUsePrerenderedLabel(bool usePreRendered);
158  void setLaunchFeedbackVisibility(bool visible);
159 
160 
161  //if null (0) passed in, then the primary launcher instance will be used
162  // returns true if complely successful, false if any failures occur in connecting
163  // (in case multiple connections; any fail will result in a false here)
164  virtual bool connectRequestsToLauncher(LauncherObject * launcherObj = 0);
165 
166  virtual LayoutItem * layoutItemAssociation() const;
167  virtual void setLayoutItemAssociation(LayoutItem * v);
168 
169  // override the geometry logic from the base class...it must be done in order to make use of "alignment" geometry, if it is used
170  // see the icongeometrysettings.h/cpp files
171  // essentially, it's a way to trick the users of this class into thinking the geometry is actually different (intended only to be < than the actual geom)
172  virtual QRectF geometry() const;
173  virtual QRectF positionRelativeGeometry() const;
174  virtual QPointF activePos() const; //see m_activePositionOffsetFromPos
175 
176  virtual bool resize(quint32 newWidth,quint32 newHeight);
177  virtual bool resize(const QSize& s);
178  virtual bool expand(const quint32 widthEx,const quint32 heightEx);
179 
180  virtual bool offer(Thing * p_offer,Thing * p_offeringThing);
181  virtual bool take(Thing * p_takerThing);
182  virtual bool taking(Thing * p_victimThing, Thing * p_takerThing);
183  virtual void taken(Thing * p_takenThing,Thing * p_takerThing);
184 
185  static QSize maxIconSize(const IconList& icons);
186  static QSize minIconSize(const IconList& icons);
187  static void iconSizeBounds(const IconList& icons,QSize& r_minSize,QSize& r_maxSize);
188 
189  static bool canUseFramePixOnIcon(const IconBase& icon,PixmapObject * p_pix);
190  static bool canUseMainPixOnIcon(const IconBase& icon,PixmapObject * p_pix);
191  static bool canUseDecorPixOnIcon(const IconBase& icon,PixmapObject * p_pix);
192 
193  virtual bool usingInstallDecorator() const;
194 
195  //TODO: temporary; remove when a better system (like styles) goes in place
196  static QFont staticLabelFontForIcons();
197 
198  static bool iconLessThanByLabel(const IconBase * p_a,const IconBase * p_b);
199 
202  // ignore it. This is currently used in the case where a tap-n-hold took the icons into reorder mode
203  // and a tap or tap-n-hold on a remove/delete decorator happens. in this case, the layout shouldn't proceed
204  // to move the icon as it would ordinarily, so this function would return true, and instead
205  // signal the layout/page a different way that a remove/delete was triggered
206  // (see signalIconActionRequest)
207  // note that unlike the touch input functions, this is like a "tap", in that it signifies
208  // a complete interaction, not a start/end (for that usage, see touchStartIntoIcon and touchEndIntoIcon)
209  // a false return means that the caller can potentially take further action; the area that was hit will be in r_hitArea
210  virtual bool tapIntoIcon(const QPointF& touchCoordinateICS,IconInternalHitAreas::Enum& r_hitArea);
211  virtual bool touchStartIntoIcon(const QPointF& touchCoordinateICS);
212  virtual bool touchEndIntoIcon(const QPointF& touchCoordinateICS);
213 
214  //TODO: UPDATE-PAINT-WORKAROUND:
215  virtual void update();
216 Q_SIGNALS:
217 
218  // params:
219  //[0] old state
220  //[1] new state
221  void signalIconLabelChanged(const QString&,const QString&);
222  void signalIconLabelChanged();
223 
224  void signalMasterIconMainImageChanged(PixmapObject * p_newMainIconPixmap);
226  void signalMasterIconInstallStatusDecoratorParamsChanged(int progressVal,int minProgressVal,int maxProgressVal);
227 
229  void signalFSMActivate();
230  void signalFSMDeactivate();
231 
232 
234 Q_SIGNALS:
235 
236  // 'cmdRequest' is actually a IconActionRequest::Enum but enums are slightly annoying and awkward w.r.t. Qt's metatype system
237  // and a queued connect requires the params to be reg'd metatypes
238  void signalIconActionRequest(int cmdRequest);
239 
240 public Q_SLOTS:
241 
242  //WARNING: this way of passing ptr ref for a param.-based-"return" could cause problems if the signal came
243  // across a thread boundary (see Qt doc)
244  virtual void slotUpdateIconPic(PixmapObject * p_newPixmap,bool propagate,PixmapObject*& r_p_oldPixmap);
245  virtual void slotChangeIconVisibility(bool show);
246 
247  virtual void slotUpdateIconFramePic(PixmapObject * p_newPixmap,PixmapObject*& r_p_oldPixmap);
248  virtual void slotChangeIconFrameVisibility(bool show);
249 
250  virtual void slotUpdateRemoveDecoratorPic(RemoveDeleteDecoratorState::Enum whichStatePic,PixmapObject * p_newPixmap,PixmapObject*& r_p_oldPixmap);
251  virtual void slotUpdateDeleteDecoratorPic(RemoveDeleteDecoratorState::Enum whichStatePic,PixmapObject * p_newPixmap,PixmapObject*& r_p_oldPixmap);
252 
253  // also resets the state to Normal
254  virtual void slotChangeRemoveDeleteDecoratorVisibility(RemoveDeleteDecoratorSelector::Enum showWhich);
255 
256  virtual void slotUpdateInstallStatusDecoratorPic(PixmapObject * p_newPixmap,PixmapObject*& r_p_oldPixmap);
257  virtual void slotUpdateInstallStatusDecoratorPicNewProgress(int progressVal,bool updateMinMax=false,int minProgressVal = 0,int maxProgressVal = 0);
258  virtual void slotUpdateInstallStatusDecoratorResetProgress(int minProgressVal = 0,int maxProgressVal = 100);
259 
260  // These two toggle this item's painting and repainting of itself as a part of the QGraphics View system
261  // when AutoRepaint is disabled, the item will only be painted when its paint function is called explicitly
262  virtual void slotEnableIconAutoRepaint();
263  virtual void slotDisableIconAutoRepaint(bool clearAutoclip=true);
264 
265  // this is a type of clip-rect similar to what QPainter achieves with setClipRect()
266  // however, it is computed manually when the icon is auto repainting; that is, when it isn't painted as a part
267  // of the layout. It will basically just redirect a paint() call as done by the QGView/Scene into
268  // a paint(QPainter *painter, const QRectF& sourceItemRect) as done by the manual layout paint,
269  // using the clipRectPCS with the current pos() of the icon to create the sourceItemRect
270  virtual void setAutopaintClipRect(const QRect& clipRectPCS);
271  virtual void clearAutopaintClipRect();
272 
273  virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option=0,QWidget *widget=0);
274  virtual void paint(QPainter *painter, const QRectF& sourceItemRect);
275  //this one is for "staged rendering"
276  virtual void paint(QPainter *painter, const QRectF& sourceRect,qint32 renderOpt);
277 
278  virtual void paintOffscreen(QPainter *painter);
279  virtual void paintOffscreen(QPainter *painter,const QRect& sourceRect,const QPoint& targetOrigin);
280  virtual void paintOffscreen(QPainter *painter,const QRect& sourceRect,const QRect& targetRect);
281 
282 protected Q_SLOTS:
283 
284  // a special case of slotUpdateIconPic, intended to only be signaled by the master icon to its copies
285  virtual void slotMasterIconMainImageChanged(PixmapObject * p_newMainIconPixmap);
286  virtual void slotMasterIconInstallStatusDecoratorChanged(PixmapObject * p_newPixmap);
287  virtual void slotMasterIconInstallStatusDecoratorParamsChanged(int progressVal,int minProgressVal,int maxProgressVal);
288 
289  virtual void slotAnimationFinished();
290 
291  //used as the completion step of the FSM
292  virtual void slotRemoveDeleteActivated();
293 
294 protected:
295 
296  IconBase(const QRectF& iconGeometry); //only used for cloning!
297  //TODO: make it a proper virt.ctor idiom by completing clone() in ThingPaintable and Thing
298 
299  virtual bool touchEvent(QTouchEvent * event) { return true; }
300  virtual bool touchStartEvent(QTouchEvent *event) { return true; }
301  virtual bool touchUpdateEvent(QTouchEvent *event) { return true; }
302  virtual bool touchEndEvent(QTouchEvent *event) { return true; }
303 
304  virtual bool sceneEvent(QEvent * event);
305  virtual bool gestureEvent(QGestureEvent *gestureEvent) { return true; }
306  virtual bool panGesture(QPanGesture *panEvent) { return true; }
307  virtual bool swipeGesture(QSwipeGesture *swipeEvent);
308  virtual bool flickGesture(FlickGesture *flickEvent);
309  virtual bool pinchGesture(QPinchGesture *pinchEvent) { return true; }
310  virtual bool tapAndHoldGesture(QTapAndHoldGesture *tapHoldEvent) { return true; }
311  virtual bool tapGesture(QTapGesture *tapEvent) { return true; }
312  virtual bool customGesture(QGesture *customGesture) { return true; }
313 
314  virtual void setupFSM();
315 
316  static QRectF GEOM(const QRectF& geom);
317  static QRectF AGEOM(const QRectF& geom); //alignment geom, if used
318  static QRectF LabelBoxGeomFromGeom(const QRectF& geom);
319  static QRectF FrameGeomFromGeom(const QRectF& geom);
320  static QRectF IconGeomFromGeom(const QRectF& geom);
321  static QRectF RemoveDeleteDecorGeomFromGeom(const QRectF& geom);
322  static QRectF InstallStatusGeomFromGeom(const QRectF& geom);
323 
324 // virtual void updateGeometry(const QRectF& mainIconGeom,const QRectF& frameIconGeom);
325 // virtual void updateGeometry(const QRectF& newGeom,bool doesGeomIncludeLabel=true);
326 
327  virtual void geometryChanged();
328  virtual void recomputePainterHelpers();
330 
331 // // recalculateLabelBoundsForCurrentGeom() works in conjunction with updateGeometry...
332 // // two possibilities when a new geometry is specified (corresponds to the two variants of updateGeometry()):
333 // // 1. updateGeometry(icon geom,frame icon geom) -- only the icon picture geoms were specified, and the label
334 // // box needs to be calculated from this and added to the given geoms to make m_geom (the final, total icon geom)
335 // // 2. updateGeometry(new geom for whole icon) -- t//he geom specified is the total geom which is frame icon pic and/or main
336 // // icon pic AND the label box, inclusive. The label size must be deduced from this, and nothing is added to the geom
337 // // specified (it's already the total geom)
338 // // (can be overriden with the bool parameter, passed through to this variant of updateGeometry)
339 // // PREREQUISITE: m_geom must be valid, at least its size()
340 // // RESULT: m_labelMaxGeom will be set correctly
341 // virtual void recalculateLabelBoundsForCurrentGeom(bool doesCurrentGeomIncludeLabel);
342 
343  static bool canUsePixOnIcon(const IconBase& icon,PixmapObject * p_pix);
344 
345  // PREREQUISITE: m_labelMaxGeom must be valid, at least its size()
346  // RESULT: m_textLayoutObject will have the correct layout, m_labelGeom will be set correctly for the current label,
347  virtual void redoLabelTextLayout(bool renderLabelPixmap = false); //run this after the geometry changes. This is absolutely necessary
348  // ALSO run this after the label changes. don't ask, just do it. Not necessary now, but it most likely will be later
349 
350  // PREREQUISITE: m_geom , m_labelMaxGeom and m_labelGeom are set
351  // RESULT: m_labelPosICS and m_labelPosPntCS are set
352  virtual void recalculateLabelPosition();
353 
354 protected:
355 
357  QString m_iconLabel;
358 // QPointer<Page> m_qp_currentOwnerPage;
359  QPointer<IconBase> m_qp_masterIcon; //ptr to the master icon if this one is a clone, or 0 if this is a master
361  QRect m_ownerSetAutopaintClip; //something like a clip-rect for QPainter, but manually done. see setAutopaintClipRect()
362  QPointer<LayoutItem> m_qp_layoutItemAssociation;
363 
365  // actually be a position above or below e.g. a finger touch hotspot, so that the icon isn't occluded by the finger.
366  // In some cases, I may want the actual position of the item, as far as the rest of the system is concerned, to be different than this visible location
367  // Therefore, this variable is used to keep track of that. It is initialized from settings, but may be overriden at runtime, even animated (bouncy icon effect)
369 
370  QRectF m_alignmentGeomPrecomputed; //populated only if IconGeometrySettings::useAlignmentGeom is set true
371 
373  QPointer<PixmapObject> m_qp_iconPixmap;
374  QRect m_iconGeom; //this is the actual geom for the layout calculation
375  QRect m_iconPixmapGeom; //this one is just for the painter, to deal with overpainting. it will be == or > m_iconGeom
376  QPoint m_iconPosICS; //this relates to m_iconGeom and m_iconPixmapGeom; both have the origin in the same place (at this point)
377  QPoint m_iconSrcPrecomputed; //a precomputed translator to speed up paint (see paint(QPainter *painter, const QRectF& sourceItemRect))
378 
380  QPointer<PixmapObject> m_qp_iconFramePixmap; //the "background"
381  QRect m_iconFrameGeom; // this is the actual geom for the layout calculation
382  QRect m_iconFramePixmapGeom; //this one is just for the painter, to deal with overpainting. it will be == or > m_iconFrameGeom
383  QPoint m_iconFramePosICS; //this relates to m_iconFrameGeom and m_iconFramePixmapGeom; both have the origin in the same place (at this point)
384  QPoint m_iconFrameSrcPrecomputed; //a precomputed translator to speed up paint (see paint(QPainter *painter, const QRectF& sourceItemRect))
385 
387  QPointer<PixmapObject> m_qp_iconFeedbackPixmap; //the launch feedback image
388  QRect m_iconFeedbackPixmapGeom; //this one is just for the painter, to deal with overpainting. it will be == or > m_iconFrameGeom
389  QPoint m_iconFeedbackPosICS; //this relates to m_iconFeedbackGeom and m_iconFeedbackPixmapGeom; both have the origin in the same place (at this point)
390  QPoint m_iconFeedbackSrcPrecomputed; //a precomputed translator to speed up paint (see paint(QPainter *painter, const QRectF& sourceItemRect))
391 
393  QPointer<PixmapObject> m_qp_removeDecoratorPixmap; //these pairs better have the same geoms
394  QPointer<PixmapObject> m_qp_removePressedDecoratorPixmap;
396  QPointer<PixmapObject> m_qp_deleteDecoratorPixmap; // ""
397  QPointer<PixmapObject> m_qp_deletePressedDecoratorPixmap;
399 
400  QPointer<PixmapObject> m_qp_drDecoratorCurrentlyRenderingPixmap; //set outside paint() so that paint can avoid checking "which"
404 
405  QRect m_removeDecoratorGeom; // these are analogues to how icon and iconFrame do things, above
409  QRect m_deleteDecoratorGeom; // these are analogues to how icon and iconFrame do things, above
413 
414  QPointer<PixmapObject> m_qp_installStatusDecoratorPixmap;
422 
426 
428 
429  //text layout for the label
430  QColor m_labelColor;
431  QPoint m_labelPosICS; // position in ICS, and corresponds to m_labelGeom
432  QRect m_labelMaxGeom; // precomputed by recalculateLabelBoundsForCurrentGeom() , based on current icon size and settings
433  // (proportions of icon pic to label box)
434  QPoint m_labelSrcPrecomputed; //a precomputed translator to speed up paint (see paint(QPainter *painter, const QRectF& sourceItemRect))
435  QRect m_labelGeom; //precomputed by redoLabelTextLayout(); this one is the CURRENT label's box (always <= m_labelMaxGeom)
436  QTextLayout m_textLayoutObject;
437  QPointer<PixmapObject> m_qp_prerenderedLabelPixmap;
439 
440 };
441 
442 #endif /* ICON_H_ */