def __init__(self):

        super(HypothesesTableModel, self).__init__()

        self._resistor_decoder = ResistorDecoder()
        self._hypotheses = []
        self._resistor_pixmaps = {}
class HypothesesTableModel(QtCore.QAbstractTableModel):

    """This class implements a table model for the hypotheses."""

    COLUMNS = ('value',
               'series',
               'tolerance',
               'temperature coefficient',
               'value range',
               'colour code',
               )

    HEADER_DATA = ('Value [R]',
                   'Series',
                   'Tolerance [%]',
                   'Temperature Coefficient [ppm/K]',
                   'Value Range [R]',
                   'Colour Code',
                   )

    ###############################################

    @classmethod
    def column_index(cls, column):

        """Return the index of the given column name."""

        return cls.COLUMNS.index(column)

    ###############################################

    def __init__(self):

        super(HypothesesTableModel, self).__init__()

        self._resistor_decoder = ResistorDecoder()
        self._hypotheses = []
        self._resistor_pixmaps = {}

    ###############################################

    @pyqtSlot(list)
    def update_hypotheses(self, bands):

        """Slot to update the hypotheses from the given list of bands."""

        colour_names = [x for x in bands if x != 'none']
        if len(colour_names) >= 3:
            self._hypotheses = self._resistor_decoder.decode(colour_names)
            self._resistor_pixmaps = {id(resistor):ResistorPixmap(resistor)
                                      for resistor in self._hypotheses}
            self._sort_by_series()
            # print '\n', colour_names
            # for i, hypothesis in enumerate(self._hypotheses):
            #     print "hypothese %u: %s" % (i +1, hypothesis)
        else:
            self._hypotheses = ()
            self._resistor_pixmaps = {}
        self.modelReset.emit()

    ###############################################

    def rowCount(self, index=QtCore.QModelIndex()):

        """Return the number of rows."""

        return len(self._hypotheses)

    ###############################################

    def columnCount(self, index=QtCore.QModelIndex()):

        """Return the number of columns."""

        return len(self.COLUMNS)

    ###############################################

    def headerData(self, section, orientation, role=Qt.DisplayRole):

        """Return the header data."""

        if role == Qt.TextAlignmentRole:
            return QtCore.QVariant(int(Qt.AlignHCenter|Qt.AlignVCenter))

        elif role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return QtCore.QVariant(self.HEADER_DATA[section])
            elif orientation == Qt.Vertical:
                return QtCore.QVariant(int(section + 1))

        else:
            return QtCore.QVariant()

    ###############################################

    def data(self, index, role=Qt.DisplayRole):

        """Return the cell data."""

        if not index.isValid() or not(0 <= index.row() < len(self._hypotheses)):
            return QtCore.QVariant()

        if role == Qt.TextAlignmentRole:
            return QtCore.QVariant(int(Qt.AlignRight|Qt.AlignVCenter))
        elif role == Qt.DisplayRole:
            return self._display_role_data(index)
        else:
            return QtCore.QVariant ()

    ###############################################

    def _display_role_data(self, index):

        row = index.row()
        resistor = self._hypotheses[row]
        column = self.COLUMNS[index.column()]

        if column == 'value':
            return QtCore.QVariant(format_value(resistor.value))

        elif column == 'series':
            return QtCore.QVariant(str(resistor.series))

        elif column == 'tolerance':
            if resistor.tolerance is not None:
                tolerance_str = '%.2f %%' % resistor.tolerance
            else:
                tolerance_str = ''
            return QtCore.QVariant(tolerance_str)

        elif column == 'temperature coefficient':
            if resistor.temperature_coefficient is not None:
                temperature_coefficient_str = '%u ppm' % resistor.temperature_coefficient
            else:
                temperature_coefficient_str = ''
            return QtCore.QVariant(temperature_coefficient_str)

        elif column == 'value range':
            value_range = resistor.value_range()
            if value_range is not None:
                value_range_str = [format_value(x) for x in value_range]
                return QtCore.QVariant('[%s, %s]' % tuple(value_range_str))
            else:
                return QtCore.QVariant ()

        elif column == 'colour code':
            resistor_pixmap = self._resistor_pixmaps[id(resistor)]
            return QtCore.QVariant(resistor_pixmap)

    ###############################################

    def _sort(self, key_function, reverse):

        """Sort the model with the given sort function and order."""

        self._hypotheses = sorted(self._hypotheses, key=key_function, reverse=reverse)

    ###############################################

    def _sort_by_values(self, reverse=False):

        """Sort the resistors by values."""

        self._sort(lambda resistor: resistor.value, reverse)

    ###############################################

    def _sort_by_series(self, reverse=False):

        """Sort the resistors by series."""

        self._sort(lambda resistor: resistor.series, reverse)

    ###############################################

    def sort(self, column, order=Qt.AscendingOrder):

        """Sort the model."""

        reverse = order != Qt.AscendingOrder
        column = self.COLUMNS[column]
        if column == 'value':
            self._sort_by_values(reverse)
        elif column == 'series':
            self._sort_by_series(reverse)
        self.reset()