LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
OverlayWindowManager_p.h
Go to the documentation of this file.
1 
2 /* @@@LICENSE
3 *
4 * Copyright (c) 2010-2012 Hewlett-Packard Development Company, L.P.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * LICENSE@@@ */
19 
20 
21 
22 
23 #ifndef OVERLAYWINDOWMANAGER_P_H_
24 #define OVERLAYWINDOWMANAGER_P_H_
25 
26 #include "Common.h"
27 
28 #include <QGesture>
29 #include <QGestureEvent>
30 #include <QGraphicsObject>
31 #include <QPainter>
32 #include <QPointer>
33 
34 #include "Settings.h"
35 #include "HostBase.h"
36 #include "Localization.h"
37 #include "SystemUiController.h"
38 
39 #include "pixmapobject.h"
40 #include "dimensionsglobal.h"
41 #include "layoutsettings.h"
42 
44 {
45  Q_OBJECT
46 
47 public:
48  SearchPill(PixmapObject * p_mainPixmap,PixmapObject * p_iconPixmap,const QRectF& geom, QGraphicsItem* parent)
49  : QGraphicsObject(parent)
50  , m_qp_pillPmo(p_mainPixmap)
51  , m_qp_iconPmo(p_iconPixmap)
52  , m_noResize(true)
53  {
54  // Cache the SearchPill in the QPixmapCache
55  // to improve speed during animations and panning
56  setCacheMode(QGraphicsItem::DeviceCoordinateCache);
57  m_geom = geom;
58  m_boundingRect = m_geom.adjusted(-0.5,-0.5,0.5,0.5);
59  if (m_qp_pillPmo)
60  {
61  m_qp_pillPmo->resize(m_geom.size().toSize());
62  }
64 
65  QFont font(qFromUtf8Stl(Settings::LunaSettings()->fontQuicklaunch), 18);
66  font.setPixelSize(18);
67  font.setStyle(QFont::StyleOblique);
68  QString title = qFromUtf8Stl(LOCALIZED("Just type..."));
69 
70  QRect textBounds(0, 0, m_boundingRect.width(), m_boundingRect.height());
71  textBounds.adjust(20,0,-45,0);
72 
73  QFontMetrics metrics(font);
74  title = metrics.elidedText(title, Qt::ElideRight, textBounds.width());
75 
76  int width = metrics.width(title);
77  QPainter painter;
78 
79 
80  m_textPix = QPixmap(width + 20, textBounds.height());
81  m_textPix.fill(Qt::transparent);
82  painter.begin(&m_textPix);
83  painter.setPen(Qt::white);
84  painter.setFont(font);
85  painter.setOpacity(0.8);
86  painter.drawText(textBounds, title, QTextOption(Qt::AlignLeft|Qt::AlignVCenter));
87  painter.end();
88 
89  m_textPos = m_geom.topLeft() + QPointF(LayoutSettings::settings()->searchPillInnerTextAdjust);
90  grabGesture(Qt::TapGesture);
91  }
92 
94  {
95  ungrabGesture(Qt::TapGesture);
96  }
97 
98  virtual bool sceneEvent(QEvent* event)
99  {
100  if (event->type() == QEvent::GestureOverride) {
101 
102  if (canInteract()) {
103  event->accept();
104  }
105  }
106  else if (event->type() == QEvent::Gesture) {
107 
108  QGestureEvent* ge = static_cast<QGestureEvent*>(event);
109  QGesture* g = ge->gesture(Qt::TapGesture);
110  if (g && g->state() == Qt::GestureFinished && canInteract()) {
111  Q_EMIT signalTapped();
112  }
113  }
114  return QGraphicsObject::sceneEvent(event);
115  }
116 
117  virtual QRectF geometry() const
118  {
119  return m_geom;
120  }
121 
122  virtual QRectF boundingRect() const
123  {
124  return m_boundingRect;
125  }
126 
127  void setNoResize(bool v=true)
128  {
129  m_noResize = v;
130  }
131 
133  {
134  if (!m_qp_iconPmo)
135  {
136  return;
137  }
138 
139  QRectF iconGeom = DimensionsGlobal::realRectAroundRealPoint(m_qp_iconPmo->size());
140  qreal rightOffset = (qreal)qMin(LayoutSettings::settings()->searchPillInnerIconRightOffset,(quint32)(m_geom.right()-iconGeom.width()));
141  m_iconPos = QPointF(m_geom.right()-iconGeom.width()-rightOffset,
142  m_geom.top()+(m_geom.height()-iconGeom.height())/2.0);
143  }
144 
145  void resize(int width, int height)
146  {
147  if (m_noResize)
148  {
149  return;
150  }
151  //make the width and height as nearest even int
152  prepareGeometryChange();
153  QSize newSize = QSize((width < 2 ? 2 : width - (width %2)),(height < 2 ? 2 : height - (height %2)));
155  m_boundingRect = m_geom.adjusted(-0.5,-0.5,0.5,0.5);
156  if (m_qp_pillPmo)
157  {
158  m_qp_pillPmo->resize(newSize);
159  }
160  //reposition icon
162  //and text
163  m_textPos = m_geom.topLeft() + QPointF(LayoutSettings::settings()->searchPillInnerTextAdjust);
164  }
165 
166 #define SEARCH_PILL_TIPS_WIDTH 50
167 
168  virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
169  {
170 
171 
172  if (m_qp_pillPmo)
173  {
174  m_qp_pillPmo->paint(painter,m_geom.topLeft());
175  }
176  if (m_qp_iconPmo)
177  {
178  m_qp_iconPmo->paint(painter,m_iconPos);
179  }
180  painter->drawPixmap(m_textPos, m_textPix);
181  }
182 
183 Q_SIGNALS:
184  void signalTapped();
185 
186 private:
187 
188  // TODO: not the best determinate of interaction but will do for now
189  bool canInteract() { return opacity() == 1.0; }
190 
191  bool m_noResize;
192  QPointer<PixmapObject> m_qp_pillPmo;
193  QPointer<PixmapObject> m_qp_iconPmo;
194  QPixmap m_textPix;
195  QRectF m_geom;
196  QPointF m_iconPos; //in painting coords (top left of pixmap target box)
197  QPointF m_textPos; // ditto
198  QRectF m_boundingRect;
199 };
200 
201 #endif