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.
213 lines
6.3 KiB
C++
213 lines
6.3 KiB
C++
#include "nmWxDFNExportDlg.h"
|
|
#include "nmWxDFNExportDlg.h"
|
|
#include "nmDataAnalyzeManager.h"
|
|
#include "nmDataFracture.h"
|
|
|
|
#include <QFile>
|
|
#include <QTextStream>
|
|
#include <QMessageBox>
|
|
#include <QApplication>
|
|
//#include <QClipboard>
|
|
#include <QVBoxLayout>
|
|
#include <QHBoxLayout>
|
|
#include <QGroupBox>
|
|
#include <QRadioButton>
|
|
#include <QLineEdit>
|
|
#include <QPushButton>
|
|
#include <QFileDialog>
|
|
#include <QLabel>
|
|
#include <QMessageBox>
|
|
|
|
nmWxDFNExportDlg::nmWxDFNExportDlg(QWidget *parent)
|
|
: QDialog(parent)
|
|
{
|
|
setWindowTitle(tr("Export DFN"));
|
|
setMinimumSize(400, 200);
|
|
|
|
initUI();
|
|
initConnections();
|
|
onTargetChanged(); // 初始化文件路径控件状态
|
|
}
|
|
|
|
nmWxDFNExportDlg::~nmWxDFNExportDlg() {}
|
|
|
|
void nmWxDFNExportDlg::initUI()
|
|
{
|
|
QVBoxLayout* pMainLayout = new QVBoxLayout(this);
|
|
|
|
// Export options group
|
|
QGroupBox* pOptionsGroup = new QGroupBox(tr("Export options"), this);
|
|
QVBoxLayout* pOptLayout = new QVBoxLayout(pOptionsGroup);
|
|
m_pRoundedRadio = new QRadioButton(tr("Rounded"), pOptionsGroup);
|
|
m_pExactRadio = new QRadioButton(tr("Exact"), pOptionsGroup);
|
|
m_pRoundedRadio->setChecked(true);
|
|
pOptLayout->addWidget(m_pRoundedRadio);
|
|
pOptLayout->addWidget(m_pExactRadio);
|
|
pMainLayout->addWidget(pOptionsGroup);
|
|
|
|
// Export to group
|
|
QGroupBox* pTargetGroup = new QGroupBox(tr("Export to"), this);
|
|
QVBoxLayout* pTgtLayout = new QVBoxLayout(pTargetGroup);
|
|
|
|
// Clipboard option
|
|
m_pClipboardRadio = new QRadioButton(tr("Clipboard"), pTargetGroup);
|
|
m_pClipboardRadio->setChecked(true);
|
|
pTgtLayout->addWidget(m_pClipboardRadio);
|
|
|
|
// File option + path + browse on one line
|
|
m_pFileRadio = new QRadioButton(tr("File"), pTargetGroup);
|
|
QHBoxLayout* fileLayout = new QHBoxLayout();
|
|
fileLayout->addWidget(m_pFileRadio);
|
|
|
|
m_pFilePathEdit = new QLineEdit(pTargetGroup);
|
|
m_pFilePathEdit->setEnabled(false);
|
|
m_pFilePathEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
|
fileLayout->addWidget(m_pFilePathEdit, 1);
|
|
|
|
m_pBrowseButton = new QPushButton(tr("..."), pTargetGroup);
|
|
m_pBrowseButton->setEnabled(false);
|
|
fileLayout->addWidget(m_pBrowseButton);
|
|
|
|
pTgtLayout->addLayout(fileLayout);
|
|
pMainLayout->addWidget(pTargetGroup);
|
|
|
|
// OK / Cancel buttons
|
|
QHBoxLayout* pBtnLayout = new QHBoxLayout();
|
|
pBtnLayout->addStretch();
|
|
m_pOkButton = new QPushButton(tr("OK"), this);
|
|
m_pCancelButton = new QPushButton(tr("Cancel"), this);
|
|
pBtnLayout->addWidget(m_pOkButton);
|
|
pBtnLayout->addWidget(m_pCancelButton);
|
|
pMainLayout->addLayout(pBtnLayout);
|
|
}
|
|
|
|
void nmWxDFNExportDlg::initConnections()
|
|
{
|
|
connect(m_pBrowseButton, SIGNAL(clicked()), this, SLOT(onBrowseClicked()));
|
|
connect(m_pFileRadio, SIGNAL(toggled(bool)), this, SLOT(onTargetChanged()));
|
|
connect(m_pClipboardRadio, SIGNAL(toggled(bool)), this, SLOT(onTargetChanged()));
|
|
connect(m_pOkButton, SIGNAL(clicked()), this, SLOT(onOkClicked()));
|
|
connect(m_pCancelButton, SIGNAL(clicked()), this, SLOT(onCancelClicked()));
|
|
}
|
|
|
|
void nmWxDFNExportDlg::onBrowseClicked()
|
|
{
|
|
QString filter = tr("(*.txt)");
|
|
QString file = QFileDialog::getSaveFileName(this, tr("Export to File"), QString(), filter);
|
|
if (!file.isEmpty()) {
|
|
m_pFilePathEdit->setText(file);
|
|
}
|
|
}
|
|
|
|
void nmWxDFNExportDlg::onTargetChanged()
|
|
{
|
|
bool toFile = m_pFileRadio->isChecked();
|
|
m_pFilePathEdit->setEnabled(toFile);
|
|
m_pBrowseButton->setEnabled(toFile);
|
|
}
|
|
|
|
void nmWxDFNExportDlg::onOkClicked()
|
|
{
|
|
// 如果是文件导出,检查路径
|
|
if ( m_pFileRadio->isChecked() && m_pFilePathEdit->text().isEmpty() ) {
|
|
QMessageBox::warning(this, tr("Error"), tr("Please select a file path"));
|
|
return;
|
|
}
|
|
|
|
// 准备精度和默认值
|
|
bool rounded = m_pRoundedRadio->isChecked();
|
|
int prec = rounded ? 2 : 4;
|
|
double defaultWf = 0.00328084; // m
|
|
double defaultPhi = 0.0; // °
|
|
QString coordUnit = tr("m");
|
|
QString widthUnit = tr("m");
|
|
//QString phiUnit = tr("°");
|
|
|
|
// 构造 CSV 头、单位行
|
|
QStringList header;
|
|
header << "Name" << "X1" << "Y1" << "X2" << "Y2" << "Wf" << "o";
|
|
|
|
QStringList units;
|
|
units << "DFN Name"
|
|
<< coordUnit
|
|
<< coordUnit
|
|
<< coordUnit
|
|
<< coordUnit
|
|
<< widthUnit ;
|
|
|
|
QStringList lines;
|
|
lines << header.join(",")
|
|
<< units.join(",");
|
|
|
|
// 拿所有 DFN 类型的裂缝,拼每一行数据
|
|
QVector<nmDataFracture*> allFracs = nmDataAnalyzeManager::getCurrentInstance()->getDFNFractureDataList();
|
|
foreach ( nmDataFracture* f, allFracs ) {
|
|
// 只导出 fractureType == "DFN"
|
|
if ( f->getFractureType().getValue().toString() != tr("DFN") )
|
|
continue;
|
|
|
|
QVector<QPointF> pts = f->getFracturePoints();
|
|
if ( pts.size() != 2 )
|
|
continue;
|
|
|
|
QStringList row;
|
|
row << f->getFractureName()
|
|
<< QString::number(pts[0].x(), 'f', prec)
|
|
<< QString::number(pts[0].y(), 'f', prec)
|
|
<< QString::number(pts[1].x(), 'f', prec)
|
|
<< QString::number(pts[1].y(), 'f', prec)
|
|
<< QString::number(defaultWf, 'f', prec)
|
|
<< QString::number(defaultPhi, 'f', prec);
|
|
|
|
lines << row.join(",");
|
|
}
|
|
|
|
QString content = lines.join("\n");
|
|
|
|
// 导出到文件或剪贴板
|
|
if ( m_pFileRadio->isChecked() ) {
|
|
QString path = m_pFilePathEdit->text();
|
|
if ( !path.endsWith(".txt", Qt::CaseInsensitive) )
|
|
path += ".txt";
|
|
|
|
QFile file(path);
|
|
if ( !file.open(QIODevice::WriteOnly | QIODevice::Text) ) {
|
|
QMessageBox::warning(this, tr("Export Error"),
|
|
tr("Cannot write file:\n%1").arg(path));
|
|
return;
|
|
}
|
|
QTextStream out(&file);
|
|
out << content;
|
|
file.close();
|
|
|
|
}
|
|
else {
|
|
// TODO 写入剪切板
|
|
//QClipboard *cb = QApplication::clipboard();
|
|
//cb->setText(content);
|
|
//QMessageBox::information(this, tr("Export"), tr("Copied to clipboard"));
|
|
}
|
|
|
|
accept();
|
|
}
|
|
|
|
void nmWxDFNExportDlg::onCancelClicked()
|
|
{
|
|
reject();
|
|
}
|
|
|
|
bool nmWxDFNExportDlg::isRounded() const
|
|
{
|
|
return m_pRoundedRadio->isChecked();
|
|
}
|
|
|
|
bool nmWxDFNExportDlg::exportToFile() const
|
|
{
|
|
return m_pFileRadio->isChecked();
|
|
}
|
|
|
|
QString nmWxDFNExportDlg::exportFilePath() const
|
|
{
|
|
return m_pFilePathEdit->text();
|
|
}
|