1
# -*- coding: utf-8 -*-
3
# Copyright © 2009-2010 Pierre Raybaut
4
# Licensed under the terms of the MIT License
5
# (see spyderlib/__init__.py for details)
8
Text Editor Dialog based on Qt
11
from spyderlib.qt.QtCore import Qt, SIGNAL, SLOT
12
from spyderlib.qt.QtGui import QVBoxLayout, QTextEdit, QDialog, QDialogButtonBox
15
from spyderlib.baseconfig import _
16
from spyderlib.guiconfig import get_icon, get_font
19
class TextEditor(QDialog):
20
"""Array Editor Dialog"""
21
def __init__(self, text, title='', font=None, parent=None,
22
readonly=False, size=(400, 300)):
23
QDialog.__init__(self, parent)
25
# Destroying the C++ object right after closing the dialog box,
26
# otherwise it may be garbage-collected in another QThread
27
# (e.g. the editor's analysis thread in Spyder), thus leading to
28
# a segmentation fault on UNIX or an application crash on Windows
29
self.setAttribute(Qt.WA_DeleteOnClose)
33
self._conv = str if isinstance(text, str) else unicode
35
self.layout = QVBoxLayout()
36
self.setLayout(self.layout)
39
self.edit = QTextEdit(parent)
40
self.connect(self.edit, SIGNAL('textChanged()'), self.text_changed)
41
self.edit.setReadOnly(readonly)
42
self.edit.setPlainText(text)
44
font = get_font('texteditor')
45
self.edit.setFont(font)
46
self.layout.addWidget(self.edit)
48
# Buttons configuration
49
buttons = QDialogButtonBox.Ok
51
buttons = buttons | QDialogButtonBox.Cancel
52
bbox = QDialogButtonBox(buttons)
53
self.connect(bbox, SIGNAL("accepted()"), SLOT("accept()"))
54
self.connect(bbox, SIGNAL("rejected()"), SLOT("reject()"))
55
self.layout.addWidget(bbox)
57
# Make the dialog act as a window
58
self.setWindowFlags(Qt.Window)
60
self.setWindowIcon(get_icon('edit.png'))
61
self.setWindowTitle(_("Text editor") + \
62
"%s" % (" - "+str(title) if str(title) else ""))
63
self.resize(size[0], size[1])
65
def text_changed(self):
66
"""Text has changed"""
67
self.text = self._conv(self.edit.toPlainText())
70
"""Return modified text"""
71
# It is import to avoid accessing Qt C++ object as it has probably
72
# already been destroyed, due to the Qt.WA_DeleteOnClose attribute
77
"""Text editor demo"""
78
from spyderlib.utils.qthelpers import qapplication
80
dialog = TextEditor("""
81
01234567890123456789012345678901234567890123456789012345678901234567890123456789
82
dedekdh elkd ezd ekjd lekdj elkdfjelfjk e
86
text = dialog.get_value()
87
print "Accepted:", text
88
dialog = TextEditor(text)
93
if __name__ == "__main__":
1
# -*- coding: utf-8 -*-
3
# Copyright © 2009-2010 Pierre Raybaut
4
# Licensed under the terms of the MIT License
5
# (see spyderlib/__init__.py for details)
8
Text Editor Dialog based on Qt
11
from spyderlib.qt.QtCore import Qt, SIGNAL, SLOT
12
from spyderlib.qt.QtGui import QVBoxLayout, QTextEdit, QDialog, QDialogButtonBox
15
from spyderlib.baseconfig import _
16
from spyderlib.guiconfig import get_font
17
from spyderlib.utils.qthelpers import get_icon
20
class TextEditor(QDialog):
21
"""Array Editor Dialog"""
22
def __init__(self, text, title='', font=None, parent=None,
23
readonly=False, size=(400, 300)):
24
QDialog.__init__(self, parent)
26
# Destroying the C++ object right after closing the dialog box,
27
# otherwise it may be garbage-collected in another QThread
28
# (e.g. the editor's analysis thread in Spyder), thus leading to
29
# a segmentation fault on UNIX or an application crash on Windows
30
self.setAttribute(Qt.WA_DeleteOnClose)
34
self._conv = str if isinstance(text, str) else unicode
36
self.layout = QVBoxLayout()
37
self.setLayout(self.layout)
40
self.edit = QTextEdit(parent)
41
self.connect(self.edit, SIGNAL('textChanged()'), self.text_changed)
42
self.edit.setReadOnly(readonly)
43
self.edit.setPlainText(text)
45
font = get_font('texteditor')
46
self.edit.setFont(font)
47
self.layout.addWidget(self.edit)
49
# Buttons configuration
50
buttons = QDialogButtonBox.Ok
52
buttons = buttons | QDialogButtonBox.Cancel
53
bbox = QDialogButtonBox(buttons)
54
self.connect(bbox, SIGNAL("accepted()"), SLOT("accept()"))
55
self.connect(bbox, SIGNAL("rejected()"), SLOT("reject()"))
56
self.layout.addWidget(bbox)
58
# Make the dialog act as a window
59
self.setWindowFlags(Qt.Window)
61
self.setWindowIcon(get_icon('edit.png'))
62
self.setWindowTitle(_("Text editor") + \
63
"%s" % (" - "+str(title) if str(title) else ""))
64
self.resize(size[0], size[1])
66
def text_changed(self):
67
"""Text has changed"""
68
self.text = self._conv(self.edit.toPlainText())
71
"""Return modified text"""
72
# It is import to avoid accessing Qt C++ object as it has probably
73
# already been destroyed, due to the Qt.WA_DeleteOnClose attribute
78
"""Text editor demo"""
79
from spyderlib.utils.qthelpers import qapplication
81
dialog = TextEditor("""
82
01234567890123456789012345678901234567890123456789012345678901234567890123456789
83
dedekdh elkd ezd ekjd lekdj elkdfjelfjk e
87
text = dialog.get_value()
88
print "Accepted:", text
89
dialog = TextEditor(text)
94
if __name__ == "__main__":
b'\\ No newline at end of file'