TraitsUI: getting selected text from CodeEditor?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

TraitsUI: getting selected text from CodeEditor?

Kevin Salvesen
Hi all,

I am trying to get the text selected by the user in a CodeEditor
(using Qt4 and Qscintilla) using the selected_text trait, but my code
is not working and I really don't know why or how to fix it.

Here's some sample code:

=============================

class TextDisplayHandler(Handler):
    def foo(self, info):
        print info.object.selec

class TextDisplay(HasTraits):
    code =  Str('Balvabaklmsd  sfd1 fsd2f')

    selec = Str()

    tool_bar = ToolBar(Action(name='foo', action='foo'))

    traits_view = View(Item('code', editor = CodeEditor(selected_text='selec')),
                       toolbar = tool_bar,
                       handler = TextDisplayHandler(),
                       resizable = True)

if __name__ == '__main__':
    t = TextDisplay()
    t.configure_traits()

=============================

My aim here would be to replace the selected text with something else.
If anyone had some pointers on what I'm doing wrong and how I can fix
it, it would be greatly appreciated.

Thanks,
Kevin
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: TraitsUI: getting selected text from CodeEditor?

bryce hendrix-2
Quoting Kevin Salvesen <[hidden email]>:

> Hi all,
>
> I am trying to get the text selected by the user in a CodeEditor
> (using Qt4 and Qscintilla) using the selected_text trait, but my code
> is not working and I really don't know why or how to fix it.

Ah, I just had a look at  
TraitsBackendQt/trunk/enthought/traits/ui/qt/code_editor.py, and I  
found that the selected_text ettribute can only currently be used for  
setting, not getting the selected text (line 421).

I don't know of any reason, except possibly performance, that it  
shouldn't be syncronized both ways. I propose we change the  
functionality. Any objections?

Bryce


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: TraitsUI: getting selected text from CodeEditor?

Evan Patterson
Actually, the synchronizing code on line 421 is correct (selected_text can be used only for gettting, not setting, the selected text). There was a bug (in both backends, as a matter of fact) that was preventing the selected_text attribute from being updated under certain circumstances. This bug is fixed as of revisions 25615 and 25616.

Evan

On Mon, Jun 21, 2010 at 12:18 PM, <[hidden email]> wrote:
Quoting Kevin Salvesen <[hidden email]>:

> Hi all,
>
> I am trying to get the text selected by the user in a CodeEditor
> (using Qt4 and Qscintilla) using the selected_text trait, but my code
> is not working and I really don't know why or how to fix it.

Ah, I just had a look at
TraitsBackendQt/trunk/enthought/traits/ui/qt/code_editor.py, and I
found that the selected_text ettribute can only currently be used for
setting, not getting the selected text (line 421).

I don't know of any reason, except possibly performance, that it
shouldn't be syncronized both ways. I propose we change the
functionality. Any objections?

Bryce


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: TraitsUI: getting selected text from CodeEditor?

Jonathan Blakes
Hi Kevin,

I suspect that the selected_text trait of the CodeEditor requires the
context name of the object being edited to be append to the trait name,
e.g. adding 'object.' before 'selec', yielding:

traits_view = View(
     Item('code',
         editor=CodeEditor(
             selected_text='object.selec')
         ),
     ),
     toolbar = tool_bar,
     handler = TextDisplayHandler(),
     resizable = True,
)

Jon


> Subject:
> [Enthought-Dev] TraitsUI: getting selected text from CodeEditor?
> From:
> Kevin Salvesen <[hidden email]>
> Date:
> Mon, 21 Jun 2010 12:38:57 -0400
> To:
> [hidden email]
>
> To:
> [hidden email]
>
>
> Hi all,
>
> I am trying to get the text selected by the user in a CodeEditor
> (using Qt4 and Qscintilla) using the selected_text trait, but my code
> is not working and I really don't know why or how to fix it.
>
> Here's some sample code:
>
> =============================
>
> class TextDisplayHandler(Handler):
>     def foo(self, info):
>         print info.object.selec
>
> class TextDisplay(HasTraits):
>     code =  Str('Balvabaklmsd  sfd1 fsd2f')
>
>     selec = Str()
>
>     tool_bar = ToolBar(Action(name='foo', action='foo'))
>
>     traits_view = View(Item('code', editor = CodeEditor(selected_text='selec')),
>                        toolbar = tool_bar,
>                        handler = TextDisplayHandler(),
>                        resizable = True)
>
> if __name__ == '__main__':
>     t = TextDisplay()
>     t.configure_traits()
>
> =============================
>
> My aim here would be to replace the selected text with something else.
> If anyone had some pointers on what I'm doing wrong and how I can fix
> it, it would be greatly appreciated.
>
> Thanks,
> Kevin
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev