Commit a416a5b6 authored by Eike Ziller's avatar Eike Ziller
Browse files

Help: Option to open links in window



Task-number: QTCREATORBUG-16842
Change-Id: I420f48a00c56ebc89bade7e91ceb8ec391316e36
Reviewed-by: David Schulz's avatarDavid Schulz <david.schulz@qt.io>
Reviewed-by: Riitta-Leena Miettinen's avatarLeena Miettinen <riitta-leena.miettinen@qt.io>
parent 2cd307a7
......@@ -63,5 +63,8 @@ static const char SB_BOOKMARKS[] = QT_TRANSLATE_NOOP("Help::Internal::HelpPlugin
static const char SB_OPENPAGES[] = QT_TRANSLATE_NOOP("Help::Internal::HelpPlugin", "Open Pages");
static const char SB_SEARCH[] = QT_TRANSLATE_NOOP("Help::Internal::HelpPlugin", "Search");
static const char TR_OPEN_LINK_AS_NEW_PAGE[] = QT_TRANSLATE_NOOP("HelpViewer", "Open Link as New Page");
static const char TR_OPEN_LINK_IN_WINDOW[] = QT_TRANSLATE_NOOP("HelpViewer", "Open Link in Window");
} // Constants
} // Help
......@@ -108,6 +108,8 @@ static const char kToolTipHelpContext[] = "Help.ToolTip";
using namespace Core;
using namespace Utils;
static HelpPlugin *m_instance = nullptr;
HelpPlugin::HelpPlugin()
: m_mode(0),
m_centralWidget(0),
......@@ -116,6 +118,7 @@ HelpPlugin::HelpPlugin()
m_helpManager(0),
m_openPagesManager(0)
{
m_instance = this;
}
HelpPlugin::~HelpPlugin()
......@@ -476,7 +479,7 @@ void HelpPlugin::setupHelpEngineIfNeeded()
LocalHelpManager::setupGuiHelpEngine();
}
bool HelpPlugin::canShowHelpSideBySide() const
bool HelpPlugin::canShowHelpSideBySide()
{
RightPanePlaceHolder *placeHolder = RightPanePlaceHolder::current();
if (!placeHolder)
......@@ -503,19 +506,19 @@ HelpViewer *HelpPlugin::viewerForHelpViewerLocation(HelpManager::HelpViewerLocat
: HelpManager::HelpModeAlways;
if (actualLocation == HelpManager::ExternalHelpAlways)
return externalHelpViewer();
return m_instance->externalHelpViewer();
if (actualLocation == HelpManager::SideBySideAlways) {
createRightPaneContextViewer();
RightPaneWidget::instance()->setWidget(m_rightPaneSideBarWidget);
m_instance->createRightPaneContextViewer();
RightPaneWidget::instance()->setWidget(m_instance->m_rightPaneSideBarWidget);
RightPaneWidget::instance()->setShown(true);
return m_rightPaneSideBarWidget->currentViewer();
return m_instance->m_rightPaneSideBarWidget->currentViewer();
}
QTC_CHECK(actualLocation == HelpManager::HelpModeAlways);
activateHelpMode(); // should trigger an createPage...
HelpViewer *viewer = m_centralWidget->currentViewer();
HelpViewer *viewer = m_instance->m_centralWidget->currentViewer();
if (!viewer)
viewer = OpenPagesManager::instance().createPage();
return viewer;
......
......@@ -75,6 +75,8 @@ public:
void extensionsInitialized();
ShutdownFlag aboutToShutdown();
static HelpViewer *viewerForHelpViewerLocation(Core::HelpManager::HelpViewerLocation location);
static HelpViewer *createHelpViewer(qreal zoom);
private:
......@@ -101,9 +103,8 @@ private:
void slotSystemInformation();
void resetFilter();
void activateHelpMode();
bool canShowHelpSideBySide() const;
HelpViewer *viewerForHelpViewerLocation(Core::HelpManager::HelpViewerLocation location);
static void activateHelpMode();
static bool canShowHelpSideBySide();
HelpViewer *viewerForContextHelp();
HelpWidget *createHelpWidget(const Core::Context &context, HelpWidget::WidgetStyle style);
void createRightPaneContextViewer();
......
......@@ -86,6 +86,19 @@ HelpViewer::HelpViewer(QWidget *parent)
{
}
void HelpViewer::setActionVisible(Action action, bool visible)
{
if (visible)
m_visibleActions |= Actions(action);
else
m_visibleActions &= ~Actions(action);
}
bool HelpViewer::isActionVisible(HelpViewer::Action action)
{
return (m_visibleActions & Actions(action)) != 0;
}
bool HelpViewer::isLocalUrl(const QUrl &url)
{
return url.scheme() == "about" // "No documenation available"
......
......@@ -42,6 +42,12 @@ class HelpViewer : public QWidget
Q_OBJECT
public:
enum class Action {
NewPage = 0x01,
ExternalWindow = 0x02
};
Q_DECLARE_FLAGS(Actions, Action)
explicit HelpViewer(QWidget *parent = 0);
~HelpViewer() { }
......@@ -65,7 +71,8 @@ public:
virtual bool isBackwardAvailable() const = 0;
virtual void addBackHistoryItems(QMenu *backMenu) = 0;
virtual void addForwardHistoryItems(QMenu *forwardMenu) = 0;
virtual void setOpenInNewPageActionVisible(bool visible) = 0;
void setActionVisible(Action action, bool visible);
bool isActionVisible(Action action);
virtual bool findText(const QString &text, Core::FindFlags flags,
bool incremental, bool fromSearch, bool *wrapped = 0) = 0;
......@@ -95,10 +102,14 @@ signals:
void forwardAvailable(bool);
void backwardAvailable(bool);
void loadFinished();
void newPageRequested(const QUrl &url);
void externalPageRequested(const QUrl &url);
protected:
void slotLoadStarted();
void slotLoadFinished();
Actions m_visibleActions = 0;
};
} // namespace Internal
......
......@@ -417,8 +417,8 @@ void HelpWidget::addViewer(HelpViewer *viewer)
{
m_viewerStack->addWidget(viewer);
viewer->setFocus(Qt::OtherFocusReason);
if (m_style == SideBarWidget || m_style == ExternalWindow)
viewer->setOpenInNewPageActionVisible(false);
viewer->setActionVisible(HelpViewer::Action::NewPage, m_style == ModeWidget);
viewer->setActionVisible(HelpViewer::Action::ExternalWindow, m_style != ExternalWindow);
connect(viewer, &HelpViewer::sourceChanged, this, [viewer, this](const QUrl &url) {
if (currentViewer() == viewer)
emit sourceChanged(url);
......@@ -438,6 +438,15 @@ void HelpWidget::addViewer(HelpViewer *viewer)
connect(viewer, &HelpViewer::titleChanged, this, &HelpWidget::updateWindowTitle);
connect(viewer, &HelpViewer::loadFinished, this, &HelpWidget::highlightSearchTerms);
connect(viewer, &HelpViewer::newPageRequested, [](const QUrl &url) {
OpenPagesManager::instance().createPage(url);
});
connect(viewer, &HelpViewer::externalPageRequested, [](const QUrl &url) {
HelpViewer *viewer = HelpPlugin::viewerForHelpViewerLocation(Core::HelpManager::ExternalHelpAlways);
if (QTC_GUARD(viewer))
viewer->setSource(url);
Core::ICore::raiseWindow(viewer);
});
updateCloseButton();
}
......
......@@ -59,11 +59,10 @@ public:
MacWebKitHelpWidget(MacWebKitHelpViewer *parent);
~MacWebKitHelpWidget();
void setOpenInNewPageActionVisible(bool visible);
WebView *webView() const;
void startToolTipTimer(const QPoint &pos, const QString &text);
void hideToolTip();
MacWebKitHelpViewer *viewer() const;
protected:
void hideEvent(QHideEvent *);
......@@ -102,7 +101,7 @@ public:
bool isBackwardAvailable() const;
void addBackHistoryItems(QMenu *backMenu);
void addForwardHistoryItems(QMenu *forwardMenu);
void setOpenInNewPageActionVisible(bool visible);
void setActionVisible(bool visible);
bool findText(const QString &text, Core::FindFlags flags,
bool incremental, bool fromSearch, bool *wrapped = 0);
......
......@@ -25,6 +25,7 @@
#include "macwebkithelpviewer.h"
#include "helpconstants.h"
#include "localhelpmanager.h"
#include "openpagesmanager.h"
......@@ -280,15 +281,15 @@ static void ensureProtocolHandler()
Help::Internal::MacWebKitHelpWidget *widget;
}
@property (assign) BOOL openInNewPageActionVisible;
- (id)initWithWidget:(Help::Internal::MacWebKitHelpWidget *)theWidget;
- (void)webView:(WebView *)sender makeFirstResponder:(NSResponder *)responder;
- (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element
defaultMenuItems:(NSArray *)defaultMenuItems;
- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request;
- (void)webView:(WebView *)sender mouseDidMoveOverElement:(NSDictionary *)elementInformation
modifierFlags:(NSUInteger)modifierFlags;
- (void)openAsNewPage:(id)sender;
- (void)openInWindow:(id)sender;
@end
@implementation UIDelegate
......@@ -298,7 +299,6 @@ static void ensureProtocolHandler()
self = [super init];
if (self) {
widget = theWidget;
self.openInNewPageActionVisible = YES;
}
return self;
}
......@@ -312,6 +312,16 @@ static void ensureProtocolHandler()
}
}
static NSMenuItem *menuItem(NSURL *url, id target, SEL action, const QString &title)
{
NSMenuItem *openItem = [[NSMenuItem alloc] initWithTitle:title.toNSString()
action:action
keyEquivalent:@""];
openItem.representedObject = url;
openItem.target = target;
return [openItem autorelease];
}
- (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element
defaultMenuItems:(NSArray *)defaultMenuItems
{
......@@ -335,11 +345,18 @@ static void ensureProtocolHandler()
[ret addObject:item];
break;
case WebMenuItemTagOpenLinkInNewWindow:
case WebMenuItemTagOpenImageInNewWindow:
if (self.openInNewPageActionVisible) {
item.title = QCoreApplication::translate("HelpViewer", "Open Link as New Page").toNSString();
[ret addObject:item];
case WebMenuItemTagOpenImageInNewWindow: {
NSURL *url = [element objectForKey:WebElementLinkURLKey];
if (QTC_GUARD(url)) {
if (widget->viewer()->isActionVisible(Help::Internal::HelpViewer::Action::NewPage))
[ret addObject:menuItem(url, self, @selector(openAsNewPage:),
QCoreApplication::translate("HelpViewer", Help::Constants::TR_OPEN_LINK_AS_NEW_PAGE))];
if (widget->viewer()->isActionVisible(Help::Internal::HelpViewer::Action::ExternalWindow))
[ret addObject:menuItem(url, self, @selector(openInWindow:),
QCoreApplication::translate("HelpViewer", Help::Constants::TR_OPEN_LINK_IN_WINDOW))];
}
break;
}
default:
break;
}
......@@ -347,16 +364,6 @@ static void ensureProtocolHandler()
return [ret autorelease];
}
- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
{
Q_UNUSED(sender)
Q_UNUSED(request)
Help::Internal::MacWebKitHelpViewer* viewer
= static_cast<Help::Internal::MacWebKitHelpViewer *>(
Help::Internal::OpenPagesManager::instance().createPage(QUrl()));
return viewer->widget()->webView();
}
- (void)webView:(WebView *)sender mouseDidMoveOverElement:(NSDictionary *)elementInformation
modifierFlags:(NSUInteger)modifierFlags
{
......@@ -374,6 +381,16 @@ static void ensureProtocolHandler()
QString::fromNSString(url.absoluteString));
}
- (void)openAsNewPage:(id)item
{
widget->viewer()->newPageRequested(QUrl::fromNSURL([item representedObject]));
}
- (void)openInWindow:(id)item
{
widget->viewer()->externalPageRequested(QUrl::fromNSURL([item representedObject]));
}
@end
// #pragma mark -- MyWebView
......@@ -420,8 +437,9 @@ namespace Internal {
class MacWebKitHelpWidgetPrivate
{
public:
MacWebKitHelpWidgetPrivate()
: m_savedResponder(nil)
MacWebKitHelpWidgetPrivate(MacWebKitHelpViewer *parent)
: m_viewer(parent),
m_savedResponder(nil)
{
}
......@@ -432,6 +450,7 @@ public:
[m_uiDelegate release];
}
MacWebKitHelpViewer *m_viewer;
WebView *m_webView;
FrameLoadDelegate *m_frameLoadDelegate;
UIDelegate *m_uiDelegate;
......@@ -445,7 +464,7 @@ public:
MacWebKitHelpWidget::MacWebKitHelpWidget(MacWebKitHelpViewer *parent)
: QMacCocoaViewContainer(0, parent),
d(new MacWebKitHelpWidgetPrivate)
d(new MacWebKitHelpWidgetPrivate(parent))
{
d->m_toolTipTimer.setSingleShot(true);
connect(&d->m_toolTipTimer, &QTimer::timeout, this, &MacWebKitHelpWidget::showToolTip);
......@@ -462,6 +481,7 @@ MacWebKitHelpWidget::MacWebKitHelpWidget(MacWebKitHelpViewer *parent)
setCocoaView(d->m_webView);
}
}
MacWebKitHelpWidget::~MacWebKitHelpWidget()
......@@ -469,9 +489,9 @@ MacWebKitHelpWidget::~MacWebKitHelpWidget()
delete d;
}
void MacWebKitHelpWidget::setOpenInNewPageActionVisible(bool visible)
MacWebKitHelpViewer *MacWebKitHelpWidget::viewer() const
{
d->m_uiDelegate.openInNewPageActionVisible = visible;
return d->m_viewer;
}
WebView *MacWebKitHelpWidget::webView() const
......@@ -679,11 +699,6 @@ void MacWebKitHelpViewer::addForwardHistoryItems(QMenu *forwardMenu)
}
}
void MacWebKitHelpViewer::setOpenInNewPageActionVisible(bool visible)
{
m_widget->setOpenInNewPageActionVisible(visible);
}
DOMRange *MacWebKitHelpViewer::findText(NSString *text, bool forward, bool caseSensitive, DOMNode *startNode, int startOffset)
{
QTC_ASSERT(text, return nil);
......
......@@ -198,11 +198,6 @@ void TextBrowserHelpViewer::addForwardHistoryItems(QMenu *forwardMenu)
}
}
void TextBrowserHelpViewer::setOpenInNewPageActionVisible(bool visible)
{
m_textBrowser->m_openInNewPageActionVisible = visible;
}
bool TextBrowserHelpViewer::findText(const QString &text, Core::FindFlags flags,
bool incremental, bool fromSearch, bool *wrapped)
{
......@@ -305,7 +300,6 @@ TextBrowserHelpWidget::TextBrowserHelpWidget(TextBrowserHelpViewer *parent)
: QTextBrowser(parent)
, zoomCount(0)
, forceFont(false)
, m_openInNewPageActionVisible(true)
, m_parent(parent)
{
installEventFilter(this);
......@@ -334,16 +328,6 @@ QString TextBrowserHelpWidget::linkAt(const QPoint &pos)
return anchor;
}
void TextBrowserHelpWidget::openLink(const QUrl &url, bool newPage)
{
if (url.isEmpty() || !url.isValid())
return;
if (newPage)
OpenPagesManager::instance().createPage(url);
else
setSource(url);
}
void TextBrowserHelpWidget::scaleUp()
{
if (zoomCount < 10) {
......@@ -373,13 +357,18 @@ void TextBrowserHelpWidget::contextMenuEvent(QContextMenuEvent *event)
if (!link.isEmpty() && link.isValid()) {
QAction *action = menu.addAction(tr("Open Link"));
connect(action, &QAction::triggered, this, [this, link]() {
openLink(link, false/*newPage*/);
setSource(link);
});
if (m_openInNewPageActionVisible) {
action = menu.addAction(QCoreApplication::translate("HelpViewer",
"Open Link as New Page"));
if (m_parent->isActionVisible(HelpViewer::Action::NewPage)) {
action = menu.addAction(QCoreApplication::translate("HelpViewer", Constants::TR_OPEN_LINK_AS_NEW_PAGE));
connect(action, &QAction::triggered, this, [this, link]() {
emit m_parent->newPageRequested(link);
});
}
if (m_parent->isActionVisible(HelpViewer::Action::ExternalWindow)) {
action = menu.addAction(QCoreApplication::translate("HelpViewer", Constants::TR_OPEN_LINK_IN_WINDOW));
connect(action, &QAction::triggered, this, [this, link]() {
openLink(link, true/*newPage*/);
emit m_parent->externalPageRequested(link);
});
}
copyAnchorAction = menu.addAction(tr("Copy Link"));
......@@ -440,7 +429,7 @@ void TextBrowserHelpWidget::mouseReleaseEvent(QMouseEvent *e)
bool controlPressed = e->modifiers() & Qt::ControlModifier;
const QString link = linkAt(e->pos());
if ((controlPressed || e->button() == Qt::MidButton) && link.isEmpty()) {
openLink(link, true/*newPage*/);
emit m_parent->newPageRequested(QUrl(link));
return;
}
......
......@@ -62,7 +62,6 @@ public:
bool isBackwardAvailable() const;
void addBackHistoryItems(QMenu *backMenu);
void addForwardHistoryItems(QMenu *forwardMenu);
void setOpenInNewPageActionVisible(bool visible);
bool findText(const QString &text, Core::FindFlags flags,
bool incremental, bool fromSearch, bool *wrapped = 0);
......@@ -105,11 +104,9 @@ protected:
private:
QString linkAt(const QPoint& pos);
void openLink(const QUrl &url, bool newPage);
int zoomCount;
bool forceFont;
bool m_openInNewPageActionVisible;
TextBrowserHelpViewer *m_parent;
friend class Help::Internal::TextBrowserHelpViewer;
};
......
......@@ -25,6 +25,7 @@
#include "webenginehelpviewer.h"
#include "helpconstants.h"
#include "localhelpmanager.h"
#include "openpagesmanager.h"
......@@ -34,6 +35,7 @@
#include <QContextMenuEvent>
#include <QCoreApplication>
#include <QVBoxLayout>
#include <QWebEngineContextMenuData>
#include <QWebEngineHistory>
#include <QWebEngineProfile>
#include <QWebEngineSettings>
......@@ -98,7 +100,7 @@ WebEngineHelpViewer::WebEngineHelpViewer(QWidget *parent) :
QAction* action = m_widget->pageAction(QWebEnginePage::OpenLinkInNewTab);
action->setText(QCoreApplication::translate("HelpViewer",
"Open Link as New Page"));
Constants::TR_OPEN_LINK_AS_NEW_PAGE));
QWebEnginePage *viewPage = m_widget->page();
QTC_ASSERT(viewPage, return);
......@@ -202,11 +204,6 @@ void WebEngineHelpViewer::addForwardHistoryItems(QMenu *forwardMenu)
}
}
void WebEngineHelpViewer::setOpenInNewPageActionVisible(bool visible)
{
m_widget->setOpenInNewPageActionVisible(visible);
}
bool WebEngineHelpViewer::findText(const QString &text, Core::FindFlags flags, bool incremental,
bool fromSearch, bool *wrapped)
{
......@@ -276,35 +273,55 @@ WebEngineHelpPage::WebEngineHelpPage(QObject *parent)
{
}
#if QT_VERSION < QT_VERSION_CHECK(5, 7, 0)
QWebEnginePage *WebEngineHelpPage::createWindow(QWebEnginePage::WebWindowType)
{
auto viewer = static_cast<WebEngineHelpViewer *>(OpenPagesManager::instance().createPage());
return viewer->page();
}
#endif
WebView::WebView(QWidget *parent)
: QWebEngineView(parent)
{
}
void WebView::setOpenInNewPageActionVisible(bool visible)
WebView::WebView(WebEngineHelpViewer *viewer)
: QWebEngineView(viewer),
m_viewer(viewer)
{
m_openInNewPageActionVisible = visible;
}
void WebView::contextMenuEvent(QContextMenuEvent *event)
{
QMenu *menu = page()->createStandardContextMenu();
if (m_openInNewPageActionVisible) {
// insert Open In New Tab if OpenLinkInThisWindow is also there
const QList<QAction*> actions = menu->actions();
auto it = std::find(actions.cbegin(), actions.cend(), page()->action(QWebEnginePage::OpenLinkInThisWindow));
if (it != actions.cend()) {
// insert after
++it;
QAction *before = (it == actions.cend() ? 0 : *it);
menu->insertAction(before, page()->action(QWebEnginePage::OpenLinkInNewTab));
// insert Open as New Page etc if OpenLinkInThisWindow is also there
const QList<QAction*> actions = menu->actions();
auto it = std::find(actions.cbegin(), actions.cend(),
page()->action(QWebEnginePage::OpenLinkInThisWindow));
if (it != actions.cend()) {
// insert after
++it;
QAction *before = (it == actions.cend() ? 0 : *it);
#if QT_VERSION < QT_VERSION_CHECK(5, 7, 0)
if (m_viewer->isActionVisible(HelpViewer::Action::NewPage)) {
QAction *openLinkInNewTab = page()->action(QWebEnginePage::OpenLinkInNewTab);
menu->insertAction(before, openLinkInNewTab);
}
#else
QUrl url = page()->contextMenuData().linkUrl();
if (m_viewer->isActionVisible(HelpViewer::Action::NewPage)) {
auto openLink = new QAction(QCoreApplication::translate("HelpViewer",
Constants::TR_OPEN_LINK_IN_NEW_PAGE), menu);
connect(openLink, &QAction::triggered, m_viewer, [this, url] {
m_viewer->newPageRequested(url);
});
menu->insertAction(before, openLink);
}
if (m_viewer->isActionVisible(HelpViewer::Action::ExternalWindow)) {
auto openLink = new QAction(QCoreApplication::translate("HelpViewer",
Constants::TR_OPEN_LINK_IN_WINDOW), menu);
connect(openLink, &QAction::triggered, m_viewer, [this, url] {
m_viewer->externalPageRequested(url);
});
menu->insertAction(before, openLink);
}
#endif
}
connect(menu, &QMenu::aboutToHide, menu, &QObject::deleteLater);
......
......@@ -33,6 +33,8 @@
namespace Help {
namespace Internal {
class WebEngineHelpViewer;
class HelpUrlSchemeHandler : public QWebEngineUrlSchemeHandler
{
public:
......@@ -44,21 +46,21 @@ class WebEngineHelpPage : public QWebEnginePage
{
public:
explicit WebEngineHelpPage(QObject *parent = 0);
#if QT_VERSION < QT_VERSION_CHECK(5, 7, 0)
QWebEnginePage *createWindow(QWebEnginePage::WebWindowType) override;
#endif
};
class WebView : public QWebEngineView
{
public:
explicit WebView(QWidget *parent = 0);
void setOpenInNewPageActionVisible(bool visible);
explicit WebView(WebEngineHelpViewer *viewer);
protected:
void contextMenuEvent(QContextMenuEvent *event) override;
private:
bool m_openInNewPageActionVisible = true;
WebEngineHelpViewer *m_viewer;
};
class WebEngineHelpViewer : public HelpViewer
......@@ -80,7 +82,6 @@ public:
bool isBackwardAvailable() const override;
void addBackHistoryItems(QMenu *backMenu) override;
void addForwardHistoryItems(QMenu *forwardMenu) override;
void setOpenInNewPageActionVisible(bool visible) override;
bool findText(const QString &text, Core::FindFlags flags, bool incremental, bool fromSearch, bool *wrapped) override;
WebEngineHelpPage *page() const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment