File: listbox.py

package info (click to toggle)
python-panwid 0.3.0.dev15-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 308 kB
  • sloc: python: 3,702; makefile: 3
file content (323 lines) | stat: -rw-r--r-- 10,425 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from __future__ import division
import logging
logger = logging.getLogger(__name__.split(".")[0])

import urwid
from urwid_utils.palette import *

class ListBoxScrollBar(urwid.WidgetWrap):

    def __init__(self, parent):
        self.parent = parent
        self.pile = urwid.Pile([])
        super(ListBoxScrollBar, self).__init__(self.pile)

    def update(self, size):
        width, height = size
        scroll_marker_height = 1
        del self.pile.contents[:]
        if (len(self.parent.body)
            and self.parent.row_count
            and self.parent.focus is not None
            and self.parent.row_count > height):
            scroll_position = int(
                self.parent.focus_position / self.parent.row_count * height
            )
            scroll_marker_height = max( height * (height / self.parent.row_count ), 1)
        else:
            scroll_position = -1

        pos_marker = urwid.AttrMap(urwid.Text(" "),
                                   {None: "scroll_pos"}
        )

        down_marker = urwid.AttrMap(urwid.Text(u"\N{DOWNWARDS ARROW}"),
                                   {None: "scroll_marker"}
        )

        begin_marker = urwid.AttrMap(urwid.Text(u"\N{CIRCLED MINUS}"),
                                   {None: "scroll_marker"}
        )

        end_marker = urwid.AttrMap(urwid.Text(u"\N{CIRCLED PLUS}"),
                                   {None: "scroll_marker"}
        )

        view_marker = urwid.AttrMap(urwid.Text(" "),
                                    {None: "scroll_view"}
        )

        bg_marker = urwid.AttrMap(urwid.Text(" "),
                                   {None: "scroll_bg"}
        )

        for i in range(height):
            if abs( i - scroll_position ) <= scroll_marker_height//2:
                if i == 0 and self.parent.focus_position == 0:
                    marker = begin_marker
                elif i+1 == height and self.parent.row_count == self.parent.focus_position+1:
                    marker = end_marker
                elif len(self.parent.body) == self.parent.focus_position+1 and i == scroll_position + scroll_marker_height//2:
                    marker = down_marker
                else:
                    marker = pos_marker
            else:
                if i < scroll_position:
                    marker = view_marker
                elif self.parent.row_count and i/height < ( len(self.parent.body) / self.parent.row_count):
                    marker = view_marker
                else:
                    marker = bg_marker
            self.pile.contents.append(
                (urwid.Filler(marker), self.pile.options("weight", 1))
            )
        self._invalidate()

    def selectable(self):
        # FIXME: mouse click/drag
        return False


