presik_pos/app/commons/menu_buttons.py

285 lines
9.4 KiB
Python
Raw Normal View History

2020-06-12 04:36:27 +02:00
import os
from pathlib import Path
2020-12-27 20:06:21 +01:00
from PyQt5.QtCore import Qt, pyqtSignal, QSize, QRect
2020-12-28 01:56:10 +01:00
from PyQt5.QtGui import QIcon
2020-12-28 00:19:15 +01:00
from PyQt5.QtWidgets import (
QWidget, QHBoxLayout, QScroller, QVBoxLayout, QPushButton, QGridLayout,
2021-01-11 03:24:26 +01:00
QScrollArea
2020-12-28 00:19:15 +01:00
)
2020-06-12 04:36:27 +02:00
from .custom_button import CustomButton
pkg_dir = str(Path(os.path.dirname(__file__)).parents[0])
file_back_icon = os.path.join(pkg_dir, 'share', 'back.svg')
2020-12-28 00:19:15 +01:00
file_close_icon = os.path.join(pkg_dir, 'share', 'close.svg')
2020-06-12 04:36:27 +02:00
file_menu_img = os.path.join(pkg_dir, 'share', 'menu.png')
__all__ = ['GridButtons', 'MenuDash']
def money(v):
return '${:9,}'.format(int(v))
2020-12-29 03:13:18 +01:00
class MenuDash(QWidget):
2020-06-12 04:36:27 +02:00
def __init__(self, parent, values, selected_method=None, title=None):
"""
parent: parent window
values: is to list of list/tuples values for data model
[('a' 'b', 'c'), ('d', 'e', 'f')...]
on_selected: method to call when triggered the selection
title: title of window
"""
super(MenuDash, self).__init__()
2020-12-29 04:20:33 +01:00
self.setContentsMargins(0, 0, 0, 0)
self.setObjectName('dash_menu')
2020-06-12 04:36:27 +02:00
self.parent = parent
self.values = values
2020-12-28 22:08:45 +01:00
self.current_view = '0'
self._view = 0
2020-12-29 03:13:18 +01:00
self.main = QVBoxLayout()
2020-12-29 04:20:33 +01:00
self.main.setSpacing(0)
2020-12-29 03:13:18 +01:00
self.setLayout(self.main)
2020-12-28 22:08:45 +01:00
self.ctx_widgets = {}
2020-06-12 04:36:27 +02:00
self.button_size = parent.screen_size
self.method_on_selected = getattr(self.parent, selected_method)
self.create_categories()
2020-12-29 03:13:18 +01:00
self.expand = False
2020-12-27 20:06:21 +01:00
self.push_menu = QPushButton('MENU')
self.push_menu.setObjectName('button_push_menu')
self.push_menu.clicked.connect(self.expand_menu)
2020-06-12 04:36:27 +02:00
2020-12-28 00:19:15 +01:00
self.close_menu = QPushButton()
self.close_menu.setObjectName('button_close_menu')
self.close_menu.setIcon(QIcon(file_close_icon))
2021-01-28 02:20:29 +01:00
self.close_menu.setIconSize(QSize(30, 30))
2020-12-28 00:19:15 +01:00
self.close_menu.clicked.connect(self.stretch_menu)
self.close_menu.hide()
2020-06-12 04:36:27 +02:00
widget_head = QWidget()
2021-01-28 02:20:29 +01:00
widget_head.setContentsMargins(0, 0, 0, 0)
widget_head.setStyleSheet("background-color: white; max-height: 70px")
2020-06-12 04:36:27 +02:00
self.layout_head = QHBoxLayout()
widget_head.setLayout(self.layout_head)
2020-12-29 03:13:18 +01:00
self.main.addWidget(widget_head, 0)
2020-06-12 04:36:27 +02:00
self.pushButtonBack = QPushButton()
2021-01-28 02:20:29 +01:00
self.pushButtonBack.setObjectName('button_back_menu')
2020-06-12 04:36:27 +02:00
self.pushButtonBack.setIcon(QIcon(file_back_icon))
2021-01-28 02:20:29 +01:00
self.pushButtonBack.setIconSize(QSize(30, 30))
2020-06-12 04:36:27 +02:00
2020-12-28 00:19:15 +01:00
self.layout_head.addWidget(self.pushButtonBack, stretch=0)
self.layout_head.addWidget(self.push_menu, stretch=1)
self.layout_head.addWidget(self.close_menu, stretch=0)
2020-12-29 03:13:18 +01:00
self.main.addWidget(self.menu_area, 0)
2020-06-12 04:36:27 +02:00
self.pushButtonBack.clicked.connect(self.action_back)
2020-12-28 00:19:15 +01:00
width = self.parent.screen_width
2020-12-29 03:13:18 +01:00
start_width = int(width / 3)
last_width = width - start_width - 10
self.rect_expanded = QRect(start_width, 0, last_width, self.parent.screen_height)
2020-12-28 00:19:15 +01:00
2020-12-29 03:13:18 +01:00
def paintEvent(self, event):
super(MenuDash, self).paintEvent(event)
if self.expand:
self.expand_menu()
2020-12-28 22:08:45 +01:00
2020-12-28 23:39:10 +01:00
def get_products(self, values, name=None):
2020-12-28 22:08:45 +01:00
grid_buttons = GridButtons(
2020-12-28 01:56:10 +01:00
self.parent,
2020-12-28 22:08:45 +01:00
values,
2020-12-28 01:56:10 +01:00
num_cols=4,
action=self.method_on_selected
)
2020-12-28 22:08:45 +01:00
scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True)
scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
scroll_area.setWidget(grid_buttons)
QScroller.grabGesture(scroll_area, QScroller.LeftMouseButtonGesture)
return scroll_area
2020-12-28 01:56:10 +01:00
2020-12-29 22:58:21 +01:00
def change_view(self, view_id):
view_id = str(view_id)
view = self.ctx_widgets[self.current_view]['items']
view.hide()
if not view_id:
view_id = '0'
new_view = self.ctx_widgets[view_id]['items']
if not new_view:
return
new_view.show()
self.main.addWidget(new_view)
self.current_view = view_id
2022-05-03 00:25:05 +02:00
self.setGeometry(self.rect_expanded)
2020-12-29 22:58:21 +01:00
2020-06-12 04:36:27 +02:00
def create_categories(self):
# set the list model
2020-12-28 22:08:45 +01:00
self.menu_area = QScrollArea()
self.menu_area.setWidgetResizable(True)
QScroller.grabGesture(self.menu_area, QScroller.LeftMouseButtonGesture)
2020-12-29 03:13:18 +01:00
self.menu = QWidget()
self.menu_area.setWidget(self.menu)
2020-12-28 22:08:45 +01:00
self.main_categories = QGridLayout()
self.main_categories.setSpacing(4)
2020-12-29 03:13:18 +01:00
self.menu.setLayout(self.main_categories)
2020-12-28 22:08:45 +01:00
2021-01-28 02:20:29 +01:00
num_cat_cols = 3
2020-12-29 03:13:18 +01:00
row_cat = 0
col_cat = 0
2020-12-28 22:08:45 +01:00
parent_id = '0'
self.ctx_widgets = {
'0': {
'items': self.menu_area,
'parent': None,
},
}
2020-06-12 04:36:27 +02:00
for value in self.values:
2020-12-28 22:08:45 +01:00
cat_id = str(value['id'])
2020-06-12 04:36:27 +02:00
if not value:
continue
2020-12-29 03:13:18 +01:00
if col_cat > num_cat_cols - 1:
col_cat = 0
row_cat += 1
2020-06-12 04:36:27 +02:00
button = CustomButton(
parent=self,
2020-12-29 22:58:21 +01:00
id=value['id'],
2020-06-12 04:36:27 +02:00
icon=value['icon'],
method='selected_method',
2020-12-29 22:58:21 +01:00
desc_extractor='name',
record=value,
2020-06-12 04:36:27 +02:00
size=self.button_size,
2020-12-31 17:02:27 +01:00
name_style='toolbar'
2020-06-12 04:36:27 +02:00
)
2020-12-29 03:13:18 +01:00
self.main_categories.addWidget(button, row_cat, col_cat)
col_cat += 1
2020-12-28 22:08:45 +01:00
items = value.get('items')
2020-12-28 23:39:10 +01:00
childs = value.get('childs', [])
2020-12-28 22:08:45 +01:00
if items:
2020-12-28 23:39:10 +01:00
products = self.get_products(items)
2020-12-28 22:08:45 +01:00
self.ctx_widgets[cat_id] = {
'items': products,
'parent': parent_id,
}
2020-12-29 00:17:37 +01:00
if childs:
subrow = 0
subcol = 0
sub_categories = QGridLayout()
sub_categories.setSpacing(4)
scroll_subarea = QScrollArea()
scroll_subarea.setWidgetResizable(True)
scroll_subarea.setLayout(sub_categories)
self.ctx_widgets[cat_id] = {
'items': scroll_subarea,
'parent': parent_id,
}
for subcat in childs:
sub_id = str(subcat['id'])
if subcol > num_cat_cols - 1:
subcol = 0
subrow += 1
button = CustomButton(
parent=self,
2020-12-29 22:58:21 +01:00
id=sub_id,
2020-12-29 04:00:24 +01:00
icon=value['icon'],
2020-12-29 00:17:37 +01:00
method='selected_method',
2020-12-29 22:58:21 +01:00
desc_extractor='name',
record=subcat,
2020-12-29 00:17:37 +01:00
size=self.button_size,
name_style='category_button'
)
2020-12-29 03:13:18 +01:00
sub_categories.addWidget(button, subrow, subcol)
2020-12-29 00:17:37 +01:00
subcol += 1
products_list = subcat.get('items')
2020-12-29 03:13:18 +01:00
products_items = None
2020-12-29 00:17:37 +01:00
if products_list:
products_items = self.get_products(products_list)
2020-12-29 03:13:18 +01:00
self.ctx_widgets[sub_id] = {
'items': products_items,
'parent': cat_id,
}
sub_categories.setRowStretch(subrow + 1, 1)
self.main_categories.setRowStretch(row_cat + 1, 1)
2020-06-12 04:36:27 +02:00
def action_back(self):
2020-12-28 22:08:45 +01:00
parent_id = self.ctx_widgets[self.current_view]['parent']
2020-12-29 03:13:18 +01:00
if parent_id and parent_id != self.current_view:
self.change_view(parent_id)
2020-06-12 04:36:27 +02:00
def selected_method(self, args):
2020-12-29 22:58:21 +01:00
self.change_view(args['id'])
2020-12-29 03:13:18 +01:00
self.expand = True
2020-12-27 20:06:21 +01:00
def setDisabled(self, bool):
2020-12-28 22:08:45 +01:00
self.menu_area.setDisabled(bool)
2020-12-27 20:06:21 +01:00
2020-12-28 00:19:15 +01:00
def stretch_menu(self):
2020-12-29 03:13:18 +01:00
self.expand = False
self.change_view('0')
2020-12-28 00:19:15 +01:00
self.close_menu.hide()
2020-12-29 03:13:18 +01:00
self.parent.show_right_panel(True)
2020-12-28 00:19:15 +01:00
2020-12-27 20:06:21 +01:00
def expand_menu(self):
2020-12-29 03:13:18 +01:00
self.expand = True
2020-12-28 00:19:15 +01:00
self.parent.show_right_panel(False)
self.close_menu.show()
2022-05-03 00:25:05 +02:00
self.setGeometry(self.rect_expanded)
2020-12-29 04:20:33 +01:00
class GridButtons(QWidget):
sigItem_selected = pyqtSignal(str)
2021-02-01 23:33:35 +01:00
def __init__(self, parent, values, num_cols, action, style='product_button'):
2020-12-29 04:20:33 +01:00
"""
2021-02-01 23:33:35 +01:00
values: a list of lists
2020-12-29 04:20:33 +01:00
num_cols: number of columns?
"""
QWidget.__init__(self)
self.parent = parent
self.layout = QGridLayout()
self.setLayout(self.layout)
self.button_size = parent.screen_size
2021-02-01 23:33:35 +01:00
self.values = values
2020-12-29 04:20:33 +01:00
self.action = action
self.num_cols = num_cols
self.style = style
self.create_list_items()
self.layout.setSpacing(2)
self.set_items()
def create_list_items(self):
self.list_items = []
2021-02-01 23:33:35 +01:00
for value in self.values:
_button = CustomButton(
2020-12-29 04:20:33 +01:00
parent=self,
2021-02-01 23:33:35 +01:00
id=value['id'],
2020-12-29 22:58:21 +01:00
desc_extractor='name',
2020-12-29 04:20:33 +01:00
method='action_selected',
2021-02-01 23:33:35 +01:00
record=value,
2020-12-29 04:20:33 +01:00
size=self.button_size,
name_style=self.style,
)
2021-02-01 23:33:35 +01:00
self.list_items.append(_button)
2020-12-29 04:20:33 +01:00
def action_selected(self, idx):
self.action(idx)
def set_items(self):
colx = 0
rowy = 0
for item_button in self.list_items:
if colx >= self.num_cols:
colx = 0
rowy += 1
self.layout.addWidget(item_button, rowy, colx)
colx += 1
self.layout.setRowStretch(rowy + 1, 1)