Skip to content

Commit

Permalink
Added save/load to/from user specified file to combat tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilInTheGaps committed Dec 19, 2023
1 parent 4e9b2c6 commit ef3d999
Show file tree
Hide file tree
Showing 19 changed files with 236 additions and 49 deletions.
2 changes: 2 additions & 0 deletions app/ui/FileDialog/filedialogbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Q_LOGGING_CATEGORY(gmFileDialog, "gm.files.dialog")

FileDialogBackend::FileDialogBackend(QObject *parent) : QObject(parent)
{
setCurrentDir(File::getHomeDir());

connect(this, &FileDialogBackend::currentDirChanged, &FileDialogBackend::onCurrentDirChanged);
}

Expand Down
2 changes: 1 addition & 1 deletion app/ui/FileDialog/filedialogbackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public slots:
void forward();
void back();
void createFolder(const QString &folderName);
void updateFileList();

signals:
void currentDirChanged(const QString &dir);
Expand All @@ -50,7 +51,6 @@ public slots:

QFuture<FileListResult> m_currentFuture;

void updateFileList();
void clearFileList();
void stopCurrentRequest();
void clearForward();
Expand Down
68 changes: 60 additions & 8 deletions app/ui/common/CustomFileDialog.qml
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,48 @@ Dialog {

modal: true

property alias selectedPath: selection_text_field.text
enum Mode {
OpenOne,
Save
}

function getSelectedPath() {
if (main_list_view.currentIndex >= 0) {
return backend.getSelected(main_list_view.currentIndex);
}
let result = backend.getSelected(main_list_view.currentIndex);
if (root.mode === CustomFileDialog.Mode.Save) {
result += "/" + selectedEntryName;
}
return result;
}

property alias selectedEntryName: selection_text_field.text
property alias folder: backend.currentDir
property alias foldersOnly: backend.folderMode
property var textField: undefined
property string replacePath: ""
property int mode: CustomFileDialog.Mode.OpenOne

FileDialogBackend {
id: backend
}

onOpened: {
backend.updateFileList();
if (root.mode === CustomFileDialog.Mode.Save) {
selection_text_field.forceActiveFocus();
selection_text_field.selectAll();
}
main_list_view.currentIndex = -1;
}

onAccepted: {
if (textField) {
if (replacePath) {
textField.text = selection_text_field.text.replace(replacePath, "");
textField.text = root.getSelectedPath().replace(replacePath, "");
} else {
textField.text = selection_text_field.text;
textField.text = root.getSelectedPath();
}
if (textField.savePath) {
textField.savePath();
Expand Down Expand Up @@ -84,6 +110,8 @@ Dialog {

onClicked: {
new_folder_form.visible = !new_folder_form.visible;
new_folder_form_row_text_field.forceActiveFocus();
new_folder_form_row_text_field.selectAll();
}
}
}
Expand Down Expand Up @@ -114,6 +142,7 @@ Dialog {
}

TextField {
id: new_folder_form_row_text_field
placeholderText: qsTr("New Folder")
height: parent.height
anchors.verticalCenter: parent.verticalCenter
Expand Down Expand Up @@ -147,8 +176,8 @@ Dialog {
visible: main_list_view.contentHeight > main_list_view.height
}

onCurrentIndexChanged: {
selection_text_field.text = backend.getSelected(currentIndex);
onModelChanged: {
currentIndex = -1;
}

model: backend.entries
Expand Down Expand Up @@ -197,8 +226,25 @@ Dialog {
id: delegate_mouse_area
anchors.fill: delegate_item
hoverEnabled: true
onClicked: main_list_view.currentIndex = delegate_item.index
onDoubleClicked: backend.enterFolder(delegate_item.index)

onClicked: {
main_list_view.currentIndex = delegate_item.index;
if (root.mode === CustomFileDialog.Mode.Save) {
selection_text_field.text = delegate_item.modelData.name;
}
}

onDoubleClicked: {
if (delegate_item.modelData.isFolder) {
backend.enterFolder(delegate_item.index);
return;
}
main_list_view.currentIndex = delegate_item.index;
if (root.mode === CustomFileDialog.Mode.Save) {
selection_text_field.text = delegate_item.modelData.name;
}
root.accept();
}
}
}

Expand All @@ -219,6 +265,12 @@ Dialog {
anchors.left: parent.left
anchors.right: buttons.left
anchors.margins: 5

visible: root.mode === CustomFileDialog.Mode.Save

onAccepted: {
root.accept();
}
}

Row {
Expand All @@ -231,7 +283,7 @@ Dialog {

Button {
id: open_button
text: qsTr("Open")
text: root.mode === CustomFileDialog.Mode.Save ? qsTr("Save") : qsTr("Open")

anchors.top: parent.top
anchors.bottom: parent.bottom
Expand Down
41 changes: 40 additions & 1 deletion app/ui/tools/CombatTracker.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CustomComponents
import IconFonts
import src
import ".."
import "../common"
import "./combat_tracker"

Page {
Expand All @@ -14,6 +15,23 @@ Page {

Component.onCompleted: CombatTrackerTool.loadData()

CustomFileDialog {
id: file_dialog

onAccepted: {
switch (file_dialog.mode) {
case CustomFileDialog.Mode.OpenOne:
CombatTrackerTool.loadFile(file_dialog.getSelectedPath());
break;
case CustomFileDialog.Mode.Save:
CombatTrackerTool.saveFile(file_dialog.getSelectedPath());
break;
default:
break;
}
}
}

// Top Bar
header: ToolBar {
id: top_bar
Expand All @@ -36,7 +54,28 @@ Page {
buttonText: qsTr("Add")
usesFixedWidth: false
onClicked: {
add_rect.visible = !add_rect.visible
add_rect.visible = !add_rect.visible;
}
}

CustomToolBarButton {
iconText: FontAwesome.fileArrowUp
buttonText: qsTr("Load")
usesFixedWidth: false
onClicked: {
file_dialog.mode = CustomFileDialog.Mode.OpenOne;
file_dialog.open();
}
}

CustomToolBarButton {
iconText: FontAwesome.fileArrowDown
buttonText: qsTr("Save")
usesFixedWidth: false
onClicked: {
file_dialog.mode = CustomFileDialog.Mode.Save;
file_dialog.selectedEntryName = "combat-state.json";
file_dialog.open();
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/ui/tools/audio/editor/views/EditorFileListView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Item {

foldersOnly: true
onAccepted: {
AudioTool.editor.replaceFileFolder(index, selectedPath);
AudioTool.editor.replaceFileFolder(index, getSelectedPath());
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/filesystem/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ auto File::checkAsync(const QStringList &paths, Options options, std::shared_ptr
return access ? access->checkAsync(paths, options) : QFuture<FileMultiCheckResult>();
}

auto File::getHomeDir(std::shared_ptr<FileAccess> fileAccess) -> QString
{
auto access = getFileAccess(fileAccess);
return access ? access->getHomeDir() : u"/"_s;
}

void File::updateFileAccess()
{
const auto cloudMode = SettingsManager::instance()->get(u"cloudMode"_s, u"local"_s);
Expand Down
2 changes: 2 additions & 0 deletions src/filesystem/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class File
static auto checkAsync(const QStringList &paths, Options options = Option::AllowCache,
std::shared_ptr<FileAccess> fileAccess = nullptr) -> QFuture<FileMultiCheckResult>;

static auto getHomeDir(std::shared_ptr<FileAccess> fileAccess = nullptr) -> QString;

static void updateFileAccess();

private:
Expand Down
1 change: 1 addition & 0 deletions src/filesystem/fileaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class FileAccess
virtual auto createDirAsync(const QString &path) -> QFuture<FileResult> = 0;
virtual auto checkAsync(const QString &path, Options options) -> QFuture<FileCheckResult> = 0;
virtual auto checkAsync(const QStringList &paths, Options options) -> QFuture<FileMultiCheckResult> = 0;
virtual auto getHomeDir() -> QString = 0;

static auto getInstance() -> std::shared_ptr<FileAccess>
{
Expand Down
11 changes: 8 additions & 3 deletions src/filesystem/fileaccesslocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,18 @@ auto FileAccessLocal::checkAsync(const QStringList &paths, Options options) -> Q
.then(&m_context, [](FileMultiCheckResult &&result) { return std::move(result); });
}

auto FileAccessLocal::getHomeDir() -> QString
{
return QDir::homePath();
}

auto FileAccessLocal::getDirFilter(bool files, bool folders) -> QFlags<QDir::Filter>
{
if (files && folders) return QDir::NoDotAndDotDot | QDir::AllEntries;
if (files && folders) return QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden;

if (files) return QDir::NoDotAndDotDot | QDir::Files;
if (files) return QDir::NoDotAndDotDot | QDir::Files | QDir::Hidden;

if (folders) return QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Drives;
if (folders) return QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Drives | QDir::Hidden;

return QDir::NoDotAndDotDot;
}
1 change: 1 addition & 0 deletions src/filesystem/fileaccesslocal.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class FileAccessLocal : public FileAccess
auto createDirAsync(const QString &path) -> QFuture<FileResult> override;
auto checkAsync(const QString &path, Options options) -> QFuture<FileCheckResult> override;
auto checkAsync(const QStringList &paths, Options options) -> QFuture<FileMultiCheckResult> override;
auto getHomeDir() -> QString override;

private:
static auto getData(const QString &path) -> FileDataResult;
Expand Down
5 changes: 5 additions & 0 deletions src/filesystem/fileaccessnextcloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ auto FileAccessNextcloud::checkAsync(const QStringList &paths, Options options)
return FileAccess::multiCheckAsync(MultiGetHelper<FileCheckResult>(paths), options);
}

auto FileAccessNextcloud::getHomeDir() -> QString
{
return u"/"_s;
}

auto FileAccessNextcloud::encodePath(const QString &data) -> QByteArray
{
return QUrl::toPercentEncoding(data, "/");
Expand Down
1 change: 1 addition & 0 deletions src/filesystem/fileaccessnextcloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class FileAccessNextcloud : public FileAccess
auto createDirAsync(const QString &path) -> QFuture<FileResult> override;
auto checkAsync(const QString &path, Options options) -> QFuture<FileCheckResult> override;
auto checkAsync(const QStringList &paths, Options options) -> QFuture<FileMultiCheckResult> override;
auto getHomeDir() -> QString override;

private:
Services::NextCloud &m_nc;
Expand Down
Loading

0 comments on commit ef3d999

Please sign in to comment.