|
|
|
|
#include "nmWxDFNExportDlg.h"
|
|
|
|
|
#include "nmDfnTextFormat.h"
|
|
|
|
|
#include "nmDfnUiSupport.h"
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QClipboard>
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QGroupBox>
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
|
#include <QRadioButton>
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
|
|
nmWxDFNExportDlg::nmWxDFNExportDlg(const QVector<nmDfnRecord>& vecRecords, QWidget* pParent)
|
|
|
|
|
: iDlgBase(pParent), m_vecRecords(vecRecords)
|
|
|
|
|
{
|
|
|
|
|
initUI();
|
|
|
|
|
updateState();
|
|
|
|
|
adjustSize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nmWxDFNExportDlg::~nmWxDFNExportDlg() {}
|
|
|
|
|
|
|
|
|
|
void nmWxDFNExportDlg::initUI()
|
|
|
|
|
{
|
|
|
|
|
setWindowTitle(tr("Export DFN"));
|
|
|
|
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
|
|
|
|
QVBoxLayout* pMain = new QVBoxLayout(this);
|
|
|
|
|
pMain->setContentsMargins(12, 10, 12, 0);
|
|
|
|
|
pMain->setSpacing(8);
|
|
|
|
|
QWidget* pPrecision = new QWidget(this);
|
|
|
|
|
QGridLayout* pPrecisionGrid = nmDfnUi::form(pPrecision);
|
|
|
|
|
m_pPrecisionCombo = new QComboBox(pPrecision);
|
|
|
|
|
m_pPrecisionCombo->addItems(QStringList() << tr("Full precision") << tr("6 significant digits"));
|
|
|
|
|
nmDfnUi::label(pPrecisionGrid, 0, tr("Precision"), pPrecision);
|
|
|
|
|
pPrecisionGrid->addWidget(m_pPrecisionCombo, 0, 1, 1, 2);
|
|
|
|
|
pMain->addWidget(pPrecision);
|
|
|
|
|
QGroupBox* pTarget = new QGroupBox(tr("Export to"), this);
|
|
|
|
|
pTarget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
|
|
|
QVBoxLayout* pTargetLayout = new QVBoxLayout(pTarget);
|
|
|
|
|
pTargetLayout->setContentsMargins(8, 15, 8, 10);
|
|
|
|
|
m_pClipboardRadio = new QRadioButton(tr("Clipboard"), pTarget);
|
|
|
|
|
m_pFileRadio = new QRadioButton(tr("File"), pTarget);
|
|
|
|
|
m_pClipboardRadio->setChecked(true);
|
|
|
|
|
pTargetLayout->addWidget(m_pClipboardRadio);
|
|
|
|
|
pTargetLayout->addWidget(m_pFileRadio);
|
|
|
|
|
QHBoxLayout* pFileLayout = new QHBoxLayout;
|
|
|
|
|
m_pFilePathEdit = new QLineEdit(pTarget);
|
|
|
|
|
m_pFilePathEdit->setReadOnly(true);
|
|
|
|
|
m_pBrowseButton = nmDfnUi::button(tr("Browse"), pTarget);
|
|
|
|
|
pFileLayout->addWidget(m_pFilePathEdit, 1);
|
|
|
|
|
pFileLayout->addWidget(m_pBrowseButton);
|
|
|
|
|
pTargetLayout->addLayout(pFileLayout);
|
|
|
|
|
pMain->addWidget(pTarget);
|
|
|
|
|
pMain->addStretch(1);
|
|
|
|
|
m_pErrorLabel = new QLabel(this);
|
|
|
|
|
m_pErrorLabel->setWordWrap(true);
|
|
|
|
|
m_pErrorLabel->hide();
|
|
|
|
|
pMain->addWidget(m_pErrorLabel);
|
|
|
|
|
QDialogButtonBox* pButtons = nmDfnUi::buttons(this, tr("Export"), tr("Cancel"));
|
|
|
|
|
m_pOkButton = pButtons->button(QDialogButtonBox::Ok);
|
|
|
|
|
pMain->addWidget(pButtons);
|
|
|
|
|
connect(m_pBrowseButton, SIGNAL(clicked()), this, SLOT(onBrowseClicked()));
|
|
|
|
|
connect(m_pFileRadio, SIGNAL(toggled(bool)), this, SLOT(updateState()));
|
|
|
|
|
connect(m_pFilePathEdit, SIGNAL(textChanged(QString)), this, SLOT(updateState()));
|
|
|
|
|
connect(pButtons, SIGNAL(accepted()), this, SLOT(accept()));
|
|
|
|
|
connect(pButtons, SIGNAL(rejected()), this, SLOT(reject()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmWxDFNExportDlg::onBrowseClicked()
|
|
|
|
|
{
|
|
|
|
|
const QString sPath = QFileDialog::getSaveFileName(this, tr("Export DFN"), ZxBaseUtil::getLastDir(),
|
|
|
|
|
tr("CSV files (*.csv);;Text files (*.txt)"));
|
|
|
|
|
if (sPath.isEmpty()) return;
|
|
|
|
|
m_pFilePathEdit->setText(sPath);
|
|
|
|
|
ZxBaseUtil::setLastDir(QFileInfo(sPath).absolutePath());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmWxDFNExportDlg::updateState()
|
|
|
|
|
{
|
|
|
|
|
const bool bFile = m_pFileRadio->isChecked();
|
|
|
|
|
m_pFilePathEdit->setEnabled(bFile);
|
|
|
|
|
m_pBrowseButton->setEnabled(bFile);
|
|
|
|
|
m_pOkButton->setEnabled(!m_vecRecords.isEmpty() && (!bFile || !m_pFilePathEdit->text().isEmpty()));
|
|
|
|
|
m_pErrorLabel->hide();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmWxDFNExportDlg::accept()
|
|
|
|
|
{
|
|
|
|
|
if (m_vecRecords.isEmpty()) return;
|
|
|
|
|
for (int i = 0; i < m_vecRecords.size(); ++i) {
|
|
|
|
|
if (!m_vecRecords[i].isValid()) {
|
|
|
|
|
m_pErrorLabel->setText(tr("Invalid DFN record at row %1.").arg(i + 1));
|
|
|
|
|
m_pErrorLabel->show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const QString sContent = nmDfnText::serialize(m_vecRecords, m_pPrecisionCombo->currentIndex() == 0);
|
|
|
|
|
if (m_pFileRadio->isChecked()) {
|
|
|
|
|
QFile oFile(m_pFilePathEdit->text());
|
|
|
|
|
// UTF-8 BOM 方便 Excel 识别中文名称;导入器也兼容无 BOM 的 UTF-8 和本地文本。
|
|
|
|
|
const QByteArray bytes = QByteArray("\xEF\xBB\xBF", 3) + sContent.toUtf8();
|
|
|
|
|
if (!oFile.open(QIODevice::WriteOnly) || oFile.write(bytes) != bytes.size() || !oFile.flush()) {
|
|
|
|
|
m_pErrorLabel->setText(tr("Cannot write file %1.").arg(m_pFilePathEdit->text()));
|
|
|
|
|
m_pErrorLabel->show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
QApplication::clipboard()->setText(sContent);
|
|
|
|
|
}
|
|
|
|
|
iDlgBase::accept();
|
|
|
|
|
}
|