From 101cc9ec266a2d3b5168ab5053efbf868be7e25b Mon Sep 17 00:00:00 2001 From: Arno Date: Sat, 16 Oct 2010 12:14:43 +0200 Subject: Implemented hover for SeriesTreeWidget SeriesTreeWidget show a popup window with the movies assigned to the series when hovering over a series entry. --- seriestreewidget.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) (limited to 'seriestreewidget.cpp') diff --git a/seriestreewidget.cpp b/seriestreewidget.cpp index e717b13..791fb9e 100644 --- a/seriestreewidget.cpp +++ b/seriestreewidget.cpp @@ -22,6 +22,14 @@ #include #include #include +#include +#include +#include +#include + +#include +#include +#include #include "seriestreewidget.h" #include "smtreemodel.h" @@ -310,7 +318,13 @@ void SeriesTreeWidget::addCover(){ } } -SeriesTreeView::SeriesTreeView(QWidget *parent) : QTreeView(parent) {} +SeriesTreeView::SeriesTreeView(QWidget *parent) : QTreeView(parent), mCursorOffset(0) { + setAttribute(Qt::WA_Hover); + mHoverWin = new SeriesTreeHoverWindow; + XFixesCursorImage *curImage = XFixesGetCursorImage(QX11Info::display()); + mCursorOffset = curImage->height; + XFree(curImage); +} void SeriesTreeView::contextMenuEvent(QContextMenuEvent *e){ QMenu contextMenu(this); @@ -320,6 +334,91 @@ void SeriesTreeView::contextMenuEvent(QContextMenuEvent *e){ contextMenu.exec(e->globalPos()); } +bool SeriesTreeView::event(QEvent *e){ + QModelIndex curIdx; + QHoverEvent *hEvent = static_cast(e); + QPoint hotSpot(hEvent->pos().x(), hEvent->pos().y() - mCursorOffset); + QPoint globalPos = mapToGlobal(hotSpot); + QPoint where = globalPos + QPoint(30, 30); + + curIdx = indexAt(hotSpot); + if((e->type() == QEvent::HoverEnter) || (e->type() == QEvent::HoverMove)){ + if(!curIdx.isValid()){ + return false; + } + if(curIdx.parent() != QModelIndex()){ + mHoverWin->setVisible(false); + mCurHover = QModelIndex(); + return false; + } + } + + if(e->type() == QEvent::HoverEnter){ + mCurHover = curIdx; + mHoverWin->setContent(curIdx.data(Qt::DisplayRole).toString(), children(curIdx)); + mHoverWin->move(where); + mHoverWin->setVisible(true); + return true; + } + if(e->type() == QEvent::HoverMove){ + if(curIdx != mCurHover){ + mCurHover = curIdx; + mHoverWin->setContent(curIdx.data(Qt::DisplayRole).toString(), children(curIdx)); + mHoverWin->setVisible(false); + mHoverWin->move(where); + mHoverWin->setVisible(true); + return true; + }else{ + mHoverWin->move(where); + return true; + } + } + if(e->type() == QEvent::HoverLeave){ + mHoverWin->setVisible(false); + return true; + } + return QTreeView::event(e); +} + +QStringList SeriesTreeView::children(const QModelIndex &idx) const{ + if(!idx.isValid()){ + return QStringList(); + } + QStringList retval; + QModelIndex curIdx = idx.child(0,0); + while(curIdx.isValid()){ + retval << curIdx.data(Qt::DisplayRole).toString(); + curIdx = idx.model()->index(curIdx.row() + 1, curIdx.column(), curIdx.parent()); + } + return retval; +} + +SeriesTreeHoverWindow::SeriesTreeHoverWindow(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f){ + setVisible(false); + setWindowOpacity(0.7); + setStyleSheet("QLabel { background-color: #D6A583; color: black; border-width: 2px; border-style: solid; padding: 4px; }"); + QHBoxLayout *mainLayout = new QHBoxLayout; + mainLayout->setContentsMargins(4, 4, 4, 4); + mLabel = new QLabel; + mainLayout->addWidget(mLabel); + setLayout(mainLayout); +} + +void SeriesTreeHoverWindow::setContent(const QString &parent, const QStringList &children){ + QString curText = QString("

%1

").arg(parent); + curText.append("
    "); + int count = qMin(6, children.size()); + int i = 0; + for(i = 0; i < count; ++i){ + curText.append(QString("
  • %1
  • ").arg(children.at(i))); + } + if(i < children.count()){ + curText.append("
  • ...
  • "); + } + curText.append("
"); + mLabel->setText(curText); +} + SeriesTreeSortModel::SeriesTreeSortModel(QObject *parent) : QSortFilterProxyModel(parent) {} bool SeriesTreeSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const{ -- cgit v1.2.3-70-g09d2