Relativity Seems like a good idea here!

This commit is contained in:
Timothy Warren 2019-05-24 16:16:40 -04:00
parent 9a2f12f06b
commit 49628f042d
3 changed files with 120 additions and 33 deletions

View File

@ -1,4 +1,3 @@
#include <libgen.h>
#include <unordered_set> #include <unordered_set>
#include "src/widgets/FilePane.h" #include "src/widgets/FilePane.h"
@ -29,8 +28,8 @@ FilePane::FilePane(
wxString defaultPath("."); wxString defaultPath(".");
wxFileName filename(defaultPath); wxFileName filename(defaultPath);
filename.MakeAbsolute(defaultPath); filename.MakeAbsolute(defaultPath);
wxTreeListItem root = this->GetRootItem();
this->CreateTree(defaultPath, root); this->CreateTree(defaultPath);
this->AppendColumn("", this->AppendColumn("",
wxCOL_WIDTH_AUTOSIZE, wxCOL_WIDTH_AUTOSIZE,
@ -64,8 +63,10 @@ void FilePane::OpenFolder(wxTreeListEvent& event)
* *
* @access private * @access private
*/ */
void FilePane::CreateTree(const wxString &path, wxTreeListItem &root) void FilePane::CreateTree(const wxString &path)
{ {
wxTreeListItem root = this->GetRootItem();
auto *files = new wxArrayString(); auto *files = new wxArrayString();
wxFileName rootPath(path); wxFileName rootPath(path);
rootPath.MakeAbsolute(); rootPath.MakeAbsolute();
@ -83,21 +84,22 @@ void FilePane::CreateTree(const wxString &path, wxTreeListItem &root)
// If the file is at the root, add it to the tree // If the file is at the root, add it to the tree
if (fileName.GetDirCount() == rootPath.GetDirCount()) if (fileName.GetDirCount() == rootPath.GetDirCount())
{ {
this->AddDirFiles(path, root); this->AddDirFiles(root, path);
} }
auto dir = std::string(fileName.GetPath()); auto dir = std::string(fileName.GetPath());
if (dir.empty()) if (dir.empty() || BaseName(this->base_path) == BaseName(dir))
{ {
continue; continue;
} }
// Append the folder to the tree // Append the folder to the tree
wxString wdir = wxString(dir); wxString wdir = wxString(dir);
// wxLogDebug("Creating Dir Tree: %s", wdir); this->AddDirToTree(root, BaseName(dir), wxString(""));
this->AddDirToTree(this->BaseName(wdir), root, wxString(""));
} }
delete files;
} }
/** /**
@ -105,16 +107,16 @@ void FilePane::CreateTree(const wxString &path, wxTreeListItem &root)
* *
* @access private * @access private
*/ */
void FilePane::AddDirToTree(const wxString &path, wxTreeListItem &root, const wxString &parent) void FilePane::AddDirToTree(wxTreeListItem &root, const wxString &path, const wxString &parent)
{ {
auto pathBase = this->BaseName(path); auto pathBase = BaseName(path);
auto fullPath = this->base_path; auto fullPath = this->base_path;
if ( ! parent.empty()) if ( ! parent.empty())
{ {
fullPath += "/"; fullPath += "/";
wxString par = parent.Clone(); auto par = parent.Clone();
par.Replace((wxString)this->base_path, ""); par.Replace((wxString)this->base_path, "");
fullPath += par.ToStdString(); fullPath += par.ToStdString();
@ -128,8 +130,13 @@ void FilePane::AddDirToTree(const wxString &path, wxTreeListItem &root, const wx
wxFileName parentDir(fullPath); wxFileName parentDir(fullPath);
parentDir.MakeAbsolute(); parentDir.MakeAbsolute();
parentDir.Normalize();
wxLogDebug("Rendering Dir Tree for %s, full path: %s", path, fullPath); auto parentDirs = parentDir.GetDirs();
wxString wFullPath(fullPath);
wxLogInfo("Rendering Dir Tree for %s, full path: %s", path, fullPath);
auto *files = new wxArrayString(); auto *files = new wxArrayString();
wxDir::GetAllFiles(fullPath, files); wxDir::GetAllFiles(fullPath, files);
@ -137,12 +144,88 @@ void FilePane::AddDirToTree(const wxString &path, wxTreeListItem &root, const wx
for (const wxString &item: *files) for (const wxString &item: *files)
{ {
wxFileName fileName(item); wxFileName fileName(item);
fileName.MakeRelativeTo(fullPath);
auto dir = std::string(fileName.GetPath()); auto dir = std::string(fileName.GetPath());
wxLogDebug("File %s in %s", fileName.GetFullName(), fullPath);
wxLogDebug("Dir %s in %s", dir, fullPath);
// Add files
if (fileName.GetDirCount() == 1)
{
this->AddDirFiles(root, fullPath);
}
// Stop early if folder exists
/*auto it = this->dir_set.find(std::string(dir));
if (it != this->dir_set.end())
{
continue;
}
if (fileName.GetDirCount() < parentDir.GetDirCount() || ( ! item.Contains(wFullPath)))
{
continue;
}
auto dirs = fileName.GetDirs();
// Remove the last folder from the filename to pass as the parent dir
fileName.RemoveLastDir();
auto parentPath = fileName.GetPath();
parentPath.Replace((wxString)this->base_path, "");
parentPath.Replace("/", "");
if (parentDirs.GetCount() == dirs.GetCount())
{
continue;
}
for (auto i = 0; i < dirs.GetCount(); i++)
{
if (dirs[i] == "")
{
break;
}
// Skip path segments that already exist
if (parentDirs.GetCount() > i && parentDirs[i] == dirs[i])
{
continue;
}
if (parentDirs.GetCount() > i && parentDirs[i] != dirs[i])
{
wxLogWarning("Wat?! Where'd this path segment come from? :%s not in %s", dirs[i], parentPath);
continue;
}
wxLogDebug("Path segment to Add: %s, Base Dir: %s", dirs[i], fullPath);
auto fileData = new wxStringClientData();
fileData->SetData(dir);
auto dir_label = BaseName(dir);
auto dir_node = this->AppendItem(root, dir_label, Icon_FolderClosed, Icon_FolderOpened, fileData);
// wxLogDebug("Recursing for dir %s, from parent %s", parentPath, fullPath);
// this->AddDirToTree(dir_node, dir_label, parentPath);
break;
} */
/* auto dir = std::string(fileName.GetPath());
// Remove the last folder from the filename to pass as the parent dir
fileName.RemoveLastDir();
auto parentPath = fileName.GetPath();
parentPath.Replace((wxString)this->base_path, "");
wxString wdir(dir); wxString wdir(dir);
wxFileName dirName(dir); wxFileName dirName(dir);
if ( ! (wdir.Contains(pathBase) || dirName.GetDirCount() != (parentDir.GetDirCount() + 1))) if (( ! wdir.Contains((wxString) fullPath)) || dirName.GetDirCount() != parentDir.GetDirCount())
{ {
continue; continue;
} }
@ -157,22 +240,23 @@ void FilePane::AddDirToTree(const wxString &path, wxTreeListItem &root, const wx
auto fileData = new wxStringClientData(); auto fileData = new wxStringClientData();
fileData->SetData(dir); fileData->SetData(dir);
auto dir_label = this->BaseName(dir); auto dir_label = BaseName(dir);
auto dir_node = this->AppendItem(root, dir_label, Icon_FolderClosed, Icon_FolderOpened, fileData); auto dir_node = this->AppendItem(root, dir_label, Icon_FolderClosed, Icon_FolderOpened, fileData);
this->dir_set.insert(std::string(dir)); this->dir_set.insert(std::string(dir));
wxLogDebug("Recursing for dir %s, from parent %s", dir_label, fullPath); wxLogDebug("Recursing for dir %s, from parent %s", parentPath, fullPath);
this->AddDirFiles(fullPath, dir_node); this->AddDirToTree(dir_node, dir_label, parentPath); */
this->AddDirToTree(dir_label, dir_node, fullPath);
}
} }
void FilePane::AddDirFiles(const wxString &path, wxTreeListItem &root) // delete files;
}
void FilePane::AddDirFiles(wxTreeListItem &root, const wxString &path)
{ {
auto *files = new wxArrayString(); auto *files = new wxArrayString();
wxDir::GetAllFiles(path, files); wxDir::GetAllFiles(path, files, wxEmptyString, wxDIR_FILES);
wxFileName rootPath(path); wxFileName rootPath(path);
rootPath.MakeAbsolute(); rootPath.MakeAbsolute();
@ -197,11 +281,13 @@ void FilePane::AddDirFiles(const wxString &path, wxTreeListItem &root)
auto fileData = new wxStringClientData(); auto fileData = new wxStringClientData();
fileData->SetData(fileName.GetFullPath()); fileData->SetData(fileName.GetFullPath());
auto fileLabel = this->BaseName(fileName.GetFullName()); auto fileLabel = BaseName(fileName.GetFullName());
this->AppendItem(root, fileLabel, Icon_File, Icon_File, fileData); this->AppendItem(root, fileLabel, Icon_File, Icon_File, fileData);
this->file_set.insert(std::string(fileName.GetFullPath())); this->file_set.insert(std::string(fileName.GetFullPath()));
} }
delete files;
} }
/** /**
@ -248,11 +334,3 @@ void FilePane::InitImageList()
); );
} }
} }
wxString FilePane::BaseName(wxString path)
{
auto fullPath = path.char_str();
auto base = basename(fullPath);
return (wxString) base;
}

View File

@ -14,6 +14,7 @@ public:
const wxString &name=wxTreeListCtrlNameStr const wxString &name=wxTreeListCtrlNameStr
); );
~FilePane() override; ~FilePane() override;
void CreateTree(const wxString &path);
private: private:
wxString base_path = ""; wxString base_path = "";
wxImageList *img_list = nullptr; wxImageList *img_list = nullptr;
@ -23,9 +24,7 @@ private:
void OpenFolder(wxTreeListEvent& event); void OpenFolder(wxTreeListEvent& event);
void OpenFileInEditor(wxTreeListEvent& event); void OpenFileInEditor(wxTreeListEvent& event);
void InitImageList(); void InitImageList();
void CreateTree(const wxString &path, wxTreeListItem &root); void AddDirToTree(wxTreeListItem &root, const wxString &path, const wxString &parent);
void AddDirToTree(const wxString &path, wxTreeListItem &root, const wxString &parent); void AddDirFiles(wxTreeListItem &root, const wxString &path);
void AddDirFiles(const wxString &path, wxTreeListItem &root);
wxString BaseName(wxString path);
}; };

View File

@ -3,6 +3,8 @@
*/ */
#pragma once #pragma once
#include <libgen.h>
#include "common.h" #include "common.h"
// Disable annoying warning // Disable annoying warning
@ -50,5 +52,13 @@ wxSize static CalculateWindowSize()
return base; return base;
} }
wxString static BaseName(wxString path)
{
auto fullPath = path.char_str();
auto base = basename(fullPath);
return (wxString) base;
}
// Tyro-specific variables // Tyro-specific variables
#include "definitions.h" #include "definitions.h"