]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltipmanager.cpp
Provide tooltips. Per default tooltips are turned off because the information sidebar...
[dolphin.git] / src / tooltipmanager.cpp
1 /*******************************************************************************
2 * Copyright (C) 2008 by Konstantin Heil <konst.heil@stud.uni-heidelberg.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, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 *******************************************************************************/
19
20 #include "tooltipmanager.h"
21
22 #include "dolphinmodel.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "ktooltip.h"
25 #include "kicon.h"
26
27 #include <QTimer>
28
29 ToolTipManager::ToolTipManager(QAbstractItemView* parent,
30 DolphinSortFilterProxyModel* model) :
31 QObject(parent),
32 m_view(parent),
33 m_dolphinModel(0),
34 m_proxyModel(model),
35 m_timer(0),
36 m_item(),
37 m_pos(),
38 m_delegate()
39 {
40 KToolTip::setToolTipDelegate(&m_delegate);
41
42 m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
43 connect(parent, SIGNAL(entered(const QModelIndex&)),
44 this, SLOT(requestToolTip(const QModelIndex&)));
45 connect(parent, SIGNAL(viewportEntered()),
46 this, SLOT(hideToolTip()));
47
48 m_timer = new QTimer(this);
49 m_timer->setSingleShot(true);
50 connect(m_timer, SIGNAL(timeout()),
51 this, SLOT(showToolTip()));
52
53 m_view->viewport()->installEventFilter(this);
54 }
55
56 ToolTipManager::~ToolTipManager()
57 {
58 }
59
60 bool ToolTipManager::eventFilter(QObject* watched, QEvent* event)
61 {
62 if ((watched == m_view->viewport()) && (event->type() == QEvent::Leave)) {
63 hideToolTip();
64 }
65
66 return QObject::eventFilter(watched, event);
67 }
68
69 void ToolTipManager::requestToolTip(const QModelIndex& index)
70 {
71 KToolTip::hideTip();
72
73 const QRect rect = m_view->visualRect(index);
74 m_pos = m_view->viewport()->mapToGlobal(rect.bottomRight());
75
76 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
77 m_item = m_dolphinModel->itemForIndex(dirIndex);
78
79 m_timer->start(500);
80 }
81
82 void ToolTipManager::hideToolTip()
83 {
84 m_timer->stop();
85 KToolTip::hideTip();
86 }
87
88 void ToolTipManager::showToolTip()
89 {
90 KToolTipItem* tip = new KToolTipItem(KIcon(m_item.iconName()), m_item.getToolTipText());
91 KToolTip::showTip(m_pos, tip);
92 }
93
94 #include "tooltipmanager.moc"