User Tools

Site Tools


k16:k16.19:start

16.19 Class Spinner

The class Spinner (gb. form) implements a control that shows an animation in two variants:

B1 B2

Figure 16.19.1: Spinner variant with activated progress indicator (Gambas 3.7.1 and Gambas > 3.8.0)

16.19.1 Properties

The class Spinner has these relevant characteristics:

tablelayout

PropertyData typeDescription
BorderBooleanSpecifies whether a frame should be set or not. Use the border class for a list of constants.
EnabledBooleanShows whether the control is enabled or not.
LabelBooleanSpecifies whether a progress value should be displayed or not.
ValueFloatReturns a progress value between 0 (0%) and 1.0 (100%). You can also set the value within the range of values [0|1]. The progress value is only displayed if the Label property is set to True.

Table 16.19.1.1: Properties of the class spinner

16.19.2 Methods

The first two methods are particularly important for the use of the class spinners:

tablelayout

MethodDescription
StartStarts the animation.
StopStop the animation.
WaitCalls the event loop and ensures that the event loop is not called more often than every 200 milliseconds.

Table 16.19.2.1: Methods of the class Spinner

16.19.3 Project

The ArchivDownLoadGIT project adopts the program idea from the FileDownLoad project in? chapter 17.1, where the archive file (gambas source code) is loaded from a server into a specific directory in a certain version and then unpacked. To display the download progress of the archive file on https://gitlab.com/gambas/gambas/repository/stable/archive.tar.bz2, the Progressbar is replaced by a spinner.

B3

Figure 16.19.3.1: Downloading the archive file from the server (GB 3.10.0)

The wget program is used to download the archive file. The outputs of the program wget are evaluated via a suitable process variable hWGETProcess As Process.

