]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltipmanager.cpp
;--
[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
25 #include <kformattedballoontipdelegate.h>
26 #include <kicon.h>
27 #include <ktooltip.h>
28
29 #include <QTimer>
30
31 K_GLOBAL_STATIC(KFormattedBalloonTipDelegate, g_delegate)
32
33 ToolTipManager::ToolTipManager(QAbstractItemView* parent,
34 DolphinSortFilterProxyModel* model) :
35 QObject(parent),
36 m_view(parent),
37 m_dolphinModel(0),
38 m_proxyModel(model),
39 m_timer(0),
40 m_item(),
41 m_pos()
42 {
43 KToolTip::setToolTipDelegate(g_delegate);
44
45 m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
46 connect(parent, SIGNAL(entered(const QModelIndex&)),
47 this, SLOT(requestToolTip(const QModelIndex&)));
48 connect(parent, SIGNAL(viewportEntered()),
49 this, SLOT(hideToolTip()));
50
51 m_timer = new QTimer(this);
52 m_timer->setSingleShot(true);
53 connect(m_timer, SIGNAL(timeout()),
54 this, SLOT(showToolTip()));
55
56 m_view->viewport()->installEventFilter(this);
57 }
58
59 ToolTipManager::~ToolTipManager()
60 {
61 }
62
63 void ToolTipManager::hideTip()
64 {
65 hideToolTip();
66 }
67
68 bool ToolTipManager::eventFilter(QObject* watched, QEvent* event)
69 {
70 if ((watched == m_view->viewport()) && (event->type() == QEvent::Leave)) {
71 hideToolTip();
72 }
73
74 return QObject::eventFilter(watched, event);
75 }
76
77 void ToolTipManager::requestToolTip(const QModelIndex& index)
78 {
79 KToolTip::hideTip();
80
81 const QRect rect = m_view->visualRect(index);
82 m_pos = m_view->viewport()->mapToGlobal(rect.bottomRight());
83
84 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
85 m_item = m_dolphinModel->itemForIndex(dirIndex);
86
87 m_timer->start(500);
88 }
89
90 void ToolTipManager::hideToolTip()
91 {
92 m_timer->stop();
93 KToolTip::hideTip();
94 }
95
96 void ToolTipManager::showToolTip()
97 {
98 KToolTipItem* tip = new KToolTipItem(KIcon(m_item.iconName()), m_item.getToolTipText());
99 KToolTip::showTip(m_pos, tip);
100 }
101
102 #include "tooltipmanager.moc"