2018-08-01 00:02:18 +03:00
|
|
|
import traceback
|
2018-07-28 23:56:23 +03:00
|
|
|
|
2018-08-01 00:02:18 +03:00
|
|
|
from PyQt5.QtWidgets import QDialog, QCommandLinkButton, QListWidgetItem
|
2018-07-28 23:56:23 +03:00
|
|
|
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):
|
2018-08-01 00:02:18 +03:00
|
|
|
NEW_PROJECT = 0
|
|
|
|
OPEN_PROJECT = 1
|
2018-07-28 23:56:23 +03:00
|
|
|
|
|
|
|
def __init__(self, projectManager):
|
|
|
|
super().__init__()
|
|
|
|
self.__projectManager = projectManager
|
2018-08-01 00:02:18 +03:00
|
|
|
self.resultAction = None
|
|
|
|
self.projectToOpen = None
|
2018-07-28 23:56:23 +03:00
|
|
|
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)
|
2018-08-01 00:02:18 +03:00
|
|
|
self.ui.listRecentProjects.currentItemChanged.connect(self.listRecentProjectsCurrentChanged)
|
|
|
|
self.ui.listRecentProjects.itemActivated.connect(self.listRecentProjectsItemActivated)
|
2018-07-28 23:56:23 +03:00
|
|
|
|
|
|
|
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:
|
2018-08-01 00:02:18 +03:00
|
|
|
|
|
|
|
widget = RecentProjectWidget(project)
|
|
|
|
widget.pinned.connect(self.__projectPinned)
|
|
|
|
widget.deleted.connect(self.__projectDeleted)
|
|
|
|
|
2018-07-28 23:56:23 +03:00
|
|
|
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):
|
2018-08-01 00:02:18 +03:00
|
|
|
self.resultAction = WelcomeDialog.NEW_PROJECT
|
2018-07-28 23:56:23 +03:00
|
|
|
self.accept()
|
|
|
|
|
|
|
|
def openProjectPressed(self):
|
2018-08-01 00:02:18 +03:00
|
|
|
# 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()
|