[Spreadsheet] add preference page and 3 import/export parameters

This commit is contained in:
mwganson
2021-09-27 21:57:45 -05:00
parent fdfe0291fd
commit 0be3565dbe
10 changed files with 518 additions and 4 deletions

View File

@@ -127,6 +127,46 @@ void Sheet::clearAll()
observers.clear();
}
//validate import/export parameters
bool Sheet::getCharsFromPrefs(char &delim, char &quote, char &escape, std::string &errMsg){
bool isValid = true;
ParameterGrp::handle group = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Spreadsheet");
QString delimiter = QString::fromStdString(group->GetASCII("ImportExportDelimiter","tab"));
QString quoteChar = QString::fromStdString(group->GetASCII("ImportExportQuoteCharacter","\""));
QString escapeChar = QString::fromStdString(group->GetASCII("ImportExportEscapeCharacter","\\"));
delim = delimiter.size() == 1 ? delimiter[0].toLatin1() : '\0';
if (delimiter.compare(QLatin1String("tab"), Qt::CaseInsensitive) == 0 || delimiter.compare(QLatin1String("\\t"),Qt::CaseInsensitive) == 0){
delim = '\t';
} else if (delimiter.compare(QLatin1String("comma"), Qt::CaseInsensitive) == 0){
delim = ',';
} else if (delimiter.compare(QLatin1String("semicolon"), Qt::CaseInsensitive) == 0){
delim = ';';
}
if(delim != '\0' && quoteChar.size() == 1 && escapeChar.size() == 1){
quote = quoteChar[0].toLatin1();
escape = escapeChar[0].toLatin1();
} else {
isValid = false;
std::string importExport = errMsg;
std::stringstream errStream;
errStream << "Invalid spreadsheet Import/Export parameter.\n";
if (delim == '\0') {
errStream << "Unrecognized delimiter: " << delimiter.toStdString() << " (recognized tokens: \\t, tab, semicolon, comma, or any single character)\n";
}
if (quoteChar.size() != 1){
errStream << "Invalid quote character: " << quoteChar.toStdString() << " (quote character must be one single character)\n";
}
if (escapeChar.size() != 1){
errStream << "Invalid escape character: " << escapeChar.toStdString() << " (escape character must be one single character)\n";
}
errStream << importExport << " not done.\n";
errMsg = errStream.str();
}
return isValid;
}
/**
* Import a file into the spreadsheet object.
*

View File

@@ -86,6 +86,8 @@ public:
bool importFromFile(const std::string & filename, char delimiter = '\t', char quoteChar = '\0', char escapeChar = '\\');
bool getCharsFromPrefs(char &delimiter, char &quote, char &escape, std::string &errMsg);
bool exportToFile(const std::string & filename, char delimiter = '\t', char quoteChar = '\0', char escapeChar = '\\') const;
bool mergeCells(const App::Range &range);

View File

@@ -41,8 +41,10 @@
#include <Gui/Document.h>
#include <Gui/Application.h>
#include <Gui/BitmapFactory.h>
#include <Gui/WidgetFactory.h>
#include <Gui/Language/Translator.h>
#include <Mod/Spreadsheet/App/Sheet.h>
#include "DlgSettingsImp.h"
#include "Workbench.h"
#include "ViewProviderSpreadsheet.h"
#include "SpreadsheetView.h"
@@ -119,6 +121,9 @@ PyMOD_INIT_FUNC(SpreadsheetGui)
SpreadsheetGui::Workbench::init();
SpreadsheetGui::SheetView::init();
// register preference page
new Gui::PrefPageProducer<SpreadsheetGui::DlgSettingsImp> ("Spreadsheet");
// add resources and reloads the translators
loadSpreadsheetResource();

View File

@@ -64,6 +64,9 @@ SET(SpreadsheetGui_SRCS
${SpreadsheetGui_XML_SRCS}
AppSpreadsheetGui.cpp
Command.cpp
DlgSettings.ui
DlgSettingsImp.cpp
DlgSettingsImp.h
LineEdit.h
LineEdit.cpp
ViewProviderSpreadsheet.cpp
@@ -93,6 +96,26 @@ SET(SpreadsheetGuiIcon_SVG
Resources/icons/SpreadsheetWorkbench.svg
)
set(SpreadsheetGui_MOC_HDRS
DlgSettingsImp.h
)
SOURCE_GROUP("Moc" FILES ${SpreadsheetGui_MOC_SRCS})
SET(Resource_SRCS
${Resource_SRCS}
Resources/Spreadsheet.qrc
)
set(SpreadsheetGui_UIC_SRCS
DlgSettings.ui
)
if (BUILD_QT5)
qt5_wrap_ui(SpreadsheetGui_UIC_HDRS ${SpreadsheetGui_UIC_SRCS})
endif()
add_library(SpreadsheetGui SHARED ${SpreadsheetGui_SRCS} ${SpreadsheetGuiIcon_SVG})
target_link_libraries(SpreadsheetGui ${SpreadsheetGui_LIBS})

View File

@@ -197,9 +197,19 @@ void CmdSpreadsheetImport::activated(int iMsg)
if (!fileName.isEmpty()) {
std::string FeatName = getUniqueObjectName("Spreadsheet");
Sheet * sheet = freecad_dynamic_cast<Sheet>(App::GetApplication().getActiveDocument()->addObject("Spreadsheet::Sheet", FeatName.c_str()));
if (sheet){
char delim, quote, escape;
std::string errMsg = "Import";
bool isValid = sheet->getCharsFromPrefs(delim, quote, escape, errMsg);
sheet->importFromFile(Base::Tools::toStdString(fileName), '\t', '"', '\\');
sheet->execute();
if (isValid){
sheet->importFromFile(fileName.toStdString(), delim, quote, escape);
sheet->execute();
} else {
Base::Console().Error(errMsg.c_str());
return;
}
}
}
}
@@ -240,8 +250,20 @@ void CmdSpreadsheetExport::activated(int iMsg)
QString(),
formatList,
&selectedFilter);
if (!fileName.isEmpty())
sheet->exportToFile(Base::Tools::toStdString(fileName), '\t', '"', '\\');
if (!fileName.isEmpty()){
if (sheet){
char delim, quote, escape;
std::string errMsg = "Export";
bool isValid = sheet->getCharsFromPrefs(delim, quote, escape, errMsg);
if (isValid){
sheet->exportToFile(fileName.toStdString(), delim, quote, escape);
} else {
Base::Console().Error(errMsg.c_str());
return;
}
}
}
}
}
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,111 @@
/***************************************************************************
* Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QApplication>
#endif
#include "DlgSettingsImp.h"
#include "ui_DlgSettings.h"
#include <Gui/Application.h>
#include <Gui/PrefWidgets.h>
#include <Base/Console.h>
using namespace SpreadsheetGui;
/* TRANSLATOR SpreadsheetGui::DlgSettingsImp */
DlgSettingsImp::DlgSettingsImp( QWidget* parent )
: PreferencePage( parent )
, ui(new Ui_DlgSettings)
{
ui->setupUi(this);
}
/**
* Destroys the object and frees any allocated resources
*/
DlgSettingsImp::~DlgSettingsImp()
{
// no need to delete child widgets, Qt does it all for us
}
void DlgSettingsImp::saveSettings()
{
/** use whatever the user has entered here
* we'll check for validity during import/export
*/
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Spreadsheet");
QString delimiter = ui->delimiterComboBox->currentText();
hGrp->SetASCII("ImportExportDelimiter", delimiter.toStdString().c_str());
ui->quoteCharLineEdit->onSave();
ui->escapeCharLineEdit->onSave();
}
void DlgSettingsImp::loadSettings()
{
/** items "tab", ";", and "," have already been added to the combo box in the .ui file
* we'll recognize a few tokens: comma, semicolon, tab, and \t
*/
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Spreadsheet");
QString delimiter = QString::fromStdString(hGrp->GetASCII("ImportExportDelimiter", "tab"));
int idx = ui->delimiterComboBox->findText(delimiter, Qt::MatchFixedString);
if(idx != -1){
ui->delimiterComboBox->setCurrentIndex(idx);
} else if(delimiter.compare(QLatin1String("\\t"), Qt::CaseInsensitive) == 0){
idx = ui->delimiterComboBox->findText(QLatin1String("tab"), Qt::MatchFixedString);
ui->delimiterComboBox->setCurrentIndex(idx);
} else if(delimiter.compare(QLatin1String("semicolon"), Qt::CaseInsensitive) == 0){
idx = ui->delimiterComboBox->findText(QLatin1String(";"), Qt::MatchFixedString);
ui->delimiterComboBox->setCurrentIndex(idx);
} else if(delimiter.compare(QLatin1String("comma"), Qt::CaseInsensitive) == 0){
idx = ui->delimiterComboBox->findText(QLatin1String(","), Qt::MatchFixedString);
ui->delimiterComboBox->setCurrentIndex(idx);
} else {
ui->delimiterComboBox->addItem(delimiter);
idx = ui->delimiterComboBox->findText(delimiter, Qt::MatchFixedString);
ui->delimiterComboBox->setCurrentIndex(idx);
}
ui->quoteCharLineEdit->onRestore();
ui->escapeCharLineEdit->onRestore();
}
/**
* Sets the strings of the subwidgets using the current language.
*/
void DlgSettingsImp::changeEvent(QEvent *e)
{
if (e->type() == QEvent::LanguageChange) {
ui->retranslateUi(this);
}
else {
QWidget::changeEvent(e);
}
}
#include "moc_DlgSettingsImp.cpp"

