Files
2020-04-13 21:37:30 +08:00

294 lines
7.5 KiB
C++

#include "qnavigationwidget.h"
#include <QPainter>
#include <QDebug>
QNavigationWidget::QNavigationWidget(QWidget *parent) : QWidget(parent)
{
backgroundColor = "#E4E4E4";
selectedColor = "#2CA7F8";
rowHeight = 40;
currentIndex = 0;
MouseColor = "#87CEFF";
MouseIndex = -1;
controlBarColor = "#CAE1FF";
controlSlideColor = "#1E90FF";
controlBarWidth = 10;
setAttribute(Qt::WA_AlwaysStackOnTop);
setWindowFlags(Qt::WindowStaysOnTopHint);
setMouseTracking(true);
setFixedWidth(150);
}
QNavigationWidget::~QNavigationWidget()
{
}
void QNavigationWidget::addItem(const QString &title, QIcon &icon)
{
Q_UNUSED(icon);
listItems << title;
update();
}
void QNavigationWidget::setWidth(const int &width)
{
setFixedWidth(width);
}
void QNavigationWidget::setBackgroundColor(const QString &color)
{
backgroundColor = color;
update();
}
void QNavigationWidget::setSelectColor(const QString &color)
{
selectedColor = color;
update();
}
void QNavigationWidget::setRowHeight(const int &height)
{
rowHeight = height;
update();
}
void QNavigationWidget::setCurrentIndex(const int &index)
{
currentIndex = index;
update();
}
int QNavigationWidget::Index(void)
{
return currentIndex;
}
void QNavigationWidget::resizeEvent(QResizeEvent *event)
{
emit SizeChanged(event);
}
void QNavigationWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
// Draw background color.
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(backgroundColor));
painter.drawRect(rect());
if(!isHidden)
{
QPainterPath MenuPath;
MenuPath.addRect(QRect(width() - controlBarWidth,0,controlBarWidth, height()));
painter.setPen("#202020");
painter.fillPath(MenuPath, QColor(controlBarColor));
int h = height()/2;
QPolygon polygon1,polygon2,polygon3,polygon4;
polygon1.setPoints(4,
width() - controlBarWidth,h ,
width(), h - controlBarWidth,
width(), h - 3 * controlBarWidth / 2,
width() - controlBarWidth,h - controlBarWidth / 2);
polygon2.setPoints(4,
width() - controlBarWidth,h ,
width(), h + controlBarWidth,
width(), h + 3 * controlBarWidth / 2,
width() - controlBarWidth,h + controlBarWidth / 2);
polygon3.setPoints(4,
width() - controlBarWidth,h - controlBarWidth ,
width(), h - 2 * controlBarWidth,
width(), h - 5 * controlBarWidth /2,
width() - controlBarWidth,h - 3 * controlBarWidth /2);
polygon4.setPoints(4,
width() - controlBarWidth,h + controlBarWidth ,
width(), h + 2 * controlBarWidth,
width(), h + 5 * controlBarWidth /2,
width() - controlBarWidth,h + 3 * controlBarWidth /2);
QPainterPath SlidePath;
SlidePath.addRegion(polygon1);
SlidePath.addRegion(polygon2);
SlidePath.addRegion(polygon3);
SlidePath.addRegion(polygon4);
painter.fillPath(SlidePath, QColor(controlSlideColor));
// Draw Items
int count = 0;
for (const QString &str : listItems) {
QPainterPath itemPath;
itemPath.addRect(QRect(0, count * rowHeight, width() - controlBarWidth, rowHeight));
if (currentIndex == count) {
painter.setPen("#FFFFFF");
painter.fillPath(itemPath, QColor(selectedColor));
}else {
painter.setPen("#202020");
if(MouseIndex == count)
{
painter.fillPath(itemPath, QColor(MouseColor));
}
else
{
painter.fillPath(itemPath, QColor(backgroundColor));
}
}
painter.drawText(QRect(0, count * rowHeight, width(), rowHeight), Qt::AlignVCenter | Qt::AlignHCenter, str);
++count;
}
//DrawHandle
}
else//隐藏状态
{
QPainterPath MenuPath;
MenuPath.addRect(QRect(0,0,controlBarWidth, height()));
painter.setPen("#202020");
painter.fillPath(MenuPath, QColor(controlBarColor));
int h = height()/2;
QPolygon polygon1,polygon2,polygon3,polygon4;
polygon1.setPoints(4,
controlBarWidth,h ,
0, h - controlBarWidth,
0, h - 3 * controlBarWidth / 2,
controlBarWidth,h - controlBarWidth / 2);
polygon2.setPoints(4,
controlBarWidth,h ,
0, h + controlBarWidth,
0, h + 3 * controlBarWidth / 2,
controlBarWidth,h + controlBarWidth / 2);
polygon3.setPoints(4,
controlBarWidth,h - controlBarWidth ,
0, h - 2 * controlBarWidth,
0, h - 5 * controlBarWidth /2,
controlBarWidth,h - 3 * controlBarWidth /2);
polygon4.setPoints(4,
controlBarWidth,h + controlBarWidth ,
0, h + 2 * controlBarWidth,
0, h + 5 * controlBarWidth /2,
controlBarWidth,h + 3 * controlBarWidth /2);
QPainterPath SlidePath;
SlidePath.addRegion(polygon1);
SlidePath.addRegion(polygon2);
SlidePath.addRegion(polygon3);
SlidePath.addRegion(polygon4);
painter.fillPath(SlidePath, QColor(controlSlideColor));
}
}
void QNavigationWidget::mouseMoveEvent(QMouseEvent *e)
{
if ((e->y() / rowHeight < listItems.count())&&
(e->x() < (width() - controlBarWidth)))
{
MouseIndex = e->y() / rowHeight;
update();
}
else
{
MouseIndex = -1;
update();
}
}
void QNavigationWidget::enterEvent(QEvent *e)
{
qDebug() << e;
}
void QNavigationWidget::leaveEvent(QEvent *e)
{
Q_UNUSED(e);
MouseIndex = -1;
update();
}
void QNavigationWidget::mousePressEvent(QMouseEvent *e)
{
if ((e->y() / rowHeight < listItems.count())&&
(e->x() < (width() - controlBarWidth)))
{
currentIndex = e->y() / rowHeight;
emit ItemChanged(currentIndex);
update();
}
else if((e->x() > (width() - controlBarWidth)) &&
(e->x() < width()) )
{
isHidden = isHidden?false:true;
//qDebug() << isHidden;
if(isHidden){
setWidth(controlBarWidth);
}
else {
setWidth(150);
}
update();
}
}
void QNavigationWidget::mouseReleaseEvent(QMouseEvent *e)
{
}