Jump to content

Import Export TUniTreeView TXT file


Alessandro
 Share

Recommended Posts

Hi

 

Here is some code that works with TTREEVIEW, which will probably work with TuniTreeView. It saves data to a INI file, which is a text file with some structure. Drag-and-drop also supported.

unit MainForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
  private
    procedure ReloadTree;
  public
    { Public declarations }
  end;

  TTreeNodeData = record
    GUID : String;
    Parent : String;
    Mapped : Boolean;
  end;
  PTreeNodeData = ^TTreeNodeData;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Uses
  INIFiles;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ReloadTree;
  ShowMessage( 'Reloaded!' );
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  INI : TMemINIFile;
  i : integer;
begin
  DeleteFile( 'tree.ini' );
  INI := TMemINIFile.Create( 'tree.ini' );

  for i := 0 to TreeView1.Items.Count - 1 do
  begin
    INI.WriteString( PTreeNodeData(TreeView1.Items.Data)^.GUID, 'NAME', TreeView1.Items.Text );
    INI.WriteString( PTreeNodeData(TreeView1.Items.Data)^.GUID, 'PARENT', PTreeNodeData(TreeView1.Items.Data)^.Parent );
  end;
  INI.UpdateFile;
  INI.Free;

  if Assigned(Sender) then
    ShowMessage( 'Saved!' );
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ReloadTree;
end;

procedure TForm1.ReloadTree;
  procedure MapItems;
  var
    i, j : integer;
    pNodeData : PTreeNodeData;
  begin
    i := 0;
    while (i < TreeView1.Items.Count) do
    begin
      j := 0;
      while (j < TreeView1.Items.Count) do
      begin
        if (PTreeNodeData(TreeView1.Items.Data)^.GUID = PTreeNodeData(TreeView1.Items[j].Data)^.Parent) and
           (PTreeNodeData(TreeView1.Items[j].Data)^.Mapped = False) then
        begin
          PTreeNodeData(TreeView1.Items[j].Data)^.Mapped := True;
          TreeView1.Items[j].MoveTo( TreeView1.Items, naAddChild );
          j := 0;
        end
        else
          inc(j);
      end;
      inc(i);
    end;
  end;

var
  INI : TMemINIFile;
  GUIDs : TStringList;
  Parent, Item : TTreeNode;
  i : integer;
  pNodeData : PTreeNodeData;
begin
  INI := TMemINIFile.Create( 'tree.ini' );
  GUIDs := TStringList.Create;
  INI.ReadSections( GUIDs ); //Load all the sections/GUIDs

  TreeView1.Items.BeginUpdate;
  TreeView1.Items.Clear;

  //Let's add all to the tree...assumption is all is orphans
  for i := 0 to GUIDs.Count - 1 do
  begin
    pNodeData := AllocMem( SizeOf(TTreeNodeData) );
    Initialize( pNodeData^ );
    pNodeData^.GUID := GUIDs.Strings;
    pNodeData^.Mapped := False;
    pNodeData^.Parent := INI.ReadString( GUIDs.Strings, 'PARENT', '' );
    //Create all in root
    Item := TreeView1.Items.Add( Nil, INI.ReadString( GUIDs.Strings, 'NAME', '' ) );
    Item.Data := pNodeData;
  end;

  MapItems;

  TreeView1.Items.EndUpdate;

  GUIDs.Free;
  INI.Free;
end;

procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
  SourceNode, TargetNode : TTreeNode;
begin
  SourceNode := TreeView1.Selected;
  TargetNode := TreeView1.GetNodeAt( X, Y );
  if Assigned(TargetNode) and Assigned(SourceNode) then
  begin
    PTreeNodeData(SourceNode.Data)^.Parent := PTreeNodeData(TargetNode.Data)^.GUID;
    SourceNode.MoveTo( TargetNode, naAddChild );

    //Save INI without messagebox
    Button2Click( Nil );
  end;
end;

procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
var
  Src, Dst: TTreeNode;
begin
  Src := TreeView1.Selected;
  Dst := TreeView1.GetNodeAt(X,Y);
  Accept := Assigned(Dst) and (Src<>Dst);
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  i : integer;
begin
  for i := 0 to TreeView1.Items.Count - 1 do
  begin
    Finalize( PTreeNodeData(TreeView1.Items.Data)^ );
    FreeMem( TreeView1.Items.Data );
  end;
end;

end.

 

 

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...