1
1
import os
2
+ import re
3
+ import typing
2
4
3
5
from PyQt5 import uic
4
6
from PyQt5 .QtCore import QDir , pyqtSignal
7
+ from PyQt5 .QtGui import QValidator
5
8
from PyQt5 .QtWidgets import QAction , QFileDialog , QSizePolicy , QTreeWidgetItem
6
9
7
10
from tribler .gui .defs import BUTTON_TYPE_NORMAL
@@ -17,6 +20,24 @@ def __init__(self, parent):
17
20
QTreeWidgetItem .__init__ (self , parent )
18
21
19
22
23
+ class TorrentNameValidator (QValidator ):
24
+ """
25
+ Validator used in Torrent name QLineEdit field to disallow multiline text.
26
+ If a new line character is detected, then it is converted to space using fixup().
27
+
28
+ Docs: https://doc.qt.io/qtforpython-5/PySide2/QtGui/QValidator.html
29
+ """
30
+ ESCAPE_CHARS_REGEX = r'[\n\r\t]'
31
+
32
+ def validate (self , text : str , pos : int ) -> typing .Tuple ['QValidator.State' , str , int ]:
33
+ if re .search (self .ESCAPE_CHARS_REGEX , text ):
34
+ return QValidator .Intermediate , text , pos
35
+ return QValidator .Acceptable , text , pos
36
+
37
+ def fixup (self , text : str ) -> str :
38
+ return re .sub (self .ESCAPE_CHARS_REGEX , ' ' , text )
39
+
40
+
20
41
class CreateTorrentDialog (DialogContainer ):
21
42
create_torrent_notification = pyqtSignal (dict )
22
43
add_to_channel_selected = pyqtSignal (str )
@@ -27,6 +48,7 @@ def __init__(self, parent):
27
48
uic .loadUi (get_ui_file_path ('createtorrentdialog.ui' ), self .dialog_widget )
28
49
29
50
self .dialog_widget .setSizePolicy (QSizePolicy .Fixed , QSizePolicy .Expanding )
51
+ self .dialog_widget .create_torrent_name_field .setValidator (TorrentNameValidator (parent = self ))
30
52
connect (self .dialog_widget .btn_cancel .clicked , self .close_dialog )
31
53
connect (self .dialog_widget .create_torrent_choose_files_button .clicked , self .on_choose_files_clicked )
32
54
connect (self .dialog_widget .create_torrent_choose_dir_button .clicked , self .on_choose_dir_clicked )
0 commit comments