代码拉取完成,页面将自动刷新
#include "ejsidebar.h"
#include <QPaintEvent>
#include <QPainter>
#include <QFont>
#include <QBrush>
#include <QColor>
EjSideBar::EjSideBar(QWidget * parent) : QWidget(parent)
{
m_checkedAction = nullptr;
m_pressedAction = nullptr;
}
void EjSideBar::addAction(QAction *action)
{
m_actions.push_back(action);
action->setCheckable(true);
update();
}
QAction *EjSideBar::addAction(const QString &text, const QIcon &icon)
{
auto action = new QAction(icon, text, this);
m_actions.push_back(action);
action->setCheckable(true);
update();
return action;
}
void EjSideBar::paintEvent(QPaintEvent *event)
{
QPainter p(this);
QFont fontText(p.font());
fontText.setFamily("Helvetica Neue");
p.setFont(fontText);
QImage texture(":/res/png/texture.png");
p.fillRect(event->rect(), QBrush(texture));
p.setPen(Qt::black);
p.drawLine(event->rect().topRight(), event->rect().bottomRight());
int actions_height = m_actions.size()*action_height;
int action_y = event->rect().height()/2-actions_height/2;
foreach(QAction *action, m_actions)
{
QRect actionRect(0, action_y, event->rect().width(), action_height);
if(action->isChecked())
{
p.setOpacity(0.5);
p.fillRect(actionRect, QColor(19, 19, 19));
p.setPen(QColor(9, 9, 9));
p.drawLine(actionRect.topLeft(), actionRect.topRight());
p.setOpacity(1);
}
if(action == m_actions.last())
{
p.setPen(QColor(15, 15, 15));
p.drawLine(QPoint(0, actionRect.bottomLeft().y()-1), QPoint(actionRect.width(), actionRect.bottomRight().y()-1));
p.setPen(QColor(55, 55, 55));
p.drawLine(actionRect.bottomLeft(), actionRect.bottomRight());
}
if(!action->isChecked())
{
p.setPen(QColor(15, 15, 15));
p.drawLine(actionRect.topLeft(), actionRect.topRight());
p.setPen(QColor(55, 55, 55));
p.drawLine(QPoint(0, actionRect.topLeft().y()+1), QPoint(actionRect.width(), actionRect.topRight().y()+1));
}
QRect actionIconRect(0, action_y, event->rect().width(), action_height-20);
QIcon actionIcon(action->icon());
actionIcon.paint(&p, actionIconRect);
p.setPen(QColor(217, 217, 217));
if(action->isChecked())
p.setPen(QColor(255, 255, 255));
QRect actionTextRect(0, action_y+actionRect.height()-20, event->rect().width(), 15);
p.drawText(actionTextRect, Qt::AlignCenter, action->text());
action_y += actionRect.height();
}
}
void EjSideBar::mousePressEvent(QMouseEvent *event)
{
m_pressedAction = actionAt(event->pos());
if(m_pressedAction == nullptr || m_pressedAction == m_checkedAction)
return;
update();
}
void EjSideBar::mouseReleaseEvent(QMouseEvent *event)
{
QAction* tempAction = actionAt(event->pos());
if(m_pressedAction != tempAction || tempAction == nullptr)
{
m_pressedAction = nullptr;
return;
}
if(m_checkedAction != nullptr)
m_checkedAction->setChecked(false);
m_checkedAction = m_pressedAction;
if(m_checkedAction != nullptr)
{
m_checkedAction->activate(QAction::Trigger);
m_checkedAction->setChecked(true);
}
update();
m_pressedAction = nullptr;
}
QSize EjSideBar::minimumSizeHint() const
{
return QSize(90, m_actions.size()*action_height);
}
QAction *EjSideBar::actionAt(const QPoint &at)
{
int actions_height = m_actions.size()*action_height;
int action_y = rect().height()/2-actions_height/2;
foreach(QAction *action, m_actions)
{
QRect actionRect(0, action_y, rect().width(), action_height);
if(actionRect.contains(at))
return action;
action_y += actionRect.height();
}
return nullptr;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。