Commit 6ed775f5 authored by Eike Ziller's avatar Eike Ziller

Fix wrong context help if tool tip was not shown for focus widget

The tool tip never has focus, so it cannot become the IContext that is
checked for context help. So, integrate the help id into Utils::ToolTip
and check the tool tip first when checking for context help.
As a side effect the [F1] button and help id for the tool tip is now also
available for use outside of the text editors.

Task-number: QTCREATORBUG-5345
Change-Id: Id975703caf161d1183c247e8ad8bb693b90fd306
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@theqtcompany.com>
Reviewed-by: default avatarDavid Schulz <david.schulz@theqtcompany.com>
parent 6781e3c9
...@@ -55,6 +55,17 @@ QTipLabel::QTipLabel(QWidget *parent) : ...@@ -55,6 +55,17 @@ QTipLabel::QTipLabel(QWidget *parent) :
QLabel(parent, Qt::ToolTip | Qt::BypassGraphicsProxyWidget) QLabel(parent, Qt::ToolTip | Qt::BypassGraphicsProxyWidget)
{} {}
void QTipLabel::setHelpId(const QString &id)
{
m_helpId = id;
update();
}
QString QTipLabel::helpId() const
{
return m_helpId;
}
ColorTip::ColorTip(QWidget *parent) ColorTip::ColorTip(QWidget *parent)
: QTipLabel(parent) : QTipLabel(parent)
...@@ -88,9 +99,9 @@ bool ColorTip::canHandleContentReplacement(int typeId) const ...@@ -88,9 +99,9 @@ bool ColorTip::canHandleContentReplacement(int typeId) const
return typeId == ToolTip::ColorContent; return typeId == ToolTip::ColorContent;
} }
bool ColorTip::equals(int typeId, const QVariant &other) const bool ColorTip::equals(int typeId, const QVariant &other, const QString &otherHelpId) const
{ {
return typeId == ToolTip::ColorContent && other == m_color; return typeId == ToolTip::ColorContent && otherHelpId == helpId() && other == m_color;
} }
void ColorTip::paintEvent(QPaintEvent *event) void ColorTip::paintEvent(QPaintEvent *event)
...@@ -131,7 +142,12 @@ void TextTip::setContent(const QVariant &content) ...@@ -131,7 +142,12 @@ void TextTip::setContent(const QVariant &content)
void TextTip::configure(const QPoint &pos, QWidget *w) void TextTip::configure(const QPoint &pos, QWidget *w)
{ {
setText(m_text); if (helpId().isEmpty())
setText(m_text);
else
setText(QString::fromLatin1("<table><tr><td valign=middle>%1</td><td>&nbsp;&nbsp;"
"<img src=\":/utils/tooltip/images/f1.png\"></td>"
"</tr></table>").arg(m_text));
// Make it look good with the default ToolTip font on Mac, which has a small descent. // Make it look good with the default ToolTip font on Mac, which has a small descent.
QFontMetrics fm(font()); QFontMetrics fm(font());
...@@ -166,9 +182,9 @@ int TextTip::showTime() const ...@@ -166,9 +182,9 @@ int TextTip::showTime() const
return 10000 + 40 * qMax(0, m_text.size() - 100); return 10000 + 40 * qMax(0, m_text.size() - 100);
} }
bool TextTip::equals(int typeId, const QVariant &other) const bool TextTip::equals(int typeId, const QVariant &other, const QString &otherHelpId) const
{ {
return typeId == ToolTip::TextContent && other.toString() == m_text; return typeId == ToolTip::TextContent && otherHelpId == helpId() && other.toString() == m_text;
} }
void TextTip::paintEvent(QPaintEvent *event) void TextTip::paintEvent(QPaintEvent *event)
...@@ -245,9 +261,10 @@ bool WidgetTip::canHandleContentReplacement(int typeId) const ...@@ -245,9 +261,10 @@ bool WidgetTip::canHandleContentReplacement(int typeId) const
return false; return false;
} }
bool WidgetTip::equals(int typeId, const QVariant &other) const bool WidgetTip::equals(int typeId, const QVariant &other, const QString &otherHelpId) const
{ {
return typeId == ToolTip::WidgetContent && other.value<QWidget *>() == m_widget; return typeId == ToolTip::WidgetContent && otherHelpId == helpId()
&& other.value<QWidget *>() == m_widget;
} }
// need to include it here to force it to be inside the namespaces // need to include it here to force it to be inside the namespaces
......
...@@ -56,7 +56,12 @@ public: ...@@ -56,7 +56,12 @@ public:
virtual int showTime() const = 0; virtual int showTime() const = 0;
virtual void configure(const QPoint &pos, QWidget *w) = 0; virtual void configure(const QPoint &pos, QWidget *w) = 0;
virtual bool canHandleContentReplacement(int typeId) const = 0; virtual bool canHandleContentReplacement(int typeId) const = 0;
virtual bool equals(int typeId, const QVariant &other) const = 0; virtual bool equals(int typeId, const QVariant &other, const QString &helpId) const = 0;
virtual void setHelpId(const QString &id);
virtual QString helpId() const;
private:
QString m_helpId;
}; };
class TextTip : public QTipLabel class TextTip : public QTipLabel
...@@ -68,7 +73,7 @@ public: ...@@ -68,7 +73,7 @@ public:
virtual void configure(const QPoint &pos, QWidget *w); virtual void configure(const QPoint &pos, QWidget *w);
virtual bool canHandleContentReplacement(int typeId) const; virtual bool canHandleContentReplacement(int typeId) const;
virtual int showTime() const; virtual int showTime() const;
virtual bool equals(int typeId, const QVariant &other) const; virtual bool equals(int typeId, const QVariant &other, const QString &otherHelpId) const;
virtual void paintEvent(QPaintEvent *event); virtual void paintEvent(QPaintEvent *event);
virtual void resizeEvent(QResizeEvent *event); virtual void resizeEvent(QResizeEvent *event);
...@@ -85,7 +90,7 @@ public: ...@@ -85,7 +90,7 @@ public:
virtual void configure(const QPoint &pos, QWidget *w); virtual void configure(const QPoint &pos, QWidget *w);
virtual bool canHandleContentReplacement(int typeId) const; virtual bool canHandleContentReplacement(int typeId) const;
virtual int showTime() const { return 4000; } virtual int showTime() const { return 4000; }
virtual bool equals(int typeId, const QVariant &other) const; virtual bool equals(int typeId, const QVariant &other, const QString &otherHelpId) const;
virtual void paintEvent(QPaintEvent *event); virtual void paintEvent(QPaintEvent *event);
private: private:
...@@ -105,7 +110,7 @@ public: ...@@ -105,7 +110,7 @@ public:
virtual void configure(const QPoint &pos, QWidget *w); virtual void configure(const QPoint &pos, QWidget *w);
virtual bool canHandleContentReplacement(int typeId) const; virtual bool canHandleContentReplacement(int typeId) const;
virtual int showTime() const { return 30000; } virtual int showTime() const { return 30000; }
virtual bool equals(int typeId, const QVariant &other) const; virtual bool equals(int typeId, const QVariant &other, const QString &otherHelpId) const;
virtual bool isInteractive() const { return true; } virtual bool isInteractive() const { return true; }
private: private:
......
...@@ -66,28 +66,28 @@ ToolTip *ToolTip::instance() ...@@ -66,28 +66,28 @@ ToolTip *ToolTip::instance()
return &tooltip; return &tooltip;
} }
void ToolTip::show(const QPoint &pos, const QString &content, QWidget *w, const QRect &rect) void ToolTip::show(const QPoint &pos, const QString &content, QWidget *w, const QString &helpId, const QRect &rect)
{ {
if (content.isEmpty()) if (content.isEmpty())
instance()->hideTipWithDelay(); instance()->hideTipWithDelay();
else else
instance()->showInternal(pos, QVariant(content), TextContent, w, rect); instance()->showInternal(pos, QVariant(content), TextContent, w, helpId, rect);
} }
void ToolTip::show(const QPoint &pos, const QColor &color, QWidget *w, const QRect &rect) void ToolTip::show(const QPoint &pos, const QColor &color, QWidget *w, const QString &helpId, const QRect &rect)
{ {
if (!color.isValid()) if (!color.isValid())
instance()->hideTipWithDelay(); instance()->hideTipWithDelay();
else else
instance()->showInternal(pos, QVariant(color), ColorContent, w, rect); instance()->showInternal(pos, QVariant(color), ColorContent, w, helpId, rect);
} }
void ToolTip::show(const QPoint &pos, QWidget *content, QWidget *w, const QRect &rect) void ToolTip::show(const QPoint &pos, QWidget *content, QWidget *w, const QString &helpId, const QRect &rect)
{ {
if (!content) if (!content)
instance()->hideTipWithDelay(); instance()->hideTipWithDelay();
else else
instance()->showInternal(pos, QVariant::fromValue(content), WidgetContent, w, rect); instance()->showInternal(pos, QVariant::fromValue(content), WidgetContent, w, helpId, rect);
} }
void ToolTip::move(const QPoint &pos, QWidget *w) void ToolTip::move(const QPoint &pos, QWidget *w)
...@@ -111,10 +111,15 @@ bool ToolTip::pinToolTip(QWidget *w, QWidget *parent) ...@@ -111,10 +111,15 @@ bool ToolTip::pinToolTip(QWidget *w, QWidget *parent)
return false; return false;
} }
QString ToolTip::contextHelpId()
{
return instance()->m_tip ? instance()->m_tip->helpId() : QString();
}
bool ToolTip::acceptShow(const QVariant &content, bool ToolTip::acceptShow(const QVariant &content,
int typeId, int typeId,
const QPoint &pos, const QPoint &pos,
QWidget *w, QWidget *w, const QString &helpId,
const QRect &rect) const QRect &rect)
{ {
if (isVisible()) { if (isVisible()) {
...@@ -123,8 +128,9 @@ bool ToolTip::acceptShow(const QVariant &content, ...@@ -123,8 +128,9 @@ bool ToolTip::acceptShow(const QVariant &content,
QPoint localPos = pos; QPoint localPos = pos;
if (w) if (w)
localPos = w->mapFromGlobal(pos); localPos = w->mapFromGlobal(pos);
if (tipChanged(localPos, content, typeId, w)) { if (tipChanged(localPos, content, typeId, w, helpId)) {
m_tip->setContent(content); m_tip->setContent(content);
m_tip->setHelpId(helpId);
setUp(pos, w, rect); setUp(pos, w, rect);
} }
return false; return false;
...@@ -155,9 +161,10 @@ void ToolTip::setUp(const QPoint &pos, QWidget *w, const QRect &rect) ...@@ -155,9 +161,10 @@ void ToolTip::setUp(const QPoint &pos, QWidget *w, const QRect &rect)
m_showTimer.start(m_tip->showTime()); m_showTimer.start(m_tip->showTime());
} }
bool ToolTip::tipChanged(const QPoint &pos, const QVariant &content, int typeId, QWidget *w) const bool ToolTip::tipChanged(const QPoint &pos, const QVariant &content, int typeId, QWidget *w,
const QString &helpId) const
{ {
if (!m_tip->equals(typeId, content) || m_widget != w) if (!m_tip->equals(typeId, content, helpId) || m_widget != w)
return true; return true;
if (!m_rect.isNull()) if (!m_rect.isNull())
return !m_rect.contains(pos); return !m_rect.contains(pos);
...@@ -220,12 +227,13 @@ void ToolTip::hideTipImmediately() ...@@ -220,12 +227,13 @@ void ToolTip::hideTipImmediately()
m_showTimer.stop(); m_showTimer.stop();
m_hideDelayTimer.stop(); m_hideDelayTimer.stop();
qApp->removeEventFilter(this); qApp->removeEventFilter(this);
emit hidden();
} }
void ToolTip::showInternal(const QPoint &pos, const QVariant &content, void ToolTip::showInternal(const QPoint &pos, const QVariant &content,
int typeId, QWidget *w, const QRect &rect) int typeId, QWidget *w, const QString &helpId, const QRect &rect)
{ {
if (acceptShow(content, typeId, pos, w, rect)) { if (acceptShow(content, typeId, pos, w, helpId, rect)) {
QWidget *target = 0; QWidget *target = 0;
if (HostOsInfo::isWindowsHost()) if (HostOsInfo::isWindowsHost())
target = QApplication::desktop()->screen(Internal::screenNumber(pos, w)); target = QApplication::desktop()->screen(Internal::screenNumber(pos, w));
...@@ -244,10 +252,12 @@ void ToolTip::showInternal(const QPoint &pos, const QVariant &content, ...@@ -244,10 +252,12 @@ void ToolTip::showInternal(const QPoint &pos, const QVariant &content,
break; break;
} }
m_tip->setContent(content); m_tip->setContent(content);
m_tip->setHelpId(helpId);
setUp(pos, w, rect); setUp(pos, w, rect);
qApp->installEventFilter(this); qApp->installEventFilter(this);
showTip(); showTip();
} }
emit shown();
} }
void ToolTip::placeTip(const QPoint &pos, QWidget *w) void ToolTip::placeTip(const QPoint &pos, QWidget *w)
......
...@@ -75,9 +75,12 @@ public: ...@@ -75,9 +75,12 @@ public:
static ToolTip *instance(); static ToolTip *instance();
static void show(const QPoint &pos, const QString &content, QWidget *w = 0, const QRect &rect = QRect()); static void show(const QPoint &pos, const QString &content, QWidget *w = 0,
static void show(const QPoint &pos, const QColor &color, QWidget *w = 0, const QRect &rect = QRect()); const QString &helpId = QString(), const QRect &rect = QRect());
static void show(const QPoint &pos, QWidget *content, QWidget *w = 0, const QRect &rect = QRect()); static void show(const QPoint &pos, const QColor &color, QWidget *w = 0,
const QString &helpId = QString(), const QRect &rect = QRect());
static void show(const QPoint &pos, QWidget *content, QWidget *w = 0,
const QString &helpId = QString(), const QRect &rect = QRect());
static void move(const QPoint &pos, QWidget *w); static void move(const QPoint &pos, QWidget *w);
static void hide(); static void hide();
static bool isVisible(); static bool isVisible();
...@@ -88,12 +91,21 @@ public: ...@@ -88,12 +91,21 @@ public:
// using WidgetContent // using WidgetContent
static bool pinToolTip(QWidget *w, QWidget *parent); static bool pinToolTip(QWidget *w, QWidget *parent);
static QString contextHelpId();
signals:
void shown();
void hidden();
private: private:
void showInternal(const QPoint &pos, const QVariant &content, int typeId, QWidget *w, const QRect &rect); void showInternal(const QPoint &pos, const QVariant &content, int typeId, QWidget *w,
const QString &helpId, const QRect &rect);
void hideTipImmediately(); void hideTipImmediately();
bool acceptShow(const QVariant &content, int typeId, const QPoint &pos, QWidget *w, const QRect &rect); bool acceptShow(const QVariant &content, int typeId, const QPoint &pos, QWidget *w,
const QString &helpId, const QRect &rect);
void setUp(const QPoint &pos, QWidget *w, const QRect &rect); void setUp(const QPoint &pos, QWidget *w, const QRect &rect);
bool tipChanged(const QPoint &pos, const QVariant &content, int typeId, QWidget *w) const; bool tipChanged(const QPoint &pos, const QVariant &content, int typeId, QWidget *w,
const QString &helpId) const;
void setTipRect(QWidget *w, const QRect &rect); void setTipRect(QWidget *w, const QRect &rect);
void placeTip(const QPoint &pos, QWidget *w); void placeTip(const QPoint &pos, QWidget *w);
void showTip(); void showTip();
...@@ -104,6 +116,7 @@ private: ...@@ -104,6 +116,7 @@ private:
QRect m_rect; QRect m_rect;
QTimer m_showTimer; QTimer m_showTimer;
QTimer m_hideDelayTimer; QTimer m_hideDelayTimer;
QString m_helpId;
}; };
} // namespace Utils } // namespace Utils
......
...@@ -14,5 +14,6 @@ ...@@ -14,5 +14,6 @@
<file>images/progressindicator_small.png</file> <file>images/progressindicator_small.png</file>
<file>images/progressindicator_small@2x.png</file> <file>images/progressindicator_small@2x.png</file>
<file>images/triangle_vert.png</file> <file>images/triangle_vert.png</file>
<file>tooltip/images/f1.png</file>
</qresource> </qresource>
</RCC> </RCC>
...@@ -528,19 +528,20 @@ void ICore::raiseWindow(QWidget *widget) ...@@ -528,19 +528,20 @@ void ICore::raiseWindow(QWidget *widget)
} }
} }
void ICore::updateAdditionalContexts(const Context &remove, const Context &add) void ICore::updateAdditionalContexts(const Context &remove, const Context &add,
ContextPriority priority)
{ {
m_mainwindow->updateAdditionalContexts(remove, add); m_mainwindow->updateAdditionalContexts(remove, add, priority);
} }
void ICore::addAdditionalContext(const Context &context) void ICore::addAdditionalContext(const Context &context, ContextPriority priority)
{ {
m_mainwindow->updateAdditionalContexts(Context(), context); m_mainwindow->updateAdditionalContexts(Context(), context, priority);
} }
void ICore::removeAdditionalContext(const Context &context) void ICore::removeAdditionalContext(const Context &context)
{ {
m_mainwindow->updateAdditionalContexts(context, Context()); m_mainwindow->updateAdditionalContexts(context, Context(), ContextPriority::Low);
} }
void ICore::addContextObject(IContext *context) void ICore::addContextObject(IContext *context)
......
...@@ -65,6 +65,11 @@ class CORE_EXPORT ICore : public QObject ...@@ -65,6 +65,11 @@ class CORE_EXPORT ICore : public QObject
~ICore(); ~ICore();
public: public:
enum class ContextPriority {
High,
Low
};
// This should only be used to acccess the signals, so it could // This should only be used to acccess the signals, so it could
// theoretically return an QObject *. For source compatibility // theoretically return an QObject *. For source compatibility
// it returns a ICore. // it returns a ICore.
...@@ -107,8 +112,10 @@ public: ...@@ -107,8 +112,10 @@ public:
static IContext *currentContextObject(); static IContext *currentContextObject();
// Adds and removes additional active contexts, these contexts are appended // Adds and removes additional active contexts, these contexts are appended
// to the currently active contexts. // to the currently active contexts.
static void updateAdditionalContexts(const Context &remove, const Context &add); static void updateAdditionalContexts(const Context &remove, const Context &add,
static void addAdditionalContext(const Context &context); ContextPriority priority = ContextPriority::Low);
static void addAdditionalContext(const Context &context,
ContextPriority priority = ContextPriority::Low);
static void removeAdditionalContext(const Context &context); static void removeAdditionalContext(const Context &context);
static void addContextObject(IContext *context); static void addContextObject(IContext *context);
static void removeContextObject(IContext *context); static void removeContextObject(IContext *context);
......
...@@ -112,7 +112,7 @@ enum { debugMainWindow = 0 }; ...@@ -112,7 +112,7 @@ enum { debugMainWindow = 0 };
MainWindow::MainWindow() : MainWindow::MainWindow() :
AppMainWindow(), AppMainWindow(),
m_coreImpl(new ICore(this)), m_coreImpl(new ICore(this)),
m_additionalContexts(Constants::C_GLOBAL), m_lowPrioAdditionalContexts(Constants::C_GLOBAL),
m_settingsDatabase(new SettingsDatabase(QFileInfo(PluginManager::settings()->fileName()).path(), m_settingsDatabase(new SettingsDatabase(QFileInfo(PluginManager::settings()->fileName()).path(),
QLatin1String("QtCreator"), QLatin1String("QtCreator"),
this)), this)),
...@@ -985,23 +985,27 @@ void MainWindow::writeSettings() ...@@ -985,23 +985,27 @@ void MainWindow::writeSettings()
m_navigationWidget->saveSettings(settings); m_navigationWidget->saveSettings(settings);
} }
void MainWindow::updateAdditionalContexts(const Context &remove, const Context &add) void MainWindow::updateAdditionalContexts(const Context &remove, const Context &add,
ICore::ContextPriority priority)
{ {
foreach (const Id id, remove) { foreach (const Id id, remove) {
if (!id.isValid()) if (!id.isValid())
continue; continue;
int index = m_lowPrioAdditionalContexts.indexOf(id);
int index = m_additionalContexts.indexOf(id); if (index != -1)
m_lowPrioAdditionalContexts.removeAt(index);
index = m_highPrioAdditionalContexts.indexOf(id);
if (index != -1) if (index != -1)
m_additionalContexts.removeAt(index); m_highPrioAdditionalContexts.removeAt(index);
} }
foreach (const Id id, add) { foreach (const Id id, add) {
if (!id.isValid()) if (!id.isValid())
continue; continue;
Context &cref = (priority == ICore::ContextPriority::High ? m_highPrioAdditionalContexts
if (!m_additionalContexts.contains(id)) : m_lowPrioAdditionalContexts);
m_additionalContexts.prepend(id); if (!cref.contains(id))
cref.prepend(id);
} }
updateContext(); updateContext();
...@@ -1009,12 +1013,12 @@ void MainWindow::updateAdditionalContexts(const Context &remove, const Context & ...@@ -1009,12 +1013,12 @@ void MainWindow::updateAdditionalContexts(const Context &remove, const Context &
void MainWindow::updateContext() void MainWindow::updateContext()
{ {
Context contexts; Context contexts = m_highPrioAdditionalContexts;
foreach (IContext *context, m_activeContext) foreach (IContext *context, m_activeContext)
contexts.add(context->context()); contexts.add(context->context());
contexts.add(m_additionalContexts); contexts.add(m_lowPrioAdditionalContexts);
Context uniquecontexts; Context uniquecontexts;
for (int i = 0; i < contexts.size(); ++i) { for (int i = 0; i < contexts.size(); ++i) {
......
...@@ -103,7 +103,8 @@ public: ...@@ -103,7 +103,8 @@ public:
IContext * currentContextObject() const; IContext * currentContextObject() const;
QStatusBar *statusBar() const; QStatusBar *statusBar() const;
void updateAdditionalContexts(const Context &remove, const Context &add); void updateAdditionalContexts(const Context &remove, const Context &add,
ICore::ContextPriority priority);
void setSuppressNavigationWidget(bool suppress); void setSuppressNavigationWidget(bool suppress);
...@@ -151,7 +152,8 @@ private: ...@@ -151,7 +152,8 @@ private:
void writeSettings(); void writeSettings();
ICore *m_coreImpl; ICore *m_coreImpl;
Context m_additionalContexts; Context m_highPrioAdditionalContexts;
Context m_lowPrioAdditionalContexts;
SettingsDatabase *m_settingsDatabase; SettingsDatabase *m_settingsDatabase;
mutable QPrinter *m_printer; mutable QPrinter *m_printer;
WindowSupport *m_windowSupport; WindowSupport *m_windowSupport;
......
...@@ -119,7 +119,6 @@ void CppHoverHandler::decorateToolTip() ...@@ -119,7 +119,6 @@ void CppHoverHandler::decorateToolTip()
prefix = QLatin1String("enum "); prefix = QLatin1String("enum ");
setToolTip(prefix + help.helpId()); setToolTip(prefix + help.helpId());
} }
addF1ToToolTip();
} }
} }
......
...@@ -78,6 +78,7 @@ ...@@ -78,6 +78,7 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/styledbar.h> #include <utils/styledbar.h>
#include <utils/theme/theme.h>