Files
pcdefenser-ui/tablepagewidget.cpp
2024-04-07 19:16:05 +08:00

226 lines
6.2 KiB
C++
Executable File

#include "tablepagewidget.h"
#include "ui_tablepagewidget.h"
#include <QWidget>
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
TablePageWidget::TablePageWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::TablePageWidget),
pageLine(10),
pageTotal(0),
currentPage(0),
currentLine(0),
currentFivePage(0),
TotalFivePage(0)
{
ui->setupUi(this);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableWidget->verticalHeader()->setHidden(true);
initForm();
}
TablePageWidget::~TablePageWidget()
{
delete ui;
}
void TablePageWidget::setTableHeaders(QStringList headerList)
{
for(int i = 0; i < headerList.size(); i++)
ui->tableWidget->insertColumn(i);
for(int i = 0; i < headerList.size(); i++)
ui->tableWidget->setHorizontalHeaderItem(i, new QTableWidgetItem(headerList.at(i)));
}
void TablePageWidget::setTableData(QStringList dataList)
{
this->dataList = dataList;
int col = ui->tableWidget->columnCount();
int row = dataList.size() / col;
pageTotal = row % pageLine;
pageTotal = pageTotal + 1;
currentPage = 1;
currentLine = 0;
TotalFivePage = (pageTotal / 5 > 0) ? 0 : (pageTotal / 5 + 2);
currentFivePage = 1;
for(int i = currentLine; i < currentPage * pageLine; i++)
{
for(int j = 0; j < col; j++)
ui->tableWidget->setItem(i, j, new QTableWidgetItem(this->dataList.at(currentLine * col + j)));
}
if(pageTotal > 5)
{
foreach (QAbstractButton* btn, this->findChildren<QAbstractButton *>()) {
if(!(btn->text() == "<"))
btn->setEnabled(true);
}
}
else if(pageTotal <= 5)
{
foreach (QAbstractButton* btn, this->findChildren<QAbstractButton *>()){
if(btn->text().toInt() <= pageTotal)
btn->setEnabled(true);
}
}
}
void TablePageWidget::appendTableData(QStringList newDataList)
{
this->dataList<< newDataList;
}
void TablePageWidget::changeTablePageLine(int idx)
{
switch (idx) {
case 0:
this->pageLine = 10;
break;
case 1:
this->pageLine = 50;
break;
case 2:
this->pageLine = 80;
break;
case 3:
this->pageLine = 100;
break;
}
if(dataList.isEmpty())
return;
pageTotal = dataList.size() / currentLine;
TotalFivePage = (pageTotal / 5 > 0) ? (pageTotal / 5 + 1) : 0;
if(pageTotal < currentPage)
{
currentPage == pageTotal;
if(currentPage % 5 == 0 && currentPage >= 5)
currentFivePage = currentPage / 5;
else if(currentPage % 5 != 0)
currentFivePage = currentPage / 5 + 1;
else if(currentPage < 5)
currentFivePage = 1;
setCurrentFivePage(this->currentFivePage);
}
setCurrentData();
}
void TablePageWidget::on_toolbuttonBehind_clicked()
{
this->currentFivePage++;
setCurrentFivePage(currentFivePage);
}
void TablePageWidget::on_toolbuttonFront_clicked()
{
if(currentFivePage > 1)
currentFivePage--;
setCurrentFivePage(currentFivePage);
}
void TablePageWidget::on_toolbutton1_clicked()
{
currentPage = ui->btn1->text().toInt();
setCurrentData();
}
void TablePageWidget::on_toolbutton2_clicked()
{
currentPage = ui->btn2->text().toInt();
setCurrentData();
}
void TablePageWidget::on_toolbutton3_clicked()
{
currentPage = ui->btn3->text().toInt();
setCurrentData();
}
void TablePageWidget::on_toolbutton4_clicked()
{
currentPage = ui->btn4->text().toInt();
setCurrentData();
}
void TablePageWidget::on_toolbutton5_clicked()
{
currentPage = ui->btn5->text().toInt();
setCurrentData();
}
void TablePageWidget::initForm()
{
QStringList pageNum;
pageNum.append("10条/页");
pageNum.append("50条/页");
pageNum.append("80条/页");
pageNum.append("100条/页");
ui->comboBox->addItems(pageNum);
foreach (QAbstractButton* btn, this->findChildren<QAbstractButton *>()) {
btn->setEnabled(false);
}
this->dataList.clear();
connect(ui->comboBox, SIGNAL(activated(int)), this, SLOT(changeTablePageLine(int)));
connect(ui->btnFront, &QToolButton::clicked, this, &TablePageWidget::on_toolbuttonFront_clicked);
connect(ui->btnBehind, &QToolButton::clicked, this, &TablePageWidget::on_toolbuttonBehind_clicked);
}
void TablePageWidget::setCurrentFivePage(int idx)
{
ui->btn1->setText(QString::number(5 * (idx - 1) + 1));
ui->btn2->setText(QString::number(5 * (idx - 1) + 2));
ui->btn2->setText(QString::number(5 * (idx - 1) + 3));
ui->btn4->setText(QString::number(5 * (idx - 1) + 4));
ui->btn5->setText(QString::number(5 * (idx - 1) + 5));
if(TotalFivePage > ui->btn5->text().toInt() && currentFivePage != 1)
{
foreach (QAbstractButton* btn, this->findChildren<QAbstractButton *>()) {
btn->setEnabled(true);
if(btn->text().toInt() == currentPage)
btn->setChecked(true);
else
btn->setChecked(false);
}
}
else if(TotalFivePage > ui->btn5->text().toInt() && currentFivePage == 1)
{
foreach (QAbstractButton* btn, this->findChildren<QAbstractButton *>()) {
if(btn != ui->btnFront)
btn->setEnabled(true);
else
btn->setEnabled(false);
if(btn->text().toInt() == currentPage)
btn->setChecked(true);
else
btn->setChecked(false);
}
}
}
void TablePageWidget::setCurrentData()
{
if(!this->dataList.isEmpty())
{
currentLine = pageLine * (currentPage - 1);
for(int i = currentLine; i < currentPage * pageLine; i++)
{
for(int j = 0; j < ui->tableWidget->colorCount(); j++)
ui->tableWidget->setItem(currentLine - (pageLine * currentPage),
j, new QTableWidgetItem(this->dataList.at(currentLine * ui->tableWidget->columnCount() + j)));
}
}
}