Class: Chip_editor

Inherits:
Qt::Widget
  • Object
show all
Defined in:
lib/class/Chip_editor.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent, chip, action) ⇒ Chip_editor

Returns a new instance of Chip_editor.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
# File 'lib/class/Chip_editor.rb', line 19

def initialize(parent, chip, action)
  super()
  @gui_chip_editor = Ui_Chip_editor.new
  centerWindow(self)
  @gui_chip_editor.setupUi(self)
  @parent = parent
  @chip = chip

  inputRestrict(@gui_chip_editor.lie_pack_name, 2)
  inputRestrict(@gui_chip_editor.lie_pack_pin, 0)
  inputRestrict(@gui_chip_editor.lie_manu_name, 2)
  inputRestrict(@gui_chip_editor.lie_type_name, 2)
  inputRestrict(@gui_chip_editor.lie_reference, 2)
  inputRestrict(@gui_chip_editor.lie_description, 2)

  # Combobox
  # Package
  package = Package.all
  package.each do |p|
    @gui_chip_editor.cbx_package.addItem(p.package_name)
  end
  # Manufacturer
  manufacturer = Manufacturer.all
  manufacturer.each do |p|
    @gui_chip_editor.cbx_manufacturer.addItem(p.manufacturer_name)
  end
  # Type
  c_type = CType.all
  c_type.each do |p|
    @gui_chip_editor.cbx_type.addItem(p.cType_name)
  end

#Bus
@bus_list = Bus.all

  if action != 'new'
    package_name = package.find_by(package_id: @chip.chip_package).package_name
    manufacturer_name = manufacturer.find_by(manufacturer_id: @chip.chip_manufacturer).manufacturer_name
    c_type_name = c_type.find_by(cType_id: @chip.chip_type).cType_name

    @gui_chip_editor.cbx_package.setCurrentIndex(@gui_chip_editor.cbx_package.findText(package_name))
    @gui_chip_editor.lie_pack_name.setText(package_name)
    @gui_chip_editor.lie_pack_name.setEnabled(false)
    @gui_chip_editor.cbx_manufacturer.setCurrentIndex(@gui_chip_editor.cbx_manufacturer.findText(manufacturer_name))
    @gui_chip_editor.lie_manu_name.setText(manufacturer_name)
    @gui_chip_editor.lie_manu_name.setEnabled(false)
    @gui_chip_editor.cbx_type.setCurrentIndex(@gui_chip_editor.cbx_type.findText(c_type_name))
    @gui_chip_editor.lie_type_name.setText(c_type_name)
    @gui_chip_editor.lie_type_name.setEnabled(false)
    @gui_chip_editor.rbn_rectangular.setEnabled(false)
    @gui_chip_editor.rbn_square.setEnabled(false)
    if @chip.chip_voltage.zero?
      @gui_chip_editor.rbn_5v.setChecked(true)
    else
      @gui_chip_editor.rbn_3v.setChecked(true)
    end
    package_shape = Package.find_by(package_id: @chip.chip_package).package_shape
    if package_shape.zero?
      @gui_chip_editor.rbn_square.setChecked(true)
    else
      @gui_chip_editor.rbn_rectangular.setChecked(true)
    end

    # Line Edit
    # Name
    if action == 'edit'
      @gui_chip_editor.lie_reference.setText(@chip.chip_reference)
    end
    # Load pin array
    fill_pin_table('edit')
    # Misc tab
    @gui_chip_editor.lie_description.setText(@chip.chip_detail)
  end

  # Array struct
  @gui_chip_editor.tbl_pins.horizontalHeader.stretchLastSection = true
  @gui_chip_editor.tbl_pins.verticalHeader.setVisible(false)

  # Button text
  if action == 'edit'
    @gui_chip_editor.btn_add.setText('Edit')
    Qt::Object.connect(@gui_chip_editor.btn_add, SIGNAL('clicked()'), self, SLOT('edit_chip()'))
  else
    @gui_chip_editor.btn_add.setText('Add')
    Qt::Object.connect(@gui_chip_editor.btn_add, SIGNAL('clicked()'), self, SLOT('add_chip()'))
  end
