444 lines
9.2 KiB
C++
444 lines
9.2 KiB
C++
#include "targetlistwgt.h"
|
||
|
||
#include <QFile>
|
||
#include <QToolButton>
|
||
#include <QCheckBox>
|
||
#include <QHBoxLayout>
|
||
#include <QDateTime>
|
||
#include <QMenu>
|
||
#include <qdebug.h>
|
||
|
||
#include <windows.h>
|
||
#include <windowsx.h>
|
||
|
||
#include "../DockTitleBar.h"
|
||
#include "../DockWidget.h"
|
||
|
||
#include <QMessageBox>
|
||
#include "common/SpdLogger.h"
|
||
#include "workspace/WorkSpace.h"
|
||
#include "workspace/Timestep.h"
|
||
#include "workspace/WorkSpaceManager.h"
|
||
|
||
TargetListWgt::TargetListWgt(QWidget * parent) : QWidget(parent) {
|
||
ui.setupUi(this);
|
||
|
||
setWindowTitle(QString::fromLocal8Bit("数据列表"));
|
||
setWindowFlags(/*Qt::FramelessWindowHint | */Qt::Window);
|
||
|
||
m_nCurTabPage = 0;
|
||
|
||
m_nBoundaryWth = 4;
|
||
|
||
QHeaderView* horizontalHeader = ui.tableWidget->horizontalHeader();
|
||
QColor headerColor = QColor(100, 100, 100); // 灰色
|
||
horizontalHeader->setStyleSheet(QString("QHeaderView::section {background-color: %1;}").arg(headerColor.name()));
|
||
horizontalHeader->setStretchLastSection(true);
|
||
|
||
InitWgt();
|
||
|
||
m_bMoveWgt = false;
|
||
|
||
ui.lineEdit->setText("1");
|
||
ui.lineEdit_2->setText("1");
|
||
ui.spinBox_2->setValue(1);
|
||
|
||
connect(&WorkSpaceManager::Get(), &WorkSpaceManager::WorkSpaceChanged, this, &TargetListWgt::OnWorkSpaceChanged);
|
||
}
|
||
|
||
TargetListWgt::~TargetListWgt() {
|
||
|
||
}
|
||
|
||
void TargetListWgt::AttachDock(DockWidget* dockWidget)
|
||
{
|
||
if (nullptr == dockWidget) {
|
||
qDebug() << __FUNCTION__ << "dockwidget is nullptr";
|
||
return;
|
||
}
|
||
|
||
dockWidget->SetDockWidgetTitleBar(nullptr);
|
||
dockWidget->setWidget(this);
|
||
|
||
DockTitleBar* dockTitleBar = new DockTitleBar;
|
||
|
||
dockTitleBar->SetTitle(tr("Data Table"));
|
||
|
||
dockWidget->SetDockWidgetTitleBar(dockTitleBar);
|
||
}
|
||
|
||
void TargetListWgt::SetHeader(const QStringList& headerLabels)
|
||
{
|
||
ui.tableWidget->clear();
|
||
|
||
ui.tableWidget->setColumnCount(headerLabels.size());
|
||
ui.tableWidget->setHorizontalHeaderLabels(headerLabels);
|
||
|
||
}
|
||
|
||
void TargetListWgt::AddRowData(QVariant& var)
|
||
{
|
||
if (!var.isValid())
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (var.type() != QVariant::List)
|
||
{
|
||
return;
|
||
}
|
||
|
||
QVariantList varRowList = var.toList();
|
||
if (varRowList.size() < ui.tableWidget->columnCount())
|
||
{
|
||
return;
|
||
}
|
||
|
||
m_varRowDataList.push_back(var);
|
||
|
||
UpdatePageNum();
|
||
|
||
int nCurPage = ui.lineEdit->text().toInt();
|
||
UpdatePageData(nCurPage);
|
||
}
|
||
|
||
void TargetListWgt::InitFile(const QString& strFile, int iBatchCount)
|
||
{
|
||
m_iBatchCount = iBatchCount;
|
||
m_strFile = strFile;
|
||
m_vecReportPoint.clear();
|
||
ParseReport(strFile, m_vecReportPoint, m_iBatchCount);
|
||
|
||
UpdateTable(1);
|
||
|
||
UpdatePageNum();
|
||
|
||
UpdatePageData(1);
|
||
}
|
||
|
||
void TargetListWgt::InitWgt()
|
||
{
|
||
ui.tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||
|
||
ui.tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||
|
||
ui.tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
|
||
|
||
//UpdateButtonEnable();
|
||
|
||
connect(ui.toolButton, SIGNAL(clicked()), this, SLOT(slotForwardPage()));
|
||
connect(ui.toolButton_2, SIGNAL(clicked()), this, SLOT(slotNextPage()));
|
||
|
||
connect(ui.spinBox, SIGNAL(valueChanged(int)),
|
||
this, SLOT(slotRowCountChanged(int))); // 行数改变
|
||
|
||
connect(ui.spinBox_2, SIGNAL(valueChanged(int)),
|
||
this, SLOT(slotSelPage(int))); // 跳转
|
||
|
||
connect(ui.tableWidget, SIGNAL( itemDoubleClicked (QTableWidgetItem *)),
|
||
this, SLOT(slotDoubleClickedItem(QTableWidgetItem *)));
|
||
|
||
|
||
connect(ui.tableWidget, SIGNAL(itemClicked(QTableWidgetItem *)),
|
||
this, SLOT(slotClickedItem(QTableWidgetItem *)));
|
||
|
||
|
||
connect(ui.tableWidget->horizontalHeader(),SIGNAL(sectionClicked(int)), this, SLOT(slotSortTabCol(int)));
|
||
}
|
||
|
||
void TargetListWgt::ParseReport(const QString& strFile, std::vector<std::vector<std::vector<float>>>& vecReportPoint, int iRowCount)
|
||
{
|
||
if (strFile.isEmpty())
|
||
{
|
||
QMessageBox::information(nullptr, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("请检查数据Report文件路径!"));
|
||
return;
|
||
}
|
||
|
||
QFile file(strFile);
|
||
if (file.open(QIODevice::ReadOnly))
|
||
{
|
||
int iRow = 1;
|
||
|
||
std::vector<std::vector<float>> batch;
|
||
while (!file.atEnd())
|
||
{
|
||
QString strLine = file.readLine().simplified();
|
||
if (!strLine.isEmpty())
|
||
{
|
||
QStringList listLine = strLine.split(" ");
|
||
std::vector<float> vecLine;
|
||
for (size_t i = 0; i < listLine.size(); i++)
|
||
{
|
||
vecLine.push_back(listLine[i].toFloat());
|
||
}
|
||
|
||
batch.push_back(vecLine);
|
||
}
|
||
|
||
// 批次
|
||
if (iRow % iRowCount == 0)
|
||
{
|
||
vecReportPoint.push_back(batch);
|
||
batch.clear();
|
||
}
|
||
|
||
iRow++;
|
||
}
|
||
|
||
file.close();
|
||
}
|
||
}
|
||
|
||
void TargetListWgt::slotSortTabCol(int nCol)
|
||
{
|
||
QHeaderView *pSender = (QHeaderView *)(sender());
|
||
QTableWidget *pParen = (QTableWidget *)(pSender->parent());
|
||
pParen->sortItems(nCol, Qt::DescendingOrder);
|
||
//pParen->sortItems(nCol, Qt::AscendingOrder);
|
||
|
||
}
|
||
|
||
void TargetListWgt::slotForwardPage()
|
||
{
|
||
int nCurPage = ui.lineEdit->text().toInt();
|
||
if (nCurPage == 1)
|
||
{
|
||
return;
|
||
}
|
||
|
||
nCurPage -= 1;
|
||
|
||
ui.lineEdit->setText(QString::number(nCurPage));
|
||
|
||
UpdatePageData(nCurPage);
|
||
}
|
||
|
||
void TargetListWgt::slotNextPage()
|
||
{
|
||
// 当前tab页显示的数据页数
|
||
int nCurPage = ui.lineEdit->text().toInt();
|
||
int iPageCount = ui.lineEdit_2->text().toInt();
|
||
if (nCurPage == iPageCount)
|
||
{
|
||
return;
|
||
}
|
||
|
||
nCurPage += 1; // 下一页
|
||
ui.lineEdit->setText(QString::number(nCurPage));
|
||
|
||
UpdatePageData(nCurPage);
|
||
}
|
||
|
||
void TargetListWgt::OnWorkSpaceChanged(WorkSpace* worksapce) {
|
||
if (worksapce == nullptr) {
|
||
LOG_ERROR("worksapce is nullptr");
|
||
return;
|
||
}
|
||
|
||
connect(worksapce, &WorkSpace::TimestepChanged, this, &TargetListWgt::OnTimestepChanged);
|
||
}
|
||
|
||
void TargetListWgt::OnTimestepChanged(Timestep* timestep) {
|
||
if (timestep == nullptr) {
|
||
LOG_ERROR("timestep is nullptr");
|
||
return;
|
||
}
|
||
connect(timestep, SIGNAL(TimeChanged(double)), this, SLOT(slotUpdateTime(double)));
|
||
}
|
||
|
||
void TargetListWgt::slotTimeChanged(double dTime)
|
||
{
|
||
UpdateTable(dTime);
|
||
UpdatePageData(1);
|
||
}
|
||
|
||
void TargetListWgt::slotDoubleClickedItem(QTableWidgetItem *pItem)
|
||
{
|
||
QTableWidget *pSender = (QTableWidget *)sender();
|
||
}
|
||
|
||
void TargetListWgt::slotClickedItem(QTableWidgetItem *pItem)
|
||
{
|
||
QTableWidget *pSender = (QTableWidget *)sender();
|
||
}
|
||
|
||
void TargetListWgt::timerEvent(QTimerEvent *event)
|
||
{
|
||
|
||
}
|
||
|
||
void TargetListWgt::mousePressEvent(QMouseEvent *event)
|
||
{
|
||
if (Qt::LeftButton == event->button())
|
||
{
|
||
m_bMoveWgt = true;
|
||
|
||
m_pStartPos =mapToGlobal(event->pos());
|
||
}
|
||
|
||
QWidget::mousePressEvent(event);
|
||
}
|
||
|
||
void TargetListWgt::mouseMoveEvent(QMouseEvent *event)
|
||
{
|
||
if (m_bMoveWgt)
|
||
{
|
||
QPoint mousePos = mapToGlobal(event->pos());
|
||
|
||
QPoint curPos = this->pos();
|
||
|
||
QPoint movePos = curPos + (mousePos - m_pStartPos);
|
||
|
||
this->move(movePos);
|
||
|
||
m_pStartPos = mousePos;
|
||
}
|
||
|
||
QWidget::mouseMoveEvent(event);
|
||
}
|
||
|
||
void TargetListWgt::mouseReleaseEvent(QMouseEvent *event)
|
||
{
|
||
if (Qt::LeftButton == event->button())
|
||
{
|
||
m_bMoveWgt = false;
|
||
}
|
||
|
||
QWidget::mouseReleaseEvent(event);
|
||
}
|
||
|
||
void TargetListWgt::closeEvent(QCloseEvent *event)
|
||
{
|
||
|
||
}
|
||
|
||
void TargetListWgt::UpdateTable(int iTime)
|
||
{
|
||
if (iTime < 1)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if ((iTime - 1) >= m_vecReportPoint.size())
|
||
{
|
||
return;
|
||
}
|
||
|
||
m_varRowDataList.clear();
|
||
|
||
if (iTime-1 < m_vecReportPoint.size())
|
||
{
|
||
std::vector<std::vector<float>> batch = m_vecReportPoint[iTime-1];
|
||
|
||
ui.tableWidget->setRowCount(0);
|
||
|
||
for (int i = 0; i < batch.size(); i++)
|
||
{
|
||
ui.tableWidget->insertRow(i);
|
||
|
||
std::vector<float> rowData = batch[i];
|
||
|
||
QVariantList varRow;
|
||
for (int j = 0; j < ui.tableWidget->columnCount(); j++)
|
||
{
|
||
if (rowData.size() > j)
|
||
{
|
||
varRow.push_back(rowData[j]);
|
||
}
|
||
}
|
||
|
||
m_varRowDataList.push_back(varRow);
|
||
}
|
||
}
|
||
}
|
||
|
||
void TargetListWgt::UpdatePageNum()
|
||
{
|
||
m_nPageShowLineNum = ui.spinBox->value(); // 每页显示的行数
|
||
|
||
int iPageCount = 1;
|
||
|
||
int iDataCount = m_varRowDataList.size();
|
||
if (iDataCount > m_nPageShowLineNum)
|
||
{
|
||
iPageCount = m_varRowDataList.size() / m_nPageShowLineNum;
|
||
if (m_varRowDataList.size() % m_nPageShowLineNum > 0)
|
||
{
|
||
iPageCount++;
|
||
}
|
||
}
|
||
|
||
ui.spinBox_2->setRange(1, iPageCount);
|
||
ui.lineEdit_2->setText(QString::number(iPageCount));
|
||
}
|
||
|
||
void TargetListWgt::UpdateButtonEnable()
|
||
{
|
||
m_nPageShowLineNum = ui.spinBox->value(); // 每页显示的行数
|
||
|
||
// 当前tab页显示的数据页数
|
||
int nCurPage = 1;
|
||
|
||
if (1 == nCurPage) // 首页
|
||
{
|
||
ui.toolButton->setDisabled(true); // 前一页按钮
|
||
}
|
||
else
|
||
{
|
||
ui.toolButton->setDisabled(false); // 前一页按钮
|
||
}
|
||
|
||
// 总页数
|
||
int nPageCount = 1;
|
||
if (nCurPage == nPageCount) // 最后一页
|
||
{
|
||
ui.toolButton_2->setDisabled(true); // 后一页按钮
|
||
}
|
||
else
|
||
{
|
||
ui.toolButton_2->setDisabled(false); // 后一页按钮
|
||
}
|
||
|
||
// 当前页码数
|
||
ui.lineEdit->setText(QString::number(nCurPage));
|
||
// 总页码数
|
||
ui.lineEdit_2->setText(QString::number(nPageCount));
|
||
|
||
// 设置页码数范围
|
||
ui.spinBox_2->setRange(1,nPageCount);
|
||
ui.spinBox_2->setValue(nCurPage);
|
||
}
|
||
|
||
void TargetListWgt::UpdatePageData(int iPageNum)
|
||
{
|
||
m_nPageShowLineNum = ui.spinBox->value(); // 每页显示的行数
|
||
|
||
int iIndex = (iPageNum-1) * m_nPageShowLineNum; // 每页显示的行数
|
||
int iEndIndex = iIndex + m_nPageShowLineNum;
|
||
if (iEndIndex > m_varRowDataList.size())
|
||
{
|
||
iEndIndex = m_varRowDataList.size();
|
||
}
|
||
|
||
while (ui.tableWidget->rowCount() > 0)
|
||
{
|
||
ui.tableWidget->removeRow(0);
|
||
}
|
||
|
||
int iRow = 0;
|
||
|
||
for (; iIndex < iEndIndex; iIndex++)
|
||
{
|
||
QVariantList varColList = m_varRowDataList[iIndex].toList();
|
||
|
||
ui.tableWidget->insertRow(iRow);
|
||
|
||
for (int iCol = 0; iCol < varColList.size(); iCol++)
|
||
{
|
||
ui.tableWidget->setItem(iRow, iCol, new QTableWidgetItem(varColList[iCol].toString()));
|
||
}
|
||
|
||
iRow++;
|
||
}
|
||
}
|