File: unit_thumbnail.pas

package info (click to toggle)
astap 2024.11.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,032 kB
  • sloc: pascal: 49,240; sh: 205; makefile: 5
file content (327 lines) | stat: -rw-r--r-- 10,095 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
unit unit_thumbnail; {FPC unit, shows FITS images as thumbnails (3*X)in a form using Timages. Form is fully resizable and thumbnails (Timage) will follow using the Timage stretch function}
{$mode delphi}
{Copyright (C) 2018, 2024 by Han Kleijn, www.hnsky.org
 email: han.k.. at...hnsky.org

 This Source Code Form is subject to the terms of the Mozilla Public
 License, v. 2.0. If a copy of the MPL was not distributed with this
 file, You can obtain one at https://mozilla.org/MPL/2.0/.   }

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, Menus;

type

  { Tthumbnails1 }
  Tthumbnails1 = class(TForm)
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    renameimage1: TMenuItem;
    changedirectory1: TMenuItem;
    MenuItem5: TMenuItem;
    copyto1: TMenuItem;
    moveto1: TMenuItem;
    Panel1: TPanel;
    PopupMenu1: TPopupMenu;
    procedure copyto1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: char);
    procedure FormPaint(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure renameimage1Click(Sender: TObject);
    procedure changedirectory1Click(Sender: TObject);
  private
    procedure imageMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);{generic for all Timages}
    procedure imageMouseMove(Sender: TObject; Shift: TShiftState; X,  Y: Integer);{generic for all Timages}
  public
  end;

var
  thumbnails1: Tthumbnails1;
  chosenDirectory : string;
const
   thumbnails1_width: integer=1500;{default}
   thumbnails1_height: integer=700;


procedure plot_thumbnails; {plot images in new created timage}
 {plot images in new created timage}

implementation

uses astap_main;

var
  imageindex: integer;
  image_sender: tobject;
  Myimages : Array of Timage;
{$R *.lfm}


procedure Tthumbnails1.FormShow(Sender: TObject);
begin
  thumbnails1.width:=thumbnails1_width;
  thumbnails1.height:=thumbnails1_height;
end;

procedure Tthumbnails1.MenuItem1Click(Sender: TObject);
begin
  {filename2 is set in Tthumbnails1.ImageMouseDown}
  thumbnails1.close;
  load_image(filename2,img_loaded,head,mainwindow.memo1.lines,true,true {plot});
end;

procedure Tthumbnails1.ImageMouseDown(Sender: TObject; Button: TMouseButton;{generic for all Timages}
  Shift: TShiftState; X, Y: Integer);
var
   hint : string;
begin
  hint:=TControl(Sender).hint;
  filename2:=hint;

  image_sender:=sender;

  if button=mbright then
   {$ifdef fpc}
   PopupMenu1.PopUp;{call popup manually if right key is released, not when clicked. Set in popupmenu autopopup off !!!}
   {$else} {delphi}
   PopupMenu1.PopUp(x,y);{call popup manually if right key is released, not when clicked. Set in popupmenu autopopup off !!!}
   {$endif}

   if button=mbleft then
   begin
     thumbnails1.close;
     load_image(filename2,img_loaded,head,mainwindow.memo1.lines,true,true {plot});
   end;
end;


procedure Tthumbnails1.MenuItem2Click(Sender: TObject);
var
  filename_new: string;
begin
  {filename2 is set in Tthumbnails1.ImageMouseDown}
  deletefile(changeFileExt(filename2,'.bak'));{delete *.bak left over from astrometric solution}
  filename_new:=ChangeFileExt(filename2,'.bak');
  RenameFile(filename2,filename_new);
  TControl(image_sender).width:=20;{make very small indicating renamed}
  TControl(image_sender).height:=20;
  TControl(image_sender).hint:=filename_new;
end;


procedure Tthumbnails1.renameimage1Click(Sender: TObject);
var
   value: string;
begin

  value:=InputBox('New name:','',filename2);
  if value=''  then exit;
  RenameFile(filename2,value);
end;

procedure Tthumbnails1.changedirectory1Click(Sender: TObject);
var i: integer;
begin
  if SelectDirectory('Select a directory', ExtractFileDir(filename2){initialdir} , chosenDirectory) then
  begin
    for i:=0 to imageindex-1 do  {resize images}
      begin
        with Myimages[i] do  {contains the Timages}
        begin
          free;
        end;

      end;
    imageindex:=0;
    plot_thumbnails;{load new thumnails}
  end;
end;

procedure Tthumbnails1.imageMouseMove(Sender: TObject; Shift: TShiftState; X,  Y: Integer);{generic for all Timages}
begin
  thumbnails1.caption:=TControl(Sender).hint;;{copy hint}
end;

procedure Tthumbnails1.FormPaint(Sender: TObject);
begin
  if imageindex=0 then
    plot_thumbnails;//fill with images
end;

procedure Tthumbnails1.FormKeyPress(Sender: TObject; var Key: char);
begin
   if key=#27 then
   begin
     esc_pressed:=true;
     thumbnails1.caption:='ESC pressed, stopped reading images.';
   end;
end;

