Display tooltip info in library only if text displayed is elided (as it was done for playlistdelegates, r2745). Partially fixes issue 451

This commit is contained in:
Arnaud Bienner 2011-02-08 21:55:32 +00:00
parent 33276132ec
commit de2193f5af
2 changed files with 52 additions and 0 deletions

View File

@ -34,10 +34,13 @@
#include <QPainter>
#include <QContextMenuEvent>
#include <QHelpEvent>
#include <QMenu>
#include <QMessageBox>
#include <QSortFilterProxyModel>
#include <QSettings>
#include <QToolTip>
#include <QWhatsThis>
using smart_playlists::Wizard;
@ -80,6 +83,50 @@ void LibraryItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o
}
}
bool LibraryItemDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *view,
const QStyleOptionViewItem &option,
const QModelIndex &index) {
Q_UNUSED(option);
if (!event || !view)
return false;
QHelpEvent *he = static_cast<QHelpEvent*>(event);
QString text = displayText(index.data(), QLocale::system());
if (text.isEmpty() || !he)
return false;
switch (event->type()) {
case QEvent::ToolTip: {
QRect displayed_text;
QSize real_text;
bool is_elided = false;
real_text = sizeHint(option, index);
displayed_text = view->visualRect(index);
is_elided = displayed_text.width() < real_text.width();
if(is_elided) {
QToolTip::showText(he->globalPos(), text, view);
} else { // in case that another text was previously displayed
QToolTip::hideText();
}
return true;
}
case QEvent::QueryWhatsThis:
return true;
case QEvent::WhatsThis:
QWhatsThis::showText(he->globalPos(), text, view);
return true;
default:
break;
}
return false;
}
LibraryView::LibraryView(QWidget* parent)
: AutoExpandingTreeView(parent),
scripts_(NULL),

View File

@ -37,9 +37,14 @@ class QMimeData;
namespace smart_playlists { class Wizard; }
class LibraryItemDelegate : public QStyledItemDelegate {
Q_OBJECT
public:
LibraryItemDelegate(QObject* parent);
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
public slots:
bool helpEvent(QHelpEvent *event, QAbstractItemView *view,
const QStyleOptionViewItem &option, const QModelIndex &index);
};
class LibraryView : public AutoExpandingTreeView {