Grupowanie właściwości

1

Witam wszystkich, potrzebuję nieco rozbudować komponent TListBox o kilkanaście właściwości, które będą umieszczone w trzech podstawowych grupach; Jak na razie próbuję zrobić pierwszą grupę, ale coś mi nie wychodzi...

Potrzebuję, by na liście w ObjectInspector pojawiła się grupa Colors, w której znajdować się będą takie właściwości:

  • Text
  • TextActive
  • Back
  • BackActive
  • DotsLine
    Wszystkie te właściwości mają przechowywać kolor (czyli typ TColor); Grupa ma się nazywać Colors;

Sugerowałem się artykułami z 4p:
http://4programmers.net/Delphi/ARtykuły/Jak_korzystać_z_TPersistent
i ze strony FlyLib:
http://flylib.com/books/en/1.228.1.154/1/

niestety grupa się pokazuje, ale nie chce się rozwijać... Grzebię w tym już od południa i dalej nie wiem, co jest źle, kod poniżej:

unit FldListBox;

interface

uses
  SysUtils,
  Classes,
  Controls,
  StdCtrls,
  Graphics;

type
  TFoldersListBoxColors = class(TPersistent)
  private
    FText: TColor;
    FTextActive: TColor;
    FBack: TColor;
    FBackActive: TColor;
    FDotsLine: TColor;
  published
    property Text: TColor read FText write FText;
    property TextActive: TColor read FTextActive write FTextActive;
    property Back: TColor read FBack write FBack;
    property BackActive: TColor read FBackActive write FBackActive;
    property DotsLine: TColor read FDotsLine write FDotsLine;
  end;

type
  TFoldersListBox = class(TListBox)
  private
    FColors: TFoldersListBoxColors;
  public
    {$WARNINGS OFF}
    constructor Create(AOwner: TComponent);
    destructor Destroy(); override;
    {$WARNINGS ON}
  published
    property Colors: TFoldersListBoxColors read FColors write FColors;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('pnggc', [TFoldersListBox]);
end;

constructor TFoldersListBox.Create(AOwner: TComponent);
begin
  inherited Create(Aowner);
  FColors := TFoldersListBoxColors.Create();

  with FColors do
    begin
      FText := clBlack;
      FTextActive := clWhite;
      FBack := clWhite;
      FBackActive := clMenuHighlight;
      FDotsLine := clGray;
    end;
end;

destructor TFoldersListBox.Destroy();
begin
  FColors.Free();
  inherited Destroy();
end;

end.

Moduł się oczywiście kompiluje, w programie 'kładąc' komponent na formie też nie wyskakują żadne błędy, program też się kompiluje i działa bez zarzutu, usuwać - też się usuwa z pamięci bezproblemowo;

Moje pytanie brzmi - co jeszcze mam zrobić by klasa działała jak należy?

0

No nic, uporałem się w końcu z tym; Poniżej zamieszczam kod modułu, który działa w 100%:

unit FldListBox;

interface

uses
  SysUtils,
  Classes,
  Controls,
  StdCtrls,
  Graphics;

type
  TFoldersListBoxIcons = class(TPersistent)
  private
    FPlus: TIcon;
    FMinus: TIcon;
    FDesktop: TIcon;
    FMyDocs: TIcon;
    FHardDrive: TIcon;
    FCDRom: TIcon;
    FFolder: TIcon;
  protected
    procedure SetPlus(Value: TIcon);
    procedure SetMinus(Value: TIcon);
    procedure SetDesktop(Value: TIcon);
    procedure SetMyDocs(Value: TIcon);
    procedure SetHardDrive(Value: TIcon);
    procedure SetCDRom(Value: TIcon);
    procedure SetFolder(Value: TIcon);
  public
    constructor Create();
    destructor Destroy(); override;
  published
    property Plus: TIcon read FPlus write SetPlus;
    property Minus: TIcon read FMinus write SetMinus;
    property Desktop: TIcon read FDesktop write SetDesktop;
    property MyDocs: TIcon read FMyDocs write SetMyDocs;
    property HardDrive: TIcon read FHardDrive write SetHardDrive;
    property CDRom: TIcon read FCDRom write SetCDRom;
    property Folder: TIcon read FFolder write SetFolder;
  end;

