From 9e6579c57b9fc4a5c2e0aa563030ea7e3c702d5f Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 22 Jan 2022 22:27:41 +0100 Subject: [PATCH] Working event system on tee structure, with debug prints. --- include/EventManager.hpp | 10 ++-- include/TreeNodeDiskUsage.hpp | 27 +++++++++-- src/Display.cpp | 18 ++++---- src/EventManager.cpp | 87 +++++++++++++++++++++++++++++------ src/TreeNodeDiskUsage.cpp | 30 +++++++++--- src/test.cpp | 5 +- 6 files changed, 138 insertions(+), 39 deletions(-) diff --git a/include/EventManager.hpp b/include/EventManager.hpp index 2d8324f..869282e 100644 --- a/include/EventManager.hpp +++ b/include/EventManager.hpp @@ -32,15 +32,17 @@ class EventManager void CallbackHome(); void CallbackHelp(); void CallbackSort(); + + void DebugPrintState(); protected: Display & display; TreeNodeDiskUsage & rootNode; - int topLine; // size descending, 1 -> name ascending }; #endif diff --git a/include/TreeNodeDiskUsage.hpp b/include/TreeNodeDiskUsage.hpp index 88b3710..ccc86f5 100644 --- a/include/TreeNodeDiskUsage.hpp +++ b/include/TreeNodeDiskUsage.hpp @@ -22,8 +22,6 @@ struct PathFilters /// \class TreeNodeDiskUsage Implements a node of a tree of the files and folders on a system. class TreeNodeDiskUsage { - friend class Display; - public: TreeNodeDiskUsage(std::string const& path_, PathFilters const* pathFilters_ = NULL); ~TreeNodeDiskUsage(); @@ -45,6 +43,9 @@ class TreeNodeDiskUsage /// Returns the total size of the node on disk, including all its children size_t GetTotalSizeOnDisk() const; + + /// Returns the total number of elements contained within the node, including all its children + size_t GetTotalElements() const; /// Returns the name of the node : /path/to/node.bla -> node.bla std::string GetNodeName() const; @@ -52,6 +53,24 @@ class TreeNodeDiskUsage /// Returns the path of the node : /path/to/node.bla std::string GetNodePath() const; + /// Returns a pointer to the node's parent. Warning : this pointer can be NULL ! + TreeNodeDiskUsage * GetParent() const; + + /// Returns true if the node corresponds to a folder. + bool IsFolder() const; + + /// Returns a constant reference to the list of children of the node. + std::vector const& GetChildren() const; + + /// Returns a constant reference to the the child i. + TreeNodeDiskUsage const& GetChild(unsigned int i) const; + + /// Returns a reference to the the child i. + TreeNodeDiskUsage & GetChild(unsigned int i); + + /// Builds the parent links. Must be called after the tree has been built. + void BuildParentLinks(); + /// Sorts the children of the node by size in descending order. void SortBySizeDesc(bool recursive = false); @@ -66,9 +85,6 @@ class TreeNodeDiskUsage protected: /// Cleans the path to remove double slashes, trailing slashes and leading and trailing whitespaces. void SanitizePath(); - - /// Explores the whole tree to compute every node's size, including all their children. - void ComputeTotalSize(); /// Removes the leading character 'c'. static std::string RemoveLeadingCharacter(std::string const& str, char c); @@ -87,6 +103,7 @@ class TreeNodeDiskUsage std::string path; // children; //GetChildrenCount()) + currentLine = (currentLine - 1) % currentNode->GetChildrenCount(); + DebugPrintState(); } void EventManager::CallbackArrowDown() { - std::cout << "CallbackArrowDown\n"; + std::cout << "CallbackArrowDown\n";// DEBUG + if(currentNode == NULL) return; + if(currentNode->GetChildrenCount()) + currentLine = (currentLine + 1) % currentNode->GetChildrenCount(); + DebugPrintState(); } void EventManager::CallbackArrowLeft() { - std::cout << "CallbackArrowLeft\n"; + std::cout << "CallbackArrowLeft\n";// DEBUG + if(currentNode == NULL) return; + CallbackBackspace(); } void EventManager::CallbackArrowRight() { - std::cout << "CallbackArrowRight\n"; + std::cout << "CallbackArrowRight\n";// DEBUG + if(currentNode == NULL) return; + CallbackEnter(); } void EventManager::CallbackEnter() { - std::cout << "CallbackEnter\n"; + std::cout << "CallbackEnter\n";// DEBUG + if(currentNode == NULL) return; + if((size_t)currentLine < currentNode->GetChildrenCount()) + if(currentNode->GetChild(currentLine).IsFolder()) + { + currentNode = ¤tNode->GetChild(currentLine); + topLine = 0; + currentLine = 0; + } + DebugPrintState(); } void EventManager::CallbackBackspace() { - std::cout << "CallbackBackspace\n"; + std::cout << "CallbackBackspace\n";// DEBUG + if(currentNode == NULL) return; + // Go up one level + if(currentNode->GetParent() != NULL) + { + currentNode = currentNode->GetParent(); + topLine = 0; + currentLine = 0; + } + DebugPrintState(); } void EventManager::CallbackHome() { - std::cout << "CallbackHome\n"; + std::cout << "CallbackHome\n";// DEBUG + if(currentNode == NULL) return; + // Set the current node to be the root node (go back "home"). + currentNode = &rootNode; + topLine = 0; + currentLine = 0; + DebugPrintState(); } void EventManager::CallbackHelp() { - std::cout << "CallbackHelp\n"; + std::cout << "CallbackHelp\n";// DEBUG + if(currentNode == NULL) return; + + DebugPrintState(); } void EventManager::CallbackSort() { - std::cout << "CallbackSort\n"; + std::cout << "CallbackSort\n";// DEBUG + if(currentNode == NULL) return; + sortType = (sortType + 1) % 2;// Cycle through all the sort types. + if(sortType == 0) + currentNode->SortBySizeDesc(); + else if(sortType == 1) + currentNode->SortByNameAsc(); + DebugPrintState(); +} + +void EventManager::DebugPrintState() +{ + PRINT_VAR(&rootNode); + PRINT_VAR(topLine); + PRINT_VAR(currentLine); + PRINT_VAR(currentNode); + PRINT_VAR(currentNode->GetParent()); + PRINT_VAR(sortType); + + if((size_t)currentLine < currentNode->GetChildrenCount()) + { + PRINT_VAR(currentNode->GetChild(currentLine).IsFolder()); + PRINT_VAR(currentNode->GetChild(currentLine).GetNodePath()); + } + std::cout << "-----------------------------------------------\n"; } diff --git a/src/TreeNodeDiskUsage.cpp b/src/TreeNodeDiskUsage.cpp index eabdbfb..8e65743 100644 --- a/src/TreeNodeDiskUsage.cpp +++ b/src/TreeNodeDiskUsage.cpp @@ -3,6 +3,7 @@ TreeNodeDiskUsage::TreeNodeDiskUsage(std::string const& path_, PathFilters const* pathFilters_) : pathFilters(pathFilters_), path(path_), + parentNode(NULL), isFolder(false), totalSize(0), totalSizeOnDisk(0), @@ -143,6 +144,7 @@ void TreeNodeDiskUsage::PrintTree(unsigned int maxDepth, bool humanReadableSizes size_t TreeNodeDiskUsage::GetChildrenCount() const { return children.size(); } size_t TreeNodeDiskUsage::GetTotalSize() const { return totalSize; } size_t TreeNodeDiskUsage::GetTotalSizeOnDisk() const { return totalSizeOnDisk; } +size_t TreeNodeDiskUsage::GetTotalElements() const { return totalElements; } std::string TreeNodeDiskUsage::GetNodePath() const{ return path; } std::string TreeNodeDiskUsage::GetNodeName() const @@ -165,6 +167,18 @@ std::string TreeNodeDiskUsage::GetNodeName() const return path.substr(lastSepPos+1, path.size()-lastSepPos-1); } +TreeNodeDiskUsage * TreeNodeDiskUsage::GetParent() const { return parentNode; } + +bool TreeNodeDiskUsage::IsFolder() const { return isFolder; } + +std::vector const& TreeNodeDiskUsage::GetChildren() const { return children; } + +/// Returns a constant reference to the the child i. +TreeNodeDiskUsage const& TreeNodeDiskUsage::GetChild(unsigned int i) const { return children[i]; } + +/// Returns a reference to the the child i. +TreeNodeDiskUsage & TreeNodeDiskUsage::GetChild(unsigned int i) { return children[i]; } + void TreeNodeDiskUsage::SortBySizeDesc(bool recursive) { std::sort(children.begin(), children.end(), TreeNodeDiskUsage::SortOperatorSizeDesc); @@ -173,6 +187,16 @@ void TreeNodeDiskUsage::SortBySizeDesc(bool recursive) children[i].SortBySizeDesc(recursive); } +void TreeNodeDiskUsage::BuildParentLinks() +{ + for(unsigned int i = 0 ; i < children.size() ; i++) + { + children[i].parentNode = this; + if(children[i].isFolder) + children[i].BuildParentLinks(); + } +} + void TreeNodeDiskUsage::SortByNameAsc(bool recursive) { std::sort(children.begin(), children.end(), TreeNodeDiskUsage::SortOperatorNameAsc); @@ -256,9 +280,3 @@ std::string TreeNodeDiskUsage::ReplaceCharacterInStr(std::string str, char c1, c str[i] = c2; return str; } - -void TreeNodeDiskUsage::ComputeTotalSize() -{ - // Recursively add all sizes to the current node's size - //if() -} diff --git a/src/test.cpp b/src/test.cpp index 5872048..5feef46 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -189,15 +189,16 @@ void runTests(int argc, char *argv[]) } } } - if(0) + //if(0) {// test EventManager Display display; TreeNodeDiskUsage tree(rootpath); tree.BuildTree(false); + tree.BuildParentLinks(); EventManager eventManager(display, tree); eventManager.MainEventLoop(); } - //if(0) + if(0) {// test interactive display of tree node (without interactivity) Display display; TreeNodeDiskUsage tree(rootpath);