diff --git a/.gitignore b/.gitignore index 26df751..617023d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,17 +1,20 @@ -# ignore all images -img/ - -# ignore config/__pache__ directory -config/__pycache__/ - -# ignore sql -DbFuncs/sql.db - -# ignore DbFuncs/__pache__ directory -DbFuncs/__pycache__/ - -# ignore model/__pache__ directory -model/__pycache__/ - -# ignore __pycache__ directory -__pycache__/ \ No newline at end of file +# ignore all images +img/ + +# ignore config/__pache__ directory +config/__pycache__/ + +# ignore sql +DbFuncs/sql.db + +# ignore DbFuncs/__pache__ directory +DbFuncs/__pycache__/ + +# ignore model/__pache__ directory +model/__pycache__/ + +# ignore __pycache__ directory +__pycache__/ + +# ignore test directory +test/ \ No newline at end of file diff --git a/DbFuncs/db.py b/DbFuncs/db.py index f2eb95d..f7edba9 100644 --- a/DbFuncs/db.py +++ b/DbFuncs/db.py @@ -1,132 +1,323 @@ -import sqlite3 -import datetime -from config import utils - -# import os -# import sys - -# script_dir = os.path.dirname( __file__ ) -# urils_dir = os.path.join( script_dir, '..', 'config' ) -# sys.path.append(urils_dir) -# import utils - -from model.Product import Product - -# insert into product table -def insert_product(product): - try: - conn = sqlite3.connect(utils.dbPath) - cursor = conn.cursor() - - insert_query = """insert into products (name, image, count, price, modifiedAt) - values (?, ?, ?, ?, ?)""" - - imageBlob = utils.convert_to_blod_data(product.image) - data = (product.name, (imageBlob), product.count, product.price, datetime.datetime.now()) - - cursor.execute(insert_query, data) - - print("inserted successfully") - - conn.commit() - cursor.close() - - return True - except sqlite3.Error as error: - print('fail', error) - return False - finally: - if conn: - conn.close() - print('connection is closed') - return False - -# update product item -def update_product(id, count): - try: - conn = sqlite3.connect(utils.dbPath) - cursor = conn.cursor() - - update_query = '''update products set count = ? where id = ?''' - params = (count, id) - cursor.execute(update_query, params) - - conn.commit() - cursor.close() - - except sqlite3.Error as error: - print('fail', error) - finally: - if conn: - conn.close() - print('connection is closed') - -# get all products -def get_products(): - try: - conn = sqlite3.connect(utils.dbPath) - cursor = conn.cursor() - - select_query = '''select id, name, image, count, price from products''' - cursor.execute(select_query) - records = cursor.fetchall() - - conn.commit() - cursor.close() - - except sqlite3.Error as error: - print('fail', error) - records = [] - finally: - if conn: - conn.close() - print('connection is closed') - - return records - -# get product -def get_product(id): - try: - conn = sqlite3.connect(utils.dbPath) - cursor = conn.cursor() - - print(f'id: {id}') - select_query = '''select id, name, image, count, price from products where id = ?''' - cursor.execute(select_query, (id,)) - record = cursor.fetchone() - - conn.commit() - cursor.close() - - except sqlite3.Error as error: - print('fail', error) - record = None - finally: - if conn: - conn.close() - print('connection is closed') - - return record - -def get_product_count(): - try: - conn = sqlite3.connect(utils.dbPath) - cursor = conn.cursor() - - print(f'id: {id}') - select_query = '''select count(id) from products ''' - cursor.execute(select_query) - record = cursor.fetchone() - - conn.commit() - cursor.close() - - except sqlite3.Error as error: - print('fail', error) - record = None - finally: - if conn: - conn.close() - print('connection is closed') - - return record[0] +import sqlite3 +import datetime +from config import utils + +# import os +# import sys + +# script_dir = os.path.dirname( __file__ ) +# urils_dir = os.path.join( script_dir, '..', 'config' ) +# sys.path.append(urils_dir) +# import utils + +from model.Product import Product +from model.Ad import Ad +from model.Machine import Machine + +def insert_ads(ad): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + insert_query = """insert into ads (type, content) + values (?, ?)""" + + blodData = utils.convert_to_blod_data(ad.content) + data = (ad.type, (blodData)) + + cursor.execute(insert_query, data) + + print("inserted ad successfully") + + conn.commit() + cursor.close() + + return True + except sqlite3.Error as error: + print('insert_ad_fail', error) + return False + finally: + if conn: + conn.close() + print('connection is closed') + return False + +def insert_machine(machine): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + insert_query = """insert into machines (name, unit, value) + values (?, ?, ?)""" + + data = (machine.name, machine.unit, machine.value) + + cursor.execute(insert_query, data) + + print("inserted machine successfully") + + conn.commit() + cursor.close() + + return True + except sqlite3.Error as error: + print('insert_machine_fail', error) + return False + finally: + if conn: + conn.close() + print('connection is closed') + return False + + +# insert into product table +def insert_product(product): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + insert_query = """insert into products (itemno, name, thumbnail, nicotine, batterypack, tankvolumn, price, currency, caution) + values (?, ?, ?, ?, ?, ?, ?, ?, ?)""" + imageBlob = utils.convert_to_blod_data(product.thumbnail) + data = (product.itemno, product.name, (imageBlob), product.nicotine, product.batterypack, product.tankvolumn, product.price, product.currency, product.caution) + cursor.execute(insert_query, data) + + print("inserted successfully") + + conn.commit() + cursor.close() + + return True + except sqlite3.Error as error: + print('insert_prodcut_fail', error) + return False + finally: + if conn: + conn.close() + print('connection is closed') + return False + +# update product item +def update_product(id, image, price): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + update_query = '''update products set thumbnail = ?, price = ? where id = ?''' + imageBlob = utils.convert_to_blod_data(image) + params = (imageBlob, price, id) + cursor.execute(update_query, params) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print('update_product_fail', error) + finally: + if conn: + conn.close() + print('connection is closed') + +# get all products +def get_products(): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + select_query = '''select * from products''' + cursor.execute(select_query) + records = cursor.fetchall() + + products = [] + for record in records: + product = convert_to_product(record) + products.append(product) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print('get_prodcuts_fail', error) + records = [] + finally: + if conn: + conn.close() + print('connection is closed') + + return products + +# get product +def get_product(id): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + select_query = '''select * from products where id = ?''' + cursor.execute(select_query, (id,)) + record = cursor.fetchone() + + product = convert_to_product(record) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print('get_prodcut_fail', error) + record = None + finally: + if conn: + conn.close() + print('connection is closed') + + return product + +# get Ad +def get_ad(): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + select_query = '''select * from ads''' + cursor.execute(select_query) + record = cursor.fetchone() + + ad = convert_to_ad(record) + print(f'len(ad):{ad.type}') + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print('get_ad_fail', error) + ad = None + finally: + if conn: + conn.close() + print('connection is closed') + + return ad + +# get Ad +def get_ad_row(id): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + select_query = '''select * from ads where id = ?''' + cursor.execute(select_query, (id,)) + record = cursor.fetchone() + + ad = convert_to_ad(record) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print('get_ad_fail', error) + ad = None + finally: + if conn: + conn.close() + print('connection is closed') + + return ad + + +# get Ad +def get_machine(id): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + select_query = '''select * from machines where id = ?''' + cursor.execute(select_query, (id,)) + record = cursor.fetchone() + + machine = convert_to_machine(record) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print('get_machine_fail', error) + machine = None + finally: + if conn: + conn.close() + print('connection is closed') + + return machine + +def get_product_count(): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + select_query = '''select count(id) from products ''' + cursor.execute(select_query) + record = cursor.fetchone() + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print('fail', error) + record = None + finally: + if conn: + conn.close() + print('connection is closed') + + return record[0] + +# delete Machines +def delete_machines(): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + delete_query = '''delete from machines''' + cursor.execute(delete_query) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print('delete_machine_fail', error) + finally: + if conn: + conn.close() + print('connection is closed') + +# delete Ads +def delete_ads(): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + delete_query = '''delete from ads''' + cursor.execute(delete_query) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print('delete_ads_fail', error) + finally: + if conn: + conn.close() + print('connection is closed') + +def convert_to_product(record): + product = Product(record[0], record[1], record[2], record[3], record[4], + record[5], record[6], record[7], record[8], record[9]) + + return product + +def convert_to_ad(record): + ad = Ad(record[0], record[1], record[2]) + return ad + +def convert_to_machine(record): + machine = Machine(record[0], record[1], record[2], record[3]) + return machine + diff --git a/DbFuncs/db_create.py b/DbFuncs/db_create.py index 039e6e1..eb7946e 100644 --- a/DbFuncs/db_create.py +++ b/DbFuncs/db_create.py @@ -1,96 +1,97 @@ -import sqlite3 -import datetime - -import os -import sys - -script_dir = os.path.dirname( __file__ ) -utils_dir = os.path.join( script_dir, '..', 'config' ) -sys.path.append(utils_dir) -import utils - - -# create product table -def create_table_if_not_exists(): - try: - conn = sqlite3.connect(utils.dbPath) - cursor = conn.cursor() - - create_product_table_query = '''CREATE TABLE IF NOT EXISTS products ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL, - image BLOB NOT NULL, - count INTEGER NOT NULL, - price REAL NOT NULL, - modifiedAt timestamp - )''' - - # Create a users table - cursor.execute(create_product_table_query) - - conn.commit() - cursor.close() - - except sqlite3.Error as error: - print("fail", error) - finally: - if conn: - conn.close() - print('connection is closed') - -def insertData(name, avatar, modifiedAt): - try: - # Connect to the database (create a new file if it doesn't exist) - conn = sqlite3.connect(utils.dbPath) - - # Create a cursor object to execute SQL commands - cursor = conn.cursor() - - # #drop table - # cursor.execute('drop table if exists clients') - # print('dropped successfully') - - create_user_table_query = '''CREATE TABLE IF NOT EXISTS clients ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL, - avatar BLOB NOT NULL, - modifiedAt timestamp - )''' - - # Create a users table - cursor.execute(create_user_table_query) - - print('Created successfully') - - # cursor.execute("PRAGMA table_info('clients')") - # columns = cursor.fetchall() - # print(columns) - - # Insert User into table - insert_user_query = """insert into clients (name, avatar, modifiedAt) - values (?, ?, ?)""" - avatarBlog = convertToBlobData(avatar) - insert_data = (name, avatarBlog, modifiedAt) - cursor.execute(insert_user_query, insert_data) - print('inserted successfully') - - # Get All Users - get_all_users_query = '''select id, name from clients''' - cursor.execute(get_all_users_query) - records = cursor.fetchall() - - print(records) - - - # Commit the changes and close the connection - conn.commit() - cursor.close() - - except sqlite3.Error as error: - print("fail", error) - finally: - if conn: - conn.close() - print('connection is closed') - -# insertData("Smith", "D:\Workspace\Python\Kivy\Hello-kivy\img/1-1.png", datetime.datetime.now()) \ No newline at end of file +import sqlite3 +import datetime + +import os +import sys + +from config import utils + + +def create_tables(): + create_product_table_if_not_exists() + create_ads_table_if_not_exists() + create_machine_table_if_not_exists() + +# create product table +def create_product_table_if_not_exists(): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + create_product_table_query = '''CREATE TABLE IF NOT EXISTS products ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + itemno TEXT NOT NULL, + name TEXT NOT NULL, + thumbnail BLOB NOT NULL, + nicotine TEXT NOT NULL, + batterypack TEXT NOT NULL, + tankvolumn TEXT NOT NULL, + price REAL NOT NULL, + currency TEXT NOT NULL, + caution TEXT NOT NULL + )''' + + # Create a users table + cursor.execute(create_product_table_query) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print("product_table_fail", error) + finally: + if conn: + conn.close() + print('connection is closed') + +# create product table +def create_ads_table_if_not_exists(): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + create_ads_table_query = '''CREATE TABLE IF NOT EXISTS ads ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + type TEXT NOT NULL, + content BLOB NOT NULL + )''' + + # Create a users table + cursor.execute(create_ads_table_query) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print("ads_table_fail", error) + finally: + if conn: + conn.close() + print('connection is closed') + +# create product table +def create_machine_table_if_not_exists(): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + create_machine_table_query = '''CREATE TABLE IF NOT EXISTS machines ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + unit TEXT NOT NULL, + value TEXT NOT NULL + )''' + + # Create a users table + cursor.execute(create_machine_table_query) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print("machine_table_fail", error) + finally: + if conn: + conn.close() + print('connection is closed') + diff --git a/DbFuncs/db_get_list_test.py b/DbFuncs/db_get_list_test.py index 7b31ef6..7b6cb5f 100644 --- a/DbFuncs/db_get_list_test.py +++ b/DbFuncs/db_get_list_test.py @@ -1,35 +1,101 @@ -import sqlite3 -import datetime - -import os -import sys - -script_dir = os.path.dirname( __file__ ) -constants_dir = os.path.join( script_dir, '..', 'config' ) -sys.path.append(constants_dir) -import constants - -conn = sqlite3.connect(constants.dbPath) -cursor = conn.cursor() - - -cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") - -# Fetch all the table names -tables = cursor.fetchall() - -for table in tables: - print(table[0]) - -# Get All Products -get_all_data_query = '''select id, name, price from products''' -cursor.execute(get_all_data_query) -records = cursor.fetchall() - -print(records) - - - -conn.commit() -cursor.close() -conn.close() +import sqlite3 +import datetime +import os +import sys +# import db +script_dir = os.path.dirname( __file__ ) +constants_dir = os.path.join( script_dir, '..', 'config' ) +sys.path.append(constants_dir) +import utils + +class Ad: + def __init__(self, id, type, content): + self.id = id + self.type = type + self.content = content + +# convert image to blob data +def convert_to_blod_data(filename): + with open(filename, 'rb') as file: + blobData = file.read() + return blobData + +def update_product(id, image, price): + try: + conn = sqlite3.connect('./sql.db') + cursor = conn.cursor() + + update_query = '''update products set thumbnail = ?, price = ? where id = ?''' + imageBlob = utils.convert_to_blod_data(image) + params = (imageBlob, price, id) + cursor.execute(update_query, params) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print('update_product_fail', error) + finally: + if conn: + conn.close() + print('connection is closed') + +# insert into product table +def insert_product(product): + try: + conn = sqlite3.connect('./sql.db') + cursor = conn.cursor() + + insert_query = """insert into products (itemno, name, thumbnail, nicotine, batterypack, tankvolumn, price, currency, caution) + values (?, ?, ?, ?, ?, ?, ?, ?, ?)""" + imageBlob = utils.convert_to_blod_data(product.thumbnail) + data = (product.itemno, product.name, (imageBlob), product.nicotine, product.batterypack, product.tankvolumn, product.price, product.currency, product.caution) + cursor.execute(insert_query, data) + + print("inserted successfully") + + conn.commit() + cursor.close() + + return True + except sqlite3.Error as error: + print('insert_prodcut_fail', error) + return False + finally: + if conn: + conn.close() + print('connection is closed') + return False + +def insert_ads(ad): + try: + conn = sqlite3.connect('./sql.db') + cursor = conn.cursor() + + insert_query = """insert into ads (type, content) + values (?, ?)""" + + blodData = convert_to_blod_data(ad.content) + data = (ad.type, (blodData)) + + cursor.execute(insert_query, data) + + print("inserted ad successfully") + + conn.commit() + cursor.close() + + return True + except sqlite3.Error as error: + print('insert_ad_fail', error) + return False + finally: + if conn: + conn.close() + print('connection is closed') + return False + +# update_product(1, '../img/owl.jpg', 500) +ad = Ad(1, 'PPT', '../222.pptx') +insert_ads(ad) +# db.get_ad_row(31) \ No newline at end of file diff --git a/DbFuncs/sql.db b/DbFuncs/sql.db index c726953..883dcc1 100644 Binary files a/DbFuncs/sql.db and b/DbFuncs/sql.db differ diff --git a/ad.py b/ad.py new file mode 100644 index 0000000..507ba9a --- /dev/null +++ b/ad.py @@ -0,0 +1,110 @@ +from kivy.core.window import Window +from kivy.lang import Builder +from kivy.uix.label import Label +from kivy.uix.screenmanager import ScreenManager, Screen +from kivy.clock import Clock +from kivy.uix.video import Video +from DbFuncs import db +import base64 +from kivy.uix.boxlayout import BoxLayout +from kivy.uix.videoplayer import VideoPlayer +from kivy.uix.image import Image +from pptx import Presentation +from config import utils +import os +from io import BytesIO +import subprocess + +# import pyglet +# from pyglet.media import AVBinSource, StaticMemorySource, Player + + +class AdScreen(Screen): + def __init__(self, **kwargs): + super(AdScreen, self).__init__(**kwargs) + Clock.schedule_once(self.retrieve_layout) + + def retrieve_layout(self, dt): + ad = db.get_ad_row(35) + if ad: + if ad.type == 'MP4': + temp_file = './img/ad.mp4' + utils.write_to_file(ad.content, temp_file) + + videoPlayerLayout = VideoPlayerLayout(temp_file) + print(f'self.manager{self.manager}') + videoPlayerLayout.manager = self.manager + videoPlayerLayout.bind(on_touch_up=videoPlayerLayout.on_video_touch_up) + + # Add the VideoPlayer widget to the BoxLayout + self.add_widget(videoPlayerLayout) + elif True: + + ppt_file = BytesIO(ad.content) + + # Save the blob data as a temporary .ppt file + pptFileName = 'temp.ppt' + pptxFileName = 'temp.pptx' + temp_file = './' + pptFileName + utils.write_to_file(ad.content, './' + pptFileName) + + # Convert .ppt file into .pptx file + # Define the command as a list of arguments + path = os.path.dirname( __file__ ) + '/' + command = ['soffice', '--headless', '--convert-to', 'pptx', '--outdir', + path, path + pptFileName] + + # Execute the command + subprocess.run(command) + + + presentation = Presentation(path + pptxFileName) + slides = presentation.slides + + for slide in slides: + # Iterate through the shapes in the slide + for shape in slide.shapes: + # Check if the shape is an image + if shape.shape_type == 13: # 13 corresponds to image shape type + # Extract the image + image = shape.image + image_bytes = image.blob + image_data = BytesIO(image_bytes) + + # Convert the BytesIO object to a base64-encoded string + base64_image = base64.b64encode(image_data.getvalue()).decode() + + # Create an Image widget with the base64-encoded string as the source + slide_image = Image(source=f"data:image/png;base64,{base64_image}", height=400) + slide_image.bind(on_touch_down=self.touch_screen) + self.add_widget(slide_image) + + os.remove('./' + pptFileName) + os.remove('./' + pptxFileName) + + else: + self.add_widget(Label(text='Ads not found', color=(1,0,0,1))) + + def touch_screen(self, instance, touch): + if instance.collide_point(*touch.pos): + self.manager.current = 'List' + +class VideoPlayerLayout(BoxLayout): + def __init__(self, temp_file, **kwargs): + super(VideoPlayerLayout, self).__init__(**kwargs) + self.manager = None + self.temp_file = temp_file + + # Create a VideoPlayer widget + self.player = VideoPlayer(source=temp_file, state='play', + options={'eos': 'loop'}) + + # Add the VideoPlayer widget to the BoxLayout + self.add_widget(self.player) + + def on_video_touch_up(self, video, touch): + # Handle the video player touch up event + if video.collide_point(*touch.pos): + self.manager.current = 'List' + os.remove(self.temp_file) + diff --git a/client.py b/client.py new file mode 100644 index 0000000..aa59d30 --- /dev/null +++ b/client.py @@ -0,0 +1,29 @@ +from websocket import create_connection +import requests +import time + +def create_connect(): + ws = create_connection("ws://echo.websocket.events/") + print(ws.recv()) + print("Sending 'Hello, World'...") + ws.send("Hello, World") + print("Sent") + print("Receiving...") + result = ws.recv() + print("Received '%s'" % result) + ws.close() + +def send_vending_status(): + while True: + # Send an HTTP GET request to a URL of your choice + url = "https://example.com" # Replace with the URL you want to request + response = requests.get(url) + + # Check the response status code + if response.status_code == 200: + print("Request successful.") + else: + print(f"Request failed with status code: {response.status_code}") + + # Wait for 5 seconds before sending the next request + time.sleep(5) \ No newline at end of file diff --git a/config/utils.py b/config/utils.py index a831545..f44abe5 100644 --- a/config/utils.py +++ b/config/utils.py @@ -1,25 +1,25 @@ -dbPath = './DbFuncs/sql.db' - -screenX = 600 -screenY = 900 - -itemLength = 220 - - -# convert image to blob data -def convert_to_blod_data(filename): - with open(filename, 'rb') as file: - blobData = file.read() - return blobData - -# write -def write_to_file(data, filename): - # Convert binary data to proper format and write it on Hard Disk - with open(filename, 'wb') as file: - file.write(data) - print("Stored blob data into: ", filename, "\n") - - - -# path = 'D:\\tmp\\' + product[1] + '.png' +dbPath = './DbFuncs/sql.db' + +screenX = 600 +screenY = 900 + +itemLength = 220 + + +# convert image to blob data +def convert_to_blod_data(filename): + with open(filename, 'rb') as file: + blobData = file.read() + return blobData + +# write +def write_to_file(data, filename): + # Convert binary data to proper format and write it on Hard Disk + with open(filename, 'wb') as file: + file.write(data) + print("Stored blob data into: ", filename, "\n") + + + +# path = 'D:\\tmp\\' + product[1] + '.png' # utils.write_to_file(product[2], path) \ No newline at end of file diff --git a/convert.pptx b/convert.pptx new file mode 100644 index 0000000..0d28b1c Binary files /dev/null and b/convert.pptx differ diff --git a/item.py b/item.py index 950031a..8890a95 100644 --- a/item.py +++ b/item.py @@ -1,211 +1,214 @@ -from kivy.app import App -from kivy.core.window import Window -from kivy.lang import Builder -from kivy.uix.button import ButtonBehavior, Button -from kivy.uix.image import Image -from kivy.uix.label import Label -from kivy.uix.screenmanager import ScreenManager, Screen -from kivy.uix.floatlayout import FloatLayout -from kivy.uix.boxlayout import BoxLayout -from kivy.uix.gridlayout import GridLayout -from kivy.core.image import Image as CoreImage -from kivy.clock import Clock -from kivy.graphics import Color, Rectangle, Triangle, Bezier -import io -import math - -from DbFuncs import db -from DbFuncs import db_create -from model.Product import Product - -from config import utils - -from kivy.graphics import RoundedRectangle, Color, Line - -class ItemScreen(Screen): - def __init__(self, **kwargs): - super(ItemScreen, self).__init__(**kwargs) - self.itemId = None - - def set_item_id(self, id): - self.itemId = id - self.draw_page(self.itemId) - - def draw_page(self, id): - product = db.get_product(id) - - # display name - self.draw_title(product[1]) - - # display product (image, price, ...) - self.draw_product_info(product) - - self.draw_bigo() - - self.ids.back_img.bind(on_touch_down = self.on_back_press) - - def draw_title(self, name): - titleLayout = self.ids.title_layout - titleLabel = Label(text=name) - titleLabel.color = '#c00000' - titleLabel.font_size = 30 - titleLabel.size_hint_x = None - titleLabel.padding = [200, 5, 5, 5] - titleLayout.add_widget(titleLabel) - - def draw_product_info(self, product): - image = Image() - image_stream = io.BytesIO(product[2]) - img = CoreImage(image_stream, ext='png') - image.texture = img.texture - self.ids.item_image_layout.add_widget(image) - - self.draw_label('Abc1234') - self.draw_label('600') - self.draw_label('20mg') - self.draw_label('40asda0mAh') - self.draw_label('2ml') - - priceStr = str(product[4]) + ' EUR' - priceLabel = Label( - text= priceStr, - size_hint=(None, None), - pos_hint={'center_x': 0.5, 'center_y': 0.5}, # Center the label on the image - color=(0, 0, 0, 1), # Set the text color (white in this example) - font_size = 30 - ) - self.ids.price_layout.add_widget(priceLabel) - - # display infos - def draw_label(self, value): - boxLayout = BoxLayout() - boxLayout.size_hint_x = None - boxLayout.padding = [10, 2, 2, 2] - - firstLabel = Label(text=value) - firstLabel.color = '#000000' - firstLabel.halign = 'left' - firstLabel.size_hint_x = None - firstLabel.text_size = (firstLabel.width, None) - boxLayout.add_widget(firstLabel) - self.ids.info_layout.add_widget(boxLayout) - - # display bigo (labels, img on the top-right ) - def draw_bigo(self): - bigoLayout = self.ids.bigo_layout - # Create an Image widget - img = Image(source='./img/bigo.png') # Replace 'your_image.png' with your image file path - - # Calculate the position for the image (top-right corner) - image_x = utils.screenX - img.width - 50 - image_y = utils.screenY / 4 - img.height / 2 - - # Draw the image on the canvas - with bigoLayout.canvas: - Rectangle(pos=(image_x, image_y), size=img.size, texture=img.texture) - - def on_back_press(self, instance, touch): - if instance.collide_point(*touch.pos): - self.manager.current = 'List' - self.clear_widgets() - self.__init__() - - def on_buy_press(self): - print(f'buy_pressed') - - def on_money_button_press(self): - print('money_button_press') - - -class CountNumber(GridLayout): - def __init__(self, **kwargs): - super(CountNumber, self).__init__(**kwargs) - self.number = 0 - self.cols = 2 - self.padding = [0,0,25,0] - self.number_text = None - self.draw_widget() - - def draw_widget(self): - self.number_text = Label( - text=str(self.number), - color='#000000', - font_size = 30 - ) - self.number_text.size_hint_x = 0.6 - self.add_widget(self.number_text) - - controlLayout = BoxLayout(orientation='vertical') - controlLayout.size_hint_x = None - controlLayout.width = 30 - - incImg = Image(source='./img/up-shape.png') - incImg.allow_stretch = True - incImg.bind(on_touch_down = self.increase_number) - controlLayout.add_widget(incImg) - decImg = Image(source='./img/down-shape.png') - decImg.allow_stretch = True - decImg.bind(on_touch_down = self.decrease_number) - controlLayout.add_widget(decImg) - - self.add_widget(controlLayout) - - def increase_number(self, instance, touch): - if instance.collide_point(*touch.pos): - self.number += 1 - self.update_number() - def decrease_number(self, instance, touch): - if instance.collide_point(*touch.pos): - if self.number > 0: - self.number -= 1 - self.update_number() - - def update_number(self): - self.number_text.text = str(self.number) - - -class RoundedBorderGrid(GridLayout): - def __init__(self, **kwargs): - super(RoundedBorderGrid, self).__init__(**kwargs) - - self.bind(pos=self.update_canvas, size=self.update_canvas) - - def update_canvas(self, *args): - self.canvas.before.clear() - with self.canvas.before: - Color(1, 0, 0, 1) # Set the color of the border - - # Parameters for top left rounded corner - top_left_center = (self.x, self.top) - top_left_radius = 20 - - # Parameters for top right rounded corner - top_right_center = (self.right, self.top) - top_right_radius = 20 - - # Parameters for straight bottom corners - bottom_left = (self.x, self.y) - bottom_right = (self.right, self.y) - - self.draw_rounded_border(top_left_center, top_left_radius, top_right_center, top_right_radius, bottom_left, - bottom_right, width=2) - - def draw_rounded_border(self, top_left_center, top_left_radius, top_right_center, top_right_radius, bottom_left, - bottom_right, width=2): - diameter_top_left = top_left_radius * 2 - diameter_top_right = top_right_radius * 2 - - # Draw the top-left rounded corner - with self.canvas.before: - Bezier(points=[bottom_left, bottom_left, top_left_center[0] - top_left_radius, - top_left_center[1] + top_left_radius, bottom_left], segment_length=20, width=width) - - # Draw the top-right rounded corner - with self.canvas.before: - Bezier(points=[bottom_right, bottom_right, top_right_center[0] + top_right_radius, - top_right_center[1] + top_right_radius, bottom_right], segment_length=20, width=width) - - # Draw the straight bottom sides - with self.canvas.before: - Color(1, 1, 1, 1) # Set the color for straight borders +from kivy.app import App +from kivy.core.window import Window +from kivy.lang import Builder +from kivy.uix.button import ButtonBehavior, Button +from kivy.uix.image import Image +from kivy.uix.label import Label +from kivy.uix.screenmanager import ScreenManager, Screen +from kivy.uix.floatlayout import FloatLayout +from kivy.uix.boxlayout import BoxLayout +from kivy.uix.gridlayout import GridLayout +from kivy.core.image import Image as CoreImage +from kivy.clock import Clock +from kivy.graphics import Color, Rectangle, Triangle, Bezier +import io +import math + +from DbFuncs import db +from DbFuncs import db_create +from model.Product import Product + +from config import utils + +from kivy.graphics import RoundedRectangle, Color, Line + +class ItemScreen(Screen): + def __init__(self, **kwargs): + super(ItemScreen, self).__init__(**kwargs) + self.itemId = None + + def set_item_id(self, id): + self.itemId = id + self.draw_page(self.itemId) + + def draw_page(self, id): + product = db.get_product(id) + + # display name + self.draw_title(product.name) + + # display product (image, price, ...) + self.draw_product_info(product) + + # inputMoneyLabel = Label(text='Geld einwerfen') + # self.ids.input_money_button.width = inputMoneyLabel.width + # self.ids.input_money_button.text = inputMoneyLabel.text + + self.draw_bigo() + + self.ids.back_img.bind(on_touch_down = self.on_back_press) + + def draw_title(self, name): + titleLayout = self.ids.title_layout + titleLabel = Label(text=name) + titleLabel.color = (1, 0, 0, 1) + titleLabel.font_size = 30 + titleLabel.size_hint_x = None + titleLabel.width = 200 + # titleLabel.padding = [20, 5, 5, 5] + titleLayout.add_widget(titleLabel) + + def draw_product_info(self, product): + image = Image() + image_stream = io.BytesIO(product.thumbnail) + img = CoreImage(image_stream, ext='png') + image.texture = img.texture + self.ids.item_image_layout.add_widget(image) + + self.draw_label('Abc1234') + self.draw_label('600') + self.draw_label('20mg') + self.draw_label('40asda0mAh') + self.draw_label('2ml') + + priceStr = str(product.price) + ' EUR' + priceLabel = Label( + text= priceStr, + size_hint=(None, None), + pos_hint={'center_x': 0.5, 'center_y': 0.5}, # Center the label on the image + color=(0, 0, 0, 1), # Set the text color (white in this example) + font_size = 30 + ) + self.ids.price_layout.add_widget(priceLabel) + + # display infos + def draw_label(self, value): + boxLayout = BoxLayout() + boxLayout.size_hint_x = None + boxLayout.padding = [10, 2, 2, 2] + + firstLabel = Label(text=value) + firstLabel.color = (0,0,0,1) + firstLabel.halign = 'left' + firstLabel.size_hint_x = None + firstLabel.text_size = (firstLabel.width, None) + boxLayout.add_widget(firstLabel) + self.ids.info_layout.add_widget(boxLayout) + + # display bigo (labels, img on the top-right ) + def draw_bigo(self): + bigoLayout = self.ids.bigo_layout + # Create an Image widget + img = Image(source='./img/bigo.png') # Replace 'your_image.png' with your image file path + # Calculate the position for the image (top-right corner) + image_x = utils.screenX - img.width - 20 + image_y = utils.screenY / 3 - img.height / 2 -100 + # Draw the image on the canvas + with bigoLayout.canvas: + Rectangle(pos=(image_x, image_y), size=img.size, texture=img.texture) + + def on_back_press(self, instance, touch): + if instance.collide_point(*touch.pos): + self.manager.current = 'List' + self.clear_widgets() + self.__init__() + + def on_buy_press(self): + print(f'buy_pressed') + + def on_money_button_press(self): + print('money_button_press') + + +class CountNumber(GridLayout): + def __init__(self, **kwargs): + super(CountNumber, self).__init__(**kwargs) + self.number = 0 + self.cols = 2 + self.padding = [0,0,25,0] + self.number_text = None + self.draw_widget() + + def draw_widget(self): + self.number_text = Label( + text=str(self.number), + color=(0,0,0,1), + font_size = 30 + ) + self.number_text.size_hint_x = 0.6 + self.add_widget(self.number_text) + + controlLayout = BoxLayout(orientation='vertical') + controlLayout.size_hint_x = None + controlLayout.width = 30 + + incImg = Image(source='./img/up-shape.png') + incImg.allow_stretch = True + incImg.bind(on_touch_down = self.increase_number) + controlLayout.add_widget(incImg) + decImg = Image(source='./img/down-shape.png') + decImg.allow_stretch = True + decImg.bind(on_touch_down = self.decrease_number) + controlLayout.add_widget(decImg) + + self.add_widget(controlLayout) + + def increase_number(self, instance, touch): + if instance.collide_point(*touch.pos): + self.number += 1 + self.update_number() + def decrease_number(self, instance, touch): + if instance.collide_point(*touch.pos): + if self.number > 0: + self.number -= 1 + self.update_number() + + def update_number(self): + self.number_text.text = str(self.number) + + +class RoundedBorderGrid(GridLayout): + def __init__(self, **kwargs): + super(RoundedBorderGrid, self).__init__(**kwargs) + + self.bind(pos=self.update_canvas, size=self.update_canvas) + + def update_canvas(self, *args): + self.canvas.before.clear() + with self.canvas.before: + Color(1, 0, 0, 1) # Set the color of the border + + # Parameters for top left rounded corner + top_left_center = (self.x, self.top) + top_left_radius = 20 + + # Parameters for top right rounded corner + top_right_center = (self.right, self.top) + top_right_radius = 20 + + # Parameters for straight bottom corners + bottom_left = (self.x, self.y) + bottom_right = (self.right, self.y) + + self.draw_rounded_border(top_left_center, top_left_radius, top_right_center, top_right_radius, bottom_left, + bottom_right, width=2) + + def draw_rounded_border(self, top_left_center, top_left_radius, top_right_center, top_right_radius, bottom_left, + bottom_right, width=2): + diameter_top_left = top_left_radius * 2 + diameter_top_right = top_right_radius * 2 + + # Draw the top-left rounded corner + with self.canvas.before: + Bezier(points=[bottom_left, bottom_left, top_left_center[0] - top_left_radius, + top_left_center[1] + top_left_radius, bottom_left], segment_length=20, width=width) + + # Draw the top-right rounded corner + with self.canvas.before: + Bezier(points=[bottom_right, bottom_right, top_right_center[0] + top_right_radius, + top_right_center[1] + top_right_radius, bottom_right], segment_length=20, width=width) + + # Draw the straight bottom sides + with self.canvas.before: + Color(1, 1, 1, 1) # Set the color for straight borders Bezier(points=[bottom_left, bottom_right], width=width) \ No newline at end of file diff --git a/kv/list.kv b/kv/list.kv index 1c3ff6f..83ea42b 100644 --- a/kv/list.kv +++ b/kv/list.kv @@ -1,289 +1,288 @@ -WindowManager: - ListScreen: - ItemScreen: - -: - name: 'List' - canvas.before: - Color: - rgba: 1, 1, 1, 1 # Set the color to red (RGBA format) - Rectangle: - pos: self.pos - size: self.size - - GridLayout: - rows: 2 - canvas.before: - Color: - rgb: (0, 0, 0) - Line: - width: 1 - rectangle: self.x + 5 , self.y + 10, self.width - 10, self.height - 18 - - BoxLayout: - pos: self.pos - size_hint: (0.8, 0.2) - padding:20, 10, 20, 10 - canvas.before: - Color: - rgb: 192 ,0 ,0 - Line: - width: 2 - rectangle: self.x + 15 , self.y + 10 , self.width - 30, self.height - 30 - - ScrollView: - id: category_scroll_view - do_scroll_x: True - do_scroll_y: False - scroll_type: ["bars","content"] - bar_width: 5 - GridLayout: - id: category_layout - padding: 10, 30, 10, 10 - size_hint: (None, 1) - rows: 1 - - GridLayout: - rows: 3 - padding:6, 10, 6, 10 - - BoxLayout: # up button layout - id: up_image_layout - canvas.before: - Color: - rgb: 192 ,0 ,0 - Rectangle: - pos: self.pos[0], self.pos[1] + self.height / 2 - size: self.width, self.height / 2 - size_hint_y: 0.15 - padding: [root.WIDTH / 6, 0, 0, 0] - Image: - id: up_image - source: './img/up.png' - size: self.texture_size - - ImageScrollView: # product images view - id: image_scroll_view - size: root.width, root.height - pos: 0, 0 - scroll_type: ["bars","content"] - bar_width: 5 - canvas.before: - Color: - rgb: 192 ,100 ,0 - Rectangle: - pos: self.pos - size: self.size - GridLayout: - id:image_layout - cols:2 - size_hint_y: None - spacing: 20 - padding: 10, 30, 10, 20 - BoxLayout: # down button layout - canvas.before: - Color: - rgb: 192 ,0 ,0 - Rectangle: - pos: self.pos - size: self.width, self.height / 2 - size_hint_y: 0.15 - padding: [root.WIDTH / 6, 0, 0, 0] - Image: - id: down_image - source: './img/down.png' - size: self.texture_size - -: - name: 'Item' - - canvas.before: - Color: - rgba: 1, 1, 1, 1 # Set the color to red (RGBA format) - Rectangle: - pos: self.pos - size: self.size - - GridLayout: - rows: 4 - GridLayout: - id: title_layout - cols: 2 - size_hint_y: 0.2 - padding: [20, 10, 20, 10] - canvas.before: - Color: - rgb: (1,1,1) - Rectangle: - pos: self.pos - size: self.size - Image: - id: back_img - source:'./img/back.png' - size: self.size - size_hint_x: None - - GridLayout: - cols: 2 - spacing: 10 - BoxLayout: - padding: 10 - size_hint_x: .6 - RelativeLayout: - id: item_image_layout - - GridLayout: - rows: 3 - size_hint_x: .4 - GridLayout: - padding: 10, 50, 10, 10 - cols: 2 - spacing: 5 - size_hint_y:.3 - canvas.before: - Color: - rgb: (192,0,0) - Line: - width: 1 - rectangle: self.x , self.y - 15 , self.width-10, self.height-10 - - GridLayout: - rows: 5 - spacing: 5 - Label: - text: 'Art.sdasdads:' - color: (0,0,0,1) - halign: 'left' - valign: 'middle' - size_hint_x: None - text_size: self.width, None - Label: - text: 'Züge:' - color: (0,0,0,1) - halign: 'left' - valign: 'middle' - size_hint_x: None - text_size: self.width, None - Label: - text: 'Nikotin:' - color: (0,0,0,1) - halign: 'left' - size_hint_x: None - text_size: self.width, None - Label: - text: 'Akku:' - color: (0,0,0,1) - size_hint_x: None - halign: 'left' - text_size: self.width, None - Label: - text: 'Tankvol.:' - color: (0,0,0,1) - size_hint_x: None - halign: 'left' - text_size: self.width, None - GridLayout: - rows: 5 - spacing: 5 - id: info_layout - RelativeLayout: - id: price_layout - size_hint_y:.15 - Image: - source: './img/price.png' - size: self.size - CountNumber: - size_hint_y:.15 - canvas.before: - Color: - rgb: (1,1,0,1) - Rectangle: - pos: self.pos - size: self.size - Color: - rgb: (192,0,0) - Line: - width: 1 - rectangle: self.x + 15 , self.y + 10 , self.width - 35, self.height - 20 - - Line: - width: 1 - rectangle: self.x + 15 , self.y + 10 , self.width - 75, self.height - 20 - - - GridLayout: - cols: 2 - spacing: 20 - padding: 10, 20, 10, 20 - size_hint: (1, 0.3) - BoxLayout: - size_hint_x: 0.5 - padding: [10, 0, 0, 0] - Button: - text: '20.00 Eur' - color: (0,0,0,1) - font_size: 30 - background_normal: './img/bg-btn-1.png' - background_color: (1,1,1,0.5) - background_down: './img/price.png' - size_hint_x: None - size:(200, self.height) - on_press:root.on_money_button_press() - BoxLayout: - size_hint_x: 0.4 - padding: [10, 0, 0, 0] - Button: - id: round_button - canvas.before: - Color: - rgba: (1,.7,0.3,1) if self.state=='normal' else (1,0,0,1) - RoundedRectangle: - pos: self.pos - size: self.size - radius: [20,] - - text: 'Buy' - color: (1,1,1,1) - font_size: 30 - background_color: (1,1,1,0) - size_hint_x: None - size:(200, self.height) - on_press: root.on_buy_press() - - BoxLayout: - id: bigo_layout - size_hint:(1, 0.8) - orientation: 'vertical' - padding: 10 - canvas.before: - Color: - rgb: (192,0,0) - Line: - width: 1 - rectangle: self.x + 5 , self.y + 20 , self.width - 10, self.height - 50 - Label: - text: 'Hello worldHello worldHello worldworldHello world' - color: (0.1,0.1,0.1,.5) - halign: 'left' - valign: 'middle' - size_hint_x: None - size: (500, self.height) - text_size: self.width, None - Label: - text: 'Hello world' - color: (0.1,0.1,0.1,.5) - halign: 'left' - valign: 'middle' - size_hint_x: None - text_size: self.width, None - Label: - text: 'Hello world' - color: (0.1,0.1,0.1,.5) - halign: 'left' - valign: 'middle' - size_hint_x: None - text_size: self.width, None - -: -: +WindowManager: + AdScreen: + ListScreen: + ItemScreen: + +: + name:'Ad' + +: + name: 'List' + canvas.before: + Color: + rgba: 1, 1, 1, 1 # Set the color to red (RGBA format) + Rectangle: + pos: self.pos + size: self.size + + GridLayout: + rows: 2 + canvas.before: + Color: + rgb: (0, 0, 0) + Line: + width: 1 + rectangle: self.x + 5 , self.y + 10, self.width - 10, self.height - 18 + + BoxLayout: + pos: self.pos + size_hint: (0.8, 0.2) + padding:20, 10, 20, 10 + canvas.before: + Color: + rgb: 192 ,0 ,0 + Line: + width: 2 + rectangle: self.x + 15 , self.y + 10 , self.width - 30, self.height - 30 + + ScrollView: + id: category_scroll_view + do_scroll_x: True + do_scroll_y: False + scroll_type: ["bars","content"] + bar_width: 5 + GridLayout: + id: category_layout + padding: 10, 30, 10, 10 + size_hint: (None, 1) + rows: 1 + + GridLayout: + rows: 3 + padding:6, 10, 6, 10 + + BoxLayout: # up button layout + id: up_image_layout + canvas.before: + Color: + rgb: 192 ,0 ,0 + Rectangle: + pos: self.pos[0], self.pos[1] + self.height / 2 + size: self.width, self.height / 2 + size_hint_y: 0.15 + padding: [root.WIDTH / 6, 0, 0, 0] + Image: + id: up_image + source: './img/up.png' + size: self.texture_size + + ImageScrollView: # product images view + id: image_scroll_view + size: root.width, root.height + pos: 0, 0 + scroll_type: ["bars","content"] + bar_width: 5 + canvas.before: + Color: + rgb: 192 ,100 ,0 + Rectangle: + pos: self.pos + size: self.size + GridLayout: + id:image_layout + cols:2 + size_hint_y: None + spacing: 20 + padding: 10, 30, 10, 20 + BoxLayout: # down button layout + canvas.before: + Color: + rgb: 192 ,0 ,0 + Rectangle: + pos: self.pos + size: self.width, self.height / 2 + size_hint_y: 0.15 + padding: [root.WIDTH / 6, 0, 0, 0] + Image: + id: down_image + source: './img/down.png' + size: self.texture_size + +: + name: 'Item' + + canvas.before: + Color: + rgba: 1, 1, 1, 1 # Set the color to red (RGBA format) + Rectangle: + pos: self.pos + size: self.size + + GridLayout: + rows: 4 + GridLayout: + id: title_layout + cols: 2 + size_hint_y: 0.2 + padding: [20, 10, 20, 10] + canvas.before: + Color: + rgb: (1,1,1) + Rectangle: + pos: self.pos + size: self.size + Image: + id: back_img + source:'./img/back.png' + size: self.size + size_hint_x: None + + GridLayout: + cols: 2 + spacing: 10 + BoxLayout: + padding: 10 + size_hint_x: .6 + RelativeLayout: + id: item_image_layout + + GridLayout: + rows: 3 + size_hint_x: .4 + GridLayout: + padding: 10, 50, 10, 10 + cols: 2 + spacing: 5 + size_hint_y:.3 + canvas.before: + Color: + rgb: (192,0,0) + Line: + width: 1 + rectangle: self.x , self.y - 15 , self.width-10, self.height-10 + + GridLayout: + rows: 5 + spacing: 5 + Label: + text: 'Art.sdasdads:' + color: (0,0,0,1) + halign: 'left' + valign: 'middle' + size_hint_x: None + text_size: self.width, None + Label: + text: 'Züge:' + color: (0,0,0,1) + halign: 'left' + valign: 'middle' + size_hint_x: None + text_size: self.width, None + Label: + text: 'Nikotin:' + color: (0,0,0,1) + halign: 'left' + size_hint_x: None + text_size: self.width, None + Label: + text: 'Akku:' + color: (0,0,0,1) + size_hint_x: None + halign: 'left' + text_size: self.width, None + Label: + text: 'Tankvol.:' + color: (0,0,0,1) + size_hint_x: None + halign: 'left' + text_size: self.width, None + GridLayout: + rows: 5 + spacing: 5 + id: info_layout + RelativeLayout: + id: price_layout + size_hint_y:.15 + Image: + source: './img/price.png' + size: self.size + CountNumber: + size_hint_y:.15 + canvas.before: + Color: + rgb: (192,0,0) + Line: + width: 1 + rectangle: self.x + 15 , self.y + 10 , self.width - 35, self.height - 20 + + Line: + width: 1 + rectangle: self.x + 15 , self.y + 10 , self.width - 75, self.height - 20 + + + GridLayout: + cols: 2 + spacing: 20 + padding: 10, 20, 10, 20 + size_hint: (1, 0.3) + BoxLayout: + size_hint_x: 0.5 + padding: [10, 0, 0, 0] + Button: + id: input_money_button + text: 'Geld einwerfen' + color: (1,0,0,1) + halign: 'center' + font_size: 30 + background_normal: './img/bg-btn-1.png' + background_color: (1,1,1,0.5) + background_down: './img/price.png' + on_press:root.on_money_button_press() + BoxLayout: + size_hint_x: 0.4 + padding: [30, 0, 0, 0] + Button: + id: round_button + canvas.before: + Color: + rgba: (1,.7,0.3,1) if self.state=='normal' else (1,0,0,1) + RoundedRectangle: + pos: self.pos + size: self.size + radius: [20,] + + text: 'Buy' + color: (1,1,1,1) + font_size: 30 + background_color: (1,1,1,0) + size_hint_x: None + size:(200, self.height) + on_press: root.on_buy_press() + + BoxLayout: + id: bigo_layout + size_hint:(1, 0.8) + orientation: 'vertical' + padding: 10 + canvas.before: + Color: + rgb: (192,0,0) + Line: + width: 1 + rectangle: self.x + 5 , self.y + 20 , self.width - 10, self.height - 50 + Label: + text: 'Hello worldHello worldHello worldworldHello world' + color: (0.1,0.1,0.1,.5) + halign: 'left' + valign: 'middle' + size_hint_x: None + size: (500, self.height) + text_size: self.width, None + Label: + text: 'Hello world' + color: (0.1,0.1,0.1,.5) + halign: 'left' + valign: 'middle' + size_hint_x: None + text_size: self.width, None + Label: + text: 'Hello world' + color: (0.1,0.1,0.1,.5) + halign: 'left' + valign: 'middle' + size_hint_x: None + text_size: self.width, None + +: +: : \ No newline at end of file diff --git a/list.py b/list.py index db4da52..905d415 100644 --- a/list.py +++ b/list.py @@ -1,191 +1,191 @@ -from kivy.app import App -from kivy.core.window import Window -from kivy.lang import Builder -from kivy.uix.button import ButtonBehavior -from kivy.uix.image import Image -from kivy.uix.label import Label -from kivy.uix.screenmanager import ScreenManager, Screen -from kivy.uix.floatlayout import FloatLayout -from kivy.uix.gridlayout import GridLayout -from kivy.uix.boxlayout import BoxLayout -from kivy.uix.scrollview import ScrollView -from kivy.core.image import Image as CoreImage -from kivy.clock import Clock -from kivy.graphics import Color, Rectangle, Bezier -# from kivy.graphics.texture import Texture -from kivy.properties import StringProperty -from PIL import Image as PILImage -import io -import math - -from DbFuncs import db -from DbFuncs import db_create -from model.Product import Product - -from config import utils -from item import ItemScreen - - -class ListScreen(Screen): - WIDTH = utils.screenX - def __init__(self, **kwargs): - super(ListScreen, self).__init__(**kwargs) - Clock.schedule_once(self.retrieve_image_layout) - Clock.schedule_once(self.retrieve_up_and_down_image) - Clock.schedule_once(self.retrieve_category_layout) - # self.imageList = [] - self.imageLayoutHeight = 0 - self.scroll_position = 0 - self.scroll_move_dis = utils.itemLength - self.scroll_y_dis = 0 - self.scroll_y_offset = 0 - - # prevent to delay, so we can get image_layou and draw dynamically - def retrieve_image_layout(self, dt): - image_layout = self.ids.image_layout # Access the image_layout widget - - # get all products. - products = self.get_products() - - # draw Items - if products: - # get scroll_y step - self.scroll_y_dis = 1 / (math.ceil(len(products) / 2) + 1) - - for product in products: - image = self.on_draw_item(product) - self.imageLayoutHeight += image.height - container = BoxLayout() - lp = (utils.screenX / 2 - utils.itemLength - 10) / 2 - container.padding = [lp ,10,lp,10] - container.size_hint_y = None - container.height = image.height + 10 - container.add_widget(image) - # self.imageList.append(image) - image_layout.add_widget(container) - - else: - image_layout.add_widget(Label(text='Image not found')) - - image_layout.height = self.imageLayoutHeight + 10 - - def get_products(self): - products = db.get_products() - return products - - # draw one image - def on_draw_item(self, product): - image = ImageItem() - image_stream = io.BytesIO(product[2]) - img = CoreImage(image_stream, ext='png') - image.texture = img.texture - image.name = product[0] - image.manager = self.manager - - return image - - ################################################################### - def retrieve_up_and_down_image(self, dt): - up_image = self.ids.up_image - up_image.size_hint_x = None - up_image.width = utils.screenX * 2 / 3 - up_image.bind(on_touch_down = self.on_up_img_click) - down_image = self.ids.down_image - down_image.size_hint_x = None - down_image.width = utils.screenX * 2 / 3 - down_image.bind(on_touch_down = self.on_down_img_click) - - def on_up_img_click(self, instance, touch): - if instance.collide_point(*touch.pos): - # image_scroll_view.scroll_y = 1 - # if(self.scroll_position > 0): - # self.ids.image_scroll_view.scroll_y += self.scroll_y_dis - # self.scroll_position -= self.scroll_move_dis - - imageScrollView = self.ids.image_scroll_view - imageLayout = self.ids.image_layout - if imageLayout.children: - firstChild = imageLayout.children[-1] - imageScrollView.scroll_to(firstChild) - - - - def on_down_img_click(self, instance, touch): - if instance.collide_point(*touch.pos): - # image_scroll_view.scroll_y = 1 - # if(self.ids.image_scroll_view.scroll_y > 0.01): - # self.ids.image_scroll_view.scroll_y -= self.scroll_y_dis - # self.scroll_position += self.scroll_move_dis - - imageScrollView = self.ids.image_scroll_view - imageLayout = self.ids.image_layout - if imageLayout.children: - lastChild = imageLayout.children[0] - imageScrollView.scroll_to(lastChild) - - - ######################################################################## - - def retrieve_category_layout(self, dt): - categoryLayout = self.ids.category_layout # Access the image_layout widget - - for i in range(8): - image = self.on_draw_category_item() - categoryLayout.add_widget(image) - categoryLayout.width += image.width - - def on_draw_category_item(self): - boxlayout = BoxLayout(orientation='vertical') - image = CategoryItem(source='./img/category.png') - specLabel = Label(text='bis zu 600') - specLabel.color = '#000000' - specLabel.font_size = 15 - nameLabel = Label(text='Zuge') - nameLabel.color = '#000000' - nameLabel.font_size = 15 - nameLabel.background_color = '#111111' - boxlayout.add_widget(image) - boxlayout.add_widget(specLabel) - boxlayout.add_widget(nameLabel) - boxlayout.height = 100 - - return boxlayout - -# product image item -class ImageItem(Image): - def __init__(self, **kwargs): - super(ImageItem, self).__init__(**kwargs) - self.manager = None - self.size = (utils.itemLength, utils.itemLength) - self.size_hint = (None, None) - - def on_touch_down(self, touch): - if self.collide_point(*touch.pos): - self.manager.current = 'Item' - self.manager.get_screen("Item").set_item_id(self.name) - -# top bar image -class CategoryItem(Image): - def __init__(self, **kwargs): - super(CategoryItem, self).__init__(**kwargs) - - -# image scroll view -class ImageScrollView(ScrollView): - def __init__(self, **kwargs): - super(ImageScrollView, self).__init__(**kwargs) - - # self.imageLayout = self.ids.image_layout - - # def on_touch_move(self, touch): - # if self.collide_point(*touch.pos): - # if touch.dy > 0 and self.scroll_y < 1: # Scrolling up - # print(f'scroll----{self.scroll_y}') - # self.scroll_y -= self.scroll_y_dis # Adjust the scroll_y value to modify the scrolling speed - # elif self.scroll_y > 0.01: # Scrolling down - # print(f'scroll+++++{self.scroll_y}') - # self.scroll_y += self.scroll_y_dis - # else: - # print('out') - - # return super(ImageScrollView, self).on_touch_move(touch) +from kivy.app import App +from kivy.core.window import Window +from kivy.lang import Builder +from kivy.uix.button import ButtonBehavior +from kivy.uix.image import Image +from kivy.uix.label import Label +from kivy.uix.screenmanager import ScreenManager, Screen +from kivy.uix.floatlayout import FloatLayout +from kivy.uix.gridlayout import GridLayout +from kivy.uix.boxlayout import BoxLayout +from kivy.uix.scrollview import ScrollView +from kivy.core.image import Image as CoreImage +from kivy.clock import Clock +from kivy.graphics import Color, Rectangle, Bezier +# from kivy.graphics.texture import Texture +from kivy.properties import StringProperty +from PIL import Image as PILImage +import io +import math + +from DbFuncs import db +from DbFuncs import db_create +from model.Product import Product + +from config import utils +from item import ItemScreen + + +class ListScreen(Screen): + WIDTH = utils.screenX + def __init__(self, **kwargs): + super(ListScreen, self).__init__(**kwargs) + Clock.schedule_once(self.retrieve_image_layout) + Clock.schedule_once(self.retrieve_up_and_down_image) + Clock.schedule_once(self.retrieve_category_layout) + # self.imageList = [] + self.productImageHeight = 0 + self.scroll_position = 0 + self.scroll_move_dis = utils.itemLength + self.scroll_y_dis = 0 + self.scroll_y_offset = 0 + + # prevent to delay, so we can get image_layou and draw dynamically + def retrieve_image_layout(self, dt): + image_layout = self.ids.image_layout # Access the image_layout widget + + # get all products. + products = self.get_products() + + # draw Items + if products: + # get scroll_y step + # self.scroll_y_dis = 1 / (math.ceil(len(products) / 2) - 1) + + for product in products: + image = self.on_draw_item(product) + self.productImageHeight = image.height + container = BoxLayout() + lp = (utils.screenX / 2 - utils.itemLength - 10) / 2 + container.padding = [lp ,10,lp,10] + container.size_hint_y = None + container.height = image.height + 10 + container.add_widget(image) + image_layout.add_widget(container) + + else: + image_layout.add_widget(Label(text='Image not found', color=(0,0,0,1))) + + rowCnt = math.ceil(len(products) / 2 + 1) + image_layout.height = rowCnt * self.productImageHeight + (rowCnt - 1) * 20 + + def get_products(self): + products = db.get_products() + return products + + # draw one image + def on_draw_item(self, product): + image = ImageItem() + image_stream = io.BytesIO(product.thumbnail) + img = CoreImage(image_stream, ext='png') + image.texture = img.texture + image.name = product.id + image.manager = self.manager + + return image + + ################################################################### + def retrieve_up_and_down_image(self, dt): + up_image = self.ids.up_image + up_image.size_hint_x = None + up_image.width = utils.screenX * 2 / 3 + up_image.bind(on_touch_down = self.on_up_img_click) + down_image = self.ids.down_image + down_image.size_hint_x = None + down_image.width = utils.screenX * 2 / 3 + down_image.bind(on_touch_down = self.on_down_img_click) + + def on_up_img_click(self, instance, touch): + if instance.collide_point(*touch.pos): + # image_scroll_view.scroll_y = 1 + # if(self.scroll_position > 0): + # self.ids.image_scroll_view.scroll_y += self.scroll_y_dis + # self.scroll_position -= self.scroll_move_dis + + imageScrollView = self.ids.image_scroll_view + imageLayout = self.ids.image_layout + if imageLayout.children: + firstChild = imageLayout.children[-1] + imageScrollView.scroll_to(firstChild) + + + + def on_down_img_click(self, instance, touch): + if instance.collide_point(*touch.pos): + # image_scroll_view.scroll_y = 1 + # if(self.ids.image_scroll_view.scroll_y > 0.01): + # self.ids.image_scroll_view.scroll_y -= self.scroll_y_dis + # self.scroll_position += self.scroll_move_dis + + imageScrollView = self.ids.image_scroll_view + imageLayout = self.ids.image_layout + if imageLayout.children: + lastChild = imageLayout.children[0] + imageScrollView.scroll_to(lastChild) + + + ######################################################################## + + def retrieve_category_layout(self, dt): + categoryLayout = self.ids.category_layout # Access the image_layout widget + + for i in range(8): + image = self.on_draw_category_item() + categoryLayout.add_widget(image) + categoryLayout.width += image.width + + def on_draw_category_item(self): + boxlayout = BoxLayout(orientation='vertical') + image = CategoryItem(source='./img/category.png') + specLabel = Label(text='bis zu 600') + specLabel.color = (0,0,0,1) + specLabel.font_size = 15 + nameLabel = Label(text='Zuge') + nameLabel.color = (0,0,0,1) + nameLabel.font_size = 15 + nameLabel.background_color = (1,1,1,1) + boxlayout.add_widget(image) + boxlayout.add_widget(specLabel) + boxlayout.add_widget(nameLabel) + boxlayout.height = 100 + + return boxlayout + +# product image item +class ImageItem(Image): + def __init__(self, **kwargs): + super(ImageItem, self).__init__(**kwargs) + self.manager = None + self.size = (utils.itemLength, utils.itemLength) + self.size_hint = (None, None) + + def on_touch_down(self, touch): + if self.collide_point(*touch.pos): + self.manager.current = 'Item' + self.manager.get_screen("Item").set_item_id(self.name) + +# top bar image +class CategoryItem(Image): + def __init__(self, **kwargs): + super(CategoryItem, self).__init__(**kwargs) + + +# image scroll view +class ImageScrollView(ScrollView): + def __init__(self, **kwargs): + super(ImageScrollView, self).__init__(**kwargs) + + # self.imageLayout = self.ids.image_layout + + # def on_touch_move(self, touch): + # if self.collide_point(*touch.pos): + # if touch.dy > 0 and self.scroll_y < 1: # Scrolling up + # print(f'scroll----{self.scroll_y}') + # self.scroll_y -= self.scroll_y_dis # Adjust the scroll_y value to modify the scrolling speed + # elif self.scroll_y > 0.01: # Scrolling down + # print(f'scroll+++++{self.scroll_y}') + # self.scroll_y += self.scroll_y_dis + # else: + # print('out') + + # return super(ImageScrollView, self).on_touch_move(touch) diff --git a/main.py b/main.py index 69bdc36..f39d827 100644 --- a/main.py +++ b/main.py @@ -1,66 +1,83 @@ -from kivy.app import App -from kivy.core.window import Window -from kivy.lang import Builder -from kivy.uix.button import ButtonBehavior -from kivy.uix.image import Image -from kivy.uix.label import Label -from kivy.uix.screenmanager import ScreenManager, Screen -from kivy.uix.floatlayout import FloatLayout -from kivy.uix.relativelayout import RelativeLayout -from kivy.uix.boxlayout import BoxLayout -from kivy.core.image import Image as CoreImage -from kivy.clock import Clock -from kivy.graphics import Color, Rectangle -# from kivy.graphics.texture import Texture -from kivy.properties import StringProperty -from PIL import Image as PILImage -import io -import math - -from DbFuncs import db -from DbFuncs import db_create -from model.Product import Product - -from config import utils - -from item import ItemScreen -from list import ListScreen - -dbFlag = False - -class WindowManager(ScreenManager): - pass - -kv = Builder.load_file('./kv/list.kv') - -class MainApp(App): - # Main Application - def build(self): - Window.size = (utils.screenX, utils.screenY) - - sm = WindowManager() - sm.add_widget(ListScreen(name='List')) - sm.add_widget(ItemScreen(name='Item')) - - # create db and insert data - if dbFlag: - self.insertProduct() - - return sm - - # create db and insert data - def insertProduct(self): - - db_create.create_table_if_not_exists() - - names = ['1-1.png', '1-2.png', '2-1.png', '2-2.png', '1-1.png', '1-2.png', '2-1.png'] - path = 'D:\Workspace\Python\Kivy\Hello-kivy\img' - for name in names: - image = path + "/" + name - data = Product('111', image, 10, 100) - db.insert_product(data) - - - -if __name__ == '__main__': +from kivy.app import App +from kivy.core.window import Window +from kivy.lang import Builder +from kivy.uix.button import ButtonBehavior +from kivy.uix.image import Image +from kivy.uix.label import Label +from kivy.uix.screenmanager import ScreenManager, Screen +from kivy.uix.floatlayout import FloatLayout +from kivy.uix.relativelayout import RelativeLayout +from kivy.uix.boxlayout import BoxLayout +from kivy.core.image import Image as CoreImage +from kivy.clock import Clock +from kivy.graphics import Color, Rectangle +# from kivy.graphics.texture import Texture +from kivy.properties import StringProperty +from PIL import Image as PILImage +import io +import math + +from DbFuncs import db +from DbFuncs import db_create +from model.Product import Product +from model.Ad import Ad + +from config import utils + +from item import ItemScreen +from list import ListScreen +from ad import AdScreen + +dbFlag = False + +class WindowManager(ScreenManager): + def __init__(self, **kwargs): + super().__init__(**kwargs) + with self.canvas.before: + Color(1, 1, 1, 1) # a white color + self.rect = Rectangle(pos=self.pos, size=self.size) + self.bind(pos=self.update_rect, size=self.update_rect) + + def update_rect(self, *args): + self.rect.pos = self.pos + self.rect.size = self.size + +kv = Builder.load_file('./kv/list.kv') + +class MainApp(App): + # Main Application + def build(self): + Window.size = (utils.screenX, utils.screenY) + + sm = WindowManager() + sm.add_widget(AdScreen(name='Ad')) + sm.add_widget(ListScreen(name='List')) + sm.add_widget(ItemScreen(name='Item')) + + # create db and insert data + if dbFlag: + self.insertProduct() + + return sm + + # create db and insert data + def insertProduct(self): + + # db.delete_ads() + db_create.create_tables() + + + names = ['1-1.png', '1-2.png', '2-1.png', '2-2.png', '1-1.png', '1-2.png', '2-1.png'] + path = './img' + for name in names: + image = path + "/" + name + data = Product(1, '1234', 'Prodcut1', image, '20mg', '300mAh', 'XXX', 10, 'EUR', 'This is .....') + # db.insert_product(data) + + ad = Ad(1, 'PPT', './pptx.pptx') + # ad = Ad(1, 'MP4', './test.mp4') + db.insert_ads(ad) + + +if __name__ == '__main__': MainApp().run() \ No newline at end of file diff --git a/model/Ad.py b/model/Ad.py new file mode 100644 index 0000000..95c02cd --- /dev/null +++ b/model/Ad.py @@ -0,0 +1,5 @@ +class Ad: + def __init__(self, id, type, content): + self.id = id + self.type = type + self.content = content \ No newline at end of file diff --git a/model/Machine.py b/model/Machine.py new file mode 100644 index 0000000..5dd64ef --- /dev/null +++ b/model/Machine.py @@ -0,0 +1,6 @@ +class Machine: + def __init__(self, id, name, unit, value): + self.id = id + self.name = name + self.unit = unit + self.value = value \ No newline at end of file diff --git a/model/Product.py b/model/Product.py index 5d23d4b..97fe566 100644 --- a/model/Product.py +++ b/model/Product.py @@ -1,6 +1,12 @@ -class Product: - def __init__(self, name, image, count, price): - self.name = name - self.image = image - self.count = count - self.price = price \ No newline at end of file +class Product: + def __init__(self, id, itemno, name, thumbnail, nicotine, batterypack, tankvolumn, price, currency, caution): + self.id = id + self.itemno = name + self.name = name + self.thumbnail = thumbnail + self.nicotine = nicotine + self.batterypack = batterypack + self.tankvolumn = tankvolumn + self.price = price + self.currency = currency + self.caution = caution \ No newline at end of file diff --git a/test.py b/test.py deleted file mode 100644 index e2e7b09..0000000 --- a/test.py +++ /dev/null @@ -1,53 +0,0 @@ -from kivy.app import App -from kivy.uix.boxlayout import BoxLayout -from kivy.lang import Builder - - -class KivyGuiApp(App): - def build(self): - myBox = MyBox() - myBox.print_ids() - return root_widget - - -class MyBox(BoxLayout): - def print_ids(self, *args): - print("\nids:") - print(self.ids) - for widget in self.walk(): - print("{} -> {}".format(widget, widget.id)) - def print_names(self, *args): - print("\nnames:") - for widget in self.walk(): - print("{} -> {}".format(widget, widget.name)) - - - -root_widget = Builder.load_string(""" -MyBox: - id: screen_manager - name: 'screen_manager' - - SimpleLayout: - id: simple_layout - name: 'simple_layout' - - -: - id: simple_layout_rule - name: 'simple_layout_rule' - Button: - id: button_ids - name: 'button_ids' - text: 'print ids to console' - on_release: app.root.print_ids() - Button: - id: button_names - name: 'button_names' - text: 'print names to console' - on_release: app.root.print_names() -""") - - -if __name__ == '__main__': - KivyGuiApp().run() \ No newline at end of file