111 lines
4.0 KiB
Python
111 lines
4.0 KiB
Python
__author__ = 'tanshu'
|
|
from PyQt5 import uic, QtCore, QtWidgets
|
|
|
|
|
|
file = '/home/tanshu/Programming/summer/summer/QtDesignerFiles/ListForm.ui'
|
|
base, form = uic.loadUiType(file)
|
|
|
|
|
|
class ProductList(base, form):
|
|
def __init__(self, router, products, parent=None):
|
|
super(ProductList, self).__init__()
|
|
self.setupUi(self)
|
|
self.translateUi()
|
|
self.router = router
|
|
self.products = products
|
|
self.model = ProductsTableModel(products)
|
|
self.tblItems.setModel(self.model)
|
|
self.tblItems.doubleClicked.connect(self.show_product)
|
|
self.btnAddNew.clicked.connect(self.add_new_product)
|
|
self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
|
|
self.tblItems.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
|
|
|
|
def translateUi(self):
|
|
self.setObjectName("ProductList")
|
|
_translate = QtCore.QCoreApplication.translate
|
|
self.setWindowTitle(_translate("ListForm", "Products"))
|
|
self.btnAddNew.setText(_translate("ListForm", "Add Product"))
|
|
|
|
def update_data(self, data):
|
|
self.products = data
|
|
self.model = ProductsTableModel(self.products)
|
|
self.tblItems.setModel(self.model)
|
|
|
|
def show_product(self, index):
|
|
product = self.model.products[index.row()]
|
|
self.router.show_product(product['ProductID'])
|
|
|
|
def add_new_product(self):
|
|
self.router.show_product(None)
|
|
|
|
|
|
class ProductsTableModel(QtCore.QAbstractTableModel):
|
|
def __init__(self, products=None, parent=None):
|
|
QtCore.QAbstractTableModel.__init__(self, parent)
|
|
self.products = products if products is not None else []
|
|
|
|
def rowCount(self, parent):
|
|
return len(self.products)
|
|
|
|
def columnCount(self, parent):
|
|
return 12
|
|
|
|
def data(self, index, role):
|
|
if role == QtCore.Qt.DisplayRole:
|
|
item = self.products[index.row()]
|
|
column = index.column()
|
|
if column == 0:
|
|
return item['Name']
|
|
elif column == 1:
|
|
return item['Units']
|
|
elif column == 2:
|
|
return item['ProductGroup']['Name']
|
|
elif column == 3:
|
|
return '{0:,.2f}'.format(item['Price'])
|
|
elif column == 4:
|
|
return item['HasHappyHour']
|
|
elif column == 5:
|
|
return item['ServiceTax']['Name'] + ' ' + '({0:,.2f}%)'.format(item['ServiceTax']['Rate'] * 100)
|
|
elif column == 6:
|
|
return item['Vat']['Name'] + ' ' + '({0:,.2f}%)'.format(item['Vat']['Rate'] * 100)
|
|
elif column == 7:
|
|
return '{0:,.2f}%'.format(item['ServiceCharge'] * 100)
|
|
elif column == 8:
|
|
return item['ScTaxable']
|
|
elif column == 9:
|
|
return item['IsActive']
|
|
elif column == 10:
|
|
return item['IsFixture']
|
|
elif column == 11:
|
|
return item['SortOrder']
|
|
|
|
def headerData(self, section, orientation, role):
|
|
if role == QtCore.Qt.DisplayRole:
|
|
if orientation == QtCore.Qt.Horizontal:
|
|
if section == 0:
|
|
return 'Name'
|
|
elif section == 1:
|
|
return 'Units'
|
|
elif section == 2:
|
|
return 'Product Group'
|
|
elif section == 3:
|
|
return 'Price'
|
|
elif section == 4:
|
|
return 'Has Happy Hour'
|
|
elif section == 5:
|
|
return 'Service Tax'
|
|
elif section == 6:
|
|
return 'Vat'
|
|
elif section == 7:
|
|
return 'Service Charge'
|
|
elif section == 8:
|
|
return 'Is Sc Taxable?'
|
|
elif section == 9:
|
|
return 'Is Active?'
|
|
elif section == 10:
|
|
return 'Is Fixture?'
|
|
elif section == 11:
|
|
return 'Sort Order'
|
|
else:
|
|
return section + 1
|