rescue Exception => msg
  logger = Logger.new($logFilePath)
  logger.error msg
  Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', 'Error occured when opening the chip wizard interface. Consult the log for more details').exec
end

Instance Method Details

#add_chipObject

Add chip in database



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/class/Chip_editor.rb', line 201

def add_chip
  unless control_form_val
    return
  end
  if Chip.exists?(chip_reference: @gui_chip_editor.lie_reference.text)
    Qt::MessageBox.new(Qt::MessageBox::Critical, 'Invalid form', 'This chip reference already exists').exec
    return
  end
  chip = Chip.new
  # Reference
  chip.chip_reference = @gui_chip_editor.lie_reference.text
  # Custom
  chip.chip_custom = 1
  # Manufacturer
  manu = Manufacturer.find_or_create_by(manufacturer_name: @gui_chip_editor.lie_manu_name.text)
  chip.chip_manufacturer = manu.manufacturer_id
  # Package
  if @gui_chip_editor.cbx_package.currentIndex != 0
    chip.chip_package = Package.find_by(package_name: @gui_chip_editor.cbx_package.currentText).package_id
  else
    if @gui_chip_editor.rbn_square.checked
      shape = 0
    else
      shape = 1
    end
    Package.create(
      package_name:       @gui_chip_editor.lie_pack_name.text,
      package_pinNumber:  @gui_chip_editor.lie_pack_pin.text,
      package_shape:      shape
    )
    chip.chip_package = Package.ids.last
  end
  # Type
  ctype = CType.find_or_create_by(cType_name: @gui_chip_editor.lie_type_name.text)
  chip.chip_type = ctype.cType_id
  # Voltage
  if @gui_chip_editor.rbn_3v.checked
    voltage = 1
  else
    voltage = 0
  end
  chip.chip_voltage = voltage
  # Divers
  chip.chip_detail = @gui_chip_editor.lie_description.text
  chip.save
  # Ajout des PINs
  for i in 0..(@gui_chip_editor.lie_pack_pin.text.to_i) - 1 do
    pin_num = @gui_chip_editor.tbl_pins.item(i, 0)
    pin_signal = @gui_chip_editor.tbl_pins.cellWidget(i, 2)
    pin_bus = @gui_chip_editor.tbl_pins.cellWidget(i, 1)
    if pin_bus.currentIndex == 0
      signal = 62
    else
      signal = Signall.find_by(signal_name: pin_signal.currentText).signal_id
    end
    Pin.create(
      pin_number: pin_num.text.to_i,
      pin_chip: chip.chip_id,
      pin_signal: signal
    )
  end
  # Reloading array
  @parent.feed_chip_array
  close
end

#control_form_valObject

Control the form values



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
# File 'lib/class/Chip_editor.rb', line 112

def control_form_val
  error = ''
  # Package
  if @gui_chip_editor.lie_pack_name.text.empty?
    error = 'Package reference missing'
  # Pin number
  elsif @gui_chip_editor.lie_pack_pin.text.empty?
    error = 'Package pin number missing'
  elsif @gui_chip_editor.tbl_pins.rowCount != @gui_chip_editor.lie_pack_pin.text.to_i
    error = 'The pin number and the line count in the pin array does not match'
  # Reference
  elsif @gui_chip_editor.lie_reference.text.empty?
    error = 'Chip reference missing'
  # Manufacturer
  elsif @gui_chip_editor.lie_manu_name.text.empty?
    error = 'Chip manufacturer missing'
  # Type
  elsif @gui_chip_editor.lie_type_name.text.empty?
    error = 'Chip type missing'
  end
  unless error.empty?
    Qt::MessageBox.new(Qt::MessageBox::Critical, 'Invalid form', error).exec
    return false
