import traceback from PyQt5.QtWidgets import QDialog, QCommandLinkButton, QListWidgetItem from PyQt5.QtGui import QPixmap from PyQt5.QtCore import Qt from ui.welcome.recent_project import RecentProjectWidget from ui.welcome.welcome_dialog_ui import Ui_WelcomeDialog class WelcomeDialog(QDialog): NEW_PROJECT = 0 OPEN_PROJECT = 1 def __init__(self, projectManager): super().__init__() self.__projectManager = projectManager self.resultAction = None self.projectToOpen = 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) self.ui.listRecentProjects.currentItemChanged.connect(self.listRecentProjectsCurrentChanged) self.ui.listRecentProjects.itemActivated.connect(self.listRecentProjectsItemActivated) 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: widget = RecentProjectWidget(project) widget.pinned.connect(self.__projectPinned) widget.deleted.connect(self.__projectDeleted) item = QListWidgetItem(project['name']) item.setData(Qt.UserRole, project) item.setSizeHint(widget.sizeHint()) self.ui.listRecentProjects.addItem(item) self.ui.listRecentProjects.setItemWidget(item, widget) def newProjectPressed(self): self.resultAction = WelcomeDialog.NEW_PROJECT self.accept() def openProjectPressed(self): # TODO: show open dialog self.resultAction = WelcomeDialog.OPEN_PROJECT self.accept() def listRecentProjectsCurrentChanged(self, current, previous): if not current is None: recentProjectWidget = self.ui.listRecentProjects.itemWidget(current) recentProjectWidget.setSelected(True) if not previous is None: recentProjectWidget = self.ui.listRecentProjects.itemWidget(previous) if not recentProjectWidget is None: recentProjectWidget.setSelected(False) def listRecentProjectsItemActivated(self, item): project = item.data(Qt.UserRole) self.resultAction = WelcomeDialog.OPEN_PROJECT self.projectToOpen = project['path'] self.accept() def __projectPinned(self, isPinned): project = self.ui.listRecentProjects.currentItem().data(Qt.UserRole) self.__projectManager.pinRecentProject(project, isPinned) def __projectDeleted(self): project = self.ui.listRecentProjects.currentItem().data(Qt.UserRole) try: self.__projectManager.deleteRecentProject(project) self.ui.listRecentProjects.takeItem(self.ui.listRecentProjects.currentRow()) except: print("Error while deleting! ") traceback.print_exc()