Recent

Author Topic: Creating a TJvThumbView fails  (Read 2459 times)

RedOctober

  • Sr. Member
  • ****
  • Posts: 450
Creating a TJvThumbView fails
« on: November 18, 2019, 10:09:28 pm »
Platform:  Laz 1.8.4  FPC 3.0.4,  OS:  Windows Server 2016
Components:  TPageControl, TJvThumbView
Goal:  To create a new tab, containing a new thumbviewer in the new tab, for each file opened (test exercise, similar to, but not the same as, the production version)(Just a demo of the problem)

I can create a panel, set it's owner, set it's parent, etc, and it works correctly.  However, creating new TJvThumbView components programmatically always fail with a SIGSEV.  The attached file shows the problem.  Select 3 different image files using the 'Load any image" button (F8 step thru) and you'll see the issue right away. 

Central Question:  How do I create new instances of TJvThumbView in each new TabSheet?

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Creating a TJvThumbView fails
« Reply #1 on: November 19, 2019, 12:35:19 am »
Tough... Something's missing in the Delphi conversion.

As a workaround I found that the Thumbview is working when the newly created TabSheet is made the active Tabsheet of the PageControl before the ThumbView is added. And yes, Application.ProcessMessages must be called to complete creation of the ThumbView for some reason. Don't ask me why...

This works:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   fn: String;
  4.   img_idx: Integer;
  5.   new_tab_sht: TTabSheet;  
  6.   new_thm_vw: TJvThumbView;
  7. begin
  8.   if OpenPictureDialog1.Execute then
  9.     for fn in OpenPictureDialog1.Files do
  10.     begin
  11.       new_tab_sht := PageControl1.AddTabSheet;
  12.       new_tab_sht.Caption := 'Tab' + IntToStr(PageControl1.PageCount);
  13.       PageControl1.ActivePage := new_tab_sht;
  14.  
  15.       new_thm_vw := TJvThumbView.Create(Self);
  16.       new_thm_vw.Parent := new_tab_sht;
  17.       new_thm_vw.Align := alClient;
  18.      
  19.       Application.ProcessMessages;
  20.  
  21.       img_idx := new_thm_vw.AddFromFile(fn);
  22.       new_thm_vw.ThumbList[img_idx].Title := FormatDateTime('zzz', Now);    
  23.     end;
  24. end;  

P.S.
I would not set the "Name" of a run-time created class unless you really need it. A non-empty "Name" must be unique and it is your responsibility to make sure that it is. In your sample code it is unique, but after small modifications of the code it may no longer be.

RedOctober

  • Sr. Member
  • ****
  • Posts: 450
Re: Creating a TJvThumbView fails
« Reply #2 on: November 19, 2019, 12:43:44 am »
Thank you so much wp!   I take your point about names, and I'm very careful to ensure uniqueness.   I did not know if programmatically instantiated components were better off with, or without names.  I just assigned one bc I think I'm a bit OCD.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Creating a TJvThumbView fails
« Reply #3 on: November 19, 2019, 01:16:19 am »
@RedOctober

Qick & dirty:

Code: Pascal  [Select][+][-]
  1. function PoorMensUniqName : string;
  2. const alphaNum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrsruvwxyz_0123456789';
  3.  
  4. var i,max : integer;
  5.     begin
  6.     result := '';
  7.     max := Length(alphanum) -10; // first char must be from alphabet
  8.     for i := 1 to 255 do
  9.        begin
  10.        result := result + alphaNum[random(max)+1];
  11.        if i = 1 then max := length(alphaNum);
  12.        end;
  13.     end;                    

Gives impressive Names !

Winni

RedOctober

  • Sr. Member
  • ****
  • Posts: 450
Re: Creating a TJvThumbView fails
« Reply #4 on: November 19, 2019, 10:29:24 pm »
Got the images to display now.  Only problem:  I am giving the images the title of 1, 2, 3 etc. according to the numbers in my database table.  The images are instead sorting according to file name, (which is a random GUID)no matter the setting of "Sorted".  How can I tell the TJvThumbView to NOT sort the images as they are loaded, just leave them loaded in the order that they were loaded.  That wd fix my sort problem.

Thanks in advance for any help.

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Creating a TJvThumbView fails
« Reply #5 on: November 20, 2019, 12:07:19 am »
This is because the sorted list, ThumbList, is a stringlist which holds the filename in its Strings[] property after you called AddFromFile. But there is also an AddThumb(ATitle: String; Redraw: Boolean) which can get any string for the stringlist.Strings[]. After calling this, however, you must extract the thumbnail from the list and set the filename in order to load the image. Something like this:

Code: Pascal  [Select][+][-]
  1. uses
  2.   JvThumbnails, JvThumbviews;
  3.  
  4. var
  5.   thmb: TJvThumbnail;
  6. ...
  7.   JvThumbView1.AddThumb(ATitle, true);
  8.   thmb := JvThumbView1.ThumbList.Thumbnail[JvThumbView1.Count-1];
  9.   thmb.fileName := fn;  // this loads the file
  10.  

When testing this, there were issues with not displaying some images. I think someone (I?) should spend some time in fixing this component...

----------------------

Alternatively, you could turn off sorting (JvListView1.Sorted := false) and add the images in the order that you need.

