Worked on welcome dialog implementation.

This commit is contained in:
Tiberiu Chibici 2018-08-01 00:02:18 +03:00
parent ac582a8b0b
commit 59cd033cad
6 changed files with 127 additions and 46 deletions

12
.vscode/launch.json vendored
View File

@ -4,6 +4,12 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{
"name": "Python: main.py",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/main.py"
},
{ {
"name": "Python: Current File", "name": "Python: Current File",
"type": "python", "type": "python",
@ -20,11 +26,5 @@
"secret": "my_secret", "secret": "my_secret",
"host": "localhost" "host": "localhost"
}, },
{
"name": "Python: main.py",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/main.py"
},
] ]
} }

View File

@ -4,39 +4,46 @@ class ProjectManager(object):
def __init__(self, appDataStorage): def __init__(self, appDataStorage):
self.__appDataStorage = appDataStorage self.__appDataStorage = appDataStorage
self.__recentProjects = [] self.__recentProjects = None
def getRecentProjects(self): def getRecentProjects(self):
if self.__recentProjects is None: if self.__recentProjects is None:
self.__recentProjects = list(self.__appDataStorage.readRecentProjects()) self.__recentProjects = {}
return self.__recentProjects for item in self.__appDataStorage.readRecentProjects():
self.__recentProjects[item['path']] = item
return self.__recentProjects.values()
def pinRecentProject(self, entry, isPinned = True): def pinRecentProject(self, entry, isPinned = True):
entry['pinned'] = isPinned entry['pinned'] = isPinned
self.__appDataStorage.writeRecentProjects(self.__recentProjects) self.__appDataStorage.writeRecentProjects(self.__recentProjects.values())
def deleteRecentProject(self, entry):
self.__recentProjects.pop(entry['path'], None)
self.__appDataStorage.writeRecentProjects(self.__recentProjects.values())
def debug_populateRecentProjects(self): def debug_populateRecentProjects(self):
self.__recentProjects.append({ self.__recentProjects['/home/tibi/Videos/project.pro'] = {
'name' : 'Debug project', 'name' : 'Debug project',
'path' : '/home/tibi/Videos/project.pro', 'path' : '/home/tibi/Videos/project.pro',
'pinned' : True, 'pinned' : True,
'date' : 1 'date' : 1
}) }
self.__recentProjects.append({ self.__recentProjects['/home/tibi/Videos/project2.pro'] = {
'name' : 'Debug project 2', 'name' : 'Debug project 2',
'path' : '/home/tibi/Videos/project2.pro', 'path' : '/home/tibi/Videos/project2.pro',
'pinned' : False, 'pinned' : False,
'date' : 2 'date' : 2
}) }
self.__recentProjects.append({ self.__recentProjects['/home/tibi/Videos/project3.pro'] = {
'name' : 'Debug project 3', 'name' : 'Debug project 3',
'path' : '/home/tibi/Videos/project3.pro', 'path' : '/home/tibi/Videos/project3.pro',
'pinned' : False, 'pinned' : False,
'date' : 3 'date' : 3
}) }
self.__recentProjects.append({ self.__recentProjects['/home/tibi/Videos/project4.pro'] = {
'name' : 'Debug project 4', 'name' : 'Debug project 4',
'path' : '/home/tibi/Videos/project4.pro', 'path' : '/home/tibi/Videos/project4.pro',
'pinned' : False, 'pinned' : False,
'date' : 4 'date' : 4
}) }

View File

@ -30,15 +30,20 @@ class AppDataStorage(object):
def writeSettings(self, settings): def writeSettings(self, settings):
pass # todo: finish this pass # todo: finish this
def __parseRow(self, row):
row['date'] = int(row['date'])
row['pinned'] = row['pinned'].lower() in ['true', 'yes', '1', 'on']
return row
def readRecentProjects(self): def readRecentProjects(self):
if (os.path.exists(self.__recentProjectsPath)): if (os.path.exists(self.__recentProjectsPath)):
with open(self.__recentProjectsPath, 'rb') as recentProjectsFile: with open(self.__recentProjectsPath, 'r') as recentProjectsFile:
csvreader = csv.DictReader(recentProjectsFile, fieldnames=AppDataStorage.RECENT_PROJECTS_FIELDS) csvreader = csv.DictReader(recentProjectsFile, fieldnames=AppDataStorage.RECENT_PROJECTS_FIELDS)
for row in csvreader: for row in csvreader:
yield row yield self.__parseRow(row)
def writeRecentProjects(self, items): def writeRecentProjects(self, items):
self.__createPath(self.__recentProjectsPath) self.__createPath(self.__recentProjectsPath)
with open(self.__recentProjectsPath, 'wb') as recentProjectsFile: with open(self.__recentProjectsPath, 'w') as recentProjectsFile:
csvwriter = csv.DictWriter(recentProjectsFile, AppDataStorage.RECENT_PROJECTS_FIELDS) csvwriter = csv.DictWriter(recentProjectsFile, AppDataStorage.RECENT_PROJECTS_FIELDS)
csvwriter.writerows(items) csvwriter.writerows(items)

View File

@ -17,7 +17,7 @@ class EmberApplication(QApplication):
# setup resources # setup resources
appDataStorage = AppDataStorage() appDataStorage = AppDataStorage()
projectManager = ProjectManager(appDataStorage) projectManager = ProjectManager(appDataStorage)
projectManager.debug_populateRecentProjects() #projectManager.debug_populateRecentProjects()
# show main window # show main window
mainwindow = MainWindow(projectManager) mainwindow = MainWindow(projectManager)

View File

