1
- from PySide2 .QtWidgets import QWidget , QLabel , QHBoxLayout , QLineEdit , QSizePolicy
2
- from PySide2 .QtGui import QPixmap , QMouseEvent
3
- from PySide2 .QtCore import Qt , QSize
4
- import sys
1
+ import os
2
+
3
+ from PySide2 .QtWidgets import QWidget , QLabel , QHBoxLayout , QLineEdit , QSizePolicy , QVBoxLayout
4
+ from PySide2 .QtGui import QPixmap , QMouseEvent , QKeyEvent , QDesktopServices
5
+ from PySide2 .QtCore import Qt , QSize , Slot , QUrl
5
6
import time
6
7
import random
7
8
import file_walker
13
14
14
15
class ImageWidget (QWidget ):
15
16
def __init__ (self ):
17
+ self .default_title = "Amit's Image Viewer"
16
18
QWidget .__init__ (self )
17
- self ._layout = QHBoxLayout ()
18
- self ._image_label = QLabel ()
19
- self ._size_method = None
19
+ self ._layout = QVBoxLayout ()
20
+ self ._image_label = QLabel (self )
21
+ self ._image_label .setSizePolicy (QSizePolicy .Ignored , QSizePolicy .Ignored )
22
+ self ._image_label .setScaledContents (True )
23
+ self .is_full_screen : bool = False
24
+ # self._size_method = None
20
25
self ._seed = time .time ()
21
- self ._line_edit : QLineEdit = None
22
- self .setSizePolicy (QSizePolicy .Maximum , QSizePolicy .Maximum )
26
+ # self._line_edit: QLineEdit = None
27
+ # self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
23
28
24
29
# self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Minimum)
25
30
# self.sizeHint()
26
31
random .seed (self ._seed )
27
32
28
33
# self._all_images: List = file_walker.walk(sys.argv[1])
29
- self ._all_images : List = file_walker .walk ()
30
- self ._current_index = 0
31
- self .image_shuffle ()
34
+
32
35
# self._set_image(self._current_index)
33
36
self ._layout .addWidget (self ._image_label )
34
37
self .setLayout (self ._layout )
38
+ self .initialize_images ()
39
+
40
+ def initialize_images (self , mode = None ):
41
+ self ._all_images : List = file_walker .walk (mode )
42
+ self ._current_index = 0
43
+ self .image_shuffle ()
44
+
45
+
46
+
47
+ def is_image_landscape (self , image : QPixmap ):
48
+ if image .width ()/ image .height () > 1 :
49
+ return True
50
+ else :
51
+ return False
35
52
36
53
def image_shuffle (self ):
37
54
l .log ("shuffle" )
@@ -57,58 +74,90 @@ def image_delete(self):
57
74
send2trash (self ._all_images [self ._current_index ])
58
75
# self._all_images: List = file_walker.walk(sys.argv[1])
59
76
self ._all_images : List = file_walker .walk ()
60
- self .image_shuffle ()
77
+ self ._set_image (self ._current_index )
78
+ # self.image_shuffle()
61
79
62
80
def _set_image (self , index ):
63
81
if index > len (self ._all_images )- 1 :
64
- print ( "Oops " )
82
+ l . log ( "error: shuffling again " )
65
83
self .image_shuffle ()
66
84
l .log ("setting image" )
85
+
67
86
image_pix_map = QPixmap (self ._all_images [index ])
87
+
88
+
89
+ # if self.is_image_landscape(image_pix_map):
90
+ # self._image_label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Ignored)
91
+ # else:
92
+ # self._image_label.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Fixed)
68
93
print ("image: " , image_pix_map .width (), image_pix_map .height ())
69
94
70
95
# image_pix_map = image_pix_map.scaled(900, 900, Qt.KeepAspectRatio)
71
96
# print(self.width(), self.height())
72
- window_height = bpg ._window_height
73
- window_width = bpg ._window_width
74
- print ("window_size At set_image: " , window_width , window_height )
75
- if image_pix_map .width () < window_width and image_pix_map .height () < window_height :
76
- if image_pix_map .width () < bpg .screen_width and image_pix_map .height () < bpg .screen_height :
77
- print ("image size is smaller than window in all ways." )
78
- else :
79
- image_pix_map = image_pix_map .scaled (
80
- bpg .screen_width - bpg .width_spacing , bpg .screen_height - bpg .height_spacing , Qt .KeepAspectRatio )
81
- else :
82
- print ("image size {} won't fit in window {}, {}, scaling" .format (
83
- image_pix_map .size (), window_width , window_height ))
84
- image_pix_map = image_pix_map .scaled (
85
- window_width - bpg .width_spacing , window_height - bpg .height_spacing , Qt .KeepAspectRatio )
86
- # window_ratio = window_width/window_height
87
- # image_ratio = image_pix_map.width()/image_pix_map.height()
88
- # print("ratios: ", window_ratio, image_ratio)
89
- # if window_ratio < 1:
90
- # l.log("potrait mode")
91
- # image_pix_map = image_pix_map.scaledToHeight(window_height-200, Qt.FastTransformation)
92
- # else:
93
- # l.log("landscape mode")
94
- # image_pix_map = image_pix_map.scaledToWidth(window_width-90, Qt.FastTransformation)
95
- # quit()
96
- # image_pix_map = image_pix_map.scaledToWidth(window_width-50, Qt.FastTransformation)
97
- print ("image: " , image_pix_map .width (), image_pix_map .height ())
98
97
99
98
#
100
99
self ._image_label .setPixmap (image_pix_map )
101
- if self ._line_edit :
102
- self ._line_edit .setText (self ._all_images [index ])
100
+ # if self._line_edit:
101
+ # self._line_edit.setText(self._all_images[index])
103
102
104
- def set_line_edit (self , edit_line : QLineEdit ):
105
- self ._line_edit = edit_line
103
+ # def set_line_edit(self, edit_line: QLineEdit):
104
+ # self._line_edit = edit_line
106
105
107
106
def mousePressEvent (self , event : QMouseEvent ):
108
107
if event .button () == Qt .MouseButton .LeftButton :
109
108
self .image_next ()
110
109
elif event .button () == Qt .MouseButton .RightButton :
111
110
self .image_previous ()
112
111
112
+ @Slot ()
113
+ def _browse_event (self ):
114
+ print (os .path .dirname (self .get_current_image_path_str ()))
115
+ arguments : str = os .path .dirname (
116
+ self .get_current_image_path_str ())
117
+ url_path = QUrl .fromLocalFile (arguments )
118
+ print (url_path )
119
+ QDesktopServices .openUrl (url_path )
120
+
121
+ def set_title (self , image_path ):
122
+ new_title = self .default_title + image_path
123
+ l .log ("setting title: " + new_title )
124
+ self .setWindowTitle (new_title )
125
+
126
+
127
+ def keyReleaseEvent (self , event : QKeyEvent ):
128
+ l .log ("Key event at top_level_widget: " + str (event .key ()))
129
+ if event .key () == Qt .Key_S :
130
+ l .log ("Key S" )
131
+
132
+ self .image_shuffle ()
133
+ elif event .key () == Qt .Key_Left or event .key () == Qt .Key_Backspace or event .key () == Qt .Key_P :
134
+ l .log ("Key left or backspace" )
135
+ self .image_previous ()
136
+ elif event .key () == Qt .Key_Right or event .key () == Qt .Key_N or event .key () == Qt .Key_Space :
137
+ l .log ("Key Right / N / Space" )
138
+ self .image_next ()
139
+ elif event .key () == Qt .Key_Delete :
140
+ l .log ("Key Delete" )
141
+ self .image_delete ()
142
+ elif event .key () == Qt .Key_F :
143
+ l .log ("Key F" )
144
+ if self .is_full_screen :
145
+ self .showNormal ()
146
+ else :
147
+ self .showFullScreen ()
148
+ # toggle
149
+ self .is_full_screen = not self .is_full_screen
150
+ elif event .key () == Qt .Key_B :
151
+ l .log ("Key B" )
152
+ self ._browse_event ()
153
+ elif event .key () == Qt .Key_1 or event .key () == Qt .Key_L :
154
+ l .log ("Key 1 / L --> landscape mode" )
155
+ self .initialize_images ("landscape/" )
156
+ elif event .key () == Qt .Key_2 or event .key () == Qt .Key_P :
157
+ l .log ("Key 2 / P --> Portrait mode" )
158
+ self .initialize_images ("portrait/" )
159
+ self .setFocus ()
160
+ self .set_title (self .get_current_image_path_str ())
161
+
113
162
def get_current_image_path_str (self ) -> str :
114
163
return self ._all_images [self ._current_index ]
0 commit comments