You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nmWTAI-Platform/Src/nmNum/nmSubWxs/nmWxDFN.cpp

241 lines
9.8 KiB
C++

#include "nmWxDFN.h"
#include "nmDfnSession.h"
#include "nmDfnUiSupport.h"
#include "nmWxDFNGenerateDlg.h"
#include "nmWxDFNLoadFromFile.h"
#include "nmWxDFNExportDlg.h"
#include <QAbstractTableModel>
#include <QTableView>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSet>
#include <QItemSelectionModel>
#include <QMessageBox>
// 导入后的主列表也按需绘制,避免预览完成后又因逐格填表阻塞界面。
class nmDfnRecordModel : public QAbstractTableModel
{
public:
// QObject 父子关系负责释放模型。
explicit nmDfnRecordModel(QObject* pParent) : QAbstractTableModel(pParent) {}
// 保存工作记录的共享快照;表头和单位转换只在数据/显示单位变化时准备一次。
void setRecords(const QVector<nmDfnRecord>& vecRecords, const QString& sUnit, const QStringList& listHeaders)
{
beginResetModel();
m_vecRecords = vecRecords;
m_listHeaders = listHeaders;
m_oLengthConversion.configure("m", sUnit);
m_sFinite = QCoreApplication::translate("nmWxDFN", "Finite");
m_sInfinite = QCoreApplication::translate("nmWxDFN", "Infinite");
endResetModel();
}
// 每条裂缝对应一行。
int rowCount(const QModelIndex& oParent = QModelIndex()) const
{ return oParent.isValid() ? 0 : m_vecRecords.size(); }
// 名称、四个坐标、长度、模型及两项物性。
int columnCount(const QModelIndex& oParent = QModelIndex()) const
{ return oParent.isValid() ? 0 : 9; }
// 只为当前可见单元格格式化数值,业务记录始终保持完整精度。
QVariant data(const QModelIndex& oIndex, int nRole = Qt::DisplayRole) const
{
if (!oIndex.isValid() || oIndex.row() >= rowCount() || oIndex.column() >= columnCount()) return QVariant();
const int nColumn = oIndex.column();
if (nRole == Qt::TextAlignmentRole)
return int((nColumn == 0 || nColumn == 6 ? Qt::AlignLeft : Qt::AlignRight) | Qt::AlignVCenter);
if (nRole != Qt::DisplayRole && nRole != Qt::ToolTipRole) return QVariant();
const nmDfnRecord& oRecord = m_vecRecords[oIndex.row()];
if (nColumn == 0) return oRecord.m_sName;
if (nColumn == 6) return oRecord.m_bFiniteConductivity ? m_sFinite : m_sInfinite;
if (nColumn == 7) return nmDfnUi::number(oRecord.m_dConductivity);
if (nColumn == 8) return nmDfnUi::number(oRecord.m_dStorageRatio);
const double values[] = {oRecord.m_oLine.x1(), oRecord.m_oLine.y1(), oRecord.m_oLine.x2(),
oRecord.m_oLine.y2(), oRecord.m_oLine.length()};
double dDisplay = 0.0;
return m_oLengthConversion.convert(values[nColumn - 1], dDisplay) ? nmDfnUi::number(dDisplay) : QString();
}
// 表头使用窗口提供的翻译,行号保持从 1 开始。
QVariant headerData(int nSection, Qt::Orientation eOrientation, int nRole = Qt::DisplayRole) const
{
if (nRole != Qt::DisplayRole) return QVariant();
return eOrientation == Qt::Horizontal ? QVariant(m_listHeaders.value(nSection)) : QVariant(nSection + 1);
}
private:
QVector<nmDfnRecord> m_vecRecords; // 工作列表的共享值快照,不持有工程对象。
QStringList m_listHeaders; // 已生成的带单位表头。
nmDfnUnitConversion m_oLengthConversion; // 当前显示单位转换,避免逐格重新查找单位组。
QString m_sFinite; // 有限导流显示文字。
QString m_sInfinite; // 无限导流显示文字。
};
nmWxDFN::nmWxDFN(nmGuiPlot* pPlot, QWidget* pParent)
: iDlgBase(pParent), m_pSession(new nmDfnSession(pPlot))
{
initUI();
}
nmWxDFN::~nmWxDFN()
{
delete m_pSession;
}
bool nmWxDFN::initialize()
{
QString sError;
if (!m_pSession->initialize(sError)) {
QMessageBox::warning(this, tr("DFN"), sError);
return false;
}
refreshTable();
adjustSize();
return true;
}
void nmWxDFN::initUI()
{
setWindowTitle(tr("DFN"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
QVBoxLayout* pMain = new QVBoxLayout(this);
pMain->setContentsMargins(12, 10, 12, 0);
pMain->setSpacing(8);
QHBoxLayout* pActions = new QHBoxLayout;
QPushButton* pLoad = nmDfnUi::button(tr("Import"), this);
QPushButton* pGenerate = nmDfnUi::button(tr("Generate"), this);
m_pDeleteButton = nmDfnUi::button(tr("Delete selected"), this);
m_pClearButton = nmDfnUi::button(tr("Clear all"), this);
m_pExportButton = nmDfnUi::button(tr("Export"), this);
pActions->addWidget(pLoad);
pActions->addWidget(pGenerate);
pActions->addWidget(m_pDeleteButton);
pActions->addWidget(m_pClearButton);
pActions->addWidget(m_pExportButton);
pActions->addStretch();
pMain->addLayout(pActions);
QWidget* pSummary = new QWidget(this);
QGridLayout* pSummaryGrid = nmDfnUi::form(pSummary);
m_pCountEdit = new QLineEdit(pSummary);
m_pTotalEdit = new QLineEdit(pSummary);
m_pAverageEdit = new QLineEdit(pSummary);
QLineEdit* edits[] = { m_pCountEdit, m_pTotalEdit, m_pAverageEdit };
const QStringList labels = QStringList() << tr("Fracture count") << tr("Total length") << tr("Average length");
for (int i = 0; i < 3; ++i) {
edits[i]->setReadOnly(true);
edits[i]->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
nmDfnUi::label(pSummaryGrid, i, labels[i], pSummary);
pSummaryGrid->addWidget(edits[i], i, 1);
}
m_pLengthUnit = new QComboBox(pSummary);
nmDfnUi::units(m_pLengthUnit, "m");
pSummaryGrid->addWidget(m_pLengthUnit, 1, 2);
pMain->addWidget(pSummary);
m_pTable = new nmDfnTableView(12, this);
m_pTableModel = new nmDfnRecordModel(this);
m_pTable->setModel(m_pTableModel);
pMain->addWidget(m_pTable, 1);
m_pErrorLabel = new QLabel(this);
m_pErrorLabel->setWordWrap(true);
m_pErrorLabel->hide();
pMain->addWidget(m_pErrorLabel);
QDialogButtonBox* pButtons = nmDfnUi::buttons(this, tr("OK"), tr("Cancel"));
pMain->addWidget(pButtons);
connect(pLoad, SIGNAL(clicked()), this, SLOT(onLoadButtonClicked()));
connect(pGenerate, SIGNAL(clicked()), this, SLOT(onGenerateButtonClicked()));
connect(m_pDeleteButton, SIGNAL(clicked()), this, SLOT(onDeleteButtonClicked()));
connect(m_pClearButton, SIGNAL(clicked()), this, SLOT(onClearButtonClicked()));
connect(m_pExportButton, SIGNAL(clicked()), this, SLOT(onExportButtonClicked()));
connect(m_pLengthUnit, SIGNAL(currentIndexChanged(int)), this, SLOT(refreshTable()));
connect(m_pTable->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(updateButtonsState()));
connect(pButtons, SIGNAL(accepted()), this, SLOT(accept()));
connect(pButtons, SIGNAL(rejected()), this, SLOT(reject()));
}
void nmWxDFN::refreshTable()
{
const QVector<nmDfnRecord>& vecRecords = m_pSession->records();
const QString sUnit = m_pLengthUnit->currentText();
QSet<int> setSelected;
foreach (const QModelIndex& oIndex, m_pTable->selectionModel()->selectedRows()) setSelected.insert(oIndex.row());
m_pTable->setUpdatesEnabled(false);
const bool bBlocked = m_pTable->selectionModel()->blockSignals(true);
const QString sSuffix = " (" + sUnit + ")";
const QStringList headers = QStringList() << tr("Name") << "X1" + sSuffix << "Y1" + sSuffix
<< "X2" + sSuffix << "Y2" + sSuffix << tr("Length") + sSuffix << tr("Flow model")
<< tr("Conductivity") + " (mD.m)" << tr("Storage ratio");
m_pTableModel->setRecords(vecRecords, sUnit, headers);
double dTotal = 0.0;
for (int i = 0; i < vecRecords.size(); ++i) dTotal += vecRecords[i].m_oLine.length();
foreach (int nRow, setSelected)
if (nRow < vecRecords.size())
m_pTable->selectionModel()->select(m_pTableModel->index(nRow, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
m_pTable->selectionModel()->blockSignals(bBlocked);
m_pTable->setUpdatesEnabled(true);
double dDisplay = 0.0;
m_pCountEdit->setText(QString::number(vecRecords.size()));
m_pTotalEdit->setText(nmDfnUi::convert(dTotal, "m", sUnit, dDisplay) ? nmDfnUi::number(dDisplay) : QString());
m_pAverageEdit->setText(nmDfnUi::number(vecRecords.isEmpty() ? 0.0 : dDisplay / vecRecords.size()));
updateButtonsState();
}
void nmWxDFN::updateButtonsState()
{
const bool bHasData = !m_pSession->records().isEmpty();
m_pDeleteButton->setEnabled(!m_pTable->selectionModel()->selectedRows().isEmpty());
m_pClearButton->setEnabled(bHasData);
m_pExportButton->setEnabled(bHasData);
}
void nmWxDFN::onLoadButtonClicked()
{
nmWxDFNLoadFromFile oDialog(this);
if (oDialog.exec() != QDialog::Accepted) return;
m_pSession->records() = oDialog.records();
m_pTable->clearSelection();
m_pErrorLabel->hide();
refreshTable();
}
void nmWxDFN::onGenerateButtonClicked()
{
nmWxDFNGenerateDlg oDialog(m_pSession->boundary(), this);
if (oDialog.exec() != QDialog::Accepted) return;
m_pSession->records() = oDialog.records();
m_pTable->clearSelection();
m_pErrorLabel->hide();
refreshTable();
}
void nmWxDFN::onDeleteButtonClicked()
{
QSet<int> setRows;
foreach (const QModelIndex& oIndex, m_pTable->selectionModel()->selectedRows()) setRows.insert(oIndex.row());
for (int i = m_pSession->records().size() - 1; i >= 0; --i)
if (setRows.contains(i)) m_pSession->records().remove(i);
m_pTable->clearSelection();
m_pErrorLabel->hide();
refreshTable();
}
void nmWxDFN::onClearButtonClicked()
{
m_pSession->records().clear();
m_pErrorLabel->hide();
refreshTable();
}
void nmWxDFN::onExportButtonClicked()
{
nmWxDFNExportDlg oDialog(m_pSession->records(), this);
oDialog.exec();
}
void nmWxDFN::accept()
{
QString sError;
if (!m_pSession->commit(sError)) {
m_pErrorLabel->setText(sError);
m_pErrorLabel->show();
return;
}
iDlgBase::accept();
}