@ -1,11 +1,20 @@
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QToolButton, QLayout from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QToolButton, QLayout
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import QFont from PyQt5.QtGui import QFont
class RecentProjectWidget(QWidget): class RecentProjectWidget(QWidget):
deleted = pyqtSignal()
pinned = pyqtSignal(bool)
def __init__(self, recentProject): def __init__(self, recentProject):
super().__init__() super().__init__()
self.project = recentProject
self.__selected = False
self.setupUi() self.setupUi()
self.setProject(recentProject) self.setupActions()
self.__setProject(recentProject)
self.setSelected(False)
def setupUi(self): def setupUi(self):
layout = QHBoxLayout() layout = QHBoxLayout()
@ -18,16 +27,46 @@ class RecentProjectWidget(QWidget):
layout.addStretch() layout.addStretch()
# pin button # pin button
self.__button = QToolButton() self.__pin_button = QToolButton()
self.__button.setFont(QFont("Font Awesome 5 Free")) self.__pin_button.setFont(QFont("Font Awesome 5 Free"))
self.__button.setText("\uf08d") self.__pin_button.setText("\uf08d")
self.__button.setCheckable(True) self.__pin_button.setCheckable(True)
layout.addWidget(self.__button) layout.addWidget(self.__pin_button)
# delete button
self.__del_button = QToolButton()
self.__del_button.setFont(QFont("Font Awesome 5 Free"))
self.__del_button.setText("\uf2ed")
layout.addWidget(self.__del_button)
# pin indicator
self.__pin_indicator = QLabel()
self.__pin_indicator.setFont(QFont("Font Awesome 5 Free"))
self.__pin_indicator.setText("\uf08d")
layout.addWidget(self.__pin_indicator)
# done # done
layout.setSizeConstraint(QLayout.SetMinimumSize) layout.setSizeConstraint(QLayout.SetMinimumSize)
self.setLayout(layout) self.setLayout(layout)
def setProject(self, project): def setupActions(self):
self.__pin_button.toggled.connect(self.__pinnedToggled)
self.__del_button.pressed.connect(self.deleted)
def setSelected(self, selected = True):
self.__selected = selected
self.__pin_button.setVisible(selected)
self.__del_button.setVisible(selected)
self.__pin_indicator.setVisible(not self.__selected and self.__pin_button.isChecked())
def setPinned(self, isPinned = True):
self.__pin_button.setChecked(isPinned)
def __setProject(self, project):
self.__text = project['name'] self.__text = project['name']
self.__button.setChecked(project['pinned']) self.setPinned(project['pinned'])
def __pinnedToggled(self, isPinned):
self.__pin_indicator.setVisible(not self.__selected and isPinned)
if (isPinned != self.project['pinned']):
self.pinned.emit(isPinned)

View File

@ -1,27 +1,21 @@
import os import traceback
import functools
from enum import Enum
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QWidget, QCommandLinkButton, QListWidgetItem from PyQt5.QtWidgets import QDialog, QCommandLinkButton, QListWidgetItem
from PyQt5.QtGui import QPixmap from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5 import uic
from util import str_compare
from ui.welcome.recent_project import RecentProjectWidget from ui.welcome.recent_project import RecentProjectWidget
from ui.welcome.welcome_dialog_ui import Ui_WelcomeDialog from ui.welcome.welcome_dialog_ui import Ui_WelcomeDialog
class WelcomeDialogResult(Enum):
NewProject = 0
OpenProject = 1
OpenRecentProject = 2
class WelcomeDialog(QDialog): class WelcomeDialog(QDialog):
NEW_PROJECT = 0
OPEN_PROJECT = 1
def __init__(self, projectManager): def __init__(self, projectManager):
super().__init__() super().__init__()
self.__projectManager = projectManager self.__projectManager = projectManager
self.__resultAction = None self.resultAction = None
self.projectToOpen = None
self.setResult(QDialog.Rejected) self.setResult(QDialog.Rejected)
self.setupUi() self.setupUi()
self.setupActions() self.setupActions()
@ -35,6 +29,8 @@ class WelcomeDialog(QDialog):
def setupActions(self): def setupActions(self):
self.ui.buttonNewProject.pressed.connect(self.newProjectPressed) self.ui.buttonNewProject.pressed.connect(self.newProjectPressed)
self.ui.buttonOpenProject.pressed.connect(self.openProjectPressed) 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): def resizeEvent(self, event):
super().resizeEvent(event) super().resizeEvent(event)
@ -48,18 +44,52 @@ class WelcomeDialog(QDialog):
projects = sorted(projects, key=lambda x: (x['pinned'], x['date']), reverse=True) projects = sorted(projects, key=lambda x: (x['pinned'], x['date']), reverse=True)
for project in projects: for project in projects:
widget = RecentProjectWidget(project)
widget.pinned.connect(self.__projectPinned)
widget.deleted.connect(self.__projectDeleted)
item = QListWidgetItem(project['name']) item = QListWidgetItem(project['name'])
item.setData(Qt.UserRole, project) item.setData(Qt.UserRole, project)
widget = RecentProjectWidget(project)
item.setSizeHint(widget.sizeHint()) item.setSizeHint(widget.sizeHint())
self.ui.listRecentProjects.addItem(item) self.ui.listRecentProjects.addItem(item)
self.ui.listRecentProjects.setItemWidget(item, widget) self.ui.listRecentProjects.setItemWidget(item, widget)
def newProjectPressed(self): def newProjectPressed(self):
self.__resultAction = WelcomeDialogResult.NewProject self.resultAction = WelcomeDialog.NEW_PROJECT
self.accept() self.accept()
def openProjectPressed(self): def openProjectPressed(self):
self.__resultAction = WelcomeDialogResult.OpenProject # TODO: show open dialog
self.accept() 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()