From 257fb2f913b581668d900af39d87a0e426e05c5f Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Wed, 27 Sep 2017 13:25:18 +0300 Subject: [PATCH] Refactor export to CSV. Preparation for export final measurements. --HG-- branch : feature --- src/app/tape/tmainwindow.cpp | 9 ++-- src/app/tape/tmainwindow.h | 3 +- src/app/valentina/mainwindow.cpp | 9 ++-- src/app/valentina/mainwindow.h | 3 +- src/libs/vmisc/dialogs/dialogexporttocsv.cpp | 43 ++++++++++++++++---- src/libs/vmisc/dialogs/dialogexporttocsv.h | 18 ++++---- src/libs/vmisc/vcommonsettings.cpp | 6 +-- src/libs/vmisc/vcommonsettings.h | 6 +-- src/libs/vwidgets/vabstractmainwindow.cpp | 41 +++++++++++++------ src/libs/vwidgets/vabstractmainwindow.h | 6 ++- 10 files changed, 96 insertions(+), 48 deletions(-) diff --git a/src/app/tape/tmainwindow.cpp b/src/app/tape/tmainwindow.cpp index 71db1cfde..914b4b848 100644 --- a/src/app/tape/tmainwindow.cpp +++ b/src/app/tape/tmainwindow.cpp @@ -32,7 +32,6 @@ #include "dialogs/dialognewmeasurements.h" #include "dialogs/dialogmdatabase.h" #include "dialogs/dialogtapepreferences.h" -#include "dialogs/dialogexporttocsv.h" #include "../vpatterndb/calculator.h" #include "../vpatterndb/pmsystems.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; 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; 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); 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) { if (not mIsReadOnly) diff --git a/src/app/tape/tmainwindow.h b/src/app/tape/tmainwindow.h index 9a3163b88..f602d260b 100644 --- a/src/app/tape/tmainwindow.h +++ b/src/app/tape/tmainwindow.h @@ -70,7 +70,8 @@ protected: virtual void changeEvent(QEvent* event) Q_DECL_OVERRIDE; virtual void showEvent(QShowEvent *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: void FileNew(); diff --git a/src/app/valentina/mainwindow.cpp b/src/app/valentina/mainwindow.cpp index 6ba2aff97..8d66bf784 100644 --- a/src/app/valentina/mainwindow.cpp +++ b/src/app/valentina/mainwindow.cpp @@ -40,7 +40,6 @@ #include "../vmisc/vsettings.h" #include "../vmisc/def.h" #include "../vmisc/qxtcsvmodel.h" -#include "../vmisc/dialogs/dialogexporttocsv.h" #include "undocommands/renamepp.h" #include "core/vtooloptionspropertybrowser.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; @@ -1491,7 +1490,7 @@ void MainWindow::ExportToCSVData(const QString &fileName, const DialogExportToCS csv.insertColumn(1); csv.insertColumn(2); - if (dialog.WithHeader()) + if (withHeader) { csv.setHeaderText(0, tr("Name")); csv.setHeaderText(1, tr("The calculated value")); @@ -1536,7 +1535,7 @@ void MainWindow::ExportToCSVData(const QString &fileName, const DialogExportToCS SavePreviewCalculation(false); 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->actionOpen, &QAction::triggered, this, &MainWindow::Open); 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) { diff --git a/src/app/valentina/mainwindow.h b/src/app/valentina/mainwindow.h index 38a6bfeeb..627820e6e 100644 --- a/src/app/valentina/mainwindow.h +++ b/src/app/valentina/mainwindow.h @@ -103,7 +103,8 @@ protected: virtual void customEvent(QEvent * event) Q_DECL_OVERRIDE; virtual void CleanLayout() 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: void MouseMove(const QPointF &scenePos); void Clear(); diff --git a/src/libs/vmisc/dialogs/dialogexporttocsv.cpp b/src/libs/vmisc/dialogs/dialogexporttocsv.cpp index 4e3dae8de..8584d305c 100644 --- a/src/libs/vmisc/dialogs/dialogexporttocsv.cpp +++ b/src/libs/vmisc/dialogs/dialogexporttocsv.cpp @@ -44,23 +44,21 @@ DialogExportToCSV::DialogExportToCSV(QWidget *parent) { ui->setupUi(this); - ui->checkBoxWithHeader->setChecked(qApp->Settings()->GetCSVWithHeader()); - foreach (int mib, QTextCodec::availableMibs()) { 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); SCASSERT(bDefaults != nullptr) connect(bDefaults, &QPushButton::clicked, this, [this]() { 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()); }); @@ -73,19 +71,46 @@ DialogExportToCSV::~DialogExportToCSV() } //--------------------------------------------------------------------------------------------------------------------- -bool DialogExportToCSV::WithHeader() const +bool DialogExportToCSV::IsWithHeader() const { 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()) { diff --git a/src/libs/vmisc/dialogs/dialogexporttocsv.h b/src/libs/vmisc/dialogs/dialogexporttocsv.h index 5c684d934..d1782aa26 100644 --- a/src/libs/vmisc/dialogs/dialogexporttocsv.h +++ b/src/libs/vmisc/dialogs/dialogexporttocsv.h @@ -31,8 +31,9 @@ #include -namespace Ui { -class DialogExportToCSV; +namespace Ui +{ + class DialogExportToCSV; } class DialogExportToCSV : public QDialog @@ -43,9 +44,14 @@ public: explicit DialogExportToCSV(QWidget *parent = nullptr); virtual ~DialogExportToCSV(); - bool WithHeader() const; - int SelectedMib() const; - QChar Separator() const; + bool IsWithHeader() const; + void SetWithHeader(bool value); + + int GetSelectedMib() const; + void SetSelectedMib(int value); + + QChar GetSeparator() const; + void SetSeparator(const QChar &separator); protected: virtual void changeEvent(QEvent* event) Q_DECL_OVERRIDE; @@ -55,8 +61,6 @@ private: Q_DISABLE_COPY(DialogExportToCSV) Ui::DialogExportToCSV *ui; bool isInitialized; - - void SetSeparator(const QChar &separator); }; #endif // DIALOGEXPORTTOCSV_H diff --git a/src/libs/vmisc/vcommonsettings.cpp b/src/libs/vmisc/vcommonsettings.cpp index b7a97d3d9..cc3a410ba 100644 --- a/src/libs/vmisc/vcommonsettings.cpp +++ b/src/libs/vmisc/vcommonsettings.cpp @@ -710,7 +710,7 @@ bool VCommonSettings::GetCSVWithHeader() const } //--------------------------------------------------------------------------------------------------------------------- -bool VCommonSettings::GetDefCSVWithHeader() const +bool VCommonSettings::GetDefCSVWithHeader() { return false; } @@ -730,7 +730,7 @@ int VCommonSettings::GetCSVCodec() const } //--------------------------------------------------------------------------------------------------------------------- -int VCommonSettings::GetDefCSVCodec() const +int VCommonSettings::GetDefCSVCodec() { return QTextCodec::codecForLocale()->mibEnum(); } @@ -775,7 +775,7 @@ QChar VCommonSettings::GetCSVSeparator() const } //--------------------------------------------------------------------------------------------------------------------- -QChar VCommonSettings::GetDefCSVSeparator() const +QChar VCommonSettings::GetDefCSVSeparator() { return QChar(','); } diff --git a/src/libs/vmisc/vcommonsettings.h b/src/libs/vmisc/vcommonsettings.h index 231c44527..4460dbb53 100644 --- a/src/libs/vmisc/vcommonsettings.h +++ b/src/libs/vmisc/vcommonsettings.h @@ -149,15 +149,15 @@ public: void SetCSVWithHeader(bool withHeader); bool GetCSVWithHeader() const; - bool GetDefCSVWithHeader() const; + static bool GetDefCSVWithHeader(); void SetCSVCodec(int mib); int GetCSVCodec() const; - int GetDefCSVCodec() const; + static int GetDefCSVCodec(); void SetCSVSeparator(const QChar &separator); QChar GetCSVSeparator() const; - QChar GetDefCSVSeparator() const; + static QChar GetDefCSVSeparator(); void SetDefaultSeamAllowance(double value); double GetDefaultSeamAllowance(); diff --git a/src/libs/vwidgets/vabstractmainwindow.cpp b/src/libs/vwidgets/vabstractmainwindow.cpp index 6454ae80a..58dfec4b7 100644 --- a/src/libs/vwidgets/vabstractmainwindow.cpp +++ b/src/libs/vwidgets/vabstractmainwindow.cpp @@ -87,13 +87,7 @@ void VAbstractMainWindow::ToolBarStyle(QToolBar *bar) } //--------------------------------------------------------------------------------------------------------------------- -void VAbstractMainWindow::WindowsLocale() -{ - qApp->Settings()->GetOsSeparator() ? setLocale(QLocale()) : setLocale(QLocale::c()); -} - -//--------------------------------------------------------------------------------------------------------------------- -void VAbstractMainWindow::ExportToCSV() +QString VAbstractMainWindow::CSVFilePath() { const QString filters = tr("Comma-Separated Values") + QLatin1String(" (*.csv)"); const QString suffix("csv"); @@ -104,7 +98,7 @@ void VAbstractMainWindow::ExportToCSV() if (fileName.isEmpty()) { - return; + return fileName; } QFileInfo f( fileName ); @@ -113,13 +107,36 @@ void VAbstractMainWindow::ExportToCSV() 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); + dialog.SetWithHeader(qApp->Settings()->GetCSVWithHeader()); + dialog.SetSelectedMib(qApp->Settings()->GetCSVCodec()); + dialog.SetSeparator(qApp->Settings()->GetCSVSeparator()); + if (dialog.exec() == QDialog::Accepted) { - ExportToCSVData(fileName, dialog); + ExportToCSVData(fileName, dialog.IsWithHeader(), dialog.GetSelectedMib(), dialog.GetSeparator()); - qApp->Settings()->SetCSVSeparator(dialog.Separator()); - qApp->Settings()->SetCSVCodec(dialog.SelectedMib()); - qApp->Settings()->SetCSVWithHeader(dialog.WithHeader()); + qApp->Settings()->SetCSVSeparator(dialog.GetSeparator()); + qApp->Settings()->SetCSVCodec(dialog.GetSelectedMib()); + qApp->Settings()->SetCSVWithHeader(dialog.IsWithHeader()); } } diff --git a/src/libs/vwidgets/vabstractmainwindow.h b/src/libs/vwidgets/vabstractmainwindow.h index 95e89ab1f..588d978ae 100644 --- a/src/libs/vwidgets/vabstractmainwindow.h +++ b/src/libs/vwidgets/vabstractmainwindow.h @@ -49,7 +49,7 @@ public slots: protected slots: void WindowsLocale(); - void ExportToCSV(); + void ExportDataToCSV(); protected: int m_curFileFormatVersion; @@ -58,9 +58,11 @@ protected: bool ContinueFormatRewrite(const QString ¤tFormatVersion, const QString &maxFormatVersion); 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: Q_DISABLE_COPY(VAbstractMainWindow) + + QString CSVFilePath(); }; #endif // VABSTRACTMAINWINDOW_H