procedure Tthumbnails1.FormCreate(Sender: TObject);
begin
  imageindex:=0;
end;

procedure Tthumbnails1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  thumbnails1_width:=thumbnails1.width; {remember}
  thumbnails1_height:=thumbnails1.height;
end;

procedure Tthumbnails1.copyto1Click(Sender: TObject);
var
  path2,destname : string;
  OK: Boolean;
begin
   {filename2 is set in Tthumbnails1.ImageMouseDown}
   if SelectDirectory('Select a directory', ExtractFileDir(filename2){initialdir} , path2) then
   begin
      destname:= path2+PathDelim+ExtractFilename(filename2);
      if destname=filename2 then
        ShowMessage('Abort!, source and destination are the same.')
      else
      begin
        if not FileExists(DestName) or  //only copy if the file does not exists yet, or the user accepts overwriting the existig one
        (MessageDlg('File exists: overwrite?',mtConfirmation,[mbYes,mbNo],0) = mrYes) then
        begin
          //try to copy/move the file
          if sender=copyto1 then OK := CopyFile(filename2,destname , [cffPreserveTime, cffOverwriteFile])
          else
          if sender=moveto1 then OK := renameFile(filename2,destname) {move the file to a diffent location by renaming}
          else
          ok:=false;{should never happen, programmers failure}

          if not OK then ShowMessage('Write error!!');
        end;
      end;
   end;
end;

procedure Tthumbnails1.FormResize(Sender: TObject);
var
   x,y,max_height,i,ww: integer;
   ratio : double;
begin
  if imageindex=0 then exit;
  thumbnails1.panel1.width:=thumbnails1.width-25;
  x:=0;
  y:=0;
  max_height:=0;

  for i:=0 to imageindex-1 do  {resize images}
  begin
    with Myimages[i] do  {contains the Timages}
    begin
       if Myimages[i]=nil then
          exit;

       ratio:=height/width;
       Width := round((thumbnails1.panel1.width-2)/3);
       height := round(width*ratio);
       if height>max_height then max_height:=height;{find largest heigth}
       thumbnails1.VertScrollBar.Increment:=max_height;

       left :=x;
       top := y;
       inc(x,Width+1);
       if x+1>=thumbnails1.panel1.width then {new row}
       begin
         x:=0;
         y:=y+max_height+1;
         max_height:=0;
       end;
      thumbnails1.panel1.height:=y+max_height;
    end;
  end;
end;


procedure plot_thumbnails; {plot images in new created timage}
var
    newimage : timage;
    x,y,max_height:integer;
    searchResult : TSearchRec;
const
    nrimages: integer =200;
begin
  esc_pressed:=false;

  x:=0;
  y:=0;
  setlength(Myimages,nrimages);
  thumbnails1.panel1.width:=thumbnails1.width-25;
  thumbnails1.panel1.height:=thumbnails1.height*2;
  max_height:=0;
  if SysUtils.findfirst(chosenDirectory+PathDelim+'*.fit*', faAnyFile, searchResult) = 0 then
  begin
    repeat
      newImage := TImage.Create(thumbnails1.panel1);
      Inc(imageIndex);
      if imageindex>=nrimages then //increase size
      begin
        nrimages:=nrimages+100;
        setlength(Myimages,nrimages);
     end;
      with thumbnails1.panel1 do
      begin
        newimage.parent:=thumbnails1.panel1;
        newImage.Tag := imageIndex;
        newImage.Name := 'Thumb_Image' + IntToStr(imageIndex);
        newImage.Visible := True;

        filename2:= chosenDirectory+PathDelim+searchResult.Name;
        thumbnails1.caption:=filename2;{show whats happening}
        if load_fits(filename2,false {light},true,false {update memo},0,memox,head,img_loaded) then
        begin
          if head.naxis<2 then exit; {WCS file}
          use_histogram(img_loaded,true {update}); {plot histogram, set sliders}

          newImage.Width := round((thumbnails1.panel1.width-2)/3);
          newImage.height := round(newImage.Width* head.height/head.width);
          if newImage.height>max_height then max_height:=newImage.height;{find largest heigth}

          thumbnails1.VertScrollBar.Increment:=max_height;

          newImage.left :=x;
          newImage.top := y;
          inc(x,newImage.Width+1);
          if x+1>=thumbnails1.panel1.width then {new row}
          begin
            x:=0;
            y:=y+max_height+1;
            max_height:=0;
          end;
          newImage.hint := filename2; //inttostr(imageindex);
          newimage.OnMouseDown:= thumbnails1.imageMouseDown;
          newimage.onMouseMove:=thumbnails1.imageMouseMove;
          newImage.showhint := true;
          newImage.stretch := true;
          myimages[imageIndex-1]:=newimage;{store the timage}

          plot_fits(newimage,false,false);     {mainwindow.image1.Visible:=true; is done in plot_fits}

          thumbnails1.panel1.height:=y+max_height; {causes a repaint}

          application.processmessages;

        end;//successfull image load
      end;
    until ((SysUtils.FindNext(searchResult) <> 0) or (esc_pressed));

    // Must free up resources used by these successful finds
    SysUtils.FindClose(searchResult);
  end;
end;


end.