#include "listdelegate.h"
#include "listview.h"
#include <QDebug>
#include <QEvent>
#include <QPainter>
#include <QApplication>
#include <QToolTip>
#include <QMouseEvent>
enum TextColumn
{
kSn,
kTitle,
kHide,
kAlbum,
kDuration,
kSize,
kTextCount
};
enum ButtonColumn
{
kPlay,
kCollection,
kComment,
kMore,
kBtnCount
};
enum IconPixmapIdx
{
kPlayNormal,
kPlayHover,
kCollectionNormal,
kCollectionHover,
kCommentNormal,
kMoreNormal,
kMoreHover,
};
const int kIconSize = 32;
const int kTitleWidth = 60;
const int kBaseWidth = 150;
ListDelegate::ListDelegate(QObject *parent)
: QStyledItemDelegate(parent)
, m_listView(qobject_cast<ListView*>(parent))
{
Q_ASSERT(m_listView);
QStringList nameList = {"play_normal",
"play_hover",
"collection_normal",
"collection_hover",
"comment_normal",
"more_normal",
"more_hover"};
for(int i =0;i<nameList.size();++i)
{
QString iconPath = QString(":/resource/%1.png").arg(nameList[i]);
m_iconPixmaps.push_back(new QPixmap(iconPath));
}
}
ListDelegate::~ListDelegate()
{
qDeleteAll(m_iconPixmaps);
}
void ListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QPixmap *playIcon = nullptr;
QPixmap *collectionIcon = nullptr;
QPixmap *commentIcon = nullptr;
QPixmap *moreIcon = nullptr;
m_eventType = -1;
//计算5个区域Rect
auto [textRects,btnRects] = calculateRectArea(option.rect);
const bool isHover = option.state.testFlag(QStyle::State_MouseOver);
const QPoint globalPos = QCursor::pos();
const QPoint mousePos = m_listView->mapFromGlobal(globalPos);
Qt::CursorShape cursorShape = Qt::ArrowCursor;
QString tipText;
//背景
if(isHover)
{
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(QBrush(QColor(255,255,255)));
painter->drawRoundedRect(option.rect,10,10);
painter->restore();
}
//第一列:鼠标hover为播放按钮,否则为序号
if(isHover)
{
if(btnRects[kPlay].contains(mousePos))
{
cursorShape = Qt::PointingHandCursor;
tipText = "播放";
playIcon = m_iconPixmaps[kPlayHover];
m_eventType = kPlay;
}
else
{
playIcon = m_iconPixmaps[kPlayNormal];
}
}
else
{
painter->drawText(textRects[kSn],Qt::AlignCenter,QString("%1").arg(index.row(),2,10,QChar('0')));
}
//第二列:歌曲名+歌手名
QString title = index.data(Qt::UserRole).toString();
painter->drawText(textRects[kTitle],Qt::AlignVCenter,title);
if(textRects[kTitle].contains(mousePos))
{
QStringList strList = title.split("\n");
if(mousePos.y()<option.rect.center().y()) tipText = strList[0];
else tipText = strList[1];
}
//第三列:隐藏的按钮
if(isHover)
{
collectionIcon = m_iconPixmaps[kCollectionNormal];
commentIcon = m_iconPixmaps[kCommentNormal];
moreIcon = m_iconPixmaps[kMoreNormal];
if(!tipText.isEmpty())
{
}
else if(btnRects[kCollection].contains(mousePos))
{
cursorShape = Qt::PointingHandCursor;
tipText = "收藏";
collectionIcon = m_iconPixmaps[kCollectionHover];
m_eventType = kCollection;
}
else if(btnRects[kComment].contains(mousePos))
{
cursorShape = Qt::ForbiddenCursor;
tipText = "评论";
//commentIcon = m_iconPixmaps[kCommentHover];
//m_eventType = kComment;
}
else if(btnRects[kMore].contains(mousePos))
{
cursorShape = Qt::PointingHandCursor;
tipText = "更多";
moreIcon = m_iconPixmaps[kMoreHover];
m_eventType = kMore;
}
}
//第四列:专辑名
QString album = index.data(Qt::UserRole+1).toString();
painter->drawText(textRects[kAlbum],Qt::AlignCenter,album);
//第五列:时长
QString duration = index.data(Qt::UserRole+2).toString();
painter->drawText(textRects[kDuration],Qt::AlignCenter,duration);
//第六列:大小
QString size = index.data(Qt::UserRole+3).toString();
painter->drawText(textRects[kSize],Qt::AlignCenter,size);
//真正的画出按钮
if(playIcon!=nullptr) painter->drawPixmap(btnRects[kPlay],*playIcon);
if(collectionIcon!=nullptr) painter->drawPixmap(btnRects[kCollection],*collectionIcon);
if(commentIcon!=nullptr) painter->drawPixmap(btnRects[kComment],*commentIcon);
if(moreIcon!=nullptr) painter->drawPixmap(btnRects[kMore],*moreIcon);
//设置鼠标样式
QApplication::setOverrideCursor(cursorShape);
//设置提示文本
if(tipText.isEmpty()) QToolTip::hideText();
else QToolTip::showText(globalPos,tipText,m_listView);
}
QSize ListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QSize(option.rect.width(),85);
}
bool ListDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
if(event->type() == QEvent::MouseMove)
{
return true;
}
else if(event->type() == QEvent::MouseButtonPress)
{
QMouseEvent *me = static_cast<QMouseEvent*>(event);
if(me->button() == Qt::LeftButton)
{
if(m_eventType==kPlay) emit playBtnClicked(index.row());
else if(m_eventType==kCollection) emit collectionBtnClicked(index.row());
else if(m_eventType==kMore) emit moreBtnClicked(index.row());
}
}
else if(event->type() == QEvent::MouseButtonDblClick)
{
QMouseEvent *me = static_cast<QMouseEvent*>(event);
if(me->button() == Qt::LeftButton)
{
emit playBtnClicked(index.row());
}
}
return QStyledItemDelegate::editorEvent(event,model,option,index);
}
//返回<文本rects,按钮rects>
QPair<QVector<QRect>,QVector<QRect>> ListDelegate::calculateRectArea(const QRect &area) const
{
QVector<QRect> textRects(kTextCount);
for(int i =0;i<textRects.size();++i)
{
int left = (i==0 ? 0 : textRects[i-1].right()+1);
int width = 0;
if(i==kSn)
{
width = kTitleWidth;
}
else if(i==kTitle)
{
width = area.width() - (kTitleWidth + (kSize-kHide+1)*kBaseWidth);
}
else
{
width = kBaseWidth;
}
textRects[i] = QRect(left,area.top(),width,area.height());
}
QVector<QRect> btnRects(kBtnCount);
const int top = (area.height() - kIconSize)/2 + area.top();
QVector<int> vx(kBtnCount);
vx[kPlay] = (textRects[kSn].width() - kIconSize)/2 + textRects[kSn].left();
const int count = (kMore-kCollection+1);
const int interval = (kBaseWidth-count*kIconSize)/(count+1);
for(int i =kCollection;i<=kMore;++i)
{
if(i==kCollection)
{
vx[i] = textRects[kHide].left() + interval;
}
else
{
vx[i] = vx[i-1] + kIconSize + interval;
}
}
for(int i =0;i<vx.size();++i)
{
btnRects[i] = QRect(vx[i],top,kIconSize,kIconSize);
}
return qMakePair(textRects,btnRects);
}
QListView(仿网易云本地音乐列表)
需积分: 0 59 浏览量
2024-03-24
22:35:31
上传
评论
收藏 10KB 7Z 举报
邺清璇
- 粉丝: 9
- 资源: 2
最新资源
- mysql触发器及相关代码示例
- mysql的使用语法及代码示例
- EXCEL从图片链接获取图片
- Untitled-1 (1).dwt
- ChatGPT是一款由OpenAI研发的人工智能聊天机器人程序,以下是对其特点、发展历程和功能的详细归纳: 特点 技术驱动:C
- 基于STM32的毕业设计项目可以涵盖多个领域和应用,以下是一个典型的基于STM32的毕业设计项目框架,并结合参考文章中的相关数字
- 在植物大战僵尸杂交版中,代码资源或秘籍指令可以提供各种游戏内的效果 以下是一些常用的代码资源和秘籍指令,以及它们的描述和用途:
- C语言题库-信.doc
- 对于端午节代码资源,你可以考虑以下几个方向: ### 1. 端午节主题的小游戏 你可以创建一个端午节主题的小游戏,比如"捞粽
- 如果你是在寻找编程相关的节日主题代码资源,我可以为你提供一些常见的做法和示例 以下是一些可能会有帮助的方法: ### 1. 制
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