yunji 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.
yunji-0.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 YunSong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
yunji-0.1/PKG-INFO ADDED
@@ -0,0 +1,55 @@
1
+ Metadata-Version: 2.1
2
+ Name: yunji
3
+ Version: 0.1
4
+ Summary: A PyQt5-based text editor
5
+ Home-page: https://github.com/YSP0Github/yunji.git
6
+ Author: Yun Song
7
+ Author-email: ysp@cug.edu.cn
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.6
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: PyQt5
15
+
16
+ # 云记 (Yunji Editor)
17
+
18
+ ## 简介
19
+ 云记是一款基于PyQt5的简洁文本编辑器,支持基本的文本编辑功能,包括打开、保存、查找和替换文本,支持显示行号、设置文本颜色等功能。
20
+
21
+ ## 功能
22
+ - 打开文件、保存文件、另存为
23
+ - 撤销、重做、剪切、复制、粘贴
24
+ - 查找和替换文本
25
+ - 设置文本颜色和行号颜色
26
+ - 显示行号
27
+ - 自动换行
28
+ - 快捷键支持
29
+
30
+ ## 快捷键
31
+ - `Ctrl+O`:打开文件
32
+ - `Ctrl+S`:保存文件
33
+ - `Ctrl+Shift+S`:另存为
34
+ - `Ctrl+N`:新建窗口
35
+ - `Ctrl+Z`:撤销
36
+ - `Ctrl+Y`:重做
37
+ - `Ctrl+X`:剪切
38
+ - `Ctrl+C`:复制
39
+ - `Ctrl+V`:粘贴
40
+ - `Ctrl+F`:查找
41
+ - `Ctrl+H`:替换
42
+ - `Ctrl+B`:加粗
43
+ - `Ctrl+I`:斜体
44
+
45
+ ## 安装和运行
46
+
47
+ ### 环境要求
48
+ - Python 3.6+
49
+ - PyQt5
50
+
51
+ ### 安装依赖
52
+ 首先确保你已经安装了Python 3.6以上的版本。然后,使用pip安装所需的库:
53
+
54
+ ```bash
55
+ pip install -r requirements.txt
yunji-0.1/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # 云记 (Yunji Editor)
2
+
3
+ ## 简介
4
+ 云记是一款基于PyQt5的简洁文本编辑器,支持基本的文本编辑功能,包括打开、保存、查找和替换文本,支持显示行号、设置文本颜色等功能。
5
+
6
+ ## 功能
7
+ - 打开文件、保存文件、另存为
8
+ - 撤销、重做、剪切、复制、粘贴
9
+ - 查找和替换文本
10
+ - 设置文本颜色和行号颜色
11
+ - 显示行号
12
+ - 自动换行
13
+ - 快捷键支持
14
+
15
+ ## 快捷键
16
+ - `Ctrl+O`:打开文件
17
+ - `Ctrl+S`:保存文件
18
+ - `Ctrl+Shift+S`:另存为
19
+ - `Ctrl+N`:新建窗口
20
+ - `Ctrl+Z`:撤销
21
+ - `Ctrl+Y`:重做
22
+ - `Ctrl+X`:剪切
23
+ - `Ctrl+C`:复制
24
+ - `Ctrl+V`:粘贴
25
+ - `Ctrl+F`:查找
26
+ - `Ctrl+H`:替换
27
+ - `Ctrl+B`:加粗
28
+ - `Ctrl+I`:斜体
29
+
30
+ ## 安装和运行
31
+
32
+ ### 环境要求
33
+ - Python 3.6+
34
+ - PyQt5
35
+
36
+ ### 安装依赖
37
+ 首先确保你已经安装了Python 3.6以上的版本。然后,使用pip安装所需的库:
38
+
39
+ ```bash
40
+ pip install -r requirements.txt
yunji-0.1/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
yunji-0.1/setup.py ADDED
@@ -0,0 +1,37 @@
1
+ # setup.py
2
+
3
+ from setuptools import setup, find_packages
4
+
5
+ # 读取 README.md 文件内容,并使用 UTF-8 编码
6
+ with open('README.md', encoding='utf-8') as f:
7
+ long_description = f.read()
8
+
9
+ setup(
10
+ name='yunji',
11
+ version='0.1',
12
+ author="Yun Song",
13
+ author_email="ysp@cug.edu.cn",
14
+ description="A PyQt5-based text editor",
15
+ long_description=long_description,
16
+ long_description_content_type='text/markdown',
17
+ url="https://github.com/YSP0Github/yunji.git",
18
+ packages=find_packages(),
19
+ install_requires=[
20
+ 'PyQt5',
21
+ ],
22
+ entry_points={
23
+ 'console_scripts': [
24
+ 'yunji=yunji.editor:cli_editor',
25
+ ],
26
+ },
27
+ package_data={
28
+ 'yunji': ['images/*.png'], # 包含 images 文件夹下的所有 png 文件
29
+ },
30
+ include_package_data=True, # 确保在安装包中包含非代码文件
31
+ classifiers=[
32
+ "Programming Language :: Python :: 3",
33
+ "License :: OSI Approved :: MIT License",
34
+ "Operating System :: OS Independent",
35
+ ],
36
+ python_requires='>=3.6',
37
+ )
File without changes
@@ -0,0 +1,58 @@
1
+ # test_editor.py
2
+
3
+ import unittest
4
+ import os
5
+ from PyQt5.QtWidgets import QApplication
6
+ from yunji.editor import YunjiEditor
7
+
8
+ class TestYunjiEditor(unittest.TestCase):
9
+ @classmethod
10
+ def setUpClass(cls):
11
+ cls.app = QApplication([])
12
+
13
+ def setUp(self):
14
+ self.editor = YunjiEditor()
15
+
16
+ def tearDown(self):
17
+ self.editor.close()
18
+
19
+ def test_initial_state(self):
20
+ self.assertIsNone(self.editor.file_path)
21
+ self.assertEqual(self.editor.status_label_center.text(), "行: 1 ; 列: 1")
22
+ self.assertEqual(self.editor.status_label_right.text(), "文档状态: 未修改")
23
+
24
+ def test_set_text(self):
25
+ test_text = "Hello, world!"
26
+ self.editor.text_edit.setPlainText(test_text)
27
+ self.assertEqual(self.editor.text_edit.toPlainText(), test_text)
28
+ self.assertFalse(self.editor.is_saved)
29
+ self.assertEqual(self.editor.status_label_right.text(), "文档状态: 已修改")
30
+
31
+ def test_save_file(self):
32
+ test_text = "Hello, world!"
33
+ self.editor.text_edit.setPlainText(test_text)
34
+ test_file = 'testfile.txt'
35
+ self.editor.file_path = test_file
36
+ self.editor.save_file()
37
+ self.assertTrue(self.editor.is_saved)
38
+ with open(test_file, 'r') as file:
39
+ self.assertEqual(file.read(), test_text)
40
+ os.remove(test_file) # 清理测试文件
41
+
42
+ def test_open_file(self):
43
+ test_text = "Hello, world!"
44
+ test_file = 'testfile.txt'
45
+ with open(test_file, 'w') as file:
46
+ file.write(test_text)
47
+ self.editor.open_file(test_file)
48
+ self.assertEqual(self.editor.text_edit.toPlainText(), test_text)
49
+ self.assertEqual(self.editor.file_path, test_file)
50
+ os.remove(test_file) # 清理测试文件
51
+
52
+ def test_line_number_visibility(self):
53
+ self.editor.text_edit.line_numbers_visible = True
54
+ self.editor.text_edit.updateLineNumberAreaWidth(0)
55
+ self.assertTrue(self.editor.text_edit.lineNumberArea.isVisible())
56
+
57
+ if __name__ == '__main__':
58
+ unittest.main()
File without changes
@@ -0,0 +1,703 @@
1
+ # editor
2
+
3
+ import sys
4
+ import os
5
+ from PyQt5.QtWidgets import (QApplication, QMainWindow, QTextEdit, QPlainTextEdit, QAction, QFileDialog, QMessageBox, QLabel, QColorDialog,
6
+ QVBoxLayout, QWidget, QFontDialog, QHBoxLayout, QPushButton, QDialog, QLineEdit, QCheckBox, QStatusBar)
7
+ from PyQt5.QtGui import QIcon, QFont, QTextCharFormat, QTextDocument, QTextCursor, QPalette, QColor, QTextFormat, QPainter, QPixmap
8
+ from PyQt5.QtCore import Qt, QSize,QEvent,QTimer, QRect
9
+
10
+
11
+ class TextEditor(QPlainTextEdit):
12
+ def __init__(self, parent = None):
13
+ super(TextEditor, self).__init__(parent)
14
+ self.lineNumberArea = LineNumberArea(self)
15
+ self.blockCountChanged.connect(self.updateLineNumberAreaWidth)
16
+ self.updateRequest.connect(self.updateLineNumberArea)
17
+ self.cursorPositionChanged.connect(self.highlightCurrentLine)
18
+ self.line_numbers_visible = False # 用于控制行号区域的可见性
19
+ self.lineNumberColor = QColor(Qt.cyan) # 默认行号颜色
20
+ self.cursorPositionChanged.connect(self.update_cursor_position)
21
+ self.updateLineNumberAreaWidth(0)
22
+ self.parent = parent # 设置对父对象的引用
23
+ # 连接文档修改信号到槽函数
24
+ self.document().contentsChanged.connect(self.document_modified)
25
+
26
+ def lineNumberAreaWidth(self):
27
+ if not self.line_numbers_visible:
28
+ return 0
29
+ digits = len(str(self.blockCount()))
30
+ space = 3 + self.fontMetrics().width('9') * digits
31
+ return space
32
+
33
+ def updateLineNumberAreaWidth(self, _):
34
+ self.setViewportMargins(self.lineNumberAreaWidth(), 0, 0, 0)
35
+ self.lineNumberArea.setVisible(self.line_numbers_visible)
36
+
37
+ def updateLineNumberArea(self, rect, dy):
38
+ if dy:
39
+ self.lineNumberArea.scroll(0, dy)
40
+ else:
41
+ self.lineNumberArea.update(0, rect.y(), self.lineNumberArea.width(), rect.height())
42
+
43
+ if rect.contains(self.viewport().rect()):
44
+ self.updateLineNumberAreaWidth(0)
45
+
46
+ def resizeEvent(self, event):
47
+ super(TextEditor, self).resizeEvent(event)
48
+ cr = self.contentsRect()
49
+ self.lineNumberArea.setGeometry(QRect(cr.left(), cr.top(), self.lineNumberAreaWidth(), cr.height()))
50
+
51
+ def highlightCurrentLine(self):
52
+ extraSelections = []
53
+
54
+ if not self.isReadOnly():
55
+ selection = QTextEdit.ExtraSelection()
56
+ lineColor = QColor(Qt.gray).lighter(150)
57
+ selection.format.setBackground(lineColor)
58
+ selection.format.setProperty(QTextFormat.FullWidthSelection, True)
59
+ selection.cursor = self.textCursor()
60
+ selection.cursor.clearSelection()
61
+ extraSelections.append(selection)
62
+
63
+ self.setExtraSelections(extraSelections)
64
+
65
+ def wheelEvent(self, event):
66
+ self.current_font_size = self.font().pointSize()
67
+ modifiers = QApplication.keyboardModifiers()
68
+ if modifiers & Qt.ControlModifier :
69
+ angle = event.angleDelta().y()
70
+ if angle > 0:
71
+ self.current_font_size += 1
72
+ else:
73
+ self.current_font_size -= 1
74
+ new_font_size = min(max(self.current_font_size,6),60)
75
+ font = self.font()
76
+ font.setPointSize(new_font_size)
77
+ self.setFont(font)
78
+ event.accept()
79
+ else:
80
+ super().wheelEvent(event)
81
+
82
+ def lineNumberAreaPaintEvent(self, event):
83
+ if not self.line_numbers_visible:
84
+ return
85
+
86
+ painter = QPainter(self.lineNumberArea)
87
+
88
+ # 设置行号区域的背景颜色
89
+ backgroundColor = QColor(Qt.gray) # 例如,浅灰色
90
+ painter.fillRect(event.rect(), backgroundColor)
91
+
92
+ block = self.firstVisibleBlock()
93
+ blockNumber = block.blockNumber()
94
+ self.topnumber = blockNumber
95
+ top = int(self.blockBoundingGeometry(block).translated(self.contentOffset()).top())
96
+ bottom = top + int(self.blockBoundingRect(block).height())
97
+
98
+ # 设置行号的字体
99
+ font = QFont("Consolas")
100
+ font.setPointSize(self.font().pointSize())
101
+ painter.setFont(font)
102
+
103
+ # 设置行号的字体颜色
104
+ painter.setPen(self.lineNumberColor)
105
+
106
+ while block.isValid() and top <= event.rect().bottom():
107
+ if block.isVisible() and bottom >= event.rect().top():
108
+ number = str(blockNumber + 1)
109
+ painter.drawText(0, top, self.lineNumberArea.width(), self.fontMetrics().height(),
110
+ Qt.AlignRight, number)
111
+
112
+ block = block.next()
113
+ top = bottom
114
+ bottom = top + int(self.blockBoundingRect(block).height())
115
+ blockNumber += 1
116
+ return
117
+
118
+ def update_cursor_position(self):
119
+ cursor = self.textCursor()
120
+ line = cursor.blockNumber() + 1
121
+ col = cursor.columnNumber() + 1
122
+ if self.parent:
123
+ self.parent.status_label_center.setText(f"行: {line} ; 列: {col}")
124
+
125
+ def document_modified(self):
126
+ if self.document().isModified() and self.parent:
127
+ self.parent.status_label_right.setText("文档状态: 已修改")
128
+ self.is_saved = False # 文本更改后,设置未保存标志
129
+
130
+ class LineNumberArea(QWidget):
131
+ def __init__(self, editor):
132
+ super(LineNumberArea, self).__init__(editor)
133
+ self.textEditor = editor
134
+
135
+ def sizeHint(self):
136
+ return QSize(self.textEditor.lineNumberAreaWidth(), 0)
137
+
138
+ def paintEvent(self, event):
139
+ self.textEditor.lineNumberAreaPaintEvent(event)
140
+
141
+ class FindReplaceDialog(QDialog):
142
+ def __init__(self, parent=None, find=False, initial_text=''):
143
+ super(FindReplaceDialog, self).__init__(parent)
144
+
145
+ self.setWindowTitle("查找")
146
+
147
+ self.find_label = QLabel("查找内容:")
148
+ self.find_input = QLineEdit()
149
+ self.find_input.setText(initial_text) # 设置初始文本
150
+ self.replace_label = QLabel("替换为:")
151
+ self.replace_input = QLineEdit()
152
+
153
+ self.case_checkbox = QCheckBox("区分大小写")
154
+ self.whole_checkbox = QCheckBox("全字匹配")
155
+
156
+ self.find_button = QPushButton("查找")
157
+ self.replace_button = QPushButton("替换")
158
+ self.replace_all_button = QPushButton("全部替换")
159
+
160
+ layout = QVBoxLayout()
161
+ button_layout = QHBoxLayout()
162
+ layout.addWidget(self.find_label)
163
+ layout.addWidget(self.find_input)
164
+ layout.addWidget(self.case_checkbox)
165
+ layout.addWidget(self.whole_checkbox)
166
+
167
+ button_layout.addWidget(self.find_button)
168
+
169
+ if find:
170
+ self.setWindowTitle("查找和替换")
171
+ layout.addWidget(self.replace_label)
172
+ layout.addWidget(self.replace_input)
173
+ button_layout.addWidget(self.replace_button)
174
+ button_layout.addWidget(self.replace_all_button)
175
+
176
+ layout.addLayout(button_layout)
177
+
178
+ self.setLayout(layout)
179
+ self.installEventFilter(self) # 安装事件过滤器
180
+
181
+ def eventFilter(self, source, event):
182
+ if event.type() == QEvent.KeyPress:
183
+ if event.key() == Qt.Key_H and event.modifiers() == Qt.ControlModifier:
184
+ self.parent().replace_text() # 调用父窗口的替换对话框方法
185
+ self.close() # 关闭当前查找对话框
186
+ return True
187
+ elif event.key() == Qt.Key_F and event.modifiers() == Qt.ControlModifier:
188
+ self.parent().find_text() # 调用父窗口的查找对话框方法
189
+ self.close() # 关闭当前替换对话框
190
+ return True
191
+ return super().eventFilter(source, event)
192
+
193
+ def get_find_replace_texts(self):
194
+ return (
195
+ self.find_input.text(),
196
+ self.replace_input.text(),
197
+ self.case_checkbox.isChecked(),
198
+ self.whole_checkbox.isChecked()
199
+ )
200
+
201
+ class YunjiEditor(QMainWindow):
202
+ def __init__(self, filename=None):
203
+ super().__init__()
204
+ self.init_ui()
205
+ self.file_path = None # 初始化文件路径为None,表示未保存过
206
+ self.child_windows = [] # 存储子窗口实例的列表
207
+ self.is_saved = True # 用于跟踪文件是否已保存
208
+ self.text_edit.textChanged.connect(self.on_text_changed)
209
+ if filename:
210
+ self.open_file(filename)
211
+
212
+ def init_ui(self):
213
+ # 创建一个主窗口的中央小部件,并设置为布局
214
+ central_widget = QWidget()
215
+ self.setCentralWidget(central_widget)
216
+ layout = QVBoxLayout(central_widget)
217
+
218
+ # 设置布局小部件的背景颜色
219
+ background_widget = QWidget()
220
+ background_widget.setStyleSheet("QWidget { background-color: #99CCCC; }")
221
+ background_widget.setFixedHeight(1)
222
+ layout.addWidget(background_widget)
223
+
224
+ # 文件名标签
225
+ self.filename_label = QLabel(" ")
226
+ self.filename_label.setTextInteractionFlags(Qt.TextSelectableByMouse)
227
+ self.filename_label.setFixedHeight(20)
228
+ self.filename_label.setContentsMargins(5, 0, 0, 0)
229
+ self.filename_label.setStyleSheet("QLabel { background-color: #A4DDD3; color: #996600; }")
230
+ self.filename_label.setFont(QFont("Consolas", 12))
231
+ layout.addWidget(self.filename_label, alignment=Qt.AlignLeft)
232
+
233
+ # 创建状态栏
234
+ self.status_bar = QStatusBar()
235
+ self.setStatusBar(self.status_bar)
236
+
237
+ # 创建状态栏的标签
238
+ self.status_label_left = QLabel(os.path.dirname(os.path.realpath(__file__)))
239
+ self.status_label_left.setTextInteractionFlags(Qt.TextSelectableByMouse)
240
+ self.status_label_left.setFixedHeight(15)
241
+ self.status_label_left.setContentsMargins(5, 0, 0, 0)
242
+ self.status_label_left.setStyleSheet("QLabel { background-color: #A4DDD3; color: #000000; }")
243
+ self.status_label_left.setFont(QFont("Consolas", 10))
244
+
245
+ self.status_label_center = QLabel("行: 1 ; 列: 1")
246
+ self.status_label_center.setContentsMargins(10, 0, 10, 0)
247
+
248
+ self.status_label_right = QLabel("文档状态: 未修改")
249
+ self.status_label_right.setContentsMargins(10, 0, 10, 0)
250
+
251
+ # 将标签添加到状态栏
252
+ self.status_bar.addWidget(self.status_label_left, 3)
253
+ self.status_bar.addWidget(self.status_label_center, 1)
254
+ self.status_bar.addPermanentWidget(self.status_label_right,1)
255
+
256
+ #创建文本框-------------------------------------
257
+ self.text_edit = TextEditor(parent=self)
258
+ # self.text_edit.setPlainText("Hello\n" * 500) # 添加大量文本用于测试
259
+
260
+ # 设置 QTextEdit 的光标颜色和粗细
261
+ palette = self.text_edit.palette()
262
+ palette.setColor(QPalette.Highlight, QColor("#A4DDD3")) # 光标颜色
263
+ self.text_edit.setPalette(palette)
264
+ self.text_edit.setStyleSheet("QTextEdit { selection-background-color: #A4DDD3; }") # 设置选中背景颜色
265
+
266
+ font = QFont("Consolas", 14)
267
+ self.text_edit.setFont(font)
268
+ layout.addWidget(self.text_edit)
269
+ layout.setContentsMargins(5, 0, 5, 0)
270
+
271
+ # 设置图标路径
272
+ icon_base_path = os.path.join(os.path.dirname(__file__), "images")
273
+ open_path = os.path.join(icon_base_path, 'open.png')
274
+ save_path = os.path.join(icon_base_path, 'save.png')
275
+ save_as_path = os.path.join(icon_base_path, 'save_as.png')
276
+ editor_path = os.path.join(icon_base_path, 'editor.png')
277
+ add_path = os.path.join(icon_base_path, 'add.png')
278
+ self.setting_path = os.path.join(icon_base_path, 'setting.png')
279
+
280
+ # 创建一个透明图标,用于与复选框对齐
281
+ transparent_pixmap = QPixmap(16, 16)
282
+ transparent_pixmap.fill(Qt.transparent)
283
+ transparent_icon = QIcon(transparent_pixmap)
284
+ #region
285
+ # 文件菜单动作
286
+ open_action = QAction(QIcon(open_path), '打开', self)
287
+ open_action.setShortcut('Ctrl+O')
288
+ open_action.triggered.connect(self.open_file_dialog)
289
+
290
+ save_action = QAction(QIcon(save_path), '保存', self)
291
+ save_action.setShortcut('Ctrl+S')
292
+ save_action.triggered.connect(self.save_file)
293
+
294
+ save_as_action = QAction(QIcon(save_as_path), '另存为', self)
295
+ save_as_action.setShortcut('Ctrl+Shift+S')
296
+ save_as_action.triggered.connect(self.save_file_as)
297
+
298
+ add_action = QAction(QIcon(add_path), '新建窗口', self) # 新建窗口选项
299
+ add_action.setShortcut('Ctrl+N')
300
+ add_action.triggered.connect(self.new_window)
301
+
302
+ # 编辑菜单动作
303
+ undo_action = QAction('撤销', self)
304
+ undo_action.setShortcut('Ctrl+Z')
305
+ undo_action.triggered.connect(self.text_edit.undo)
306
+
307
+ redo_action = QAction('重做', self)
308
+ redo_action.setShortcut('Ctrl+Y')
309
+ redo_action.triggered.connect(self.text_edit.redo)
310
+
311
+ cut_action = QAction('剪切', self)
312
+ cut_action.setShortcut('Ctrl+X')
313
+ cut_action.triggered.connect(self.text_edit.cut)
314
+
315
+ copy_action = QAction('复制', self)
316
+ copy_action.setShortcut('Ctrl+C')
317
+ copy_action.triggered.connect(self.text_edit.copy)
318
+
319
+ paste_action = QAction('粘贴', self)
320
+ paste_action.setShortcut('Ctrl+V')
321
+ paste_action.triggered.connect(self.text_edit.paste)
322
+
323
+ find_action = QAction('查找', self)
324
+ find_action.setShortcut('Ctrl+F')
325
+ find_action.triggered.connect(self.find_text)
326
+
327
+ replace_action = QAction('替换', self)
328
+ replace_action.setShortcut('Ctrl+H')
329
+ replace_action.triggered.connect(self.replace_text)
330
+
331
+ # 格式菜单动作
332
+ font_bold_action = QAction('加粗', self)
333
+ font_bold_action.setShortcut('Ctrl+B')
334
+ font_bold_action.triggered.connect(self.bold_text)
335
+
336
+ font_italic_action = QAction('斜体', self)
337
+ font_italic_action.setShortcut('Ctrl+I')
338
+ font_italic_action.triggered.connect(self.italic_text)
339
+
340
+ # 创建设置正文颜色的动作
341
+ text_color_action = QAction('正文颜色', self)
342
+ text_color_action.setIcon(transparent_icon)
343
+ text_color_action.triggered.connect(self.set_text_color)
344
+
345
+ # 创建设置行号颜色的动作
346
+ line_number_color_action = QAction('行号颜色', self)
347
+ line_number_color_action.setIcon(transparent_icon)
348
+ line_number_color_action.triggered.connect(self.set_line_number_color)
349
+
350
+ # 创建显示行号的动作
351
+ self.show_line_numbers_action = QAction('显示行号', self, checkable=True, checked=False)
352
+ self.show_line_numbers_action.triggered.connect(self.toggle_line_numbers)
353
+
354
+ # 创建自动换行的动作和复选框
355
+ auto_wrap_action = QAction('自动换行', self, checkable=True)
356
+ auto_wrap_action.setChecked(True) # 默认选中自动换行
357
+ auto_wrap_action.triggered.connect(self.toggle_auto_wrap)
358
+
359
+ # 创建菜单栏--------------------------------------------------------------------
360
+ menubar = self.menuBar()
361
+
362
+ # 文件菜单
363
+ file_menu = menubar.addMenu('文件')
364
+ file_menu.addAction(add_action)
365
+ file_menu.addAction(open_action)
366
+ file_menu.addAction(save_action)
367
+ file_menu.addAction(save_as_action)
368
+
369
+ # 编辑菜单
370
+ edit_menu = menubar.addMenu('编辑')
371
+ edit_menu.addAction(undo_action)
372
+ edit_menu.addAction(redo_action)
373
+ edit_menu.addAction(cut_action)
374
+ edit_menu.addAction(copy_action)
375
+ edit_menu.addAction(paste_action)
376
+ edit_menu.addAction(find_action)
377
+ edit_menu.addAction(replace_action)
378
+
379
+ # 格式菜单
380
+ format_menu = menubar.addMenu('格式')
381
+ format_menu.addAction(font_bold_action)
382
+ format_menu.addAction(font_italic_action)
383
+
384
+ #视图菜单
385
+ view_menu = menubar.addMenu('视图')
386
+ view_menu.addAction(text_color_action)
387
+ view_menu.addAction(line_number_color_action)
388
+ view_menu.addAction(self.show_line_numbers_action)
389
+ view_menu.addAction(auto_wrap_action)
390
+ #endregion
391
+ # 创建一个按钮放置在菜单栏右侧
392
+ widget = QWidget(self)
393
+ hbox = QHBoxLayout(widget)
394
+ hbox.setContentsMargins(0, 10, 15, 0) # 设置右侧留白
395
+
396
+ setting_button = QPushButton(QIcon(self.setting_path), '')
397
+ setting_button.setIconSize(QSize(20, 20)) # 设置图标大小为20x20
398
+ setting_button.setToolTip('设置')
399
+ setting_button.clicked.connect(self.open_settings_dialog)
400
+ setting_button.setStyleSheet("QPushButton { width: 18px; height: 18px; background-color: #A4DDD3; border: none }")
401
+ setting_button.setCursor(Qt.PointingHandCursor) # 添加手形光标
402
+
403
+ hbox.addWidget(setting_button, alignment=Qt.AlignRight)
404
+ widget.setLayout(hbox)
405
+ menubar.setCornerWidget(widget, Qt.TopRightCorner)
406
+
407
+ self.setGeometry(200, 100, 1000, 900)
408
+ self.setWindowTitle('云记')
409
+ self.setWindowIcon(QIcon(editor_path))
410
+ self.setStyleSheet("""
411
+ QMainWindow {
412
+ background-color: #A4DDD3;
413
+ }
414
+ QTextEdit {
415
+ background-color: #ffffff;
416
+ padding: 0px;
417
+ border: 1px solid #ccc;
418
+ border-radius: 5px;
419
+ margin: 0px;
420
+ }
421
+ QMenuBar {
422
+ background-color: #A4DDD3;
423
+ font: 16px;
424
+ padding: 0px;
425
+ }
426
+ QMenuBar::item {
427
+ padding: 8px 10px;
428
+ background: transparent;
429
+ border-radius: 5px;
430
+ }
431
+ QMenuBar::item:selected {
432
+ background: #ABD7EC;
433
+ color: white;
434
+ }
435
+ QMenu {
436
+ background-color: #A4DDD3;
437
+ border: 1px solid #ccc;
438
+ border-radius: 5px;
439
+ }
440
+ QMenu::item {
441
+ padding: 8px 10px;
442
+ background: transparent;
443
+ border-radius: 5px;
444
+ }
445
+ QMenu::item:selected {
446
+ background-color: #A4DDD3;
447
+ color: white;
448
+ }
449
+ """)
450
+ self.show()
451
+
452
+ def wheelEvent(self, event):
453
+ self.text_edit.wheelEvent(event)
454
+
455
+ def open_file_dialog(self):
456
+ file_path, _ = QFileDialog.getOpenFileName(self, '打开文件', '', '文本文件 (*.txt);;所有文件 (*)')
457
+ if file_path:
458
+ self.open_file(file_path)
459
+
460
+ def open_file(self, file_path):
461
+ self.file_path = file_path
462
+ with open(file_path, 'r', encoding='utf-8') as file:
463
+ try:
464
+ self.text_edit.setPlainText(file.read())
465
+ self.filename_label.setText(os.path.basename(file_path))
466
+ self.status_label_left.setText(f'打开文件: {file_path}')
467
+ self.status_label_right.setText("文档状态: 已打开")
468
+ except FileNotFoundError:
469
+ QMessageBox.critical(self, "File Error", f"文件 '{os.path.basename(file_path)}' 不存在.")
470
+ self.clear_text_edit() # 清空文本编辑器内容或者执行其他回滚操作
471
+ except Exception as e:
472
+ QMessageBox.critical(self, "Error", f"无法打开文件 '{os.path.basename(file_path)}': {str(e)}")
473
+ self.clear_text_edit() # 清空文本编辑器内容或者执行其他回滚操作
474
+
475
+ def clear_text_edit(self):
476
+ self.text_edit.clear()
477
+ self.status_label_left.setText("当前路径: ")
478
+ self.status_label_center.setText("行: 1 ; 列: 1")
479
+ self.status_label_right.setText("文档状态: 未修改")
480
+
481
+ def save_file(self):
482
+ if self.file_path:
483
+ with open(self.file_path, 'w', encoding='utf-8') as file:
484
+ file.write(self.text_edit.toPlainText())
485
+ self.filename_label.setText(os.path.basename(self.file_path))
486
+ self.status_label_left.setText(f'保存文件: {self.file_path}')
487
+ self.status_label_right.setText("文档状态: 已保存")
488
+ self.is_saved = True # 设置已保存标志
489
+ else:
490
+ self.save_file_as()
491
+
492
+ def save_file_as(self):
493
+ file_path, _ = QFileDialog.getSaveFileName(self, '另存为', '', '文本文件 (*.txt);;所有文件 (*)')
494
+ if file_path:
495
+ self.file_path = file_path
496
+ with open(file_path, 'w', encoding='utf-8') as file:
497
+ file.write(self.text_edit.toPlainText())
498
+ self.filename_label.setText(os.path.basename(file_path))
499
+ self.status_label_left.setText(f'另存文件: {file_path}')
500
+ self.status_label_right.setText("文档状态: 已保存")
501
+ self.is_saved = True # 设置已保存标志
502
+
503
+ def new_window(self):
504
+ new_window = YunjiEditor()
505
+ self.child_windows.append(new_window)
506
+ new_window.show()
507
+
508
+ def find_text(self):
509
+ if hasattr(self, 'find_dialog') and self.find_dialog.isVisible():
510
+ self.find_dialog.close()
511
+ selected_text = self.text_edit.textCursor().selectedText()
512
+ self.find_dialog = FindReplaceDialog(self, initial_text=selected_text) # 创建查找/替换对话框实例,传入选中的文本
513
+ self.find_dialog.find_button.clicked.connect(self.find_next_text)
514
+ self.find_dialog.show() # 显示非模态对话框
515
+
516
+ def replace_text(self):
517
+ if hasattr(self, 'find_dialog') and self.find_dialog.isVisible():
518
+ self.find_dialog.close()
519
+ selected_text = self.text_edit.textCursor().selectedText()
520
+ self.find_dialog = FindReplaceDialog(self,True, initial_text=selected_text) # 创建查找/替换对话框实例,传入选中的文本
521
+ self.find_dialog.find_button.clicked.connect(self.find_next_text)
522
+ self.find_dialog.replace_button.clicked.connect(self.replace_next_text)
523
+ self.find_dialog.replace_all_button.clicked.connect(self.replace_all_text)
524
+ self.find_dialog.show() # 显示非模态对话框
525
+
526
+ def find_next_text(self):
527
+ find_str, _, case_sensitive, whole_words = self.find_dialog.get_find_replace_texts()
528
+ if find_str:
529
+ flags = QTextDocument.FindFlags()
530
+ if case_sensitive:
531
+ flags |= QTextDocument.FindCaseSensitively
532
+ if whole_words:
533
+ flags |= QTextDocument.FindWholeWords
534
+
535
+ cursor = self.text_edit.textCursor()
536
+ document = self.text_edit.document()
537
+ cursor = document.find(find_str, cursor, flags)
538
+ if cursor.isNull():
539
+ # 如果查找不到,从文档开头重新查找一次
540
+ cursor = QTextCursor(document)
541
+ msg_box = QMessageBox(self)
542
+ msg_box.setWindowTitle(' ')
543
+ msg_box.setPlainText('已经从第一行开始查找')
544
+ msg_box.exec_()
545
+
546
+ cursor = document.find(find_str, cursor, flags)
547
+ if cursor.isNull():
548
+ QMessageBox.information(self, '查找', f'未找到"{find_str}"')
549
+ else:
550
+ self.text_edit.setTextCursor(cursor) #文中高亮显示
551
+ else:
552
+ self.text_edit.setTextCursor(cursor)
553
+
554
+ def replace_next_text(self):
555
+ find_str, replace_str, case_sensitive, whole_words = self.find_dialog.get_find_replace_texts()
556
+ if find_str:
557
+ flags = QTextDocument.FindFlags()
558
+ if case_sensitive:
559
+ flags |= QTextDocument.FindCaseSensitively
560
+ if whole_words:
561
+ flags |= QTextDocument.FindWholeWords
562
+
563
+ cursor = self.text_edit.textCursor()
564
+ document = self.text_edit.document()
565
+ cursor = document.find(find_str, cursor, flags)
566
+ if cursor.isNull():
567
+ # 如果查找不到,从文档开头重新查找一次
568
+ cursor = QTextCursor(document)
569
+ msg_box = QMessageBox(self)
570
+ msg_box.setWindowTitle(' ')
571
+ msg_box.setPlainText('已经从第一行开始查找')
572
+ msg_box.exec_()
573
+ cursor = document.find(find_str, cursor, flags)
574
+ if cursor.isNull():
575
+ QMessageBox.information(self, '查找', f'未找到"{find_str}"')
576
+ else:
577
+ cursor.insertText(replace_str)
578
+ self.text_edit.setTextCursor(cursor)
579
+ else:
580
+ cursor.insertText(replace_str)
581
+ self.text_edit.setTextCursor(cursor)
582
+
583
+ def replace_all_text(self):
584
+ find_str, replace_str, case_sensitive, whole_words = self.find_dialog.get_find_replace_texts()
585
+ if find_str:
586
+ flags = QTextDocument.FindFlags()
587
+ if case_sensitive:
588
+ flags |= QTextDocument.FindCaseSensitively
589
+ if whole_words:
590
+ flags |= QTextDocument.FindWholeWords
591
+
592
+ cursor = self.text_edit.textCursor()
593
+ document = self.text_edit.document()
594
+ replacements = 0
595
+ started_from_beginning = False
596
+ while True:
597
+ cursor = document.find(find_str, cursor, flags)
598
+ if cursor.isNull():
599
+ if started_from_beginning:
600
+ break
601
+ else:
602
+ # 如果查找不到,从文档开头重新查找一次
603
+ started_from_beginning = True
604
+ cursor = QTextCursor(document)
605
+ msg_box = QMessageBox(self)
606
+ msg_box.setWindowTitle(' ')
607
+ msg_box.setPlainText('已经从第一行开始查找')
608
+ msg_box.exec_()
609
+ continue
610
+
611
+ # 替换前检查当前文本是否与查找文本相同,避免自我替换陷入死循环
612
+ if cursor.selectedText() == find_str:
613
+ cursor.insertText(replace_str)
614
+ replacements += 1
615
+
616
+ if replacements > 0:
617
+ QMessageBox.information(self, '替换', f'全部替换完成,共替换了 {replacements} 个匹配项。')
618
+ else:
619
+ QMessageBox.information(self, '查找', f'未找到"{find_str}"')
620
+
621
+ def bold_text(self):
622
+ cursor = self.text_edit.textCursor()
623
+ if cursor.charFormat().fontWeight() != QFont.Bold:
624
+ cursor.mergeCharFormat(self.bold_format())
625
+ else:
626
+ cursor.mergeCharFormat(self.normal_format())
627
+
628
+ def italic_text(self):
629
+ cursor = self.text_edit.textCursor()
630
+ if not cursor.charFormat().fontItalic():
631
+ cursor.mergeCharFormat(self.italic_format())
632
+ else:
633
+ cursor.mergeCharFormat(self.normal_format())
634
+
635
+ def bold_format(self):
636
+ format_ = QTextCharFormat()
637
+ format_.setFontWeight(QFont.Bold)
638
+ return format_
639
+
640
+ def italic_format(self):
641
+ format_ = QTextCharFormat()
642
+ format_.setFontItalic(True)
643
+ return format_
644
+
645
+ def normal_format(self):
646
+ format_ = QTextCharFormat()
647
+ format_.setFontWeight(QFont.Normal)
648
+ format_.setFontItalic(False)
649
+ return format_
650
+
651
+ def open_settings_dialog(self):
652
+ font, ok = QFontDialog.getFont(self.text_edit.font(), self, "字体设置")
653
+ if ok:
654
+ self.text_edit.setFont(font)
655
+
656
+ def toggle_line_numbers(self):
657
+ self.text_edit.line_numbers_visible = self.show_line_numbers_action.isChecked()
658
+ self.text_edit.updateLineNumberAreaWidth(0)
659
+ self.text_edit.update()
660
+
661
+ def toggle_auto_wrap(self, checked):
662
+ self.text_edit.setLineWrapMode(QPlainTextEdit.WidgetWidth if checked else QPlainTextEdit.NoWrap)
663
+
664
+ def set_line_number_color(self):
665
+ color = QColorDialog.getColor()
666
+ if color.isValid():
667
+ self.text_edit.lineNumberColor = color
668
+ self.text_edit.lineNumberArea.update()
669
+
670
+ def set_text_color(self):
671
+ color = QColorDialog.getColor()
672
+ if color.isValid():
673
+ self.text_edit.setStyleSheet(f'color: {color.name()};')
674
+
675
+ def closeEvent(self, event):
676
+ if not self.is_saved and self.text_edit.document().isModified(): # 检查文本内容是否被修改过
677
+ reply = QMessageBox.question(self, '确认关闭', '是否保存已修改的内容?',
678
+ QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)
679
+ if reply == QMessageBox.Save:
680
+ self.save_file()
681
+ elif reply == QMessageBox.Cancel:
682
+ event.ignore() # 忽略关闭事件
683
+ return
684
+ event.accept()
685
+
686
+ def on_text_changed(self):
687
+ self.is_saved = False # 文本更改后,设置未保存标志
688
+
689
+ def open_with_yunji(filename=None):
690
+ app = QApplication(sys.argv)
691
+ editor = YunjiEditor(filename)
692
+ editor.show()
693
+ app.exec_()
694
+
695
+ def cli_editor():
696
+ if len(sys.argv) > 1:
697
+ file_path = sys.argv[1]
698
+ open_with_yunji(file_path)
699
+ else:
700
+ open_with_yunji()
701
+
702
+ if __name__ == "__main__":
703
+ cli_editor()
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,55 @@
1
+ Metadata-Version: 2.1
2
+ Name: yunji
3
+ Version: 0.1
4
+ Summary: A PyQt5-based text editor
5
+ Home-page: https://github.com/YSP0Github/yunji.git
6
+ Author: Yun Song
7
+ Author-email: ysp@cug.edu.cn
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.6
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: PyQt5
15
+
16
+ # 云记 (Yunji Editor)
17
+
18
+ ## 简介
19
+ 云记是一款基于PyQt5的简洁文本编辑器,支持基本的文本编辑功能,包括打开、保存、查找和替换文本,支持显示行号、设置文本颜色等功能。
20
+
21
+ ## 功能
22
+ - 打开文件、保存文件、另存为
23
+ - 撤销、重做、剪切、复制、粘贴
24
+ - 查找和替换文本
25
+ - 设置文本颜色和行号颜色
26
+ - 显示行号
27
+ - 自动换行
28
+ - 快捷键支持
29
+
30
+ ## 快捷键
31
+ - `Ctrl+O`:打开文件
32
+ - `Ctrl+S`:保存文件
33
+ - `Ctrl+Shift+S`:另存为
34
+ - `Ctrl+N`:新建窗口
35
+ - `Ctrl+Z`:撤销
36
+ - `Ctrl+Y`:重做
37
+ - `Ctrl+X`:剪切
38
+ - `Ctrl+C`:复制
39
+ - `Ctrl+V`:粘贴
40
+ - `Ctrl+F`:查找
41
+ - `Ctrl+H`:替换
42
+ - `Ctrl+B`:加粗
43
+ - `Ctrl+I`:斜体
44
+
45
+ ## 安装和运行
46
+
47
+ ### 环境要求
48
+ - Python 3.6+
49
+ - PyQt5
50
+
51
+ ### 安装依赖
52
+ 首先确保你已经安装了Python 3.6以上的版本。然后,使用pip安装所需的库:
53
+
54
+ ```bash
55
+ pip install -r requirements.txt
@@ -0,0 +1,23 @@
1
+ LICENSE
2
+ README.md
3
+ setup.py
4
+ tests/__init__.py
5
+ tests/test_editor.py
6
+ yunji/__init__.py
7
+ yunji/editor.py
8
+ yunji.egg-info/PKG-INFO
9
+ yunji.egg-info/SOURCES.txt
10
+ yunji.egg-info/dependency_links.txt
11
+ yunji.egg-info/entry_points.txt
12
+ yunji.egg-info/requires.txt
13
+ yunji.egg-info/top_level.txt
14
+ yunji/images/add.png
15
+ yunji/images/add_g.png
16
+ yunji/images/editor.png
17
+ yunji/images/editor_.png
18
+ yunji/images/font-size.png
19
+ yunji/images/open.png
20
+ yunji/images/save.png
21
+ yunji/images/save_as.png
22
+ yunji/images/setting-two.png
23
+ yunji/images/setting.png
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ yunji = yunji.editor:cli_editor
@@ -0,0 +1 @@
1
+ PyQt5
@@ -0,0 +1,2 @@
1
+ tests
2
+ yunji