Fix crashes when importing and opening .csv files on macOS (#17084)
* Open recent documents from start same way as in menu * Make insert spreadsheet create document if not available. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Chris Hennes <chennes@pioneerlibrarysystem.org>
This commit is contained in:
@@ -65,6 +65,20 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
void load(App::Document* pcDoc, const std::string& Name)
|
||||
{
|
||||
try {
|
||||
Base::FileInfo file(Name);
|
||||
Spreadsheet::Sheet* pcSheet = static_cast<Spreadsheet::Sheet*>(
|
||||
pcDoc->addObject("Spreadsheet::Sheet", file.fileNamePure().c_str()));
|
||||
|
||||
pcSheet->importFromFile(Name, '\t', '"', '\\');
|
||||
pcSheet->execute();
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
throw Py::RuntimeError(e.what());
|
||||
}
|
||||
}
|
||||
Py::Object open(const Py::Tuple& args)
|
||||
{
|
||||
char* Name;
|
||||
@@ -75,19 +89,9 @@ private:
|
||||
std::string EncodedName = std::string(Name);
|
||||
PyMem_Free(Name);
|
||||
|
||||
try {
|
||||
Base::FileInfo file(EncodedName);
|
||||
App::Document* pcDoc =
|
||||
App::GetApplication().newDocument(DocName ? DocName : QT_TR_NOOP("Unnamed"));
|
||||
Spreadsheet::Sheet* pcSheet = static_cast<Spreadsheet::Sheet*>(
|
||||
pcDoc->addObject("Spreadsheet::Sheet", file.fileNamePure().c_str()));
|
||||
|
||||
pcSheet->importFromFile(EncodedName, '\t', '"', '\\');
|
||||
pcSheet->execute();
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
throw Py::RuntimeError(e.what());
|
||||
}
|
||||
App::Document* pcDoc =
|
||||
App::GetApplication().newDocument(DocName ? DocName : QT_TR_NOOP("Unnamed"));
|
||||
load(pcDoc, EncodedName);
|
||||
|
||||
return Py::None();
|
||||
}
|
||||
@@ -102,18 +106,11 @@ private:
|
||||
std::string EncodedName = std::string(Name);
|
||||
PyMem_Free(Name);
|
||||
|
||||
try {
|
||||
Base::FileInfo file(EncodedName);
|
||||
App::Document* pcDoc = Gui::Application::Instance->activeDocument()->getDocument();
|
||||
Spreadsheet::Sheet* pcSheet = static_cast<Spreadsheet::Sheet*>(
|
||||
pcDoc->addObject("Spreadsheet::Sheet", file.fileNamePure().c_str()));
|
||||
|
||||
pcSheet->importFromFile(EncodedName, '\t', '"', '\\');
|
||||
pcSheet->execute();
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
throw Py::RuntimeError(e.what());
|
||||
App::Document* pcDoc = App::GetApplication().getDocument(DocName);
|
||||
if (!pcDoc) {
|
||||
pcDoc = App::GetApplication().newDocument(DocName ? DocName : QT_TR_NOOP("Unnamed"));
|
||||
}
|
||||
load(pcDoc, EncodedName);
|
||||
|
||||
return Py::None();
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include <QGridLayout>
|
||||
#include <QLabel>
|
||||
#include <QListView>
|
||||
#include <QMessageBox>
|
||||
#include <QPushButton>
|
||||
#include <QScrollArea>
|
||||
#include <QWidget>
|
||||
@@ -41,14 +42,16 @@
|
||||
#include "FileCardView.h"
|
||||
#include "FirstStartWidget.h"
|
||||
#include "FlowLayout.h"
|
||||
#include "Gui/Workbench.h"
|
||||
#include <Gui/Document.h>
|
||||
#include <App/DocumentObject.h>
|
||||
#include <App/Application.h>
|
||||
#include <Base/Interpreter.h>
|
||||
#include <Base/Tools.h>
|
||||
#include <Gui/Application.h>
|
||||
#include <Gui/Command.h>
|
||||
#include <Gui/MainWindow.h>
|
||||
#include <Gui/Document.h>
|
||||
#include <Gui/Workbench.h>
|
||||
#include <Gui/FileDialog.h>
|
||||
#include <Gui/View3DInventor.h>
|
||||
#include <Gui/View3DInventorViewer.h>
|
||||
#include <gsl/pointers>
|
||||
@@ -435,11 +438,22 @@ void StartView::fileCardSelected(const QModelIndex& index)
|
||||
auto file = index.data(static_cast<int>(Start::DisplayedFilesModelRoles::path)).toString();
|
||||
std::string escapedstr = Base::Tools::escapedUnicodeFromUtf8(file.toStdString().c_str());
|
||||
escapedstr = Base::Tools::escapeEncodeFilename(escapedstr);
|
||||
auto command = std::string("FreeCAD.loadFile('") + escapedstr + "')";
|
||||
try {
|
||||
Base::Interpreter().runString(command.c_str());
|
||||
Gui::Application::checkForRecomputes();
|
||||
postStart(PostStartBehavior::doNotSwitchWorkbench);
|
||||
QString filename = QString::fromStdString(escapedstr);
|
||||
QFileInfo fi(filename);
|
||||
if (!fi.exists() || !fi.isFile()) {
|
||||
QMessageBox::critical(Gui::getMainWindow(),
|
||||
tr("File not found"),
|
||||
tr("The file '%1' cannot be opened.").arg(filename));
|
||||
}
|
||||
else {
|
||||
// invokes appendFile()
|
||||
Gui::SelectModule::Dict dict = Gui::SelectModule::importHandler(filename);
|
||||
for (Gui::SelectModule::Dict::iterator it = dict.begin(); it != dict.end(); ++it) {
|
||||
Gui::Application::Instance->open(it.key().toUtf8(), it.value().toLatin1());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Base::PyException& e) {
|
||||
Base::Console().Error(e.getMessage().c_str());
|
||||
|
||||
Reference in New Issue
Block a user