505 lines
14 KiB
C++
505 lines
14 KiB
C++
#include "devicepropertypage.h"
|
||
|
||
#include <QGuiApplication>
|
||
#include <QTimer>
|
||
#include <QMessageBox>
|
||
#include <QTableView>
|
||
#include <QVBoxLayout>
|
||
#include <QPushButton>
|
||
#include <QScrollBar>
|
||
#include <QHeaderView>
|
||
#include <QSpinBox>
|
||
#include <QDebug>
|
||
|
||
#include "globalparameters.h"
|
||
#include "mytablemodel.h"
|
||
|
||
#define PAGES_FORMAT_STRING ("Total %1 Pages, Current No.%2 Page")
|
||
#define PER_PAGE_SIZE (30)
|
||
|
||
DevicePropertyPage::DevicePropertyPage(QWidget *parent) :
|
||
QWidget(parent),m_nDownCounter(0),m_pTableView(nullptr),m_pButton(nullptr),m_totalPageCount(0),m_currentPage(0)
|
||
{
|
||
InitializeTable();
|
||
|
||
m_pTimer = new QTimer(this);
|
||
connect(m_pTimer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
|
||
//m_pTimer->start(AppData::getInstance()->nTimeOut);
|
||
|
||
m_pDownCounterTimer = new QTimer(this);
|
||
connect(m_pDownCounterTimer, SIGNAL(timeout()), this, SLOT(handleDownCounter()));
|
||
|
||
updatePageButtonState();
|
||
updatePageLabel();
|
||
}
|
||
|
||
DevicePropertyPage::~DevicePropertyPage()
|
||
{
|
||
}
|
||
|
||
void DevicePropertyPage::handleTimeout()
|
||
{
|
||
if(m_pTimer->isActive())
|
||
{
|
||
Refresh();
|
||
}
|
||
}
|
||
|
||
void DevicePropertyPage::handleDownCounter()
|
||
{
|
||
m_nDownCounter--;
|
||
m_pDownCounterLabel->setText(QString("%1").arg(m_nDownCounter));
|
||
if (m_nDownCounter == 0)
|
||
m_nDownCounter = m_pTimerIntervalSpinBox->value();
|
||
}
|
||
|
||
void DevicePropertyPage::InitializeTableView(MyTableModel *model, QTableView *tableView)
|
||
{
|
||
//设置tableview的model
|
||
model->setHeadData(AppData::getInstance()->lstDataTableHeaderText);
|
||
tableView->setModel(model);
|
||
|
||
QHeaderView* pHeaderView = tableView->horizontalHeader();
|
||
//pHeaderView->setStyleSheet("QHeaderView::section {color: black;padding-left: 4px;border: 1px solid #6c6c6c;}");
|
||
pHeaderView->setStyleSheet("QHeaderView::section{background:lightgray;}");
|
||
|
||
// pHeaderView->setSectionResizeMode(QHeaderView::Stretch); //Stretch
|
||
|
||
pHeaderView->setHidden(false); //false 显示行号列 true Hide
|
||
|
||
pHeaderView->setStretchLastSection(true); //设置充满表宽度
|
||
|
||
//pHeaderView->setVisible(true);
|
||
//pHeaderView->setFixedHeight(40);
|
||
|
||
//点击表时不对表头行光亮(获取焦点)
|
||
pHeaderView->setHighlightSections(false);
|
||
// pHeaderView->setDefaultSectionSize(200);
|
||
|
||
// //设置表头字体加粗
|
||
// QFont font = pHeaderView->font();
|
||
// font.setBold(true);
|
||
// pHeaderView->setFont(font);
|
||
|
||
//设置表头字体
|
||
pHeaderView->setFont(QFont("Arial", 12));
|
||
|
||
// 设置表头列宽自动调整
|
||
for (int i = 0; i < model->columnCount(); ++i)
|
||
{
|
||
pHeaderView->setSectionResizeMode(i, QHeaderView::Interactive);
|
||
}
|
||
|
||
// 允许用户通过拖动表头边缘调整列宽
|
||
//pHeaderView->setSectionResizeMode(QHeaderView::Interactive);
|
||
|
||
tableView->verticalHeader()->setDefaultSectionSize(30); //行高
|
||
|
||
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
|
||
|
||
tableView->setAlternatingRowColors(true);
|
||
tableView->setTextElideMode(Qt::ElideMiddle);
|
||
|
||
|
||
//所有单元格的字体 设置成一样
|
||
tableView->setFont(QFont("Arial", 9));
|
||
|
||
//设置表格数据区内的所有单元格都不允许编辑
|
||
tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||
|
||
#if 1
|
||
//设置列宽
|
||
int base = 120; //w / (model->columnCount()-1);
|
||
tableView->setColumnWidth(0,base-30);
|
||
tableView->setColumnWidth(1,base*5);
|
||
tableView->setColumnWidth(2,base);
|
||
tableView->setColumnWidth(3,base-40);
|
||
tableView->setColumnWidth(4,base+60);
|
||
tableView->setColumnWidth(5,base+60);
|
||
#endif
|
||
|
||
tableView->show();
|
||
}
|
||
|
||
void DevicePropertyPage::InitializeTable()
|
||
{
|
||
m_myModel = new MyTableModel(this);
|
||
m_myModel->setHeadData(AppData::getInstance()->lstDataTableHeaderText);
|
||
|
||
m_pTableView = new QTableView(this);
|
||
|
||
//m_pButton = new QPushButton(tr("Refresh"), this);
|
||
// 设置固定大小和位置的按钮
|
||
//m_pButton->setFixedSize(100, 30); // 设置按钮的固定大小
|
||
|
||
m_pFirstButton = new QPushButton(tr("First"), this);
|
||
m_pForwardButton = new QPushButton(tr("Forward"), this);
|
||
m_pNextButton = new QPushButton(tr("Next"), this);
|
||
m_pLastButton = new QPushButton(tr("Last"), this);
|
||
|
||
m_pFirstButton->setFixedSize(80, 30); // 设置按钮的固定大小
|
||
m_pForwardButton->setFixedSize(80, 30); // 设置按钮的固定大小
|
||
m_pNextButton->setFixedSize(80, 30); // 设置按钮的固定大小
|
||
m_pLastButton->setFixedSize(80, 30); // 设置按钮的固定大小
|
||
|
||
m_pGotoPageNumberLineEdit = new QLineEdit(tr("1"), this);
|
||
m_pGotoPageNumberLineEdit->setFixedWidth(40);
|
||
m_pValidator = new QIntValidator(this);
|
||
m_pGotoPageNumberLineEdit->setValidator(m_pValidator);
|
||
m_pValidator->setRange(0, 100);
|
||
|
||
m_pGotoPageNumberLabel = new QLabel(tr("Page"), this);
|
||
m_pGotoPageNumberButton = new QPushButton(tr("Go"), this);
|
||
m_pGotoPageNumberButton->setFixedSize(40, 30);
|
||
|
||
m_pPagesLabel = new QLabel(tr("Total %d Pages, Current No.%d Page"), this);
|
||
|
||
// 设置字体为粗体
|
||
QFont font = m_pPagesLabel->font();
|
||
font.setBold(true); // 设置为粗体
|
||
m_pPagesLabel->setFont(font);
|
||
|
||
// 创建一个QSpinBox,范围为10到100
|
||
m_pTimerIntervalSpinBox = new QSpinBox(this);
|
||
m_pTimerIntervalSpinBox->setRange(15, 300); // 设置范围为10到100
|
||
m_pTimerIntervalSpinBox->setValue(15); // 默认值设为10
|
||
|
||
m_pRefreshCheckBox = new QCheckBox(tr("Auto Refresh"), this);
|
||
m_pDownCounterLabel = new QLabel(tr("0"), this);
|
||
|
||
m_pDownCounterLabel->setFont(font);
|
||
|
||
// 创建主布局
|
||
QVBoxLayout *mainLayout = new QVBoxLayout(this);
|
||
|
||
InitializeTableView(m_myModel,m_pTableView);
|
||
|
||
mainLayout->addWidget(m_pTableView);
|
||
|
||
// 创建一个水平布局来包含按钮
|
||
QHBoxLayout *buttonLayout0 = new QHBoxLayout();
|
||
//buttonLayout0->addStretch(); // 让按钮保持在右侧
|
||
buttonLayout0->setContentsMargins(0, 0, 0, 0); // 取消按钮的边距
|
||
|
||
// 添加标签、QSpinBox到布局
|
||
buttonLayout0->addWidget(new QLabel(tr("Interval(Sec.)"), this));
|
||
buttonLayout0->addWidget(m_pTimerIntervalSpinBox);
|
||
buttonLayout0->addWidget(m_pRefreshCheckBox);
|
||
buttonLayout0->addWidget(m_pDownCounterLabel);
|
||
m_pDownCounterLabel->setVisible(false);
|
||
|
||
|
||
QSpacerItem *spacer2 = new QSpacerItem(10, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
|
||
buttonLayout0->addItem(spacer2);
|
||
|
||
// 创建一个水平布局来包含按钮
|
||
QHBoxLayout *buttonLayout = new QHBoxLayout();
|
||
//buttonLayout->addStretch(); // 让按钮保持在右侧
|
||
|
||
buttonLayout->addWidget(m_pPagesLabel);
|
||
QSpacerItem *spacer0 = new QSpacerItem(10, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
|
||
buttonLayout->addItem(spacer0);
|
||
|
||
|
||
buttonLayout->addWidget(m_pFirstButton);
|
||
buttonLayout->addWidget(m_pForwardButton);
|
||
buttonLayout->addWidget(m_pNextButton);
|
||
buttonLayout->addWidget(m_pLastButton);
|
||
|
||
QSpacerItem *spacer = new QSpacerItem(10, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
|
||
buttonLayout->addItem(spacer);
|
||
|
||
buttonLayout->addWidget(m_pGotoPageNumberLabel);
|
||
buttonLayout->addWidget(m_pGotoPageNumberLineEdit);
|
||
buttonLayout->addWidget(m_pGotoPageNumberButton);
|
||
|
||
//buttonLayout->addWidget(m_pButton);
|
||
|
||
buttonLayout->setContentsMargins(0, 0, 0, 0); // 取消按钮的边距
|
||
|
||
// 将表格视图和按钮布局添加到主布局
|
||
mainLayout->addLayout(buttonLayout0);
|
||
mainLayout->addLayout(buttonLayout);
|
||
|
||
mainLayout->setContentsMargins(0, 0, 0, 0); // 取消主布局的边距
|
||
mainLayout->setSpacing(10); // 取消布局间的间距
|
||
|
||
|
||
// 连接信号和槽
|
||
connect(m_pTableView, &QTableView::doubleClicked, this, &DevicePropertyPage::onTableViewDoubleClicked);
|
||
connect(m_pButton, &QPushButton::clicked, this, &DevicePropertyPage::onButtonClicked);
|
||
|
||
connect(m_pRefreshCheckBox, &QCheckBox::stateChanged, this, &DevicePropertyPage::CheckRefresh);
|
||
|
||
connect(m_pFirstButton, &QPushButton::clicked, this, &DevicePropertyPage::onFirstButtonClicked);
|
||
connect(m_pLastButton, &QPushButton::clicked, this, &DevicePropertyPage::onLastButtonClicked);
|
||
connect(m_pForwardButton, &QPushButton::clicked, this, &DevicePropertyPage::onForwardButtonClicked);
|
||
connect(m_pNextButton, &QPushButton::clicked, this, &DevicePropertyPage::onNextButtonClicked);
|
||
connect(m_pGotoPageNumberButton, &QPushButton::clicked, this, &DevicePropertyPage::onGotoButtonClicked);
|
||
|
||
setLayout(mainLayout);
|
||
}
|
||
|
||
void DevicePropertyPage::CheckRefresh(int state)
|
||
{
|
||
if (state == Qt::Checked)
|
||
{
|
||
qDebug() << "Checkbox is checked";
|
||
|
||
Refresh();
|
||
|
||
pageTo(1);
|
||
|
||
int nInterver = m_pTimerIntervalSpinBox->value() * 1000;
|
||
m_pTimer->start(nInterver);
|
||
|
||
m_nDownCounter = m_pTimerIntervalSpinBox->value();
|
||
m_pDownCounterLabel->setText(QString("%1").arg(m_nDownCounter));
|
||
|
||
m_pDownCounterTimer->start(1000);
|
||
m_pDownCounterLabel->setVisible(true);
|
||
|
||
}
|
||
else if (state == Qt::Unchecked)
|
||
{
|
||
qDebug() << "Checkbox is unchecked";
|
||
if (m_pTimer->isActive())
|
||
{
|
||
m_pTimer->stop();
|
||
}
|
||
if (m_pDownCounterTimer->isActive())
|
||
{
|
||
m_pDownCounterTimer->stop();
|
||
};
|
||
|
||
m_pDownCounterLabel->setVisible(false);
|
||
m_pDownCounterLabel->setText("0");
|
||
}
|
||
else
|
||
{
|
||
qDebug() << "Checkbox is in an indeterminate state";
|
||
}
|
||
}
|
||
void DevicePropertyPage::setBaseType(unsigned int base, unsigned int mask)
|
||
{
|
||
filterBaseType = base;
|
||
mask_code = mask;
|
||
}
|
||
|
||
void DevicePropertyPage::onButtonClicked()
|
||
{
|
||
m_pTimer->stop();
|
||
|
||
Refresh();
|
||
}
|
||
|
||
void DevicePropertyPage::Refresh()
|
||
{
|
||
QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
|
||
m_TotalTableData.clear();
|
||
if (Fetch(m_TotalTableData,106))
|
||
{
|
||
PageDataProcess();
|
||
m_myModel->setModelData(m_currentPageData);
|
||
}
|
||
updatePageButtonState();
|
||
updatePageLabel();
|
||
QGuiApplication::restoreOverrideCursor();
|
||
}
|
||
|
||
void DevicePropertyPage::keyPressEvent(QKeyEvent *event)
|
||
{
|
||
// 检查是否按下特定按键
|
||
switch(event->key())
|
||
{
|
||
case Qt::Key_PageDown:
|
||
{
|
||
if (m_currentPage < m_totalPageCount)
|
||
onNextButtonClicked();
|
||
break;
|
||
}
|
||
|
||
case Qt::Key_PageUp:
|
||
{
|
||
if (m_currentPage > 1)
|
||
onForwardButtonClicked();
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 处理其他按键,调用基类的 keyPressEvent
|
||
QWidget::keyPressEvent(event);
|
||
}
|
||
|
||
int DevicePropertyPage::PageDataProcess()
|
||
{
|
||
//获取数据总数
|
||
m_totalRows = m_TotalTableData.size();
|
||
|
||
m_totalPageCount = 1;
|
||
m_currentPage = 1;
|
||
|
||
m_currentPageData.clear();
|
||
|
||
if (m_totalRows <= PER_PAGE_SIZE)
|
||
{
|
||
m_currentPageData = m_TotalTableData;
|
||
|
||
return 1;
|
||
}
|
||
|
||
m_totalPageCount = int(m_totalRows / PER_PAGE_SIZE) + 1;
|
||
m_currentPageData = m_TotalTableData.mid(0, PER_PAGE_SIZE);
|
||
|
||
return 1;
|
||
}
|
||
|
||
void DevicePropertyPage::updatePageLabel()
|
||
{
|
||
QString s = QString(PAGES_FORMAT_STRING).arg(m_totalPageCount).arg(m_currentPage);
|
||
m_pPagesLabel->setText(s);
|
||
}
|
||
|
||
void DevicePropertyPage::updatePageButtonState()
|
||
{
|
||
if ( m_currentPage == m_totalPageCount) //翻到底了,超过最大的页数
|
||
{
|
||
m_pNextButton->setEnabled(false);
|
||
m_pForwardButton->setEnabled(true);
|
||
m_pLastButton->setEnabled(false);
|
||
m_pFirstButton->setEnabled(true);
|
||
}
|
||
else if (m_currentPage == 1) //翻到头了
|
||
{
|
||
m_pNextButton->setEnabled(true);
|
||
m_pForwardButton->setEnabled(false);
|
||
m_pLastButton->setEnabled(true);
|
||
m_pFirstButton->setEnabled(false);
|
||
}
|
||
else if (m_currentPage < m_totalPageCount) //页数在中间状态
|
||
{
|
||
m_pNextButton->setEnabled(true);
|
||
m_pForwardButton->setEnabled(true);
|
||
m_pLastButton->setEnabled(true);
|
||
m_pFirstButton->setEnabled(true);
|
||
}
|
||
else
|
||
{
|
||
assert(false);
|
||
}
|
||
}
|
||
|
||
void DevicePropertyPage::pageTo(int page)
|
||
{
|
||
m_currentPageData.clear();
|
||
int numLeftElements = m_totalRows - (page - 1) * PER_PAGE_SIZE;
|
||
int numElements = numLeftElements >= PER_PAGE_SIZE ? PER_PAGE_SIZE : numLeftElements;
|
||
m_currentPageData = m_TotalTableData.mid((page-1) * PER_PAGE_SIZE, numElements);
|
||
m_myModel->setModelData(m_currentPageData);
|
||
}
|
||
|
||
void DevicePropertyPage::onTableViewDoubleClicked(const QModelIndex &index)
|
||
{
|
||
//QAbstractItemModel *model=ui->tableView->model();
|
||
MyTableModel *model = (MyTableModel *)m_pTableView->model();
|
||
QModelIndex mindex = model->index(index.row(),7); //index.row()为算选择的行号。7为所选中行的第8列。。
|
||
QVariant datatemp=model->data(mindex);
|
||
}
|
||
|
||
void DevicePropertyPage::onFirstButtonClicked()
|
||
{
|
||
m_currentPage = 1;
|
||
|
||
updatePageButtonState();
|
||
|
||
pageTo(m_currentPage);
|
||
|
||
updatePageLabel();
|
||
}
|
||
|
||
void DevicePropertyPage::onLastButtonClicked()
|
||
{
|
||
m_currentPage = m_totalPageCount;
|
||
|
||
updatePageButtonState();
|
||
|
||
pageTo(m_currentPage);
|
||
|
||
updatePageLabel();
|
||
}
|
||
|
||
void DevicePropertyPage::onForwardButtonClicked()
|
||
{
|
||
m_currentPage--;
|
||
|
||
updatePageButtonState();
|
||
|
||
m_currentPageData.clear();
|
||
|
||
pageTo(m_currentPage);
|
||
|
||
updatePageLabel();
|
||
}
|
||
|
||
void DevicePropertyPage::onNextButtonClicked()
|
||
{
|
||
m_currentPage++;
|
||
|
||
updatePageButtonState();
|
||
|
||
m_currentPageData.clear();
|
||
|
||
pageTo(m_currentPage);
|
||
|
||
updatePageLabel();
|
||
}
|
||
|
||
void DevicePropertyPage::onGotoButtonClicked()
|
||
{
|
||
int page = m_pGotoPageNumberLineEdit->text().toInt();
|
||
if (page <=0 || page>m_totalPageCount)
|
||
{
|
||
QString s = QString(tr("Page number should be large than 0 and less than %1 !")).arg(m_totalPageCount);
|
||
QMessageBox::critical(this, tr("Critical Message"),s);
|
||
return ;
|
||
}
|
||
|
||
m_currentPage = page;
|
||
|
||
updatePageButtonState();
|
||
|
||
m_currentPageData.clear();
|
||
|
||
pageTo(m_currentPage);
|
||
|
||
updatePageLabel();
|
||
}
|
||
|
||
void DevicePropertyPage::resizeEvent(QResizeEvent *event)
|
||
{
|
||
QWidget::resizeEvent(event);
|
||
#if 0
|
||
// 获取当前QTableView的总宽度
|
||
int currentTotalWidth = m_pTableView->viewport()->width();
|
||
|
||
// 计算总的初始宽度
|
||
int initialTotalWidth = 0;
|
||
for (int width : columnWidths)
|
||
{
|
||
initialTotalWidth += width;
|
||
}
|
||
|
||
// 计算比例因子
|
||
double scaleFactor = static_cast<double>(currentTotalWidth) / initialTotalWidth;
|
||
|
||
// 重新设置列宽,按比例调整
|
||
for (int i = 0; i < columnWidths.size(); ++i)
|
||
{
|
||
int newWidth = static_cast<int>(columnWidths[i] * scaleFactor);
|
||
m_pTableView->setColumnWidth(i, newWidth);
|
||
}
|
||
#endif
|
||
}
|
||
|