|
a/WebKit/qt/QGVLauncher/custompopup.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Library General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Library General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Library General Public License |
| 15 |
* along with this library; see the file COPYING.LIB. If not, write to |
| 16 |
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 |
* Boston, MA 02110-1301, USA. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "custompopup.h" |
| 22 |
|
| 23 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0) |
| 24 |
#include <QPropertyAnimation> |
| 25 |
#endif |
| 26 |
#include <QStandardItemModel> |
| 27 |
|
| 28 |
CustomListWidget::CustomListWidget() |
| 29 |
{ |
| 30 |
} |
| 31 |
|
| 32 |
void CustomListWidget::focusOutEvent(QFocusEvent* event) |
| 33 |
{ |
| 34 |
emit focusOut(); |
| 35 |
QListWidget::focusOutEvent(event); |
| 36 |
} |
| 37 |
|
| 38 |
CustomPopup::CustomPopup() |
| 39 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0) |
| 40 |
: m_animation(0) |
| 41 |
#endif |
| 42 |
{ |
| 43 |
connect(&m_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(itemChanged())); |
| 44 |
connect(&m_list, SIGNAL(focusOut()), this, SLOT(focusOut())); |
| 45 |
} |
| 46 |
|
| 47 |
CustomPopup::~CustomPopup() |
| 48 |
{ |
| 49 |
} |
| 50 |
|
| 51 |
void CustomPopup::setParent(QWidget* parent) |
| 52 |
{ |
| 53 |
m_list.setParent(parent); |
| 54 |
} |
| 55 |
|
| 56 |
void CustomPopup::show(const QRect& geometry, int selectedIndex) |
| 57 |
{ |
| 58 |
m_list.setGeometry(QRect(0, 0, geometry.width(), 250)); |
| 59 |
m_list.setCurrentRow(selectedIndex); |
| 60 |
QPoint end = geometry.topLeft(); |
| 61 |
QWidget* parent = m_list.parentWidget(); |
| 62 |
if (parent) { |
| 63 |
end.setX(qMin(end.x(), parent->width() - m_list.width())); |
| 64 |
end.setY(qMin(end.y(), parent->height() - m_list.height())); |
| 65 |
} |
| 66 |
m_list.setGeometry(QRect(end.x(), end.y(), geometry.width(), 250)); |
| 67 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0) |
| 68 |
if (m_animation) |
| 69 |
m_animation->stop(); |
| 70 |
|
| 71 |
QPoint start = QPoint(-m_list.width(), -m_list.height()); |
| 72 |
|
| 73 |
if (parent) { |
| 74 |
start.setX(2 * end.x() < parent->width() ? -m_list.width() : parent->width()); |
| 75 |
start.setY(2 * end.y() < parent->height() ? -m_list.height() : parent->height()); |
| 76 |
} |
| 77 |
|
| 78 |
QPropertyAnimation* animation = new QPropertyAnimation(); |
| 79 |
animation->setTargetObject(&m_list); |
| 80 |
animation->setPropertyName("pos"); |
| 81 |
animation->setEasingCurve(QEasingCurve::OutBounce); |
| 82 |
animation->setDuration(1000); |
| 83 |
animation->setStartValue(start); |
| 84 |
animation->setEndValue(end); |
| 85 |
|
| 86 |
m_animation = animation; |
| 87 |
connect(animation, SIGNAL(finished()), this, SLOT(inAnimationEnd())); |
| 88 |
m_animation->start(QAbstractAnimation::DeleteWhenStopped); |
| 89 |
#else |
| 90 |
inAnimationEnd(); |
| 91 |
#endif |
| 92 |
m_list.setVisible(true); |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
void CustomPopup::hide() |
| 97 |
{ |
| 98 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0) |
| 99 |
if (m_animation) |
| 100 |
m_animation->stop(); |
| 101 |
|
| 102 |
QPropertyAnimation* animation = new QPropertyAnimation; |
| 103 |
animation->setTargetObject(&m_list); |
| 104 |
animation->setPropertyName("pos"); |
| 105 |
animation->setEasingCurve(QEasingCurve::OutCubic); |
| 106 |
animation->setDuration(500); |
| 107 |
animation->setEndValue(QPoint(-m_list.width(), -m_list.height())); |
| 108 |
|
| 109 |
m_animation = animation; |
| 110 |
connect(animation, SIGNAL(finished()), this, SLOT(outAnimationEnd())); |
| 111 |
m_animation->start(QAbstractAnimation::DeleteWhenStopped); |
| 112 |
#else |
| 113 |
outAnimationEnd(); |
| 114 |
#endif |
| 115 |
} |
| 116 |
|
| 117 |
void CustomPopup::populate(const QList<Item>& items) |
| 118 |
{ |
| 119 |
|
| 120 |
m_list.clear(); |
| 121 |
|
| 122 |
for (int i = 0; i < items.size(); i++) { |
| 123 |
if (items[i].type == QWebPopup::Item::Separator) |
| 124 |
m_list.addItem("--------------"); |
| 125 |
else |
| 126 |
m_list.addItem(items[i].text); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
void CustomPopup::inAnimationEnd() |
| 131 |
{ |
| 132 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0) |
| 133 |
m_animation = 0; |
| 134 |
#endif |
| 135 |
m_list.setFocus(); |
| 136 |
} |
| 137 |
|
| 138 |
void CustomPopup::outAnimationEnd() |
| 139 |
{ |
| 140 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0) |
| 141 |
m_animation = 0; |
| 142 |
#endif |
| 143 |
didHide(true); |
| 144 |
m_list.setParent(0); |
| 145 |
m_list.setVisible(false); |
| 146 |
} |
| 147 |
|
| 148 |
void CustomPopup::itemChanged() |
| 149 |
{ |
| 150 |
setIndex(m_list.currentIndex().row()); |
| 151 |
hide(); |
| 152 |
} |