169 lines
4.8 KiB
C++
169 lines
4.8 KiB
C++
|
|
//customtabbar.cpp
|
|||
|
|
|
|||
|
|
#include "customtabbar.h"
|
|||
|
|
#include <QDebug>
|
|||
|
|
#include <QDrag>
|
|||
|
|
#include <QMimeData>
|
|||
|
|
#include <QMouseEvent>
|
|||
|
|
#include <QApplication>
|
|||
|
|
#include <QCursor>
|
|||
|
|
#include <QMainWindow>
|
|||
|
|
#include <QFrame>
|
|||
|
|
#include "customtabwidget.h"
|
|||
|
|
|
|||
|
|
CustomTabBar::CustomTabBar(QWidget *parent)
|
|||
|
|
: QTabBar(parent)
|
|||
|
|
{
|
|||
|
|
setAcceptDrops(true);
|
|||
|
|
m_pInsertMarkerLine = new QFrame(this);
|
|||
|
|
m_pInsertMarkerLine->setObjectName("InsertMarkerLine");
|
|||
|
|
m_pInsertMarkerLine->setStyleSheet("QFrame{background-color: #ff0000;\
|
|||
|
|
max-width: 2px;}");
|
|||
|
|
m_pInsertMarkerLine->hide();
|
|||
|
|
setElideMode(Qt::ElideMiddle);
|
|||
|
|
|
|||
|
|
//
|
|||
|
|
setStyleSheet("QTabBar{qproperty-tabsClosable:true;}");
|
|||
|
|
|
|||
|
|
//关闭tab时,信号槽触发的函数
|
|||
|
|
//connect(this, SIGNAL(tabCloseRequested(int)),this,SLOT(slot_tabClose(int)));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
CustomTabBar::~CustomTabBar()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
////关闭
|
|||
|
|
//void CustomTabBar::slot_tabClose(int index)
|
|||
|
|
//{
|
|||
|
|
// auto parent = dynamic_cast<CustomTabWidget*>(parentWidget());
|
|||
|
|
// if (parent)
|
|||
|
|
// {
|
|||
|
|
// auto child = parent->currentWidget();
|
|||
|
|
// auto strTab = parent->tabText(index);
|
|||
|
|
// if (!child)
|
|||
|
|
// {
|
|||
|
|
// return;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// qDebug()<<"slot_tabClose Name= " << strTab;
|
|||
|
|
// child->deleteLater();
|
|||
|
|
// parent->removeTab(index);
|
|||
|
|
// }
|
|||
|
|
//}
|
|||
|
|
|
|||
|
|
void CustomTabBar::mousePressEvent(QMouseEvent *event)
|
|||
|
|
{
|
|||
|
|
QTabBar::mousePressEvent(event);
|
|||
|
|
if (event->button() == Qt::LeftButton)
|
|||
|
|
m_dragStartPos = event->pos();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void CustomTabBar::mouseMoveEvent(QMouseEvent *event)
|
|||
|
|
{
|
|||
|
|
QTabBar::mouseMoveEvent(event);
|
|||
|
|
if (!(event->buttons() & Qt::LeftButton))
|
|||
|
|
return;
|
|||
|
|
if ((event->pos() - m_dragStartPos).manhattanLength()
|
|||
|
|
< QApplication::startDragDistance())
|
|||
|
|
return;
|
|||
|
|
auto parent = dynamic_cast<CustomTabWidget*>(parentWidget());
|
|||
|
|
if (parent)
|
|||
|
|
{
|
|||
|
|
auto child = parent->currentWidget();
|
|||
|
|
auto strTab = parent->tabText(parent->currentIndex());
|
|||
|
|
if (!child)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
QPixmap pixmap = QPixmap::grabWidget(child,
|
|||
|
|
0, 0, child->width(), child->height());
|
|||
|
|
QByteArray itemData;
|
|||
|
|
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
|
|||
|
|
dataStream << (long long)parent << parent->tabText(parent->currentIndex()) << (long long)child << parent->currentIndex();
|
|||
|
|
QMimeData *mimeData = new QMimeData;
|
|||
|
|
mimeData->setData("application/itemdata", itemData);
|
|||
|
|
QDrag *drag = new QDrag(this);
|
|||
|
|
drag->setMimeData(mimeData);
|
|||
|
|
drag->setPixmap(pixmap);
|
|||
|
|
parent->removeTab(parent->currentIndex());
|
|||
|
|
if (drag->exec(Qt::MoveAction, Qt::MoveAction) != Qt::MoveAction)
|
|||
|
|
{
|
|||
|
|
auto newTabWidget = new CustomTabWidget;
|
|||
|
|
newTabWidget->setAutoDelete(true);
|
|||
|
|
newTabWidget->addTab(child, strTab);
|
|||
|
|
newTabWidget->show();
|
|||
|
|
newTabWidget->move(QCursor::pos());
|
|||
|
|
newTabWidget->setCurrentIndex(0);
|
|||
|
|
m_pInsertMarkerLine->hide();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void CustomTabBar::mouseReleaseEvent(QMouseEvent *event)
|
|||
|
|
{
|
|||
|
|
QTabBar::mouseReleaseEvent(event);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void CustomTabBar::dragMoveEvent(QDragMoveEvent *event)
|
|||
|
|
{
|
|||
|
|
//显示效果可以在此添加
|
|||
|
|
m_pInsertMarkerLine->show();
|
|||
|
|
int index = tabAt(event->pos());
|
|||
|
|
auto rectTmp = tabRect(index);
|
|||
|
|
rectTmp.setWidth(rectTmp.width() / 2);
|
|||
|
|
if (rectTmp.contains(event->pos()))
|
|||
|
|
{
|
|||
|
|
m_pInsertMarkerLine->move(tabRect(index).topLeft());
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
m_pInsertMarkerLine->move(tabRect(index).topRight());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void CustomTabBar::dragLeaveEvent(QDragLeaveEvent *event)
|
|||
|
|
{
|
|||
|
|
m_pInsertMarkerLine->hide();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void CustomTabBar::dragEnterEvent(QDragEnterEvent *event)
|
|||
|
|
{
|
|||
|
|
m_pInsertMarkerLine->show();
|
|||
|
|
event->acceptProposedAction();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void CustomTabBar::dropEvent(QDropEvent *event)
|
|||
|
|
{
|
|||
|
|
if (event->mimeData()->hasFormat("application/itemdata"))
|
|||
|
|
{
|
|||
|
|
QByteArray itemData = event->mimeData()->data("application/itemdata");
|
|||
|
|
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
|
|||
|
|
long long llPtrParent;
|
|||
|
|
QString strTab;
|
|||
|
|
long long llPtrChild;
|
|||
|
|
int nTabIndex;
|
|||
|
|
dataStream >> llPtrParent >> strTab >> llPtrChild >> nTabIndex;
|
|||
|
|
QWidget *child = (QWidget *)llPtrChild;
|
|||
|
|
auto parent = dynamic_cast<CustomTabWidget*>(parentWidget());
|
|||
|
|
if (parent && child)
|
|||
|
|
{
|
|||
|
|
int index = tabAt(event->pos());
|
|||
|
|
auto rectTmp = tabRect(index);
|
|||
|
|
rectTmp.setWidth(rectTmp.width() / 2);
|
|||
|
|
if (!rectTmp.contains(event->pos()))
|
|||
|
|
{
|
|||
|
|
index++;
|
|||
|
|
}
|
|||
|
|
parent->insertTab(index, child, strTab);
|
|||
|
|
setCurrentIndex(index);
|
|||
|
|
event->acceptProposedAction();
|
|||
|
|
m_pInsertMarkerLine->hide();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
event->ignore();
|
|||
|
|
}
|
|||
|
|
}
|