Treeview Docs

Added Treeview docs
Improved some stuff in the treeview object
This commit is contained in:
Robert Jelic
2023-05-01 14:56:16 +02:00
parent f2eacf4a82
commit 6bab574e8a
23 changed files with 786 additions and 1 deletions

View File

@@ -25,13 +25,20 @@ return function(name, basalt)
local node = {}
local onSelect
node = {
getChildren = function()
return children
end,
setParent = function(p)
if(parent~=nil)then
parent.removeChild(parent.findChildrenByText(node.getText()))
end
parent = p
base:updateDraw()
return node
end,
getParent = function()
@@ -51,15 +58,32 @@ return function(name, basalt)
expanded = exp
end
base:updateDraw()
return node
end,
isExpanded = function()
return expanded
end,
onSelect = function(...)
for _,v in pairs(table.pack(...))do
if(type(v)=="function")then
onSelect = v
end
end
return node
end,
callOnSelect = function()
if(onSelect~=nil)then
onSelect(node)
end
end,
setExpandable = function(expandable)
expandable = expandable
base:updateDraw()
return node
end,
isExpandable = function()
@@ -67,13 +91,23 @@ return function(name, basalt)
end,
removeChild = function(index)
if(type(index)=="table")then
for k,v in pairs(index)do
if(v==index)then
index = k
break
end
end
end
table.remove(children, index)
base:updateDraw()
return node
end,
findChildrenByText = function(searchText)
local foundNodes = {}
for _, child in ipairs(children) do
if child.getText() == searchText then
if string.find(child.getText(), searchText) then
table.insert(foundNodes, child)
end
end
@@ -86,6 +120,8 @@ return function(name, basalt)
setText = function(t)
text = t
base:updateDraw()
return node
end
}
@@ -150,6 +186,27 @@ return function(name, basalt)
return root
end,
setRoot = function(self, node)
root = node
node.setParent(nil)
return self
end,
onSelect = function(self, ...)
for _,v in pairs(table.pack(...))do
if(type(v)=="function")then
self:registerEvent("treeview_select", v)
end
end
return self
end,
selectionHandler = function(self, node)
node.callOnSelect(node)
self:sendEvent("treeview_select", node)
return self
end,
mouseHandler = function(self, button, x, y)
if base.mouseHandler(self, button, x, y) then
local currentLine = 1 - yOffset
@@ -159,6 +216,7 @@ return function(name, basalt)
if y == oby+currentLine-1 then
if x >= obx and x < obx + w then
node.setExpanded(not node.isExpanded())
self:selectionHandler(node)
self:setValue(node)
self:updateDraw()
return true

57
docs/objects/Treeview.md Normal file
View File

