Class: Qt::ColorListWidget

Inherits:
ListWidget show all
Defined in:
lib/cosmos/gui/qt.rb

Overview

This class attempts to duplicate Fox’s FXColorList by adding a colored icon to each item in the ListWidget

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ListWidget

#each, #remove_selected_items

Constructor Details

#initialize(parent, enable_key_delete = true) ⇒ ColorListWidget

Returns a new instance of ColorListWidget.



587
588
589
590
591
592
593
594
595
596
597
# File 'lib/cosmos/gui/qt.rb', line 587

def initialize(parent, enable_key_delete = true)
  super(parent)
  # When a layout adds this widget it automatically gets re-parented so
  # store the original parent in case someone needs it
  @original_parent = parent
  @enable_key_delete = enable_key_delete
  @colors = []
  @maps = []
  @icons = []
  setUniformItemSizes(true)
end

Instance Attribute Details

#original_parentObject (readonly)

Returns the value of attribute original_parent.



585
586
587
# File 'lib/cosmos/gui/qt.rb', line 585

def original_parent
  @original_parent
end

Instance Method Details

#addItemColor(text, color = Cosmos::BLACK) ⇒ Object



631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/cosmos/gui/qt.rb', line 631

def addItemColor(text, color = Cosmos::BLACK)
  @colors << color
  @maps << Qt::Pixmap.new(20,14)
  color = Cosmos::getColor(color)
  @maps[-1].fill(color)
  icon = Qt::Icon.new
  icon.addPixmap(@maps[-1], Qt::Icon::Normal)
  icon.addPixmap(@maps[-1], Qt::Icon::Selected)
  @icons << icon
  item = Qt::ListWidgetItem.new(@icons[-1], text.to_s)
  addItem(item)
end

#clearItemsObject



658
659
660
661
662
# File 'lib/cosmos/gui/qt.rb', line 658

def clearItems
  (0...count).each do
    takeItemColor(0)
  end
end

#disposeObject



664
665
666
667
668
# File 'lib/cosmos/gui/qt.rb', line 664

def dispose
  super()
  @maps.each {|map| map.dispose}
  @icons.each {|icon| icon.dispose}
end

#getItemColor(index) ⇒ Object



644
645
646
# File 'lib/cosmos/gui/qt.rb', line 644

def getItemColor(index)
  @colors[index]
end

#keyPressEvent(event) ⇒ Object



622
623
624
625
626
627
628
629
# File 'lib/cosmos/gui/qt.rb', line 622

def keyPressEvent(event)
  if @enable_key_delete
    if (event.key == Qt::Key_Delete || event.key == Qt::Key_Backspace)
      takeItemColor(currentRow)
    end
  end
  super(event)
end

#resize_to_contentsObject



599
600
601
602
603
604
605
606
607
# File 'lib/cosmos/gui/qt.rb', line 599

def resize_to_contents
  return if count == 0
  #setResizeMode(Qt::ListView::Adjust)
  # Disable the vertical scrollbar and don't display it
  verticalScrollBar.setDisabled(true)
  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff)
  # Get the height of an item and multiply by the number of items
  setFixedHeight(visualItemRect(item(0)).height * (count + 1))
end

#set_read_onlyObject



609
610
611
612
613
614
615
616
617
618
619
620
# File 'lib/cosmos/gui/qt.rb', line 609

def set_read_only
  setSelectionMode(Qt::AbstractItemView::NoSelection)
  filter = Qt::Object.new
  filter.define_singleton_method(:eventFilter) do |obj, event|
    if event.type == Qt::Event::KeyPress &&
      (event.key == Qt::Key_Delete || event.key == Qt::Key_Backspace)
      return true
    end
    return false
  end
  installEventFilter(filter)
end

#takeItemColor(row) ⇒ Object



648
649
650
651
652
653
654
655
656
# File 'lib/cosmos/gui/qt.rb', line 648

def takeItemColor(row)
  item = takeItem(row)
  if item
    @colors.delete_at(row)
    @maps.delete_at(row).dispose
    @icons.delete_at(row).dispose
    item.dispose
  end
end