Skip to content

Commit 3b95f49

Browse files
author
Roberto De Ioris
committed
fixed execute in sandbox
1 parent 0bbfb19 commit 3b95f49

14 files changed

+164
-79
lines changed

Source/PythonEditor/Private/PythonEditorStyle.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ void FPythonEditorStyle::Initialize()
5757
StyleSet->Set("PythonEditor.SaveAll.Small", new IMAGE_BRUSH("UI/SaveAll_40x", Icon16x16));
5858
StyleSet->Set("PythonEditor.Execute", new IMAGE_BRUSH("UI/Excute_x40", Icon40x40));
5959
StyleSet->Set("PythonEditor.Execute.Small", new IMAGE_BRUSH("UI/Excute_x40", Icon16x16));
60+
StyleSet->Set("PythonEditor.ExecuteInSandbox", new IMAGE_BRUSH("UI/Excute_x40", Icon40x40));
61+
StyleSet->Set("PythonEditor.ExecuteInSandbox.Small", new IMAGE_BRUSH("UI/Excute_x40", Icon16x16));
6062
}
6163

6264
const FSlateFontInfo Consolas10 = TTF_FONT("Font/DroidSansMono", 9);
@@ -100,7 +102,7 @@ void FPythonEditorStyle::Initialize()
100102

101103
StyleSet->Set("ProjectEditor.Icon.Project", new IMAGE_BRUSH("UI/FolderClosed", Icon16x16, FLinearColor(0.25f,0.25f,0.25f,1)));
102104
StyleSet->Set("ProjectEditor.Icon.Folder", new IMAGE_BRUSH("UI/FolderClosed", Icon16x16, FLinearColor(0.25f,0.25f,0.25f,1)));
103-
StyleSet->Set("ProjectEditor.Icon.File", new IMAGE_BRUSH("UI/GenericFile", Icon16x16));
105+
StyleSet->Set("ProjectEditor.Icon.File", new IMAGE_BRUSH("UI/PythonEditor_16x", Icon16x16));
104106
StyleSet->Set("ProjectEditor.Icon.GenericFile", new IMAGE_BRUSH("UI/GenericFile", Icon16x16));
105107
}
106108

Source/PythonEditor/Private/PythonProjectEditor.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ void FPythonProjectEditor::BindCommands()
239239
FCanExecuteAction::CreateSP(this, &FPythonProjectEditor::CanExecute)
240240
);
241241

242+
ToolkitCommands->MapAction(FPythonProjectEditorCommands::Get().ExecuteInSandbox,
243+
FExecuteAction::CreateSP(this, &FPythonProjectEditor::ExecuteInSandbox_Internal),
244+
FCanExecuteAction::CreateSP(this, &FPythonProjectEditor::CanExecute)
245+
);
246+
242247
}
243248

244249
void FPythonProjectEditor::CloseFileForEditing(UPythonProjectItem* Item)
@@ -417,11 +422,17 @@ bool FPythonProjectEditor::SaveAll()
417422

418423
return bResult;
419424
}
425+
420426
void FPythonProjectEditor::Execute_Internal()
421427
{
422428
Execute();
423429
}
424430

431+
void FPythonProjectEditor::ExecuteInSandbox_Internal()
432+
{
433+
Execute();
434+
}
435+
425436

426437
bool FPythonProjectEditor::Execute()
427438
{
@@ -434,6 +445,17 @@ bool FPythonProjectEditor::Execute()
434445
return true;
435446
}
436447

448+
bool FPythonProjectEditor::ExecuteInSandbox()
449+
{
450+
if (DocumentManager.IsValid() && DocumentManager->GetActiveTab().IsValid())
451+
{
452+
TSharedRef<SPythonEditor> PythonEditorRef = StaticCastSharedRef<SPythonEditor>(DocumentManager->GetActiveTab()->GetContent());
453+
PythonEditorRef->ExecuteInSandbox();
454+
}
455+
456+
return true;
457+
}
458+
437459

438460
bool FPythonProjectEditor::CanSaveAll() const
439461
{

Source/PythonEditor/Private/PythonProjectEditor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class FPythonProjectEditor : public FWorkflowCentricApplication, public FGCObjec
5959

6060
bool Execute();
6161

62+
bool ExecuteInSandbox();
63+
6264
FString GetNoneRepeatName();
6365

6466
private:
@@ -74,6 +76,8 @@ class FPythonProjectEditor : public FWorkflowCentricApplication, public FGCObjec
7476

7577
void Execute_Internal();
7678

79+
void ExecuteInSandbox_Internal();
80+
7781
bool CanNew() const;
7882

7983
bool CanSave() const;

Source/PythonEditor/Private/PythonProjectEditorCommands.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#define LOCTEXT_NAMESPACE "PythonProjectEditorCommands"
77

88

9-
FPythonProjectEditorCommands::FPythonProjectEditorCommands()
9+
FPythonProjectEditorCommands::FPythonProjectEditorCommands()
1010
: TCommands<FPythonProjectEditorCommands>("PythonEditor", LOCTEXT("General", "General"), NAME_None, FPythonEditorStyle::GetStyleSetName())
1111
{
1212
}
@@ -19,7 +19,8 @@ void FPythonProjectEditorCommands::RegisterCommands()
1919

2020
UI_COMMAND(Save, "Save", "Save the currently active document.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control, EKeys::S));
2121
UI_COMMAND(SaveAll, "Save All", "Save all open documents.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control | EModifierKey::Shift, EKeys::S));
22-
UI_COMMAND(Execute, "Execute", "Execute Current Python File.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control ,EKeys::Enter));
22+
UI_COMMAND(Execute, "Execute", "Execute Current Python File.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control, EKeys::Enter));
23+
UI_COMMAND(ExecuteInSandbox, "Execute In Sandbox", "Execute Current Python File in a Sandbox.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control | EModifierKey::Shift, EKeys::Enter));
2324

2425
}
2526

Source/PythonEditor/Private/PythonProjectEditorCommands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class FPythonProjectEditorCommands : public TCommands<FPythonProjectEditorComman
1414
TSharedPtr<FUICommandInfo> Save;
1515
TSharedPtr<FUICommandInfo> SaveAll;
1616
TSharedPtr<FUICommandInfo> Execute;
17+
TSharedPtr<FUICommandInfo> ExecuteInSandbox;
1718
/** Initialize commands */
1819
virtual void RegisterCommands() override;
1920
};

Source/PythonEditor/Private/PythonProjectEditorToolbar.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void FPythonProjectEditorToolbar::FillEditorToolbar(FToolBarBuilder& ToolbarBuil
3535
ToolbarBuilder.BeginSection(TEXT("CodeExcute"));
3636
{
3737
ToolbarBuilder.AddToolBarButton(FPythonProjectEditorCommands::Get().Execute);
38+
ToolbarBuilder.AddToolBarButton(FPythonProjectEditorCommands::Get().ExecuteInSandbox);
3839
}
3940
ToolbarBuilder.EndSection();
4041

Source/PythonEditor/Private/PythonProjectItem.cpp

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ UPythonProjectItem::UPythonProjectItem(const FObjectInitializer& ObjectInitializ
1212

1313
void UPythonProjectItem::RescanChildren()
1414
{
15-
if(Path.Len() > 0)
15+
if (Path.Len() > 0)
1616
{
1717
TArray<UPythonProjectItem*> DeleteChildren;
1818
for (const auto& Child : Children)
@@ -32,6 +32,7 @@ void UPythonProjectItem::RescanChildren()
3232
}
3333
}
3434
}
35+
3536
UPythonProjectItem* UPythonProjectItem::GetItemByPath(FString FullPath)
3637
{
3738
for (const auto& Child : Children)
@@ -48,26 +49,26 @@ void UPythonProjectItem::HandleDirectoryScanned(const FString& InPathName, EPyth
4849
{
4950
// check for a child that already exists
5051
bool bCreateNew = true;
51-
for(const auto& Child : Children)
52+
for (const auto& Child : Children)
5253
{
53-
if(Child->Type == InType && Child->Path == InPathName)
54+
if (Child->Type == InType && Child->Path == InPathName)
5455
{
5556
bCreateNew = false;
5657
break;
5758
}
5859
}
5960

6061
// create children now & kick off their scan
61-
if(bCreateNew)
62+
if (bCreateNew)
6263
{
63-
if (InType == EPythonProjectItemType::File && FPaths::GetExtension(InPathName)!= TEXT("py")) {
64+
if (InType == EPythonProjectItemType::File && FPaths::GetExtension(InPathName) != TEXT("py")) {
6465
return;
6566
}
6667
UPythonProjectItem* NewItem = NewObject<UPythonProjectItem>(GetOutermost(), UPythonProjectItem::StaticClass());
6768
NewItem->Type = InType;
6869
NewItem->Path = InPathName;
6970
NewItem->Name = FPaths::GetBaseFilename(InPathName);
70-
if(InType != EPythonProjectItemType::Folder)
71+
if (InType != EPythonProjectItemType::Folder)
7172
{
7273
NewItem->Extension = FPaths::GetExtension(InPathName);
7374
}
@@ -76,17 +77,17 @@ void UPythonProjectItem::HandleDirectoryScanned(const FString& InPathName, EPyth
7677

7778
Children.Sort(
7879
[](const UPythonProjectItem& ItemA, const UPythonProjectItem& ItemB) -> bool
80+
{
81+
if (ItemA.Type != ItemB.Type)
7982
{
80-
if(ItemA.Type != ItemB.Type)
81-
{
82-
return ItemA.Type < ItemB.Type;
83-
}
84-
85-
return ItemA.Name.Compare(ItemB.Name) < 0;
83+
return ItemA.Type < ItemB.Type;
8684
}
85+
86+
return ItemA.Name.Compare(ItemB.Name) < 0;
87+
}
8788
);
8889

89-
if(InType == EPythonProjectItemType::Folder)
90+
if (InType == EPythonProjectItemType::Folder)
9091
{
9192
FDirectoryScanner::AddDirectory(InPathName, FOnDirectoryScanned::CreateUObject(NewItem, &UPythonProjectItem::HandleDirectoryScanned));
9293
}
@@ -96,28 +97,28 @@ void UPythonProjectItem::HandleDirectoryScanned(const FString& InPathName, EPyth
9697
void UPythonProjectItem::HandleDirectoryChanged(const TArray<FFileChangeData>& FileChanges)
9798
{
9899
// @TODO: dynamical update directory watchers so we can update the view in real-time
99-
for(const auto& Change : FileChanges)
100+
for (const auto& Change : FileChanges)
100101
{
101-
switch(Change.Action)
102+
switch (Change.Action)
102103
{
103104
default:
104105
case FFileChangeData::FCA_Unknown:
105106
break;
106107
case FFileChangeData::FCA_Added:
107-
{
108+
{
108109

109-
}
110-
break;
110+
}
111+
break;
111112
case FFileChangeData::FCA_Modified:
112-
{
113+
{
113114

114-
}
115-
break;
115+
}
116+
break;
116117
case FFileChangeData::FCA_Removed:
117-
{
118+
{
118119

119-
}
120-
break;
120+
}
121+
break;
121122
}
122123
}
123124
}

Source/PythonEditor/Private/SProjectViewItem.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
void SProjectViewItem::Construct(const FArguments& InArgs)
1212
{
13-
TreeItem=InArgs._TreeItem;
13+
TreeItem = InArgs._TreeItem;
1414
OnNameChanged = InArgs._OnNameChanged;
1515
ChildSlot
16-
[
17-
SNew(SHorizontalBox)
18-
+SHorizontalBox::Slot()
16+
[
17+
SNew(SHorizontalBox)
18+
+ SHorizontalBox::Slot()
1919
.HAlign(HAlign_Left)
2020
.VAlign(VAlign_Center)
2121
.Padding(1.0f)
@@ -24,18 +24,18 @@ void SProjectViewItem::Construct(const FArguments& InArgs)
2424
SNew(SImage)
2525
.Image(FPythonEditorStyle::Get().GetBrush(InArgs._IconName))
2626
]
27-
+SHorizontalBox::Slot()
27+
+ SHorizontalBox::Slot()
2828
.HAlign(HAlign_Left)
2929
.VAlign(VAlign_Center)
3030
.Padding(1.0f)
3131
.FillWidth(1.0f)
3232
[
33-
SAssignNew(InlineRenameWidget,SInlineEditableTextBlock)
33+
SAssignNew(InlineRenameWidget, SInlineEditableTextBlock)
3434
.Text(InArgs._Text)
35-
.OnTextCommitted(this, &SProjectViewItem::HandleNameCommitted)
36-
.IsSelected(InArgs._IsSelected)
35+
.OnTextCommitted(this, &SProjectViewItem::HandleNameCommitted)
36+
.IsSelected(InArgs._IsSelected)
3737
]
38-
];
38+
];
3939
}
4040

4141
void SProjectViewItem::HandleNameCommitted(const FText& NewText, ETextCommit::Type)
@@ -48,8 +48,8 @@ void SProjectViewItem::HandleNameCommitted(const FText& NewText, ETextCommit::Ty
4848
TreeItem->Path.Split(TEXT("/"), &Path, NULL, ESearchCase::CaseSensitive, ESearchDir::FromEnd);
4949
FString NewPath = Path + TEXT("/") + NewText.ToString() + TEXT(".py");
5050
if (!FPaths::FileExists(NewPath)) {
51-
int Result = rename( TCHAR_TO_ANSI(*OldPath), TCHAR_TO_ANSI(*NewPath));
52-
if (Result==0) {
51+
int Result = rename(TCHAR_TO_ANSI(*OldPath), TCHAR_TO_ANSI(*NewPath));
52+
if (Result == 0) {
5353
TreeItem->Name = NewText.ToString();
5454
TreeItem->Path = NewPath;
5555
InlineRenameWidget->SetText(TreeItem->Name);

Source/PythonEditor/Private/SPythonEditableText.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#include "SPythonEditableText.h"
55

66

7-
void SPythonEditableText::Construct( const FArguments& InArgs )
7+
void SPythonEditableText::Construct(const FArguments& InArgs)
88
{
9-
9+
1010
SMultiLineEditableText::Construct(
1111
SMultiLineEditableText::FArguments()
1212
.Font(FPythonEditorStyle::Get().GetWidgetStyle<FTextBlockStyle>("TextEditor.NormalText").Font)
@@ -18,7 +18,7 @@ void SPythonEditableText::Construct( const FArguments& InArgs )
1818
.HScrollBar(InArgs._HScrollBar)
1919
.VScrollBar(InArgs._VScrollBar)
2020
.OnTextChanged(InArgs._OnTextChanged)
21-
);
21+
);
2222
OnExecuted = InArgs._OnExecuted;
2323
}
2424

@@ -32,7 +32,7 @@ FReply SPythonEditableText::OnKeyChar(const FGeometry& MyGeometry, const FCharac
3232
return Reply;
3333
}
3434
Reply = FReply::Handled();
35-
if(Character == TEXT('\t'))
35+
if (Character == TEXT('\t'))
3636
{
3737
FString String;
3838
String.AppendChar(Character);
@@ -65,7 +65,7 @@ FReply SPythonEditableText::OnKeyChar(const FGeometry& MyGeometry, const FCharac
6565
//}
6666
else
6767
{
68-
Reply = SMultiLineEditableText::OnKeyChar( MyGeometry, InCharacterEvent );
68+
Reply = SMultiLineEditableText::OnKeyChar(MyGeometry, InCharacterEvent);
6969
}
7070

7171
return Reply;

Source/PythonEditor/Private/SPythonEditor.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ void SPythonEditor::Execute() const
9999
PythonModule.RunString(TCHAR_TO_UTF8(*SelectionString));
100100
}
101101

102+
void SPythonEditor::ExecuteInSandbox() const
103+
{
104+
Save();
105+
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
106+
107+
FString SelectionString = PythonEditableText->GetSelectedText().ToString();
108+
if (SelectionString.Len() == 0) {
109+
SelectionString = PythonEditableText->GetText().ToString();
110+
}
111+
PythonModule.RunStringSandboxed(TCHAR_TO_UTF8(*SelectionString));
112+
}
113+
102114

103115
void SPythonEditor::GotoLineAndColumn(int32 LineNumber, int32 ColumnNumber)
104116
{

0 commit comments

Comments
 (0)