Recent

Author Topic: order image TImageList  (Read 3877 times)

lugdanum

  • New Member
  • *
  • Posts: 14
order image TImageList
« on: April 27, 2018, 12:57:46 pm »
Bonjour

Est-ce que quelqu'un aurait la gentillesse de m'expliquer pourquoi avec ce code :
Code: Pascal  [Select][+][-]
  1. //list image in thumbtmp : 0.jpg, 1.jpg, 2.jpg....
  2.  
  3.     SL := FindAllFilesEx('thumbtmp','*',True, faDirectory);
  4.  
  5.       ImageList1.BeginUpdate;
  6.       ListView1.Items.BeginUpdate;
  7.       try
  8.         for i:=0 to SL.Count-1 do
  9.             begin
  10.               jpg := TJpegImage.Create;
  11.               jpg.LoadFromFile(SL[i]);
  12.               ImageList1.Add(jpg, nil);
  13.             end;
  14.  
  15.       finally
  16.         for j := 0 to ImageList1.Count - 1 do
  17.              begin
  18.              LI := ListView1.Items.Add;
  19.              LI.Caption := 'blabla....';
  20.              LI.ImageIndex := j;
  21.              LI.SubItems.Add('blabla....');
  22.              LI.SubItemImages[0] := j; // SubItems[ColumnIndex] := ImageIndex;
  23.              end;
  24.  
  25.         Listview1.Items.EndUpdate;
  26.         ImageList1.EndUpdate;
  27.  
  28.       end;
  29.   SL.Free;                        
  30.  

Mon dossier thumbtmp contient des images nommées 0.jpg, 1.jpg, 2.jpg ....
Or quand je les affiches dans ma Listview1 elles apparaissent n'importe comment, elle ne respecte pas l'ordre alphabétique.
J'ai essayé aussi de les faire apparaitre par date de création mais rien à faire... L'ordre est aléatoire, aucune logique mais c'est toujours le même par contre.

Si quelqu'un a une idée, je le remercie d'avance !

----------------------------------------------
Hello

Would anyone be kind enough to explain to me why with this code:
Code: Pascal  [Select][+][-]
  1. //list image in thumbtmp : 0.jpg, 1.jpg, 2.jpg....
  2.  
  3.     SL := FindAllFilesEx('thumbtmp','*',True, faDirectory);
  4.  
  5.       ImageList1.BeginUpdate;
  6.       ListView1.Items.BeginUpdate;
  7.       try
  8.         for i:=0 to SL.Count-1 do
  9.             begin
  10.               jpg := TJpegImage.Create;
  11.               jpg.LoadFromFile(SL[i]);
  12.               ImageList1.Add(jpg, nil);
  13.             end;
  14.  
  15.       finally
  16.         for j := 0 to ImageList1.Count - 1 do
  17.              begin
  18.              LI := ListView1.Items.Add;
  19.              LI.Caption := 'blabla....';
  20.              LI.ImageIndex := j;
  21.              LI.SubItems.Add('blabla....');
  22.              LI.SubItemImages[0] := j; // SubItems[ColumnIndex] := ImageIndex;
  23.              end;
  24.  
  25.         Listview1.Items.EndUpdate;
  26.         ImageList1.EndUpdate;
  27.  
  28.       end;
  29.   SL.Free;                        
  30.  

My thumbtmp folder contains images named 0.jpg, 1.jpg, 2.jpg ....
But when I post them in my Listview1 they appear anyhow, it does not respect the alphabetical order.
I also tried to make them appear by date of creation but nothing to do ... The order is random, no logic but it is always the same cons.

If anyone has an idea, I thank him in advance!

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: order image TImageList
« Reply #1 on: April 27, 2018, 01:36:53 pm »
Because you expect that this:
Code: Pascal  [Select][+][-]
  1. SL := FindAllFilesEx('thumbtmp','*',True, faDirectory);
makes all files sorted alphabetically. But it is not true. Files in directories are not sorted.
Just try this:
Code: Pascal  [Select][+][-]
  1. y
  2.         for i:=0 to SL.Count-1 do
  3.             begin
  4.               jpg := TJpegImage.Create;
  5.               writeln(SL[i]);    //  <-  add this line to see the order
  6.               jpg.LoadFromFile(SL[i]);
  7.               ImageList1.Add(jpg, nil);
  8.             end;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: order image TImageList
