PowerMaster/source/customTab.cpp

170 lines
5.0 KiB
C++
Raw Normal View History

#include "customTab.h"
#include <QLabel>
#include <QBoxLayout>
#include <QMouseEvent>
#include <QDrag>
#include <QMimeData>
#include <QApplication>
CustomTab::CustomTab(QWidget* parent)
:QFrame(parent)
{
setContextMenuPolicy(Qt::CustomContextMenu);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
setStyleSheet("QLabel{\n"
" color:rgb(250,250,250);\n"
" font:600 11pt \"微软雅黑\";\n"
" border:0px;\n"
" background-color:transparent;\n"
"}\n");
m_bLeftButtonPressed = false;
m_bDragging = false;
m_pTitle = new QLabel(this);
m_pTitle->setAlignment(Qt::AlignCenter);
m_IconSize = QSize(15,15);
m_pIconLabel = new QLabel(this);
m_pIconLabel->setMinimumSize(m_IconSize);
m_pIconLabel->setMaximumSize(m_IconSize);
QBoxLayout* iconLayout = new QBoxLayout(QBoxLayout::TopToBottom);
iconLayout->setContentsMargins(0, 1, 0, 0);
iconLayout->addWidget(m_pIconLabel);
QSpacerItem* spacer = new QSpacerItem(10, 40, QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Expanding);
iconLayout->addSpacerItem(spacer);
m_pLayout = new QBoxLayout(QBoxLayout::LeftToRight);
m_pLayout->setContentsMargins(15, 0, 0, 0);
m_pLayout->setSpacing(1);
m_pLayout->addWidget(m_pTitle);
m_pLayout->addLayout(iconLayout);
setLayout(m_pLayout);
m_strTabBar = "";
}
CustomTab::~CustomTab()
{
}
void CustomTab::mousePressEvent(QMouseEvent* event)
{
if(event->button() == Qt::LeftButton)
{
emit clicked();
m_pStartPos = event->pos();
m_bLeftButtonPressed = true;
}
QFrame::mousePressEvent(event);
}
void CustomTab::mouseMoveEvent(QMouseEvent* event)
{
if(m_bLeftButtonPressed && !m_bDragging)
{
//qDebug() << "dragging";
int distance = (event->pos() - m_pStartPos).manhattanLength();
if (distance >= QApplication::startDragDistance())
{
QPoint hotSpot = event->pos();
QByteArray data;
QDataStream dataStream(&data, QIODevice::WriteOnly);
dataStream << text() << m_strTabBar;
QMimeData *mimeData = new QMimeData;
mimeData->setData("customTabDrag", data);
QDrag* drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(grab()); //通过render绘制的pixmap会有噪点直接通过grab获取效果更好
drag->setHotSpot(hotSpot);
m_bDragging = true;
Qt::DropAction dropAction = drag->exec();
//exec之后的语句都会在drg操作完成之后(鼠标抬起)执行
delete drag;
m_bLeftButtonPressed = false;
m_bDragging = false;
}
}
QFrame::mouseMoveEvent(event);
}
void CustomTab::mouseReleaseEvent(QMouseEvent* event)
{
//如果没有拖动事件发生,在这里处理相关逻辑
m_bLeftButtonPressed = false;
QFrame::mouseReleaseEvent(event);
}
void CustomTab::setActive(bool bActive)
{
QString strStyleSheet = "";
if(bActive)
strStyleSheet = "QFrame{\n"
" border-bottom:3px solid rgb(67,160,249);\n"
"}\n"
"QFrame:hover{\n"
" background-color:rgba(60, 60, 60, 150);\n"
"}\n"
"QLabel{\n"
" color:rgb(250,250,250);\n"
" font:600 11pt \"微软雅黑\";\n"
" border:0px;\n"
"}\n"
"QLabel:hover{\n"
" background-color:transparent;"
"}\n";
else
strStyleSheet = "QFrame{\n"
" border-bottom:0px;\n"
"}\n"
"QFrame:hover{\n"
" background-color:rgba(60, 60, 60, 150);\n"
"}\n"
"QLabel{\n"
" color:rgb(250,250,250);\n"
" font:600 11pt \"微软雅黑\";\n"
" border:0px;\n"
"}\n"
"QLabel:hover{\n"
" background-color:transparent;\n"
"}\n";
setStyleSheet(strStyleSheet);
}
void CustomTab::setText(const QString& strTitle)
{
m_pTitle->setText(strTitle);
QFontMetrics metrics(m_pTitle->font());
QRect rect = metrics.boundingRect(m_pTitle->text());
m_pTitle->setMinimumSize(rect.width() + 10, 0);
m_pTitle->setMaximumSize(rect.width() + 10, 1660);
setObjectName("dashboardTab_" + strTitle);
}
QString CustomTab::text()
{
return m_pTitle->text();
}
void CustomTab::setIcon(const QIcon& icon)
{
if(icon.isNull())
m_pIconLabel->clear();
else
m_pIconLabel->setPixmap(icon.pixmap(m_IconSize));
}
void CustomTab::setTabBar(const QString strBar)
{
m_strTabBar = strBar;
}
QString CustomTab::tabBar()
{
return m_strTabBar;
}