logplus/logPlus/customtabwidget.cpp

190 lines
4.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//customtabwidget.cpp
#include "customtabwidget.h"
#include <QStackedWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QApplication>
#include <QPainter>
#include <QPixmap>
#include <QMimeData>
#include <QDrag>
#include <QFrame>
#include <QDropEvent>
#include <QDebug>
#include "customtabbar.h"
CustomTabWidget::CustomTabWidget(QWidget *parent)
: QWidget(parent)
{
m_pTabBar = new CustomTabBar(this);
QHBoxLayout *pHLayout = new QHBoxLayout;
pHLayout->addWidget(m_pTabBar);
pHLayout->addStretch();
m_pStackedWidget = new QStackedWidget(this);
QVBoxLayout *pVLayout = new QVBoxLayout(this);
pVLayout->addLayout(pHLayout);
pVLayout->addWidget(m_pStackedWidget);
pVLayout->setStretch(1, 1);
pVLayout->setMargin(0);
pVLayout->setSpacing(0);
connect(m_pTabBar, SIGNAL(currentChanged(int)), this, SLOT(setCurrentIndex(int)));
setAcceptDrops(true);
m_pStackedWidget->setStyleSheet("QStackedWidget{border: 1px solid #000000;}");
//关闭tab时信号槽触发的函数
connect(m_pTabBar, SIGNAL(tabCloseRequested(int)),this,SLOT(slot_tabClose(int)));
}
CustomTabWidget::~CustomTabWidget()
{
}
void CustomTabWidget::addTab(QWidget *widget, const QString &strTab)
{
int iIndex = m_pTabBar->addTab(strTab);
m_pStackedWidget->addWidget(widget);
widget->setAcceptDrops(true);
//
m_pTabBar->setCurrentIndex(iIndex);
}
void CustomTabWidget::addTab(QWidget *page, const QIcon &icon, const QString &label)
{
m_pTabBar->addTab(icon, label);
m_pStackedWidget->addWidget(page);
}
void CustomTabWidget::addTabs(CustomTabWidget *pOtherTab)
{
for (int i = 0; i < pOtherTab->count(); i++)
{
addTab(pOtherTab->tabWidget(0), pOtherTab->tabText(i));
}
}
int CustomTabWidget::count() const
{
return m_pTabBar->count();
}
int CustomTabWidget::currentIndex() const
{
return m_pTabBar->currentIndex();
}
QWidget * CustomTabWidget::currentWidget() const
{
return m_pStackedWidget->currentWidget();
}
int CustomTabWidget::insertTab(int index, QWidget *page, const QString &label)
{
m_pTabBar->insertTab(index, label);
return m_pStackedWidget->insertWidget(index, page);
}
int CustomTabWidget::insertTab(int index, QWidget *page, const QIcon &icon, const QString &label)
{
m_pTabBar->insertTab(index, icon, label);
return m_pStackedWidget->insertWidget(index, page);
}
void CustomTabWidget::removeTab(int index)
{
m_pTabBar->removeTab(index);
m_pStackedWidget->removeWidget(m_pStackedWidget->widget(index)); //widget is not deleted but simply removed from the stacked layout, causing it to be hidden.
if (m_pTabBar->count() == 0)
{
emit tabIsEmpty();
if (m_bAutoDelete)
{
hide();
deleteLater();
}
}
}
QString CustomTabWidget::tabText(int index) const
{
return m_pTabBar->tabText(index);
}
QWidget * CustomTabWidget::tabWidget(int index) const
{
return m_pStackedWidget->widget(index);
}
void CustomTabWidget::setAutoHideTabBar(bool bHide)
{
m_bAutoHideBar = bHide;
}
void CustomTabWidget::setCurrentIndex(int index)
{
m_pStackedWidget->setCurrentIndex(index);
emit currentTabChanged(m_pTabBar->tabText(index));
}
void CustomTabWidget::setAutoDelete(bool bAuto)
{
m_bAutoDelete = bAuto;
}
void CustomTabWidget::mousePressEvent(QMouseEvent *event)
{
QWidget::mousePressEvent(event);
if (event->button() == Qt::LeftButton)
m_dragStartPos = event->pos();
}
void CustomTabWidget::mouseMoveEvent(QMouseEvent *event)
{
QWidget::mouseMoveEvent(event);
}
void CustomTabWidget::paintEvent(QPaintEvent *event)
{
if (m_bAutoHideBar)
{
m_pTabBar->setVisible(m_pTabBar->count() > 1);
}
}
void CustomTabWidget::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
void CustomTabWidget::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;
CustomTabWidget *tabWidgetFrom = (CustomTabWidget *)llPtrParent;
QWidget *child = (QWidget *)llPtrChild;
if (child)
{
addTab(child, strTab);
event->acceptProposedAction();
}
}
else
{
event->ignore();
}
}
//关闭
void CustomTabWidget::slot_tabClose(int index)
{
removeTab(index);
}