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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
/* This file is part of the KDE project
Copyright 2007 Vladimir Prus
Copyright 2009-2010 David Nolden
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "activetooltip.h"
#include "debug.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QEvent>
#include <QMenu>
#include <QMouseEvent>
#include <QPalette>
#include <QPoint>
#include <QPointer>
#include <QScreen>
#include <QStyleOption>
#include <QStylePainter>
#include <limits>
using namespace KDevelop;
namespace {
class ActiveToolTipManager : public QObject
{
Q_OBJECT
public Q_SLOTS:
void doVisibility();
public:
using ToolTipPriorityMap = QMultiMap<float, QPair<QPointer<ActiveToolTip>, QString>>;
ToolTipPriorityMap registeredToolTips;
};
ActiveToolTipManager* manager()
{
static ActiveToolTipManager m;
return &m;
}
QWidget* masterWidget(QWidget* w)
{
while (w && w->parent() && qobject_cast<QWidget*>(w->parent())) {
w = qobject_cast<QWidget*>(w->parent());
}
return w;
}
void ActiveToolTipManager::doVisibility()
{
bool exclusive = false;
int lastBottomPosition = -1;
int lastLeftPosition = -1;
QRect fullGeometry; //Geometry of all visible tooltips together
for (auto it = registeredToolTips.constBegin(); it != registeredToolTips.constEnd(); ++it) {
QPointer<ActiveToolTip> w = (*it).first;
if (w) {
if (exclusive) {
(w.data())->hide();
} else {
QRect geom = (w.data())->geometry();
if ((w.data())->geometry().top() < lastBottomPosition) {
geom.moveTop(lastBottomPosition);
}
if (lastLeftPosition != -1) {
geom.moveLeft(lastLeftPosition);
}
(w.data())->setGeometry(geom);
lastBottomPosition = (w.data())->geometry().bottom();
lastLeftPosition = (w.data())->geometry().left();
if (it == registeredToolTips.constBegin()) {
fullGeometry = (w.data())->geometry();
} else {
fullGeometry = fullGeometry.united((w.data())->geometry());
}
}
if (it.key() == 0) {
exclusive = true;
}
}
}
if (!fullGeometry.isEmpty()) {
QRect oldFullGeometry = fullGeometry;
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
const auto *screen = QGuiApplication::screenAt(fullGeometry.topLeft());
if (!screen) {
screen = qApp->primaryScreen();
qWarning() << "failed to find screen:" << fullGeometry << "fallback primary geometry:" << screen->geometry();
}
QRect screenGeometry = screen->geometry();
#else
QRect screenGeometry = QApplication::desktop()->screenGeometry(fullGeometry.topLeft());
#endif
if (fullGeometry.bottom() > screenGeometry.bottom()) {
//Move up, avoiding the mouse-cursor
fullGeometry.moveBottom(fullGeometry.top() - 10);
if (fullGeometry.adjusted(-20, -20, 20, 20).contains(QCursor::pos())) {
fullGeometry.moveBottom(QCursor::pos().y() - 20);
}
}
if (fullGeometry.right() > screenGeometry.right()) {
//Move to left, avoiding the mouse-cursor
fullGeometry.moveRight(fullGeometry.left() - 10);
if (fullGeometry.adjusted(-20, -20, 20, 20).contains(QCursor::pos())) {
fullGeometry.moveRight(QCursor::pos().x() - 20);
}
}
// Now fit this to screen
if (fullGeometry.left() < 0) {
fullGeometry.setLeft(0);
}
if (fullGeometry.top() < 0) {
fullGeometry.setTop(0);
}
QPoint offset = fullGeometry.topLeft() - oldFullGeometry.topLeft();
if (!offset.isNull()) {
for (auto& toolTipData : qAsConst(registeredToolTips)) {
auto& toolTip = toolTipData.first;
if (toolTip) {
toolTip->move(toolTip->pos() + offset);
}
}
}
}
//Always include the mouse cursor in the full geometry, to avoid
//closing the tooltip unexpectedly
fullGeometry = fullGeometry.united(QRect(QCursor::pos(), QCursor::pos()));
//Set bounding geometry, and remove old tooltips
for (auto it = registeredToolTips.begin(); it != registeredToolTips.end();) {
if (!it->first) {
it = registeredToolTips.erase(it);
} else {
it->first.data()->setBoundingGeometry(fullGeometry);
++it;
}
}
//Final step: Show tooltips
for (const auto& tooltip : qAsConst(registeredToolTips)) {
if (tooltip.first.data() && masterWidget(tooltip.first.data())->isActiveWindow()) {
tooltip.first.data()->show();
}
if (exclusive) {
break;
}
}
}
}
namespace KDevelop {
class ActiveToolTipPrivate
{
public:
QRect rect_;
QRect handleRect_;
QVector<QPointer<QObject>> friendWidgets_;
};
}
ActiveToolTip::ActiveToolTip(QWidget* parent, const QPoint& position)
: QWidget(parent, Qt::ToolTip)
, d_ptr(new ActiveToolTipPrivate)
{
Q_D(ActiveToolTip);
Q_ASSERT(parent);
setMouseTracking(true);
d->rect_ = QRect(position, position);
d->rect_.adjust(-10, -10, 10, 10);
move(position);
QPalette p;
// adjust background color to use tooltip colors
p.setColor(backgroundRole(), p.color(QPalette::ToolTipBase));
p.setColor(QPalette::Base, p.color(QPalette::ToolTipBase));
// adjust foreground color to use tooltip colors
p.setColor(foregroundRole(), p.color(QPalette::ToolTipText));
p.setColor(QPalette::Text, p.color(QPalette::ToolTipText));
setPalette(p);
setWindowFlags(Qt::WindowDoesNotAcceptFocus | windowFlags());
qApp->installEventFilter(this);
}
ActiveToolTip::~ActiveToolTip() = default;
bool ActiveToolTip::eventFilter(QObject* object, QEvent* e)
{
Q_D(ActiveToolTip);
switch (e->type()) {
case QEvent::MouseMove:
if (underMouse() || insideThis(object)) {
return false;
} else {
QPoint globalPos = static_cast<QMouseEvent*>(e)->globalPos();
QRect mergedRegion = d->rect_.united(d->handleRect_);
if (mergedRegion.contains(globalPos)) {
return false;
}
close();
}
break;
case QEvent::WindowActivate:
if (insideThis(object)) {
return false;
}
close();
break;
case QEvent::WindowBlocked:
// Modal dialog activated somewhere, it is the only case where a cursor
// move may be missed and the popup has to be force-closed
close();
break;
default:
break;
}
return false;
}
void ActiveToolTip::addFriendWidget(QWidget* widget)
{
Q_D(ActiveToolTip);
d->friendWidgets_.append(( QObject* )widget);
}
bool ActiveToolTip::insideThis(QObject* object)
{
Q_D(ActiveToolTip);
while (object) {
if (qobject_cast<QMenu*>(object)) {
return true;
}
if (object == this || object == ( QObject* )this->windowHandle() || d->friendWidgets_.contains(object)) {
return true;
}
object = object->parent();
}
// If the object clicked is inside a QQuickWidget, its parent is null even
// if it is part of a tool-tip. This check ensures that a tool-tip is never
// closed while the mouse is in it
return underMouse();
}
void ActiveToolTip::showEvent(QShowEvent*)
{
adjustRect();
}
void ActiveToolTip::resizeEvent(QResizeEvent*)
{
adjustRect();
// set mask from style
QStyleOptionFrame opt;
opt.init(this);
QStyleHintReturnMask mask;
if (style()->styleHint(QStyle::SH_ToolTip_Mask, &opt, this, &mask) && !mask.region.isEmpty()) {
setMask(mask.region);
}
emit resized();
}
void ActiveToolTip::paintEvent(QPaintEvent* event)
{
QStylePainter painter(this);
painter.setClipRegion(event->region());
QStyleOptionFrame opt;
opt.init(this);
painter.drawPrimitive(QStyle::PE_PanelTipLabel, opt);
}
void ActiveToolTip::setHandleRect(const QRect& rect)
{
Q_D(ActiveToolTip);
d->handleRect_ = rect;
}
void ActiveToolTip::adjustRect()
{
Q_D(ActiveToolTip);
// For tooltip widget, geometry() returns global coordinates.
QRect r = geometry();
r.adjust(-10, -10, 10, 10);
d->rect_ = r;
}
void ActiveToolTip::setBoundingGeometry(const QRect& geometry)
{
Q_D(ActiveToolTip);
d->rect_ = geometry;
d->rect_.adjust(-10, -10, 10, 10);
}
void ActiveToolTip::showToolTip(ActiveToolTip* tooltip, float priority, const QString& uniqueId)
{
auto& registeredToolTips = manager()->registeredToolTips;
if (!uniqueId.isEmpty()) {
for (const auto& tooltip : qAsConst(registeredToolTips)) {
if (tooltip.second == uniqueId) {
delete tooltip.first.data();
}
}
}
registeredToolTips.insert(priority, qMakePair(QPointer<ActiveToolTip>(tooltip), uniqueId));
connect(tooltip, &ActiveToolTip::resized,
manager(), &ActiveToolTipManager::doVisibility);
QMetaObject::invokeMethod(manager(), "doVisibility", Qt::QueuedConnection);
}
void ActiveToolTip::closeEvent(QCloseEvent* event)
{
QWidget::closeEvent(event);
deleteLater();
}
#include "activetooltip.moc"
|