Skip to content

Commit

Permalink
Merge pull request #145 from pankajp/qt-codeeditor-honor-line-number-…
Browse files Browse the repository at this point in the history
…widget

Qt CodeEditor: Honor `show_line_numbers` and `readonly`.
  • Loading branch information
corranwebster committed Dec 16, 2013
2 parents 5075b19 + 47d5f6e commit 9604b69
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 7 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -10,6 +10,7 @@ before_install:
install:
# nose is already installed
- pip install unittest2
- pip install pygments
- sudo apt-get install python-numpy
# Test against the current master of traits, traitsui and enable
- pip install git+http://github.com/enthought/traits.git#egg=traits
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.txt
Expand Up @@ -4,6 +4,9 @@ Traits UI Changelog
What's new in TraitsUI 4.5.0
----------------------------

Fixes

* Qt CodeEditor now honors 'show_line_numbers' and the 'readonly' style (#137)


Release 4.4.0
Expand Down
12 changes: 5 additions & 7 deletions traitsui/qt4/code_editor.py
Expand Up @@ -121,15 +121,17 @@ def init ( self, parent ):

# Set up listeners for the signals we care about
code_editor = self._widget.code
if not self.readonly:
if self.readonly:
code_editor.setReadOnly(True)
else:
code_editor.textChanged.connect(self.update_object)
if factory.auto_set:
code_editor.textChanged.connect(self.update_object)
if factory.selected_text != '':
code_editor.selectionChanged.connect(self._selection_changed)
if (factory.line != '') or (factory.column != ''):
code_editor.cursorPositionChanged.connect(self._position_changed)

code_editor.line_number_widget.setVisible(factory.show_line_numbers)

# Make sure the editor has been initialized:
self.update_editor()

Expand Down Expand Up @@ -206,9 +208,6 @@ def update_editor ( self ):
if control.code.toPlainText() != new_value:
control.code.setPlainText(new_value)

# TODO: check the readonly flag and make sure the editor
# is still readonly when we're done.

if self.factory.selected_line:
# TODO: update the factory selected line
pass
Expand Down Expand Up @@ -356,7 +355,6 @@ def _style_document(self):




# Define the simple, custom, text and readonly editors, which will be accessed
# by the editor factory for code editors.

Expand Down
91 changes: 91 additions & 0 deletions traitsui/tests/editors/test_code_editor.py
@@ -0,0 +1,91 @@
#------------------------------------------------------------------------------
#
# Copyright (c) 2013, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
#------------------------------------------------------------------------------

from traits.has_traits import HasTraits
from traits.trait_types import Bool, Enum, Instance, Str
from traitsui.handler import ModelView
from traitsui.view import View, Group
from traitsui.item import Item
from traitsui.editors.code_editor import CodeEditor


from traitsui.tests._tools import *


class CodeModel(HasTraits):
code = Str('world domination code')

class CodeView(ModelView):
model = Instance(CodeModel)
show_line_numbers = Bool(True)
style = Enum('simple', 'readonly')

def default_traits_view(self):
traits_view = View(
Item('model.code',
editor=CodeEditor(show_line_numbers=self.show_line_numbers),
style=self.style)
)
return traits_view


@skip_if_not_qt4
def test_code_editor_show_line_numbers():
""" CodeEditor should honor the `show_line_numbers` setting
"""
def is_line_numbers_visible(ui):
from pyface import qt
txt_ctrl = ui.control.findChild(qt.QtGui.QPlainTextEdit)
return txt_ctrl.line_number_widget.isVisible()

def test_line_numbers_visibility(show=True):
with store_exceptions_on_all_threads():
code_model = CodeModel()
code_view = CodeView(model=code_model,
show_line_numbers=show)
ui = code_view.edit_traits()
nose.tools.assert_equal(is_line_numbers_visible(ui), show)
ui.control.close()

test_line_numbers_visibility(True)
test_line_numbers_visibility(False)

@skip_if_not_qt4
def test_code_editor_readonly():
""" Test readonly editor style for CodeEditor
"""
from pyface import qt
with store_exceptions_on_all_threads():
code_model = CodeModel()
code_view = CodeView(model=code_model,
style='readonly')
ui = code_view.edit_traits()
txt_ctrl = ui.control.findChild(qt.QtGui.QPlainTextEdit)
nose.tools.assert_true(txt_ctrl.isReadOnly())

# Test changing the object's text
nose.tools.assert_equal(txt_ctrl.toPlainText(), code_model.code)
code_model.code += 'some more code'
nose.tools.assert_true(txt_ctrl.isReadOnly())
nose.tools.assert_equal(txt_ctrl.toPlainText(), code_model.code)

# Test changing the underlying object
code_model2 = CodeModel(code=code_model.code*2)
code_view.model = code_model2
nose.tools.assert_true(txt_ctrl.isReadOnly())
nose.tools.assert_equal(txt_ctrl.toPlainText(), code_model.code)

ui.control.close()


if __name__ == '__main__':
nose.main()

0 comments on commit 9604b69

Please sign in to comment.