User Tools

Site Tools


k17:k17.1:start

17.1 ProgressBar - Progress Bar

As visual indicators of the status of a task being performed by a programme, Throbbers are often used in addition to different mouse pointers. Throbbers are small animated graphics of various sizes and designs. A well-known throbber graphic is the spinning wheel. Throbbers mark the beginning and the end of a current operation (examples: downloading, uploading or decompressing a file, waiting time).

Figure 17.1.1: Four Throbbers

Often not only the status is of interest, but also information about the progress of a task. Progress bars are used precisely for this application.

Thunderbird_Upload

Figure 17.1.1: Status display and progress bar in the email client Thunderbird

With the component ProgressBar (gb.qt4) Gambas provides a progress bar. In a ProgressBar, the progress can not only be signalled visually via the length of the displayed bar, but also optionally text (specified as a percentage) can be displayed on the bar.

Three typical cases of use have proven themselves for a ProgressBar:

  • (Relative) display of how long a process will take.
  • (Relative) Display of how far a process has already been completed.
  • Alternating display as status display: process not completed

17.1.1 Properties and methods ProgressBar

This ProgressBar (gb.qt4) class implements a progress bar.

There are only 2 selected properties and one special method for a ProgressBar:

  • Property Label As Boolean → Indicates whether a percentage value is displayed or not.
  • Property Value As Float → Determines or sets the value of the ProgressBar (relative length of the progress bar displayed . This value must be between 0 and 1.
  • Method Reset → The ProgressBar.Reset() method resets the value to 0.

Notes:

  • If you have set or changed the value of the progressbar, then you should follow this with a Wait so that the change is immediately visible.
  • Since the ProgressBar only accepts numbers (data type Float) between 0 and 1 as input values, you must transform the values to be processed to this unit interval [0|1]. A transformation function then performs a good service.
  • Although the property name Label suggests otherwise, you cannot attach your own text to the progress bar. The data type of Label is Boolean!

17.1.2 Project 1 - Progressbar as Throbber

In the presented project ThrobberPB, well-known Throbbers are used and a Progressbar is configured as a Throbber. It is also shown how you can display numerical values in a Progressbar (static and relative) - visualise them. In this context, a function is presented with which you can map numbers from an interval [x0|x1] linearly to an interval [y0|y1].

Public Function Transform(fValue As Float, x0 As Float, x1 As Float, y0 As Float, y1 As Float) As Float
' Lineare Abbildung: Intervall [x0|x1] ---> [y0|y1]
' 2-Punkte-Gleichung mit P1(x0|y0) und P2(x1|y2) im Definitionsbereich [x0|x1]
' (y-y0)*(x1-x0) = (y1-y0)*(x-x0)
' y = ((y1-y0)/(x1-x0)*(x-x0)) + y0 → y=g(x)=m*x+n
 
  If Abs(x1 - x0) > 0.0001 Then
     Return (((y1 - y0) / (x1 - x0)) * (fValue - x0)) + y0
  Else
     Message.Error("FEHLER!")
  Endif ' Abs(x1 - x0) > 0.0001 ?
 
End ' Function  Transform(..)

B3

Figure 17.1.2.1: Three throbbers and relative display number interval [0|60] → [0|1].

The source code snippet shows the main procedures:

[1] ' Gambas class file
[2]
[3] Private bSuccess As Boolean = False
[4]
[5] Public Sub _new()
[6]   Slider1.MinValue = 0
[7]   Slider1.MaxValue = 60
[8]   Slider1.Value = 20
[9]   ...
[10] End ' _new()
[11]
[12] Public Function Transformation(...)
[13] ...
[14] Public Sub Slider1_Change()
[15]   Progressbar1.Value = Transformation(Slider1.Value, Slider1.MinValue, Slider1.MaxValue, 0, 1)
[16]   Wait
[17] End ' hSlider1_Change
[18]
[19] Public Sub btnWaitOutStart_Click()
[20]   WaitOut(0.05)
[21]  End ' btnWaitOutStart_Click()
[22]
[23] Public Sub btnWaitOutStop_Click()
[24]   bSuccess = True
[25] End ' btnWaitOutStop_Click()
[26]
[27] Public Sub WaitOut(iTime As Float)
[28]   Dim iCount As Integer
[29]
[30]   If iTime < 0.01 Or iTime > 0.2 Then iTime = 0.05
[31]   pbWait.Visible = True
[32]   pbWait.Label = False
[33]   Repeat
[34]      For iCount = 0 To 10
[35]        Try pbWait.Value = iCount / 10
[36]        Wait iTime
[37]      Next
[38]      For iCount = 0 To 10
[39]        Try pbWait.Value = 1 - (iCount / 10)
[40]        Wait iTime
[41]      Next
[42]   Until bSuccess = True
[43]   bSuccess = False
[44]
[45] End ' WaitOut(iTime As Integer)
[46]
[47] Public Sub btnStart_Click()
[48]  movieboxWaitOut.Path = "WaitOutIcons/time.bar.a.gif"
[49]  movieboxWaitOut.Playing = True ' Animierte Grafik 1 zeigen
[50]  movieboxWaitOut2.Path = "WaitOutIcons/arrows_a.gif"
[51]  movieboxWaitOut2.Playing = True ' Animierte  Grafik 2 zeigen
[52] End ' btnStart_Click()
[53]
[54] Public Sub btnStop_Click()
[55]  movieboxWaitOut.Playing = False
[56]  movieboxWaitOut.Path = "WaitOutIcons/time.bar.0.gif"
[57]  movieboxWaitOut.Playing = True ' Standbild 1 zeigen
[58]  movieboxWaitOut2.Playing = False
[59]  movieboxWaitOut2.Path = "WaitOutIcons/arrows_0.gif"
[60]  movieboxWaitOut2.Playing = True ' Standbild 2 zeigen
[61] End ' btnStop_Click()
[62]
[63] Public Sub btnClose_Click()
[64]   FMain.Close
[65] End ' btnClose_Click()
[66] Public Sub Form_Close()
[67]   bSuccess = True
[68] End ' Form_Close()

If you start the programme, you will certainly recognise possibilities to use this and similar throbbers as well as the relative display of number intervals in your own programmes.

17.1.3 Project 2 - Progress bar as progress indicator for a file download

In this project, the challenge is to determine the total size of the download file before the download as well as current file size of the download file locally in the download folder during the download. Therefore, an external (console) programme, wget, is used in a process so that the programme's output can be collected, filtered and evaluated. As a special feature, it turned out that the wget programme redirects all outputs from the standard (internal) output to the standard error output! In the project, the hints - especially from chapter 21.3.3 Project - Process Control and Process Data - were successfully implemented. Therefore, only the source code of the two most important procedures is presented to you.

DL1

Figure 17.1.3.1: The download is active

DL2

Figure 17.1.3.2: The download is finished

[1] Public Sub Timer1_Timer()
[2]   Dim iCurrentFileSize As Integer
[3]
[4]   iCurrentFileSize = Stat($sArchivPath).Size ' Aktuelle Dateigröße beim Download
[5]   ProgressBar1.Value = iCurrentFileSize / $iArchivSize ' Wert im Intervall [0-1]
[6]   Wait
[7] End ' Timer1_Timer()
[8]
[9] Public Sub myWGETProcess_Error(sOutput As String)
[10] ' Alle Ausgaben kommen über die Standard-Fehler-Ausgabe
[11]   Dim aMatrix, aListe As String[]
[12]   Dim sElement As String
[13]   Dim iSizeMB As Float
[14]
[15]   aMatrix = Split(sOutput, gb.newline)
[16]   For Each sElement In aMatrix
[17]       If sElement Begins "Länge:" Or sElement Begins "Length:" Then
[18]          aListe = Split(sElement, " ")
[19]          $iArchivSize = CInteger(aListe[1]) ' 0,9765625 (Byte), 0,953674316 (MB)
[20]          iSizeMB = Round(($iArchivSize / 1000000) * 0.953674316, -1)
[21]          txaContent.Insert(gb.NewLine)
[22]          txaContent.Insert("Archiv = " & Str($iArchivSize) & " Byte  »  ( " & Str(iSizeMB) & " MiB )")
[23]          txaContent.Insert(gb.NewLine & gb.NewLine)
[24]          Timer1.Start
[25]       Endif ' sElement Begins "Länge:" ?
[26]       If sElement Ends "200 OK" Then
[27]          SetLEDColor("red")
[28]       Endif ' Ends "200 OK" ?
[29]       If sElement Like "* Server existiert." Or sElement Like "*Remote file exists*" Then
[30]          SetLEDColor("green")
[31]          txaContent.Insert("Dateigrößenübermittlung abgeschlossen!" & gb.NewLine)
[32]       Endif ' LIKE ... ?
[33]       If sElement Like "*gespeichert*" Or sElement Like "*saved*" Then
[34]          SetLEDColor("green")
[35]          txaContent.Insert("Download beendet!" & gb.NewLine)
[36]          Wait 2
[37]          Timer1.Stop
[38]          ProgressBar1.Value = 1
[39]          Wait
[40]          $bFlag = True
[41]       Endif ' LIKE ... ?
[42]
[43]   Next ' sElement
[44] End ' myWGETProcess_Error(..)

The unpacking of the archive file is signalled with a Throbber. The Throbber graphic LevelBar only shows up in a MovieBox during unpacking:

LeverBar

Figure 17.1.3.3: The download archive is unpacked - LevelBar as Throbber

Project 3 can only be found in the download area. It uses a ProgressBar on the one hand to show how many files are still to be processed (Time-remaining Progress Indicator) and a progress bar on the other hand to show the progress of a file download of k files:

GambasWikiBook

Figure 17.1.3.4: Download progress

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.
k17/k17.1/start.txt · Last modified: 30.09.2023 by emma

Page Tools