else
	return true
  end
end

#delete_cbx_elementObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/class/Chip_editor.rb', line 140

def delete_cbx_element
  msg = Qt::MessageBox.new
  msg.setWindowTitle('Delete element')
  msg.setText('By deleting this element, all the chips and commands linked to him will be deleted too. Continue ?')
  msg.setIcon(Qt::MessageBox::Warning)
  msg.setStandardButtons(Qt::MessageBox::Cancel | Qt::MessageBox::Ok)
  msg.setDefaultButton(Qt::MessageBox::Cancel)
  if msg.exec == Qt::MessageBox::Ok
    case sender.objectName
    when 'btn_removePackage'
      if @gui_chip_editor.cbx_package.currentIndex == 0
        Qt::MessageBox.new(Qt::MessageBox::Warning, 'Warning', 'Please select a valid element to delete').exec
      else
        package = Package.find_by(package_name: @gui_chip_editor.cbx_package.currentText)
        chips = Chip.where(chip_package: package)
        chips.each do |chip|
          chip.cmd.destroy_all
        end
        chips.destroy_all
        package.destroy
        @gui_chip_editor.cbx_package.removeItem(@gui_chip_editor.cbx_package.currentIndex)
        @gui_chip_editor.cbx_package.setCurrentIndex(0)
      end
    when 'btn_removeManufacturer'
      if @gui_chip_editor.cbx_manufacturer.currentIndex == 0
        Qt::MessageBox.new(Qt::MessageBox::Warning, 'Warning', 'Please select a valid element to delete').exec
      else
        manufacturer = Manufacturer.find_by(manufacturer_name: @gui_chip_editor.cbx_manufacturer.currentText)
        chips = Chip.where(chip_manufacturer: manufacturer)
        chips.each do |chip|
          chip.cmd.destroy_all
        end
        chips.destroy_all
        manufacturer.destroy
        @gui_chip_editor.cbx_manufacturer.removeItem(@gui_chip_editor.cbx_manufacturer.currentIndex)
        @gui_chip_editor.cbx_manufacturer.setCurrentIndex(0)
      end
    when 'btn_removeType'
      if @gui_chip_editor.cbx_type.currentIndex == 0
        Qt::MessageBox.new(Qt::MessageBox::Warning, 'Warning', 'Please select a valid element to delete').exec
      else
        type = CType.find_by(cType_name: @gui_chip_editor.cbx_type.currentText)
        chips = Chip.where(chip_type: type)
        chips.each do |chip|
          chip.cmd.destroy_all
        end
        chips.destroy_all
        type.destroy
        @gui_chip_editor.cbx_type.removeItem(@gui_chip_editor.cbx_type.currentIndex)
        @gui_chip_editor.cbx_type.setCurrentIndex(0)
      end
    end
    @parent.feed_chip_array
  end
rescue Exception => msg
  logger = Logger.new($logFilePath)
  logger.error msg
  Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', 'Error occured while deleting the element. Consult the log for more details').exec
end

#edit_chipObject

Edit the chip



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/class/Chip_editor.rb', line 268

