WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 41044e3

Browse files
committed
Return status when opening .dot file (even in python thread)
1 parent 370ca4c commit 41044e3

File tree

8 files changed

+41
-14
lines changed

8 files changed

+41
-14
lines changed

plugins/dot_viewer/deps/QGVCore/QGVScene.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ QGVScene::QGVScene(QObject *parent)
8181

8282
QGVScene::~QGVScene()
8383
{
84+
for (QGraphicsItem* item : items())
85+
item->setSelected(false);
8486
gvFreeLayout(_context->context(), _graph->graph());
8587
agclose(_graph->graph());
8688
gvFreeContext(_context->context());

plugins/dot_viewer/include/dot_viewer/dot_viewer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ namespace hal
5959
static DotViewer* getDotviewerInstance();
6060
QString filename() const { return mFilename; }
6161
QString creatorPlugin() const { return mCreatorPlugin; }
62+
bool loadDotFile(const QString& fileName, const QString& creator = QString());
6263

6364
public Q_SLOTS:
6465
void handleOpenInputFileByName(const QString& fileName, const QString& creator = QString());
@@ -84,7 +85,7 @@ namespace hal
8485
Q_OBJECT
8586
public:
8687
DotViewerCallFromTread(QObject* parent = nullptr) : QObject(parent) {;}
87-
void emitOpenInputFileByName(DotViewer* callee, QString filename, QString plugin);
88+
bool openInputFileByName(DotViewer* callee, QString filename, QString plugin);
8889
Q_SIGNALS:
8990
void callOpenInputFileByName(QString filename, QString plugin);
9091
};

plugins/dot_viewer/include/dot_viewer/interaction/solve_fsm_interaction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace hal {
3737
Q_OBJECT
3838
public:
3939
SolveFsmInteraction(QGVScene* parent);
40+
~SolveFsmInteraction();
4041

4142
void registerNode(QGVNode* node) override;
4243
void registerEdge(QGVEdge* edge) override;

plugins/dot_viewer/python/python_bindings.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,19 @@ namespace hal
8282

8383
m.def(
8484
"load_dot_file",
85-
[](const std::filesystem::path& path, const std::string& creator_plugin = "") {
85+
[](const std::filesystem::path& path, const std::string& creator_plugin = "") -> bool {
8686
QString qfilename = QString::fromStdString(path.string());
8787
QString qcreator = QString::fromStdString(creator_plugin);
8888
DotViewer* dv = DotViewer::getDotviewerInstance();
8989
if (dv)
9090
{
91-
dv->handleOpenInputFileByName(qfilename, qcreator);
91+
return dv->loadDotFile(qfilename, qcreator);
9292
}
9393
else
9494
{
9595
log_error("python_context", "Cannot find dot viewer instance.");
9696
}
97+
return false;
9798
},
9899
py::arg("path"),
99100
py::arg("creator_plugin") = std::string(),
@@ -102,6 +103,8 @@ namespace hal
102103
103104
:param pathlib.Path path: The path to the ``.dot`` file.
104105
:param str creator_plugin: The name of plugin that created the ``.dot`` file. Will try to detect from content or query by popup if empty.
106+
:returns: True on success, false otherwise.
107+
:rtype: bool
105108
)");
106109

107110
#ifndef PYBIND11_MODULE

plugins/dot_viewer/src/dot_viewer.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,25 @@ namespace hal
9797
void DotViewer::handleOpenInputFileDialog()
9898
{
9999
QString filename = QFileDialog::getOpenFileName(this, "Open dot file", ".", "Dot files (*.dot);;All files (*)");
100-
if (!filename.isEmpty()) handleOpenInputFileByName(filename);
100+
if (!filename.isEmpty()) loadDotFile(filename);
101101
}
102102

103103
void DotViewer::handleOpenInputFileByName(const QString& fileName, const QString &creator)
104+
{
105+
loadDotFile(fileName,creator);
106+
}
107+
108+
bool DotViewer::loadDotFile(const QString& fileName, const QString &creator)
104109
{
105110
if (dynamic_cast<PythonThread*>(QThread::currentThread()))
106111
{
107112
// call from differnt thread, we cannot create GUI objects directly
108113
DotViewerCallFromTread* dvcft = new DotViewerCallFromTread;
109-
dvcft->emitOpenInputFileByName(this, fileName, creator);
114+
bool retval = dvcft->openInputFileByName(this, fileName, creator);
110115
delete dvcft;
111-
return;
116+
return retval;
112117
}
113118

114-
mFilename = fileName;
115119
mCreatorPlugin = creator;
116120

117121
const char* halComment = "\"created by HAL plugin ";
@@ -121,9 +125,17 @@ namespace hal
121125
mDotGraphicsView->setScene(mDotScene);
122126
toDispose->deleteLater();
123127

124-
if (fileName.isEmpty()) return;
128+
if (fileName.isEmpty()) {
129+
log_warning("dot_viewer", "Cannot load dot file, no file name provided");
130+
return false;
131+
}
125132
QFile ff(fileName);
126-
if (!ff.open(QIODevice::ReadOnly)) return;
133+
if (!ff.open(QIODevice::ReadOnly)) {
134+
log_warning("dot_viewer", "An error occurred loading dot file '{}'", fileName.toStdString());
135+
return false;
136+
}
137+
138+
mFilename = fileName;
127139
QByteArray dotfileContent = ff.readAll();
128140

129141
// Determine plugin name to construct correct interaction class
@@ -163,6 +175,7 @@ namespace hal
163175

164176
// interact might be nullptr but that is OK (no interactions in this case)
165177
mDotScene->loadLayout(dotfileContent, interact);
178+
return true;
166179
}
167180

168181
void DotViewer::handleColorSelect()
@@ -195,11 +208,12 @@ namespace hal
195208
}
196209
}
197210

