diff options
Diffstat (limited to 'src/webengine/ui_delegates_manager.cpp')
-rw-r--r-- | src/webengine/ui_delegates_manager.cpp | 104 |
1 files changed, 56 insertions, 48 deletions
diff --git a/src/webengine/ui_delegates_manager.cpp b/src/webengine/ui_delegates_manager.cpp index 573d5018b..7e49bc77d 100644 --- a/src/webengine/ui_delegates_manager.cpp +++ b/src/webengine/ui_delegates_manager.cpp @@ -84,8 +84,8 @@ static QString fileNameForComponent(UIDelegatesManager::ComponentType type) static QPoint calculateToolTipPosition(QPoint &position, QSize &toolTip) { QRect screen; - QList<QScreen *> screens = QGuiApplication::screens(); - Q_FOREACH (const QScreen *src, screens) + const QList<QScreen *> screens = QGuiApplication::screens(); + for (const QScreen *src : screens) if (src->availableGeometry().contains(position)) screen = src->availableGeometry(); @@ -119,11 +119,6 @@ const char *defaultPropertyName(QObject *obj) return info.value(); } -MenuItemHandler::MenuItemHandler(QObject *parent) - : QObject(parent) -{ -} - #define COMPONENT_MEMBER_INIT(TYPE, COMPONENT) \ , COMPONENT##Component(0) @@ -143,15 +138,21 @@ UIDelegatesManager::~UIDelegatesManager() component = &COMPONENT##Component; \ break; -bool UIDelegatesManager::initializeImportDirs(QStringList &dirs, QQmlEngine *engine) { - foreach (const QString &path, engine->importPathList()) { - QFileInfo fi(path % QLatin1String("/QtWebEngine/Controls1Delegates/")); - if (fi.exists()) { +bool UIDelegatesManager::initializeImportDirs(QStringList &dirs, QQmlEngine *engine) +{ + const QStringList paths = engine->importPathList(); + for (const QString &path : paths) { + QString importPath = path % QLatin1String("/QtWebEngine/Controls1Delegates/"); + + // resource paths have to be tested using the ":/" prefix + if (importPath.startsWith(QLatin1String("qrc:/"))) + importPath.remove(0, 3); + + QFileInfo fi(importPath); + if (fi.exists()) dirs << fi.absolutePath(); - return true; - } } - return false; + return !dirs.isEmpty(); } bool UIDelegatesManager::ensureComponentLoaded(ComponentType type) @@ -177,16 +178,21 @@ bool UIDelegatesManager::ensureComponentLoaded(ComponentType type) if (!engine) return false; - foreach (const QString &importDir, m_importDirs) { - QFileInfo fi(importDir % QLatin1Char('/') % fileName); - if (!fi.exists()) + for (const QString &importDir : qAsConst(m_importDirs)) { + const QString componentFilePath = importDir % QLatin1Char('/') % fileName; + + if (!QFileInfo(componentFilePath).exists()) continue; + // FIXME: handle async loading - *component = (new QQmlComponent(engine, QUrl::fromLocalFile(fi.absoluteFilePath()), + *component = (new QQmlComponent(engine, + importDir.startsWith(QLatin1String(":/")) ? QUrl(QLatin1String("qrc") + componentFilePath) + : QUrl::fromLocalFile(componentFilePath), QQmlComponent::PreferSynchronous, m_view)); if ((*component)->status() != QQmlComponent::Ready) { - foreach (const QQmlError &err, (*component)->errors()) + const QList<QQmlError> errs = (*component)->errors(); + for (const QQmlError &err : errs) qWarning("QtWebEngine: component error: %s\n", qPrintable(err.toString())); delete *component; *component = nullptr; @@ -201,26 +207,25 @@ bool UIDelegatesManager::ensureComponentLoaded(ComponentType type) if (!prop.isSignalProperty()) \ qWarning("%s is missing %s signal property.\n", qPrintable(location.toString()), qPrintable(prop.name())); -void UIDelegatesManager::addMenuItem(MenuItemHandler *menuItemHandler, const QString &text, const QString &iconName, bool enabled, - bool checkable, bool checked) +void UIDelegatesManager::addMenuItem(QQuickWebEngineAction *action, QObject *menu, bool checkable, bool checked) { - Q_ASSERT(menuItemHandler); + Q_ASSERT(action); if (!ensureComponentLoaded(MenuItem)) return; QObject *it = menuItemComponent->beginCreate(qmlContext(m_view)); - QQmlProperty(it, QStringLiteral("text")).write(text); - QQmlProperty(it, QStringLiteral("iconName")).write(iconName); - QQmlProperty(it, QStringLiteral("enabled")).write(enabled); + QQmlProperty(it, QStringLiteral("text")).write(action->text()); + QQmlProperty(it, QStringLiteral("iconName")).write(action->iconName()); + QQmlProperty(it, QStringLiteral("enabled")).write(action->isEnabled()); QQmlProperty(it, QStringLiteral("checkable")).write(checkable); QQmlProperty(it, QStringLiteral("checked")).write(checked); QQmlProperty signal(it, QStringLiteral("onTriggered")); CHECK_QML_SIGNAL_PROPERTY(signal, menuItemComponent->url()); - QObject::connect(it, signal.method(), menuItemHandler, QMetaMethod::fromSignal(&MenuItemHandler::triggered)); + const QMetaObject *actionMeta = action->metaObject(); + QObject::connect(it, signal.method(), action, actionMeta->method(actionMeta->indexOfSlot("trigger()"))); menuItemComponent->completeCreate(); - QObject *menu = menuItemHandler->parent(); it->setParent(menu); QQmlListReference entries(menu, defaultPropertyName(menu), qmlEngine(m_view)); @@ -503,12 +508,7 @@ public: if (pos.isNull() || !item->contains(pos)) return; const QPoint oldPos = QCursor::pos(); -#if (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)) const QPoint globalPos = item->mapToGlobal(QPointF(pos)).toPoint(); -#else - const QPoint posInWindow = item->mapToItem(item->window()->contentItem(), QPointF(pos)).toPoint(); - const QPoint globalPos = item->window()->mapToGlobal(posInWindow); -#endif if (oldPos == globalPos) return; m_oldCursorPos = oldPos; @@ -575,15 +575,26 @@ UI2DelegatesManager::UI2DelegatesManager(QQuickWebEngineView *view) : UIDelegate bool UI2DelegatesManager::initializeImportDirs(QStringList &dirs, QQmlEngine *engine) { - foreach (const QString &path, engine->importPathList()) { - QFileInfo fi1(path % QLatin1String("/QtWebEngine/Controls1Delegates/")); - QFileInfo fi2(path % QLatin1String("/QtWebEngine/Controls2Delegates/")); - if (fi1.exists() && fi2.exists()) { - dirs << fi2.absolutePath() << fi1.absolutePath(); - return true; + const QStringList paths = engine->importPathList(); + for (const QString &path : paths) { + QString controls2ImportPath = path % QLatin1String("/QtWebEngine/Controls2Delegates/"); + QString controls1ImportPath = path % QLatin1String("/QtWebEngine/Controls1Delegates/"); + + // resource paths have to be tested using the ":/" prefix + if (controls2ImportPath.startsWith(QLatin1String("qrc:/"))) { + controls2ImportPath.remove(0, 3); + controls1ImportPath.remove(0, 3); } + + QFileInfo fi2(controls2ImportPath); + if (fi2.exists()) + dirs << fi2.absolutePath(); + + QFileInfo fi1(controls1ImportPath); + if (fi1.exists()) + dirs << fi1.absolutePath(); } - return false; + return !dirs.isEmpty(); } QObject *UI2DelegatesManager::addMenu(QObject *parentMenu, const QString &title, const QPoint &pos) @@ -614,28 +625,25 @@ QObject *UI2DelegatesManager::addMenu(QObject *parentMenu, const QString &title, return menu; } -void UI2DelegatesManager::addMenuItem(MenuItemHandler *menuItemHandler, const QString &text, - const QString &/*iconName*/, bool enabled, - bool checkable, bool checked) +void UI2DelegatesManager::addMenuItem(QQuickWebEngineAction *action, QObject *menu, bool checkable, bool checked) { - Q_ASSERT(menuItemHandler); + Q_ASSERT(action); if (!ensureComponentLoaded(MenuItem)) return; QObject *it = menuItemComponent->beginCreate(qmlContext(m_view)); - it->setProperty("text", text); - it->setProperty("enabled", enabled); + it->setProperty("text", action->text()); + it->setProperty("enabled", action->isEnabled()); it->setProperty("checked", checked); it->setProperty("checkable", checkable); QQmlProperty signal(it, QStringLiteral("onTriggered")); CHECK_QML_SIGNAL_PROPERTY(signal, menuItemComponent->url()); - QObject::connect(it, signal.method(), menuItemHandler, - QMetaMethod::fromSignal(&MenuItemHandler::triggered)); + const QMetaObject *actionMeta = action->metaObject(); + QObject::connect(it, signal.method(), action, actionMeta->method(actionMeta->indexOfSlot("trigger()"))); menuItemComponent->completeCreate(); - QObject *menu = menuItemHandler->parent(); it->setParent(menu); QQmlListReference entries(menu, defaultPropertyName(menu), qmlEngine(m_view)); |