def edit_chip
  unless control_form_val
    return 0
  end
  # Reference
  if @chip.chip_reference != @gui_chip_editor.lie_reference.text
    @chip.update(chip_reference: @gui_chip_editor.lie_reference.text)
  end
  # Manufacturer
  manu = Manufacturer.find_or_create_by(manufacturer_name: @gui_chip_editor.lie_manu_name.text)
  if @chip.chip_manufacturer != manu.manufacturer_id
    @chip.update(chip_manufacturer: manu.manufacturer_id)
  end
  # Package
  if @gui_chip_editor.rbn_square.isChecked
    shape = 0
  else
    shape = 1
  end
  package = Package.find_or_create_by(
    package_name:       @gui_chip_editor.lie_pack_name.text,
    package_pinNumber:  @gui_chip_editor.lie_pack_pin.text,
    package_shape:      shape
  )
  if @chip.chip_package != package.package_id
    @chip.update(chip_package: package.package_id)
  end
  # Type
  ctype = CType.find_or_create_by(cType_name: @gui_chip_editor.lie_type_name.text)
  if @chip.chip_type != ctype.cType_id
    @chip.update(chip_type: ctype.cType_id)
  end
  # Voltage
  if @gui_chip_editor.rbn_3v.checked
    voltage = 1
  else
    voltage = 0
  end
  if @chip.chip_voltage != voltage
    @chip.update(chip_voltage: voltage)
  end
  # Divers
  if @chip.chip_detail != @gui_chip_editor.lie_description.text
    @chip.update(chip_detail: @gui_chip_editor.lie_description.text)
  end

  # Edit PINs (Destroy all then recreate)
  @chip.pin.destroy_all
  for i in 0..(@gui_chip_editor.lie_pack_pin.text.to_i) - 1 do
    pin_num = @gui_chip_editor.tbl_pins.item(i, 0)
    pin_signal = @gui_chip_editor.tbl_pins.cellWidget(i, 2)
    pin_bus = @gui_chip_editor.tbl_pins.cellWidget(i, 1)
    if pin_bus.currentIndex == 0
      signal = 62
    else
      signal = Signall.find_by(signal_name: pin_signal.currentText).signal_id
    end
    Pin.create(
      pin_number: pin_num.text.to_i,
      pin_chip: @chip.chip_id,
      pin_signal: signal
    )
  end
  # Reloading array
  @parent.feed_chip_array
  close
rescue Exception => msg
  logger = Logger.new($logFilePath)
  logger.error msg
  Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', 'Error occured while editing the chip. Consult the log for more details').exec
end

#fill_pin_table(action = '') ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/class/Chip_editor.rb', line 391

def fill_pin_table(action = '')
  if @gui_chip_editor.lie_pack_pin.text.to_i <= 144 && @gui_chip_editor.lie_pack_pin.text.to_i > 4
    @gui_chip_editor.tbl_pins.setRowCount(@gui_chip_editor.lie_pack_pin.text.to_i)
    @gui_chip_editor.lie_pack_pin.text.to_i.times do |i|
      cbx_bus = Qt::ComboBox.new
      row = Qt::Variant.new(i)
      cbx_bus.setProperty('row', row)
      @gui_chip_editor.tbl_pins.setCellWidget(i, 1, cbx_bus)
		cbx_signal = Qt::ComboBox.new
      @gui_chip_editor.tbl_pins.setCellWidget(i, 2, cbx_signal)
      item =  Qt::TableWidgetItem.new
		item.setFlags(Qt::ItemIsEnabled)
      item.setData(0, Qt::Variant.new(i.next))
      @gui_chip_editor.tbl_pins.setItem(i, 0, item)
      cbx_bus.addItem('Bus...')
      @bus_list.each do |b|
        cbx_bus.addItem(b.bus_name)
      end
      if action == 'edit'
        pin = Pin.find_by(pin_number: i.next, pin_chip: @chip.chip_id)
        cbx_bus.setCurrentIndex(cbx_bus.findText(pin.signall.bus.pluck(:bus_name)[0]))
        signal_list = Use.where(bus_id: pin.signall.bus.pluck(:bus_id)[0]).pluck(:signal_id)
        signal_list.each do |s|
          cbx_signal.addItem(Signall.find_by(signal_id: s).signal_name)
        end
        cbx_signal.setCurrentIndex(cbx_signal.findText(pin.signall.signal_name))
      end
      Qt::Object.connect(cbx_bus, SIGNAL('currentIndexChanged(int)'), self, SLOT('filter_cbx()'))
    end
  else
    Qt::MessageBox.new(Qt::MessageBox::Warning, 'Invalid pin number value', 'Pin number needs to be between 4 and 144').exec
  end
