Ignore:
Timestamp:
Jun 16, 2010, 8:23:07 PM (14 years ago)
Author:
Kevin Toppenberg
Message:

Bug fixes. Improved Adding Image

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cprs/branches/tmg-cprs/CPRS-Chart/fImages.pas

    r800 r801  
    4141    published
    4242  end;
     43
     44  TImgTransferMethod = (itmDropbox,itmDirect,itmRPC);
    4345
    4446  TfrmImages = class(TfrmPage)
     
    9799    ImageInfoList : TList;
    98100    LastDisplayedTIUIEN : AnsiString;
    99     CurImageToLoad : integer;
    100     InTimerFn : Boolean;
     101    NumImagesAvailableOnServer : integer;
     102    ImageIndexLastDownloaded : integer;
    101103    procedure EnsureImageListLoaded();
    102104    procedure ClearImageList();
     
    108110    procedure UpdateImageInfoMemo(Rec: TImageInfo);
    109111    function FileSize(fileName : wideString) : Int64;
     112    function GetImagesCount : integer;
     113    function GetImageInfo(Index : integer) : TImageInfo;
     114    procedure SetupTimer;
    110115  public
    111116    CacheDir : AnsiString;
    112     UseDropBox : boolean;
     117    TransferMethod : TImgTransferMethod;
    113118    DropBoxDir : string;
    114119    NullImageName : AnsiString;
     120    DownloadImagesInBackground : boolean;
    115121    function Decode(input: AnsiString) : AnsiString;
    116122    function Encode(input: AnsiString) : AnsiString;
     
    127133    function DecodeBarcode(LocalFNamePath,ImageType: AnsiString): AnsiString;
    128134    procedure EnsureImagesDownloaded(ImagesList : TStringList);
     135    procedure EnsureALLImagesDownloaded;
     136    property ImagesCount : integer read GetImagesCount;
     137    property ImageInfo[index : integer] : TImageInfo read GetImageInfo;
     138    procedure GetThumbnailBitmapForFName (FName : string; Bitmap : TBitmap);
     139    function ThumbnailIndexForFName (FName : string) : integer;
    129140  published
    130141  end;
     142
     143Const
     144  IMAGE_TRANSFER_METHODS : Array[itmDropbox..itmRPC] of string[32] = (
     145    'Dropbox Transfer', 'Direct Access', 'Embedded in RPCs');
     146  IMAGE_DOWNLOAD_DELAY_BACKGROUND = 30000;
     147  IMAGE_DOWNLOAD_DELAY_FOREGROUND = 100;
     148  NOT_YET_CHECKED_SERVER = -2;
     149
     150  //NOTE: If order is changed in ThumbsImageList, these numbers should be changed
     151  IMAGE_INDEX_IMAGE = 0;
     152  IMAGE_INDEX_ADOBE  = 1;
     153  IMAGE_INDEX_VIDEO  = 2;
     154  IMAGE_INDEX_SOUND  = 3;
     155  IMAGE_INDEX_MISC   = 4;
    131156
    132157var
     
    157182begin
    158183  inherited;
    159   if InTimerFn=false then begin
    160     InTimerFn := true;
    161     EnsureImageListLoaded(); //only does RPC call if CurImageToLoad = 0
    162     if CurImageToLoad <> -1 then begin //-1 means RPC-> no avail images.
    163       if CurImageToLoad < ImageInfoList.Count then begin
    164         ImageTransferForm.ProgressMsg.Caption := 'Downloading Images';
    165         //if ImageTransferForm.Visible = false then begin
    166         //   ImageTransferForm.Show;
    167         //end;
    168         DownloadToCache(CurImageToLoad); //Only load 1 image per timer firing.
    169         SetupTab(CurImageToLoad);
    170         Inc(CurImageToLoad);
    171         if TabControl.TabIndex < 0 then TabControl.TabIndex := 0;
    172         TabControlChange(self);
    173       end else begin
    174         timLoadImages.Enabled := false;
    175         //ImageTransferForm.ProgressBar.Position := 100;
    176         //Sleep(1000);
    177         //ImageTransferForm.Hide;
    178       end;
    179     end;
    180     InTimerFn := false;
    181     if self.Visible = true then begin
    182       timLoadImages.Interval :=100;
    183     end else begin
    184       timLoadImages.Interval :=30000; //30 sec delay
    185     end;
    186   end;
    187 end;
    188 
     184  timLoadImages.Enabled := false;
     185  EnsureImageListLoaded();
     186  if NumImagesAvailableOnServer = 0 then exit;
     187  if (ImageIndexLastDownloaded >= (ImageInfoList.Count-1)) then exit;
     188  ImageTransferForm.ProgressMsg.Caption := 'Downloading Images';
     189  DownloadToCache(ImageIndexLastDownloaded+1); //Only load 1 image per timer firing.
     190  SetupTab(ImageIndexLastDownloaded+1);
     191  Inc(ImageIndexLastDownloaded);
     192  if TabControl.TabIndex < 0 then TabControl.TabIndex := 0;
     193  TabControlChange(self);
     194  SetupTimer;
     195end;
     196
     197procedure TfrmImages.SetupTimer;
     198begin
     199  if DownloadImagesInBackground then begin
     200    timLoadImages.Interval := IMAGE_DOWNLOAD_DELAY_BACKGROUND;
     201  end else begin
     202    timLoadImages.Interval := IMAGE_DOWNLOAD_DELAY_FOREGROUND;
     203  end;
     204  timLoadImages.Enabled := true;
     205end;
    189206
    190207procedure TfrmImages.EnsureImagesDownloaded(ImagesList : TStringList);
     
    219236end;
    220237
     238procedure TfrmImages.EnsureALLImagesDownloaded;
     239//This function's goal is to download ALL images in the FOREground.
     240begin
     241  EnsureImageListLoaded();
     242  if NumImagesAvailableOnServer = 0 then exit;
     243  ImageTransferForm.ProgressMsg.Caption := 'Downloading Images';
     244  while (ImageIndexLastDownloaded < (ImageInfoList.Count-1)) do begin
     245    DownloadToCache(ImageIndexLastDownloaded+1); //Only load 1 image per timer firing.
     246    SetupTab(ImageIndexLastDownloaded+1);
     247    Inc(ImageIndexLastDownloaded);
     248    if TabControl.TabIndex < 0 then TabControl.TabIndex := 0;
     249    TabControlChange(self);
     250  end;
     251end;
    221252
    222253{ TPage common methods --------------------------------------------------------------------- }
     
    241272begin
    242273  inherited;
    243   InTimerFn:=false;
    244274  LastDisplayedTIUIEN := '0';
    245275  ImageInfoList := TList.Create;
    246   CurImageToLoad := 0;
     276  ClearImageList(); //sets up other needed variables.
     277  DownloadImagesInBackground := true;
    247278  CacheDir := ExtractFilePath(ParamStr(0))+ 'Cache';
    248279  //NullImageName := ExtractFilePath(ParamStr(0)) + 'images\blank.htm';
     
    250281  if not DirectoryExists(CacheDir) then ForceDirectories(CacheDir);
    251282
    252   //UseDropBox := uTMGOptions.ReadBool('Use dropbox directory for transfers',false);
    253   if uTMGOptions.ReadInteger('ImageTransferMethod',0) = 0 then begin
     283  TransferMethod := TImgTransferMethod(uTMGOptions.ReadInteger('ImageTransferMethod',2));
     284  {if uTMGOptions.ReadInteger('ImageTransferMethod',0) = 0 then begin
    254285     UseDropBox := True;
    255286  end else begin
    256287     UseDropBox := False;
    257   end;   
     288  end;}
    258289  DropBoxDir := uTMGOptions.ReadString('Dropbox directory','??');
    259290  if DropBoxDir='??' then begin  //just on first run.
     
    273304
    274305procedure TfrmImages.FormShow(Sender: TObject);
    275 var
    276   TIUIEN : AnsiString;
    277                                                          
     306var  TIUIEN : AnsiString;
    278307begin
    279308  inherited;
    280309  TIUIEN := IntToStr(frmNotes.lstNotes.ItemID);
    281   timLoadImages.Enabled := true;
    282   timLoadImages.Interval := 100;
     310  DownloadImagesInBackground := false;
     311  SetupTimer;
    283312  if LastDisplayedTIUIEN <> TIUIEN then begin
    284313    UpdateNoteInfoMemo();
     
    380409        Bitmap.Height := ThumbsImageList.Height;
    381410        index := ThumbsImageList.Add(Bitmap,nil);
    382         TImageInfo(ImageInfoList[i]).TabImageIndex := index;
     411        //TImageInfo(ImageInfoList[i]).TabImageIndex := index;
     412        Rec.TabImageIndex := index;
    383413        Bitmap.Free;
    384414      end else begin
    385         Ext := LowerCase(ExtractFileExt(Rec.CacheFName));
    386         Ext := MidStr(Ext,2,99);
    387         if   (Ext='jpg')
    388           or (Ext='jpeg')
    389           or (Ext='png')
    390           or (Ext='tif')
    391           or (Ext='tiff')
    392           or (Ext='gif')
    393           or (Ext='bmp') then begin
    394             TImageInfo(ImageInfoList[i]).TabImageIndex := 0; //camera image
    395         end else
    396         if   (Ext='pdf') then begin
    397             TImageInfo(ImageInfoList[i]).TabImageIndex := 1; //adobe icon
    398         end else
    399         if   (Ext='avi')
    400           or (Ext='qt')
    401           or (Ext='mpg')
    402           or (Ext='mpeg') then begin
    403             TImageInfo(ImageInfoList[i]).TabImageIndex := 2; //video icon
    404         end else
    405         if   (Ext='mp3')
    406           or (Ext='wma')
    407           or (Ext='au')
    408           or (Ext='wav') then begin
    409             TImageInfo(ImageInfoList[i]).TabImageIndex := 3; //sound icon
    410         end else
    411         begin
    412           TImageInfo(ImageInfoList[i]).TabImageIndex := 4; // misc icon
    413         end;
     415        Rec.TabImageIndex := ThumbnailIndexForFName(Rec.CacheFName);
    414416      end;
    415417    end;
    416418    TabControl.Tabs.Add(' ');  //add the tab.  Thumbnail should exist before this
    417419  end;
     420end;
     421
     422function TfrmImages.ThumbnailIndexForFName (FName : string) : integer;
     423var
     424  index : integer;
     425  Ext : AnsiString;
     426begin
     427  Result := 4; //default
     428  Ext := LowerCase(ExtractFileExt(FName));
     429  Ext := MidStr(Ext,2,99);
     430  if   (Ext='jpg')
     431    or (Ext='jpeg')
     432    or (Ext='png')
     433    or (Ext='tif')
     434    or (Ext='tiff')
     435    or (Ext='gif')
     436    or (Ext='bmp') then begin
     437      Result := IMAGE_INDEX_IMAGE; //camera image
     438  end else
     439  if   (Ext='pdf') then begin
     440      Result := IMAGE_INDEX_ADOBE; //adobe icon
     441  end else
     442  if   (Ext='avi')
     443    or (Ext='qt')
     444    or (Ext='mpg')
     445    or (Ext='mpeg') then begin
     446      Result := IMAGE_INDEX_VIDEO; //video icon
     447  end else
     448  if   (Ext='mp3')
     449    or (Ext='wma')
     450    or (Ext='au')
     451    or (Ext='wav') then begin
     452      Result := IMAGE_INDEX_SOUND; //sound icon
     453  end else
     454  begin
     455    Result := IMAGE_INDEX_MISC; // misc icon
     456  end;
     457end;
     458
     459
     460procedure TfrmImages.GetThumbnailBitmapForFName (FName : string; Bitmap : TBitmap);
     461var index: integer;
     462begin
     463  index := ThumbnailIndexForFName(FName);
     464  ThumbsImageList.GetBitmap(index,Bitmap);
    418465end;
    419466
     
    437484    ImageInfoList.Delete(i);
    438485  end;
     486  NumImagesAvailableOnServer := NOT_YET_CHECKED_SERVER;
     487  ImageIndexLastDownloaded := -1;
    439488end;
    440489
     
    442491procedure TfrmImages.EnsureImageListLoaded();
    443492begin
    444   if CurImageToLoad = 0 then begin
     493  if NumImagesAvailableOnServer = NOT_YET_CHECKED_SERVER then begin
    445494    GetImageList();
    446     if ImageInfoList.Count=0 then CurImageToLoad := -1;
    447495  end;
    448496end;
    449497
    450498procedure TfrmImages.GetImageList();
     499//Sets up ImageInfoList
    451500var
    452501  i,j : integer;
     
    470519    on E: Exception do exit;
    471520  end;
    472   //----MOVED
    473521  StatusText('Retrieving images information...');
    474522  CallV('MAG3 CPRS TIU NOTE', [TIUIEN]);
     
    530578    end;
    531579  end;
    532    //----END MOVED
    533580  StatusText('');
     581  NumImagesAvailableOnServer := ImageInfoList.Count;
    534582end;
    535583
     
    628676  result := false;  //default of failure
    629677  if not FileExists(LocalFNamePath) then exit;
    630   if UseDropBox then begin
     678  //if UseDropBox then begin
     679  if TransferMethod = itmDropbox then begin
    631680    Result := UploadFileViaDropBox(LocalFNamePath,FPath,FName,CurrentImage,TotalImages);
    632681    exit;
    633682  end;
     683  //LATER add support for itmDirect mode
    634684  try
    635685    InFile := TFileStream.Create(LocalFNamePath,fmOpenRead or fmShareCompat);
     
    639689    // (or I could, but I'm not going to change the RPC broker...)
    640690    ImageTransferForm.setMax(InFile.Size);
    641     //ImageTransferForm.ResetStartTime;
    642691    ImageTransferForm.ProgressMsg.Caption := 'Preparing to upload...';
    643692    ImageTransferForm.Show;
     
    786835    DeleteFile(LocalSaveFNamePath);
    787836  end;
    788   if UseDropBox then begin
     837  //if UseDropBox then begin
     838  if TransferMethod = itmDropbox then begin
    789839    Result := DownloadFileViaDropBox(FPath,FName,LocalSaveFNamePath,CurrentImage,TotalImages);
    790840    exit;
    791841  end;
     842  //LATER add support for itmDirect mode
    792843  Result := true; //default to success;
    793844  StatusText('Retrieving full image...');
     
    9701021begin
    9711022  ClearTabPages();
    972   CurImageToLoad := 0;  //a -1 would signal that no images avail to load.
    973   //this will start downloading images after 5 second delay (so that if
     1023  DownloadImagesInBackground := true;
     1024  SetupTimer;
     1025  //This will start downloading images after few second delay (so that if
    9741026  //user is just browsing past note, this won't waste effort.
    9751027  //If user selects images tab, then load will occur without delay.
    976   timLoadImages.Enabled := true;
    977   timLoadImages.Interval := 60000;  //60 sec delay  -- also set in timLoadImagesTimer
    9781028  //Note: OnTimer calls timLoadImagesTimer()
    9791029  UploadImagesButton.Enabled := EditIsActive;
     
    10281078  if not IsAbortResult(AddResult) then begin
    10291079    NewNoteSelected(true);  //force a reload to show recently added image.
    1030     timLoadImages.Interval := 100;
     1080    timLoadImages.Interval := IMAGE_DOWNLOAD_DELAY_FOREGROUND;
    10311081    Node := TORTreeNode(frmNotes.tvNotes.Selected);
    10321082    case Node.StateIndex of
     
    10421092begin
    10431093  inherited;
     1094  DownloadImagesInBackground := true;
     1095
    10441096//  Application.MessageBox('Here I can hide images.','title');
    10451097end;
     
    12771329end;
    12781330
     1331function TfrmImages.GetImagesCount : integer;
     1332//Returns number of images possible, not just those already downloaded.
     1333begin
     1334  EnsureImageListLoaded();
     1335  Result := NumImagesAvailableOnServer;
     1336end;
     1337
     1338function TfrmImages.GetImageInfo(Index : integer) : TImageInfo;
     1339begin
     1340  if (Index > -1) and (Index < ImageInfoList.Count) then begin
     1341    Result := TImageInfo(ImageInfoList[Index]);
     1342  end else begin
     1343    Result := nil;
     1344  end;
     1345end;
     1346
     1347
     1348
    12791349initialization
    12801350  //put init code here
Note: See TracChangeset for help on using the changeset viewer.