@@ -0,0 +1,57 @@
Treeview objects provide a hierarchical representation of data, allowing users to navigate and interact with items organized in a tree-like structure. They are commonly used in file explorers and other applications that require a hierarchical view of data.
Treeview objects inherit methods from VisualObject and Object. This means that Treeview objects can also use methods from VisualObject and Object.
| | |
|---|---|
|[setOffset](objects/Treeview/setOffset.md)|Sets the offset for the tree view
|[getOffset](objects/Treeview/getOffset.md)|Returns the current offset
|[setScrollable](objects/Treeview/setScrollable.md)|Sets whether the tree view is scrollable
|[setSelectionColor](objects/Treeview/setSelectionColor.md)|Sets the color of the selected item
|[getSelectionColor](objects/Treeview/getSelectionColor.md)|Returns the current selection color
|[isSelectionColorActive](objects/Treeview/isSelectionColorActive.md)|Checks if the selection color is active
|[getRoot](objects/Treeview/getRoot.md)|Returns the root node of the tree view
|[setRoot](objects/Treeview/setRoot.md)|Sets a new root node
## Events
This is a list of all available events for treeviews:
| | |
|---|---|
|[onSelect](objects/Treeview/onSelect.md)|Fires when an item is clicked
## Node Methods
| | |
|---|---|
|[getChildren](objects/Treeview/getChildren.md)|Returns a table of the node's children
|[getParent](objects/Treeview/getParent.md)|Returns the node's parent
|[addChild](objects/Treeview/addChild.md)|Adds a new child node to the current node
|[setExpanded](objects/Treeview/setExpanded.md)|Sets the expanded state of the node
|[isExpanded](objects/Treeview/isExpanded.md)|Returns whether the node is expanded
|[onSelect](objects/Treeview/onSelectNode.md)|Fires when a node is clicked
|[setExpandable](objects/Treeview/setExpandable.md)|Sets whether the node can be expanded or collapsed
|[isExpandable](objects/Treeview/isExpandable.md)|Returns whether the node is expandable
|[removeChild](objects/Treeview/removeChild.md)|Removes a child node from the current node
|[findChildrenByText](objects/Treeview/findChildrenByText.md)|Finds child nodes with the specified text
|[getText](objects/Treeview/getText.md)|Returns the node's text
|[setText](objects/Treeview/setText.md)|Sets the node's text
## Example
Here's an example of how to create a TreeView object and set its properties:
```lua
local main = basalt.createFrame()
local treeView = main:addTreeView()
local rootNode = treeView:getRoot()
local childNode = rootNode.addChild("Child Node")
childNode.onSelect(function(self)
basalt.debug("Node selected:", self.getText())
end)
```
This example creates a Treeview object within a main frame, sets its properties, and adds a child node to the root node. When the child node is selected, a debug message will be printed with the node's text.

View File

@@ -0,0 +1,32 @@
## addChild
### Description
The `addChild` method adds a new child node to a specified node within a Treeview object.
### Parameters
1. `string` The text of the new child node.
2. `boolean` Whetever the child node is expandable
### Returns
1. `node` The newly created child node
### Usage
* Creates a Treeview, adds a child node to the root node:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
local childNode = rootNode.addChild("Child Node")
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to access the root node, and a new child node is added to it using the `addChild` method.

View File

@@ -0,0 +1,36 @@
## findChildrenByText
### Description
The `findChildrenByText` method allows you to search for all child nodes within a Treeview object that have a specific text value. This can be useful when you need to find nodes based on their text content, such as for searching or filtering purposes.
### Parameters
1. `string` The text value to search for in the child nodes.
### Returns
1. `table` A table containing all child nodes that have the specified text value.
### Usage
* Creates a Treeview, adds multiple child nodes to the root node, and finds all child nodes with the specified text:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
local childNode1 = rootNode.addChild("Child Node 1")
local childNode2 = rootNode.addChild("Child Node 2")
local childNode3 = rootNode.addChild("Child Node 1")
-- Find all child nodes with the text "Child Node 1"
local foundNodes = rootNode.findChildrenByText("Child Node 1")
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to access the root node, and multiple child nodes are added to it using the `addChild` method. The `findChildrenByText` method is then called on the root node with the text value "Child Node 1" as a parameter, returning a table containing all child nodes with that text value.

View File

