Agrarsense
FileUtilities.cpp
Go to the documentation of this file.
1// Copyright (c) 2023 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#include "FileUtilities.h"
7
8#include "DesktopPlatformModule.h"
9#include "Misc/Paths.h"
10#include "HAL/PlatformProcess.h"
11
12#pragma warning (disable : 4702)
13
15{
16 FString AbsolutePath = FPaths::ConvertRelativePathToFull(Path);
17 if (FPaths::DirectoryExists(AbsolutePath))
18 {
19 FPlatformProcess::ExploreFolder(*AbsolutePath);
20 }
21}
22
24{
25 FString File;
26 TArray<FString> Files = OpenFileDialogReturnFiles(Path);
27
28 if (!Files.IsEmpty())
29 {
30 File = Files[0];
31 }
32
33 return File;
34}
35
36TArray<FString> UFileUtilities::OpenFileDialogReturnFiles(const FString& Path)
37{
38 TArray<FString> OutFiles;
39
40 // OpenFileDialog doesn't work in packaged builds.
41#if !WITH_EDITOR
42 return OutFiles;
43#endif
44
45 IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
46 if (!DesktopPlatform)
47 {
48 return OutFiles;
49 }
50
51 FString DefaultPath = Path.IsEmpty() ? FPaths::ProjectDir() : Path;
52
53 // Create a file picker dialog
54 bool bOpened = DesktopPlatform->OpenFileDialog(
55 nullptr,
56 NSLOCTEXT("FileUtilities", "OpenFileDialog", "Choose a file").ToString(),
57 DefaultPath,
58 FString(),
59 TEXT("All Files (*.*)|*.*"),
60 EFileDialogFlags::Multiple,
61 OutFiles
62 );
63
64 if (bOpened)
65 {
66 for (FString& FilePath : OutFiles)
67 {
68 // Convert relative path to full path
69 FilePath = FPaths::ConvertRelativePathToFull(FilePath);
70 }
71 }
72
73 return OutFiles;
74}
static TArray< FString > OpenFileDialogReturnFiles(const FString &Path)
static void OpenFileExplorer(FString Path)
static FString OpenFileDialogReturnFirst(const FString &Path)