« Reply #2 on: April 27, 2018, 01:49:52 pm »
It is straightforward to sort the stringlist of file names.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.LoadImages;
  2. var
  3.   sl: TStringList;
  4.   i, j: Integer;
  5.   jpg: TJPEGImage;
  6.   li: TListItem;
  7. begin
  8.   ImageList1.BeginUpdate;
  9.   ListView1.Items.BeginUpdate;
  10.     sl := FindAllFiles('thumbtmp', '*.jpg', True, faDirectory);
  11.     try
  12.     sl.Sort;
  13.     jpg := TJpegImage.Create;
  14.       try
  15.         for i:=0 to sl.Count-1 do
  16.         begin
  17.           jpg.LoadFromFile(sl[i]);
  18.           j := ImageList1.Add(jpg, Nil);
  19.           li := ListView1.Items.Add;
  20.           with li do
  21.           begin
  22.             Caption := Format('Caption %d, %d',[i, j]);
  23.             ImageIndex := j;
  24.             SubItems.Add('blabla....');
  25.             SubItemImages[0] := j; // SubItems[ColumnIndex] := ImageIndex;
  26.           end;
  27.         end;
  28.       finally
  29.         jpg.Free;
  30.       end;
  31.     finally
  32.       sl.Free;
  33.     end;
  34.   Listview1.Items.EndUpdate;
  35.   ImageList1.EndUpdate;
  36. end;

lugdanum

  • New Member
  • *
  • Posts: 14
Re: order image TImageList
« Reply #3 on: April 27, 2018, 02:13:36 pm »
Merci beaucoup pour vos réponses !

Avec ton code howardpc le tri est largement mieux ! (voir fichier en pièce jointe)

il affiche bien la première 0.jpg mais après c'est la 10.jpg et ensuite 11.jpg, 1.jpg, 2.jpg, 3.jpg.... Manque plus qu'a trouver comment classer par 1,2 ,3,.....,10,11

Merci en tout cas à vous 2!!
-----------------------------------------
Thanks a lot for your answers !

With your code howardpc sorting is much better! (see file attached)

it displays the first 0.jpg but after it is the 10.jpg and then 11.jpg, 1.jpg, 2.jpg, 3.jpg .... Missing more than find how to classify by 1.2 , 3, ....., 10.11

Thank you in any case to you 2 !!

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
Re: order image TImageList
« Reply #4 on: April 27, 2018, 02:51:48 pm »
Salut lugdanum

Si tu le souhaites il existe une communauté pour FPC et Lazarus en français : https://lazarus.developpez.com

Pourquoi ta liste n'est pas triée ? C'est du à l'OS en lui même.

Tu peux trier la stringlist après ta recherche et justement un membre de développez en a discuté hier ici : https://www.developpez.net/forums/d1845635/autres-langages/pascal/lazarus/comportement-bizarre-findfirst-finnext-sous-ubuntu/

dont voici une des réponses fournies par "hmira"

Quote
Il n'a jamais été stipulé nulle part que les fonctions FindFirst et FindNext retournent les noms de fichiers classés par ordre alphabétique, et ce, y compris sous Windows !

Les fonctions FindFirst et FindNext renvoient les noms de fichiers dans l'ordre dans lequel le système de fichiers sous-jacent décide, et veut bien, de les fournir. Toute supposition de votre part d’un ordre établi, en l’occurrence, un ordre alphabétique, serait hasardeuse !

Pour résoudre ce problème, une technique commune, consiste à stocker les fichiers ainsi que leurs caractéristiques (date création, taille, etc.), dans une liste d’objets, voire même dans une simple TStringList s’il s’agit uniquement des noms de fichiers, puis de classer les objets de la liste selon une fonction personnalisée de votre choix, définie par vous-même, puis passer la dite fonction de tri à la méthode TStringList.CustomSort.

A+


lugdanum

  • New Member
  • *
  • Posts: 14
Re: order image TImageList
« Reply #5 on: April 27, 2018, 03:18:47 pm »
Salut lugdanum

Si tu le souhaites il existe une communauté pour FPC et Lazarus en français : https://lazarus.developpez.com

Pourquoi ta liste n'est pas triée ? C'est du à l'OS en lui même.

