Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] TabularEditor triggering column size recompute #1709

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

aaronayres35
Copy link
Contributor

@aaronayres35 aaronayres35 commented Jun 30, 2021

closes #1434

This PR adds a new update_column_widths Event trait to the qt4 TabularEditor, along with a listener that will call self.control.resizeColumnsToContents whenever this event is fired. EDIT: I've changed the approach.

Code I am using to manually test this:

from numpy import sqrt
from numpy.random import random

from traits.api import HasTraits, Property, Array, Any, Instance, Str
from traitsui.api import View, Item, TabularAdapter, TabularEditor, ModelView, TextEditor
from traitsui.testing.api import UITester


# -- Tabular Adapter Definition -------------------------------------------
class ArrayAdapter(TabularAdapter):

    columns = [('i', 'index'), ('x', 0), ('y', 1), ('z', 2)]

    font = 'Courier 10'
    alignment = 'right'
    format = '%.4f'  # Str('%.4f', update=True)

    index_text = Property()
    index_image = Property()

    def _get_index_text(self):
        return str(self.row)

    def _get_index_image(self):
        x, y, z = self.item
        if sqrt((x - 0.5) ** 2 + (y - 0.5) ** 2 + (z - 0.5) ** 2) <= 0.25:
            return '@icons:red_ball'

        return None



class ShowArray(ModelView):

    data = Array

    form = Str('%.4f')

    def object_form_changed(self, info):
        info.data.adapter.format = self.form
        info.data.update = True
        # with the first approach I was using the following line instead.
        # info.data.update_column_widths = True 

    traits_view = View(
        Item(
            'form',
            editor=TextEditor(auto_set=False)
        ),
        Item(
            'data',
            show_label=False,
            editor=TabularEditor(
                adapter=ArrayAdapter(),
                auto_resize=True,
                # Do not allow any kind of editing of the array:
                editable=False,
                operations=[],
                drag_move=False
            )
        ),
        title='Array Viewer',
        width=0.3,
        height=0.8,
        resizable=True
    )


# Create the demo:
demo = ShowArray(model=HasTraits(), data=random((2, 3)))

# Run the demo (if invoked from the command line):
if __name__ == '__main__':
    demo.configure_traits()

Checklist

  • Add a news fragment if this PR is news-worthy for end users. (see docs/releases/README.rst)

@aaronayres35
Copy link
Contributor Author

aaronayres35 commented Jun 30, 2021

Actually thinking about this more I do not like this approach... In my example code, one could replace the line info.data.update_column_widths = True with info.data.control.resizeColumnsToContents() and then the new Event trait is useless.

Any time a user is trying to fire the update Event Trait, they must have access to the editor. I am unsure why they couldn't just manually call editor.control.resizeColumnsToContents() then at their own leisure.

Additionally, there exists an auto_resize trait that seems like it should be dictating this sort of behavior.

EDIT: I've updated the PR to now check auto_resize whenever update is fired and use that to determine whether or not to call resizeColumnsToContents()

@aaronayres35 aaronayres35 changed the title Add update_column_widths Event trait to force calls to resizeColumnsToContents [WIP] TabularEditor triggering column size recompute Jun 30, 2021
@aaronayres35
Copy link
Contributor Author

@rahulporuri This PR isn't really ready for review if it is ready to merge, but I've opened it to get your ideas on some different approaches / so we can further discuss what makes sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TabularEditor triggering column size recompute
1 participant