198-
void DotViewerCallFromTread::emitOpenInputFileByName(DotViewer* callee, QString filename, QString plugin)
211+
bool DotViewerCallFromTread::openInputFileByName(DotViewer* callee, QString filename, QString plugin)
199212
{
200-
if (!callee) return;
213+
if (!callee) return false;
201214
connect(this, &DotViewerCallFromTread::callOpenInputFileByName, callee, &DotViewer::handleOpenInputFileByName, Qt::BlockingQueuedConnection);
202215
Q_EMIT callOpenInputFileByName(filename, plugin);
203216
disconnect(this, &DotViewerCallFromTread::callOpenInputFileByName, callee, &DotViewer::handleOpenInputFileByName);
217+
return (callee->filename() == filename); // callee will not store filename upon load error
204218
}
205219
} // namespace hal

plugins/dot_viewer/src/dot_viewer_factory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace hal {
2020
ExternalContentWidget* DotViewerFactory::contentFactory() const
2121
{
2222
DotViewer* dv = new DotViewer(name());
23-
if (!mFilename.isEmpty()) dv->handleOpenInputFileByName(mFilename, mCreator);
23+
if (!mFilename.isEmpty()) dv->loadDotFile(mFilename, mCreator);
2424
return dv;
2525
}
2626

@@ -85,6 +85,6 @@ namespace hal {
8585
if (!dv) return;
8686

8787
if (!mFilename.isEmpty())
88-
dv->handleOpenInputFileByName(mFilename, mCreator);
88+
dv->loadDotFile(mFilename, mCreator);
8989
}
9090
}

plugins/dot_viewer/src/gui_extension_dot_viewer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace hal {
3838
if (loadClicked)
3939
{
4040
DotViewer* dv = DotViewer::getDotviewerInstance();
41-
if (dv) dv->handleOpenInputFileByName(filename,plugin);
41+
if (dv) dv->loadDotFile(filename,plugin);
4242
}
4343
}
4444

plugins/dot_viewer/src/interaction/solve_fsm_interaction.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ namespace hal {
3232
connect(parent, &QGraphicsScene::changed, this, &SolveFsmInteraction::handleSceneChanged);
3333
}
3434

35+
SolveFsmInteraction::~SolveFsmInteraction()
36+
{
37+
std::cerr << "SolveFsmInteraction::~SolveFsmInteraction()" << std::endl;
38+
}
39+
40+
3541
void SolveFsmInteraction::handleSceneChanged(const QList<QRectF>& changedArea)
3642
{
3743
Q_UNUSED(changedArea);

0 commit comments

Comments
 (0)