Commit 67788b6f authored by Nikita Baryshnikov's avatar Nikita Baryshnikov

Help: show ToolTips on links hovers

Change-Id: Icce21b848f38b4f45b58ccff626b2e23a7e8ef98
Reviewed-by: default avatarEike Ziller <eike.ziller@theqtcompany.com>
parent 422b5790
......@@ -68,12 +68,15 @@ public:
void setOpenInNewPageActionVisible(bool visible);
WebView *webView() const;
void startToolTipTimer(const QPoint &pos, const QString &text);
void hideToolTip();
protected:
void hideEvent(QHideEvent *);
void showEvent(QShowEvent *);
private:
void showToolTip();
MacWebKitHelpWidgetPrivate *d;
};
......
......@@ -39,7 +39,10 @@
#include <QApplication>
#include <QClipboard>
#include <QHelpEngine>
#include <QStyle>
#include <QTimer>
#include <QtMac>
#include <QToolTip>
#include <QUrl>
#include <QVBoxLayout>
......@@ -86,6 +89,22 @@ AutoreleasePool::~AutoreleasePool()
[pool release];
}
// #pragma mark -- mac helpers
// copy from qcocoahelpers.mm
static int mainScreenHeight()
{
// The first screen in the screens array is documented
// to have the (0,0) origin.
NSRect screenFrame = [[[NSScreen screens] firstObject] frame];
return screenFrame.size.height;
}
static QPoint flipPoint(const NSPoint &p)
{
return QPoint(p.x, mainScreenHeight() - p.y);
}
// #pragma mark -- DOMNodeIterator (PrivateExtensions)
@interface DOMNodeIterator (PrivateExtensions)
......@@ -264,22 +283,23 @@ static void ensureProtocolHandler()
@interface UIDelegate : NSObject
{
QWidget *widget;
Help::Internal::MacWebKitHelpWidget *widget;
}
@property (assign) BOOL openInNewPageActionVisible;
- (id)initWithWidget:(QWidget *)theWidget;
- (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;
@end
@implementation UIDelegate
- (id)initWithWidget:(QWidget *)theWidget
- (id)initWithWidget:(Help::Internal::MacWebKitHelpWidget *)theWidget
{
self = [super init];
if (self) {
......@@ -343,6 +363,23 @@ static void ensureProtocolHandler()
return viewer->widget()->webView();
}
- (void)webView:(WebView *)sender mouseDidMoveOverElement:(NSDictionary *)elementInformation
modifierFlags:(NSUInteger)modifierFlags
{
Q_UNUSED(sender)
if (!elementInformation || (modifierFlags & NSDeviceIndependentModifierFlagsMask) != 0) {
widget->hideToolTip();
return;
}
NSURL *url = [elementInformation objectForKey:WebElementLinkURLKey];
if (!url) {
widget->hideToolTip();
return;
}
widget->startToolTipTimer(flipPoint(NSEvent.mouseLocation),
QString::fromNSString(url.absoluteString));
}
@end
// #pragma mark -- MyWebView
......@@ -405,6 +442,9 @@ public:
FrameLoadDelegate *m_frameLoadDelegate;
UIDelegate *m_uiDelegate;
NSResponder *m_savedResponder;
QTimer m_toolTipTimer;
QPoint m_toolTipPos;
QString m_toolTipText;
};
// #pragma mark -- MacWebKitHelpWidget
......@@ -413,6 +453,8 @@ MacWebKitHelpWidget::MacWebKitHelpWidget(MacWebKitHelpViewer *parent)
: QMacCocoaViewContainer(0, parent),
d(new MacWebKitHelpWidgetPrivate)
{
d->m_toolTipTimer.setSingleShot(true);
connect(&d->m_toolTipTimer, &QTimer::timeout, this, &MacWebKitHelpWidget::showToolTip);
AutoreleasePool pool; Q_UNUSED(pool)
d->m_webView = [[MyWebView alloc] init];
// Turn layered rendering on.
......@@ -442,6 +484,20 @@ WebView *MacWebKitHelpWidget::webView() const
return d->m_webView;
}
void MacWebKitHelpWidget::startToolTipTimer(const QPoint &pos, const QString &text)
{
int delay = style()->styleHint(QStyle::SH_ToolTip_WakeUpDelay, 0, this, 0);
d->m_toolTipPos = pos;
d->m_toolTipText = text;
d->m_toolTipTimer.start(delay);
}
void MacWebKitHelpWidget::hideToolTip()
{
d->m_toolTipTimer.stop();
QToolTip::showText(QPoint(), QString());
}
void MacWebKitHelpWidget::hideEvent(QHideEvent *)
{
[d->m_webView setHidden:YES];
......@@ -452,6 +508,11 @@ void MacWebKitHelpWidget::showEvent(QShowEvent *)
[d->m_webView setHidden:NO];
}
void MacWebKitHelpWidget::showToolTip()
{
QToolTip::showText(d->m_toolTipPos, d->m_toolTipText, this);
}
// #pragma mark -- MacWebKitHelpViewer
MacWebKitHelpViewer::MacWebKitHelpViewer(QWidget *parent)
......
......@@ -38,6 +38,7 @@
#include <coreplugin/find/findplugin.h>
#include <utils/hostosinfo.h>
#include <utils/networkaccessmanager.h>
#include <utils/qtcassert.h>
#include <QAction>
......@@ -59,7 +60,6 @@
#include <QNetworkReply>
#include <QNetworkRequest>
#include <utils/networkaccessmanager.h>
#include <cstring>
......@@ -411,6 +411,7 @@ QtWebKitHelpViewer::QtWebKitHelpViewer(QWidget *parent)
connect(m_webView->page(), SIGNAL(printRequested(QWebFrame*)), this, SIGNAL(printRequested()));
connect(m_webView, SIGNAL(backwardAvailable(bool)), this, SIGNAL(backwardAvailable(bool)));
connect(m_webView, SIGNAL(forwardAvailable(bool)), this, SIGNAL(forwardAvailable(bool)));
connect(page(), &HelpPage::linkHovered, this, &QtWebKitHelpViewer::setToolTip);
}
QFont QtWebKitHelpViewer::viewerFont() const
......
......@@ -43,6 +43,7 @@
#include <QHelpEngine>
#include <QKeyEvent>
#include <QMenu>
#include <QToolTip>
#include <QVBoxLayout>
using namespace Help;
......@@ -405,6 +406,10 @@ bool TextBrowserHelpWidget::eventFilter(QObject *obj, QEvent *event)
Core::FindPlugin::instance()->openFindToolBar(Core::FindPlugin::FindForwardDirection);
return true;
}
} else if (event->type() == QEvent::ToolTip) {
QHelpEvent *e = static_cast<QHelpEvent *>(event);
QToolTip::showText(e->globalPos(), linkAt(e->pos()));
return true;
}
}
return QTextBrowser::eventFilter(obj, event);
......
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