« Last Edit: November 20, 2019, 12:17:56 am by wp »

RedOctober

  • Sr. Member
  • ****
  • Posts: 450
Re: Creating a TJvThumbView fails
« Reply #6 on: November 20, 2019, 12:20:11 am »
Turning off Sorted does nothing.  It sorts according to filename, just like when Sorted is set to True.

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Creating a TJvThumbView fails
« Reply #7 on: November 20, 2019, 02:24:16 am »
Code: Pascal  [Select][+][-]
  1.   JvThumbView1.Sorted := false;
  2.   JvThumbView1.AddFromFile('image2.jpg');
  3.   JvThumbView1.AddFromFile('image1.jpg');
No. This code makes "image2" appear before "image1". I tested it. Only when the "Sorted = false" line is removed the images are reversed.

Do you extract the file names from an OpenFileDialog? I noticed that they have a strange order in OpenFileDialog.FileNames (at least in Windows 10), definitely not the order they were clicked.
« Last Edit: November 20, 2019, 02:38:17 am by wp »

RedOctober

  • Sr. Member
  • ****
  • Posts: 450
Re: Creating a TJvThumbView fails
« Reply #8 on: November 20, 2019, 03:21:03 am »
Hi Wp.  There is something different between your example project and mine.  In mine, no matter what the setting of "Sort", the images ALWAYS get sorted.  See the attached, and click on "Load 3".  If your system works correctly (the thumbs are displayed as the load order =  3, 2, 1) then there is a difference somewhere else.  I'm on Laz 1.8.4 and FPC 3.0.4. and my target OS and development machine is Windows Server 2016.   Maybe you are on something different?

Also, if you are in the component patching it, none of the six *Bevel* settings have any effect.  I wanted a plain "flat" look with a single border around the thumb (and one dividing the title box from the thumb image box is ok), but I always get that beveled look around the whole image box, and the left single pixel border is missing.

Many thanks for your continued help.  This component does 99% of what I need.


wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Creating a TJvThumbView fails
« Reply #9 on: November 20, 2019, 11:56:29 am »
Both sorting issue and border issue fixed in jvcllaz trunk.

BTW: When studying the source code of AddFromFile I found that there is an event by which a thumbnail title different from the file name can be provided. Doing this the thumbnails should be sorted by this title, not by the file name (not tested).
« Last Edit: November 20, 2019, 12:53:28 pm by wp »

RedOctober

  • Sr. Member
  • ****
  • Posts: 450
Re: Creating a TJvThumbView fails
« Reply #10 on: November 20, 2019, 09:21:37 pm »
Hi wp.  Where is the jvcllaz trunk?  I looked at:  https://sourceforge.net/projects/lazarus-ccr/files/jvcllaz/  but they are the old ones.  I also did a check on my Online Package Manager, and it's still the old one, 1.0.50
Sorry for my ignorance on this.

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Creating a TJvThumbView fails
« Reply #11 on: November 20, 2019, 10:30:29 pm »
https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/jvcllaz/

Ideally you should use svn (on Windows: TortoiseSVN) to download these files. Alternatively (not 100% sure about this, but close!) you download the three thumb*.pas files from folder "run/JvCustomControl" and replace the same files in the same directory of you current jvcl installation by them (make a backup copy first!). If this does not work ask again.

The files in OPM are not up-to-date with the trunk version in the svn repository.

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Creating a TJvThumbView fails
« Reply #12 on: November 21, 2019, 12:28:41 am »
I worked on the TJvImagesViewer which appears to be much more mature and stable than the TJvThumbView. I added a method AddImageFromFile to which you can directly pass the filename and caption (if caption is empty it is extracted from the path to the image). And I added a method SortByCaption which sorts the images by the caption.

I am attaching a demo which covers most of the points that you have addressed so far.

If you don't have svn you can get the modified files from the svn repository as explained in the previous post: Now download the files jvCustomItemViewer.pas and jvImagesViewer.pas and copy them to your current jvcl installation (don't forget the backup of the old files).

RedOctober

  • Sr. Member
  • ****
  • Posts: 450
Re: Creating a TJvThumbView fails
« Reply #13 on: November 21, 2019, 01:49:26 am »
Hi Wp,

Using your updated files I was able to get the JvThumbView working as I need it to so far. (non-sorted, no bevels).  Your edits made the difference.  Thank you for that.  I am also using the OnGetTitle event to load the titles. 

Next: I copied in the files: jvCustomItemViewer.pas and jvImagesViewer.pas into my Laz 1.8.4 and I downloaded your sample project from above.  The sample project doesn't want to recognize the new AddImageFromFile method, says it can't find it, even though, it is present in the correct .pas file.  Something small I am missing I suspect.  I have attached a screen shot. 


jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Creating a TJvThumbView fails
« Reply #14 on: November 21, 2019, 02:58:01 am »
Most likely you still have an old UNIT in there that didn't have the method or the TImageList
is being used in the old 1.8.4 , it may not have that method because TimageList was advanced for the newer releases of laz…

 So I can't say for sure but, you could try to open that file you have in the IDE, change something in it to force the compiler recompile the file so you can get a fresh UNIT from it.
 
 If that works then your problems are solved, if it does not work then maybe it's the fact of the TimageList isn't up to date.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018