type
  TFoldersListBoxColors = class(TPersistent)
  private
    FText: TColor;
    FTextActive: TColor;
    FBack: TColor;
    FBackActive: TColor;
    FDotsLine: TColor;
  published
    property Text: TColor read FText write FText;
    property TextActive: TColor read FTextActive write FTextActive;
    property Back: TColor read FBack write FBack;
    property BackActive: TColor read FBackActive write FBackActive;
    property DotsLine: TColor read FDotsLine write FDotsLine;
  end;

type
  TFoldersListBoxOffsets = class(TPersistent)
  private
    FSubItem: Byte;
    FButton: Byte;
    FIcon: Byte;
    FText: Byte;
  published
    property SubItem: Byte read FSubItem write FSubItem;
    property Button: Byte read FButton write FButton;
    property Icon: Byte read FIcon write FIcon;
    property Text: Byte read FText write FText;
  end;

type
  TFoldersListBoxGroups = class(TPersistent)
  private
    FIcons: TFoldersListBoxIcons;
    FColors: TFoldersListBoxColors;
    FOffsets: TFoldersListBoxOffsets;
  published
    procedure SetIcons(Value: TFoldersListBoxIcons);
    procedure SetColors(Value: TFoldersListBoxColors);
    procedure SetOffsets(Value: TFoldersListBoxOffsets);
  public
    constructor Create();
    destructor Destroy(); override;
  published
    property Icons: TFoldersListBoxIcons read FIcons write SetIcons;
    property Colors: TFoldersListBoxColors read FColors write SetColors;
    property Offsets: TFoldersListBoxOffsets read FOffsets write SetOffsets;
  end;

type
  TFoldersListBox = class(TListBox)
  private
    FGroups: TFoldersListBoxGroups;
  protected
    procedure SetGroups(Value: TFoldersListBoxGroups);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy(); override;
  published
    property Tree: TFoldersListBoxGroups read FGroups write SetGroups;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('pnggc', [TFoldersListBox]);
end;



constructor TFoldersListBoxIcons.Create();
begin
  inherited Create();

  FPlus := TIcon.Create();
  FMinus := TIcon.Create();
  FDesktop := TIcon.Create();
  FMyDocs := TIcon.Create();
  FHardDrive := TIcon.Create();
  FCDRom := TIcon.Create();
  FFolder := TIcon.Create();
end;

destructor TFoldersListBoxIcons.Destroy();
begin
  FPlus.Free();
  FMinus.Free();
  FDesktop.Free();
  FMyDocs.Free();
  FHardDrive.Free();
  FCDRom.Free();
  FFolder.Free();

  inherited Destroy();
end;

procedure TFoldersListBoxIcons.SetPlus(Value: TIcon);
begin
  FPlus.Assign(Value);
end;

procedure TFoldersListBoxIcons.SetMinus(Value: TIcon);
begin
  FMinus.Assign(Value);
end;

procedure TFoldersListBoxIcons.SetDesktop(Value: TIcon);
begin
  FDesktop.Assign(Value);
end;

procedure TFoldersListBoxIcons.SetMyDocs(Value: TIcon);
begin
  FMyDocs.Assign(Value);
end;

procedure TFoldersListBoxIcons.SetHardDrive(Value: TIcon);
begin
  FHardDrive.Assign(Value);
end;

procedure TFoldersListBoxIcons.SetCDRom(Value: TIcon);
begin
  FCDRom.Assign(Value);
end;

procedure TFoldersListBoxIcons.SetFolder(Value: TIcon);
begin
  FFolder.Assign(Value);
end;



constructor TFoldersListBoxGroups.Create();
begin
  inherited Create();

  FIcons := TFoldersListBoxIcons.Create();

  FColors := TFoldersListBoxColors.Create();

  with FColors do
    begin
      FText := clBlack;
      FTextActive := clWhite;
      FBack := clWhite;
      FBackActive := clMenuHighlight;
      FDotsLine := clGray;
    end;

  FOffsets := TFoldersListBoxOffsets.Create();

  with FOffsets do
    begin
      FSubItem := 0;
      FButton := 0;
      FIcon := 0;
      FText := 0;
    end;
end;

destructor TFoldersListBoxGroups.Destroy();
begin
  FIcons.Free();
  FColors.Free();
  FOffsets.Free();

  inherited Destroy();
end;

