#include "deftabwidget.h"
#include <QPaintEvent>
#include <QTabBar>
#include <QImage>
#include <QPainter>
#include <QMenu>
#include <QAction>
#include "defaction.h"
#include "settooltipdlg.h"
#include "deftabbar.h"
#include "pagewidget.h"
#include "DefEnum.h"
DefTabWidget::DefTabWidget(QWidget *parent) : QTabWidget(parent)
{
m_AddTabBtn_Image = QImage("TabImages/add.png");
m_TabListBtn_Image = QImage("TabImages/tab_list.png");
this->setTabBar(new DefTabBar(this));
PageWidget* w1 = new PageWidget(this);
PageWidget* w2 = new PageWidget(this);
this->addTab(w1,"tab1");
this->addTab(w2,"tab2");
this->setTabsClosable(true);
this->setMovable(true);
QString style = "QTabBar::tab{width:"+QString("%0").arg(TAB_MAX_WIDTH)+"px;height:"+QString("%0").arg(TAB_HEIGHT)+"px;}"
"QTabBar::tab:selected{color:blue;}" ;
this->setStyleSheet(style);
this->tabBar()->setShape(QTabBar::TriangularNorth);
connect(this,SIGNAL(tabCloseRequested(int)),this,SLOT(RemoveSubTabSlot(int)));
}
DefTabWidget::~DefTabWidget()
{
}
void DefTabWidget::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
if( (HIDE_SINGLE_TAB==0 && tabBar()->count()>=1) || tabBar()->count()>1) //注:条件短路原则;
{
QRect rectAdd(tabBar()->width(),tabBar()->rect().top(),m_AddTabBtn_Image.width(),m_AddTabBtn_Image.height());
painter.drawImage(rectAdd,m_AddTabBtn_Image);
QRect rectTabList(width()-m_TabListBtn_Image.width(),tabBar()->rect().bottom()-m_TabListBtn_Image.height(),m_TabListBtn_Image.width(),m_TabListBtn_Image.height());
painter.drawImage(rectTabList,m_TabListBtn_Image);
}
QTabWidget::paintEvent(event);
}
void DefTabWidget::resizeEvent(QResizeEvent * event)
{
this->ResizeTabBar();
QTabWidget::resizeEvent(event);
}
void DefTabWidget::mousePressEvent(QMouseEvent * event)
{
if(event->button()==Qt::LeftButton)
{
QRect tabRect = tabBar()->rect();
if(event->pos().y()>=tabRect.top() && event->pos().y()<=tabRect.bottom())
{
if(event->pos().x()>=tabRect.width() && event->pos().x()<=tabRect.width()+m_AddTabBtn_Image.width())
{
PageWidget* pageWidget = new PageWidget(this);
this->addTab(pageWidget,"new Tab");
this->ResizeTabBar();
}
else if(event->pos().x()>=width()-m_TabListBtn_Image.width())
{
QMenu* popupMenu = new QMenu(this);
m_ActionList.clear();
int tabCount = tabBar()->count();
for(int i=0;i<tabCount;i++)
{
QString tabText = tabBar()->tabText(i);
DefAction* action = new DefAction(tabText,this);
m_ActionList.append(action);
popupMenu->addAction(m_ActionList[i]);
connect(m_ActionList[i],SIGNAL(triggered()),this,SLOT(SetTabSelectedSlot()));
}
popupMenu->exec(QCursor::pos());
}
}
}
QTabWidget::mousePressEvent(event);
}
void DefTabWidget::mouseDoubleClickEvent(QMouseEvent* event)
{
if(event->button()==Qt::LeftButton)
{
QRect tabRect = tabBar()->rect();
int posX = event->pos().x();
int posY = event->pos().y();
bool isDBClickOnTab = posX>=tabRect.x() && posX<=tabRect.width() && posY>=tabRect.top() && posY<=tabRect.bottom() ;
if(isDBClickOnTab)
{
SetToolTipDlg dlg(this->currentIndex(),QCursor::pos(),this);
dlg.exec();
}
}
}
void DefTabWidget::RemoveSubTabSlot(int index)
{
if(tabBar()->count()>1)
{
this->removeTab(index);
resize(width(),height()+1);
resize(width(),height()-1);
}
else if(HIDE_SINGLE_TAB==0 && tabBar()->count()==1)
{
this->removeTab(index);
}
if(HIDE_SINGLE_TAB && tabBar()->count()==1)
{
tabBar()->setHidden(true);
}
}
void DefTabWidget::SetTabSelectedSlot()
{
for(int index=0;index<m_ActionList.count();index++)
{
if(m_ActionList[index]->IsTriggered())
{
this->setCurrentIndex(index);
break;
}
}
}
void DefTabWidget::ResizeTabBar()
{
int maxWidth = width()-m_AddTabBtn_Image.width()-m_TabListBtn_Image.width();
int avgWidth = maxWidth/(tabBar()->count());
if(avgWidth>TAB_MAX_WIDTH)
avgWidth = TAB_MAX_WIDTH;
QString style = "QTabBar::tab{width:"+QString("%0").arg(avgWidth)+"px;height:"+QString("%0").arg(TAB_HEIGHT)+"px;}"
"QTabBar::tab:selected{color:blue;}" ;
this->setStyleSheet(style);
}
- 1
- 2
- 3
前往页