import os import functools from enum import Enum from PyQt5.QtWidgets import QDialog, QVBoxLayout, QWidget, QCommandLinkButton, QListWidgetItem from PyQt5.QtGui import QPixmap from PyQt5.QtCore import Qt from PyQt5 import uic from util import str_compare from ui.welcome.recent_project import RecentProjectWidget from ui.welcome.welcome_dialog_ui import Ui_WelcomeDialog class WelcomeDialogResult(Enum): NewProject = 0 OpenProject = 1 OpenRecentProject = 2 class WelcomeDialog(QDialog): def __init__(self, projectManager): super().__init__() self.__projectManager = projectManager self.__resultAction = None self.setResult(QDialog.Rejected) self.setupUi() self.setupActions() self.populateRecentProjects() def setupUi(self): self.ui = Ui_WelcomeDialog() self.ui.setupUi(self) self.image = QPixmap(self.ui.picture.pixmap()) def setupActions(self): self.ui.buttonNewProject.pressed.connect(self.newProjectPressed) self.ui.buttonOpenProject.pressed.connect(self.openProjectPressed) def resizeEvent(self, event): super().resizeEvent(event) picSize = self.ui.picture.size() pic = self.image.scaled(picSize, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation) self.ui.picture.setPixmap(pic) def populateRecentProjects(self): projects = list(self.__projectManager.getRecentProjects()) projects = sorted(projects, key=lambda x: (x['pinned'], x['date']), reverse=True) for project in projects: item = QListWidgetItem(project['name']) item.setData(Qt.UserRole, project) widget = RecentProjectWidget(project) item.setSizeHint(widget.sizeHint()) self.ui.listRecentProjects.addItem(item) self.ui.listRecentProjects.setItemWidget(item, widget) def newProjectPressed(self): self.__resultAction = WelcomeDialogResult.NewProject self.accept() def openProjectPressed(self): self.__resultAction = WelcomeDialogResult.OpenProject self.accept()