This repository has been archived by the owner on May 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #260 from enthought/feature-multiline
Feature multiline
- Loading branch information
Showing
8 changed files
with
382 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#------------------------------------------------------------------------------ | ||
# Copyright (c) 2013, Enthought, Inc. | ||
# All rights reserved. | ||
#------------------------------------------------------------------------------ | ||
from .qt.QtGui import QTextEdit | ||
from .qt.QtCore import Signal, QTimer | ||
from .qt_control import QtControl | ||
|
||
|
||
class QMultilineEdit(QTextEdit): | ||
""" A QTextEdit which notifies on a collapsing timer. | ||
""" | ||
delayedTextChanged = Signal() | ||
|
||
def __init__(self, parent=None): | ||
super(QMultilineEdit, self).__init__(parent) | ||
self._changed_timer = timer = QTimer() | ||
timer.setInterval(200) | ||
timer.setSingleShot(True) | ||
timer.timeout.connect(self.delayedTextChanged) | ||
self.textChanged.connect(timer.start) | ||
|
||
|
||
class QtMultilineField(QtControl): | ||
""" A Qt4 implementation of an Enaml Field. | ||
""" | ||
#: Whether or not to auto synchronize the text on change. | ||
_auto_sync_text = True | ||
|
||
#-------------------------------------------------------------------------- | ||
# Setup Methods | ||
#-------------------------------------------------------------------------- | ||
def create_widget(self, parent, tree): | ||
""" Creates the underlying QFocusMultiLineEdit widget. | ||
""" | ||
return QMultilineEdit(parent) | ||
|
||
def create(self, tree): | ||
""" Create and initialize the underlying widget. | ||
""" | ||
super(QtMultilineField, self).create(tree) | ||
self._auto_sync_text = tree['auto_sync_text'] | ||
self.set_text(tree['text']) | ||
self.set_read_only(tree['read_only']) | ||
widget = self.widget() | ||
widget.delayedTextChanged.connect(self.on_text_changed) | ||
|
||
#-------------------------------------------------------------------------- | ||
# Private API | ||
#-------------------------------------------------------------------------- | ||
def _send_text_changed(self): | ||
""" Send the current text as an update to the server widget. | ||
""" | ||
text = self.widget().toPlainText() | ||
self.send_action('text_changed', {'text': text}) | ||
|
||
#-------------------------------------------------------------------------- | ||
# Signal Handlers | ||
#-------------------------------------------------------------------------- | ||
def on_text_changed(self): | ||
""" The signal handler for 'delayedTextChanged' signal. | ||
""" | ||
if self._auto_sync_text and 'text' not in self.loopback_guard: | ||
self._send_text_changed() | ||
|
||
#-------------------------------------------------------------------------- | ||
# Message Handlers | ||
#-------------------------------------------------------------------------- | ||
def on_action_set_text(self, content): | ||
""" Handle the 'set_text' action from the Enaml widget. | ||
""" | ||
self.set_text(content['text']) | ||
|
||
def on_action_set_auto_sync_text(self, content): | ||
""" Handle the 'set_auto_sync_text' action from the Enaml widget. | ||
""" | ||
self._auto_sync_text = content['auto_sync_text'] | ||
|
||
def on_action_set_read_only(self, content): | ||
""" Handle the 'set_read_only' action from the Enaml widget. | ||
""" | ||
self.set_read_only(content['read_only']) | ||
|
||
def on_action_sync_text(self, content): | ||
""" Handle the 'sync_text' action from the Enaml widget. | ||
""" | ||
self._send_text_changed() | ||
|
||
#-------------------------------------------------------------------------- | ||
# Widget Update Methods | ||
#-------------------------------------------------------------------------- | ||
def set_text(self, text): | ||
""" Set the text in the underlying widget. | ||
""" | ||
with self.loopback_guard('text'): | ||
self.widget().setText(text) | ||
|
||
def set_read_only(self, read_only): | ||
""" Set whether or not the widget is read only. | ||
""" | ||
self.widget().setEnabled(not read_only) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#------------------------------------------------------------------------------ | ||
# Copyright (c) 2013, Enthought, Inc. | ||
# All rights reserved. | ||
#------------------------------------------------------------------------------ | ||
from traits.api import Bool, Unicode | ||
|
||
from .control import Control | ||
|
||
|
||
class MultilineField(Control): | ||
""" A simple multiline editable text widget. | ||
""" | ||
#: The unicode text to display in the field. | ||
text = Unicode | ||
|
||
#: Whether or not the field is read only. | ||
read_only = Bool(False) | ||
|
||
#: Whether the text in the control should be auto-synchronized with | ||
#: the text attribute on the field. If this is True, the text will | ||
#: be updated every time the user edits the control. In order to be | ||
#: efficient, the toolkit will batch updates on a collapsing timer. | ||
auto_sync_text = Bool(True) | ||
|
||
#: Multiline fields expand freely in width and height by default. | ||
hug_width = 'ignore' | ||
hug_height = 'ignore' | ||
|
||
#-------------------------------------------------------------------------- | ||
# Initialization | ||
#-------------------------------------------------------------------------- | ||
def snapshot(self): | ||
""" Get the snapshot dict for the control. | ||
""" | ||
snap = super(MultilineField, self).snapshot() | ||
snap['text'] = self.text | ||
snap['read_only'] = self.read_only | ||
snap['auto_sync_text'] = self.auto_sync_text | ||
return snap | ||
|
||
def bind(self): | ||
""" Bind the change handlers for the control. | ||
""" | ||
super(MultilineField, self).bind() | ||
self.publish_attributes('text', 'read_only', 'auto_sync_text') | ||
|
||
#-------------------------------------------------------------------------- | ||
# Message Handling | ||
#-------------------------------------------------------------------------- | ||
def on_action_text_changed(self, content): | ||
""" Handle the 'text_changed' action from the client widget. | ||
""" | ||
text = content['text'] | ||
self.set_guarded(text=text) | ||
|
||
#-------------------------------------------------------------------------- | ||
# Public API | ||
#-------------------------------------------------------------------------- | ||
def sync_text(self): | ||
""" Send a message to the toolkit to synchronize the text. | ||
""" | ||
self.send_action('sync_text', {}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.