Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development Progress - week 06 #56

Merged
merged 8 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions pyqt5-gui/favourites.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/usr/bin/python3
import math
import sys
import inspect
from collections import deque

# Import PyQt5 Engine
from PyQt5.QtWidgets import (QApplication,
QWidget,
QPushButton,
QGridLayout,
QLabel,
QScrollArea,
QFrame,
QGroupBox,
QLineEdit,
QMessageBox,
QPlainTextEdit,
QHBoxLayout,
QVBoxLayout,
QGraphicsDropShadowEffect,
QGraphicsOpacityEffect,
)

from PyQt5.QtGui import *
from PyQt5.QtCore import *

sys.path.append(r'.')
sys.path.append(r'..')
from collections import deque
from db_handle import postgres_conn


def favourites_menu():
favorites_scroll = QScrollArea()
favorites_widget = QWidget()
products_grid_layout = QVBoxLayout()

"""ADD CUSTOM FONT TO ARRAY READY TO BE LOADED TO ANY TEXT OBJECT"""
font = QFontDatabase.addApplicationFont(r'../fonts/jetbrains-mono.regular.ttf')
if font < 0:
print('Error loading fonts!')
fonts = QFontDatabase.applicationFontFamilies(font)

postgres_conn.admin_client()

"""INNER JOIN QUERY THAT WILL GIVE US EVERY PRODUCT'S DATA"""
postgres_conn.POSTGRES_CURSOR.execute(f"select products.product_id, favourite_products.username, "
f"products.product_name, products.single_price, products.quantity, "
f"products.product_description, products.subcategory"
f" from products inner join favourite_products on "
f"favourite_products.product_id=products.product_id "
f"where favourite_products.username = 'pesho';")
result = deque(postgres_conn.POSTGRES_CURSOR.fetchall())
print(result)

res_len = math.ceil(len(result) / 3)
for row in range(res_len):
horizontal_products_layout = QHBoxLayout()
for col in range(3):
if result:
current_product = result.popleft()
product_id, username, product_name, price, quantity, product_description, subcategory = current_product

current_vertical_layout = QVBoxLayout()

product_image = QLabel()
product_image.setFixedSize(250, 200)
product_image.setPixmap(QPixmap(f"../img/products/{subcategory}/{product_name}.png"))
product_image.setScaledContents(True)

current_title = QLabel()
current_title.setText('Test Title')
current_title.setFont(QFont(fonts[0], 12))

current_sku = QLabel()
current_sku.setText(product_id)
current_sku.setFont(QFont(fonts[0], 10))

current_description = QLabel(product_description)
current_description.setWordWrap(True)
current_description.setFont(QFont(fonts[0], 9))

current_buttons_layout = QHBoxLayout()
current_buttons_layout.setAlignment(Qt.AlignLeft)

current_favorites_button = QPushButton()
current_favorites_button.setFixedWidth(35)
current_favorites_button.setFixedHeight(35)
current_favorites_button.setIcon(QIcon(r'../img/favorite.png'))
current_favorites_button.setIconSize(QSize(30, 30))
current_favorites_button.setFont(QFont(fonts[0], 12))

current_basket_button = QPushButton()
current_basket_button.setFixedWidth(35)
current_basket_button.setFixedHeight(35)
current_basket_button.setIcon(QIcon(r'../img/shoppingcart.png'))
current_basket_button.setIconSize(QSize(30, 30))
current_basket_button.setFont(QFont(fonts[0], 12))

current_buttons_layout.addWidget(current_favorites_button)
current_buttons_layout.addWidget(current_basket_button)

current_vertical_layout.insertWidget(0, product_image)
current_vertical_layout.addWidget(current_title)
current_vertical_layout.addWidget(current_sku)
current_vertical_layout.addWidget(current_description)
current_vertical_layout.addLayout(current_buttons_layout)

current_vertical_layout.addStretch()
current_vertical_layout.addSpacing(10)

horizontal_products_layout.addLayout(current_vertical_layout)

products_grid_layout.addLayout(horizontal_products_layout)

favorites_widget.setLayout(products_grid_layout)

favorites_scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
favorites_scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
favorites_scroll.setWidgetResizable(True)
favorites_scroll.setWidget(favorites_widget)

return favorites_scroll
69 changes: 42 additions & 27 deletions pyqt5-gui/main_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
sys.path.append(r'..')
from collections import deque
from db_handle import postgres_conn
import about, subcategories, edit_account, payment_options, products
import about, subcategories, edit_account, payment_options, products, favourites

