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 "src/widgets/FilePane.h"
@ -21,7 +22,7 @@ FilePane::FilePane(
const wxString &name
) : wxTreeListCtrl(parent, id, pos, size, style, name)
{
this->BindEvents();
this->BindEvents();
this->InitImageList();
this->SetImageList(this->img_list);
@ -55,7 +56,7 @@ void FilePane::OpenFolder(wxTreeListEvent& event)
wxTreeListItem item = event.GetItem();
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)
{
auto *files = new wxArrayString();
wxDir::GetAllFiles(path, files);
wxFileName rootPath(path);
rootPath.MakeAbsolute();
for (const wxString &file: *files)
{
wxFileName fileName(file);
this->base_path = rootPath.GetPath();
if (fileName.DirExists("."))
{
fileName.RemoveDir(0);
}
wxLogDebug("Base Path for Sidebar: %s", this->base_path);
// If the file is at the root, add it to the tree
if (fileName.GetDirCount() == 1)
{
this->AddDirFiles(path, root);
}
wxDir::GetAllFiles(this->base_path, files);
auto dir = std::string(fileName.GetPath());
for (const wxString &file: *files)
{
wxFileName fileName(file);
if (dir.empty())
{
continue;
}
// If the file is at the root, add it to the tree
if (fileName.GetDirCount() == rootPath.GetDirCount())
{
this->AddDirFiles(path, root);
}
// Append the folder to the tree
wxString wdir = wxString(dir);
wxLogDebug("Creating Dir Tree: %s", wdir);
this->DirToTree(wdir, root, wxString("."));
}
auto dir = std::string(fileName.GetPath());
if (dir.empty())
{
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
*/
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();
fullPath += "/";
fullPath += path;
auto pathBase = this->BaseName(path);
auto fullPath = this->base_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();
wxDir::GetAllFiles(path, files);
fullPath += par.ToStdString();
}
for (const wxString &item: *files)
{
wxFileName fileName(item);
if ( ! fullPath.Contains(pathBase))
{
fullPath += "/";
fullPath += pathBase;
}
// Remove the directory component closest to the root
if (fileName.GetDirCount() > 1 || fileName.DirExists("."))
{
fileName.RemoveDir(0);
}
wxFileName parentDir(fullPath);
parentDir.MakeAbsolute();
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
for (const wxString &dir: dirs)
{
// Stop early if folder exists
auto it = this->dir_set.find(std::string(dir));
if (it != this->dir_set.end())
{
continue;
}
auto *files = new wxArrayString();
wxDir::GetAllFiles(fullPath, files);
auto fileData = new wxStringClientData();
fileData->SetData(dir);
for (const wxString &item: *files)
{
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)
{
auto *files = new wxArrayString();
wxDir::GetAllFiles(path, files);
auto *files = new wxArrayString();
wxDir::GetAllFiles(path, files);
wxLogDebug("Redering files in : %s", path);
wxFileName rootPath(path);
rootPath.MakeAbsolute();
for (const wxString &item: *files)
{
wxFileName fileName(item);
fileName.MakeAbsolute();
for (const wxString &item: *files)
{
wxFileName fileName(item);
fileName.MakeAbsolute();
auto it = this->file_set.find(std::string(fileName.GetFullPath()));
if (it != this->file_set.end())
{
continue;
}
// If the file is in another folder, don't add it here!
if (fileName.GetDirCount() != rootPath.GetDirCount())
{
continue;
}
auto fileData = new wxStringClientData();
fileData->SetData(fileName.GetFullPath());
auto it = this->file_set.find(std::string(fileName.GetFullPath()));
if (it != this->file_set.end())
{
continue;
}
this->AppendItem(root, fileName.GetFullName(), Icon_File, Icon_File, fileData);
this->file_set.insert(std::string(fileName.GetFullPath()));
}
auto fileData = new wxStringClientData();
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)
);
}
}
}
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;
private:
wxString curr_path = "";
wxString base_path = "";
wxImageList *img_list = nullptr;
unordered_set<std::string> file_set;
unordered_set<std::string> dir_set;
@ -24,7 +24,8 @@ private:
void OpenFileInEditor(wxTreeListEvent& event);
void InitImageList();
void CreateTree(const wxString &path, wxTreeListItem &root);
void DirToTree(const wxString &path, wxTreeListItem &root, const wxString &parent);
void AddDirFiles(const wxString &path, wxTreeListItem &root);
void AddDirToTree(const wxString &path, wxTreeListItem &root, const wxString &parent);
void AddDirFiles(const wxString &path, wxTreeListItem &root);
wxString BaseName(wxString path);
};

View File

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

View File

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