-
Notifications
You must be signed in to change notification settings - Fork 0
/
controls.py
278 lines (232 loc) · 8.98 KB
/
controls.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from enum import Enum
from PyQt5.QtCore import Qt, QSize, QRect, QPoint, pyqtSignal, QObject
from PyQt5.QtGui import (QBrush, QColor, QPainter, QPen, QBrush,
QPolygon, QIcon)
from PyQt5.QtWidgets import (QToolBar, QPushButton, QFileDialog, QWidget,
QSizePolicy, QMenu, QToolButton, QActionGroup)
import os
class Direction(Enum):
Forward = 0
Backward = 1
class BaseControl(QToolBar):
def __init__(self, parent=None):
super().__init__(parent)
self.setOrientation(Qt.Horizontal)
self.setIconSize(QSize(50, 50))
self.setAutoFillBackground(True)
self.setStyleSheet(self.styleSheet()+
"""
background-color: rgba(255, 255, 255, 0.4);
""")
@property
def halfWidth(self):
return self.width() // 2
@property
def halfHeight(self):
return self.height() // 2
# Main Controls
class MainControls(BaseControl):
"Main controls for changes general settings"
imagesSelected = pyqtSignal(list)
imageModeChanged = pyqtSignal(int)
imageDirectionChanged = pyqtSignal()
diasshowStateChanged = pyqtSignal(int)
zoomChanged = pyqtSignal(bool)
rotateChanged = pyqtSignal()
def __init__(self, supported_exts, parent=None):
super().__init__(parent)
self._diasshowRunning = False
# a dummy widget to center actions
spacer1 = QWidget()
spacer1.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
self.addWidget(spacer1)
self.supportedExts = supported_exts
self._fromFile = self.addAction(QIcon("icons/image-outline.svg"), "", self.chooseFile) # load images from file
self._fromFile.setToolTip("Load image")
self._fromFolder = self.addAction(QIcon("icons/folder-open.svg"), "", self.chooseFolder) # load images from folder
self._fromFolder.setToolTip("Load from directory")
# View in native size, fit width, fit height or fit image
self._imageMode = QToolButton(self)
self._imageMode.setIcon(QIcon("icons/eye-outline.svg"))
self._imageMode.setToolTip("Image view mode")
self._imageMode.setPopupMode(QToolButton.InstantPopup)
self.addWidget(self._imageMode)
# imageMode menu
imageModeMenu = QMenu(self)
imageModeActions = QActionGroup(imageModeMenu)
imModeAct1 = imageModeActions.addAction("Native size")
imModeAct1.setCheckable(True)
imModeAct1.triggered.connect(lambda: self.imageModeChanged.emit(0))
imModeAct2 = imageModeActions.addAction("Fit in view")
imModeAct2.setCheckable(True)
imModeAct2.triggered.connect(lambda: self.imageModeChanged.emit(1))
imModeAct3 = imageModeActions.addAction("Fit width")
imModeAct3.setCheckable(True)
imModeAct3.triggered.connect(lambda: self.imageModeChanged.emit(2))
imModeAct4 = imageModeActions.addAction("Fit height")
imModeAct4.setCheckable(True)
imModeAct4.triggered.connect(lambda: self.imageModeChanged.emit(3))
imageModeActions.setExclusive(True)
imageModeMenu.addActions(imageModeActions.actions())
self._imageMode.setMenu(imageModeMenu)
self._imgDirection = self.addAction(QIcon("icons/arrow-move-outline.svg"), "", self.imageDirectionChanged.emit) # Horizontal or Vertical
self._imgDirection.setToolTip("Toggle image direction")
# start or stop diasshow
self._playDias = self.addAction(QIcon("icons/media-play-outline.svg"), "", self.diasshowState)
self._playDias.setToolTip("Start/stop diasshow")
#diasshow menu
self._diasMenu = QMenu(self)
self._diasMenu.addAction("5 seconds", lambda: self.diasshowState(5))
self._diasMenu.addAction("10 seconds", lambda: self.diasshowState(10))
self._diasMenu.addAction("30 seconds", lambda: self.diasshowState(30))
self._diasMenu.addAction("5 minutes", lambda: self.diasshowState(60*5))
self._diasMenu.addAction("10 minutes", lambda: self.diasshowState(600))
self._playDias.setMenu(self._diasMenu)
self._zoomIn = self.addAction(QIcon("icons/zoom-in-outline.svg"), "", lambda: self.zoomChanged.emit(True))
self._zoomOut = self.addAction(QIcon("icons/zoom-out-outline.svg"), "", lambda: self.zoomChanged.emit(False))
self._rotateCW = self.addAction(QIcon("icons/rotate-cw-outline.svg"), "", self.rotateChanged.emit) # Clockwise
self._rotateCW.setToolTip("Rotate Clockwise")
#self._rotateCCW = self.addAction("Rotate Left") # Counter clockwise
# a dummy widget to center actions
spacer2 = QWidget()
spacer2.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
self.addWidget(spacer2)
def chooseFolder(self):
"Load images from a folder"
folder = QFileDialog.getExistingDirectory(self.parentWidget(), "Choose folder")
if folder:
self.imagesSelected.emit(
[os.path.join(folder, x) for x in os.listdir(folder) if x.endswith(tuple(self.supportedExts))])
def chooseFile(self):
"Load a single image"
exts = ""
for i in self.supportedExts:
exts += "*"+i+" " # QFileDialog only accepts a string like this: "*.jpg *.png"
files, _ = QFileDialog.getOpenFileNames(self.parentWidget(), "Select image", filter=exts)
if files: # if a file was chosen
self.imagesSelected.emit(files)
def ensureDirection(self, ori):
"Makes sure the direction is correct"
self.move(0, 0)
pSize = self.parentWidget().size()
if ori == Qt.Horizontal:
self.resize(pSize.width(), self.iconSize().height()//2)
else:
self.resize(self.iconSize().width()//2, pSize.height())
def diasshowState(self, secs=3):
"Changes the diasshow icon and emits signal"
if self._diasshowRunning:
i = "icons/media-play-outline.svg"
self._diasshowRunning = False
else:
i = "icons/media-pause-outline.svg"
self._diasshowRunning = True
self._playDias.setIcon(QIcon(i))
self.diasshowStateChanged.emit(secs)
# NavControls
class NavControl(QPushButton):
"Arrow"
def __init__(self, direction, orientation, parent=None):
super().__init__(parent)
self._direction = direction
self._orientation = orientation
self._thickness = 15 # arrow thickness
self._brushColor = QColor(240, 240, 240, 200) # default arrow color
self.resize(35, 35) # default size
def paintEvent(self, ev):
"Paints the arrow"
# setup
rect = self.rect()
x, y, w, h = rect.getRect()
if self._orientation == Qt.Vertical:
x = w//4
w = w*0.4
else:
y = h//4
h = h*0.4
painter = QPainter(self)
painter.setRenderHint(painter.Antialiasing)
penColor = QColor(0,0,0)
painter.setPen(QPen(penColor))
painter.setBrush(QBrush(self._brushColor))
# figure out the arrow points.
arrowPoints = []
if self._direction == Direction.Backward:
if self._orientation == Qt.Horizontal:
arrowPoints.extend(
[QPoint(x + w, y),
#QPoint(x + w - self._thickness, y),
QPoint(x, y + h // 2),
#QPoint(x + w - self._thickness, y + h),
QPoint(x + w, y + h),
QPoint(x + self._thickness, y + h // 2),
QPoint(x + w, y)])
else:
arrowPoints.extend(
[QPoint(x, y + h),
QPoint(x + w // 2, y),
QPoint(x + w, y + h),
#QPoint(x + w - self._thickness, y + h),
QPoint(x + w // 2, y + self._thickness),
#QPoint(x + self._thickness, y + h),
QPoint(x, y + h)])
else:
if self._orientation == Qt.Horizontal:
arrowPoints.extend(
[QPoint(x, y),
#QPoint(x + self._thickness, y),
QPoint(x + w, y + h // 2),
#QPoint(x + self._thickness, y + h),
QPoint(x, y + h),
QPoint(x + w - self._thickness, y + h // 2),
QPoint(x, y)])
else:
arrowPoints.extend(
[QPoint(x, y),
#QPoint(x + self._thickness, y),
QPoint(x + w//2, y + h - self._thickness),
#QPoint(x + w - self._thickness, y),
QPoint(x + w, y),
QPoint(x + w//2, y + h),
QPoint(x, y)])
# now draw!
painter.drawPolygon(QPolygon(arrowPoints))
class NavControls(QObject):
"Navigation controls"
forwardClicked = pyqtSignal()
backwardClicked = pyqtSignal()
def __init__(self, view, orientation=Qt.Vertical):
super().__init__(view)
self._view = view.viewport()
self._orientation = orientation
self._forward = NavControl(Direction.Forward, orientation, view) # Go to next image
self._forward.clicked.connect(self.forwardClicked.emit)
self._backward = NavControl(Direction.Backward, orientation, view) # go to previous image
self._backward.clicked.connect(self.backwardClicked.emit)
def changeOrientation(self, orientation):
self._orientation = orientation
self._forward._orientation = orientation
self._backward._orientation = orientation
if orientation == Qt.Horizontal:
size = (50, 300,)
else:
size = (300, 50,)
self._forward.resize(*size)
self._backward.resize(*size)
self.ensureEgdes()
def ensureEgdes(self):
"Makes sure the nav controls stays at the edges and has the right size"
if self._orientation == Qt.Horizontal:
_margin = 15
# ensure positioning
self._forward.move(self._view.width() - self._forward.width() - _margin,
self._view.height() // 2 - self._forward.height() // 2)
self._backward.move(_margin,
self._view.height() // 2 - self._backward.height() // 2)
else:
# ensure positioning
_margin = 10
self._forward.move(self._view.width() // 2 - self._forward.width() // 2,
self._view.height() - self._forward.height() - _margin)
self._backward.move(self._view.width() // 2 - self._backward.width() // 2,
_margin)