|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (developer.feedback@nokia.com) |
|
6 ** |
|
7 ** This file is part of the HbCore module of the UI Extensions for Mobile. |
|
8 ** |
|
9 ** GNU Lesser General Public License Usage |
|
10 ** This file may be used under the terms of the GNU Lesser General Public |
|
11 ** License version 2.1 as published by the Free Software Foundation and |
|
12 ** appearing in the file LICENSE.LGPL included in the packaging of this file. |
|
13 ** Please review the following information to ensure the GNU Lesser General |
|
14 ** Public License version 2.1 requirements will be met: |
|
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
16 ** |
|
17 ** In addition, as a special exception, Nokia gives you certain additional |
|
18 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
20 ** |
|
21 ** If you have questions regarding the use of this file, please contact |
|
22 ** Nokia at developer.feedback@nokia.com. |
|
23 ** |
|
24 ****************************************************************************/ |
|
25 |
|
26 #include "hbthetestwidget_p.h" |
|
27 #include "hbinstance.h" |
|
28 #include "hbnamespace_p.h" |
|
29 #include <hbapplication.h> |
|
30 #include "hbtoolbutton_p.h" |
|
31 #include "hbstyleoptiontoolbutton.h" |
|
32 #include "hbcolorscheme.h" |
|
33 #include "hbtextitem.h" |
|
34 |
|
35 #ifdef Q_OS_SYMBIAN |
|
36 #include <eikenv.h> |
|
37 #include <apgtask.h> // TApaTaskList, TApaTask |
|
38 #endif |
|
39 |
|
40 #include <QGraphicsSceneMouseEvent> |
|
41 #include <QGraphicsGridLayout> |
|
42 #include <QGraphicsWidget> |
|
43 #include <QTextStream> |
|
44 #include <QFile> |
|
45 #include <QDir> |
|
46 #include <QTimer> |
|
47 #include <hbaction.h> |
|
48 |
|
49 #include <QDebug> // for qWarning |
|
50 |
|
51 #ifdef HB_TEXT_MEASUREMENT_UTILITY |
|
52 #include "hbtextmeasurementutility_p.h" |
|
53 #endif //HB_TEXT_MEASUREMENT_UTILITY |
|
54 |
|
55 //#ifdef Q_OS_SYMBIAN |
|
56 //#include <coemain.h> |
|
57 //#include <fbs.h> |
|
58 //#endif |
|
59 |
|
60 const int KWidth = 140; // container size, button width is KWidth/2 |
|
61 const int KHeight = 140; // container size, button height is KHeight/2 |
|
62 // how much must button be dragged before it is actually moved |
|
63 const int KThreshold = 16; |
|
64 |
|
65 HbTheTestButton::HbTheTestButton(QGraphicsItem *parent) |
|
66 : HbToolButton(parent), |
|
67 mPressedDown(false) |
|
68 { |
|
69 setAttribute(Hb::InteractionDisabled); // no feedback from test button |
|
70 mPosition = scenePos(); |
|
71 mScreenSize = HbDeviceProfile::profile(this).logicalSize(); |
|
72 } |
|
73 |
|
74 HbTheTestButton::~HbTheTestButton() |
|
75 { |
|
76 } |
|
77 |
|
78 QSizeF HbTheTestButton::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
79 { |
|
80 switch(which){ |
|
81 case Qt::MinimumSize: //fallthrough |
|
82 case Qt::MaximumSize: //fallthrough |
|
83 return QSizeF(KWidth/2, KHeight/2); |
|
84 default: |
|
85 return HbAbstractButton::sizeHint( which, constraint ); |
|
86 } |
|
87 } |
|
88 |
|
89 /*! |
|
90 \reimp |
|
91 Removed tooltip support. |
|
92 */ |
|
93 void HbTheTestButton::updatePrimitives() |
|
94 { |
|
95 HbToolButtonPrivate *d = static_cast<HbToolButtonPrivate *>(HbToolButton::d_ptr); |
|
96 |
|
97 HbStyleOptionToolButton option; |
|
98 if (d->action) { |
|
99 setCheckable(d->action->isCheckable()); |
|
100 setChecked(d->action->isChecked()); |
|
101 setEnabled(d->action->isEnabled()); |
|
102 } |
|
103 |
|
104 initStyleOption(&option); |
|
105 if (d->frameItem) { |
|
106 style()->updatePrimitive(d->frameItem, HbStyle::P_ToolButton_frame, &option); |
|
107 } |
|
108 if (d->textItem) { |
|
109 style()->updatePrimitive(d->textItem, HbStyle::P_ToolButton_text, &option); |
|
110 } |
|
111 if (d->iconItem) { |
|
112 style()->updatePrimitive(d->iconItem, HbStyle::P_ToolButton_icon, &option); |
|
113 } |
|
114 } |
|
115 |
|
116 /*! |
|
117 reimp |
|
118 */ |
|
119 void HbTheTestButton::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
120 { |
|
121 HbToolButton::mousePressEvent(event); |
|
122 mPressedDown = true; |
|
123 mMoved = false; |
|
124 mPressedPos = event->scenePos(); |
|
125 mPosition = scenePos() - pos(); |
|
126 event->accept(); |
|
127 } |
|
128 |
|
129 /*! |
|
130 reimp |
|
131 */ |
|
132 void HbTheTestButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
|
133 { |
|
134 if (!mMoved) { |
|
135 HbToolButton::mouseReleaseEvent(event); |
|
136 } |
|
137 mMoved = false; |
|
138 mPressedDown = false; |
|
139 event->accept(); |
|
140 } |
|
141 |
|
142 /*! |
|
143 reimp |
|
144 |
|
145 */ |
|
146 void HbTheTestButton::mouseMoveEvent(QGraphicsSceneMouseEvent *event) |
|
147 { |
|
148 HbToolButton::mouseMoveEvent(event); |
|
149 QPointF moved = mPressedPos - event->scenePos(); |
|
150 if (mMoved || (mPressedDown && ((qAbs(moved.x()) > KThreshold) || |
|
151 (qAbs(moved.y()) > KThreshold)))) { |
|
152 QPointF newPosition = mPosition - moved; |
|
153 if ((newPosition.x() + pos().x()) < 0) { |
|
154 newPosition.setX(0 - pos().x()); |
|
155 } |
|
156 if ((newPosition.y() + pos().y()) < 0) { |
|
157 newPosition.setY(0 - pos().y()); |
|
158 } |
|
159 |
|
160 if (newPosition.x() > (mScreenSize.width() - KWidth/2 - pos().x())) { |
|
161 newPosition.setX(mScreenSize.width() - KWidth/2 - pos().x()); |
|
162 } |
|
163 if (newPosition.y() > (mScreenSize.height() - KHeight/2 - pos().y())) { |
|
164 newPosition.setY(mScreenSize.height() - KHeight/2 - pos().y()); |
|
165 } |
|
166 HbTheTestWidget *parentWidget = qgraphicsitem_cast<HbTheTestWidget*>(parentItem()); |
|
167 if (parentWidget) { |
|
168 mMoved = true; |
|
169 parentWidget->setPos(newPosition); |
|
170 } |
|
171 setDown(false); |
|
172 } |
|
173 } |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 class HbTheTestWidgetPrivate |
|
179 { |
|
180 public: |
|
181 HbMainWindow *mMainWindow; |
|
182 HbTheTestButton *mButton1; |
|
183 HbTheTestButton *mButton2; |
|
184 HbTheTestButton *mButton3; |
|
185 HbTheTestButton *mButton4; |
|
186 QGraphicsGridLayout *mLayout; |
|
187 }; |
|
188 |
|
189 /*! |
|
190 \class HbTheTestWidget |
|
191 \internal |
|
192 \proto |
|
193 */ |
|
194 HbTheTestWidget::HbTheTestWidget(HbMainWindow *mainWindow, QGraphicsItem *parent) |
|
195 : HbWidget(parent) |
|
196 { |
|
197 d = new HbTheTestWidgetPrivate; |
|
198 d->mMainWindow = mainWindow; |
|
199 |
|
200 d->mLayout = new QGraphicsGridLayout(); |
|
201 d->mLayout->setContentsMargins( qreal(0.0), qreal(0.0), qreal(0.0), qreal(0.0) ); |
|
202 d->mLayout->setSpacing(0.0); |
|
203 setLayout(d->mLayout); |
|
204 |
|
205 HbAction *action1 = new HbAction(QString("1"),this); |
|
206 d->mButton1 = new HbTheTestButton(this); |
|
207 d->mButton1->setAction(action1); |
|
208 d->mButton1->setToolButtonStyle(HbToolButton::ToolButtonText); |
|
209 |
|
210 HbAction *action2 = new HbAction(QString("2"),this); |
|
211 d->mButton2 = new HbTheTestButton(this); |
|
212 d->mButton2->setAction(action2); |
|
213 d->mButton2->setToolButtonStyle(HbToolButton::ToolButtonText); |
|
214 |
|
215 HbAction *action3 = new HbAction(QString("3"),this); |
|
216 d->mButton3 = new HbTheTestButton(this); |
|
217 d->mButton3->setAction(action3); |
|
218 d->mButton3->setToolButtonStyle(HbToolButton::ToolButtonText); |
|
219 |
|
220 HbAction *action4 = new HbAction(QString("4"),this); |
|
221 d->mButton4 = new HbTheTestButton(this); |
|
222 d->mButton4->setAction(action4); |
|
223 d->mButton4->setToolButtonStyle(HbToolButton::ToolButtonText); |
|
224 |
|
225 d->mLayout->addItem(d->mButton1, 0, 0); |
|
226 d->mLayout->addItem(d->mButton2, 0, 1); |
|
227 d->mLayout->addItem(d->mButton3, 1, 0); |
|
228 d->mLayout->addItem(d->mButton4, 1, 1); |
|
229 |
|
230 QSize screenSize = HbDeviceProfile::profile(this).logicalSize(); |
|
231 setPos((screenSize.width()-KWidth)/2,(screenSize.height()-KHeight)/2); |
|
232 |
|
233 QObject::connect(mainWindow, SIGNAL(orientationChanged(Qt::Orientation)), |
|
234 this, SLOT(orientationChanged(Qt::Orientation))); |
|
235 } |
|
236 |
|
237 |
|
238 HbTheTestWidget::~HbTheTestWidget() |
|
239 { |
|
240 delete d; |
|
241 } |
|
242 |
|
243 void HbTheTestWidget::orientationChanged(Qt::Orientation newOrientation) |
|
244 { |
|
245 Q_UNUSED(newOrientation) |
|
246 QSize screenSize = HbDeviceProfile::profile(this).logicalSize(); |
|
247 setPos((screenSize.width()-KWidth)/2,(screenSize.height()-KHeight)/2); |
|
248 d->mButton1->mScreenSize = screenSize; |
|
249 d->mButton2->mScreenSize = screenSize; |
|
250 d->mButton3->mScreenSize = screenSize; |
|
251 d->mButton4->mScreenSize = screenSize; |
|
252 } |
|
253 |
|
254 QSizeF HbTheTestWidget::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
255 { |
|
256 switch(which){ |
|
257 case Qt::MinimumSize: //fallthrough |
|
258 case Qt::MaximumSize: //fallthrough |
|
259 return QSizeF(KWidth, KHeight); |
|
260 default: |
|
261 return HbWidget::sizeHint( which, constraint ); |
|
262 } |
|
263 } |
|
264 |
|
265 HbTheTestButton *HbTheTestWidget::button1() |
|
266 { |
|
267 if (d) { |
|
268 return d->mButton1; |
|
269 } |
|
270 return 0; |
|
271 } |
|
272 |
|
273 HbTheTestButton *HbTheTestWidget::button2() |
|
274 { |
|
275 if (d) { |
|
276 return d->mButton2; |
|
277 } |
|
278 return 0; |
|
279 } |
|
280 |
|
281 HbTheTestButton *HbTheTestWidget::button3() |
|
282 { |
|
283 if (d) { |
|
284 return d->mButton3; |
|
285 } |
|
286 return 0; |
|
287 } |
|
288 |
|
289 HbTheTestButton *HbTheTestWidget::button4() |
|
290 { |
|
291 if (d) { |
|
292 return d->mButton4; |
|
293 } |
|
294 return 0; |
|
295 } |
|
296 |
|
297 // slots that can be used in test buttons start here |
|
298 |
|
299 void HbTheTestWidget::textLayoutMeasure() |
|
300 { |
|
301 #ifdef HB_TEXT_MEASUREMENT_UTILITY |
|
302 HbTextMeasurementUtility *measureUtility = HbTextMeasurementUtility::instance(); |
|
303 measureUtility->measureItems(); |
|
304 #endif //HB_TEXT_MEASUREMENT_UTILITY |
|
305 } |
|
306 |
|
307 void HbTheTestWidget::textLayoutWriteReport() |
|
308 { |
|
309 #ifdef HB_TEXT_MEASUREMENT_UTILITY |
|
310 HbTextMeasurementUtility *measureUtility = HbTextMeasurementUtility::instance(); |
|
311 HbDeviceProfile profile = HbDeviceProfile::profile(d->mMainWindow); |
|
312 if (!HbApplication::applicationName().isEmpty()) { |
|
313 measureUtility->writeReport(profile, HbApplication::applicationName()); |
|
314 } else { |
|
315 measureUtility->writeReport(profile, "unknown_application"); |
|
316 } |
|
317 measureUtility->reset(); |
|
318 #endif //HB_TEXT_MEASUREMENT_UTILITY |
|
319 } |
|
320 |
|
321 void HbTheTestWidget::toggleOrientation() |
|
322 { |
|
323 d->mMainWindow->toggleOrientation(); |
|
324 } |
|
325 |
|
326 void HbTheTestWidget::toggleMirroring() |
|
327 { |
|
328 Qt::LayoutDirection dir = d->mMainWindow->layoutDirection(); |
|
329 d->mMainWindow->setLayoutDirection(dir == Qt::LeftToRight ? Qt::RightToLeft : Qt::LeftToRight); |
|
330 } |
|
331 |
|
332 void HbTheTestWidget::setApplicationBackground() |
|
333 { |
|
334 #ifdef Q_OS_SYMBIAN |
|
335 TApaTask task(CEikonEnv::Static()->WsSession()); |
|
336 task.SetWgId(CEikonEnv::Static()->RootWin().Identifier()); |
|
337 task.SendToBackground(); |
|
338 #endif |
|
339 } |
|
340 |
|
341 void HbTheTestWidget::screenCapture() |
|
342 { |
|
343 setVisible(false); |
|
344 QTimer::singleShot(1000, this, SLOT(doScreenCapture())); |
|
345 } |
|
346 |
|
347 void HbTheTestWidget::doScreenCapture() |
|
348 { |
|
349 #if defined (Q_OS_WIN32) |
|
350 QString filePath("c:\\"); |
|
351 filePath = QDir::toNativeSeparators(filePath); |
|
352 #elif defined (Q_OS_SYMBIAN) |
|
353 QString filePath("f:\\"); |
|
354 filePath = QDir::toNativeSeparators(filePath); |
|
355 if (!QDir(filePath).exists()) { |
|
356 filePath = "e:\\"; |
|
357 filePath = QDir::toNativeSeparators(filePath); |
|
358 if (!QDir(filePath).exists()) { |
|
359 filePath = "c:\\"; // this should always exist |
|
360 filePath = QDir::toNativeSeparators(filePath); |
|
361 } |
|
362 } |
|
363 #elif defined (Q_OS_UNIX) |
|
364 QString filePath(QDir::tempPath()); |
|
365 filePath.append(QDir::separator()); |
|
366 #endif |
|
367 |
|
368 filePath.append("data"); |
|
369 filePath.append(QDir::separator()); |
|
370 filePath.append("hb_screenshots"); |
|
371 |
|
372 QDir dir(filePath); |
|
373 if (!dir.exists()) { |
|
374 dir.mkpath(filePath); |
|
375 } |
|
376 |
|
377 if (!HbApplication::applicationName().isEmpty()) { |
|
378 filePath.append(HbApplication::applicationName()); |
|
379 } else { |
|
380 filePath.append("unknown_application"); |
|
381 } |
|
382 filePath.append(".png"); |
|
383 |
|
384 //#ifdef Q_OS_SYMBIAN |
|
385 // todo: not fail-safe code |
|
386 // TSize screenSize = CCoeEnv::Static()->ScreenDevice()->SizeInPixels(); |
|
387 // TDisplayMode displayMode = CCoeEnv::Static()->ScreenDevice()->DisplayMode(); |
|
388 |
|
389 // CFbsBitmap *bitmap = new (ELeave) CFbsBitmap(); |
|
390 // User::LeaveIfError(bitmap->Create(screenSize, displayMode)); |
|
391 |
|
392 // CCoeEnv::Static()->ScreenDevice()->CopyScreenToBitmap(bitmap); |
|
393 // QPixmap screenPixmap = QPixmap::fromSymbianCFbsBitmap(bitmap); |
|
394 //#else |
|
395 QPixmap screenPixmap = QPixmap::grabWindow( |
|
396 QApplication::activeWindow()->winId()); |
|
397 //#endif |
|
398 QString format = "png"; |
|
399 screenPixmap.save(filePath.toLatin1(), format.toLatin1()); |
|
400 setVisible(true); |
|
401 } |
|
402 |