# Path: pyweed/gui/MyNumericTableWidgetItem.py
# class MyNumericTableWidgetItem (CustomTableWidgetItemMixin, QtWidgets.QTableWidgetItem):
#     """
#     Custom QTableWidgetItem that forces numerical sorting
# 
#     http://stackoverflow.com/questions/25533140/sorting-qtablewidget-items-numerically
#     """
# 
#     def __init__(self, value, text):
#         super(MyNumericTableWidgetItem, self).__init__(text)
#         self.setData(QtCore.Qt.UserRole, value)
# 
#     def __lt__(self, other):
#         if (isinstance(other, MyNumericTableWidgetItem)):
#             selfDataValue = float(self.data(QtCore.Qt.UserRole))
#             otherDataValue = float(other.data(QtCore.Qt.UserRole))
#             return selfDataValue < otherDataValue
#         else:
#             return QtWidgets.QTableWidgetItem.__lt__(self, other)


from pyweed.gui.MyNumericTableWidgetItem import MyNumericTableWidgetItem
from PyQt5 import QtWidgets, QtCore
from logging import getLogger

        This is used by the various widget methods to handle arbitrary keyword arguments by
        translating them into Qt setter calls.

        For example, passing `textAlignment=QtCore.Qt.AlignCenter` as a keyword argument will result in
        `widget.setTextAlignment(QtCore.Qt.AlignCenter)`
        """
        # Look for Qt setter for any given prop name
        for prop, value in props.items():
            setter = 'set%s%s' % (prop[:1].capitalize(), prop[1:])
            if hasattr(widget, setter):
                try:
                    getattr(widget, setter)(value)
                except Exception as e:
                    LOGGER.error("Tried and failed to set %s: %s", prop, e)
        return widget

    def stringWidget(self, s, **props):
        """ Create a new item displaying the given string """
        return self.applyProps(QtWidgets.QTableWidgetItem(s), **props)

    def numericWidget(self, i, text=None, **props):
        """
        Create a new item displaying the given numeric value. Pass a string or a string format as `text` to customize
        how the number is actually displayed. The numeric value will still be used for sorting.
        """
        if text is None:
            text = "%s"
        if '%' in text:
            text = text % i

# Path: pyweed/gui/MyNumericTableWidgetItem.py
# class MyNumericTableWidgetItem (CustomTableWidgetItemMixin, QtWidgets.QTableWidgetItem):
#     """
#     Custom QTableWidgetItem that forces numerical sorting
# 
#     http://stackoverflow.com/questions/25533140/sorting-qtablewidget-items-numerically
#     """
# 
#     def __init__(self, value, text):
#         super(MyNumericTableWidgetItem, self).__init__(text)
#         self.setData(QtCore.Qt.UserRole, value)
# 
#     def __lt__(self, other):
#         if (isinstance(other, MyNumericTableWidgetItem)):
#             selfDataValue = float(self.data(QtCore.Qt.UserRole))
#             otherDataValue = float(other.data(QtCore.Qt.UserRole))
#             return selfDataValue < otherDataValue
#         else:
#             return QtWidgets.QTableWidgetItem.__lt__(self, other)


# Path: pyweed/gui/TableItems.py
from pyweed.gui.MyNumericTableWidgetItem import MyNumericTableWidgetItem
from PyQt5 import QtWidgets, QtCore
from logging import getLogger


        This is used by the various widget methods to handle arbitrary keyword arguments by
        translating them into Qt setter calls.

        For example, passing `textAlignment=QtCore.Qt.AlignCenter` as a keyword argument will result in
        `widget.setTextAlignment(QtCore.Qt.AlignCenter)`
        """
        # Look for Qt setter for any given prop name
        for prop, value in props.items():
            setter = 'set%s%s' % (prop[:1].capitalize(), prop[1:])
            if hasattr(widget, setter):
                try:
                    getattr(widget, setter)(value)
                except Exception as e:
                    LOGGER.error("Tried and failed to set %s: %s", prop, e)
        return widget

    def stringWidget(self, s, **props):
        """ Create a new item displaying the given string """
        return self.applyProps(QtWidgets.QTableWidgetItem(s), **props)

    def numericWidget(self, i, text=None, **props):
        """
        Create a new item displaying the given numeric value. Pass a string or a string format as `text` to customize
        how the number is actually displayed. The numeric value will still be used for sorting.
        """
        if text is None:
            text = "%s"
        if '%' in text:
            text = text % i

        return self.applyProps(MyNumericTableWidgetItem(i, text), **props)