From 9a2f12f06b24f55142e52171b784dabad2e38ba5 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 23 May 2019 16:22:49 -0400 Subject: [PATCH] Slow progress on File pane --- src/widgets/FilePane.cpp | 189 ++++++++++++++++++++++---------------- src/widgets/FilePane.h | 7 +- src/widgets/MainFrame.cpp | 7 +- src/widgets/MainFrame.h | 2 +- 4 files changed, 122 insertions(+), 83 deletions(-) diff --git a/src/widgets/FilePane.cpp b/src/widgets/FilePane.cpp index 2c74758..df10dbe 100644 --- a/src/widgets/FilePane.cpp +++ b/src/widgets/FilePane.cpp @@ -1,3 +1,4 @@ +#include #include #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) ); } -} \ No newline at end of file +} + +wxString FilePane::BaseName(wxString path) +{ + auto fullPath = path.char_str(); + auto base = basename(fullPath); + + return (wxString) base; +} diff --git a/src/widgets/FilePane.h b/src/widgets/FilePane.h index 311e8fd..0ca785f 100644 --- a/src/widgets/FilePane.h +++ b/src/widgets/FilePane.h @@ -15,7 +15,7 @@ public: ); ~FilePane() override; private: - wxString curr_path = ""; + wxString base_path = ""; wxImageList *img_list = nullptr; unordered_set file_set; unordered_set 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); }; diff --git a/src/widgets/MainFrame.cpp b/src/widgets/MainFrame.cpp index aff29fb..85f3176 100644 --- a/src/widgets/MainFrame.cpp +++ b/src/widgets/MainFrame.cpp @@ -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; } /** diff --git a/src/widgets/MainFrame.h b/src/widgets/MainFrame.h index 960e334..8236ad1 100644 --- a/src/widgets/MainFrame.h +++ b/src/widgets/MainFrame.h @@ -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();