Public Sub btnGetArchivFile_Click()  
 
  CreateDirectory(txbArchivDirectory.Text)
 
  If txbCodeSourceURL.Text Begins "https://" Then
     sArchivFileName = File.Name(Replace(txbCodeSourceURL.Text, "https://", ""))
  Else
     sArchivFileName = File.Name(txbCodeSourceURL.Text)
  Endif
 
  txaMessages.Clear()  
  txaMessages.Insert(gb.NewLine)  
  txaMessages.Insert("Quelle: " & gb.Tab & txbCodeSourceURL.Text & gb.NewLine)
  txaMessages.Insert("Ziel: " & gb.Tab & txbArchivDirectory.Text &/ sArchivFileName & gb.NewLine)
  txaMessages.Insert(gb.NewLine)
 
  GetArchiv(txbCodeSourceURL.Text, txbArchivDirectory.Text &/ sArchivFileName) 
 
  txaMessages.Insert(Subst("&1 '&2' &3", ("The archive"), sArchivFileName, ("will be unpacked!")) & "\n"
  Wait 2 
  UnPackArchiv(txbArchivDirectory.Text &/ sArchivFileName)
 
End ' btnGetFile_Click()

In the error event hWGETProcess_Error (sOutput As String), these outputs provide not only the start and end of the download, but also the download progress and the size of the archive on the server, because all outputs of program' wget' come via the standard error output:

Public Sub hWGETProcess_Error(sOutput As String)
 
  Dim aMatrix, aListe As String[]
  Dim sElement As String
  Dim iSizeMB As Float
 
  aMatrix = Split(sOutput, gb.newline)
 
  For Each sElement In aMatrix
      If sElement Ends "200 OK" Then
         txaMessages.Insert(("The connection to the server was successfully established."))
         txaMessages.Insert(gb.NewLine)
      Endif ' sElement Ends "200 OK" ?      
      If sElement Begins "Länge:" Or sElement Begins "Length:" Then
         aListe = Split(sElement, " ")
         iArchivSize = CInteger(aListe[1])
         iSizeMB = Round((iArchivSize / 1000000) * 0.953674316, -1)
         txaMessages.Insert(("Archive size = ") & Str(iArchivSize) & " Byte » (" & Str(iSizeMB) & " MiB )")
         txaMessages.Insert(gb.NewLine & "Download aktiv ..." & gb.NewLine)
         SizeTimer.Start() 
         Spinner1.Label = True
         Spinner1.Start() 
      Endif ' sElement Begins "Länge:" ?      
 
      If sElement Like "*gespeichert*" Or sElement Like "*saved*" Then
         txaMessages.Insert(("Download finished!") & gb.NewLine)
         SizeTimer.Stop()
         Spinner1.Stop() 
         Spinner1.Visible = False
      Endif ' LIKE ?
  Next ' sElement  
 
End ' hWGETProcess_Error(..)

While the archive file is being downloaded, the download progress (in percent) should be displayed in the spinner. To do this, you need the size of the archive file on the server before you download it. You also need to specify the percentage of the archive file that has already been loaded or the current size of the local archive file. In the project, the second variant is implemented with a timer:

Public Sub SizeTimer_Timer()
 
  Dim iCurrentFileSize As Integer
 
  iCurrentFileSize = Stat(txbArchivDirectory.Text &/ sArchivFileName).Size
  Spinner1.Value = iCurrentFileSize / iArchivSize
 
End ' Timer1_Timer()

B4

Figure 16.19.3.2: Unpacking the archive file without progress indicator in the spinner

B5

Figure 16.19.3.3.3: Achieved: The archive has been unpacked and saved.

Download

16.19 Klasse Spinner

Die Klasse Spinner (gb.form) implementiert ein Steuerelement, das eine Animation in zwei Varianten zeigt:

B1 B2

Abbildung 16.19.1: Spinner-Variante mit aktivierter Fortschrittsanzeige (Gambas 3.7.1 und Gambas > 3.8.0)

16.19.1 Eigenschaften

Die Klasse Spinner verfügt u.a. über diese relevanten Eigenschaften:

tablelayout

EigenschaftDatentypBeschreibung
BorderBooleanGibt an, ob ein Rahmen gesetzt werden soll oder nicht. Verwenden Sie die Border-Klasse für eine Liste von Konstanten.
EnabledBooleanZeigt an, ob das Steuerelement aktiviert ist oder nicht.
LabelBooleanGibt an, ob ein Fortschrittswert angezeigt werden soll oder nicht.
ValueFloatGibt einen Fortschrittswert zwischen 0 (0%) und 1.0 (100%) zurück. Sie können den Wert auch innerhalb des Wertebereichs [0|1] setzen. Der Fortschrittswert wird nur dann angezeigt, wenn die Label-Eigenschaft auf True gesetzt ist.

Tabelle 16.19.1.1 : Eigenschaften der Klasse Spinner

16.19.2 Methoden

Für den Einsatz der Klasse Spinner sind vor allem die ersten beiden Methoden bedeutsam:

MethodeBeschreibung
StartStartet die Animation.
StopStoppt die Animation.
WaitRuft die Event-Schleife auf und stellt sicher, dass die Event-Schleife nicht öfter als alle 200 Millisekunden aufgerufen wird.

Tabelle 16.19.2.1 : Methoden der Klasse Spinner

16.19.3 Projekt

Das Projekt ArchivDownLoadGIT übernimmt die Programm-Idee aus dem Projekt FileDownLoad im → Kapitel 17.1, bei dem die Archivdatei (Gambas-Quelltexte) in einer bestimmten Version von einem Server in ein bestimmtes Verzeichnis geladen und anschließend entpackt wird. Zur Anzeige des Download-Fortschritts der Archivdatei auf https://gitlab.com/gambas/gambas/repository/stable/archive.tar.bz2 wird die Progressbar durch einen Spinner ersetzt.

B3

Abbildung 16.19.3.1: Download der Archivdatei vom Server (GB 3.10.0)

Für den Download der Archivdatei wird das Programm wget eingesetzt. Die Ausgaben des Programms wget werden über eine geeignete Prozess-Variable hWGETProcess As Process ausgewertet.

Public Sub btnGetArchivFile_Click()  
 
  CreateDirectory(txbArchivDirectory.Text)
 
  If txbCodeSourceURL.Text Begins "https://" Then
     sArchivFileName = File.Name(Replace(txbCodeSourceURL.Text, "https://", ""))
  Else
     sArchivFileName = File.Name(txbCodeSourceURL.Text)
  Endif
 
  txaMessages.Clear()  
  txaMessages.Insert(gb.NewLine)  
  txaMessages.Insert("Quelle: " & gb.Tab & txbCodeSourceURL.Text & gb.NewLine)
  txaMessages.Insert("Ziel: " & gb.Tab & txbArchivDirectory.Text &/ sArchivFileName & gb.NewLine)
  txaMessages.Insert(gb.NewLine)
 
  GetArchiv(txbCodeSourceURL.Text, txbArchivDirectory.Text &/ sArchivFileName) 
 
  txaMessages.Insert(Subst("&1 '&2' &3", ("The archive"), sArchivFileName, ("will be unpacked!")) & "\n"
  Wait 2 
  UnPackArchiv(txbArchivDirectory.Text &/ sArchivFileName)
 
End ' btnGetFile_Click()

Diese Ausgaben liefern im Error-Ereignis hWGETProcess_Error(sOutput As String) neben Beginn und Ende des Downloads auch den Download-Fortschritt sowie die Größe des Archivs auf dem Server, denn alle Ausgaben von Programm 'wget' kommen über die Standard-Fehler-Ausgabe:

Public Sub hWGETProcess_Error(sOutput As String)
 
  Dim aMatrix, aListe As String[]
  Dim sElement As String
  Dim iSizeMB As Float
 
  aMatrix = Split(sOutput, gb.newline)
 
  For Each sElement In aMatrix
      If sElement Ends "200 OK" Then
         txaMessages.Insert(("The connection to the server was successfully established."))
         txaMessages.Insert(gb.NewLine)
      Endif ' sElement Ends "200 OK" ?      
      If sElement Begins "Länge:" Or sElement Begins "Length:" Then
         aListe = Split(sElement, " ")
         iArchivSize = CInteger(aListe[1])
         iSizeMB = Round((iArchivSize / 1000000) * 0.953674316, -1)
         txaMessages.Insert(("Archive size = ") & Str(iArchivSize) & " Byte » (" & Str(iSizeMB) & " MiB )")
         txaMessages.Insert(gb.NewLine & "Download aktiv ..." & gb.NewLine)
         SizeTimer.Start() 
         Spinner1.Label = True
         Spinner1.Start() 
      Endif ' sElement Begins "Länge:" ?      
 
      If sElement Like "*gespeichert*" Or sElement Like "*saved*" Then
         txaMessages.Insert(("Download finished!") & gb.NewLine)
         SizeTimer.Stop()
         Spinner1.Stop() 
         Spinner1.Visible = False
      Endif ' LIKE ?
  Next ' sElement  
 
End ' hWGETProcess_Error(..)

Während des Downloads der Archivdatei soll im Spinner der Download-Fortschritt (in Prozent) angezeigt werden. Dazu benötigen Sie einerseits – vor dem Download – die Größe der Archivdatei auf dem Server und anderseits Angaben, wie viel Prozent der Archivdatei bereits geladen worden sind oder die aktuelle Größe der lokalen Archivdatei. Im Projekt wird die 2. Variante mit einem Timer umgesetzt:

Public Sub SizeTimer_Timer()
 
  Dim iCurrentFileSize As Integer
 
  iCurrentFileSize = Stat(txbArchivDirectory.Text &/ sArchivFileName).Size
  Spinner1.Value = iCurrentFileSize / iArchivSize
 
End ' Timer1_Timer()

B4

Abbildung 16.19.3.2: Entpacken der Archivdatei ohne Fortschrittsanzeige im Spinner

B5

Abbildung 16.19.3.3: Ziel erreicht: Das Archiv wurde entpackt und gespeichert

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k16/k16.19/start.txt · Last modified: 02.07.2018 (external edit)

Page Tools