This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavatar_chooser.py
118 lines (100 loc) · 4.11 KB
/
avatar_chooser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
from PyQt4 import uic
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import avatar_prechooser
import util
import cyemussa
ym = cyemussa.CyEmussa.Instance()
class AvatarChooser(QObject):
def __init__(self, app):
QObject.__init__(app)
super(AvatarChooser, self).__init__()
self.app = app
self.avatars = {}
self.widget = uic.loadUi('ui/avatar_chooser.ui')
self.widget.setWindowFlags(self.widget.windowFlags() | Qt.Popup)
self.widget.label_avatar.setPixmap(QPixmap(self.app.me.avatar.image))
self.widget.button_delete.setEnabled(False)
self.widget.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False)
self.widget.avatarListWidget.currentItemChanged.connect(
self._itemChanged
)
self.widget.avatarListWidget.itemDoubleClicked.connect(self._submit)
self.widget.button_browse.clicked.connect(self._browseFiles)
self.widget.button_delete.clicked.connect(self._deleteAvatar)
self._listIcons()
def _listIcons(self):
paths = [
'{0}/ui/resources/avatars/'.format(QDir().currentPath()),
'{0}/avatars/'.format(QDesktopServices.storageLocation(
QDesktopServices.DataLocation
))
]
files = []
for path in paths:
if os.path.exists(path):
path_files = os.listdir(path)
for f in path_files:
full_path = '{0}/{1}'.format(path, f)
if not os.path.isdir(full_path):
if QImageReader().imageFormat(full_path):
files.append(full_path)
for avatar in files:
icon = QIcon(util.scalePixmapAspectFill(
QPixmap(avatar),
QSize(64, 64))
)
item = QListWidgetItem(self.widget.avatarListWidget)
item.setIcon(icon)
item.avatarPath = avatar
self.widget.avatarListWidget.addItem(item)
def _itemChanged(self, item, prev_item):
if item:
self.widget.buttonBox.button(QDialogButtonBox.Ok).setEnabled(True)
else:
self.widget.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False)
pixmap = util.scalePixmapAspectFill(
QPixmap(item.avatarPath),
QSize(96, 96)
)
self.widget.label_avatar.setPixmap(pixmap)
fileInfo = QFileInfo(item.avatarPath)
if QFileInfo(fileInfo.absoluteDir().absolutePath()).isWritable():
self.widget.button_delete.setEnabled(True)
else:
self.widget.button_delete.setEnabled(False)
def _browseFiles(self):
self.fileDialog = QFileDialog(self.widget, Qt.Dialog)
self.fileDialog.setAcceptMode(QFileDialog.AcceptOpen)
self.fileDialog.setFileMode(QFileDialog.ExistingFile)
self.fileDialog.setNameFilter('Image files (*.png *.xpm *.jpg)')
self.fileDialog.fileSelected.connect(self._addAvatar)
self.fileDialog.show()
def _addAvatar(self, avatar):
storagePath = QDesktopServices.storageLocation(
QDesktopServices.DataLocation
)
if not os.path.exists('{0}/avatars/'.format(storagePath)):
QDir().mkpath('{0}/avatars/'.format(storagePath))
QFile.copy(avatar, '{0}/avatars/{1}'.format(
storagePath,
QFileInfo(avatar).fileName())
)
pixmap = util.scalePixmapAspectFill(QPixmap(avatar), QSize(64, 64))
icon = QIcon(pixmap)
item = QListWidgetItem(self.widget.avatarListWidget)
item.setIcon(icon)
item.avatarPath = avatar
self.widget.avatarListWidget.addItem(item)
self.fileDialog = None
def _deleteAvatar(self):
item = self.widget.avatarListWidget.currentItem()
QFile.remove(item.avatarPath)
self.widget.avatarListWidget.takeItem(
self.widget.avatarListWidget.row(item)
)
if self.widget.avatarListWidget.count():
self.widget.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False)
def _submit(self):
self.widget.accept()