|
| 1 | +#include "extension_qt/qml_web_view_enumerator.h" |
| 2 | + |
| 3 | +#include "webdriver_session.h" |
| 4 | +#include "webdriver_logging.h" |
| 5 | + |
| 6 | +#include "extension_qt/declarative_item_view_handle.h" |
| 7 | + |
| 8 | +#include <QtCore/QGlobalStatic> |
| 9 | +#include <QtCore/QDebug> |
| 10 | +#include <QtGui/QWidget> |
| 11 | +#include <QtDeclarative/QDeclarativeView> |
| 12 | +#include <QtGui/QApplication> |
| 13 | + |
| 14 | +namespace webdriver { |
| 15 | + |
| 16 | +void QmlWebViewEnumeratorImpl::EnumerateViews(Session* session, std::set<ViewId>* views) const { |
| 17 | + session->logger().Log(kInfoLogLevel, ">>>>> QDeclarativeWebView enumerate"); |
| 18 | + |
| 19 | + foreach(QWidget* pWidget, qApp->allWidgets()) { |
| 20 | + if (pWidget->isHidden()) continue; |
| 21 | + |
| 22 | + QDeclarativeView* pView = qobject_cast<QDeclarativeView*>(pWidget); |
| 23 | + if (NULL != pView) { |
| 24 | + |
| 25 | + QDeclarativeItem *parentItem = qobject_cast<QDeclarativeItem*>(pView->rootObject()); |
| 26 | + |
| 27 | + if (NULL == parentItem) continue; |
| 28 | + |
| 29 | + QList<QDeclarativeItem*> childs = parentItem->findChildren<QDeclarativeItem*>(); |
| 30 | + childs.append(parentItem); |
| 31 | + foreach(QDeclarativeItem *child, childs) { |
| 32 | + if (isWebView(child)) { |
| 33 | + // found |
| 34 | + ViewHandlePtr handle(new QDeclarativeItemViewHandle(child)); |
| 35 | + ViewId viewId = session->GetViewForHandle(handle); |
| 36 | + if (!viewId.is_valid()) { |
| 37 | + if (session->AddNewView(handle, &viewId)) { |
| 38 | + session->logger().Log(kInfoLogLevel, |
| 39 | + "QmlWebViewEnumerator found new view("+viewId.id()+")"); |
| 40 | + } |
| 41 | + } |
| 42 | + if (viewId.is_valid()) { |
| 43 | + views->insert(viewId); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +bool QmlWebViewEnumeratorImpl::isWebView(QDeclarativeItem* item) const { |
| 52 | + if (NULL == item) return false; |
| 53 | + if (!item->isVisible()) return false; |
| 54 | + if (!item->isEnabled()) return false; |
| 55 | + |
| 56 | + // check if element is QML WebView |
| 57 | + QString className(item->metaObject()->className()); |
| 58 | + className.remove(QRegExp(QLatin1String("_QMLTYPE_\\d+"))); \ |
| 59 | + className.remove(QRegExp(QLatin1String("_QML_\\d+"))); \ |
| 60 | + if (className.startsWith(QLatin1String("QDeclarative"))) className = className.mid(12); |
| 61 | + |
| 62 | + if (className == "WebView") { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + return false; |
| 67 | +} |
| 68 | + |
| 69 | +} // namespace webdriver |
0 commit comments