Class: RUtilAnts::GUI::ProgressDialog

Inherits:
Wx::Dialog
  • Object
show all
Defined in:
lib/rUtilAnts/GUI.rb

Overview

Generic progress dialog, meant to be overriden to customize behaviour

Direct Known Subclasses

BitmapProgressDialog, TextProgressDialog

Constant Summary collapse

DEFAULT_UNDETERMINED_RANGE =

Value for the undetermined range

10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iParentWindow, iCodeToExecute, iParameters = {}) ⇒ ProgressDialog

Constructor

Parameters:

  • iParentWindow (Wx::Window): Parent window

  • iCodeToExecute (Proc): The code to execute that will update the progression

  • iParameters (map<Symbol,Object>): Additional parameters:

** :Cancellable (Boolean): Can we cancel this dialog ? [optional = false] ** :Title (String): Caption of the progress dialog [optional = ”] ** :Icon (Wx::Bitmap): Icon of the progress dialog [optional = nil]



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
# File 'lib/rUtilAnts/GUI.rb', line 75

def initialize(iParentWindow, iCodeToExecute, iParameters = {})
  lCancellable = iParameters[:Cancellable]
  if (lCancellable == nil)
    lCancellable = false
  end
  lTitle = iParameters[:Title]
  if (lTitle == nil)
    lTitle = ''
  end
  lIcon = iParameters[:Icon]
  super(iParentWindow,
    :title => lTitle,
    :style => Wx::THICK_FRAME|Wx::CAPTION
  )
  if (lIcon != nil)
    lRealIcon = Wx::Icon.new
    lRealIcon.copy_from_bitmap(lIcon)
    set_icon(lRealIcon)
  end

  @CodeToExecute = iCodeToExecute

  # Has the transaction been cancelled ?
  @Cancelled = false

  # Create components
  @GProgress = Wx::Gauge.new(self, Wx::ID_ANY, 0)
  @GProgress.set_size_hints(-1,12,-1,12)
  lBCancel = nil
  if (lCancellable)
    lBCancel = Wx::Button.new(self, Wx::CANCEL, 'Cancel')
  end
  lPTitle = getTitlePanel

  # Put them into sizers
  lMainSizer = Wx::BoxSizer.new(Wx::VERTICAL)
  lMainSizer.add_item(lPTitle, :flag => Wx::GROW|Wx::ALL, :proportion => 1, :border => 8)
  if (lCancellable)
    lBottomSizer = Wx::BoxSizer.new(Wx::HORIZONTAL)
    lBottomSizer.add_item(@GProgress, :flag => Wx::ALIGN_CENTER|Wx::ALL, :proportion => 1, :border => 4)
    lBottomSizer.add_item(lBCancel, :flag => Wx::ALIGN_CENTER, :proportion => 0)
    lMainSizer.add_item(lBottomSizer, :flag => Wx::GROW|Wx::ALL, :proportion => 0, :border => 8)
  else
    lMainSizer.add_item(@GProgress, :flag => Wx::GROW|Wx::ALL, :proportion => 0, :border => 4)
  end
  self.sizer = lMainSizer

  # Set events
  if (lCancellable)
    evt_button(lBCancel) do |iEvent|
      @Cancelled = true
      lBCancel.enable(false)
      lBCancel.label = 'Cancelling ...'
      self.fit
    end
  end
  lExecCompleted = false
  evt_idle do |iEvent|
    # Execute the code once
    if (!lExecCompleted)
      lExecCompleted = true
      @CodeToExecute.call(self)
    end
    self.end_modal(Wx::ID_OK)
  end

  # By default, consider that we don't know the range of progression
  # That's why we set a default range (undetermined progression needs a range > 0 to have visual effects)
  @GProgress.range = DEFAULT_UNDETERMINED_RANGE
  @Determined = false

  self.fit

  refreshState
end

Instance Attribute Details

#CancelledObject (readonly)

Has the current dialog been cancelled ?

Boolean


64
65
66
# File 'lib/rUtilAnts/GUI.rb', line 64

def Cancelled
  @Cancelled
end

#DeterminedObject (readonly)

Is the current dialog in determined mode ?

Boolean


60
61
62
# File 'lib/rUtilAnts/GUI.rb', line 60

def Determined
  @Determined
end

Instance Method Details

#incRange(iIncrement = 1) ⇒ Object

Increment the progress range

Parameters:

  • iIncrement (Integer): Value to increment [optional = 1]



194
195
196
197
198
199
200
201
202
203
# File 'lib/rUtilAnts/GUI.rb', line 194

def incRange(iIncrement = 1)
  if (@Determined)
    @GProgress.range += iIncrement
  else
    @Determined = true
    @GProgress.range = iIncrement
    @GProgress.value = 0
  end
  refreshState
end

#incValue(iIncrement = 1) ⇒ Object

Increment the progress value

Parameters:

  • iIncrement (Integer): Value to increment [optional = 1]



185
186
187
188
# File 'lib/rUtilAnts/GUI.rb', line 185

def incValue(iIncrement = 1)
  @GProgress.value += iIncrement
  refreshState
end

#pulseObject

Pulse the progression (to be used when we don’t know the range)



206
207
208
209
210
211
212
213
# File 'lib/rUtilAnts/GUI.rb', line 206

def pulse
  if (@Determined)
    @Determined = false
    @GProgress.range = DEFAULT_UNDETERMINED_RANGE
  end
  @GProgress.pulse
  refreshState
end

#refreshStateObject

Called to refresh our dialog



152
153
154
155
156
157
# File 'lib/rUtilAnts/GUI.rb', line 152

def refreshState
  self.refresh
  self.update
  # Process eventual user request to stop transaction
  Wx.get_app.yield
end

#setRange(iRange) ⇒ Object

Set the progress range

Parameters:

  • iRange (Integer): The progress range



163
164
165
166
167
168
169
170
# File 'lib/rUtilAnts/GUI.rb', line 163

def setRange(iRange)
  @GProgress.range = iRange
  if (!@Determined)
    @Determined = true
    @GProgress.value = 0
  end
  refreshState
end

#setValue(iValue) ⇒ Object

Set the progress value

Parameters:

  • iValue (Integer): The progress value



176
177
178
179
# File 'lib/rUtilAnts/GUI.rb', line 176

def setValue(iValue)
  @GProgress.value = iValue
  refreshState
end