# Path: FabLabKasse/UI/GUIHelper.py
# def resize_table_columns(table, widths):
#     """resize Qt table columns by the weight factors specified in widths,
#     using the whole width (excluding scrollbar width)
#     """
#     w = table.width() - table.verticalScrollBar().width() - 5
#     for i, width in enumerate(widths):
#         table.setColumnWidth(i, int(width * w / sum(widths)))


from PyQt4.QtGui import QTableView
from PyQt4 import QtGui, QtCore
from FabLabKasse.UI.GUIHelper import resize_table_columns
import functools
        order_model.setHorizontalHeaderItem(2, QtGui.QStandardItem("Artikel"))
        order_model.setHorizontalHeaderItem(3, QtGui.QStandardItem("Einzelpreis"))
        order_model.setHorizontalHeaderItem(4, QtGui.QStandardItem("Gesamtpreis"))

        # Update Order lines

        for i, line in enumerate(order_lines):
            qty = QtGui.QStandardItem(shoppingBackend.format_qty(line.qty))
            qty.setData(line.order_line_id)
            order_model.setItem(i, 0, qty)

            uos = QtGui.QStandardItem(line.unit)
            order_model.setItem(i, 1, uos)

            name = QtGui.QStandardItem(line.name)
            order_model.setItem(i, 2, name)

            price_unit = QtGui.QStandardItem(shoppingBackend.format_money(line.price_per_unit))
            order_model.setItem(i, 3, price_unit)

            subtotal = QtGui.QStandardItem(shoppingBackend.format_money(line.price_subtotal))
            order_model.setItem(i, 4, subtotal)

        # Set Model
        self.setModel(order_model)
        # Change column width to useful values
        # needs to be delayed so that resize events for the scrollbar happens first, otherwise it reports a scrollbar width of 100px at the very first call
        QtCore.QTimer.singleShot(0, functools.partial(resize_table_columns, self, [4, 6, 20, 5, 5]))
        # TODO the 100ms delay is a workaround that is necessary because the first call often comes too early.
        # this workaround looks not so good, a nicer solution would be good

# Path: FabLabKasse/UI/GUIHelper.py
# def resize_table_columns(table, widths):
#     """resize Qt table columns by the weight factors specified in widths,
#     using the whole width (excluding scrollbar width)
#     """
#     w = table.width() - table.verticalScrollBar().width() - 5
#     for i, width in enumerate(widths):
#         table.setColumnWidth(i, int(width * w / sum(widths)))


# Path: FabLabKasse/UI/CartTableView.py
from PyQt4.QtGui import QTableView
from PyQt4 import QtGui, QtCore
from FabLabKasse.UI.GUIHelper import resize_table_columns
import functools

        order_model.setHorizontalHeaderItem(2, QtGui.QStandardItem("Artikel"))
        order_model.setHorizontalHeaderItem(3, QtGui.QStandardItem("Einzelpreis"))
        order_model.setHorizontalHeaderItem(4, QtGui.QStandardItem("Gesamtpreis"))

        # Update Order lines

        for i, line in enumerate(order_lines):
            qty = QtGui.QStandardItem(shoppingBackend.format_qty(line.qty))
            qty.setData(line.order_line_id)
            order_model.setItem(i, 0, qty)

            uos = QtGui.QStandardItem(line.unit)
            order_model.setItem(i, 1, uos)

            name = QtGui.QStandardItem(line.name)
            order_model.setItem(i, 2, name)

            price_unit = QtGui.QStandardItem(shoppingBackend.format_money(line.price_per_unit))
            order_model.setItem(i, 3, price_unit)

            subtotal = QtGui.QStandardItem(shoppingBackend.format_money(line.price_subtotal))
            order_model.setItem(i, 4, subtotal)

        # Set Model
        self.setModel(order_model)
        # Change column width to useful values
        # needs to be delayed so that resize events for the scrollbar happens first, otherwise it reports a scrollbar width of 100px at the very first call
        QtCore.QTimer.singleShot(0, functools.partial(resize_table_columns, self, [4, 6, 20, 5, 5]))
        # TODO the 100ms delay is a workaround that is necessary because the first call often comes too early.
        # this workaround looks not so good, a nicer solution would be good

        QtCore.QTimer.singleShot(100, functools.partial(resize_table_columns, self, [4, 6, 20, 5, 5]))