Skip to content

Commit

Permalink
Merge pull request #85 from MartinHlavna/21-recent-files
Browse files Browse the repository at this point in the history
Implementácia nedávnych projektov and reimportovania imprtovaných súborov
  • Loading branch information
MartinHlavna authored Feb 2, 2025
2 parents 56fca2d + 83978b1 commit baad145
Show file tree
Hide file tree
Showing 9 changed files with 501 additions and 312 deletions.
20 changes: 0 additions & 20 deletions src/backend/service/metadata_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,6 @@ def save(metadata: Metadata, path: string):
with open(path, 'w') as file:
json.dump(metadata.to_dict(), file, indent=4)

@staticmethod
def get_recent_file(metadata: Metadata):
"""Get latest available recent file"""
file_path = None
while len(metadata.recent_files) > 0 and file_path is None:
file_path = metadata.recent_files[0]
if not os.path.isfile(file_path):
file_path = None
metadata.recent_files.pop(0)
return file_path

@staticmethod
def put_recent_file(metadata: Metadata, file_path: string):
"""Move recent file to top, or add new"""
if file_path in metadata.recent_files:
metadata.recent_files.remove(file_path)
metadata.recent_files.insert(0, file_path)
if len(metadata.recent_files) > 10:
metadata.recent_files.pop()

@staticmethod
def put_recent_project(metadata: Metadata, project: Project, file_path: string):
"""Move recent project to top or add new"""
Expand Down
2 changes: 2 additions & 0 deletions src/const/font_awesome_icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ class FontAwesomeIcons:
magnifying_glass = '\uf002'
rotate = '\uf2f1'
download = '\uf019'
link = '\uf0c1'
link_broken = '\uf127'
2 changes: 1 addition & 1 deletion src/const/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
HELVETICA_FONT_NAME = "Helvetica"
BOLD_FONT = "bold"
TEXT_SIZE_SECTION_HEADER = 12
TEXT_SIZE_MENU = 10
TEXT_SIZE_MENU = 9
TEXT_SIZE_BOTTOM_BAR = 10
4 changes: 1 addition & 3 deletions src/domain/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def __init__(self, data=None):
"""
if data is None:
data = {}
self.recent_files = data.get('recent_files', [])
self.recent_projects = []
for rp in data.get('recent_projects', []):
self.recent_projects.append(RecentProject(rp))
Expand All @@ -23,13 +22,12 @@ def to_dict(self):
:return: Dictionary containing the current state of the object.
"""
return {
"recent_files": self.recent_files,
"recent_projects": recent_projects_maps,
}


class RecentProject:
"""Representatin of recept project saved in Metadata.
"""Representation of recept project saved in Metadata.
Used to display table of recent projects in project selector window"""

def __init__(self, data=None):
Expand Down
55 changes: 54 additions & 1 deletion src/gui/gui_utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,68 @@
import os

from PIL import ImageFont, ImageDraw, Image, ImageTk
import tkinter as tk
from tkinter import messagebox, ttk

from src.backend.run_context import RunContext
from src.backend.service.metadata_service import MetadataService
from src.backend.service.project_service import ProjectService
from src.const.paths import METADATA_FILE_PATH
from src.domain.metadata import RecentProject
from src.domain.project import Project


class GuiUtils:
@staticmethod
def is_child_of(parent, widget):
""" Recursively check if widget is child of parent """
while widget:
if widget == parent:
return True
widget = widget.master
return False

@staticmethod
def fa_image(font, background, foreground, char, size, padding=2):
return ImageTk.PhotoImage(GuiUtils.fa_image_raw(font, background, foreground, char, size, padding))

@staticmethod
def fa_image_raw(font, background, foreground, char, size, padding=2):
img = Image.new("RGBA", (size, size), background)
draw = ImageDraw.Draw(img)
font_awesome = ImageFont.truetype(font, size - (padding * 2))
draw.text((padding, padding), char, foreground, font_awesome)
return ImageTk.PhotoImage(img)
return img

@staticmethod
def merge_icons(icon1: Image, icon2: Image, space_between=2) -> Image:
"""Merge two icons into one"""
width = icon1.width + icon2.width + space_between
height = max(icon1.height, icon2.height)
merged_image = Image.new("RGBA", (width, height))
merged_image.paste(icon1, (0, 0))
merged_image.paste(icon2, (icon1.width + space_between, 0))
return merged_image

@staticmethod
def open_recent_project(project: RecentProject, e=None):
if project.path is None or not os.path.isfile(project.path):
metadata = MetadataService.load(METADATA_FILE_PATH)
MetadataService.remove_recent_project(metadata, project.path)
MetadataService.save(metadata, METADATA_FILE_PATH)
messagebox.showerror("Nie je možné otvoriť projekt", "Projekt bol pravdepodobne zmazaný, alebo presnutý.")
return False
return GuiUtils.open_project(ProjectService.load(project.path))

@staticmethod
def open_project(project: Project):
metadata = MetadataService.load(METADATA_FILE_PATH)
MetadataService.put_recent_project(metadata, project, project.path)
MetadataService.save(metadata, METADATA_FILE_PATH)
ctx = RunContext()
ctx.project = project
ctx.current_file = None
return True

@staticmethod
def stylename_elements_options(stylename):
Expand Down
Loading

0 comments on commit baad145

Please sign in to comment.