|
|
#include "nmWxWellboreStorageCalculatorVolume.h"
|
|
|
|
|
|
#include <QTableWidget>
|
|
|
#include <QPushButton>
|
|
|
#include <QLabel>
|
|
|
#include <QLineEdit>
|
|
|
#include <QVBoxLayout>
|
|
|
#include <QHBoxLayout>
|
|
|
#include <QHeaderView>
|
|
|
#include <QCoreApplication>
|
|
|
#include <QComboBox>
|
|
|
#include <cmath>
|
|
|
|
|
|
nmWxWellboreStorageCalculatorVolume::nmWxWellboreStorageCalculatorVolume(QWidget *parent) : iDlgBase(parent)
|
|
|
{
|
|
|
this->initUI(); // 初始化用户界面
|
|
|
this->initConnections(); // 连接信号和槽
|
|
|
}
|
|
|
|
|
|
nmWxWellboreStorageCalculatorVolume::~nmWxWellboreStorageCalculatorVolume()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::initUI()
|
|
|
{
|
|
|
setWindowTitle(tr("Tested chamber volume"));
|
|
|
setFixedSize(450, 350);
|
|
|
|
|
|
// 1. 主布局
|
|
|
QVBoxLayout* mainLayout = new QVBoxLayout(this);
|
|
|
|
|
|
// 2. 表格和右侧按钮布局
|
|
|
QHBoxLayout* tableAndButtonsLayout = new QHBoxLayout;
|
|
|
|
|
|
m_pTableWidget = new QTableWidget(this);
|
|
|
m_pTableWidget->setColumnCount(2);
|
|
|
m_pTableWidget->setHorizontalHeaderLabels(QStringList() << tr("Height") << tr("I.D."));
|
|
|
|
|
|
// 设置一个自定义表头,其中第一行是单位
|
|
|
m_pTableWidget->insertRow(0);
|
|
|
|
|
|
// 为第一行的垂直表头(行号)创建一个 QTableWidgetItem 并设置为空文本
|
|
|
m_pTableWidget->setVerticalHeaderItem(0, new QTableWidgetItem());
|
|
|
m_pTableWidget->verticalHeaderItem(0)->setText("");
|
|
|
|
|
|
m_pHeightUnitCombo = new QComboBox;
|
|
|
m_pIDUnitCombo = new QComboBox;
|
|
|
m_pHeightUnitCombo->addItems(QStringList() << "m" ); // << "cm" << "mm" << "in" << "0.1 in" << "ft" << "mile");
|
|
|
m_pIDUnitCombo->addItems(QStringList() << "m" ); // << "cm" << "mm" << "in" << "0.1 in" << "ft" << "mile");
|
|
|
|
|
|
// 设置下拉框样式表,去除边框和箭头
|
|
|
QString comboStyle = "QComboBox {"
|
|
|
"border: 0px solid transparent;"
|
|
|
"background-color: transparent;"
|
|
|
"}"
|
|
|
"QComboBox::drop-down {"
|
|
|
"border: 0px;"
|
|
|
"background-color: transparent;"
|
|
|
"}"
|
|
|
"QComboBox::down-arrow {"
|
|
|
"image: url(none);"
|
|
|
"}"
|
|
|
"QComboBox:!editable, QComboBox::drop-down:editable {"
|
|
|
"background-color: transparent;"
|
|
|
"}";
|
|
|
|
|
|
m_pHeightUnitCombo->setStyleSheet(comboStyle);
|
|
|
m_pIDUnitCombo->setStyleSheet(comboStyle);
|
|
|
|
|
|
// 将下拉框放置在表格的第一行,即单位行
|
|
|
m_pTableWidget->setCellWidget(0, 0, m_pHeightUnitCombo);
|
|
|
m_pTableWidget->setCellWidget(0, 1, m_pIDUnitCombo);
|
|
|
|
|
|
// 设置垂直表头,用于显示行号
|
|
|
|
|
|
QHeaderView* verticalHeader = m_pTableWidget->verticalHeader();
|
|
|
verticalHeader->setFixedWidth(40);
|
|
|
verticalHeader->setResizeMode(QHeaderView::ResizeToContents);
|
|
|
verticalHeader->show();
|
|
|
m_pTableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
|
|
|
|
|
|
m_pAddButton = new QPushButton(tr("Add"));
|
|
|
m_pInsertButton = new QPushButton(tr("Insert"));
|
|
|
m_pDeleteButton = new QPushButton(tr("Delete"));
|
|
|
|
|
|
QVBoxLayout* sideButtonsLayout = new QVBoxLayout;
|
|
|
sideButtonsLayout->addWidget(m_pAddButton);
|
|
|
sideButtonsLayout->addWidget(m_pInsertButton);
|
|
|
sideButtonsLayout->addWidget(m_pDeleteButton);
|
|
|
sideButtonsLayout->addStretch();
|
|
|
|
|
|
tableAndButtonsLayout->addWidget(m_pTableWidget);
|
|
|
tableAndButtonsLayout->addLayout(sideButtonsLayout);
|
|
|
|
|
|
// 3. 体积显示区域布局
|
|
|
QHBoxLayout* volumeLayout = new QHBoxLayout;
|
|
|
m_pVolumeLabel = new QLabel(tr("Volume:"));
|
|
|
m_pVolumeEdit = new QLineEdit;
|
|
|
m_pVolumeEdit->setReadOnly(true);
|
|
|
m_pVolumeUnitCombo = new QComboBox;
|
|
|
m_pVolumeUnitCombo->addItems(QStringList() << tr("m^3")); // << "cm³" << "ft³" << "gallon" << "L");
|
|
|
m_pVolumeUnitCombo->setStyleSheet(comboStyle); // 使用相同的样式
|
|
|
|
|
|
m_pRefreshButton = new QPushButton;
|
|
|
QString iconDir = QCoreApplication::applicationDirPath().section('/', 0, -2) + "/Res/Icon/";
|
|
|
m_pRefreshButton->setIcon(QIcon(iconDir + "NmWellboreStorageRefresh.png"));
|
|
|
m_pRefreshButton->setIconSize(QSize(32, 32));
|
|
|
m_pRefreshButton->setFixedSize(34, 34);
|
|
|
|
|
|
volumeLayout->addWidget(m_pVolumeLabel);
|
|
|
volumeLayout->addWidget(m_pVolumeEdit);
|
|
|
volumeLayout->addWidget(m_pVolumeUnitCombo);
|
|
|
volumeLayout->addWidget(m_pRefreshButton);
|
|
|
volumeLayout->addStretch();
|
|
|
|
|
|
// 4. 底部按钮布局
|
|
|
QHBoxLayout* bottomButtonsLayout = new QHBoxLayout;
|
|
|
m_pOkButton = new QPushButton(tr("OK"));
|
|
|
m_pCancelButton = new QPushButton(tr("Cancel"));
|
|
|
bottomButtonsLayout->addStretch();
|
|
|
bottomButtonsLayout->addWidget(m_pOkButton);
|
|
|
bottomButtonsLayout->addWidget(m_pCancelButton);
|
|
|
|
|
|
mainLayout->addLayout(tableAndButtonsLayout);
|
|
|
mainLayout->addSpacing(10);
|
|
|
mainLayout->addLayout(volumeLayout);
|
|
|
mainLayout->addSpacing(10);
|
|
|
mainLayout->addLayout(bottomButtonsLayout);
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::initConnections()
|
|
|
{
|
|
|
connect(m_pAddButton, SIGNAL(clicked()), this, SLOT(onAddClicked()));
|
|
|
connect(m_pInsertButton, SIGNAL(clicked()), this, SLOT(onInsertClicked()));
|
|
|
connect(m_pDeleteButton, SIGNAL(clicked()), this, SLOT(onDeleteClicked()));
|
|
|
connect(m_pRefreshButton, SIGNAL(clicked()), this, SLOT(onRefreshClicked()));
|
|
|
connect(m_pOkButton, SIGNAL(clicked()), this, SLOT(accept()));
|
|
|
connect(m_pCancelButton, SIGNAL(clicked()), this, SLOT(reject()));
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::onAddClicked()
|
|
|
{
|
|
|
int rowCount = m_pTableWidget->rowCount();
|
|
|
m_pTableWidget->insertRow(rowCount);
|
|
|
|
|
|
// 为新行设置行号。行号从1开始,所以是当前行数减去1(因为第0行是单位行)
|
|
|
m_pTableWidget->setVerticalHeaderItem(rowCount, new QTableWidgetItem(QString::number(rowCount)));
|
|
|
|
|
|
m_pTableWidget->setItem(rowCount, 0, new QTableWidgetItem("N/A"));
|
|
|
m_pTableWidget->setItem(rowCount, 1, new QTableWidgetItem("N/A"));
|
|
|
|
|
|
m_pTableWidget->scrollToBottom();
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::onInsertClicked()
|
|
|
{
|
|
|
int currentRow = m_pTableWidget->currentRow();
|
|
|
if (currentRow <= 0) {
|
|
|
return;
|
|
|
}
|
|
|
m_pTableWidget->insertRow(currentRow);
|
|
|
m_pTableWidget->setItem(currentRow, 0, new QTableWidgetItem("N/A"));
|
|
|
m_pTableWidget->setItem(currentRow, 1, new QTableWidgetItem("N/A"));
|
|
|
|
|
|
// 插入行后,更新所有行号。行号从1开始。
|
|
|
for (int i = 1; i < m_pTableWidget->rowCount(); ++i) {
|
|
|
m_pTableWidget->setVerticalHeaderItem(i, new QTableWidgetItem(QString::number(i)));
|
|
|
}
|
|
|
|
|
|
m_pTableWidget->scrollToItem(m_pTableWidget->item(currentRow, 0));
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::onDeleteClicked()
|
|
|
{
|
|
|
int currentRow = m_pTableWidget->currentRow();
|
|
|
if (currentRow > 0 && currentRow < m_pTableWidget->rowCount()) {
|
|
|
m_pTableWidget->removeRow(currentRow);
|
|
|
|
|
|
// 删除行后,重新设置行号,行号从1开始。
|
|
|
for (int i = 1; i < m_pTableWidget->rowCount(); ++i) {
|
|
|
m_pTableWidget->setVerticalHeaderItem(i, new QTableWidgetItem(QString::number(i)));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::onRefreshClicked()
|
|
|
{
|
|
|
calculateVolume();
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::calculateVolume()
|
|
|
{
|
|
|
double totalVolume = 0.0;
|
|
|
const double PI = acos(-1.0);
|
|
|
|
|
|
// 从第二行开始遍历,跳过单位行
|
|
|
for (int i = 1; i < m_pTableWidget->rowCount(); ++i) {
|
|
|
QTableWidgetItem* heightItem = m_pTableWidget->item(i, 0);
|
|
|
QTableWidgetItem* idItem = m_pTableWidget->item(i, 1);
|
|
|
|
|
|
if (heightItem && idItem) {
|
|
|
bool okHeight, okID;
|
|
|
// 直接从表格中获取double值,不再进行单位转换
|
|
|
double height = heightItem->text().toDouble(&okHeight);
|
|
|
double id = idItem->text().toDouble(&okID);
|
|
|
|
|
|
if (okHeight && okID) {
|
|
|
// 直接使用获取到的值进行计算,因为默认单位就是m
|
|
|
double radius = id / 2.0;
|
|
|
double volumeSegment = PI * pow(radius, 2.0) * height;
|
|
|
totalVolume += volumeSegment;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 将计算结果显示在QLineEdit中,单位为立方米
|
|
|
m_pVolumeEdit->setText(QString::number(totalVolume, 'f', 6));
|
|
|
}
|
|
|
|
|
|
double nmWxWellboreStorageCalculatorVolume::getCalculatedVolume() const
|
|
|
{
|
|
|
bool ok;
|
|
|
double value = m_pVolumeEdit->text().toDouble(&ok);
|
|
|
if (ok && value >= 0) {
|
|
|
return value;
|
|
|
}
|
|
|
// 如果转换失败,返回-1
|
|
|
return -1;
|
|
|
}
|