]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltipmanager.cpp
Provide functionality for auto-expanding folders (the whole patch has been provided...
[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 <QApplication>
30 #include <QDesktopWidget>
31 #include <QTimer>
32 #include <QToolTip>
33
34 K_GLOBAL_STATIC(KFormattedBalloonTipDelegate, g_delegate)
35
36 ToolTipManager::ToolTipManager(QAbstractItemView* parent,
37 DolphinSortFilterProxyModel* model) :
38 QObject(parent),
39 m_view(parent),
40 m_dolphinModel(0),
41 m_proxyModel(model),
42 m_timer(0),
43 m_item(),
44 m_itemRect()
45 {
46 KToolTip::setToolTipDelegate(g_delegate);
47
48 m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
49 connect(parent, SIGNAL(entered(const QModelIndex&)),
50 this, SLOT(requestToolTip(const QModelIndex&)));
51 connect(parent, SIGNAL(viewportEntered()),
52 this, SLOT(hideToolTip()));
53
54 m_timer = new QTimer(this);
55 m_timer->setSingleShot(true);
56 connect(m_timer, SIGNAL(timeout()),
57 this, SLOT(showToolTip()));
58
59 m_view->viewport()->installEventFilter(this);
60 }
61
62 ToolTipManager::~ToolTipManager()
63 {
64 }
65
66 void ToolTipManager::hideTip()
67 {
68 hideToolTip();
69 }
70
71 bool ToolTipManager::eventFilter(QObject* watched, QEvent* event)
72 {
73 if ((watched == m_view->viewport()) && (event->type() == QEvent::Leave)) {
74 hideToolTip();
75 }
76
77 return QObject::eventFilter(watched, event);
78 }
79
80 void ToolTipManager::requestToolTip(const QModelIndex& index)
81 {
82 if (index.column() == DolphinModel::Name) {
83 KToolTip::hideTip();
84
85 m_itemRect = m_view->visualRect(index);
86 const QPoint pos = m_view->viewport()->mapToGlobal(m_itemRect.topLeft());
87 m_itemRect.moveTo(pos);
88
89 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
90 m_item = m_dolphinModel->itemForIndex(dirIndex);
91
92 m_timer->start(500);
93 } else {
94 hideToolTip();
95 }
96 }
97
98 void ToolTipManager::hideToolTip()
99 {
100 m_timer->stop();
101 KToolTip::hideTip();
102 }
103
104 void ToolTipManager::showToolTip()
105 {
106 KToolTipItem* tip = new KToolTipItem(KIcon(m_item.iconName()), m_item.getToolTipText());
107
108 KStyleOptionToolTip option;
109 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
110 option.direction = QApplication::layoutDirection();
111 option.fontMetrics = QFontMetrics(QToolTip::font());
112 option.activeCorner = KStyleOptionToolTip::TopLeftCorner;
113 option.palette = QToolTip::palette();
114 option.font = QToolTip::font();
115 option.rect = QRect();
116 option.state = QStyle::State_None;
117 option.decorationSize = QSize(32, 32);
118
119 const QSize size = g_delegate->sizeHint(&option, tip);
120 const QRect desktop = QApplication::desktop()->screenGeometry(m_itemRect.bottomRight());
121
122 // m_itemRect defines the area of the item, where the tooltip should be
123 // shown. Per default the tooltip is shown in the bottom right corner.
124 // If the tooltip content exceeds the desktop borders, it must be assured that:
125 // - the content is fully visible
126 // - the content is not drawn inside m_itemRect
127 int x = m_itemRect.right();
128 int y = m_itemRect.bottom();
129 if (x + size.width() - 1 > desktop.right()) {
130 x = m_itemRect.left() - size.width();
131 }
132 if (y + size.height() - 1 > desktop.bottom()) {
133 y = m_itemRect.top() - size.height();
134 }
135
136 KToolTip::showTip(QPoint(x, y), tip);
137 }
138
139 #include "tooltipmanager.moc"