Fixed parent management for folder navigation.

This commit is contained in:
Your Name 2022-01-27 22:26:44 +01:00
parent fe8afe3f87
commit 2370e70cfc
6 changed files with 37 additions and 56 deletions

View file

@ -8,6 +8,7 @@
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <termios.h> #include <termios.h>
#include <list>
#include <Display.hpp> #include <Display.hpp>
#include <TreeNodeDiskUsage.hpp> #include <TreeNodeDiskUsage.hpp>
@ -49,11 +50,11 @@ class EventManager
Display & display; Display & display;
TreeNodeDiskUsage & rootNode; TreeNodeDiskUsage & rootNode;
int topLine; //<! Index of top displayed line (for scrolling) int topLine; //<! Index of top displayed line (for scrolling)
int currentLine; //<! Currently selected line int currentLine; //<! Currently selected line
TreeNodeDiskUsage *currentNode; //<! Pointer to the currently displayed node. std::list<TreeNodeDiskUsage*> displayedNodes; //<! List of pointers to the successively displayed nodes.
int sortType; //<! Entry sort type : 0 -> size descending, 1 -> name ascending int sortType; //<! Entry sort type : 0 -> size descending, 1 -> name ascending
bool SI_units; //<! Indicates whether SI units should be used. bool SI_units; //<! Indicates whether SI units should be used.
}; };
#endif #endif

View file

