Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show format within filename when exporting file #4389

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 52 additions & 2 deletions src/app/ui/export_file_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,60 @@ void ExportFileWindow::setAniDir(const doc::AniDir aniDir)
anidir()->setSelectedItemIndex(int(aniDir));
}

std::vector<std::string> split(const std::string& str,const char delimiter) {
std::vector<std::string> tokens;
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, delimiter)) {
if (!token.empty()) {
tokens.push_back(token);
}
}
return tokens;
}

std::string ExportFileWindow::getRelativeFilename(const std::string& filename) const
{
std::vector<std::string> baseDirs = split(base::get_file_path(m_doc->filename()), base::path_separator);
std::vector<std::string> toParts = split(filename, base::path_separator);

// Find the common prefix
auto itFrom = baseDirs.begin();
auto itTo = toParts.begin();

while (itFrom != baseDirs.end() && itTo != toParts.end() && *itFrom == *itTo) {
++itFrom;
++itTo;
}

// Calculate the number of directories to go up from base path
std::string relativePath;
for (auto it = itFrom; it != baseDirs.end(); ++it) {
std::string upDir("..");
upDir.push_back(base::path_separator);
relativePath += upDir;
}

// Append the remaining part of 'toPath'
for (auto it = itTo; it != toParts.end(); ++it) {
relativePath += *it;
if (it + 1 != toParts.end()) {
relativePath.push_back(base::path_separator);
}
}

return relativePath;
}

void ExportFileWindow::setOutputFilename(const std::string& pathAndFilename)
{
m_outputPath = base::get_file_path(pathAndFilename);
m_outputFilename = base::get_file_name(pathAndFilename);
if (m_doc->filename().empty() || base::get_file_path(m_doc->filename()).empty()) {
m_outputPath = base::get_file_path(pathAndFilename);
m_outputFilename = base::get_file_name(pathAndFilename);
} else {
m_outputPath = base::get_file_path(m_doc->filename());
m_outputFilename = getRelativeFilename(base::normalize_path(pathAndFilename));
}

updateOutputFilenameEntry();
}
Expand Down
1 change: 1 addition & 0 deletions src/app/ui/export_file_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace app {
void onAdjustResize();
void onOK();
std::string defaultExtension() const;
std::string getRelativeFilename(const std::string& filename) const;

const Doc* m_doc;
DocumentPreferences& m_docPref;
Expand Down