#ifndef WIDGETLIB_LIBRARY
#define WIDGETLIB_LIBRARY
#endif
#include <QDebug>
#include "apageturnwidget.h"
APageTurnWidget::APageTurnWidget(QWidget* parentWidget)
: m_pParentWidget(parentWidget),
m_pFirstButton(new QToolButton(m_pParentWidget)),
m_pPreButton(new QToolButton(m_pParentWidget)),
m_pPageNumLineEdit(new QLineEdit(m_pParentWidget)),
m_pGotoButton(new QToolButton(m_pParentWidget)),
m_pNextButton(new QToolButton(m_pParentWidget)),
m_pLastButton(new QToolButton(m_pParentWidget)),
m_pSpacerItem(new QSpacerItem(10, 0, QSizePolicy::Expanding)),
m_pPageInfoLabel(new QLabel),
m_uiRecordCount(0),
m_uiRecordCountPerPage(20),
m_uiCurrentPageNum(0),
m_bCurrentPageChangeEnabled(true),
m_bIsFirstPage(true)
{
Q_ASSERT(m_pParentWidget != NULL);
m_pFirstButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
m_pFirstButton->setText("首页");
m_pPreButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
m_pPreButton->setText("上一页");
m_pGotoButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
m_pGotoButton->setText("跳转到");
m_pNextButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
m_pNextButton->setText("下一页");
m_pLastButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
m_pLastButton->setText("尾页");
//
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(m_pFirstButton);
layout->addWidget(m_pPreButton);
layout->addWidget(m_pPageNumLineEdit);
layout->addWidget(m_pGotoButton);
layout->addWidget(m_pNextButton);
layout->addWidget(m_pLastButton);
layout->addItem(m_pSpacerItem);
layout->addWidget(m_pPageInfoLabel);
layout->setSpacing(2);
layout->setMargin(2);
m_pParentWidget->setLayout(layout);
//
m_pFirstButton->setFixedSize(65, 27);
m_pPreButton->setFixedSize(65, 27);
m_pNextButton->setFixedSize(65, 27);
m_pLastButton->setFixedSize(65, 27);
m_pPageNumLineEdit->setFixedSize(65, 27);
m_pGotoButton->setFixedSize(65, 27);
//
connect(m_pFirstButton, SIGNAL(clicked()), this, SLOT(on_firstButton_clicked()));
connect(m_pPreButton, SIGNAL(clicked()), this, SLOT(on_preButton_clicked()));
connect(m_pNextButton, SIGNAL(clicked()), this, SLOT(on_nextButton_clicked()));
connect(m_pLastButton, SIGNAL(clicked()), this, SLOT(on_lastButton_clicked()));
connect(m_pGotoButton, SIGNAL(clicked()), this, SLOT(on_goButton_clicked()));
QString str = QString("QToolButton{ font: 75 12pt \"微软雅黑\"; color: rgb(61, 72, 88);")
+ QString("background-color: rgb(255,239,188); border: 0px ;}QToolButton::hover {")
+ QString("background-color: rgb(255,249,218);}QToolButton:pressed {background-color: rgb(255,229,178);}")
+ QString("QToolButton:disabled {border: 1px solid rgb(121, 132, 148);background-color: rgb(151, 162, 178);}");
QString sLineEdit = QString("QLineEdit{font: 75 12pt \"微软雅黑\";color: rgb(61, 72, 88);")
+ QString("border: 1px solid rgb(121, 132, 148);background-color: rgb(231, 242, 248);")
+ QString("padding: 1px;}");
QString sLabel = QString("font:75 12pt \"微软雅黑\";\ncolor: rgb(61, 72, 88);");
m_pFirstButton->setStyleSheet(str);
m_pGotoButton->setStyleSheet(str);
m_pPageNumLineEdit->setStyleSheet(sLineEdit);
m_pPreButton->setStyleSheet(str);
m_pNextButton->setStyleSheet(str);
m_pLastButton->setStyleSheet(str);
m_pPageInfoLabel->setStyleSheet(sLabel);
//hideOtherButton();//只显示上一页下一页
}
APageTurnWidget::~APageTurnWidget()
{
uninit();
}
//
bool APageTurnWidget::init()
{
if(m_uiRecordCount > 0)
{
//允许触发当前页改变信号
m_bCurrentPageChangeEnabled = true;
firstPage();
return true;
}
else
{
updateWidgetStatus();
return false;
}
}
//
void APageTurnWidget::uninit()
{
m_uiRecordCount = 0;
m_uiCurrentPageNum = 0;
m_bCurrentPageChangeEnabled = false;
updateWidgetStatus();
}
//设置总的记录数
void APageTurnWidget::setRecordCount(quint32 uiRecordCount)
{
m_uiRecordCount = uiRecordCount;
}
//设置每页记录数
void APageTurnWidget::setRecordCountPerPage(quint32 uiRecordCountPerPage)
{
if(uiRecordCountPerPage == 0)
{
return;
}
m_uiRecordCountPerPage = uiRecordCountPerPage;
}
//设置当前页号
void APageTurnWidget::setCurrentPageNum(quint32 uiPageNum)
{
if(uiPageNum > getPageCount())
{
return;
}
m_uiCurrentPageNum = uiPageNum;
updateWidgetStatus();
//允许触发当前页改变信号
if(m_bCurrentPageChangeEnabled)
{
emit current_page_changed(m_uiCurrentPageNum);
}
}
void APageTurnWidget::setIsFirstPage(bool bIsFirstPage)
{
m_bIsFirstPage = bIsFirstPage;
}
//
void APageTurnWidget::firstPage()
{
if(m_uiRecordCount == 0)
{
return;
}
m_uiCurrentPageNum = 1;
updateWidgetStatus();
//允许触发当前页改变信号
if(m_bCurrentPageChangeEnabled)
{
emit current_page_changed(m_uiCurrentPageNum);
}
}
//
quint32 APageTurnWidget::prePage()
{
if(m_uiCurrentPageNum <= 1)
{
return m_uiCurrentPageNum;
}
--m_uiCurrentPageNum;
updateWidgetStatus();
//允许触发当前页改变信号
if(m_bCurrentPageChangeEnabled)
{
emit current_page_changed(m_uiCurrentPageNum);
}
return m_uiCurrentPageNum;
}
//
quint32 APageTurnWidget::nextPage()
{
if(m_uiCurrentPageNum >= getPageCount())
{
return m_uiCurrentPageNum;
}
++m_uiCurrentPageNum;
updateWidgetStatus();
//允许触发当前页改变信号
if(m_bCurrentPageChangeEnabled)
{
emit current_page_changed(m_uiCurrentPageNum);
}
return m_uiCurrentPageNum;
}
//
void APageTurnWidget::lastPage()
{
m_uiCurrentPageNum = getPageCount();
updateWidgetStatus();
//允许触发当前页改变信号
if(m_bCurrentPageChangeEnabled)
{
emit current_page_changed(m_uiCurrentPageNum);
}
}
//返回总的记录数
quint32 APageTurnWidget::getRecordCount() const
{
return m_uiRecordCount;
}
//返回每页记录数
quint32 APageTurnWidget::getRecordCountPerPage() const
{
return m_uiRecordCountPerPage;
}
//返回页数
quint32 APageTurnWidget::getPageCount() const
{
if(m_uiRecordCountPerPage == 0)
{
return 0;
}
if(m_uiRecordCount%m_uiRecordCountPerPage > 0)
{
return m_uiRecordCount/m_uiRecordCountPerPage + 1;
}
else
{
return m_uiRecordCount/m_uiRecordCountPerPage;
}
}
//返回当前页号
quint32 APageTurnWidget::getCurrentPageNum() const
{
return m_uiCurrentPageNum;
}
bool APageTurnWidget::getIsFirstPage() const
{
return m_bIsFirstPage;
}
void APageTurnWidget::hideOtherButton()
{
m_pFirstButton->hide();
m_pGotoButton->hide();
m_pLastButton->hide();
m_pPageNumLineEdit->hide();
m_pPageInfoLabel->hide();
}
//
void APageTurnWidget::updateWidgetStatus()
{
if(m_uiRecordCount <= 1)
{
m_pFirstButton->setEnabled(false);
m_pPreButton->setEnabled(false);
m_pGotoButton->setEnabled(false);
m_pNextButton->setEnabled(false);
m_pLastButton->setEnabled(false);
}
else if(m_uiCurrentPageNum == 1)
{
m_pFirstButton->setEnabled(false);
m_pPreButton->setEnabled(true);
m_pGotoButton->setEnabled(true);
m_pNextButton->setEnabled(true);
m_pLastButton->setEnabled(true);
}
else if(m_uiCurrentPageNum == m_uiRecordCount)