|
|
#include "nmWxWellboreStorageCalculatorVolume.h"
|
|
|
#include "nmWellboreStorageUiSupport.h"
|
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
#include <QVBoxLayout>
|
|
|
#include <cmath>
|
|
|
|
|
|
nmWxWellboreStorageCalculatorVolume::nmWxWellboreStorageCalculatorVolume(QWidget* pParent)
|
|
|
: iDlgBase(pParent), m_dVolume(-1.0)
|
|
|
{
|
|
|
initUI();
|
|
|
initConnections();
|
|
|
updateSelectionButtons();
|
|
|
calculateVolume();
|
|
|
adjustSize();
|
|
|
}
|
|
|
|
|
|
nmWxWellboreStorageCalculatorVolume::~nmWxWellboreStorageCalculatorVolume()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::initUI()
|
|
|
{
|
|
|
setWindowTitle(tr("Tested chamber volume"));
|
|
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
|
|
QVBoxLayout* pMain = new QVBoxLayout(this);
|
|
|
pMain->setContentsMargins(12, 10, 12, 0);
|
|
|
pMain->setSpacing(8);
|
|
|
// 分段录入采用较紧凑的八行布局,列宽为数值编辑预留适量空间。
|
|
|
m_pTableWidget = new nmWellboreStorageTable(2, this, 8, 18);
|
|
|
// 此列是各圆筒分段长度,不是相对于井口的累计井深。
|
|
|
m_pTableWidget->setHorizontalHeaderLabels(QStringList() << tr("Segment length") << tr("I.D."));
|
|
|
m_pHeightUnitCombo = new QComboBox(m_pTableWidget);
|
|
|
m_pIdUnitCombo = new QComboBox(m_pTableWidget);
|
|
|
m_sHeightUnit = nmWellboreStorageUi::populateUnits(m_pHeightUnitCombo, "m");
|
|
|
m_sIdUnit = nmWellboreStorageUi::populateUnits(m_pIdUnitCombo, "m");
|
|
|
m_pTableWidget->setCellWidget(0, 0, m_pHeightUnitCombo);
|
|
|
m_pTableWidget->setCellWidget(0, 1, m_pIdUnitCombo);
|
|
|
pMain->addWidget(m_pTableWidget, 1);
|
|
|
|
|
|
QHBoxLayout* pOperations = new QHBoxLayout;
|
|
|
QPushButton* pAdd = new QPushButton(tr("Add"), this);
|
|
|
m_pInsertButton = new QPushButton(tr("Insert"), this);
|
|
|
m_pDeleteButton = new QPushButton(tr("Delete"), this);
|
|
|
pAdd->setIcon(zxLoadIcon("Add"));
|
|
|
m_pDeleteButton->setIcon(zxLoadIcon("NmDelete"));
|
|
|
pAdd->setAutoDefault(false);
|
|
|
m_pInsertButton->setAutoDefault(false);
|
|
|
m_pDeleteButton->setAutoDefault(false);
|
|
|
pOperations->addWidget(pAdd);
|
|
|
pOperations->addWidget(m_pInsertButton);
|
|
|
pOperations->addWidget(m_pDeleteButton);
|
|
|
pOperations->addStretch();
|
|
|
pMain->addLayout(pOperations);
|
|
|
connect(pAdd, SIGNAL(clicked()), this, SLOT(onAddClicked()));
|
|
|
|
|
|
QWidget* pResult = new QWidget(this);
|
|
|
QGridLayout* pResultGrid = nmWellboreStorageUi::form(pResult);
|
|
|
m_pVolumeField = new nmWellboreStorageUnitField(pResult, "m^3", true);
|
|
|
m_pVolumeField->addTo(pResultGrid, 0, tr("Volume"));
|
|
|
pMain->addWidget(pResult);
|
|
|
|
|
|
QWidget* pFooter = new QWidget(this);
|
|
|
pFooter->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
|
QHBoxLayout* pFooterLayout = new QHBoxLayout(pFooter);
|
|
|
pFooterLayout->setContentsMargins(4, 5, 4, 5);
|
|
|
QPushButton* pRefresh = new QPushButton(tr("Recalculate"), pFooter);
|
|
|
pRefresh->setIcon(zxLoadIcon("NmWellboreStorageRefresh"));
|
|
|
pRefresh->setAutoDefault(false);
|
|
|
pFooterLayout->addWidget(pRefresh);
|
|
|
QDialogButtonBox* pButtons = nmWellboreStorageUi::buttons(pFooter, tr("OK"), tr("Cancel"));
|
|
|
m_pOkButton = pButtons->button(QDialogButtonBox::Ok);
|
|
|
pFooterLayout->addWidget(pButtons, 1);
|
|
|
pMain->addWidget(pFooter);
|
|
|
connect(pRefresh, SIGNAL(clicked()), this, SLOT(onRefreshClicked()));
|
|
|
connect(pButtons, SIGNAL(accepted()), this, SLOT(accept()));
|
|
|
connect(pButtons, SIGNAL(rejected()), this, SLOT(reject()));
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::initConnections()
|
|
|
{
|
|
|
m_pTableWidget->setEditReceiver(this, SLOT(onCellTextEdited()));
|
|
|
connect(m_pInsertButton, SIGNAL(clicked()), this, SLOT(onInsertClicked()));
|
|
|
connect(m_pDeleteButton, SIGNAL(clicked()), this, SLOT(onDeleteClicked()));
|
|
|
connect(m_pTableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(onItemChanged(QTableWidgetItem*)));
|
|
|
connect(m_pTableWidget, SIGNAL(itemSelectionChanged()), this, SLOT(updateSelectionButtons()));
|
|
|
connect(m_pHeightUnitCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onLengthUnitChanged()));
|
|
|
connect(m_pIdUnitCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onLengthUnitChanged()));
|
|
|
connect(m_pVolumeField->unitCombo(), SIGNAL(currentIndexChanged(int)), this, SLOT(onVolumeUnitChanged()));
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::setSegments(const QVector<QPair<double, double> >& vecSegments)
|
|
|
{
|
|
|
m_pTableWidget->finishEditing();
|
|
|
m_pTableWidget->setRowCount(1);
|
|
|
for (int i = 0; i < vecSegments.size(); ++i) insertSegment(i + 1, &vecSegments[i]);
|
|
|
updateRowNumbers();
|
|
|
calculateVolume();
|
|
|
}
|
|
|
|
|
|
QVector<QPair<double, double> > nmWxWellboreStorageCalculatorVolume::segments() const
|
|
|
{
|
|
|
QVector<QPair<double, double> > vecSegments;
|
|
|
if (m_dVolume < 0.0) return vecSegments;
|
|
|
for (int nRow = 1; nRow < m_pTableWidget->rowCount(); ++nRow)
|
|
|
vecSegments.append(qMakePair(m_pTableWidget->item(nRow, 0)->data(Qt::UserRole).toDouble(),
|
|
|
m_pTableWidget->item(nRow, 1)->data(Qt::UserRole).toDouble()));
|
|
|
return vecSegments;
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::insertSegment(int nRow, const QPair<double, double>* pSegment)
|
|
|
{
|
|
|
const bool bBlocked = m_pTableWidget->blockSignals(true);
|
|
|
m_pTableWidget->insertRow(nRow);
|
|
|
for (int nColumn = 0; nColumn < 2; ++nColumn) {
|
|
|
QTableWidgetItem* pItem = new QTableWidgetItem;
|
|
|
if (pSegment) {
|
|
|
const double dBase = nColumn == 0 ? pSegment->first : pSegment->second;
|
|
|
double dDisplay = 0.0;
|
|
|
const QString sUnit = nColumn == 0 ? m_sHeightUnit : m_sIdUnit;
|
|
|
if (dBase > 0.0 && nmWellboreStorageUi::convert(dBase, "m", sUnit, dDisplay)) {
|
|
|
pItem->setText(nmWellboreStorageUi::number(dDisplay));
|
|
|
pItem->setData(Qt::UserRole, dBase);
|
|
|
}
|
|
|
}
|
|
|
m_pTableWidget->setItem(nRow, nColumn, pItem);
|
|
|
}
|
|
|
m_pTableWidget->blockSignals(bBlocked);
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::updateRowNumbers()
|
|
|
{
|
|
|
for (int nRow = 1; nRow < m_pTableWidget->rowCount(); ++nRow)
|
|
|
m_pTableWidget->setVerticalHeaderItem(nRow, new QTableWidgetItem(QString::number(nRow)));
|
|
|
updateSelectionButtons();
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::onAddClicked()
|
|
|
{
|
|
|
m_pTableWidget->finishEditing();
|
|
|
const int nRow = m_pTableWidget->rowCount();
|
|
|
insertSegment(nRow);
|
|
|
updateRowNumbers();
|
|
|
m_pTableWidget->setCurrentCell(nRow, 0);
|
|
|
m_pTableWidget->editItem(m_pTableWidget->item(nRow, 0));
|
|
|
calculateVolume();
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::onInsertClicked()
|
|
|
{
|
|
|
m_pTableWidget->finishEditing();
|
|
|
const int nRow = m_pTableWidget->currentRow();
|
|
|
if (nRow < 1 || m_pTableWidget->selectedItems().isEmpty()) return;
|
|
|
insertSegment(nRow);
|
|
|
updateRowNumbers();
|
|
|
m_pTableWidget->setCurrentCell(nRow, 0);
|
|
|
m_pTableWidget->editItem(m_pTableWidget->item(nRow, 0));
|
|
|
calculateVolume();
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::onDeleteClicked()
|
|
|
{
|
|
|
m_pTableWidget->finishEditing();
|
|
|
const int nRow = m_pTableWidget->currentRow();
|
|
|
if (nRow < 1 || m_pTableWidget->selectedItems().isEmpty()) return;
|
|
|
m_pTableWidget->removeRow(nRow);
|
|
|
updateRowNumbers();
|
|
|
calculateVolume();
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::onRefreshClicked()
|
|
|
{
|
|
|
m_pTableWidget->finishEditing();
|
|
|
calculateVolume();
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::onCellTextEdited()
|
|
|
{
|
|
|
m_pTableWidget->commitEditing();
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::onItemChanged(QTableWidgetItem* pItem)
|
|
|
{
|
|
|
if (!pItem || pItem->row() < 1 || pItem->column() > 1) return;
|
|
|
QVariant oOriginalValue;
|
|
|
if (m_pTableWidget->restoredBaseValue(oOriginalValue)) {
|
|
|
const bool bBlocked = m_pTableWidget->blockSignals(true);
|
|
|
pItem->setData(Qt::UserRole, oOriginalValue);
|
|
|
m_pTableWidget->blockSignals(bBlocked);
|
|
|
calculateVolume();
|
|
|
return;
|
|
|
}
|
|
|
bool bOk = false;
|
|
|
const double dDisplay = pItem->text().toDouble(&bOk);
|
|
|
const QString sUnit = pItem->column() == 0 ? m_sHeightUnit : m_sIdUnit;
|
|
|
double dBase = 0.0;
|
|
|
bOk = bOk && dDisplay > 0.0 && nmWellboreStorageUi::convert(dDisplay, sUnit, "m", dBase)
|
|
|
&& dBase > 0.0;
|
|
|
const bool bBlocked = m_pTableWidget->blockSignals(true);
|
|
|
// 失效值必须清除基准缓存;不允许沿用该单元格修改前的旧数值继续计算。
|
|
|
pItem->setData(Qt::UserRole, bOk ? QVariant(dBase) : QVariant());
|
|
|
m_pTableWidget->blockSignals(bBlocked);
|
|
|
calculateVolume();
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::onLengthUnitChanged()
|
|
|
{
|
|
|
QComboBox* pCombo = qobject_cast<QComboBox*>(sender());
|
|
|
if (pCombo != m_pHeightUnitCombo && pCombo != m_pIdUnitCombo) return;
|
|
|
const int nColumn = pCombo == m_pHeightUnitCombo ? 0 : 1;
|
|
|
QString& sOldUnit = nColumn == 0 ? m_sHeightUnit : m_sIdUnit;
|
|
|
const QString sNextUnit = pCombo->currentText();
|
|
|
m_pTableWidget->finishEditing();
|
|
|
QVector<QString> vecTexts;
|
|
|
double dDisplay = 0.0;
|
|
|
bool bValid = nmWellboreStorageUi::convert(0.0, "m", sNextUnit, dDisplay);
|
|
|
for (int nRow = 1; bValid && nRow < m_pTableWidget->rowCount(); ++nRow) {
|
|
|
const QTableWidgetItem* pItem = m_pTableWidget->item(nRow, nColumn);
|
|
|
if (pItem->text().trimmed().isEmpty()) {
|
|
|
vecTexts.append(QString());
|
|
|
continue;
|
|
|
}
|
|
|
bValid = pItem->data(Qt::UserRole).isValid()
|
|
|
&& nmWellboreStorageUi::convert(pItem->data(Qt::UserRole).toDouble(), "m", sNextUnit, dDisplay);
|
|
|
if (bValid) vecTexts.append(nmWellboreStorageUi::number(dDisplay));
|
|
|
}
|
|
|
if (!bValid) {
|
|
|
nmWellboreStorageUi::restoreUnit(pCombo, sOldUnit);
|
|
|
return;
|
|
|
}
|
|
|
sOldUnit = sNextUnit;
|
|
|
const bool bBlocked = m_pTableWidget->blockSignals(true);
|
|
|
for (int nRow = 1; nRow < m_pTableWidget->rowCount(); ++nRow)
|
|
|
m_pTableWidget->item(nRow, nColumn)->setText(vecTexts[nRow - 1]);
|
|
|
m_pTableWidget->blockSignals(bBlocked);
|
|
|
calculateVolume();
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::onVolumeUnitChanged()
|
|
|
{
|
|
|
m_pVolumeField->changeUnit();
|
|
|
calculateVolume();
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::updateSelectionButtons()
|
|
|
{
|
|
|
const bool bSelected = m_pTableWidget->currentRow() > 0 && !m_pTableWidget->selectedItems().isEmpty();
|
|
|
m_pInsertButton->setEnabled(bSelected);
|
|
|
m_pDeleteButton->setEnabled(bSelected);
|
|
|
}
|
|
|
|
|
|
bool nmWxWellboreStorageCalculatorVolume::calculateVolume()
|
|
|
{
|
|
|
double dVolume = 0.0;
|
|
|
bool bValid = m_pTableWidget->rowCount() > 1;
|
|
|
for (int nRow = 1; bValid && nRow < m_pTableWidget->rowCount(); ++nRow) {
|
|
|
QTableWidgetItem* pLength = m_pTableWidget->item(nRow, 0);
|
|
|
QTableWidgetItem* pId = m_pTableWidget->item(nRow, 1);
|
|
|
bValid = pLength && pId && pLength->data(Qt::UserRole).isValid() && pId->data(Qt::UserRole).isValid();
|
|
|
if (!bValid) break;
|
|
|
const double dLength = pLength->data(Qt::UserRole).toDouble();
|
|
|
const double dId = pId->data(Qt::UserRole).toDouble();
|
|
|
const double dRadius = dId * 0.5;
|
|
|
// 每行长度是独立分段长度,πr²L 累加后得到 m³,不使用累计井深相减。
|
|
|
const double dSegmentVolume = acos(-1.0) * dRadius * dRadius * dLength;
|
|
|
dVolume += dSegmentVolume;
|
|
|
bValid = qIsFinite(dLength) && dLength > 0.0 && qIsFinite(dId) && dId > 0.0
|
|
|
&& qIsFinite(dSegmentVolume) && dSegmentVolume > 0.0 && qIsFinite(dVolume);
|
|
|
}
|
|
|
if (bValid) bValid = m_pVolumeField->setValue(dVolume);
|
|
|
if (!bValid) m_pVolumeField->clear();
|
|
|
m_dVolume = bValid ? dVolume : -1.0;
|
|
|
m_pOkButton->setEnabled(bValid);
|
|
|
return bValid;
|
|
|
}
|
|
|
|
|
|
void nmWxWellboreStorageCalculatorVolume::accept()
|
|
|
{
|
|
|
m_pTableWidget->finishEditing();
|
|
|
if (calculateVolume()) iDlgBase::accept();
|
|
|
}
|
|
|
|
|
|
double nmWxWellboreStorageCalculatorVolume::getCalculatedVolume() const
|
|
|
{
|
|
|
return m_dVolume;
|
|
|
}
|