@ -68,9 +68,6 @@ class TreeNodeDiskUsage
/// Returns a reference to the the child i. /// Returns a reference to the the child i.
TreeNodeDiskUsage & GetChild(unsigned int 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. /// Sorts the children of the node by size in descending order.
void SortBySizeDesc(bool recursive = false); void SortBySizeDesc(bool recursive = false);
@ -103,7 +100,6 @@ class TreeNodeDiskUsage
std::string path; //<! Path of the node std::string path; //<! Path of the node
std::vector<TreeNodeDiskUsage> children; //<! Stores all the children contained within the folder. std::vector<TreeNodeDiskUsage> children; //<! Stores all the children contained within the folder.
TreeNodeDiskUsage * parentNode; //<! Pointer to the parent of the node.
bool isFolder; //<! Indicates whether the node is a folder or a file. bool isFolder; //<! Indicates whether the node is a folder or a file.
size_t totalSize; //<! Total size of the node, taking all children into account. size_t totalSize; //<! Total size of the node, taking all children into account.
size_t totalSizeOnDisk; //<! Total size of the node on the disk, taking all children into account. size_t totalSizeOnDisk; //<! Total size of the node on the disk, taking all children into account.

View file

@ -14,10 +14,10 @@ EventManager::EventManager(Display & display_, TreeNodeDiskUsage & rootNode_)
rootNode(rootNode_), rootNode(rootNode_),
topLine(0), topLine(0),
currentLine(0), currentLine(0),
currentNode(&rootNode),
sortType(0), sortType(0),
SI_units(true) SI_units(true)
{ {
displayedNodes.push_back(&rootNode);
SortNodeElements(); SortNodeElements();
UpdateScreen(); UpdateScreen();
} }
@ -109,41 +109,41 @@ void EventManager::MainEventLoop()
void EventManager::CallbackArrowUp() void EventManager::CallbackArrowUp()
{ {
if(currentNode == NULL) return; if(displayedNodes.back() == NULL) return;
if(currentNode->GetChildrenCount()) if(displayedNodes.back()->GetChildrenCount())
currentLine = ((currentLine - 1) < 0) ? (currentNode->GetChildrenCount() - 1) : ((currentLine - 1) % currentNode->GetChildrenCount()); currentLine = ((currentLine - 1) < 0) ? (displayedNodes.back()->GetChildrenCount() - 1) : ((currentLine - 1) % displayedNodes.back()->GetChildrenCount());
ScrollList(); ScrollList();
UpdateScreen(); UpdateScreen();
} }
void EventManager::CallbackArrowDown() void EventManager::CallbackArrowDown()
{ {
if(currentNode == NULL) return; if(displayedNodes.back() == NULL) return;
if(currentNode->GetChildrenCount()) if(displayedNodes.back()->GetChildrenCount())
currentLine = (currentLine + 1) % currentNode->GetChildrenCount(); currentLine = (currentLine + 1) % displayedNodes.back()->GetChildrenCount();
ScrollList(); ScrollList();
UpdateScreen(); UpdateScreen();
} }
void EventManager::CallbackArrowLeft() void EventManager::CallbackArrowLeft()
{ {
if(currentNode == NULL) return; if(displayedNodes.back() == NULL) return;
CallbackBackspace(); CallbackBackspace();
} }
void EventManager::CallbackArrowRight() void EventManager::CallbackArrowRight()
{ {
if(currentNode == NULL) return; if(displayedNodes.back() == NULL) return;
CallbackEnter(); CallbackEnter();
} }
void EventManager::CallbackEnter() void EventManager::CallbackEnter()
{ {
if(currentNode == NULL) return; if(displayedNodes.back() == NULL) return;
if((size_t)currentLine < currentNode->GetChildrenCount()) if((size_t)currentLine < displayedNodes.back()->GetChildrenCount())
if(currentNode->GetChild(currentLine).IsFolder()) if(displayedNodes.back()->GetChild(currentLine).IsFolder())
{ {
currentNode = &currentNode->GetChild(currentLine); displayedNodes.push_back(&displayedNodes.back()->GetChild(currentLine));
SortNodeElements(); SortNodeElements();
topLine = 0; topLine = 0;
currentLine = 0; currentLine = 0;
@ -153,11 +153,11 @@ void EventManager::CallbackEnter()
void EventManager::CallbackBackspace() void EventManager::CallbackBackspace()
{ {
if(currentNode == NULL) return; if(displayedNodes.back() == NULL) return;
// Go up one level // Go up one level
if(currentNode->GetParent() != NULL) if(displayedNodes.size() > 1)
{ {
currentNode = currentNode->GetParent(); displayedNodes.pop_back();
topLine = 0; topLine = 0;
currentLine = 0; currentLine = 0;
} }
@ -166,9 +166,9 @@ void EventManager::CallbackBackspace()
void EventManager::CallbackHome() void EventManager::CallbackHome()
{ {
if(currentNode == NULL) return;
// Set the current node to be the root node (go back "home"). // Set the current node to be the root node (go back "home").
currentNode = &rootNode; displayedNodes.clear();
displayedNodes.push_back(&rootNode);
topLine = 0; topLine = 0;
currentLine = 0; currentLine = 0;
UpdateScreen(); UpdateScreen();
@ -176,14 +176,14 @@ void EventManager::CallbackHome()
void EventManager::CallbackHelp() void EventManager::CallbackHelp()
{ {
if(currentNode == NULL) return; if(displayedNodes.back() == NULL) return;
UpdateScreen(); UpdateScreen();
} }
void EventManager::CallbackSort() void EventManager::CallbackSort()
{ {
if(currentNode == NULL) return; if(displayedNodes.back() == NULL) return;
sortType = (sortType + 1) % 2;// Cycle through all the sort types. sortType = (sortType + 1) % 2;// Cycle through all the sort types.
SortNodeElements(); SortNodeElements();
UpdateScreen(); UpdateScreen();
@ -191,15 +191,15 @@ void EventManager::CallbackSort()
void EventManager::CallbackUnits() void EventManager::CallbackUnits()
{ {
if(currentNode == NULL) return; if(displayedNodes.back() == NULL) return;
SI_units ^= true;// Invert the SI_units flag. SI_units ^= true;// Invert the SI_units flag.
UpdateScreen(); UpdateScreen();
} }
void EventManager::ScrollList() void EventManager::ScrollList()
{ {
/*if(currentNode == NULL) return; /*if(displayedNodes.back() == NULL) return;
if(currentNode->GetChildrenCount()) if(displayedNodes.back()->GetChildrenCount())
{ {
// compute visibility of selected line and scroll if needed // compute visibility of selected line and scroll if needed
PRINT_VAR(2 + currentLine - topLine); PRINT_VAR(2 + currentLine - topLine);
@ -211,18 +211,18 @@ void EventManager::ScrollList()
void EventManager::SortNodeElements() void EventManager::SortNodeElements()
{ {
if(currentNode == NULL) return; if(displayedNodes.back() == NULL) return;
if(sortType == 0) if(sortType == 0)
currentNode->SortBySizeDesc(); displayedNodes.back()->SortBySizeDesc();
else if(sortType == 1) else if(sortType == 1)
currentNode->SortByNameAsc(); displayedNodes.back()->SortByNameAsc();
} }
void EventManager::UpdateScreen() void EventManager::UpdateScreen()
{ {
//DebugPrintState(); return;// DEBUG //DebugPrintState(); return;// DEBUG
Display::ClearScreen(); Display::ClearScreen();
display.DisplayTreeNode(*currentNode, topLine, SI_units); display.DisplayTreeNode(*displayedNodes.back(), topLine, SI_units);
display.HighlightLine(2 + topLine + currentLine, true); display.HighlightLine(2 + topLine + currentLine, true);
display.DrawScreenLines(); display.DrawScreenLines();
} }
@ -232,14 +232,13 @@ void EventManager::DebugPrintState()
PRINT_VAR(&rootNode); PRINT_VAR(&rootNode);
PRINT_VAR(topLine); PRINT_VAR(topLine);
PRINT_VAR(currentLine); PRINT_VAR(currentLine);
PRINT_VAR(currentNode); PRINT_VAR(displayedNodes.back());
PRINT_VAR(currentNode->GetParent());
PRINT_VAR(sortType); PRINT_VAR(sortType);
if((size_t)currentLine < currentNode->GetChildrenCount()) if((size_t)currentLine < displayedNodes.back()->GetChildrenCount())
{ {
PRINT_VAR(currentNode->GetChild(currentLine).IsFolder()); PRINT_VAR(displayedNodes.back()->GetChild(currentLine).IsFolder());
PRINT_VAR(currentNode->GetChild(currentLine).GetNodePath()); PRINT_VAR(displayedNodes.back()->GetChild(currentLine).GetNodePath());
} }
std::cout << "-----------------------------------------------\n"; std::cout << "-----------------------------------------------\n";
} }

View file

@ -3,7 +3,6 @@
TreeNodeDiskUsage::TreeNodeDiskUsage(std::string const& path_, PathFilters const* pathFilters_) TreeNodeDiskUsage::TreeNodeDiskUsage(std::string const& path_, PathFilters const* pathFilters_)
: pathFilters(pathFilters_), : pathFilters(pathFilters_),
path(path_), path(path_),
parentNode(NULL),
isFolder(false), isFolder(false),
totalSize(0), totalSize(0),
totalSizeOnDisk(0), totalSizeOnDisk(0),
@ -167,8 +166,6 @@ std::string TreeNodeDiskUsage::GetNodeName() const
return path.substr(lastSepPos+1, path.size()-lastSepPos-1); return path.substr(lastSepPos+1, path.size()-lastSepPos-1);
} }
TreeNodeDiskUsage * TreeNodeDiskUsage::GetParent() const { return parentNode; }
bool TreeNodeDiskUsage::IsFolder() const { return isFolder; } bool TreeNodeDiskUsage::IsFolder() const { return isFolder; }
std::vector<TreeNodeDiskUsage> const& TreeNodeDiskUsage::GetChildren() const { return children; } std::vector<TreeNodeDiskUsage> const& TreeNodeDiskUsage::GetChildren() const { return children; }
@ -187,16 +184,6 @@ void TreeNodeDiskUsage::SortBySizeDesc(bool recursive)
children[i].SortBySizeDesc(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) void TreeNodeDiskUsage::SortByNameAsc(bool recursive)
{ {
std::sort(children.begin(), children.end(), TreeNodeDiskUsage::SortOperatorNameAsc); std::sort(children.begin(), children.end(), TreeNodeDiskUsage::SortOperatorNameAsc);

View file

@ -84,7 +84,6 @@ int main(int argc, char *argv[])
// Interactive mode : the user can navigate through the tree structure. // Interactive mode : the user can navigate through the tree structure.
else else
{ {
tree.BuildParentLinks();
EventManager eventManager(display, tree); EventManager eventManager(display, tree);
eventManager.MainEventLoop(); eventManager.MainEventLoop();
} }

View file

@ -194,7 +194,6 @@ void runTests(int argc, char *argv[])
Display display; Display display;
TreeNodeDiskUsage tree(rootpath); TreeNodeDiskUsage tree(rootpath);
tree.BuildTree(false); tree.BuildTree(false);
tree.BuildParentLinks();
EventManager eventManager(display, tree); EventManager eventManager(display, tree);
eventManager.MainEventLoop(); eventManager.MainEventLoop();
} }