LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
pixbutton.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2011-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 PIXBUTTON_H_
23 #define PIXBUTTON_H_
24 
25 #include "thingpaintable.h"
26 #include <QRectF>
27 #include <QRect>
28 #include <QString>
29 #include <QColor>
30 #include <QPointF>
31 #include <QFont>
32 #include <QTextLayout>
33 
34 class QGraphicsSceneMouseEvent;
35 class QGesture;
36 class QGestureEvent;
37 class QTouchEvent;
38 class QPanGesture;
39 class QSwipeGesture;
40 class QPinchGesture;
41 class QTapAndHoldGesture;
42 class QTapGesture;
43 class FlickGesture;
44 class PixmapObject;
45 
46 
47 class PixButton;
48 /*
49  *
50  * PixButtonExtraHitArea - enlarges the hit area beyond the bounds of the button
51  *
52  * supports only rectangular areas
53  *
54  */
56 {
57  Q_OBJECT
58 
59 public:
60 
61  friend class PixButton;
62 
63  //area will be min bounded by the button's geom; this class can only be used to enlarge a hit area
64  // it makes no sense in the context of trying to make it smaller, since the button's own hit detection
65  // will trigger up to its own bounds
66  PixButtonExtraHitArea(PixButton& ownerButton,const QSize& area);
67  virtual ~PixButtonExtraHitArea();
68  virtual void commonCtor();
69 
70  virtual QRectF boundingRect() const;
71  virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option=0,QWidget *widget=0);
72 
73 Q_SIGNALS:
74  void signalHit(const QPointF pt = QPointF());
75 
76 public Q_SLOTS:
77 
78  void slotActivate();
79  void slotDeactivate();
80  //use slotResize for when the hit area should be deliberately resized to get a larger hit area; don't use it as a response to an auto-resize
81  // e.g. when the UI resizes and the cascading chain of resize() calls comes all the way down to the PixButtons.
82  // for that, slotOwnerGeometryChanged will take care of the details
83  void slotResize(quint32 w,quint32 h);
84  void slotOwnerGeometryChanged(const QRectF& newGeom);
85  void slotOwnerDestroyed(QObject * p);
86 
87 protected:
88 
90  QRectF m_boundingRect; //doesn't need a geom since it'll never paint...so brect == geom
91  QPointer<PixButton> m_qp_ownerButton;
92 
93 };
94 
95 class PixButton : public ThingPaintable
96 {
97  Q_OBJECT
98  Q_INTERFACES(QGraphicsItem)
99 
100 public:
101  PixButton(const QRectF& buttonGeometry);
102  virtual ~PixButton();
103 
104  virtual bool resize(const QSize& s);
105  virtual bool resize(quint32 newWidth,quint32 newHeight);
106 
107  virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option=0,QWidget *widget=0);
108  virtual void paintOffscreen(QPainter * painter);
109  //TODO: need other paintOffscreen flavors, e.g. PixmapHugeObject
110 
111  virtual bool valid();
112 
113  virtual void setLabel(const QString& v);
114  //override to make label bigger/smaller than the default
115  virtual void setLabel(const QString& v,quint32 fontSizePx);
116  virtual void setLabelVerticalAdjust(qint32 adjustPx);
117 
118 Q_SIGNALS:
119 
120  void signalFirstContact();
121  void signalContact();
122  void signalRelease();
123  void signalLastRelease();
124  void signalCancel();
125  void signalActivated(); //high level signal indicating a full state change; the 'dumbest' and most ambiguous of all the signals
126  //useful for when the signal watcher doesn't care about what kind of button it is, it just wants to know when
127  // it was clicked, tapped, whatever
128 
129 protected:
130 
131  virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
132  virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
133  virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
134 
135  virtual bool touchStartEvent(QTouchEvent *event);
136  virtual bool touchUpdateEvent(QTouchEvent *event);
137  virtual bool touchEndEvent(QTouchEvent *event);
138 
139  virtual bool sceneEvent(QEvent * event);
140  virtual bool tapAndHoldGesture(QTapAndHoldGesture *tapHoldEvent);
141  virtual bool tapGesture(QTapGesture *tapEvent);
142  virtual bool customGesture(QGesture *customGesture);
143 
144  //unused
145  virtual bool panGesture(QPanGesture *panEvent) { return true; }
146  virtual bool swipeGesture(QSwipeGesture *swipeEvent) { return true; }
147  virtual bool flickGesture(FlickGesture *flickEvent) { return true; }
148  virtual bool pinchGesture(QPinchGesture *pinchEvent) { return true; }
149 
150  // PREREQUISITE: m_geom is valid
151  // RESULT: m_labelMaxGeom is set
153 
154  // PREREQUISITE: m_labelMaxGeom must be valid, at least its size()
155  // RESULT: m_textLayoutObject will have the correct layout, m_labelGeom will be set correctly for the current label,
156  virtual void redoLabelTextLayout(); //run this after the geometry changes. This is absolutely necessary
157  // ALSO run this after the label changes. don't ask, just do it. Not necessary now, but it most likely will be later
158 
159  // PREREQUISITE: m_geom , m_labelMaxGeom and m_labelGeom are set
160  // RESULT: m_labelPosICS and m_labelPosPntCS are set
161  virtual void recalculateLabelPosition();
162 
163  static QFont staticLabelFontForButtons();
164 
165 protected:
166 
167  // touch related
170  // --end touch related
171 
172  qint32 m_touchCount;
173  QString m_label;
174  QFont m_textFont;
177  QTextLayout m_textLayoutObject;
178  qint32 m_labelVerticalAdjust; //will be computed into the positions below; this just stores it when setLabelVerticalAdjust() is called
179  QPointF m_labelPosICS; // position in ICS, and corresponds to m_labelGeom
180  QPoint m_labelPosPntCS; // same, but precomputed to offscreen painter coords relative to 0,0 at top left
181  QRect m_labelMaxGeom; // precomputed by recalculateLabelBoundsForCurrentGeom()
182  QRect m_labelGeom; //precomputed by redoLabelTextLayout(); this one is the CURRENT label's box (always <= m_labelMaxGeom)
183 };
184 
185 #endif /* PIXBUTTON_H_ */