procedure TFoldersListBoxGroups.SetIcons(Value: TFoldersListBoxIcons);
begin
  FIcons.Assign(Value);
end;

procedure TFoldersListBoxGroups.SetColors(Value: TFoldersListBoxColors);
begin
  FColors.Assign(Value);
end;

procedure TFoldersListBoxGroups.SetOffsets(Value: TFoldersListBoxOffsets);
begin
  FOffsets.Assign(Value);
end;



constructor TFoldersListBox.Create(AOwner: TComponent);
begin
  inherited Create(Aowner);

  FGroups := TFoldersListBoxGroups.Create();
end;

destructor TFoldersListBox.Destroy();
begin
  FGroups.Free();

  inherited Destroy();
end;

procedure TFoldersListBox.SetGroups(Value: TFoldersListBoxGroups);
begin
  FGroups.Assign(Value);
end;

end.

co daje efekt:
GroupedProps.png
Kod udostępniam, bo może się komuś jeszcze przydać (Można na jego podstawie rozwinąć artykuł na tym forum odnoście używania klasy TPersistent); Pozdrawiam;

0

EDIT: Piszę nowy post, bo powyższy jest strasznie długi...

Mam jeszcze pytanie odnośnie deklarowania procedur do ładowania ikon; W jaki sposób mogę skrócić ten kod:

type
  TFoldersListBoxIcons = class(TPersistent)
  private
    FPlus,
    FMinus,
    FDesktop,
    FMyDocs,
    FHardDrive,
    FCDRom,
    FFolder: TIcon;
  protected
    procedure SetPlus(Value: TIcon);
    procedure SetMinus(Value: TIcon);
    procedure SetDesktop(Value: TIcon);
    procedure SetMyDocs(Value: TIcon);
    procedure SetHardDrive(Value: TIcon);
    procedure SetCDRom(Value: TIcon);
    procedure SetFolder(Value: TIcon);
  public
    constructor Create();
    destructor Destroy(); override;
  published
    property Plus: TIcon read FPlus write SetPlus;
    property Minus: TIcon read FMinus write SetMinus;
    property Desktop: TIcon read FDesktop write SetDesktop;
    property MyDocs: TIcon read FMyDocs write SetMyDocs;
    property HardDrive: TIcon read FHardDrive write SetHardDrive;
    property CDRom: TIcon read FCDRom write SetCDRom;
    property Folder: TIcon read FFolder write SetFolder;
  end;

tak, by była tylko jedna procedura dla wszystkich siedmiu ikon? Czy jest taka możliwość? Próbowałem to zrobić, ale nie wiadomo co wpisać w ciele tej procedury; Przydało by się coś takiego jak Self dla tych ikon, ale nie ma;

Ma ktoś jakiś pomysł? Trochę to dziadoskie rozwiązanie, bo gdybym miał grupę, która będzie przechowywać 100 ikon (czy ogólnie grafik) to musiałbym 100 osobnych procedur pisać mimo tego, że wszystkie byłyby jednego typu... Jak można to skrócić?

0

No tak, przyznaję się, lenia dostałem...

fCoords : array[0..3] of Longint;

Można by, ale musiałbym ikony przechowywać w tablicy, a tego nie robię; Choć nawet, gdybym tak zrobił to nie chcę wiedzieć co by się działo w Object Inspector... :P

W każdym razie jak ukończę pracę przy tym komponencie i będę pewny, że działa w 100% to dopiero wtedy spróbuję pakować te ikony do tablicy;

Efekt końcowy:

FoldersListBox.png

1
Furious Programming napisał(a)

No tak, przyznaję się, lenia dostałem...

fCoords : array[0..3] of Longint;

no dostales i to wiekszego niz myslisz skoro Ci sie nawet nie chce w temat zaglebic...

Furious Programming napisał(a)

musiałbym ikony przechowywać w tablicy, a tego nie robię;

po jaką cholerę w tablicy? przyjrzyj sie. przeciez mozesz to zastosowac dla istniejacych zmiennych -jesli tak Ci wygodniej- wystarczy uzyc we wspolnym setterze:

  case index of

i w zaleznosci od indeksu przypisywac nowa wartosc do wybranej zmiennej....

0

Matko święta, no przecież... Gdzie ja miałem głowę jak to pisałem...

1 użytkowników online, w tym zalogowanych: 0, gości: 1