diff --git a/plugins/projectmanagerview/projectmodelitemdelegate.h b/plugins/projectmanagerview/projectmodelitemdelegate.h --- a/plugins/projectmanagerview/projectmodelitemdelegate.h +++ b/plugins/projectmanagerview/projectmodelitemdelegate.h @@ -21,6 +21,12 @@ #define KDEVPLATFORM_PLUGIN_PROJECTMODELITEMDELEGATE_H #include +#include + +namespace KDevelop +{ +class NavigationToolTip; +} class ProjectModelItemDelegate : public QItemDelegate { @@ -31,11 +37,17 @@ void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override; void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const override; + bool helpEvent(QHelpEvent* event, QAbstractItemView* view, const QStyleOptionViewItem& option, + const QModelIndex& index) override; private: void drawBranchName(QPainter* painter, const QStyleOptionViewItem& option, const QRect& rect, const QString& branchName) const; void drawStyledBackground(QPainter* painter, const QStyleOptionViewItem& option) const; + +private: + QPointer m_tooltip; + QPersistentModelIndex m_tooltippedIndex; }; #endif // KDEVPLATFORM_PLUGIN_PROJECTMODELITEMDELEGATE_H diff --git a/plugins/projectmanagerview/projectmodelitemdelegate.cpp b/plugins/projectmanagerview/projectmodelitemdelegate.cpp --- a/plugins/projectmanagerview/projectmodelitemdelegate.cpp +++ b/plugins/projectmanagerview/projectmodelitemdelegate.cpp @@ -20,9 +20,22 @@ #include "projectmodelitemdelegate.h" #include "vcsoverlayproxymodel.h" - +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include #include +using namespace KDevelop; + ProjectModelItemDelegate::ProjectModelItemDelegate(QObject* parent) : QItemDelegate(parent) {} @@ -158,3 +171,57 @@ QFontMetrics fm(painter->fontMetrics()); painter->drawText(rect, fm.elidedText(text, Qt::ElideRight, rect.width())); } + +bool ProjectModelItemDelegate::helpEvent(QHelpEvent* event, + QAbstractItemView* view, const QStyleOptionViewItem& option, + const QModelIndex& index) +{ + if (!event || !view) { + return false; + } + + if (event->type() == QEvent::ToolTip) { + QHelpEvent* helpEvent = static_cast(event); + + // explicitely close current tooltip, as its autoclose margins overlap items + if ((m_tooltippedIndex != index) && m_tooltip) { + m_tooltip->close(); + m_tooltip.clear(); + } + + const ProjectBaseItem* it = index.data(ProjectModel::ProjectItemRole).value(); + + // show navigation tooltip for files + if (it && it->file()) { + if (!m_tooltip) { + m_tooltippedIndex = index; + KDevelop::DUChainReadLocker lock(KDevelop::DUChain::lock()); + const TopDUContext* top = DUChainUtils::standardContextForUrl(it->file()->path().toUrl()); + + if (top) { + QWidget* navigationWidget = top->createNavigationWidget(); + if (navigationWidget) { + // force possible existing normal tooltip for other list item to hide + // Seems that is still only done with a small delay though, + // but the API seems not to allow more control. + QToolTip::hideText(); + + m_tooltip = new KDevelop::NavigationToolTip(view, helpEvent->globalPos() + QPoint(40, 0), navigationWidget); + m_tooltip->resize(navigationWidget->sizeHint() + QSize(10, 10)); + auto rect = view->visualRect(m_tooltippedIndex); + rect.moveTopLeft(view->mapToGlobal(rect.topLeft())); + m_tooltip->setHandleRect(rect); + ActiveToolTip::showToolTip(m_tooltip); + } + } + } + + // tooltip successfully handled by us? + if (m_tooltip) { + return true; + } + } + } + + return QItemDelegate::helpEvent(event, view, option, index); +} diff --git a/plugins/projectmanagerview/projecttreeview.h b/plugins/projectmanagerview/projecttreeview.h --- a/plugins/projectmanagerview/projecttreeview.h +++ b/plugins/projectmanagerview/projecttreeview.h @@ -30,7 +30,6 @@ { class IProject; class ProjectBaseItem; -class NavigationToolTip; class Path; } @@ -43,8 +42,6 @@ static QModelIndex mapFromSource(const QAbstractProxyModel* proxy, const QModelIndex& sourceIdx); - bool event(QEvent* event) override; - Q_SIGNALS: void activate( const KDevelop::Path &url ); @@ -71,8 +68,6 @@ KDevelop::IProject* getCurrentProject(); QPointer m_previousSelection; - QPointer m_tooltip; - QPersistentModelIndex m_idx; }; #endif // KDEVPLATFORM_PLUGIN_PROJECTTREEVIEW_H diff --git a/plugins/projectmanagerview/projecttreeview.cpp b/plugins/projectmanagerview/projecttreeview.cpp --- a/plugins/projectmanagerview/projecttreeview.cpp +++ b/plugins/projectmanagerview/projecttreeview.cpp @@ -34,7 +34,6 @@ #include #include -#include #include #include #include @@ -50,9 +49,6 @@ #include "projectmodelsaver.h" #include "projectmodelitemdelegate.h" #include "debug.h" -#include -#include -#include #include #include @@ -459,46 +455,6 @@ } } -bool ProjectTreeView::event(QEvent* event) -{ - if(event->type()==QEvent::ToolTip) - { - QHelpEvent* helpEvent = static_cast(event); - QModelIndex idxView = indexAt(helpEvent->pos()); - - ProjectBaseItem* it = idxView.data(ProjectModel::ProjectItemRole).value(); - QModelIndex idx; - if(it) - idx = it->index(); - - if((m_idx!=idx || !m_tooltip) && it && it->file()) - { - m_idx=idx; - ProjectFileItem* file=it->file(); - KDevelop::DUChainReadLocker lock(KDevelop::DUChain::lock()); - TopDUContext* top= DUChainUtils::standardContextForUrl(file->path().toUrl()); - - if(m_tooltip) - m_tooltip->close(); - - if(top) - { - QWidget* navigationWidget = top->createNavigationWidget(); - if( navigationWidget ) - { - m_tooltip = new KDevelop::NavigationToolTip(this, helpEvent->globalPos() + QPoint(40, 0), navigationWidget); - m_tooltip->resize( navigationWidget->sizeHint() + QSize(10, 10) ); - qCDebug(PLUGIN_PROJECTMANAGERVIEW) << "tooltip size" << m_tooltip->size(); - ActiveToolTip::showToolTip(m_tooltip); - return true; - } - } - } - } - - return QAbstractItemView::event(event); -} - void ProjectTreeView::keyPressEvent(QKeyEvent* event) { if (event->key() == Qt::Key_Return && currentIndex().isValid() && state()!=QAbstractItemView::EditingState)