| 1 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | # FILE : bftrview.tcl |
|---|
| 3 | # PURPOSE : This file contains some utility functions for treeviews |
|---|
| 4 | # manipulation or generation. |
|---|
| 5 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 9 | namespace eval ::BayesTreeview { |
|---|
| 10 | # |
|---|
| 11 | # PURPOSE: The purpose of this namespace is organize code and functions and |
|---|
| 12 | # provides functions to manage treeviews. |
|---|
| 13 | # |
|---|
| 14 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 18 | # Functions that convert between treeviews and especification files. |
|---|
| 19 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 20 | # File is similar to a .ini file. There are two special items: ('children' |
|---|
| 21 | # and 'label') for each node. Here's an example: |
|---|
| 22 | # [Root] |
|---|
| 23 | # Children=1 |
|---|
| 24 | # Label=Nodo raiz |
|---|
| 25 | # Type= |
|---|
| 26 | # Data1=bla,bla,... |
|---|
| 27 | # DataN=bla,bla,... |
|---|
| 28 | # [1] |
|---|
| 29 | # Children= |
|---|
| 30 | # Label=Label del nodo 1 |
|---|
| 31 | # Type= |
|---|
| 32 | # Data1=bla,bla,... |
|---|
| 33 | # DataN=bla,bla,... |
|---|
| 34 | # |
|---|
| 35 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 36 | |
|---|
| 37 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 38 | proc ::BayesTreeview::TreeviewFromFile {tv path {proc ""}} { |
|---|
| 39 | # |
|---|
| 40 | # PURPOSE: Loads a treeview info data from a text especification file of a |
|---|
| 41 | # treeview. |
|---|
| 42 | # |
|---|
| 43 | # PARAMETERS: |
|---|
| 44 | # tv -> treeview's path |
|---|
| 45 | # path -> name of file |
|---|
| 46 | # proc -> procedure that converts type node to the path of the image of node |
|---|
| 47 | # |
|---|
| 48 | # RETURN: Returns 1 if treeview has been loaded correctly or 0 if not. |
|---|
| 49 | # |
|---|
| 50 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 51 | if ![file exists $path] { |
|---|
| 52 | return -code error "File $path doesn't exist" |
|---|
| 53 | } |
|---|
| 54 | Tolcon_Trace "Llamada a TreeviewFromFile" |
|---|
| 55 | set fd [iniparse:openfile $path] |
|---|
| 56 | NodeFromFile $tv root "" $fd $proc |
|---|
| 57 | iniparse:closefile $fd |
|---|
| 58 | return 1 |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 63 | proc ::BayesTreeview::TreeviewToFile {tv path} { |
|---|
| 64 | # |
|---|
| 65 | # PURPOSE: Saves a treeview info data into a text especification file of a |
|---|
| 66 | # treeview. |
|---|
| 67 | # |
|---|
| 68 | # PARAMETERS: |
|---|
| 69 | # tv -> treeview's path |
|---|
| 70 | # path -> name of file |
|---|
| 71 | # |
|---|
| 72 | # RETURN: Returns 1 if file has been saved correctly or 0 if not. |
|---|
| 73 | # |
|---|
| 74 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 75 | #borrar archivo pq iniparse:openfile abre para solo lectura si existe |
|---|
| 76 | file delete $path |
|---|
| 77 | set fd [iniparse:openfile $path] |
|---|
| 78 | NodeToFile $tv root $fd |
|---|
| 79 | iniparse:closefile $fd |
|---|
| 80 | return 1 |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 85 | proc ::BayesTreeview::NodeToFile {tv index fd} { |
|---|
| 86 | # |
|---|
| 87 | # PURPOSE: Saves a treeview's node info data into a text file. |
|---|
| 88 | # |
|---|
| 89 | # PARAMETERS: |
|---|
| 90 | # tv -> treeview's path |
|---|
| 91 | # index -> treeview node index |
|---|
| 92 | # fd -> channel identifier of text file |
|---|
| 93 | # |
|---|
| 94 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 95 | set key $index |
|---|
| 96 | |
|---|
| 97 | # Guardar data del nodo actual |
|---|
| 98 | array set aryData [$tv entry cget $index -data] |
|---|
| 99 | foreach item [array names aryData] { |
|---|
| 100 | writeini $fd $key $item $aryData($item) |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | writeini $fd $key Label [$tv entry cget $index -label] |
|---|
| 104 | # Guardar sus hijos |
|---|
| 105 | writeini $fd $key Children [$tv entry children $index] |
|---|
| 106 | |
|---|
| 107 | # Llamar recursivamente para los hijos |
|---|
| 108 | foreach child [$tv entry children $index] { |
|---|
| 109 | NodeToFile $tv $child $fd |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 115 | proc ::BayesTreeview::NodeFromFile {tv index parent fd {imgFromType ""}} { |
|---|
| 116 | # |
|---|
| 117 | # PURPOSE: Loads a treeview's node info data from a text file. |
|---|
| 118 | # |
|---|
| 119 | # PARAMETERS: |
|---|
| 120 | # tv -> treeview's path |
|---|
| 121 | # index -> treeview node index |
|---|
| 122 | # parent -> treeview node's parent index |
|---|
| 123 | # fd -> channel identifier of text file |
|---|
| 124 | # |
|---|
| 125 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 126 | set key $index |
|---|
| 127 | |
|---|
| 128 | if ![string equal $parent ""] { |
|---|
| 129 | set idx [$tv insert -at $parent end $index ] |
|---|
| 130 | } else { |
|---|
| 131 | set idx root |
|---|
| 132 | } |
|---|
| 133 | # Configurar data del nodo actual |
|---|
| 134 | foreach item [readini $fd $key] { |
|---|
| 135 | set value [readini $fd $key $item] |
|---|
| 136 | if {[lsearch -exact [$tv column names] $item] != -1} { |
|---|
| 137 | $tv entry configure $idx -data [list $item $value] |
|---|
| 138 | } |
|---|
| 139 | if [string equal $item "Label"] { |
|---|
| 140 | $tv entry configure $idx -label $value |
|---|
| 141 | } |
|---|
| 142 | if {[string equal $item "Type"] && ![string equal $imgFromType ""]} { |
|---|
| 143 | if ![string equal [set img [$imgFromType $value]] ""] { |
|---|
| 144 | $tv entry configure $idx -icons [list $img $img] |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | # Llamar recursivamente para los hijos |
|---|
| 149 | foreach child [readini $fd $key Children] { |
|---|
| 150 | NodeFromFile $tv $child $idx $fd $imgFromType |
|---|
| 151 | } |
|---|
| 152 | } |
|---|