-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipper.py
293 lines (221 loc) · 9.82 KB
/
clipper.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import ctypes
import os
import pickle
import random
import sys
import time
from collections import OrderedDict
import win32gui
from PyQt5.QtCore import QDir, Qt, QUrl, pyqtSignal
from PyQt5.QtGui import QColor, QFont, QFontMetrics, QIcon, QPainter
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtWidgets import (
QApplication, QCheckBox, QFrame, QGraphicsDropShadowEffect, QGroupBox,
QHBoxLayout, QLabel, QLineEdit, QMainWindow, QPushButton, QScrollArea,
QSlider, QVBoxLayout, QWidget)
from handlers import SoundHandler
from objects import ClassicQLabel
from utils import WidgetUtils
# Preventing Windows's icon choice function for grouped processes (pythonw.exe -> PyClipper,...)
# See https://stackoverflow.com/questions/1551605/how-to-set-applications-taskbar-icon-in-windows-7/1552105#1552105
# for further details.
myappid = 'Clipper_v1.0'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
app = QApplication(sys.argv)
app.setStyleSheet(open("style.css").read())
screen_resolution = app.desktop().screenGeometry()
screen_width, screen_height = screen_resolution.width(), screen_resolution.height()
window_width = 350
window_height = 600
window_clippers = OrderedDict()
class Clipper(QPushButton):
def __init__(self, master):
super().__init__()
self._id = 0
self.master = master
self.shadow = QGraphicsDropShadowEffect(self)
self.shadow.setOffset(0, 0)
self.shadow.setColor(QColor(255, 255, 255))
self.setMinimumHeight(200)
self.setGraphicsEffect(self.shadow)
self.setObjectName("ClipperButton")
self.enterEvent = lambda event: self.shadow.setBlurRadius(15)
self.leaveEvent = lambda event: self.shadow.setBlurRadius(0)
self.titlebar = QFrame(self)
self.titlebar.resize(330, 30)
self.titlebar.setObjectName("ClipperTitlebar")
self._titlebar_text = QLineEdit(self.titlebar)
self._titlebar_text.resize(self.width() - 30, 30)
self._titlebar_text.move(30, 0)
self._titlebar_text.setText("New Clipper")
self.text_label_frame = QFrame(self)
self.text_label_frame.resize(330, 170)
self.text_label_frame.move(0, 30)
self.text_label_frame.mousePressEvent = lambda event: self.text_label_pressed()
self._text_label = ClassicQLabel(self.text_label_frame)
self._text_label.resize(self.text_label_frame.frameGeometry().width() - 15, self.text_label_frame.frameGeometry().height() - 15)
self._text_label.move(5, 5)
self._text_label.mousePressEvent = lambda event: self.text_label_pressed()
self._text_label.setWordWrap(True)
self._text_label.setAlignment(Qt.AlignTop)
self.delete_button = QPushButton(self)
self.delete_button.resize(30, 30)
self.delete_button.clicked.connect(self.delete_button_clicked)
def text_label_pressed(self):
if len(self._text_label.text()) > 0:
QApplication.clipboard().setText(self._text_label.text())
def delete_button_clicked(self):
window_clippers.pop(self.id)
self.master.resize_window_clippers()
self.setParent(None)
@property
def text(self):
return self._text_label.text()
@text.setter
def text(self, text):
self.title = win32gui.GetWindowText(win32gui.GetForegroundWindow())
self._text_label.setText(text)
@property
def title(self):
return self._titlebar_text.text()
@title.setter
def title(self, text):
self._titlebar_text.setText(text)
@property
def id(self):
return self._id
@id.setter
def id(self, id):
self._id = id
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.clipboard_text = QApplication.clipboard().text()
self.volume = 100
self.clippers = []
self.init_main_window()
self.init_elements()
self.init_events()
self.load_data()
def load_data(self):
if not os.path.isfile("data.pkl"):
return False
with open("data.pkl", "rb") as read_data:
window_clippers = pickle.load(read_data)
for clipper_id, pair in window_clippers.items():
label_text = pair["text"]
titlebar_text = pair["title"]
self.add_clipper(0, label_text, titlebar_text, clipper_id)
# GUI Initialization
def init_main_window(self):
self.setGeometry(screen_width - window_width, (screen_height // 2) - (window_height // 2), window_width, window_height)
self.setFixedSize(self.size())
self.setWindowTitle("PyClipper")
self.setWindowIcon(QIcon("images/favicon.png"))
self.setWindowFlags(Qt.WindowStaysOnTopHint)
def init_elements(self):
# Clipper Button Area
self.add_clipper_button = QPushButton('Add clipper', self)
self.add_clipper_button.resize(100, 32)
self.add_clipper_button_frame, self.add_clipper_button_layout = WidgetUtils.add_widget_with_alignment(self, self.add_clipper_button, QHBoxLayout, Qt.AlignCenter)
# Clipper Scroll Area
self.clipper_scroll_area = QScrollArea(self)
self.clipper_scroll_area.resize(self.width(), 472)
self.clipper_scroll_area.move(0, 42)
self.clipper_scroll_area.setWidgetResizable(True)
self.scroll_area_layout = QVBoxLayout(self.clipper_scroll_area)
self.scroll_widget = QWidget(self)
self.scroll_widget.setLayout(self.scroll_area_layout)
self.clipper_scroll_area.setWidget(self.scroll_widget)
# Sound Setting Elements
self.sound_groupbox = QGroupBox("Sound Settings", self)
self.sound_groupbox.resize(330, 60)
self.sound_groupbox_frame, self.sound_groupbox_layout = WidgetUtils.add_widget_with_alignment(self, self.sound_groupbox, QHBoxLayout, False)
self.sound_groupbox_frame.resize(400, 75)
self.sound_groupbox_frame.move(0, 520)
self.sound_checkbox = QCheckBox(self)
self.sound_checkbox.resize(13, 13)
self.sound_checkbox.move(100, 520)
self.sound_checkbox.toggle()
self.sound_scrollbar = QSlider(Qt.Horizontal, self.sound_groupbox)
self.sound_scrollbar.resize(300, 20)
self.sound_scrollbar.move(22, 30)
self.sound_scrollbar.setMaximum(100)
self.sound_scrollbar.setValue(100)
self.sound_scrollbar_value = QLabel(self.sound_groupbox)
self.sound_scrollbar_value.setText("100%")
self.sound_scrollbar_value.setAlignment(Qt.AlignCenter)
text_width = WidgetUtils.get_label_size(self.sound_scrollbar_value)[0]
self.sound_scrollbar_value.move(WidgetUtils.horizontal_align_center(self.sound_scrollbar, text_width), 0)
def init_events(self):
# Events for application
self.destroyed.connect(self._on_destroyed)
# Events for widgets on applicaton
self.add_clipper_button.clicked.connect(self.add_clipper_pressed)
self.sound_checkbox.stateChanged.connect(self.sound_state_changed)
self.sound_scrollbar.valueChanged.connect(self.slider_value_changed)
self.sound_scrollbar.sliderReleased.connect(self.slider_released)
# Global events
QApplication.clipboard().dataChanged.connect(self.clipboard_changed)
# Event functions
def slider_value_changed(self):
self.sound_scrollbar_value.setText("{}%".format(self.sound_scrollbar.value()))
def slider_released(self):
self.set_clipper_volume(self.sound_scrollbar.value())
def sound_state_changed(self):
state = self.sound_checkbox.checkState() == 2
self.sound_groupbox.setEnabled(state)
def clipboard_changed(self, index=0):
"""
This method executes when the actual string in clipboard changes.
Furthermore plays a little clipsound using the SoundHandler by PyQT.
"""
if self.clipboard_text != QApplication.clipboard().text():
self.clipboard_text = QApplication.clipboard().text()
if self.sound_groupbox.isEnabled():
SoundHandler.play_clip_sound(self.volume)
self.add_clipper(index, self.clipboard_text)
def add_clipper_pressed(self, index=0):
"""
This method executes when the "Add clipper" button is pressed.
It adds a new Clipper on index 0 to the vertical layout in the scroll area.
"""
self.add_clipper(index)
def add_clipper(self, index, text="", title="", random_id=None):
clip = Clipper(self)
self.scroll_area_layout.insertWidget(index, clip)
if len(text) > 0:
clip.text = text
if len(title) > 0:
clip.title = title
# Create new random id, if not a specific id is given
if random_id is None:
random_id = random.randint(100000, 999999)
# Reroll id if it exists already
while(window_clippers.get(random_id)):
random_id = random.randint(100000, 999999)
clip.id = random_id
window_clippers[clip.id] = {"title": clip.title, "text": clip.text}
self.clippers.append(clip)
self.resize_window_clippers()
def resize_window_clippers(self):
for clip in self.clippers:
if len(window_clippers) > 2:
clip.setFixedWidth(314)
else:
clip.setFixedWidth(330)
def set_clipper_volume(self, volume):
self.volume = volume
@staticmethod
def _on_destroyed(self):
"""
This method executes when the window is destroyed.
Saves the window_clippers in a file, so that it can be loaded afterwards.
"""
with open("data.pkl", "bw") as write_data:
pickle.dump(window_clippers, write_data)
if __name__ == "__main__":
root = Window()
root.show()
root.setAttribute(Qt.WA_DeleteOnClose, True)
sys.exit(app.exec_())