Changeset 7995:57de76ee12a3 in orange


Ignore:
Timestamp:
06/11/11 14:03:22 (3 years ago)
Author:
janezd <janez.demsar@…>
Branch:
default
Convert:
020f196f77ecb569d18add636037b9688839e085
Message:

Bars in Data Table are now colored according to class

Location:
orange/OrangeWidgets
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • orange/OrangeWidgets/Data/OWDataTable.py

    r7867 r7995  
    1919from orngDataCaching import * 
    2020from PyQt4 import * 
     21import OWColorPalette 
    2122 
    2223############################################################################## 
     
    3435             
    3536class ExampleTableModel(QAbstractItemModel): 
    36     def __init__(self, examples, dist, *args): 
    37         QAbstractItemModel.__init__(self, *args) 
     37    def __init__(self, examples, dist, parent, *args): 
     38        QAbstractItemModel.__init__(self, parent, *args) 
     39        self.myParent = parent  # parent() is overloaded 
    3840        self.examples = examples 
    3941        self.dist = dist 
     
    7779            dist = self.dist[col] 
    7880            return QVariant((dist.max - float(val)) / (dist.max - dist.min or 1)) 
     81        elif role == OWGUI.TableBarItem.ColorRole and self.myParent.colorByClass \ 
     82               and domain.classVar and domain.classVar.varType==orange.VarTypes.Discrete: 
     83                cls = example.get_class() 
     84                if not cls.isSpecial(): 
     85                    return QVariant(self.myParent.discPalette[int(cls)].name()) 
    7986         
    8087        return self._other_data.get((index.row(), index.column(), role), QVariant()) 
     
    129136 
    130137class OWDataTable(OWWidget): 
    131     settingsList = ["showDistributions", "showMeta", "distColorRgb", "showAttributeLabels", "autoCommit"] 
     138    settingsList = ["showDistributions", "showMeta", "distColorRgb", "showAttributeLabels", "autoCommit", "selectedSchemaIndex", "colorByClass"] 
    132139 
    133140    def __init__(self, parent=None, signalManager = None): 
     
    146153        self.locale = QLocale() 
    147154        self.autoCommit = False 
    148  
     155        self.colorSettings = None 
     156        self.selectedSchemaIndex = 0 
     157        self.colorByClass = True 
     158         
    149159        self.loadSettings() 
    150160 
     
    162172 
    163173        # settings box 
    164         boxSettings = OWGUI.widgetBox(self.controlArea, "Settings") 
     174        boxSettings = OWGUI.widgetBox(self.controlArea, "Settings", addSpace=True) 
    165175        self.cbShowMeta = OWGUI.checkBox(boxSettings, self, "showMeta", 'Show meta attributes', callback = self.cbShowMetaClicked) 
    166176        self.cbShowMeta.setEnabled(False) 
    167177        self.cbShowAttLbls = OWGUI.checkBox(boxSettings, self, "showAttributeLabels", 'Show attribute labels (if any)', callback = self.cbShowAttLabelsClicked) 
    168178        self.cbShowAttLbls.setEnabled(True) 
    169         self.cbShowDistributions = OWGUI.checkBox(boxSettings, self, "showDistributions", 'Visualize continuous values', callback = self.cbShowDistributions) 
    170         colBox = OWGUI.indentedBox(boxSettings, sep=OWGUI.checkButtonOffsetHint(self.cbShowDistributions), orientation = "horizontal") 
    171         OWGUI.widgetLabel(colBox, "Color: ") 
    172         self.colButton = OWGUI.toolButton(colBox, self, callback=self.changeColor, width=20, height=20, debuggingEnabled = 0) 
    173         OWGUI.rubber(colBox) 
     179 
     180        box = OWGUI.widgetBox(self.controlArea, "Colors") 
     181        OWGUI.checkBox(box, self, "showDistributions", 'Visualize continuous values', callback = self.cbShowDistributions) 
     182        OWGUI.checkBox(box, self, "colorByClass", 'Color by class value', callback = self.cbShowDistributions) 
     183        OWGUI.button(box, self, "Set colors", self.setColors, tooltip = "Set the canvas background color and color palette for coloring continuous variables", debuggingEnabled = 0) 
    174184 
    175185        resizeColsBox = OWGUI.widgetBox(boxSettings, 0, "horizontal", 0) 
     
    189199        OWGUI.rubber(self.controlArea) 
    190200 
     201        dlg = self.createColorDialog() 
     202        self.discPalette = dlg.getDiscretePalette("discPalette") 
     203 
    191204        # GUI with tabs 
    192205        self.tabs = OWGUI.tabWidget(self.mainArea) 
     
    198211         
    199212 
    200         self.updateColor() 
    201  
    202     def changeColor(self): 
    203         color = QColorDialog.getColor(self.distColor, self) 
    204         if color.isValid(): 
    205             self.distColorRgb = color.getRgb() 
    206             self.updateColor() 
    207  
    208     def updateColor(self): 
    209         self.distColor = QColor(*self.distColorRgb) 
    210         w = self.colButton.width()-8 
    211         h = self.colButton.height()-8 
    212         pixmap = QPixmap(w, h) 
    213         painter = QPainter() 
    214         painter.begin(pixmap) 
    215         painter.fillRect(0,0,w,h, QBrush(self.distColor)) 
    216         painter.end() 
    217         self.colButton.setIcon(QIcon(pixmap)) 
     213    def createColorDialog(self): 
     214        c = OWColorPalette.ColorPaletteDlg(self, "Color Palette") 
     215        c.createDiscretePalette("discPalette", "Discrete Palette") 
     216        box = c.createBox("otherColors", "Other Colors") 
     217        c.createColorButton(box, "Default", "Default color", QColor(Qt.white)) 
     218        c.setColorSchemas(self.colorSettings, self.selectedSchemaIndex) 
     219        return c 
     220 
     221    def setColors(self): 
     222        dlg = self.createColorDialog() 
     223        if dlg.exec_(): 
     224            self.colorSettings = dlg.getColorSchemas() 
     225            self.selectedSchemaIndex = dlg.selectedSchemaIndex 
     226            self.discPalette = dlg.getDiscretePalette("discPalette") 
     227            self.distColorRgb = dlg.getColor("Default") 
    218228 
    219229    def increaseColWidth(self): 
  • orange/OrangeWidgets/OWGUI.py

    r7867 r7995  
    17181718class TableBarItem(QItemDelegate): 
    17191719    BarRole = OrangeUserRole.next() #Qt.UserRole + 1 
     1720    ColorRole = OrangeUserRole.next() 
    17201721    def __init__(self, widget, table = None, color = QColor(255, 170, 127)): 
    17211722        QItemDelegate.__init__(self, widget) 
     
    17391740                    max, span = self.table.normalizers[col] 
    17401741                    ratio = (max - value) / span 
     1742 
     1743        color = self.color 
     1744        newcolor = index.data(TableBarItem.ColorRole).toString() 
     1745        if newcolor: 
     1746            color = QColor() 
     1747            color.setNamedColor(newcolor) 
     1748 
    17411749        if ratio is not None: 
    1742             painter.fillRect(option.rect.adjusted(1, 1, -option.rect.width() * ratio, -1), self.color) 
     1750            painter.fillRect(option.rect.adjusted(0, 3, -option.rect.width() * ratio, -3), color) 
    17431751        text = index.data(Qt.DisplayRole).toString() 
    17441752 
Note: See TracChangeset for help on using the changeset viewer.