Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
BornAgain
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mlz
BornAgain
Commits
6a5feebe
Commit
6a5feebe
authored
3 years ago
by
Matthias Puchner
Browse files
Options
Downloads
Patches
Plain Diff
class -> namespace (PyImportAssistant), docu
parent
bdf8c6e5
No related branches found
No related tags found
1 merge request
!535
refactor/simplify PyImport
Pipeline
#51825
passed
3 years ago
Stage: build
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
GUI/View/Main/PyImportAssistant.cpp
+51
-60
51 additions, 60 deletions
GUI/View/Main/PyImportAssistant.cpp
GUI/View/Main/PyImportAssistant.h
+8
-16
8 additions, 16 deletions
GUI/View/Main/PyImportAssistant.h
with
59 additions
and
76 deletions
GUI/View/Main/PyImportAssistant.cpp
+
51
−
60
View file @
6a5feebe
...
@@ -62,36 +62,9 @@ QString getCandidate(const QStringList& funcNames)
...
@@ -62,36 +62,9 @@ QString getCandidate(const QStringList& funcNames)
return
funcNames
.
front
();
return
funcNames
.
front
();
}
}
}
// namespace
std
::
unique_ptr
<
MultiLayer
>
PyImportAssistant
::
importMultiLayer
()
{
auto
fileName
=
fileNameToOpen
();
if
(
fileName
.
isEmpty
())
return
{};
QString
snippet
=
readFile
(
fileName
);
if
(
snippet
.
isEmpty
())
return
{};
QString
funcName
=
getPySampleFunctionName
(
snippet
);
if
(
funcName
.
isEmpty
())
return
{};
auto
multilayer
=
createMultiLayer
(
snippet
,
funcName
);
if
(
!
multilayer
)
return
{};
if
(
multilayer
->
sampleName
()
==
"Unnamed"
)
multilayer
->
setSampleName
(
GUI
::
Util
::
Path
::
baseName
(
fileName
).
toStdString
());
return
multilayer
;
}
//! Lets user to select Python file on disk.
//! Lets user to select Python file on disk.
QString
PyImportAssistant
::
fileNameToOpen
()
QString
fileNameToOpen
()
{
{
QString
dirname
=
ProjectManager
::
instance
()
->
userImportDir
();
QString
dirname
=
ProjectManager
::
instance
()
->
userImportDir
();
...
@@ -100,25 +73,16 @@ QString PyImportAssistant::fileNameToOpen()
...
@@ -100,25 +73,16 @@ QString PyImportAssistant::fileNameToOpen()
appSettings
->
useNativeFileDialog
()
?
QFileDialog
::
Options
()
appSettings
->
useNativeFileDialog
()
?
QFileDialog
::
Options
()
:
QFileDialog
::
DontUseNativeDialog
);
:
QFileDialog
::
DontUseNativeDialog
);
saveImportDir
(
result
);
if
(
!
result
.
isEmpty
())
ProjectManager
::
instance
()
->
setImportDir
(
GUI
::
Util
::
Path
::
fileDir
(
result
));
return
result
;
return
result
;
}
}
//! Saves file location as a future import dir.
void
PyImportAssistant
::
saveImportDir
(
const
QString
&
fileName
)
{
if
(
fileName
.
isEmpty
())
return
;
ProjectManager
::
instance
()
->
setImportDir
(
GUI
::
Util
::
Path
::
fileDir
(
fileName
));
}
//! Read content of text file and returns it as a multi-line string.
//! Read content of text file and returns it as a multi-line string.
//! Pop-ups warning dialog in the case of failure.
//! Pop-ups warning dialog in the case of failure.
QString
PyImportAssistant
::
readFile
(
const
QString
&
fileName
)
QString
readFile
(
const
QString
&
fileName
)
{
{
QFile
file
(
fileName
);
QFile
file
(
fileName
);
if
(
file
.
open
(
QIODevice
::
ReadOnly
|
QIODevice
::
Text
))
if
(
file
.
open
(
QIODevice
::
ReadOnly
|
QIODevice
::
Text
))
...
@@ -131,33 +95,31 @@ QString PyImportAssistant::readFile(const QString& fileName)
...
@@ -131,33 +95,31 @@ QString PyImportAssistant::readFile(const QString& fileName)
return
{};
return
{};
}
}
//!
Returns the name of function which might generate a MultiLayer in Python code snippet
.
//!
Creates a multi-layer by executing a funcName in embedded Python
.
//!
Pop-ups dialog and asks user for help in the case of doubts
.
//!
Function is supposed to be in code provided by 'snippet'
.
QString
PyImportAssistant
::
getPySampleFunctionName
(
const
QString
&
snippet
)
std
::
unique_ptr
<
MultiLayer
>
createMultiLayer
(
const
QString
&
snippet
,
const
QString
&
funcName
)
{
{
QStringList
funcLis
t
;
std
::
unique_ptr
<
MultiLayer
>
resul
t
;
QApplication
::
setOverrideCursor
(
Qt
::
WaitCursor
);
QApplication
::
setOverrideCursor
(
Qt
::
WaitCursor
);
try
{
try
{
auto
funcs
=
Py
::
Import
::
listOfFuncti
on
s
(
snippet
.
toStdString
(),
bornagainDir
());
result
=
Py
::
Import
::
createFromPyth
on
(
snippet
.
toStdString
(),
funcName
.
toStdString
(),
funcList
=
GUI
::
Util
::
String
::
fromStdStrings
(
funcs
);
bornagainDir
()
);
}
catch
(
const
std
::
exception
&
ex
)
{
}
catch
(
const
std
::
exception
&
ex
)
{
QApplication
::
restoreOverrideCursor
();
QApplication
::
restoreOverrideCursor
();
QString
message
(
"Exception thrown while
acquiring functions from Python code
.
\n\n
"
);
QString
message
(
"Exception thrown while
executing Python code to create multilayer
.
\n\n
"
);
QString
details
=
QString
::
fromStdString
(
std
::
string
(
ex
.
what
()));
QString
details
=
QString
::
fromStdString
(
std
::
string
(
ex
.
what
()));
DetailedMessageBox
(
baWin
,
"Python failure"
,
message
,
details
).
exec
();
DetailedMessageBox
(
baWin
,
"Python failure"
,
message
,
details
).
exec
();
return
""
;
}
}
QApplication
::
restoreOverrideCursor
();
QApplication
::
restoreOverrideCursor
();
return
selectPySampleFunction
(
funcList
)
;
return
result
;
}
}
//! Lets user select a function name which generates a MultiLayer.
//! Lets user select a function name which generates a MultiLayer.
QString
PyImportAssistant
::
selectPySampleFunction
(
const
QStringList
&
funcNames
)
QString
selectPySampleFunction
(
const
QStringList
&
funcNames
)
{
{
QString
result
;
QString
result
;
...
@@ -180,29 +142,58 @@ QString PyImportAssistant::selectPySampleFunction(const QStringList& funcNames)
...
@@ -180,29 +142,58 @@ QString PyImportAssistant::selectPySampleFunction(const QStringList& funcNames)
return
result
;
return
result
;
}
}
//!
Creates a multi-layer by executing a funcName in embedded Python
.
//!
Returns the name of function which might generate a MultiLayer in Python code snippet
.
//!
Function is supposed to be in code provided by 'snippet'
.
//!
Pop-ups dialog and asks user for help in the case of doubts
.
std
::
unique_ptr
<
MultiLayer
>
PyImportAssistant
::
createMultiLayer
(
const
QString
&
snippet
,
QString
getPySampleFunctionName
(
const
QString
&
snippet
)
const
QString
&
funcName
)
{
{
std
::
unique_ptr
<
MultiLayer
>
resul
t
;
QStringList
funcLis
t
;
QApplication
::
setOverrideCursor
(
Qt
::
WaitCursor
);
QApplication
::
setOverrideCursor
(
Qt
::
WaitCursor
);
try
{
try
{
result
=
Py
::
Import
::
createFromPyth
on
(
snippet
.
toStdString
(),
funcName
.
toStdString
(),
auto
funcs
=
Py
::
Import
::
listOfFuncti
on
s
(
snippet
.
toStdString
(),
bornagainDir
());
bornagainDir
()
);
funcList
=
GUI
::
Util
::
String
::
fromStdStrings
(
funcs
);
}
catch
(
const
std
::
exception
&
ex
)
{
}
catch
(
const
std
::
exception
&
ex
)
{
QApplication
::
restoreOverrideCursor
();
QApplication
::
restoreOverrideCursor
();
QString
message
(
"Exception thrown while
executing Python code to create multilayer
.
\n\n
"
);
QString
message
(
"Exception thrown while
acquiring functions from Python code
.
\n\n
"
);
QString
details
=
QString
::
fromStdString
(
std
::
string
(
ex
.
what
()));
QString
details
=
QString
::
fromStdString
(
std
::
string
(
ex
.
what
()));
DetailedMessageBox
(
baWin
,
"Python failure"
,
message
,
details
).
exec
();
DetailedMessageBox
(
baWin
,
"Python failure"
,
message
,
details
).
exec
();
return
""
;
}
}
QApplication
::
restoreOverrideCursor
();
QApplication
::
restoreOverrideCursor
();
return
result
;
return
selectPySampleFunction
(
funcList
);
}
}
// namespace
std
::
unique_ptr
<
MultiLayer
>
PyImportAssistant
::
importMultiLayer
()
{
auto
fileName
=
fileNameToOpen
();
if
(
fileName
.
isEmpty
())
return
{};
QString
snippet
=
readFile
(
fileName
);
if
(
snippet
.
isEmpty
())
return
{};
QString
funcName
=
getPySampleFunctionName
(
snippet
);
if
(
funcName
.
isEmpty
())
return
{};
auto
multilayer
=
createMultiLayer
(
snippet
,
funcName
);
if
(
!
multilayer
)
return
{};
if
(
multilayer
->
sampleName
()
==
"Unnamed"
)
multilayer
->
setSampleName
(
GUI
::
Util
::
Path
::
baseName
(
fileName
).
toStdString
());
return
multilayer
;
}
}
//! Populates GUI models with domain multilayer.
//! Populates GUI models with domain multilayer.
MultiLayerItem
*
PyImportAssistant
::
populateModels
(
SampleModel
*
sampleModel
,
MultiLayerItem
*
PyImportAssistant
::
populateModels
(
SampleModel
*
sampleModel
,
...
...
This diff is collapsed.
Click to expand it.
GUI/View/Main/PyImportAssistant.h
+
8
−
16
View file @
6a5feebe
...
@@ -23,28 +23,20 @@ class SampleModel;
...
@@ -23,28 +23,20 @@ class SampleModel;
class
MaterialModel
;
class
MaterialModel
;
class
MultiLayerItem
;
class
MultiLayerItem
;
class
MultiLayer
;
class
MultiLayer
;
class
QString
;
class
QStringList
;
//! Assists in importing Python object to GUI models.
//! Assists in importing Python object to GUI models.
class
PyImportAssistant
{
namespace
PyImportAssistant
{
public:
//! Show select-file dialog, try to import via python, return created multilayer.
static
std
::
unique_ptr
<
MultiLayer
>
importMultiLayer
();
//!
//! If sth. went wrong, a dialog has presented already and nullptr is returned.
std
::
unique_ptr
<
MultiLayer
>
importMultiLayer
();
static
MultiLayerItem
*
populateModels
(
SampleModel
*
sampleModel
,
MaterialModel
*
materialModel
,
MultiLayerItem
*
populateModels
(
SampleModel
*
sampleModel
,
MaterialModel
*
materialModel
,
const
MultiLayer
&
multilayer
);
const
MultiLayer
&
multilayer
);
private:
}
// namespace PyImportAssistant
static
QString
fileNameToOpen
();
static
void
saveImportDir
(
const
QString
&
fileName
);
static
QString
readFile
(
const
QString
&
fileName
);
static
QString
getPySampleFunctionName
(
const
QString
&
snippet
);
static
QString
selectPySampleFunction
(
const
QStringList
&
funcNames
);
static
std
::
unique_ptr
<
MultiLayer
>
createMultiLayer
(
const
QString
&
snippet
,
const
QString
&
funcName
);
};
#endif // BORNAGAIN_PYTHON
#endif // BORNAGAIN_PYTHON
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment