1 /*
2 * Copyright (C) by Christian Kamm <mail@ckamm.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "tooltipupdater.h"
16
17 #include <QTreeView>
18 #include <QHelpEvent>
19 #include <QToolTip>
20
21 using namespace OCC;
22
ToolTipUpdater(QTreeView * treeView)23 ToolTipUpdater::ToolTipUpdater(QTreeView *treeView)
24 : QObject(treeView)
25 , _treeView(treeView)
26 {
27 connect(_treeView->model(), &QAbstractItemModel::dataChanged,
28 this, &ToolTipUpdater::dataChanged);
29 _treeView->viewport()->installEventFilter(this);
30 }
31
eventFilter(QObject *,QEvent * ev)32 bool ToolTipUpdater::eventFilter(QObject * /*obj*/, QEvent *ev)
33 {
34 if (ev->type() == QEvent::ToolTip) {
35 QHelpEvent *helpEvent = static_cast<QHelpEvent *>(ev);
36 _toolTipPos = helpEvent->globalPos();
37 }
38 return false;
39 }
40
dataChanged(const QModelIndex & topLeft,const QModelIndex & bottomRight,const QVector<int> & roles)41 void ToolTipUpdater::dataChanged(const QModelIndex &topLeft,
42 const QModelIndex &bottomRight,
43 const QVector<int> &roles)
44 {
45 if (!QToolTip::isVisible() || !roles.contains(Qt::ToolTipRole) || _toolTipPos.isNull()) {
46 return;
47 }
48
49 // Was it the item under the cursor that changed?
50 auto index = _treeView->indexAt(_treeView->mapFromGlobal(QCursor::pos()));
51 if (topLeft == bottomRight && index != topLeft) {
52 return;
53 }
54
55 // Update the currently active tooltip
56 QToolTip::showText(_toolTipPos, _treeView->model()->data(index, Qt::ToolTipRole).toString());
57 }
58