rescue Exception => msg
  logger = Logger.new($logFilePath)
  logger.error msg
  Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', 'Error occured while adding pins to the table. Consult the log for more details').exec
end

#filter_cbxObject

Filter signals by bus



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/class/Chip_editor.rb', line 430

def filter_cbx
  bus_item_row = sender.property('row').to_i
  busItem = @gui_chip_editor.tbl_pins.cellWidget(bus_item_row, 1)
  associated_signal_item = @gui_chip_editor.tbl_pins.cellWidget(bus_item_row, 2)
  associated_signal_item.clear
  if busItem.currentText != 'Bus...'
    bus = Bus.find_by(bus_name: busItem.currentText).bus_id
    filtered_signal_list = Use.where(bus_id: bus)
    filtered_signal_list.each do |s|
      signalName = Signall.find_by(signal_id: s.signal_id).signal_name
      associated_signal_item.addItem(signalName)
    end
  end
rescue Exception => msg
  logger = Logger.new($logFilePath)
  logger.error msg
  Qt::MessageBox.new(Qt::MessageBox::Critical, 'Critical error', 'Error occured while filtering the signals. Consult the log for more details').exec
end

#select_manufacturerObject

Manufacturer combobox



370
371
372
373
374
375
376
377
378
# File 'lib/class/Chip_editor.rb', line 370

def select_manufacturer
  if @gui_chip_editor.cbx_manufacturer.currentIndex != 0
    @gui_chip_editor.lie_manu_name.setEnabled(false)
    @gui_chip_editor.lie_manu_name.setText(@gui_chip_editor.cbx_manufacturer.currentText)
  else
    @gui_chip_editor.lie_manu_name.setEnabled(true)
    @gui_chip_editor.lie_manu_name.setText('')
  end
end

#select_packageObject

Package combobox



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/class/Chip_editor.rb', line 341

def select_package
  if @gui_chip_editor.cbx_package.currentIndex != 0
    selectedPackage = Package.find_by package_name: @gui_chip_editor.cbx_package.currentText
    @gui_chip_editor.lie_pack_name.setEnabled(false)
    @gui_chip_editor.lie_pack_name.setText(selectedPackage.package_name)
    @gui_chip_editor.lie_pack_pin.setEnabled(false)
    @gui_chip_editor.lie_pack_pin.setText(selectedPackage.package_pinNumber.to_s)
    @gui_chip_editor.rbn_square.setEnabled(false)
    @gui_chip_editor.rbn_rectangular.setEnabled(false)
    if selectedPackage.package_shape == 0
      @gui_chip_editor.rbn_square.setChecked(true)
    else
      @gui_chip_editor.rbn_rectangular.setChecked(true)
    end
    fill_pin_table
  else
    @gui_chip_editor.lie_pack_name.setEnabled(true)
    @gui_chip_editor.lie_pack_name.clear
    @gui_chip_editor.lie_pack_pin.setEnabled(true)
    @gui_chip_editor.lie_pack_pin.clear
    @gui_chip_editor.rbn_square.setChecked(true)
    @gui_chip_editor.rbn_rectangular.setEnabled(true)
    @gui_chip_editor.rbn_square.setEnabled(true)
    @gui_chip_editor.tbl_pins.clear
    @gui_chip_editor.tbl_pins.setRowCount(0)
  end
end

#select_typeObject

Type combobox



381
382
383
384
385
386
387
388
389
# File 'lib/class/Chip_editor.rb', line 381

def select_type
  if @gui_chip_editor.cbx_type.currentIndex != 0
    @gui_chip_editor.lie_type_name.setEnabled(false)
    @gui_chip_editor.lie_type_name.setText(@gui_chip_editor.cbx_type.currentText)
  else
    @gui_chip_editor.lie_type_name.setEnabled(true)
    @gui_chip_editor.lie_type_name.setText('')
  end
end