1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "tooltip.h"
#include "tips.h"
#include "tipcontents.h"
#include "effects.h"
#include "reuse.h"
#include <utils/hostosinfo.h>
#include <QString>
#include <QColor>
#include <QApplication>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QWidget>
#include <QMenu>
#include <QDebug>
using namespace Utils;
using namespace Internal;
ToolTip::ToolTip() : m_tip(0), m_widget(0)
{
connect(&m_showTimer, SIGNAL(timeout()), this, SLOT(hideTipImmediately()));
connect(&m_hideDelayTimer, SIGNAL(timeout()), this, SLOT(hideTipImmediately()));
}
ToolTip::~ToolTip()
{
m_tip = 0;
}
ToolTip *ToolTip::instance()
{
static ToolTip tooltip;
return &tooltip;
}
void ToolTip::show(const QPoint &pos, const TipContent &content, QWidget *w, const QRect &rect)
{
instance()->showInternal(pos, content, w, rect);
}
void ToolTip::show(const QPoint &pos, const TipContent &content, QWidget *w)
{
show(pos, content, w, QRect());
}
bool ToolTip::acceptShow(const TipContent &content,
const QPoint &pos,
QWidget *w,
const QRect &rect)
{
if (!validateContent(content))
return false;
if (isVisible()) {
if (m_tip->canHandleContentReplacement(content)) {
// Reuse current tip.
QPoint localPos = pos;
if (w)
localPos = w->mapFromGlobal(pos);
if (tipChanged(localPos, content, w))
setUp(pos, content, w, rect);
return false;
}
hideTipImmediately();
}
#if !defined(QT_NO_EFFECTS) && !defined(Q_OS_MAC)
// While the effect takes places it might be that although the widget is actually on
// screen the isVisible function doesn't return true.
else if (m_tip
&& (QApplication::isEffectEnabled(Qt::UI_FadeTooltip)
|| QApplication::isEffectEnabled(Qt::UI_AnimateTooltip))) {
hideTipImmediately();
}
#endif
return true;
}
bool ToolTip::validateContent(const TipContent &content)
{
if (!content.isValid()) {
if (isVisible())
hideTipWithDelay();
return false;
}
return true;
}
void ToolTip::setUp(const QPoint &pos, const TipContent &content, QWidget *w, const QRect &rect)
{
m_tip->setContent(content);
m_tip->configure(pos, w);
placeTip(pos, w);
setTipRect(w, rect);
if (m_hideDelayTimer.isActive())
m_hideDelayTimer.stop();
m_showTimer.start(content.showTime());
}
bool ToolTip::tipChanged(const QPoint &pos, const TipContent &content, QWidget *w) const
{
if (!m_tip->content().equals(content) || m_widget != w)
return true;
if (!m_rect.isNull())
return !m_rect.contains(pos);
return false;
}
void ToolTip::setTipRect(QWidget *w, const QRect &rect)
{
if (!m_rect.isNull() && !w) {
qWarning("ToolTip::show: Cannot pass null widget if rect is set");
} else {
m_widget = w;
m_rect = rect;
}
}
bool ToolTip::isVisible()
{
ToolTip *t = instance();
return t->m_tip && t->m_tip->isVisible();
}
void ToolTip::showTip()
{
#if !defined(QT_NO_EFFECTS) && !defined(Q_OS_MAC)
if (QApplication::isEffectEnabled(Qt::UI_FadeTooltip))
qFadeEffect(m_tip);
else if (QApplication::isEffectEnabled(Qt::UI_AnimateTooltip))
qScrollEffect(m_tip);
else
m_tip->show();
#else
m_tip->show();
#endif
}
void ToolTip::hide()
{
instance()->hideTipWithDelay();
}
void ToolTip::hideTipWithDelay()
{
if (!m_hideDelayTimer.isActive())
m_hideDelayTimer.start(300);
}
void ToolTip::hideTipImmediately()
{
if (m_tip) {
m_tip->close();
m_tip->deleteLater();
m_tip = 0;
}
m_showTimer.stop();
m_hideDelayTimer.stop();
qApp->removeEventFilter(this);
}
void ToolTip::showInternal(const QPoint &pos, const TipContent &content, QWidget *w, const QRect &rect)
{
if (acceptShow(content, pos, w, rect)) {
QWidget *target = 0;
if (HostOsInfo::isWindowsHost())
target = QApplication::desktop()->screen(Internal::screenNumber(pos, w));
else
target = w;
switch (content.typeId()) {
case TextContent::TEXT_CONTENT_ID:
m_tip = new TextTip(target);
break;
case ColorContent::COLOR_CONTENT_ID:
m_tip = new ColorTip(target);
break;
case WidgetContent::WIDGET_CONTENT_ID:
m_tip = new WidgetTip(target);
break;
}
setUp(pos, content, w, rect);
qApp->installEventFilter(this);
showTip();
}
}
void ToolTip::placeTip(const QPoint &pos, QWidget *w)
{
QRect screen = Internal::screenGeometry(pos, w);
QPoint p = pos;
p += QPoint(2, Utils::HostOsInfo::isWindowsHost() ? 21 : 16);
if (p.x() + m_tip->width() > screen.x() + screen.width())
p.rx() -= 4 + m_tip->width();
if (p.y() + m_tip->height() > screen.y() + screen.height())
p.ry() -= 24 + m_tip->height();
if (p.y() < screen.y())
p.setY(screen.y());
if (p.x() + m_tip->width() > screen.x() + screen.width())
p.setX(screen.x() + screen.width() - m_tip->width());
if (p.x() < screen.x())
p.setX(screen.x());
if (p.y() + m_tip->height() > screen.y() + screen.height())
p.setY(screen.y() + screen.height() - m_tip->height());
m_tip->move(p);
}
bool ToolTip::eventFilter(QObject *o, QEvent *event)
{
if (!o->isWidgetType())
return false;
switch (event->type()) {
#ifdef Q_OS_MAC
case QEvent::KeyPress:
case QEvent::KeyRelease: {
int key = static_cast<QKeyEvent *>(event)->key();
Qt::KeyboardModifiers mody = static_cast<QKeyEvent *>(event)->modifiers();
if (!(mody & Qt::KeyboardModifierMask)
&& key != Qt::Key_Shift && key != Qt::Key_Control
&& key != Qt::Key_Alt && key != Qt::Key_Meta)
hideTipWithDelay();
break;
}
#endif
case QEvent::Leave:
if (o == m_tip && !m_tip->isAncestorOf(qApp->focusWidget()))
hideTipWithDelay();
break;
case QEvent::Enter:
// User moved cursor into tip and wants to interact.
if (m_tip && m_tip->isInteractive() && o == m_tip) {
if (m_hideDelayTimer.isActive())
m_hideDelayTimer.stop();
}
break;
case QEvent::WindowActivate:
case QEvent::WindowDeactivate:
case QEvent::FocusOut:
case QEvent::FocusIn:
if (m_tip && !m_tip->isInteractive()) // Windows: A sequence of those occurs when interacting
hideTipImmediately();
break;
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
case QEvent::MouseButtonDblClick:
case QEvent::Wheel:
if (m_tip) {
if (m_tip->isInteractive()) { // Do not close on interaction with the tooltip
if (o != m_tip && !m_tip->isAncestorOf(static_cast<QWidget *>(o)))
hideTipImmediately();
} else {
hideTipImmediately();
}
}
break;
case QEvent::MouseMove:
if (o == m_widget &&
!m_rect.isNull() &&
!m_rect.contains(static_cast<QMouseEvent*>(event)->pos())) {
hideTipWithDelay();
}
default:
break;
}
return false;
}
|