presik_pos/app/commons/menu_buttons.py

257 lines
8.3 KiB
Python
Raw Normal View History

2020-06-12 04:36:27 +02:00
import os
from pathlib import Path
from decimal import Decimal
2020-12-27 20:06:21 +01:00
from PyQt5.QtCore import Qt, pyqtSignal, QSize, QRect
2020-12-28 00:19:15 +01:00
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtWidgets import (
QWidget, QHBoxLayout, QScroller, QVBoxLayout, QPushButton, QGridLayout,
QScrollArea
)
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))
class GridButtons(QWidget):
sigItem_selected = pyqtSignal(str)
2020-12-06 05:09:41 +01:00
def __init__(self, parent, rows, num_cols, action, style='product_button'):
2020-06-12 04:36:27 +02:00
"""
rows: a list of lists
num_cols: number of columns?
"""
QWidget.__init__(self)
self.parent = parent
self.layout = QGridLayout()
self.setLayout(self.layout)
self.button_size = parent.screen_size
self.rows = rows
self.action = action
self.num_cols = num_cols
2020-12-06 05:09:41 +01:00
self.style = style
2020-12-28 00:19:15 +01:00
self.create_list_items()
2020-12-06 05:09:41 +01:00
self.layout.setSpacing(2)
2020-12-28 00:19:15 +01:00
self.set_items()
2020-06-12 04:36:27 +02:00
2020-12-28 00:19:15 +01:00
def create_list_items(self):
self.list_items = []
for row in self.rows:
2020-06-12 04:36:27 +02:00
if isinstance(row[3], Decimal):
row[3] = money(int(row[3]))
item_button = CustomButton(
parent=self,
id=row[0],
title=row[2],
desc=str(row[3]),
method='action_selected',
target=row[0],
size=self.button_size,
2020-12-06 05:09:41 +01:00
name_style=self.style,
2020-06-12 04:36:27 +02:00
)
2020-12-28 00:19:15 +01:00
self.list_items.append(item_button)
def action_selected(self, idx):
print('action_selected', idx)
self.action(idx)
def change_columns(self, value):
self.num_cols = value
print('Yeahhhh ....')
self.set_items()
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, 0)
self.layout.setRowStretch(rowy + 1, 1)
def set_items_exp(self):
colx = 0
rowy = 0
for item_button in self.list_items:
if colx >= self.num_cols:
colx = 0
rowy += 1
2020-06-12 04:36:27 +02:00
self.layout.addWidget(item_button, rowy, colx)
colx += 1
2020-12-06 05:09:41 +01:00
self.layout.setRowStretch(rowy + 1, 0)
2020-06-12 04:36:27 +02:00
self.layout.setRowStretch(rowy + 1, 1)
class MenuDash(QVBoxLayout):
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__()
self.parent = parent
self.values = values
self.current_view = None
self.button_size = parent.screen_size
self.method_on_selected = getattr(self.parent, selected_method)
self.create_categories()
2020-12-28 00:19:15 +01:00
# pixmap = QPixmap(file_menu_img)
# new_pixmap = pixmap.scaled(180, 55)
# push_menu.setPixmap(new_pixmap)
self.is_expanded = 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))
self.close_menu.setIconSize(QSize(40, 40))
self.close_menu.clicked.connect(self.stretch_menu)
self.close_menu.hide()
2020-06-12 04:36:27 +02:00
widget_head = QWidget()
widget_head.setStyleSheet("background-color: white;")
self.layout_head = QHBoxLayout()
widget_head.setLayout(self.layout_head)
self.addWidget(widget_head, 0)
self.pushButtonBack = QPushButton()
self.pushButtonBack.setIcon(QIcon(file_back_icon))
2020-12-28 00:19:15 +01:00
self.pushButtonBack.setIconSize(QSize(15, 15))
self.pushButtonBack.setMaximumWidth(15)
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-06-12 04:36:27 +02:00
self.addWidget(self.category_area, 0)
self.pushButtonBack.clicked.connect(self.action_back)
2020-12-28 00:19:15 +01:00
width = self.parent.screen_width
self.start_width = int(width / 3)
self.last_width = width - self.start_width - 10
2020-06-12 04:36:27 +02:00
def setState(self, args):
2020-12-28 00:19:15 +01:00
if args.get('category_invisible'):
2020-06-12 04:36:27 +02:00
self.category_area.hide()
else:
if self.current_view:
self.current_view.hide()
2020-12-28 00:19:15 +01:00
self.category_area.show()
2020-06-12 04:36:27 +02:00
if args.get('button'):
view_id = args.get('button')
if hasattr(self, 'view_' + str(view_id)):
self.current_view = getattr(self, 'view_' + str(view_id))
self.addWidget(self.current_view)
self.current_view.show()
2020-12-28 00:19:15 +01:00
self.expand_menu()
2020-06-12 04:36:27 +02:00
def create_categories(self):
# set the list model
self.category_area = QScrollArea()
self.category_area.setWidgetResizable(True)
self.category_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
QScroller.grabGesture(self.category_area, QScroller.LeftMouseButtonGesture)
2020-12-27 20:06:21 +01:00
self.category = QWidget()
self.category_area.setWidget(self.category)
2020-06-12 04:36:27 +02:00
self.layout_category = QGridLayout()
2020-12-06 05:09:41 +01:00
self.layout_category.setSpacing(4)
2020-12-27 20:06:21 +01:00
self.category.setLayout(self.layout_category)
2020-06-12 04:36:27 +02:00
id_ = 1
2020-12-28 00:19:15 +01:00
num_cat_cols = 2
2020-06-12 04:36:27 +02:00
row = 0
col = 0
for value in self.values:
if not value:
continue
2020-12-28 00:19:15 +01:00
if col > num_cat_cols - 1:
2020-06-12 04:36:27 +02:00
col = 0
row += 1
name_button = 'button_' + str(id_)
button = CustomButton(
parent=self,
id=name_button,
icon=value['icon'],
desc=value['name'],
method='selected_method',
target=str(id_),
size=self.button_size,
name_style='category_button'
)
2020-12-06 05:09:41 +01:00
2020-06-12 04:36:27 +02:00
self.layout_category.addWidget(button, row, col)
2020-09-03 06:00:57 +02:00
if value.get('items'):
2020-12-28 00:19:15 +01:00
self.grid_buttons = GridButtons(
self.parent,
value['items'],
num_cols=4,
action=self.method_on_selected
)
2020-12-27 20:06:21 +01:00
self.scroll_area = QScrollArea()
self.scroll_area.setWidgetResizable(True)
self.scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll_area.setWidget(self.grid_buttons)
QScroller.grabGesture(self.scroll_area, QScroller.LeftMouseButtonGesture)
setattr(self, 'view_' + str(id_), self.scroll_area)
2020-06-12 04:36:27 +02:00
col += 1
id_ += 1
def action_back(self):
2020-12-28 00:19:15 +01:00
print('si action back...')
2020-06-12 04:36:27 +02:00
self.setState({
2020-12-28 00:19:15 +01:00
'category_invisible': False
2020-06-12 04:36:27 +02:00
})
2020-12-28 00:19:15 +01:00
self.expand_menu()
if self.is_expanded:
print("expandido..........")
# self.close_menu.show()
2020-06-12 04:36:27 +02:00
def selected_method(self, args):
self.setState({
2020-12-28 00:19:15 +01:00
'category_invisible': True,
2020-06-12 04:36:27 +02:00
'button': args,
})
2020-12-28 00:19:15 +01:00
self.expand_menu()
2020-12-27 20:06:21 +01:00
def setDisabled(self, bool):
self.category_area.setDisabled(bool)
self.grid_buttons.setDisabled(bool)
self.scroll_area.setDisabled(bool)
2020-12-28 00:19:15 +01:00
def stretch_menu(self):
self.parent.show_right_panel(True)
self.setState({'category_invisible': False})
self.close_menu.hide()
2020-12-27 20:06:21 +01:00
def expand_menu(self):
2020-12-28 00:19:15 +01:00
self.parent.show_right_panel(False)
rect = QRect(self.start_width, 0, self.last_width, self.parent.screen_height)
2020-12-27 20:06:21 +01:00
self.setGeometry(rect)
2020-12-28 00:19:15 +01:00
self.close_menu.show()
self.is_expanded = True