first commit
This commit is contained in:
116
widgets/pageindicator.cpp
Normal file
116
widgets/pageindicator.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
#include "pageindicator.h"
|
||||
#include <QPainter>
|
||||
#include <QMouseEvent>
|
||||
|
||||
PageIndicator::PageIndicator(int pageCount, QWidget *parent)
|
||||
: QWidget(parent), m_pageCount(pageCount), m_currentPage(0), m_dotSize(8), m_selectedDotSize(10), m_dotSpacing(14), m_dotColor(QColor(255, 255, 255, 120)), m_selectedColor(QColor(255, 255, 255, 255))
|
||||
{
|
||||
setCursor(Qt::PointingHandCursor);
|
||||
setAttribute(Qt::WA_TranslucentBackground);
|
||||
}
|
||||
|
||||
PageIndicator::~PageIndicator()
|
||||
{
|
||||
}
|
||||
|
||||
void PageIndicator::setPageCount(int count)
|
||||
{
|
||||
if (count != m_pageCount)
|
||||
{
|
||||
m_pageCount = count;
|
||||
if (m_currentPage >= m_pageCount)
|
||||
{
|
||||
m_currentPage = m_pageCount - 1;
|
||||
}
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void PageIndicator::setCurrentPage(int page)
|
||||
{
|
||||
if (page >= 0 && page < m_pageCount && page != m_currentPage)
|
||||
{
|
||||
m_currentPage = page;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void PageIndicator::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
|
||||
QPainter painter(this);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
// 计算总宽度
|
||||
int totalWidth = 0;
|
||||
for (int i = 0; i < m_pageCount; ++i)
|
||||
{
|
||||
int dotSize = (i == m_currentPage) ? m_selectedDotSize : m_dotSize;
|
||||
totalWidth += dotSize;
|
||||
if (i < m_pageCount - 1)
|
||||
{
|
||||
totalWidth += m_dotSpacing;
|
||||
}
|
||||
}
|
||||
|
||||
// 计算起始位置(居中)
|
||||
int startX = (width() - totalWidth) / 2;
|
||||
int centerY = height() / 2;
|
||||
|
||||
// 绘制指示点
|
||||
int x = startX;
|
||||
for (int i = 0; i < m_pageCount; ++i)
|
||||
{
|
||||
bool isSelected = (i == m_currentPage);
|
||||
int dotSize = isSelected ? m_selectedDotSize : m_dotSize;
|
||||
QColor color = isSelected ? m_selectedColor : m_dotColor;
|
||||
|
||||
painter.setPen(Qt::NoPen);
|
||||
painter.setBrush(color);
|
||||
|
||||
int y = centerY - dotSize / 2;
|
||||
painter.drawEllipse(x, y, dotSize, dotSize);
|
||||
|
||||
x += dotSize + m_dotSpacing;
|
||||
}
|
||||
}
|
||||
|
||||
void PageIndicator::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
// 计算点击位置对应的页面
|
||||
int totalWidth = 0;
|
||||
for (int i = 0; i < m_pageCount; ++i)
|
||||
{
|
||||
int dotSize = (i == m_currentPage) ? m_selectedDotSize : m_dotSize;
|
||||
totalWidth += dotSize;
|
||||
if (i < m_pageCount - 1)
|
||||
{
|
||||
totalWidth += m_dotSpacing;
|
||||
}
|
||||
}
|
||||
|
||||
int startX = (width() - totalWidth) / 2;
|
||||
int x = startX;
|
||||
|
||||
for (int i = 0; i < m_pageCount; ++i)
|
||||
{
|
||||
int dotSize = (i == m_currentPage) ? m_selectedDotSize : m_dotSize;
|
||||
|
||||
// 扩大点击区域
|
||||
QRect hitRect(x - 8, 0, dotSize + 16, height());
|
||||
|
||||
if (hitRect.contains(event->pos()))
|
||||
{
|
||||
if (i != m_currentPage)
|
||||
{
|
||||
emit pageClicked(i);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
x += dotSize + m_dotSpacing;
|
||||
}
|
||||
|
||||
QWidget::mousePressEvent(event);
|
||||
}
|
||||
Reference in New Issue
Block a user