Tu peux trier la stringlist après ta recherche et justement un membre de développez en a discuté hier ici : https://www.developpez.net/forums/d1845635/autres-langages/pascal/lazarus/comportement-bizarre-findfirst-finnext-sous-ubuntu/

dont voici une des réponses fournies par "hmira"

Quote
Il n'a jamais été stipulé nulle part que les fonctions FindFirst et FindNext retournent les noms de fichiers classés par ordre alphabétique, et ce, y compris sous Windows !

Les fonctions FindFirst et FindNext renvoient les noms de fichiers dans l'ordre dans lequel le système de fichiers sous-jacent décide, et veut bien, de les fournir. Toute supposition de votre part d’un ordre établi, en l’occurrence, un ordre alphabétique, serait hasardeuse !

Pour résoudre ce problème, une technique commune, consiste à stocker les fichiers ainsi que leurs caractéristiques (date création, taille, etc.), dans une liste d’objets, voire même dans une simple TStringList s’il s’agit uniquement des noms de fichiers, puis de classer les objets de la liste selon une fonction personnalisée de votre choix, définie par vous-même, puis passer la dite fonction de tri à la méthode TStringList.CustomSort.

A+

Merci beaucoup, je vais voir ça tout de suite !
J'ai jamais pensé à aller sur  developpez.net pour Lazarus alors que j'y vais pour Python !

Encore merci !!

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: order image TImageList
« Reply #6 on: April 27, 2018, 03:51:33 pm »
A suitable CustomSort might be as follows (depending on the format of your image file names):

Code: Pascal  [Select][+][-]
  1. function CompareNumeric(List: TStringList; Index1, Index2: Integer): Integer;
  2.  
  3.   function GetNumFromName(anIndex: Integer): Integer;
  4.   begin
  5.     Exit(StrToIntDef(ExtractFileNameOnly(List[anIndex]), -1));
  6.   end;
  7.  
  8. begin
  9.   Exit(GetNumFromName(Index1) - GetNumFromName(Index2));
  10. end;
  11.  
  12. procedure TForm1.LoadImages;
  13. var
  14.   sl: TStringList;
  15.   i, j: Integer;
  16.   jpg: TJPEGImage;
  17.   li: TListItem;
  18. begin
  19.   ImageList1.BeginUpdate;
  20.   ListView1.Items.BeginUpdate;
  21.     sl := FindAllFiles('thumbtmp', '*.jpg', True, faDirectory);
  22.     try
  23.     sl.CustomSort(@CompareNumeric);
  24.     jpg := TJpegImage.Create;
  25.       try ...
               

lugdanum

  • New Member
  • *
  • Posts: 14
Re: order image TImageList
« Reply #7 on: April 27, 2018, 04:59:44 pm »
A suitable CustomSort might be as follows (depending on the format of your image file names):

Code: Pascal  [Select][+][-]
  1. function CompareNumeric(List: TStringList; Index1, Index2: Integer): Integer;
  2.  
  3.   function GetNumFromName(anIndex: Integer): Integer;
  4.   begin
  5.     Exit(StrToIntDef(ExtractFileNameOnly(List[anIndex]), -1));
  6.   end;
  7.  
  8. begin
  9.   Exit(GetNumFromName(Index1) - GetNumFromName(Index2));
  10. end;
  11.  
  12. procedure TForm1.LoadImages;
  13. var
  14.   sl: TStringList;
  15.   i, j: Integer;
  16.   jpg: TJPEGImage;
  17.   li: TListItem;
  18. begin
  19.   ImageList1.BeginUpdate;
  20.   ListView1.Items.BeginUpdate;
  21.     sl := FindAllFiles('thumbtmp', '*.jpg', True, faDirectory);
  22.     try
  23.     sl.CustomSort(@CompareNumeric);
  24.     jpg := TJpegImage.Create;
  25.       try ...
             

Ça fonctionne du tonnerre ! C'est génial, merci beaucoup!!!!
It works with thunder! It's great, thank you very much !!!!

 :D

circular

  • Hero Member
  • *****
  • Posts: 4217
    • Personal webpage
Re: order image TImageList
« Reply #8 on: April 30, 2018, 10:15:16 am »
La traduction de "ça fonctionne du tonnerre" semble plutôt être "it works like a hot damn".

Il y a aussi le plus classique "it works like a charm".

Bonne journée
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018