1 /*******************************************************************************
2 * Copyright (C) 2008 by Konstantin Heil <konst.heil@stud.uni-heidelberg.de> *
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. *
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. *
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 *******************************************************************************/
20 #include "tooltipmanager.h"
22 #include "dolphinmodel.h"
23 #include "dolphinsortfilterproxymodel.h"
25 #include <kformattedballoontipdelegate.h>
29 #include <QApplication>
30 #include <QDesktopWidget>
34 K_GLOBAL_STATIC(KFormattedBalloonTipDelegate
, g_delegate
)
36 ToolTipManager::ToolTipManager(QAbstractItemView
* parent
,
37 DolphinSortFilterProxyModel
* model
) :
46 KToolTip::setToolTipDelegate(g_delegate
);
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()));
54 m_timer
= new QTimer(this);
55 m_timer
->setSingleShot(true);
56 connect(m_timer
, SIGNAL(timeout()),
57 this, SLOT(showToolTip()));
59 m_view
->viewport()->installEventFilter(this);
62 ToolTipManager::~ToolTipManager()
66 void ToolTipManager::hideTip()
71 bool ToolTipManager::eventFilter(QObject
* watched
, QEvent
* event
)
73 if ((watched
== m_view
->viewport()) && (event
->type() == QEvent::Leave
)) {
77 return QObject::eventFilter(watched
, event
);
80 void ToolTipManager::requestToolTip(const QModelIndex
& index
)
82 if (index
.column() == DolphinModel::Name
) {
85 m_itemRect
= m_view
->visualRect(index
);
86 const QPoint pos
= m_view
->viewport()->mapToGlobal(m_itemRect
.topLeft());
87 m_itemRect
.moveTo(pos
);
89 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
90 m_item
= m_dolphinModel
->itemForIndex(dirIndex
);
98 void ToolTipManager::hideToolTip()
104 void ToolTipManager::showToolTip()
106 KToolTipItem
* tip
= new KToolTipItem(KIcon(m_item
.iconName()), m_item
.getToolTipText());
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);
119 const QSize size
= g_delegate
->sizeHint(&option
, tip
);
120 const QRect desktop
= QApplication::desktop()->screenGeometry(m_itemRect
.bottomRight());
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();
132 if (y
+ size
.height() - 1 > desktop
.bottom()) {
133 y
= m_itemRect
.top() - size
.height();
136 KToolTip::showTip(QPoint(x
, y
), tip
);
139 #include "tooltipmanager.moc"