~ubuntu-branches/debian/experimental/spyder/experimental

« back to all changes in this revision

Viewing changes to spyderlib/widgets/texteditor.py

  • Committer: Package Import Robot
  • Author(s): Picca Frédéric-Emmanuel
  • Date: 2013-02-27 09:51:28 UTC
  • mfrom: (1.1.18)
  • Revision ID: package-import@ubuntu.com-20130227095128-wtx1irpvf4vl79lj
Tags: 2.2.0~beta3+dfsg-1
* Imported Upstream version 2.2.0~beta3+dfsg
* debian /patches
  - 0002-feature-forwarded-add-icon-to-desktop-file.patch (deleted)
    this patch was integrated by the upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
#
3
 
# Copyright © 2009-2010 Pierre Raybaut
4
 
# Licensed under the terms of the MIT License
5
 
# (see spyderlib/__init__.py for details)
6
 
 
7
 
"""
8
 
Text Editor Dialog based on Qt
9
 
"""
10
 
 
11
 
from spyderlib.qt.QtCore import Qt, SIGNAL, SLOT
12
 
from spyderlib.qt.QtGui import QVBoxLayout, QTextEdit, QDialog, QDialogButtonBox
13
 
 
14
 
# Local import
15
 
from spyderlib.baseconfig import _
16
 
from spyderlib.guiconfig import get_icon, get_font
17
 
 
18
 
 
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)
24
 
        
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)
30
 
        
31
 
        self.text = None
32
 
        
33
 
        self._conv = str if isinstance(text, str) else unicode
34
 
        
35
 
        self.layout = QVBoxLayout()
36
 
        self.setLayout(self.layout)
37
 
 
38
 
        # Text edit
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)
43
 
        if font is None:
44
 
            font = get_font('texteditor')
45
 
        self.edit.setFont(font)
46
 
        self.layout.addWidget(self.edit)
47
 
 
48
 
        # Buttons configuration
49
 
        buttons = QDialogButtonBox.Ok
50
 
        if not readonly:
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)
56
 
        
57
 
        # Make the dialog act as a window
58
 
        self.setWindowFlags(Qt.Window)
59
 
        
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])
64
 
    
65
 
    def text_changed(self):
66
 
        """Text has changed"""
67
 
        self.text = self._conv(self.edit.toPlainText())
68
 
        
69
 
    def get_value(self):
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
73
 
        return self.text
74
 
    
75
 
    
76
 
def test():
77
 
    """Text editor demo"""
78
 
    from spyderlib.utils.qthelpers import qapplication
79
 
    _app = qapplication()
80
 
    dialog = TextEditor("""
81
 
    01234567890123456789012345678901234567890123456789012345678901234567890123456789
82
 
    dedekdh elkd ezd ekjd lekdj elkdfjelfjk e
83
 
    """)
84
 
    dialog.show()
85
 
    if dialog.exec_():
86
 
        text = dialog.get_value()
87
 
        print "Accepted:", text
88
 
        dialog = TextEditor(text)
89
 
        dialog.exec_()
90
 
    else:
91
 
        print "Canceled"
92
 
 
93
 
if __name__ == "__main__":
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright © 2009-2010 Pierre Raybaut
 
4
# Licensed under the terms of the MIT License
 
5
# (see spyderlib/__init__.py for details)
 
6
 
 
7
"""
 
8
Text Editor Dialog based on Qt
 
9
"""
 
10
 
 
11
from spyderlib.qt.QtCore import Qt, SIGNAL, SLOT
 
12
from spyderlib.qt.QtGui import QVBoxLayout, QTextEdit, QDialog, QDialogButtonBox
 
13
 
 
14
# Local import
 
15
from spyderlib.baseconfig import _
 
16
from spyderlib.guiconfig import get_font
 
17
from spyderlib.utils.qthelpers import get_icon
 
18
 
 
19
 
 
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)
 
25
        
 
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)
 
31
        
 
32
        self.text = None
 
33
        
 
34
        self._conv = str if isinstance(text, str) else unicode
 
35
        
 
36
        self.layout = QVBoxLayout()
 
37
        self.setLayout(self.layout)
 
38
 
 
39
        # Text edit
 
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)
 
44
        if font is None:
 
45
            font = get_font('texteditor')
 
46
        self.edit.setFont(font)
 
47
        self.layout.addWidget(self.edit)
 
48
 
 
49
        # Buttons configuration
 
50
        buttons = QDialogButtonBox.Ok
 
51
        if not readonly:
 
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)
 
57
        
 
58
        # Make the dialog act as a window
 
59
        self.setWindowFlags(Qt.Window)
 
60
        
 
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])
 
65
    
 
66
    def text_changed(self):
 
67
        """Text has changed"""
 
68
        self.text = self._conv(self.edit.toPlainText())
 
69
        
 
70
    def get_value(self):
 
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
 
74
        return self.text
 
75
    
 
76
    
 
77
def test():
 
78
    """Text editor demo"""
 
79
    from spyderlib.utils.qthelpers import qapplication
 
80
    _app = qapplication()
 
81
    dialog = TextEditor("""
 
82
    01234567890123456789012345678901234567890123456789012345678901234567890123456789
 
83
    dedekdh elkd ezd ekjd lekdj elkdfjelfjk e
 
84
    """)
 
85
    dialog.show()
 
86
    if dialog.exec_():
 
87
        text = dialog.get_value()
 
88
        print "Accepted:", text
 
89
        dialog = TextEditor(text)
 
90
        dialog.exec_()
 
91
    else:
 
92
        print "Canceled"
 
93
 
 
94
if __name__ == "__main__":
94
95
    test()
 
 
b'\\ No newline at end of file'