View File

@@ -0,0 +1,57 @@
/***************************************************************************
* Copyright (c) 2021 Mark Ganson <TheMarkster> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef SPREADSHEETGUI_DLGSETTINGSIMP_H
#define SPREADSHEETGUI_DLGSETTINGSIMP_H
#include <Gui/PropertyPage.h>
#include <memory>
namespace SpreadsheetGui {
class Ui_DlgSettings;
/**
* The DlgSettingsImp class implements a preference page to change settings
* for the Spreadsheet workbench.
* /author TheMarkster, based on work by Jürgen Riegel
*/
class DlgSettingsImp : public Gui::Dialog::PreferencePage
{
Q_OBJECT
public:
DlgSettingsImp( QWidget* parent = 0 );
~DlgSettingsImp();
protected:
void saveSettings();
void loadSettings();
void changeEvent(QEvent *e);
private:
std::unique_ptr<Ui_DlgSettings> ui;
};
} // namespace SpreadsheetGui
#endif // SPREADSHEETGUI_DLGSETTINGSIMP_H

View File

@@ -1,5 +1,6 @@
<RCC>
<qresource prefix="/">
<file>icons/preferences-spreadsheet.svg</file>
<file>icons/Spreadsheet.svg</file>
<file>icons/SpreadsheetController.svg</file>
<file>icons/SpreadsheetImport.svg</file>

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 5.9 KiB