Refactor export to CSV.

Preparation for export final measurements.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2017-09-27 13:25:18 +03:00
parent 4caab9a479
commit 257fb2f913
10 changed files with 96 additions and 48 deletions

View File

@ -32,7 +32,6 @@
#include "dialogs/dialognewmeasurements.h" #include "dialogs/dialognewmeasurements.h"
#include "dialogs/dialogmdatabase.h" #include "dialogs/dialogmdatabase.h"
#include "dialogs/dialogtapepreferences.h" #include "dialogs/dialogtapepreferences.h"
#include "dialogs/dialogexporttocsv.h"
#include "../vpatterndb/calculator.h" #include "../vpatterndb/calculator.h"
#include "../vpatterndb/pmsystems.h" #include "../vpatterndb/pmsystems.h"
#include "../ifc/ifcdef.h" #include "../ifc/ifcdef.h"
@ -663,7 +662,7 @@ bool TMainWindow::eventFilter(QObject *object, QEvent *event)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void TMainWindow::ExportToCSVData(const QString &fileName, const DialogExportToCSV &dialog) void TMainWindow::ExportToCSVData(const QString &fileName, bool withHeader, int mib, const QChar &separator)
{ {
QxtCsvModel csv; QxtCsvModel csv;
const int columns = ui->tableWidget->columnCount(); const int columns = ui->tableWidget->columnCount();
@ -678,7 +677,7 @@ void TMainWindow::ExportToCSVData(const QString &fileName, const DialogExportToC
} }
} }
if (dialog.WithHeader()) if (withHeader)
{ {
int colCount = 0; int colCount = 0;
for (int column = 0; column < columns; ++column) for (int column = 0; column < columns; ++column)
@ -708,7 +707,7 @@ void TMainWindow::ExportToCSVData(const QString &fileName, const DialogExportToC
} }
} }
csv.toCSV(fileName, dialog.WithHeader(), dialog.Separator(), QTextCodec::codecForMib(dialog.SelectedMib())); csv.toCSV(fileName, withHeader, separator, QTextCodec::codecForMib(mib));
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -1886,7 +1885,7 @@ void TMainWindow::SetupMenu()
connect(ui->actionSaveAs, &QAction::triggered, this, &TMainWindow::FileSaveAs); connect(ui->actionSaveAs, &QAction::triggered, this, &TMainWindow::FileSaveAs);
ui->actionSaveAs->setShortcuts(QKeySequence::SaveAs); ui->actionSaveAs->setShortcuts(QKeySequence::SaveAs);
connect(ui->actionExportToCSV, &QAction::triggered, this, &TMainWindow::ExportToCSV); connect(ui->actionExportToCSV, &QAction::triggered, this, &TMainWindow::ExportDataToCSV);
connect(ui->actionReadOnly, &QAction::triggered, this, [this](bool ro) connect(ui->actionReadOnly, &QAction::triggered, this, [this](bool ro)
{ {
if (not mIsReadOnly) if (not mIsReadOnly)

View File

@ -70,7 +70,8 @@ protected:
virtual void changeEvent(QEvent* event) Q_DECL_OVERRIDE; virtual void changeEvent(QEvent* event) Q_DECL_OVERRIDE;
virtual void showEvent(QShowEvent *event) Q_DECL_OVERRIDE; virtual void showEvent(QShowEvent *event) Q_DECL_OVERRIDE;
virtual bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE; virtual bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE;
virtual void ExportToCSVData(const QString &fileName, const DialogExportToCSV &dialog) Q_DECL_FINAL; virtual void ExportToCSVData(const QString &fileName, bool withHeader, int mib,
const QChar &separator) Q_DECL_FINAL;
private slots: private slots:
void FileNew(); void FileNew();

View File

@ -40,7 +40,6 @@
#include "../vmisc/vsettings.h" #include "../vmisc/vsettings.h"
#include "../vmisc/def.h" #include "../vmisc/def.h"
#include "../vmisc/qxtcsvmodel.h" #include "../vmisc/qxtcsvmodel.h"
#include "../vmisc/dialogs/dialogexporttocsv.h"
#include "undocommands/renamepp.h" #include "undocommands/renamepp.h"
#include "core/vtooloptionspropertybrowser.h" #include "core/vtooloptionspropertybrowser.h"
#include "options.h" #include "options.h"
@ -1483,7 +1482,7 @@ void MainWindow::PrepareSceneList()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void MainWindow::ExportToCSVData(const QString &fileName, const DialogExportToCSV &dialog) void MainWindow::ExportToCSVData(const QString &fileName, bool withHeader, int mib, const QChar &separator)
{ {
QxtCsvModel csv; QxtCsvModel csv;
@ -1491,7 +1490,7 @@ void MainWindow::ExportToCSVData(const QString &fileName, const DialogExportToCS
csv.insertColumn(1); csv.insertColumn(1);
csv.insertColumn(2); csv.insertColumn(2);
if (dialog.WithHeader()) if (withHeader)
{ {
csv.setHeaderText(0, tr("Name")); csv.setHeaderText(0, tr("Name"));
csv.setHeaderText(1, tr("The calculated value")); csv.setHeaderText(1, tr("The calculated value"));
@ -1536,7 +1535,7 @@ void MainWindow::ExportToCSVData(const QString &fileName, const DialogExportToCS
SavePreviewCalculation(false); SavePreviewCalculation(false);
SavePreviewCalculation(true); SavePreviewCalculation(true);
csv.toCSV(fileName, dialog.WithHeader(), dialog.Separator(), QTextCodec::codecForMib(dialog.SelectedMib())); csv.toCSV(fileName, withHeader, separator, QTextCodec::codecForMib(mib));
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -3978,7 +3977,7 @@ void MainWindow::CreateActions()
connect(ui->actionSave, &QAction::triggered, this, &MainWindow::Save); connect(ui->actionSave, &QAction::triggered, this, &MainWindow::Save);
connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::Open); connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::Open);
connect(ui->actionNew, &QAction::triggered, this, &MainWindow::New); connect(ui->actionNew, &QAction::triggered, this, &MainWindow::New);
connect(ui->actionExportIncrementsToCSV, &QAction::triggered, this, &MainWindow::ExportToCSV); connect(ui->actionExportIncrementsToCSV, &QAction::triggered, this, &MainWindow::ExportDataToCSV);
connect(ui->actionTable, &QAction::triggered, this, [this](bool checked) connect(ui->actionTable, &QAction::triggered, this, [this](bool checked)
{ {

View File

@ -103,7 +103,8 @@ protected:
virtual void customEvent(QEvent * event) Q_DECL_OVERRIDE; virtual void customEvent(QEvent * event) Q_DECL_OVERRIDE;
virtual void CleanLayout() Q_DECL_OVERRIDE; virtual void CleanLayout() Q_DECL_OVERRIDE;
virtual void PrepareSceneList() Q_DECL_OVERRIDE; virtual void PrepareSceneList() Q_DECL_OVERRIDE;
virtual void ExportToCSVData(const QString &fileName, const DialogExportToCSV &dialog) Q_DECL_FINAL; virtual void ExportToCSVData(const QString &fileName, bool withHeader, int mib,
const QChar &separator) Q_DECL_FINAL;
private slots: private slots:
void MouseMove(const QPointF &scenePos); void MouseMove(const QPointF &scenePos);
void Clear(); void Clear();

View File

@ -44,23 +44,21 @@ DialogExportToCSV::DialogExportToCSV(QWidget *parent)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->checkBoxWithHeader->setChecked(qApp->Settings()->GetCSVWithHeader());
foreach (int mib, QTextCodec::availableMibs()) foreach (int mib, QTextCodec::availableMibs())
{ {
ui->comboBoxCodec->addItem(QTextCodec::codecForMib(mib)->name(), mib); ui->comboBoxCodec->addItem(QTextCodec::codecForMib(mib)->name(), mib);
} }
ui->comboBoxCodec->setCurrentIndex(ui->comboBoxCodec->findData(qApp->Settings()->GetCSVCodec())); ui->comboBoxCodec->setCurrentIndex(ui->comboBoxCodec->findData(VCommonSettings::GetDefCSVCodec()));
SetSeparator(qApp->Settings()->GetCSVSeparator()); SetSeparator(qApp->Settings()->GetDefCSVSeparator());
QPushButton *bDefaults = ui->buttonBox->button(QDialogButtonBox::RestoreDefaults); QPushButton *bDefaults = ui->buttonBox->button(QDialogButtonBox::RestoreDefaults);
SCASSERT(bDefaults != nullptr) SCASSERT(bDefaults != nullptr)
connect(bDefaults, &QPushButton::clicked, this, [this]() connect(bDefaults, &QPushButton::clicked, this, [this]()
{ {
ui->checkBoxWithHeader->setChecked(qApp->Settings()->GetDefCSVWithHeader()); ui->checkBoxWithHeader->setChecked(qApp->Settings()->GetDefCSVWithHeader());
ui->comboBoxCodec->setCurrentIndex(ui->comboBoxCodec->findData(qApp->Settings()->GetDefCSVCodec())); ui->comboBoxCodec->setCurrentIndex(ui->comboBoxCodec->findData(VCommonSettings::GetDefCSVCodec()));
SetSeparator(qApp->Settings()->GetDefCSVSeparator()); SetSeparator(qApp->Settings()->GetDefCSVSeparator());
}); });
@ -73,19 +71,46 @@ DialogExportToCSV::~DialogExportToCSV()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool DialogExportToCSV::WithHeader() const bool DialogExportToCSV::IsWithHeader() const
{ {
return ui->checkBoxWithHeader->isChecked(); return ui->checkBoxWithHeader->isChecked();
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
int DialogExportToCSV::SelectedMib() const void DialogExportToCSV::SetWithHeader(bool value)
{ {
return ui->comboBoxCodec->currentData().toInt(); ui->checkBoxWithHeader->setChecked(value);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QChar DialogExportToCSV::Separator() const int DialogExportToCSV::GetSelectedMib() const
{
if (ui->comboBoxCodec->currentIndex() != -1)
{
return ui->comboBoxCodec->currentData().toInt();
}
else
{
return VCommonSettings::GetDefCSVCodec();
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogExportToCSV::SetSelectedMib(int value)
{
const int index = ui->comboBoxCodec->findData(value);
if (index != -1)
{
ui->comboBoxCodec->setCurrentIndex(index);
}
else
{
ui->comboBoxCodec->setCurrentIndex(ui->comboBoxCodec->findData(VCommonSettings::GetDefCSVCodec()));
}
}
//---------------------------------------------------------------------------------------------------------------------
QChar DialogExportToCSV::GetSeparator() const
{ {
if (ui->radioButtonTab->isChecked()) if (ui->radioButtonTab->isChecked())
{ {

View File

@ -31,7 +31,8 @@
#include <QDialog> #include <QDialog>
namespace Ui { namespace Ui
{
class DialogExportToCSV; class DialogExportToCSV;
} }
@ -43,9 +44,14 @@ public:
explicit DialogExportToCSV(QWidget *parent = nullptr); explicit DialogExportToCSV(QWidget *parent = nullptr);
virtual ~DialogExportToCSV(); virtual ~DialogExportToCSV();
bool WithHeader() const; bool IsWithHeader() const;
int SelectedMib() const; void SetWithHeader(bool value);
QChar Separator() const;
int GetSelectedMib() const;
void SetSelectedMib(int value);
QChar GetSeparator() const;
void SetSeparator(const QChar &separator);
protected: protected:
virtual void changeEvent(QEvent* event) Q_DECL_OVERRIDE; virtual void changeEvent(QEvent* event) Q_DECL_OVERRIDE;
@ -55,8 +61,6 @@ private:
Q_DISABLE_COPY(DialogExportToCSV) Q_DISABLE_COPY(DialogExportToCSV)
Ui::DialogExportToCSV *ui; Ui::DialogExportToCSV *ui;
bool isInitialized; bool isInitialized;
void SetSeparator(const QChar &separator);
}; };
#endif // DIALOGEXPORTTOCSV_H #endif // DIALOGEXPORTTOCSV_H

View File

@ -710,7 +710,7 @@ bool VCommonSettings::GetCSVWithHeader() const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool VCommonSettings::GetDefCSVWithHeader() const bool VCommonSettings::GetDefCSVWithHeader()
{ {
return false; return false;
} }
@ -730,7 +730,7 @@ int VCommonSettings::GetCSVCodec() const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
int VCommonSettings::GetDefCSVCodec() const int VCommonSettings::GetDefCSVCodec()
{ {
return QTextCodec::codecForLocale()->mibEnum(); return QTextCodec::codecForLocale()->mibEnum();
} }
@ -775,7 +775,7 @@ QChar VCommonSettings::GetCSVSeparator() const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QChar VCommonSettings::GetDefCSVSeparator() const QChar VCommonSettings::GetDefCSVSeparator()
{ {
return QChar(','); return QChar(',');
} }

View File

@ -149,15 +149,15 @@ public:
void SetCSVWithHeader(bool withHeader); void SetCSVWithHeader(bool withHeader);
bool GetCSVWithHeader() const; bool GetCSVWithHeader() const;
bool GetDefCSVWithHeader() const; static bool GetDefCSVWithHeader();
void SetCSVCodec(int mib); void SetCSVCodec(int mib);
int GetCSVCodec() const; int GetCSVCodec() const;
int GetDefCSVCodec() const; static int GetDefCSVCodec();
void SetCSVSeparator(const QChar &separator); void SetCSVSeparator(const QChar &separator);
QChar GetCSVSeparator() const; QChar GetCSVSeparator() const;
QChar GetDefCSVSeparator() const; static QChar GetDefCSVSeparator();
void SetDefaultSeamAllowance(double value); void SetDefaultSeamAllowance(double value);
double GetDefaultSeamAllowance(); double GetDefaultSeamAllowance();

View File

@ -87,13 +87,7 @@ void VAbstractMainWindow::ToolBarStyle(QToolBar *bar)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VAbstractMainWindow::WindowsLocale() QString VAbstractMainWindow::CSVFilePath()
{
qApp->Settings()->GetOsSeparator() ? setLocale(QLocale()) : setLocale(QLocale::c());
}
//---------------------------------------------------------------------------------------------------------------------
void VAbstractMainWindow::ExportToCSV()
{ {
const QString filters = tr("Comma-Separated Values") + QLatin1String(" (*.csv)"); const QString filters = tr("Comma-Separated Values") + QLatin1String(" (*.csv)");
const QString suffix("csv"); const QString suffix("csv");
@ -104,7 +98,7 @@ void VAbstractMainWindow::ExportToCSV()
if (fileName.isEmpty()) if (fileName.isEmpty())
{ {
return; return fileName;
} }
QFileInfo f( fileName ); QFileInfo f( fileName );
@ -113,13 +107,36 @@ void VAbstractMainWindow::ExportToCSV()
fileName += QLatin1String(".") + suffix; fileName += QLatin1String(".") + suffix;
} }
return fileName;
}
//---------------------------------------------------------------------------------------------------------------------
void VAbstractMainWindow::WindowsLocale()
{
qApp->Settings()->GetOsSeparator() ? setLocale(QLocale()) : setLocale(QLocale::c());
}
//---------------------------------------------------------------------------------------------------------------------
void VAbstractMainWindow::ExportDataToCSV()
{
QString fileName = CSVFilePath();
if (fileName.isEmpty())
{
return;
}
DialogExportToCSV dialog(this); DialogExportToCSV dialog(this);
dialog.SetWithHeader(qApp->Settings()->GetCSVWithHeader());
dialog.SetSelectedMib(qApp->Settings()->GetCSVCodec());
dialog.SetSeparator(qApp->Settings()->GetCSVSeparator());
if (dialog.exec() == QDialog::Accepted) if (dialog.exec() == QDialog::Accepted)
{ {
ExportToCSVData(fileName, dialog); ExportToCSVData(fileName, dialog.IsWithHeader(), dialog.GetSelectedMib(), dialog.GetSeparator());
qApp->Settings()->SetCSVSeparator(dialog.Separator()); qApp->Settings()->SetCSVSeparator(dialog.GetSeparator());
qApp->Settings()->SetCSVCodec(dialog.SelectedMib()); qApp->Settings()->SetCSVCodec(dialog.GetSelectedMib());
qApp->Settings()->SetCSVWithHeader(dialog.WithHeader()); qApp->Settings()->SetCSVWithHeader(dialog.IsWithHeader());
} }
} }

View File

@ -49,7 +49,7 @@ public slots:
protected slots: protected slots:
void WindowsLocale(); void WindowsLocale();
void ExportToCSV(); void ExportDataToCSV();
protected: protected:
int m_curFileFormatVersion; int m_curFileFormatVersion;
@ -58,9 +58,11 @@ protected:
bool ContinueFormatRewrite(const QString &currentFormatVersion, const QString &maxFormatVersion); bool ContinueFormatRewrite(const QString &currentFormatVersion, const QString &maxFormatVersion);
void ToolBarStyle(QToolBar *bar); void ToolBarStyle(QToolBar *bar);
virtual void ExportToCSVData(const QString &fileName, const DialogExportToCSV &dialog)=0; virtual void ExportToCSVData(const QString &fileName, bool withHeader, int mib, const QChar &separator)=0;
private: private:
Q_DISABLE_COPY(VAbstractMainWindow) Q_DISABLE_COPY(VAbstractMainWindow)
QString CSVFilePath();
}; };
#endif // VABSTRACTMAINWINDOW_H #endif // VABSTRACTMAINWINDOW_H