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 "dolphintooltip.h"
23 #include "dolphinmodel.h"
24 #include "dolphinsortfilterproxymodel.h"
29 #include <QApplication>
30 #include <QDesktopWidget>
34 K_GLOBAL_STATIC(DolphinBalloonTooltipDelegate
, 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 // TODO - create tip during requestTip(...) - this makes it more likely that the previews
107 // job will have completed by the time the tooltip is shown, resulting in less flicker.
108 // The memory management will be more intricate, though.
109 DolphinToolTipItem
*tip
= new DolphinToolTipItem(m_item
);
111 KStyleOptionToolTip option
;
112 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
113 option
.direction
= QApplication::layoutDirection();
114 option
.fontMetrics
= QFontMetrics(QToolTip::font());
115 option
.activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
116 option
.palette
= QToolTip::palette();
117 option
.font
= QToolTip::font();
118 option
.rect
= QRect();
119 option
.state
= QStyle::State_None
;
120 option
.decorationSize
= QSize(32, 32);
122 const QSize size
= g_delegate
->sizeHint(&option
, tip
);
123 const QRect desktop
= QApplication::desktop()->screenGeometry(m_itemRect
.bottomRight());
125 // m_itemRect defines the area of the item, where the tooltip should be
126 // shown. Per default the tooltip is shown in the bottom right corner.
127 // If the tooltip content exceeds the desktop borders, it must be assured that:
128 // - the content is fully visible
129 // - the content is not drawn inside m_itemRect
130 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() >= desktop
.left());
131 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() <= desktop
.right());
132 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() >= desktop
.top());
133 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() <= desktop
.bottom());
134 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
141 if (hasRoomToLeft
|| hasRoomToRight
) {
142 x
= hasRoomToRight
? m_itemRect
.right() : m_itemRect
.left() - size
.width();
144 // Put the tooltip at the far right of the screen. The item will be overlapped
145 // horizontally, but the y-coordinate will be adjusted afterwards so that no overlapping
146 // occurs vertically.
147 x
= desktop
.right() - size
.width();
151 if (hasRoomBelow
|| hasRoomAbove
) {
152 y
= hasRoomBelow
? m_itemRect
.bottom() : m_itemRect
.top() - size
.height();
154 // Put the tooltip at the bottom of the screen. The x-coordinate has already
155 // been adjusted, so that no overlapping with m_itemRect occurs.
156 y
= desktop
.bottom() - size
.height();
159 KToolTip::showTip(QPoint(x
, y
), tip
);
162 #include "tooltipmanager.moc"