class ScrollingListBox(urwid.WidgetWrap):

    signals = ["select",
               "drag_start", "drag_continue", "drag_stop",
               "load_more"]

    def __init__(self, body,
                 infinite = False,
                 with_scrollbar=False,
                 scroll_rows=None,
                 row_count_fn = None):

        self.infinite = infinite
        self.with_scrollbar = with_scrollbar
        self.scroll_rows = scroll_rows
        self.row_count_fn = row_count_fn

        self.mouse_state = 0
        self.drag_from = None
        self.drag_last = None
        self.drag_to = None
        self.load_more = False
        self.height = 0
        self.page = 0

        self.queued_keypress = None

        self.listbox = urwid.ListBox(body)
        self.columns = urwid.Columns([
            ('weight', 1, self.listbox)
        ])
        if self.with_scrollbar:
            self.scroll_bar = ListBoxScrollBar(self)
            self.columns.contents.append(
                (self.scroll_bar, self.columns.options("given", 1))
            )
        super(ScrollingListBox, self).__init__(self.columns)

    @classmethod
    def get_palette_entries(cls):

        return {

            "scroll_pos": PaletteEntry(
                mono = "white",
                foreground = "black",
                background = "white",
                foreground_high = "black",
                background_high = "white"
            ),
            "scroll_marker": PaletteEntry(
                mono = "white,bold",
                foreground = "black,bold",
                background = "white",
                foreground_high = "black,bold",
                background_high = "white"
            ),
            "scroll_view": PaletteEntry(
                mono = "black",
                foreground = "black",
                background = "light gray",
                foreground_high = "black",
                background_high = "g50"
            ),
            "scroll_bg": PaletteEntry(
                mono = "black",
                foreground = "light gray",
                background = "dark gray",
                foreground_high = "light gray",
                background_high = "g23"
            ),

        }

    def mouse_event(self, size, event, button, col, row, focus):

        SCROLL_WHEEL_HEIGHT_RATIO = 0.5
        if row < 0 or row >= self.height:
            return
        if event == 'mouse press':
            if button == 1:
                self.mouse_state = 1
                self.drag_from = self.drag_last = (col, row)
            elif button == 4:
                pos = self.listbox.focus_position - int(self.height * SCROLL_WHEEL_HEIGHT_RATIO)
                if pos < 0:
                    pos = 0
                self.listbox.focus_position = pos
                self.listbox.make_cursor_visible(size)
                self._invalidate()
            elif button == 5:
                pos = self.listbox.focus_position + int(self.height * SCROLL_WHEEL_HEIGHT_RATIO)
                if pos > len(self.listbox.body) - 1:
                    if self.infinite:
                        self.load_more = True
                    pos = len(self.listbox.body) - 1
                self.listbox.focus_position = pos
                self.listbox.make_cursor_visible(size)
                self._invalidate()
        elif event == 'mouse drag':
            if self.drag_from is None:
                return
            if button == 1:
                self.drag_to = (col, row)
                if self.mouse_state == 1:
                    self.mouse_state = 2
                    urwid.signals.emit_signal(
                        self, "drag_start",self, self.drag_from
                    )
                else:
                    urwid.signals.emit_signal(
                        self, "drag_continue",self,
                        self.drag_last, self.drag_to
                    )

            self.drag_last = (col, row)

        elif event == 'mouse release':
            if self.mouse_state == 2:
                self.drag_to = (col, row)
                urwid.signals.emit_signal(
                    self, "drag_stop",self, self.drag_from, self.drag_to
                )
            self.mouse_state = 0
        return super(ScrollingListBox, self).mouse_event(size, event, button, col, row, focus)


    def keypress(self, size, key):

        command = self._command_map[key]
        if not command:
            return super(ScrollingListBox, self).keypress(size, key)

        # down, page down at end trigger load of more data
        if (
                command in ["cursor down", "cursor page down"]
                and self.infinite
                and (
                    not len(self.body)
                    or self.focus_position == len(self.body)-1)
        ):
                self.load_more = True
                self.queued_keypress = key
                self._invalidate()

        elif command == "activate":
            urwid.signals.emit_signal(self, "select", self, self.selection)

        # else:
        return super(ScrollingListBox, self).keypress(size, key)

    @property
    def selection(self):

        if len(self.body):
            return self.body[self.focus_position]


    def render(self, size, focus=False):

        maxcol = size[0]
        self.width = maxcol
        if len(size) > 1:
            maxrow = size[1]
            self.height = maxrow

        # print
        # print
        # print self.listbox.get_focus_offset_inset(size)
        if (self.load_more
            and (len(self.body) == 0
                 or "bottom" in self.ends_visible((maxcol, maxrow))
            )
        ):

            self.load_more = False
            self.page += 1
            # old_len = len(self.body)
            try:
                focus = self.focus_position
            except IndexError:
                focus = None
            urwid.signals.emit_signal(
                self, "load_more", focus)
            if (self.queued_keypress
                and focus
                and focus < len(self.body)
            ):
                # logger.info("send queued keypress")
                self.keypress(size, self.queued_keypress)
            self.queued_keypress = None
            # self.listbox._invalidate()
            # self._invalidate()

        if self.with_scrollbar and len(self.body):
            self.scroll_bar.update(size)

        return super(ScrollingListBox, self).render(size, focus)


    def disable(self):
        self.selectable = lambda: False

    def enable(self):
        self.selectable = lambda: True

    @property
    def contents(self):
        return self.columns.contents

    @property
    def focus(self):
        return self.listbox.focus

    @property
    def focus_position(self):
        if not len(self.listbox.body):
            raise IndexError
        if len(self.listbox.body):
            return self.listbox.focus_position
        return None

    @focus_position.setter
    def focus_position(self, value):
        if not len(self.body):
            return
        self.listbox.focus_position = value
        self.listbox._invalidate()

    def __getattr__(self, attr):
        if attr in ["ends_visible", "focus_position", "set_focus", "set_focus_valign", "body", "focus"]:
            return getattr(self.listbox, attr)
        # elif attr == "body":
        #     return self.walker
        raise AttributeError(attr)

    @property
    def row_count(self):
        if self.row_count_fn:
            return self.row_count_fn()
        return len(self.body)

__all__ = ["ScrollingListBox"]