Slow progress on File pane

This commit is contained in:
Timothy Warren 2019-05-23 16:22:49 -04:00
parent f2dc0a9eae
commit 9a2f12f06b
4 changed files with 122 additions and 83 deletions

View File

@ -1,3 +1,4 @@
#include <libgen.h>
#include <unordered_set> #include <unordered_set>
#include "src/widgets/FilePane.h" #include "src/widgets/FilePane.h"
@ -21,7 +22,7 @@ FilePane::FilePane(
const wxString &name const wxString &name
) : wxTreeListCtrl(parent, id, pos, size, style, name) ) : wxTreeListCtrl(parent, id, pos, size, style, name)
{ {
this->BindEvents(); this->BindEvents();
this->InitImageList(); this->InitImageList();
this->SetImageList(this->img_list); this->SetImageList(this->img_list);
@ -55,7 +56,7 @@ void FilePane::OpenFolder(wxTreeListEvent& event)
wxTreeListItem item = event.GetItem(); wxTreeListItem item = event.GetItem();
const wxString path = this->GetItemText(item, 0); const wxString path = this->GetItemText(item, 0);
wxLogDebug(path); wxLogDebug("Opening sidebar dir: %s", path);
} }
/** /**
@ -66,35 +67,37 @@ void FilePane::OpenFolder(wxTreeListEvent& event)
void FilePane::CreateTree(const wxString &path, wxTreeListItem &root) void FilePane::CreateTree(const wxString &path, wxTreeListItem &root)
{ {
auto *files = new wxArrayString(); auto *files = new wxArrayString();
wxDir::GetAllFiles(path, files); wxFileName rootPath(path);
rootPath.MakeAbsolute();
for (const wxString &file: *files) this->base_path = rootPath.GetPath();
{
wxFileName fileName(file);
if (fileName.DirExists(".")) wxLogDebug("Base Path for Sidebar: %s", this->base_path);
{
fileName.RemoveDir(0);
}
// If the file is at the root, add it to the tree wxDir::GetAllFiles(this->base_path, files);
if (fileName.GetDirCount() == 1)
{
this->AddDirFiles(path, root);
}
auto dir = std::string(fileName.GetPath()); for (const wxString &file: *files)
{
wxFileName fileName(file);
if (dir.empty()) // If the file is at the root, add it to the tree
{ if (fileName.GetDirCount() == rootPath.GetDirCount())
continue; {
} this->AddDirFiles(path, root);
}
// Append the folder to the tree auto dir = std::string(fileName.GetPath());
wxString wdir = wxString(dir);
wxLogDebug("Creating Dir Tree: %s", wdir); if (dir.empty())
this->DirToTree(wdir, root, wxString(".")); {
} continue;
}
// Append the folder to the tree
wxString wdir = wxString(dir);
// wxLogDebug("Creating Dir Tree: %s", wdir);
this->AddDirToTree(this->BaseName(wdir), root, wxString(""));
}
} }
/** /**
@ -102,79 +105,103 @@ void FilePane::CreateTree(const wxString &path, wxTreeListItem &root)
* *
* @access private * @access private
*/ */
void FilePane::DirToTree(const wxString &path, wxTreeListItem &root, const wxString &parent) void FilePane::AddDirToTree(const wxString &path, wxTreeListItem &root, const wxString &parent)
{ {
auto fullPath = parent.Clone(); auto pathBase = this->BaseName(path);
fullPath += "/"; auto fullPath = this->base_path;
fullPath += path;
this->dir_set.insert("."); if ( ! parent.empty())
{
fullPath += "/";
wxLogDebug("Rendering Dir Tree for %s", fullPath); wxString par = parent.Clone();
par.Replace((wxString)this->base_path, "");
auto *files = new wxArrayString(); fullPath += par.ToStdString();
wxDir::GetAllFiles(path, files); }
for (const wxString &item: *files) if ( ! fullPath.Contains(pathBase))
{ {
wxFileName fileName(item); fullPath += "/";
fullPath += pathBase;
}
// Remove the directory component closest to the root wxFileName parentDir(fullPath);
if (fileName.GetDirCount() > 1 || fileName.DirExists(".")) parentDir.MakeAbsolute();
{
fileName.RemoveDir(0);
}
const wxArrayString dirs = fileName.GetDirs(); wxLogDebug("Rendering Dir Tree for %s, full path: %s", path, fullPath);
// See if the path already exists on the tree auto *files = new wxArrayString();
for (const wxString &dir: dirs) wxDir::GetAllFiles(fullPath, files);
{
// Stop early if folder exists
auto it = this->dir_set.find(std::string(dir));
if (it != this->dir_set.end())
{
continue;
}
auto fileData = new wxStringClientData(); for (const wxString &item: *files)
fileData->SetData(dir); {
wxFileName fileName(item);
auto dir_node = this->AppendItem(root, dir, Icon_FolderClosed, Icon_FolderOpened, fileData); auto dir = std::string(fileName.GetPath());
wxString wdir(dir);
wxFileName dirName(dir);
this->dir_set.insert(std::string(dir)); if ( ! (wdir.Contains(pathBase) || dirName.GetDirCount() != (parentDir.GetDirCount() + 1)))
{
continue;
}
this->DirToTree(dir, dir_node, fullPath); // Stop early if folder exists
auto it = this->dir_set.find(std::string(dir));
if (it != this->dir_set.end())
{
continue;
}
// break; auto fileData = new wxStringClientData();
} fileData->SetData(dir);
}
auto dir_label = this->BaseName(dir);
auto dir_node = this->AppendItem(root, dir_label, Icon_FolderClosed, Icon_FolderOpened, fileData);
this->dir_set.insert(std::string(dir));
wxLogDebug("Recursing for dir %s, from parent %s", dir_label, fullPath);
this->AddDirFiles(fullPath, dir_node);
this->AddDirToTree(dir_label, dir_node, fullPath);
}
} }
void FilePane::AddDirFiles(const wxString &path, wxTreeListItem &root) void FilePane::AddDirFiles(const wxString &path, wxTreeListItem &root)
{ {
auto *files = new wxArrayString(); auto *files = new wxArrayString();
wxDir::GetAllFiles(path, files); wxDir::GetAllFiles(path, files);
wxLogDebug("Redering files in : %s", path); wxFileName rootPath(path);
rootPath.MakeAbsolute();
for (const wxString &item: *files) for (const wxString &item: *files)
{ {
wxFileName fileName(item); wxFileName fileName(item);
fileName.MakeAbsolute(); fileName.MakeAbsolute();
auto it = this->file_set.find(std::string(fileName.GetFullPath())); // If the file is in another folder, don't add it here!
if (it != this->file_set.end()) if (fileName.GetDirCount() != rootPath.GetDirCount())
{ {
continue; continue;
} }
auto fileData = new wxStringClientData(); auto it = this->file_set.find(std::string(fileName.GetFullPath()));
fileData->SetData(fileName.GetFullPath()); if (it != this->file_set.end())
{
continue;
}
this->AppendItem(root, fileName.GetFullName(), Icon_File, Icon_File, fileData); auto fileData = new wxStringClientData();
this->file_set.insert(std::string(fileName.GetFullPath())); fileData->SetData(fileName.GetFullPath());
}
auto fileLabel = this->BaseName(fileName.GetFullName());
this->AppendItem(root, fileLabel, Icon_File, Icon_File, fileData);
this->file_set.insert(std::string(fileName.GetFullPath()));
}
} }
/** /**
@ -220,4 +247,12 @@ void FilePane::InitImageList()
wxArtProvider::GetIcon(icon, wxART_LIST, iconSize) wxArtProvider::GetIcon(icon, wxART_LIST, iconSize)
); );
} }
} }
wxString FilePane::BaseName(wxString path)
{
auto fullPath = path.char_str();
auto base = basename(fullPath);
return (wxString) base;
}

View File

@ -15,7 +15,7 @@ public:
); );
~FilePane() override; ~FilePane() override;
private: private:
wxString curr_path = ""; wxString base_path = "";
wxImageList *img_list = nullptr; wxImageList *img_list = nullptr;
unordered_set<std::string> file_set; unordered_set<std::string> file_set;
unordered_set<std::string> dir_set; unordered_set<std::string> dir_set;
@ -24,7 +24,8 @@ private:
void OpenFileInEditor(wxTreeListEvent& event); void OpenFileInEditor(wxTreeListEvent& event);
void InitImageList(); void InitImageList();
void CreateTree(const wxString &path, wxTreeListItem &root); void CreateTree(const wxString &path, wxTreeListItem &root);
void DirToTree(const wxString &path, wxTreeListItem &root, const wxString &parent); void AddDirToTree(const wxString &path, wxTreeListItem &root, const wxString &parent);
void AddDirFiles(const wxString &path, wxTreeListItem &root); void AddDirFiles(const wxString &path, wxTreeListItem &root);
wxString BaseName(wxString path);
}; };

View File

@ -58,6 +58,7 @@ MainFrame::~MainFrame()
wxDELETE(this->findData); wxDELETE(this->findData);
wxDELETE(this->replaceDlg); wxDELETE(this->replaceDlg);
wxDELETE(this->findReplaceData); wxDELETE(this->findReplaceData);
wxDELETE(this->toolBar);
Glob_status_bar->Destroy(); Glob_status_bar->Destroy();
@ -72,7 +73,7 @@ MainFrame::~MainFrame()
void MainFrame::DoLayout() void MainFrame::DoLayout()
{ {
this->manager = new wxAuiManager(this); this->manager = new wxAuiManager(this);
this->SetupToolbar(); this->toolBar = this->SetupToolbar();
// Setup properties for each AUI pane // Setup properties for each AUI pane
wxAuiPaneInfo toolBarPaneInfo; wxAuiPaneInfo toolBarPaneInfo;
@ -123,7 +124,7 @@ void MainFrame::SetupStatusBar()
* *
* @return void * @return void
*/ */
void MainFrame::SetupToolbar() wxAuiToolBar* MainFrame::SetupToolbar()
{ {
// Icon files // Icon files
#ifndef __WXGTK__ #ifndef __WXGTK__
@ -163,6 +164,8 @@ void MainFrame::SetupToolbar()
toolBar->AddStretchSpacer(); toolBar->AddStretchSpacer();
toolBar->Realize(); toolBar->Realize();
return toolBar;
} }
/** /**

View File

@ -26,7 +26,7 @@ class MainFrame: public wxFrame
wxFindReplaceData *findData = nullptr; wxFindReplaceData *findData = nullptr;
wxFindReplaceDialog *findDlg = nullptr; wxFindReplaceDialog *findDlg = nullptr;
wxFindReplaceDialog *replaceDlg = nullptr; wxFindReplaceDialog *replaceDlg = nullptr;
void SetupToolbar(); wxAuiToolBar* SetupToolbar();
void SetupStatusBar(); void SetupStatusBar();
void BindEvents(); void BindEvents();
void DoLayout(); void DoLayout();