1
1
import sys
2
2
from akari .utils import loadDB
3
3
from PyQt5 .QtCore import QSize
4
- from PyQt5 .QtGui import QIcon , QPixmap
5
- from PyQt5 .QtWidgets import QApplication , QWidget , QPushButton , QListWidget , QListWidgetItem , QListView , QHBoxLayout , QVBoxLayout
4
+ from PyQt5 .QtGui import QIcon , QColor , QFont , QPixmap
5
+ from PyQt5 .QtWidgets import QApplication , QWidget , QPushButton , QListWidget , QListWidgetItem , QListView , QHBoxLayout , QVBoxLayout , QAbstractItemView , QMenu
6
6
7
7
class Main (QWidget ):
8
8
def __init__ (self ):
9
9
super ().__init__ ()
10
10
self .setWindowTitle ('akari' )
11
11
self .setWindowIcon (QIcon ('static/icon.jpg' ))
12
+ self .db = None
12
13
13
14
self .mainLayout = QHBoxLayout ()
14
15
15
16
self .sidePanel = QVBoxLayout ()
16
17
self .tagList = QListWidget ()
17
18
self .filerButton = QPushButton ("Filter" )
19
+ self .clearFilerButton = QPushButton ("Reset" )
18
20
self .sidePanel .addWidget (self .tagList )
19
21
20
22
self .imagePanel = QVBoxLayout ()
@@ -32,37 +34,95 @@ def __init__(self):
32
34
33
35
def initUI (self ):
34
36
self .sidePanel .addWidget (self .filerButton , 1 )
37
+ self .sidePanel .addWidget (self .clearFilerButton , 1 )
38
+ self .tagList .setSelectionMode (QAbstractItemView .ExtendedSelection )
35
39
36
40
self .imageList .setViewMode (QListView .IconMode )
37
41
self .imageList .setIconSize (QSize (300 ,300 ))
42
+ self .imageList .installEventFilter (self )
38
43
39
44
self .imagePanel .addLayout (self .imagePanelOptions )
40
45
self .imagePanelOptions .addWidget (self .miscButton1 )
41
46
self .imagePanelOptions .addWidget (self .miscButton2 )
42
47
48
+ self .addEventHandlers ()
43
49
self .show ()
44
50
45
- def addImages (self , db ):
46
- for imgPath in db :
51
+ def updateDB (self , db ):
52
+ self .db = db
53
+
54
+ def addEventHandlers (self ):
55
+ self .filerButton .clicked .connect (self .filterButton_pressed )
56
+ self .clearFilerButton .clicked .connect (self .clearFilterButton_pressed )
57
+
58
+ def filterButton_pressed (self ):
59
+ selected_items = self .tagList .selectedItems ()
60
+ selected_tags = []
61
+ filtered_images = []
62
+ for i in range (len (selected_items )):
63
+ selected_tags .append (str (self .tagList .selectedItems ()[i ].text ()).split (" " )[1 ])
64
+ for imgPath in self .db :
47
65
if imgPath == 'akari-tags' :
48
66
continue
67
+ if all (elem in self .db [imgPath ] for elem in selected_tags ):
68
+ if imgPath not in filtered_images :
69
+ filtered_images .append (imgPath )
70
+ self .addImages (filtered_images )
71
+
72
+ def clearFilterButton_pressed (self ):
73
+ imgList = self .getImgList ()
74
+ self .addImages (imgList )
75
+ self .addTags ()
76
+
77
+ def viewMetadata (self ):
78
+ selected_image = self .imageList .selectedItems ()[0 ].data (0 )
79
+ tags = self .db [selected_image ]
80
+ self .tagList .clear ()
81
+ self .tagList .addItems (tags )
82
+ self .addImages ([selected_image ])
83
+
84
+ def contextMenuEvent (self , event ):
85
+ menu = QMenu (self )
86
+ if self .imageList .selectedItems () != []:
87
+ viewMetadata = menu .addAction ("View Metadata" )
88
+ action = menu .exec_ (self .mapToGlobal (event .pos ()))
89
+ if action == viewMetadata :
90
+ self .viewMetadata ()
91
+
92
+ def getImgList (self ):
93
+ imgList = []
94
+ for imgPath in self .db :
95
+ if imgPath == 'akari-tags' :
96
+ continue
97
+ imgList .append (imgPath )
98
+ return imgList
99
+
100
+ def addImages (self , imgList ):
101
+ self .imageList .clear ()
102
+ for imgPath in imgList :
49
103
item = QListWidgetItem ()
50
104
icon = QIcon ()
51
105
pixmap = QPixmap (imgPath )
52
106
icon .addPixmap (pixmap )
107
+ item .setFont (QFont ("Times" ,1 ))
108
+ item .setForeground (QColor ("white" ))
109
+ item .setText (imgPath )
53
110
item .setIcon (icon )
54
111
self .imageList .addItem (item )
55
112
56
- def addTags (self , db ):
113
+ def addTags (self ):
114
+ self .tagList .clear ()
57
115
tagList = []
58
- for tag in sorted (db ['akari-tags' ], key = db ['akari-tags' ].get , reverse = True ):
59
- tagList .append (str (db ['akari-tags' ][tag ]) + ' ' + tag )
116
+ for tag in sorted (self . db ['akari-tags' ], key = self . db ['akari-tags' ].get , reverse = True ):
117
+ tagList .append (str (self . db ['akari-tags' ][tag ]) + ' ' + tag )
60
118
self .tagList .addItems (tagList )
61
119
62
120
def init_gui ():
63
121
db = loadDB ()
64
122
app = QApplication (sys .argv )
65
123
main = Main ()
66
- main .addTags (db )
67
- main .addImages (db )
124
+ main .updateDB (db )
125
+ main .addTags ()
126
+ imgList = main .getImgList ()
127
+ main .addImages (imgList )
68
128
sys .exit (app .exec_ ())
0 commit comments