st-spider 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- st-spider-0.0.1/LICENSE.txt +19 -0
- st-spider-0.0.1/PKG-INFO +48 -0
- st-spider-0.0.1/README.md +25 -0
- st-spider-0.0.1/setup.cfg +4 -0
- st-spider-0.0.1/setup.py +37 -0
- st-spider-0.0.1/spider/Annealing.py +150 -0
- st-spider-0.0.1/spider/MainWidget.py +134 -0
- st-spider-0.0.1/spider/PaintBoard.py +92 -0
- st-spider-0.0.1/spider/__init__.py +4 -0
- st-spider-0.0.1/spider/enhance.py +244 -0
- st-spider-0.0.1/spider/main.py +21 -0
- st-spider-0.0.1/spider/run_scsim.py +110 -0
- st-spider-0.0.1/spider/scsim.py +285 -0
- st-spider-0.0.1/spider/sim_expr.py +194 -0
- st-spider-0.0.1/spider/simulate_10X.py +291 -0
- st-spider-0.0.1/spider/utils.py +115 -0
- st-spider-0.0.1/st_spider.egg-info/PKG-INFO +48 -0
- st-spider-0.0.1/st_spider.egg-info/SOURCES.txt +19 -0
- st-spider-0.0.1/st_spider.egg-info/dependency_links.txt +1 -0
- st-spider-0.0.1/st_spider.egg-info/requires.txt +11 -0
- st-spider-0.0.1/st_spider.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) [year] [fullname]
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
st-spider-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: st-spider
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A tools to simulate spatial transcriptomics data.
|
|
5
|
+
Home-page: https://github.com/YANG-ERA/Spider/tree/main
|
|
6
|
+
Author: Jiyuan Yang
|
|
7
|
+
Author-email: 599568651@qq.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE.txt
|
|
11
|
+
Requires-Dist: anndata
|
|
12
|
+
Requires-Dist: matplotlib
|
|
13
|
+
Requires-Dist: numba
|
|
14
|
+
Requires-Dist: numpy
|
|
15
|
+
Requires-Dist: pandas
|
|
16
|
+
Requires-Dist: PyQt5
|
|
17
|
+
Requires-Dist: scanpy
|
|
18
|
+
Requires-Dist: scikit_learn
|
|
19
|
+
Requires-Dist: scipy
|
|
20
|
+
Requires-Dist: seaborn
|
|
21
|
+
Requires-Dist: squidpy
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# Spider:
|
|
25
|
+
|
|
26
|
+
Xxx
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
`Spider` is available in [python](https://www.python.org).
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
pip install spider
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
## Output files
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
## Citation
|
st-spider-0.0.1/setup.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
|
|
2
|
+
import io
|
|
3
|
+
import os
|
|
4
|
+
from setuptools import setup, find_packages
|
|
5
|
+
|
|
6
|
+
here = os.path.abspath(os.path.dirname(__file__))
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
|
|
10
|
+
long_description = '\n' + f.read()
|
|
11
|
+
except FileNotFoundError:
|
|
12
|
+
long_description = DESCRIPTION
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# Package meta-data.
|
|
16
|
+
NAME = 'st-spider'
|
|
17
|
+
DESCRIPTION = 'A tools to simulate spatial transcriptomics data.'
|
|
18
|
+
EMAIL = '599568651@qq.com'
|
|
19
|
+
URL="https://github.com/YANG-ERA/Spider/tree/main"
|
|
20
|
+
AUTHOR ='Jiyuan Yang'
|
|
21
|
+
VERSION = '0.0.1'
|
|
22
|
+
|
|
23
|
+
setup(
|
|
24
|
+
name=NAME,
|
|
25
|
+
version=VERSION,
|
|
26
|
+
author=AUTHOR,
|
|
27
|
+
author_email=EMAIL,
|
|
28
|
+
license='MIT',
|
|
29
|
+
description=DESCRIPTION,
|
|
30
|
+
url=URL,
|
|
31
|
+
long_description_content_type="text/markdown",
|
|
32
|
+
long_description=long_description,
|
|
33
|
+
packages=find_packages(),
|
|
34
|
+
install_requires=["anndata", "matplotlib", "numba", "numpy",
|
|
35
|
+
"pandas", "PyQt5", "scanpy", "scikit_learn",
|
|
36
|
+
"scipy", "seaborn", "squidpy"]
|
|
37
|
+
)
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
#import squidpy as sq
|
|
2
|
+
import scanpy as sc
|
|
3
|
+
import anndata as ad
|
|
4
|
+
import numpy as np
|
|
5
|
+
import pandas as pd
|
|
6
|
+
import squidpy as sq
|
|
7
|
+
import matplotlib.pyplot as plt
|
|
8
|
+
import scipy
|
|
9
|
+
import random
|
|
10
|
+
import matplotlib
|
|
11
|
+
import seaborn as sns
|
|
12
|
+
import math
|
|
13
|
+
#import time
|
|
14
|
+
from scipy.special import softmax
|
|
15
|
+
from scipy.optimize import minimize
|
|
16
|
+
from numpy.random import uniform
|
|
17
|
+
from sklearn.preprocessing import OneHotEncoder
|
|
18
|
+
from sklearn.preprocessing import LabelEncoder
|
|
19
|
+
from .utils import *
|
|
20
|
+
|
|
21
|
+
def STsim(Num_sample=None,
|
|
22
|
+
Num_celltype = None,
|
|
23
|
+
celltype_assignment=None,
|
|
24
|
+
target_trans = None,
|
|
25
|
+
T=1000,
|
|
26
|
+
chain_len=100,
|
|
27
|
+
error=1e2,
|
|
28
|
+
tol=2e-2,
|
|
29
|
+
decay=0.5,
|
|
30
|
+
onehot_ct=None,
|
|
31
|
+
nb_count=None,
|
|
32
|
+
sn=None,
|
|
33
|
+
swap_num=None,
|
|
34
|
+
smallsample_max_iter=None,
|
|
35
|
+
bigsample_max_iter=None):
|
|
36
|
+
'''
|
|
37
|
+
smallsample: <10000 and swap_num = 1
|
|
38
|
+
bigsample: >=10000
|
|
39
|
+
'''
|
|
40
|
+
|
|
41
|
+
error_record = []
|
|
42
|
+
iter_n = 0
|
|
43
|
+
print("Refine cell type using Metropolis–Hastings algorithm.")
|
|
44
|
+
print("Sample num"+":{}".format(Num_sample) )
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
nb_freq = get_nb_freq(nb_count=nb_count, onehot_ct=onehot_ct)
|
|
48
|
+
dist_before = sum(scipy.stats.entropy(target_trans, nb_freq))
|
|
49
|
+
|
|
50
|
+
if Num_sample <= 10000:
|
|
51
|
+
swap_num = 1
|
|
52
|
+
max_iter = smallsample_max_iter
|
|
53
|
+
criterion3 = True
|
|
54
|
+
same_errlen = 250
|
|
55
|
+
while error > tol and iter_n < max_iter and criterion3:
|
|
56
|
+
|
|
57
|
+
for i in range(chain_len):
|
|
58
|
+
|
|
59
|
+
before_ct_assign, before_onehot_ct, before_nb_count = celltype_assignment.copy(), onehot_ct.copy(), nb_count.copy()
|
|
60
|
+
|
|
61
|
+
swap_i, swap_j = swap_ct(
|
|
62
|
+
celltype_assignment=celltype_assignment,
|
|
63
|
+
Num_celltype=Num_celltype,
|
|
64
|
+
swap_num=swap_num)
|
|
65
|
+
|
|
66
|
+
celltype_assignment[swap_i[0]], celltype_assignment[
|
|
67
|
+
swap_j[0]] = celltype_assignment[
|
|
68
|
+
swap_j[0]], celltype_assignment[swap_i[0]]
|
|
69
|
+
onehot_ct[swap_i[0]], onehot_ct[swap_j[0]] = onehot_ct[
|
|
70
|
+
swap_j[0]], onehot_ct[swap_i[0]]
|
|
71
|
+
|
|
72
|
+
nb_count = get_swap_nb_count(nb_count=nb_count,
|
|
73
|
+
swap_i=swap_i,
|
|
74
|
+
swap_j=swap_j,
|
|
75
|
+
sn=sn)
|
|
76
|
+
# nb_count = sn * onehot_ct
|
|
77
|
+
nb_freq = get_nb_freq(nb_count=nb_count, onehot_ct=onehot_ct)
|
|
78
|
+
|
|
79
|
+
dist_after = sum(scipy.stats.entropy(target_trans, nb_freq))
|
|
80
|
+
|
|
81
|
+
if 0 > (dist_before - dist_after):
|
|
82
|
+
#Reject the swap, so swap back.
|
|
83
|
+
p = math.exp((dist_before - dist_after) / T)
|
|
84
|
+
r = np.random.uniform(low=0, high=1)
|
|
85
|
+
if p < r:
|
|
86
|
+
celltype_assignment, nb_count, onehot_ct = before_ct_assign, before_nb_count, before_onehot_ct
|
|
87
|
+
else:
|
|
88
|
+
dist_before = dist_after
|
|
89
|
+
else:
|
|
90
|
+
dist_before = dist_after
|
|
91
|
+
iter_n += 1
|
|
92
|
+
# print(dist_before - dist_after)
|
|
93
|
+
T *= decay
|
|
94
|
+
if iter_n % 100 == 0:
|
|
95
|
+
error = np.linalg.norm(nb_freq - target_trans)
|
|
96
|
+
error_record.append(error)
|
|
97
|
+
if iter_n % 1000 ==0:
|
|
98
|
+
print("%5d iteration, error %.3f" % (iter_n, error))
|
|
99
|
+
|
|
100
|
+
if len(error_record) > same_errlen:
|
|
101
|
+
if len(set(error_record[-same_errlen:])) == 1:
|
|
102
|
+
criterion3 = False
|
|
103
|
+
else:
|
|
104
|
+
max_iter = bigsample_max_iter
|
|
105
|
+
criterion3 = True
|
|
106
|
+
while error > tol and iter_n < max_iter:
|
|
107
|
+
|
|
108
|
+
for i in range(chain_len):
|
|
109
|
+
|
|
110
|
+
#dist_before = sum(scipy.stats.entropy(target_trans,nb_freq))
|
|
111
|
+
|
|
112
|
+
before_ct_assign, before_onehot_ct, before_nb_count = celltype_assignment.copy(), onehot_ct.copy(), nb_count.copy()
|
|
113
|
+
|
|
114
|
+
swap_i, swap_j = swap_ct(
|
|
115
|
+
celltype_assignment=celltype_assignment,
|
|
116
|
+
Num_celltype=Num_celltype,
|
|
117
|
+
swap_num=swap_num)
|
|
118
|
+
|
|
119
|
+
celltype_assignment[swap_i[0]], celltype_assignment[
|
|
120
|
+
swap_j[0]] = celltype_assignment[
|
|
121
|
+
swap_j[0]], celltype_assignment[swap_i[0]]
|
|
122
|
+
onehot_ct[swap_i[0]], onehot_ct[swap_j[0]] = onehot_ct[
|
|
123
|
+
swap_j[0]], onehot_ct[swap_i[0]]
|
|
124
|
+
|
|
125
|
+
nb_count = get_swap_nb_count(nb_count=nb_count,
|
|
126
|
+
swap_i=swap_i,
|
|
127
|
+
swap_j=swap_j,
|
|
128
|
+
sn=sn)
|
|
129
|
+
# nb_count = sn * onehot_ct
|
|
130
|
+
nb_freq = get_nb_freq(nb_count=nb_count, onehot_ct=onehot_ct)
|
|
131
|
+
|
|
132
|
+
dist_after = sum(scipy.stats.entropy(target_trans, nb_freq))
|
|
133
|
+
|
|
134
|
+
if 0 > (dist_before - dist_after):
|
|
135
|
+
p = math.exp((dist_before - dist_after) / T)
|
|
136
|
+
r = np.random.uniform(low=0, high=1)
|
|
137
|
+
if p < r:
|
|
138
|
+
celltype_assignment, nb_count, onehot_ct = before_ct_assign, before_nb_count, before_onehot_ct
|
|
139
|
+
else:
|
|
140
|
+
dist_before = dist_after
|
|
141
|
+
else:
|
|
142
|
+
dist_before = dist_after
|
|
143
|
+
iter_n += 1
|
|
144
|
+
# print(dist_before - dist_after)
|
|
145
|
+
T *= decay
|
|
146
|
+
if iter_n % 500 == 0:
|
|
147
|
+
error = np.linalg.norm(nb_freq - target_trans)
|
|
148
|
+
print("%5d iteration, error %.3f" % (iter_n, error))
|
|
149
|
+
|
|
150
|
+
return celltype_assignment
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
|
|
2
|
+
from PyQt5.Qt import QWidget, QColor, QPixmap, QIcon, QSize, QCheckBox
|
|
3
|
+
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QPushButton, QSplitter, \
|
|
4
|
+
QComboBox, QLabel, QSpinBox, QFileDialog
|
|
5
|
+
|
|
6
|
+
from paint3.PaintBoard import PaintBoard
|
|
7
|
+
import os
|
|
8
|
+
|
|
9
|
+
class MainWidget(QWidget):
|
|
10
|
+
|
|
11
|
+
def __init__(self, Parent=None):
|
|
12
|
+
|
|
13
|
+
super().__init__(Parent)
|
|
14
|
+
|
|
15
|
+
self.__InitData() # initial
|
|
16
|
+
self.__InitView()
|
|
17
|
+
|
|
18
|
+
def __InitData(self):
|
|
19
|
+
|
|
20
|
+
self.__paintBoard = PaintBoard(self)
|
|
21
|
+
self.__colorList = QColor.colorNames()
|
|
22
|
+
|
|
23
|
+
def __InitView(self):
|
|
24
|
+
|
|
25
|
+
self.setFixedSize(1200, 640)
|
|
26
|
+
self.setWindowTitle("PaintBoard of PyQt5")
|
|
27
|
+
|
|
28
|
+
main_layout = QHBoxLayout(self)
|
|
29
|
+
main_layout.setSpacing(10)
|
|
30
|
+
|
|
31
|
+
main_layout.addWidget(self.__paintBoard)
|
|
32
|
+
|
|
33
|
+
sub_layout = QVBoxLayout()
|
|
34
|
+
|
|
35
|
+
sub_layout.setContentsMargins(10, 10, 10, 10)
|
|
36
|
+
|
|
37
|
+
self.__btn_Clear = QPushButton("Clean")
|
|
38
|
+
self.__btn_Clear.setParent(self)
|
|
39
|
+
|
|
40
|
+
# 将按键按下信号与画板清空函数相关联
|
|
41
|
+
self.__btn_Clear.clicked.connect(self.__paintBoard.Clear)
|
|
42
|
+
sub_layout.addWidget(self.__btn_Clear)
|
|
43
|
+
|
|
44
|
+
self.__btn_Quit = QPushButton("exit")
|
|
45
|
+
self.__btn_Quit.setParent(self)
|
|
46
|
+
self.__btn_Quit.clicked.connect(self.Quit)
|
|
47
|
+
sub_layout.addWidget(self.__btn_Quit)
|
|
48
|
+
|
|
49
|
+
self.__btn_Save = QPushButton("save picture")
|
|
50
|
+
self.__btn_Save.setParent(self)
|
|
51
|
+
self.__btn_Save.clicked.connect(self.on_btn_Save_Clicked)
|
|
52
|
+
sub_layout.addWidget(self.__btn_Save)
|
|
53
|
+
|
|
54
|
+
self.__cbtn_Eraser = QCheckBox("use eraser")
|
|
55
|
+
self.__cbtn_Eraser.setParent(self)
|
|
56
|
+
self.__cbtn_Eraser.clicked.connect(self.on_cbtn_Eraser_clicked)
|
|
57
|
+
sub_layout.addWidget(self.__cbtn_Eraser)
|
|
58
|
+
|
|
59
|
+
splitter = QSplitter(self)
|
|
60
|
+
sub_layout.addWidget(splitter)
|
|
61
|
+
|
|
62
|
+
self.__label_penThickness = QLabel(self)
|
|
63
|
+
self.__label_penThickness.setText("Pen thickness")
|
|
64
|
+
self.__label_penThickness.setFixedHeight(20)
|
|
65
|
+
sub_layout.addWidget(self.__label_penThickness)
|
|
66
|
+
|
|
67
|
+
self.__spinBox_penThickness = QSpinBox(self)
|
|
68
|
+
self.__spinBox_penThickness.setMaximum(20)
|
|
69
|
+
self.__spinBox_penThickness.setMinimum(2)
|
|
70
|
+
self.__spinBox_penThickness.setValue(10)
|
|
71
|
+
self.__spinBox_penThickness.setSingleStep(2)
|
|
72
|
+
self.__spinBox_penThickness.valueChanged.connect(
|
|
73
|
+
self.on_PenThicknessChange)
|
|
74
|
+
sub_layout.addWidget(self.__spinBox_penThickness)
|
|
75
|
+
|
|
76
|
+
self.__label_penColor = QLabel(self)
|
|
77
|
+
self.__label_penColor.setText("Pen color")
|
|
78
|
+
self.__label_penColor.setFixedHeight(20)
|
|
79
|
+
sub_layout.addWidget(self.__label_penColor)
|
|
80
|
+
|
|
81
|
+
self.__comboBox_penColor = QComboBox(self)
|
|
82
|
+
self.__fillColorList(self.__comboBox_penColor)
|
|
83
|
+
self.__comboBox_penColor.currentIndexChanged.connect(
|
|
84
|
+
self.on_PenColorChange)
|
|
85
|
+
sub_layout.addWidget(self.__comboBox_penColor)
|
|
86
|
+
|
|
87
|
+
main_layout.addLayout(sub_layout)
|
|
88
|
+
|
|
89
|
+
def __fillColorList(self, comboBox):
|
|
90
|
+
|
|
91
|
+
index_black = 0
|
|
92
|
+
index = 0
|
|
93
|
+
for color in self.__colorList:
|
|
94
|
+
if color == "black":
|
|
95
|
+
index_black = index
|
|
96
|
+
index += 1
|
|
97
|
+
pix = QPixmap(70, 20)
|
|
98
|
+
pix.fill(QColor(color))
|
|
99
|
+
comboBox.addItem(QIcon(pix), None)
|
|
100
|
+
comboBox.setIconSize(QSize(70, 20))
|
|
101
|
+
comboBox.setSizeAdjustPolicy(QComboBox.AdjustToContents)
|
|
102
|
+
|
|
103
|
+
comboBox.setCurrentIndex(index_black)
|
|
104
|
+
|
|
105
|
+
def on_PenColorChange(self):
|
|
106
|
+
color_index = self.__comboBox_penColor.currentIndex()
|
|
107
|
+
color_str = self.__colorList[color_index]
|
|
108
|
+
self.__paintBoard.ChangePenColor(color_str)
|
|
109
|
+
|
|
110
|
+
def on_PenThicknessChange(self):
|
|
111
|
+
penThickness = self.__spinBox_penThickness.value()
|
|
112
|
+
self.__paintBoard.ChangePenThickness(penThickness)
|
|
113
|
+
|
|
114
|
+
def on_btn_Save_Clicked(self):
|
|
115
|
+
# savePath = QFileDialog.getSaveFileName(self, 'Save Your Paint', '.\\', '*.png')
|
|
116
|
+
# print(savePath)
|
|
117
|
+
# if savePath[0] == "":
|
|
118
|
+
# print("Save cancel")
|
|
119
|
+
# return
|
|
120
|
+
#savePath = "C:\\Users\\quyang\\paint2\\test.png"
|
|
121
|
+
####
|
|
122
|
+
image = self.__paintBoard.GetContentAsQImage()
|
|
123
|
+
#image.save(savePath)
|
|
124
|
+
|
|
125
|
+
image.save(os.getcwd()+"/test.jpg")
|
|
126
|
+
|
|
127
|
+
def on_cbtn_Eraser_clicked(self):
|
|
128
|
+
if self.__cbtn_Eraser.isChecked():
|
|
129
|
+
self.__paintBoard.EraserMode = True
|
|
130
|
+
else:
|
|
131
|
+
self.__paintBoard.EraserMode = False
|
|
132
|
+
|
|
133
|
+
def Quit(self):
|
|
134
|
+
self.close()
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
#https://blog.csdn.net/CreatorGG/article/details/81542837?spm=1001.2101.3001.6650.14&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-14-81542837-blog-118923269.pc_relevant_multi_platform_featuressortv2dupreplace&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-14-81542837-blog-118923269.pc_relevant_multi_platform_featuressortv2dupreplace&utm_relevant_index=20
|
|
2
|
+
'''
|
|
3
|
+
Created on 2018年8月9日
|
|
4
|
+
|
|
5
|
+
@author: Freedom
|
|
6
|
+
'''
|
|
7
|
+
from PyQt5.QtWidgets import QWidget
|
|
8
|
+
from PyQt5.Qt import QPixmap, QPainter, QPoint, QPaintEvent, QMouseEvent, QPen, \
|
|
9
|
+
QColor, QSize
|
|
10
|
+
from PyQt5.QtCore import Qt
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class PaintBoard(QWidget):
|
|
14
|
+
|
|
15
|
+
def __init__(self, Parent=None):
|
|
16
|
+
'''
|
|
17
|
+
Constructor
|
|
18
|
+
'''
|
|
19
|
+
super().__init__(Parent)
|
|
20
|
+
|
|
21
|
+
self.__InitData()
|
|
22
|
+
self.__InitView()
|
|
23
|
+
|
|
24
|
+
def __InitData(self):
|
|
25
|
+
|
|
26
|
+
self.__size = QSize(1000, 600)
|
|
27
|
+
|
|
28
|
+
self.__board = QPixmap(self.__size)
|
|
29
|
+
self.__board.fill(Qt.white)
|
|
30
|
+
|
|
31
|
+
self.__IsEmpty = True
|
|
32
|
+
self.EraserMode = False
|
|
33
|
+
|
|
34
|
+
self.__lastPos = QPoint(0, 0)
|
|
35
|
+
self.__currentPos = QPoint(0, 0)
|
|
36
|
+
|
|
37
|
+
self.__painter = QPainter()
|
|
38
|
+
|
|
39
|
+
self.__thickness = 10
|
|
40
|
+
self.__penColor = QColor("black")
|
|
41
|
+
self.__colorList = QColor.colorNames()
|
|
42
|
+
|
|
43
|
+
def __InitView(self):
|
|
44
|
+
self.setFixedSize(self.__size)
|
|
45
|
+
|
|
46
|
+
def Clear(self):
|
|
47
|
+
self.__board.fill(Qt.white)
|
|
48
|
+
self.update()
|
|
49
|
+
self.__IsEmpty = True
|
|
50
|
+
|
|
51
|
+
def ChangePenColor(self, color="black"):
|
|
52
|
+
self.__penColor = QColor(color)
|
|
53
|
+
|
|
54
|
+
def ChangePenThickness(self, thickness=10):
|
|
55
|
+
self.__thickness = thickness
|
|
56
|
+
|
|
57
|
+
def IsEmpty(self):
|
|
58
|
+
return self.__IsEmpty
|
|
59
|
+
|
|
60
|
+
def GetContentAsQImage(self):
|
|
61
|
+
image = self.__board.toImage()
|
|
62
|
+
return image
|
|
63
|
+
|
|
64
|
+
def paintEvent(self, paintEvent):
|
|
65
|
+
self.__painter.begin(self)
|
|
66
|
+
self.__painter.drawPixmap(0, 0, self.__board)
|
|
67
|
+
self.__painter.end()
|
|
68
|
+
|
|
69
|
+
def mousePressEvent(self, mouseEvent):
|
|
70
|
+
self.__currentPos = mouseEvent.pos()
|
|
71
|
+
self.__lastPos = self.__currentPos
|
|
72
|
+
|
|
73
|
+
def mouseMoveEvent(self, mouseEvent):
|
|
74
|
+
self.__currentPos = mouseEvent.pos()
|
|
75
|
+
self.__painter.begin(self.__board)
|
|
76
|
+
|
|
77
|
+
if self.EraserMode == False:
|
|
78
|
+
self.__painter.setPen(QPen(self.__penColor, self.__thickness)) # 设置画笔颜色,粗细
|
|
79
|
+
else:
|
|
80
|
+
self.__painter.setPen(QPen(Qt.white, 10))
|
|
81
|
+
|
|
82
|
+
self.__painter.drawLine(self.__lastPos, self.__currentPos)
|
|
83
|
+
self.__painter.end()
|
|
84
|
+
self.__lastPos = self.__currentPos
|
|
85
|
+
|
|
86
|
+
self.update()
|
|
87
|
+
|
|
88
|
+
def mouseReleaseEvent(self, mouseEvent):
|
|
89
|
+
self.__IsEmpty = False
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|