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]



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

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


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

def Cancelled
  @Cancelled
end

#DeterminedObject (readonly)

Is the current dialog in determined mode ?

Boolean


55
56
57
# File 'lib/rUtilAnts/GUI.rb', line 55

def Determined
  @Determined
end

Instance Method Details

#inc_value(iIncrement = 1) ⇒ Object

Increment the progress value

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



180
181
182
183
# File 'lib/rUtilAnts/GUI.rb', line 180

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

#incRange(iIncrement = 1) ⇒ Object

Increment the progress range

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



189
190
191
192
193
194
195
196
197
198
# File 'lib/rUtilAnts/GUI.rb', line 189

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

#pulseObject

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



201
202
203
204
205
206
207
208
# File 'lib/rUtilAnts/GUI.rb', line 201

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

#refreshStateObject

Called to refresh our dialog



147
148
149
150
151
152
# File 'lib/rUtilAnts/GUI.rb', line 147

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

#set_range(iRange) ⇒ Object

Set the progress range

Parameters
  • iRange (Integer): The progress range



158
159
160
161
162
163
164
165
# File 'lib/rUtilAnts/GUI.rb', line 158

def set_range(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



171
172
173
174
# File 'lib/rUtilAnts/GUI.rb', line 171

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