# This global variable should be modified to accept it's value dynamically, based on the cattegory button clicked
global subcategory_name
subcategory_name = ''
layouts_list = []

"""INIT CONNECTION TO THE DATABASE"""
postgres_conn.admin_client()
Expand All @@ -43,7 +44,7 @@ def __init__(self):
self.setMaximumHeight(700)



"""OPEN THE PROPER SUBCATEGORY"""
def open_func(curr_subcat_name):
global subcategory_name
Expand All @@ -53,6 +54,9 @@ def open_func(curr_subcat_name):
def open_category_func(subcat_name):
return lambda: open_func(subcat_name)

def open_favourites_func():
return lambda: open_favourites()

"""LEFT LAYOUT BUTTONS CALL FUNCTION"""
left_layout_buttons_dict = {
'edit_account': lambda: open_update_account(),
Expand Down Expand Up @@ -113,7 +117,6 @@ def open_category_func(subcat_name):
button.clicked.connect(left_layout_buttons_dict[button_function])
buttons.appendleft(button)


left_buttons_layout.addWidget(button)

left_buttons_layout.addStretch(0)
Expand All @@ -129,6 +132,7 @@ def open_category_func(subcat_name):
favourites_button.setFont(QFont(fonts[0], 9))
favourites_button.setFixedWidth(120)
favourites_button.setFixedHeight(30)
favourites_button.clicked.connect(open_favourites_func())

about_button = QPushButton()
about_button.setText("About")
Expand Down Expand Up @@ -228,37 +232,17 @@ def open_category_func(subcat_name):
self.setLayout(main_layout)
self.show()

"""BRING BACK THE CATEGORIES"""
def open_categories():
# Not the best approach, that could be improved
try:
edit_account_layout.hide()
except Exception as error:
print("Edit Account Not Opened")
try:
payment_options_layout.hide()
except Exception as error:
print("Payment Options Not Opened")

try:
products.products_groupbox.hide()
subcategories.subcategories_groupbox.hide()
except Exception as error:
print("Product Not Opened")

categories_groupbox.show()
buttons[-1].setEnabled(True)
buttons[-2].setEnabled(True)
buttons[-3].setEnabled(True)
buttons[-4].setEnabled(True)

"""OPEN EDIT ACCOUNT LAYOUT/REPLACE CATEGORIES LAYOUT"""
def open_update_account():
global edit_account_layout
global layouts_list
edit_account_layout = edit_account.open_edit_account()
layouts_list.append(edit_account_layout)
categories_groupbox.hide()
try:
payment_options_layout.hide()
subcategories_layout.hide()
except Exception as error:
print("Payment Options Not Opened")
main_layout.addWidget(edit_account_layout, 1, 1)
Expand All @@ -268,21 +252,26 @@ def open_update_account():
buttons[-2].setEnabled(True)
buttons[-3].setEnabled(True)
buttons[-4].setEnabled(True)
open_update_account()

"""OPEN PAYMENT OPTIONS LAYOUT/REPLACE CATEGORIES LAYOUT"""
def open_payment_options():
global payment_options_layout
global layouts_list
payment_options_layout = payment_options.open_payment_options()
layouts_list.append(payment_options_layout)
categories_groupbox.hide()
try:
edit_account_layout.hide()
subcategories_layout.hide()
except Exception as error:
print("Edit Account Not Opened")
main_layout.addWidget(payment_options_layout, 1, 1)
buttons[-3].setEnabled(False)
buttons[-1].setEnabled(True)
buttons[-2].setEnabled(True)
buttons[-4].setEnabled(True)
open_payment_options()

"""OPEN USER ORDERS HISTORY/REPLACE CATEGORIES LAYOUT"""
def open_user_orders():
Expand All @@ -291,14 +280,40 @@ def open_user_orders():
"""OPEN ABOUT WINDOW"""
def open_about():
about.start_window()
# main_window.hide()

"""OPEN SUBCATEGORIES WINDOW"""
def open_subcategories(sub_cat_name):
global subcategories_layout
global layouts_list
subcategories_layout = subcategories.open_subcategory(sub_cat_name)
layouts_list.append(subcategories_layout)
categories_groupbox.hide()
main_layout.addWidget(subcategories_layout, 1, 1)

def open_favourites():
global favourites_layout
global layouts_list
favourites_layout = favourites.favourites_menu()
layouts_list.append(favourites_layout)
categories_groupbox.hide()
main_layout.addWidget(favourites_layout, 1, 1)



"""BRING BACK THE CATEGORIES"""
def open_categories():
# Not the best approach, that could be improved
for i in layouts_list:
try:
i.hide()
except Exception as error:
print(f"{i} is not opened")
categories_groupbox.show()
buttons[-1].setEnabled(True)
buttons[-2].setEnabled(True)
buttons[-3].setEnabled(True)
buttons[-4].setEnabled(True)
open_categories()


"""OBSOLETE - KEEP FOR NOW FOR DEBUGING PURPOSES, BUT MOST PROBEBLY WONT BE NEEDED"""
Expand Down
41 changes: 35 additions & 6 deletions pyqt5-gui/products.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/python3
import sys
import requests
import inspect
from collections import deque

# Import PyQt5 Engine
from PyQt5.QtWidgets import (QApplication,
QWidget,
Expand All @@ -23,13 +25,19 @@

sys.path.append(r'.')
sys.path.append(r'..')


from collections import deque
from db_handle import postgres_conn


def products_menu(subcategory_name):
global products_groupbox

"""THIS FUNCTION WILL REDIRECT THE CURRENT PROD ID AND PROD NAME TO THE INSERT TO THE POSTGRE DB FUNCTION"""
def redirect_to_insert_to_postgre_func(prod_id, prod_name):
return lambda: insert_into_favourite_products(prod_id, prod_name)

products_groupbox = QGroupBox("Products")
products_grid_layout = QGridLayout()

Expand All @@ -49,11 +57,13 @@ def products_menu(subcategory_name):
products_ids = deque(p[2] for p in result)
print(products_names)


for col in range(3):

current_vertical_layout = QVBoxLayout()

product_name = products_names.popleft()
product_id = products_ids.popleft()

product_image = QLabel()
product_image.setFixedSize(250, 200)
Expand All @@ -65,15 +75,11 @@ def products_menu(subcategory_name):
current_title.setFont(QFont(fonts[0], 12))

current_sku = QLabel()
current_sku.setText(products_ids.popleft())
current_sku.setText(product_id)
current_sku.setFont(QFont(fonts[0], 10))

product_description = products_descriptions.popleft()

# current_description = QPlainTextEdit()
# current_description.insertPlainText(product_description)
# current_description.setFont(QFont(fonts[0], 12))

current_description = QLabel(product_description)
current_description.setWordWrap(True)
current_description.setFont(QFont(fonts[0], 9))
Expand All @@ -87,6 +93,7 @@ def products_menu(subcategory_name):
current_favorites_button.setIcon(QIcon(r'../img/favorite.png'))
current_favorites_button.setIconSize(QSize(30, 30))
current_favorites_button.setFont(QFont(fonts[0], 12))
current_favorites_button.clicked.connect(redirect_to_insert_to_postgre_func(product_id, product_name))

current_basket_button = QPushButton()
current_basket_button.setFixedWidth(35)
Expand All @@ -111,4 +118,26 @@ def products_menu(subcategory_name):

products_groupbox.setLayout(products_grid_layout)

def insert_into_favourite_products(curr_id, curr_product_name):

postgres_conn.POSTGRES_CURSOR.execute("SELECT current_user;")
current_user = postgres_conn.POSTGRES_CURSOR.fetchone()[0]

postgres_conn.POSTGRES_CURSOR.execute(f"SELECT * FROM favourite_products WHERE product_id = '{curr_id}' AND username = 'pesho'")
result = postgres_conn.POSTGRES_CURSOR.fetchall()

if result:
error_msg_box = QMessageBox()
error_msg_box.setIcon(QMessageBox.Warning)
error_msg_box.setText("Product already exists in favorites.")
error_msg_box.setWindowTitle("Info Message")
error_msg_box.setStandardButtons(QMessageBox.Ok)
msg_box = error_msg_box.exec()
else:
postgres_conn.POSTGRES_CURSOR.execute(f"INSERT INTO favourite_products VALUES "
f"('pesho', '{curr_id}', '{curr_product_name}')")
postgres_conn.POSTGRES_CONNECTION.commit()
print("Done") # this line is only to check if the function is executing


return products_groupbox
2 changes: 1 addition & 1 deletion pyqt5-gui/subcategories.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def open_products(cat):
product_layout = products.products_menu(cat)
main_grid_layout.addWidget(product_layout, 1, 0)
subcategories_groupbox.setLayout(main_grid_layout)
print(5)


return subcategories_groupbox

Expand Down