@@ -0,0 +1,32 @@
## getChildren
### Description
The `getChildren` method returns a table containing all the children nodes of a specified node within a Treeview object.
### Returns
1. `table` A table containing all the children nodes of the specified node
### Usage
* Creates a Treeview, adds a child node, and retrieves its children:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
local childNode1 = rootNode.addChild("Child Node 1")
local childNode2 = rootNode.addChild("Child Node 2")
local children = rootNode.getChildren()
basalt.debug("Children count:", #children)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to access the root node, and two new child nodes are added to it using the `addChild` method. The `getChildren` method is then used to retrieve a table containing all the children nodes of the root node, and the number of children is printed to the console using `basalt.debug`.

View File

@@ -0,0 +1,30 @@
## setOffset
### Description
Returns the current x and y offset values for the Treeview object. These values determine the indentation of the nodes within the TreeView object on both axes.
### Returns
1. `number` The current x offset value of the Treeview object.
2. `number` The current y offset value of the Treeview object.
### Usage
* Creates a Treeview, sets the x and y offsets, and retrieves the current offset values:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
:setOffset(2, 3)
local offsetX, offsetY = treeview:getOffset()
basalt.debug("Treeview x offset:", offsetX)
basalt.debug("Treeview y offset:", offsetY)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `setOffset` method is used to set the x and y indentation of the nodes within the TreeView object to 2 pixels and 3 pixels, respectively. The `getOffset` method is then called to retrieve the current x and y offset values, which are printed to the debug console.

View File

@@ -0,0 +1,31 @@
## getParent
### Description
The `getParent` method returns the parent node of a specified node within a Treeview object.
### Returns
1. `node` The parent node of the specified node, or nil if the node is a root node
### Usage
* Creates a Treeview, adds a child node, and retrieves its parent:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
local childNode = rootNode.addChild("Child Node")
local parentNode = childNode.getParent()
basalt.debug("Parent node:", parentNode)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to access the root node, and a new child node is added to it using the `addChild` method. The `getParent` method is then used to retrieve the parent node of the child node, which is the root node in this case.

View File

@@ -0,0 +1,28 @@
## getRoot
### Description
Retrieves the root node of the Treeview object.
### Returns
1. `node` The root node of the Treeview object
### Usage
* Creates a Treeview and retrieves its root node:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
basalt.debug("Root node:", rootNode)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to retrieve the root node of the Treeview object.

View File

@@ -0,0 +1,31 @@
## getSelectionColor
### Description
Retrieves the background and foreground colors for the selected node in the Treeview object.
### Returns
1. `color` The background color for the selected node.
2. `color` The foreground color for the selected node.
### Usage
* Creates a Treeview, sets the selection colors, and retrieves them:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
:setSelectionColor(colors.blue, colors.white)
local bgColor, fgColor, active = treeview:getSelectionColor()
basalt.debug("Background color:", bgColor)
basalt.debug("Foreground color:", fgColor)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `setSelectionColor` method is used to set the background color to blue and the foreground color to white. The `getSelectionColor` method is then used to retrieve the selection colors.

View File

@@ -0,0 +1,30 @@
## getText
### Description
The `getText` method allows you to retrieve the text value of a node within a Treeview object. This can be useful when you need to access the text content of a node for various purposes, such as displaying the node's text or for comparison with other nodes.
### Returns
1. `string` The text value of the node
### Usage
* Creates a Treeview, adds a child node to the root node, and retrieves the text value of the child node:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
local childNode = rootNode.addChild("Child Node 1")
-- Get the text value of the child node
local childNodeText = childNode.getText()
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to access the root node, and a child node is added to it using the `addChild` method. The `getText` method is then called on the child node to retrieve its text value.

View File

@@ -0,0 +1,33 @@
## isExpandable
### Description
The `isExpandable` method allows you to check if a node within a Treeview object is currently set as expandable or not. If a node is expandable, it means that it can be expanded or collapsed by the user when it has child nodes.
### Returns
1. `boolean` A boolean value indicating whether the node is set as expandable (true) or not (false).
### Usage
* Creates a Treeview, adds a child node to the root node, sets the child node to be expandable, and checks if the child node is expandable:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
local childNode = rootNode.addChild("Expandable Child Node")
childNode.setExpandable(true)
local isExpandable = childNode.isExpandable()
basalt.debug("Is the child node expandable?", isExpandable)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to access the root node, and a new child node is added to it using the `addChild` method. The `setExpandable` method is then called on the child node with a true parameter, making the node expandable. The `isExpandable` method is used to check if the child node is currently set as expandable.

View File

@@ -0,0 +1,35 @@
## isExpanded
### Description
The `isExpanded` method returns the current expanded state of a specified node within a Treeview object. If a node is expanded, its children nodes are displayed. If a node is collapsed, its children nodes are hidden.
### Returns
1. `boolean` The expanded state of the node (true for expanded, false for collapsed)
### Usage
* Creates a Treeview, adds a child node to the root node, and checks if the root node is expanded:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
local childNode = rootNode.addChild("Child Node")
rootNode.setExpanded(true)
if rootNode.isExpanded() then
basalt.debug("The root node is expanded.")
else
basalt.debug("The root node is not expanded.")
end
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to access the root node, and a new child node is added to it using the `addChild` method. The `setExpanded` method is then used to set the expanded state of the root node to true. The `isExpanded` method is used to check if the root node is expanded and display the result using `basalt.debug`.

View File

@@ -0,0 +1,29 @@
## isSelectionColorActive
### Description
Checks if the selection color is active for the Treeview object.
### Returns
1. `boolean` A boolean value indicating whether the selection color is active (`true`) or not (`false`).
### Usage
* Creates a Treeview, sets the selection colors, and checks if the selection color is active:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
:setSelectionColor(colors.blue, colors.white)
local active = treeview:isSelectionColorActive()
basalt.debug("Selection color active:", active)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `setSelectionColor` method is used to set the background color to blue and the foreground color to white. The `isSelectionColorActive` method is then used to check if the selection color is active.

View File

@@ -0,0 +1,31 @@
## onSelect
### Description
`onSelect(self, node)`
The `onSelect` event is triggered when a node in the Treeview object is clicked by the user. You can use this event to handle user interaction with the Treeview nodes.
### Returns
1. `object` The object in use
### Usage
* Creates a Treeview and handles the onSelect event:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
local childNode = rootNode:addChild("New Child")
treeview:onSelect(function(self, node)
basalt.debug("Selected node:", node:getText())
end)
basalt.autoUpdate()
```

View File

@@ -0,0 +1,36 @@
## onSelect
### Description
The `onSelect` event is triggered when a node within a Treeview object is clicked or selected by the user. This event allows you to define custom behavior or actions that should be executed when the node is selected.
### Parameters
1. `function` The callback function that will be executed when the node is selected. This function receives one argument:
* `node`: The node that was selected.
### Returns
1. `node` The node in use
### Usage
* Creates a Treeview, adds a child node to the root node, and checks if the root node is expanded:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
local childNode = rootNode.addChild("Child Node")
childNode.onSelect(function(node)
basalt.debug("Node selected:", node.getText())
end)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to access the root node, and a new child node is added to it using the `addChild` method. The `onSelect` event is then assigned to the child node with a callback function that will display the text of the selected node using `basalt.debug` when the node is clicked or selected.

View File

@@ -0,0 +1,34 @@
## removeChild
### Description
The `removeChild` method allows you to remove a child node from its parent node within a Treeview object. This can be useful for managing the hierarchy of nodes in your Treeview and keeping it up to date with the required information.
### Parameters
1. `node` The child node to be removed from its parent node.
### Returns
1. `node` The node in use
### Usage
* Creates a Treeview, adds a child node to the root node, and removes the child node from the root node:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
local childNode = rootNode.addChild("Child Node")
-- Remove the child node from the root node
rootNode.removeChild(childNode)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to access the root node, and a new child node is added to it using the `addChild` method. The `removeChild` method is then called on the root node with the child node as a parameter, effectively removing the child node from the Treeview.

View File

@@ -0,0 +1,33 @@
## setExpandable
### Description
The `setExpandable` method allows you to control whether a node within a Treeview object can be expanded or collapsed by the user. When a node is set to be expandable, it will show a visual indicator (such as a "+" symbol) when it has child nodes, allowing the user to expand or collapse the node by clicking on the indicator.
### Parameters
1. `boolean` A boolean value indicating whether the node should be expandable (true) or not (false).
### Returns
1. `node` The node in use
### Usage
* Creates a Treeview, adds a child node to the root node, and sets the child node to be expandable:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
local childNode = rootNode.addChild("Expandable Child Node")
childNode.setExpandable(true)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to access the root node, and a new child node is added to it using the `addChild` method. The `setExpandable` method is then called on the child node with a true parameter, making the node expandable by the user.

View File

@@ -0,0 +1,33 @@
## setExpanded
### Description
The `setExpanded` method sets the expanded state of a specified node within a Treeview object. When a node is expanded, its children nodes are displayed. If a node is collapsed, its children nodes are hidden.
### Parameters
1. `boolean` The expanded state to set for the node (true for expanded, false for collapsed)
### Returns
1. `object` The object in use
### Usage
* Creates a Treeview, adds a child node to the root node, and sets the expanded state to false:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
local childNode = rootNode.addChild("Child Node")
rootNode.setExpanded(false)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to access the root node, and a new child node is added to it using the `addChild` method. The `setExpanded` method is then used to set the expanded state of the root node to false, hiding its children nodes.

View File

@@ -0,0 +1,30 @@
## setOffset
### Description
Sets the x and y offset values for the Treeview object, determining the indentation of the nodes within the Treeview object on both axes.
### Parameters
1. `number` the x offset value to set for the Treeview object.
2. `number` the y offset value to set for the Treeview object.
### Returns
1. `object` The object in use
### Usage
* Creates a Treeview and sets the x and y offsets to 2 pixels and 3 pixels, respectively:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
:setOffset(2, 3)
basalt.autoUpdate()
```
In this example, the `setOffset` method is used to set the x and y indentation of the nodes within the TreeView object to 2 pixels and 3 pixels, respectively. This causes the nodes to be drawn 2 pixels from the left edge and 3 pixels from the top edge of the TreeView object.

View File

@@ -0,0 +1,31 @@
## setRoot
### Description
Sets a new root node for the Treeview object. This can be useful if you want to replace the current root node with a child node or an entirely new node.
### Parameters
1. `node` The new root node for the Treeview object. This should be a valid Treeview node object.
### Returns
1. `object` The object in use
### Usage
* Creates a Treeview, adds a child node, and then sets the child node as the new root node:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local childNode = treeview:getRoot().addChild("New Child")
treeview:setRoot(childNode)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to access the root node, and a new child node is added to it with the `addChild` method. Then, the `setRoot` method is used to set the new child node as the root node of the Treeview object.

View File

@@ -0,0 +1,30 @@
## setScrollable
### Description
Sets the scrollability of the Treeview object. If set to `true`, the Treeview will be scrollable; if set to `false`, it will not be scrollable.
Default: `true`
### Parameters
1. `boolean` A boolean value indicating whether the Treeview should be scrollable (true) or not (false).
### Returns
1. `object` The object in use
### Usage
* Creates a Treeview and sets it to be scrollable:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
:setScrollable(true)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `setScrollable` method is used to set the Treeview object as scrollable by passing the true boolean value.

View File

@@ -0,0 +1,31 @@
## setSelectionColor
### Description
Sets the background and foreground colors for the selected node in the Treeview object. Optionally, you can also specify if the selection color should be active or not.
### Parameters
1. `color` The background color for the selected node.
2. `color` The foreground color for the selected node.
3. `boolean` (optional) A boolean value indicating whether the selection color should be active (true) or not (false). Default value is true.
### Returns
1. `object` The object in use
### Usage
* Creates a Treeview and sets the selection colors:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
:setSelectionColor(colors.blue, colors.white)
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The setSelectionColor method is used to set the background color to blue, the foreground color to white, and the selection color to active.

View File

@@ -0,0 +1,34 @@
## setText
### Description
The `setText` method allows you to change the text value of a node within a Treeview object. This can be useful when you need to update the text content of a node due to changes in your application's data or based on user input.
### Parameters
1. `string` The new text value for the node.
### Returns
1. `node` The node in use
### Usage
* Creates a Treeview, adds a child node to the root node, and updates the text value of the child node:
```lua
local basalt = require("basalt")
local mainFrame = basalt.createFrame()
local treeview = mainFrame:addTreeview()
local rootNode = treeview:getRoot()
local childNode = rootNode.addChild("Child Node 1")
-- Update the text value of the child node
childNode.setText("Updated Child Node 1")
basalt.autoUpdate()
```
In this example, a Treeview object is created and added to the mainFrame. The `getRoot` method is used to access the root node, and a child node is added to it using the `addChild` method. The `setText` method is then called on the child node to update its text value.