]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/ToolTipFormatter.cpp
Make the InsetInfo dialog a bit less esoteric.
[lyx.git] / src / frontends / qt4 / ToolTipFormatter.cpp
1 /**
2  * \file ToolTipFormatter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Guillaume Munch
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ToolTipFormatter.h"
14 #include "qt_helpers.h"
15
16 #include <QAbstractItemView>
17 #include <QTextDocument>
18 #include <QTextLayout>
19 #include <QToolTip>
20
21
22 //#include "support/debug.h"
23 //#include "support/lstrings.h"
24
25
26 namespace lyx {
27 namespace frontend {
28
29
30 ToolTipFormatter::ToolTipFormatter(QObject * parent) : QObject(parent) {}
31
32
33 bool ToolTipFormatter::eventFilter(QObject * o, QEvent * e)
34 {
35         if (e->type() != QEvent::ToolTip)
36                 return false;
37
38         // Format the tooltip of the widget being considered.
39         QWidget * w = qobject_cast<QWidget *>(o);
40         if (!w)
41                 return false;
42         // Unchanged if empty or already formatted
43         w->setToolTip(formatToolTip(w->toolTip()));
44
45         // Now, if the tooltip is for an item in a QListView or a QTreeView,
46         // then the widget above was probably not the one with the tooltip.
47         // Check if the parent is a QAbstractItemView.
48         QAbstractItemView * iv = qobject_cast<QAbstractItemView *>(w->parent());
49         if (!iv)
50                 return false;
51         // In this case, the item is retrieved from the position of the QHelpEvent
52         // on the screen.
53         QPoint pos = static_cast<QHelpEvent *>(e)->pos();
54         QModelIndex item = iv->indexAt(pos);
55         if (!item.isValid())
56                 return false;
57         QVariant data = iv->model()->data(item, Qt::ToolTipRole);
58         if (data.isValid() && data.typeName() == toqstr("QString"))
59                 // Unchanged if empty or already formatted
60                 iv->model()->setData(item, formatToolTip(data.toString()),
61                                      Qt::ToolTipRole);
62         // We must let the tooltip event reach its destination.
63         return false;
64 }
65
66
67 } // namespace frontend
68 } // namespace lyx
69
70 #include "moc_ToolTipFormatter.cpp"