Fixed parent management for folder navigation.
This commit is contained in:
parent
fe8afe3f87
commit
2370e70cfc
6 changed files with 37 additions and 56 deletions
|
|
@ -8,6 +8,7 @@
|
|||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <list>
|
||||
|
||||
#include <Display.hpp>
|
||||
#include <TreeNodeDiskUsage.hpp>
|
||||
|
|
@ -49,11 +50,11 @@ class EventManager
|
|||
Display & display;
|
||||
TreeNodeDiskUsage & rootNode;
|
||||
|
||||
int topLine; //<! Index of top displayed line (for scrolling)
|
||||
int currentLine; //<! Currently selected line
|
||||
TreeNodeDiskUsage *currentNode; //<! Pointer to the currently displayed node.
|
||||
int sortType; //<! Entry sort type : 0 -> size descending, 1 -> name ascending
|
||||
bool SI_units; //<! Indicates whether SI units should be used.
|
||||
int topLine; //<! Index of top displayed line (for scrolling)
|
||||
int currentLine; //<! Currently selected line
|
||||
std::list<TreeNodeDiskUsage*> displayedNodes; //<! List of pointers to the successively displayed nodes.
|
||||
int sortType; //<! Entry sort type : 0 -> size descending, 1 -> name ascending
|
||||
bool SI_units; //<! Indicates whether SI units should be used.
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -68,9 +68,6 @@ class TreeNodeDiskUsage
|
|||
/// 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);
|
||||
|
||||
|
|
@ -103,7 +100,6 @@ class TreeNodeDiskUsage
|
|||
|
||||
std::string path; //<! Path of the node
|
||||
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.
|
||||
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.
|
||||
|
|
|
|||
|
|
@ -14,10 +14,10 @@ EventManager::EventManager(Display & display_, TreeNodeDiskUsage & rootNode_)
|
|||
rootNode(rootNode_),
|
||||
topLine(0),
|
||||
currentLine(0),
|
||||
currentNode(&rootNode),
|
||||
sortType(0),
|
||||
SI_units(true)
|
||||
{
|
||||
displayedNodes.push_back(&rootNode);
|
||||
SortNodeElements();
|
||||
UpdateScreen();
|
||||
}
|
||||
|
|
@ -109,41 +109,41 @@ void EventManager::MainEventLoop()
|
|||
|
||||
void EventManager::CallbackArrowUp()
|
||||
{
|
||||
if(currentNode == NULL) return;
|
||||
if(currentNode->GetChildrenCount())
|
||||
currentLine = ((currentLine - 1) < 0) ? (currentNode->GetChildrenCount() - 1) : ((currentLine - 1) % currentNode->GetChildrenCount());
|
||||
if(displayedNodes.back() == NULL) return;
|
||||
if(displayedNodes.back()->GetChildrenCount())
|
||||
currentLine = ((currentLine - 1) < 0) ? (displayedNodes.back()->GetChildrenCount() - 1) : ((currentLine - 1) % displayedNodes.back()->GetChildrenCount());
|
||||
ScrollList();
|
||||
UpdateScreen();
|
||||
}
|
||||
|
||||
void EventManager::CallbackArrowDown()
|
||||
{
|
||||
if(currentNode == NULL) return;
|
||||
if(currentNode->GetChildrenCount())
|
||||
currentLine = (currentLine + 1) % currentNode->GetChildrenCount();
|
||||
if(displayedNodes.back() == NULL) return;
|
||||
if(displayedNodes.back()->GetChildrenCount())
|
||||
currentLine = (currentLine + 1) % displayedNodes.back()->GetChildrenCount();
|
||||
ScrollList();
|
||||
UpdateScreen();
|
||||
}
|
||||
|
||||
void EventManager::CallbackArrowLeft()
|
||||
{
|
||||
if(currentNode == NULL) return;
|
||||
if(displayedNodes.back() == NULL) return;
|
||||
CallbackBackspace();
|
||||
}
|
||||
|
||||
void EventManager::CallbackArrowRight()
|
||||
{
|
||||
if(currentNode == NULL) return;
|
||||
if(displayedNodes.back() == NULL) return;
|
||||
CallbackEnter();
|
||||
}
|
||||
|
||||
void EventManager::CallbackEnter()
|
||||
{
|
||||
if(currentNode == NULL) return;
|
||||
if((size_t)currentLine < currentNode->GetChildrenCount())
|
||||
if(currentNode->GetChild(currentLine).IsFolder())
|
||||
if(displayedNodes.back() == NULL) return;
|
||||
if((size_t)currentLine < displayedNodes.back()->GetChildrenCount())
|
||||
if(displayedNodes.back()->GetChild(currentLine).IsFolder())
|
||||
{
|
||||
currentNode = ¤tNode->GetChild(currentLine);
|
||||
displayedNodes.push_back(&displayedNodes.back()->GetChild(currentLine));
|
||||
SortNodeElements();
|
||||
topLine = 0;
|
||||
currentLine = 0;
|
||||
|
|
@ -153,11 +153,11 @@ void EventManager::CallbackEnter()
|
|||
|
||||
void EventManager::CallbackBackspace()
|
||||
{
|
||||
if(currentNode == NULL) return;
|
||||
if(displayedNodes.back() == NULL) return;
|
||||
// Go up one level
|
||||
if(currentNode->GetParent() != NULL)
|
||||
if(displayedNodes.size() > 1)
|
||||
{
|
||||
currentNode = currentNode->GetParent();
|
||||
displayedNodes.pop_back();
|
||||
topLine = 0;
|
||||
currentLine = 0;
|
||||
}
|
||||
|
|
@ -166,9 +166,9 @@ void EventManager::CallbackBackspace()
|
|||
|
||||
void EventManager::CallbackHome()
|
||||
{
|
||||
if(currentNode == NULL) return;
|
||||
// Set the current node to be the root node (go back "home").
|
||||
currentNode = &rootNode;
|
||||
displayedNodes.clear();
|
||||
displayedNodes.push_back(&rootNode);
|
||||
topLine = 0;
|
||||
currentLine = 0;
|
||||
UpdateScreen();
|
||||
|
|
@ -176,14 +176,14 @@ void EventManager::CallbackHome()
|
|||
|
||||
void EventManager::CallbackHelp()
|
||||
{
|
||||
if(currentNode == NULL) return;
|
||||
if(displayedNodes.back() == NULL) return;
|
||||
|
||||
UpdateScreen();
|
||||
}
|
||||
|
||||
void EventManager::CallbackSort()
|
||||
{
|
||||
if(currentNode == NULL) return;
|
||||
if(displayedNodes.back() == NULL) return;
|
||||
sortType = (sortType + 1) % 2;// Cycle through all the sort types.
|
||||
SortNodeElements();
|
||||
UpdateScreen();
|
||||
|
|
@ -191,15 +191,15 @@ void EventManager::CallbackSort()
|
|||
|
||||
void EventManager::CallbackUnits()
|
||||
{
|
||||
if(currentNode == NULL) return;
|
||||
if(displayedNodes.back() == NULL) return;
|
||||
SI_units ^= true;// Invert the SI_units flag.
|
||||
UpdateScreen();
|
||||
}
|
||||
|
||||
void EventManager::ScrollList()
|
||||
{
|
||||
/*if(currentNode == NULL) return;
|
||||
if(currentNode->GetChildrenCount())
|
||||
/*if(displayedNodes.back() == NULL) return;
|
||||
if(displayedNodes.back()->GetChildrenCount())
|
||||
{
|
||||
// compute visibility of selected line and scroll if needed
|
||||
PRINT_VAR(2 + currentLine - topLine);
|
||||
|
|
@ -211,18 +211,18 @@ void EventManager::ScrollList()
|
|||
|
||||
void EventManager::SortNodeElements()
|
||||
{
|
||||
if(currentNode == NULL) return;
|
||||
if(displayedNodes.back() == NULL) return;
|
||||
if(sortType == 0)
|
||||
currentNode->SortBySizeDesc();
|
||||
displayedNodes.back()->SortBySizeDesc();
|
||||
else if(sortType == 1)
|
||||
currentNode->SortByNameAsc();
|
||||
displayedNodes.back()->SortByNameAsc();
|
||||
}
|
||||
|
||||
void EventManager::UpdateScreen()
|
||||
{
|
||||
//DebugPrintState(); return;// DEBUG
|
||||
Display::ClearScreen();
|
||||
display.DisplayTreeNode(*currentNode, topLine, SI_units);
|
||||
display.DisplayTreeNode(*displayedNodes.back(), topLine, SI_units);
|
||||
display.HighlightLine(2 + topLine + currentLine, true);
|
||||
display.DrawScreenLines();
|
||||
}
|
||||
|
|
@ -232,14 +232,13 @@ void EventManager::DebugPrintState()
|
|||
PRINT_VAR(&rootNode);
|
||||
PRINT_VAR(topLine);
|
||||
PRINT_VAR(currentLine);
|
||||
PRINT_VAR(currentNode);
|
||||
PRINT_VAR(currentNode->GetParent());
|
||||
PRINT_VAR(displayedNodes.back());
|
||||
PRINT_VAR(sortType);
|
||||
|
||||
if((size_t)currentLine < currentNode->GetChildrenCount())
|
||||
if((size_t)currentLine < displayedNodes.back()->GetChildrenCount())
|
||||
{
|
||||
PRINT_VAR(currentNode->GetChild(currentLine).IsFolder());
|
||||
PRINT_VAR(currentNode->GetChild(currentLine).GetNodePath());
|
||||
PRINT_VAR(displayedNodes.back()->GetChild(currentLine).IsFolder());
|
||||
PRINT_VAR(displayedNodes.back()->GetChild(currentLine).GetNodePath());
|
||||
}
|
||||
std::cout << "-----------------------------------------------\n";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
TreeNodeDiskUsage::TreeNodeDiskUsage(std::string const& path_, PathFilters const* pathFilters_)
|
||||
: pathFilters(pathFilters_),
|
||||
path(path_),
|
||||
parentNode(NULL),
|
||||
isFolder(false),
|
||||
totalSize(0),
|
||||
totalSizeOnDisk(0),
|
||||
|
|
@ -167,8 +166,6 @@ 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<TreeNodeDiskUsage> const& TreeNodeDiskUsage::GetChildren() const { return children; }
|
||||
|
|
@ -187,16 +184,6 @@ 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);
|
||||
|
|
|
|||
|
|
@ -84,7 +84,6 @@ int main(int argc, char *argv[])
|
|||
// Interactive mode : the user can navigate through the tree structure.
|
||||
else
|
||||
{
|
||||
tree.BuildParentLinks();
|
||||
EventManager eventManager(display, tree);
|
||||
eventManager.MainEventLoop();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -194,7 +194,6 @@ void runTests(int argc, char *argv[])
|
|||
Display display;
|
||||
TreeNodeDiskUsage tree(rootpath);
|
||||
tree.BuildTree(false);
|
||||
tree.BuildParentLinks();
|
||||
EventManager eventManager(display, tree);
|
||||
eventManager.MainEventLoop();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue