forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClipboardMaster.py
101 lines (89 loc) · 3.04 KB
/
ClipboardMaster.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2020/7/31
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: [email protected]
@file: ClipboardMaster
@description:
"""
import sys
from PyQt5.QtCore import QUrl, pyqtSlot, pyqtSignal, QLoggingCategory, QVariant, QMimeData
from PyQt5.QtRemoteObjects import QRemoteObjectHost
from PyQt5.QtWidgets import QTextBrowser
class WindowMaster(QTextBrowser):
SignalUpdateMimeData = pyqtSignal(
bool, QVariant, # color
bool, QVariant, # html
bool, QVariant, # image
bool, QVariant, # text
bool, QVariant, # urls
)
def __init__(self, *args, **kwargs):
super(WindowMaster, self).__init__(*args, **kwargs)
# 监听剪切板
clipboard = QApplication.clipboard()
clipboard.dataChanged.connect(self.on_data_changed)
# 开启节点
host = QRemoteObjectHost(QUrl('tcp://0.0.0.0:' + sys.argv[1]), parent=self)
host.enableRemoting(self, 'WindowMaster')
self.append('开启节点完成')
def on_data_changed(self):
# 服务端剪贴板变化后发送到客户端
clipboard = QApplication.clipboard()
clipboard.blockSignals(True)
mime_data = clipboard.mimeData()
self.SignalUpdateMimeData.emit(
mime_data.hasColor(), mime_data.colorData(),
mime_data.hasHtml(), mime_data.html(),
mime_data.hasImage(), mime_data.imageData(),
mime_data.hasText(), mime_data.text(),
mime_data.hasUrls(), mime_data.urls(),
)
clipboard.blockSignals(False)
@pyqtSlot(
bool, QVariant, # color
bool, QVariant, # html
bool, QVariant, # image
bool, QVariant, # text
bool, QVariant, # urls
bool, QVariant # files
)
def updateMimeData(self,
hasColor, color,
hasHtml, html,
hasImage, image,
hasText, text,
hasUrls, urls,
hasFiles, files,
):
# 客户端剪切板同步到服务端
self.append('收到客户端发送的剪贴板')
clipboard = QApplication.clipboard()
clipboard.blockSignals(True)
data = QMimeData()
if hasColor:
data.setColorData(color)
if hasHtml:
data.setHtml(html)
if hasImage:
data.setImageData(image)
if hasText:
data.setText(text)
# if hasUrls:
# data.setUrls(urls)
if hasFiles:
data.setData('')
clipboard.setMimeData(data)
clipboard.blockSignals(False)
if __name__ == '__main__':
import cgitb
cgitb.enable(format='text')
from PyQt5.QtWidgets import QApplication
QLoggingCategory.setFilterRules('qt.remoteobjects.debug=true\n'
'qt.remoteobjects.warning=true')
app = QApplication(sys.argv)
w = WindowMaster()
w.show()
sys.exit(app.exec_())