aboutsummaryrefslogtreecommitdiffstats
path: root/lvc/widgets/osx/tablemodel.py
blob: d81a8f511a6d945d600c99a7e3024436b02c693d (plain)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# @Base: Miro - an RSS based video player application
# Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
# Participatory Culture Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the OpenSSL
# library.
#
# You must obey the GNU General Public License in all respects for all of
# the code used other than OpenSSL. If you modify file(s) with this
# exception, you may extend this exception to your version of the file(s),
# but you are not obligated to do so. If you do not wish to do so, delete
# this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here.

"""tablemodel.py -- Model classes for TableView.  """

import logging

from AppKit import (NSDragOperationNone, NSDragOperationAll, NSTableViewDropOn,
                    NSOutlineViewDropOnItemIndex, protocols)
from Foundation import NSObject, NSNotFound, NSMutableIndexSet
from objc import YES, NO, nil

from lvc import signals
from lvc.errors import WidgetActionError
import fasttypes
import wrappermap

MIRO_DND_ITEM_LOCAL = 'miro-local-item'

# XXX need unsigned but value comes out as signed.
NSDragOperationEvery = NSDragOperationAll

def list_from_nsindexset(index_set):
    rows = list()
    index = index_set.firstIndex()
    while (index != NSNotFound):
        rows.append(index)
        index = index_set.indexGreaterThanIndex_(index)
    return rows

class RowList(object):
    """RowList is a Linked list that has some optimizations for looking up
    rows by index number.
    """
    def __init__(self):
        self.list = fasttypes.LinkedList()
        self.iter_cache = []

    def firstIter(self):
        return self.list.firstIter()

    def lastIter(self):
        return self.list.lastIter()

    def insertBefore(self, iter, value):
        self.iter_cache = []
        if iter is None:
            return self.list.append(value)
        else:
            return self.list.insertBefore(iter, value)

    def append(self, value):
        return self.list.append(value)

    def __len__(self):
        return len(self.list)

    def __getitem__(self, iter):
        return self.list[iter]

    def __iter__(self):
        iter = self.firstIter()
        while iter != self.lastIter():
            yield iter.value()
            iter.forward()

    def remove(self, iter):
        self.iter_cache = []
        return self.list.remove(iter)

    def nth_iter(self, index):
        if index < 0:
            raise IndexError(index)
        elif index >= len(self):
            raise LookupError()
        if len(self.iter_cache) == 0:
            self.iter_cache.append(self.firstIter())
        try:
            return self.iter_cache[index].copy()
        except IndexError:
            pass
        iter = self.iter_cache[-1].copy()
        index -= len(self.iter_cache) - 1
        for x in xrange(index):
            iter.forward()
            self.iter_cache.append(iter.copy())
        return iter

class TableModelBase(signals.SignalEmitter):
    """Base class for TableModel and TreeTableModel."""
    def __init__(self, *column_types):
        signals.SignalEmitter.__init__(self)
        self.row_list = RowList()
        self.column_types = column_types
        self.create_signal('row-changed')
        self.create_signal('structure-will-change')

    def check_column_values(self, column_values):
        if len(self.column_types) != len(column_values):
            raise ValueError("Wrong number of columns")
        # We might want to do more typechecking here

    def get_column_data(self, row, column):
        attr_map = column.attrs()
        return dict((name, row[index]) for name, index in attr_map.items())

    def update_value(self, iter, index, value):
        iter.value().values[index] = value
        self.emit('row-changed', iter)

    def update(self, iter, *column_values):
        iter.value().update_values(column_values)
        self.emit('row-changed', iter)

    def remove(self, iter):
        self.emit('structure-will-change')
        row_list = self.containing_list(iter)
        rv = row_list.remove(iter)
        if rv == row_list.lastIter():
            rv = None
        return rv

    def nth_iter(self, index):
        return self.row_list.nth_iter(index)

    def next_iter(self, iter):
        row_list = self.containing_list(iter)
        retval = iter.copy()
        retval.forward()
        if retval == row_list.lastIter():
            return None
        else:
            return retval

    def first_iter(self):
        if len(self.row_list) > 0:
            return self.row_list.firstIter()
        else:
            return None

    def __len__(self):
        return len(self.row_list)

    def __getitem__(self, iter):
        return iter.value()

    def __iter__(self):
        return iter(self.row_list)

class TableRow(object):
    """See https://develop.participatoryculture.org/index.php/WidgetAPITableView for a description of the API for this class."""
    def __init__(self, column_values):
        self.update_values(column_values)

    def update_values(self, column_values):
        self.values = list(column_values)

    def __getitem__(self, index):
        return self.values[index]

    def __len__(self):
        return len(self.values)

    def __iter__(self):
        return iter(self.values)

class TableModel(TableModelBase):
    """See https://develop.participatoryculture.org/index.php/WidgetAPITableView for a description of the API for this class."""
    def __init__(self, *column_types):
        TableModelBase.__init__(self, column_types)
        self.row_indexes = {}

    def remember_row_at_index(self, row, index):
        if row not in self.row_indexes:
            self.row_indexes[row] = index

    def row_of_iter(self, tableview, iter):
        row = iter.value()
        try:
            return self.row_indexes[row]
        except KeyError:
            iter = self.row_list.firstIter()
            index = 0
            while iter != self.row_list.lastIter():
                current_row = iter.value()
                self.row_indexes[current_row] = index
                if current_row is row:
                    return index
                index += 1
                iter.forward()
            raise LookupError("%s is not in this table" % row)

    def containing_list(self, iter):
        return self.row_list

    def append(self, *column_values):
        self.emit('structure-will-change')
        self.row_indexes = {}
        retval = self.row_list.append(TableRow(column_values))
        return retval

    def remove(self, iter):
        self.row_indexes = {}
        return TableModelBase.remove(self, iter)

    def insert_before(self, iter, *column_values):
        self.emit('structure-will-change')
        self.row_indexes = {}
        row = TableRow(column_values)
        retval = self.row_list.insertBefore(iter, row)
        return retval

    def iter_for_row(self, tableview, row):
        return self.row_list.nth_iter(row)


class TreeNode(NSObject, TableRow):
    """A row in a TreeTableModel"""

    # Implementation note: these need to be NSObjects because we return them
    # to the NSOutlineView.

    def initWithValues_parent_(self, column_values, parent):
        self.children = RowList()
        self.update_values(column_values)
        self.parent = parent
        return self

    @staticmethod
    def create_(values, parent):
        return TreeNode.alloc().initWithValues_parent_(values, parent)

    def iterchildren(self):
        return iter(self.children)

class TreeTableModel(TableModelBase):
    """https://develop.participatoryculture.org/index.php/WidgetAPITableView"""
    def __init__(self, *column_values):
        TableModelBase.__init__(self, *column_values)
        self.iter_for_item = {}

    def containing_list(self, iter):
        return self.row_list_for_iter(iter.value().parent)

    def row_list_for_iter(self, iter):
        """Return the rows of all direct children of iter."""
        if iter is None:
            return self.row_list
        else:
            return iter.value().children

    def remember_iter(self, iter):
        self.iter_for_item[iter.value()] = iter
        return iter

    def append(self, *column_values):
        self.emit('structure-will-change')
        retval = self.row_list.append(TreeNode.create_(column_values, None))
        return self.remember_iter(retval)

    def forget_iter_for_item(self, item):
        del self.iter_for_item[item]
        for child in item.children:
            self.forget_iter_for_item(child)

    def remove(self, iter):
        item = iter.value()
        rv = TableModelBase.remove(self, iter)
        self.forget_iter_for_item(item)
        return rv

    def insert_before(self, iter, *column_values):
        self.emit('structure-will-change')
        row = TreeNode.create_(column_values, self.parent_iter(iter))
        retval = self.containing_list(iter).insertBefore(iter, row)
        return self.remember_iter(retval)

    def append_child(self, iter, *column_values):
        self.emit('structure-will-change')
        row_list = self.row_list_for_iter(iter)
        retval = row_list.append(TreeNode.create_(column_values, iter))
        return self.remember_iter(retval)

    def child_iter(self, iter):
        row_list = iter.value().children
        if len(row_list) == 0:
            return None
        else:
            return row_list.firstIter()

    def nth_child_iter(self, iter, index):
        row_list = self.row_list_for_iter(iter)
        return row_list.nth_iter(index)

    def has_child(self, iter):
        return len(iter.value().children) > 0

    def children_count(self, iter):
        if iter is not None:
            return len(iter.value().children)
        else:
            return len(self.row_list)

    def children_iters(self, iter):
        return self.iters_in_rowlist(self.row_list_for_iter(iter))

    def parent_iter(self, iter):
        return iter.value().parent

    def iter_for_row(self, tableview, row):
        item = tableview.itemAtRow_(row)
        if item in self.iter_for_item:
            return self.iter_for_item[item]
        elif item == -1:
            raise WidgetActionError("no item at row %s" % row)
        else:
            raise WidgetActionError("no iter for item %s at row %s" %
                                   (repr(item), row))

    def row_of_iter(self, tableview, iter):
        item = iter.value()
        row = tableview.rowForItem_(item)
        if row == -1:
            raise LookupError("%s is not in this table" % repr(item))
        return row

    def get_path(self, iter_):
        """Not implemented (yet?) for Cocoa. Currently the only place this is
        needed is tablistmanager, where the situation that uses paths results
        from GTK peculiarities.
        """
        return NotImplemented

class DataSourceBase(NSObject):
    def initWithModel_(self, model):
        self.model = model
        self.drag_source = None
        self.drag_dest = None
        return self

    def setDragSource_(self, drag_source):
        self.drag_source = drag_source

    def setDragDest_(self, drag_dest):
        self.drag_dest = drag_dest

    def view_writeColumnData_ToPasteboard_(self, view, data, pasteboard):
        if not self.drag_source:
            return NO
        wrapper = wrappermap.wrapper(view)
        drag_data = self.drag_source.begin_drag(wrapper, data)
        if not drag_data:
            return NO
        pasteboard.declareTypes_owner_((MIRO_DND_ITEM_LOCAL,), self)
        for typ, value in drag_data.items():
            stringval = repr((repr(value), typ))
            pasteboard.setString_forType_(stringval, MIRO_DND_ITEM_LOCAL)
        return YES

    def calcType_(self, drag_info):
        source_actions = drag_info.draggingSourceOperationMask()
        if not (self.drag_dest and
                (self.drag_dest.allowed_actions() | source_actions)):
            return None
        types = self.drag_dest.allowed_types()
        available = drag_info.draggingPasteboard().availableTypeFromArray_(
            (MIRO_DND_ITEM_LOCAL,))
        if available:
            # XXX using eval() sucks.
            data = eval(drag_info.draggingPasteboard().stringForType_(
                MIRO_DND_ITEM_LOCAL))
            if data:
                _, typ = data
                return typ
        return None

    def validateDrop_dragInfo_parentIter_position_(self, view, drag_info,
            parent, position):
        typ = self.calcType_(drag_info)
        if typ:
            wrapper = wrappermap.wrapper(view)
            drop_action = self.drag_dest.validate_drop(
                wrapper, self.model, typ,
                drag_info.draggingSourceOperationMask(), parent,
                position)
            if not drop_action:
                return NSDragOperationNone
            if isinstance(drop_action, (tuple, list)):
                drop_action, iter = drop_action
                view.setDropRow_dropOperation_(
                    self.model.row_of_iter(view, iter),
                    NSTableViewDropOn)
            return drop_action
        else:
            return NSDragOperationNone

    def acceptDrop_dragInfo_parentIter_position_(self, view, drag_info,
            parent, position):
        typ = self.calcType_(drag_info)
        if typ:
            # XXX using eval sucks.
            data = eval(drag_info.draggingPasteboard().stringForType_(MIRO_DND_ITEM_LOCAL))
            ids, _ = data
            ids = eval(ids)
            wrapper = wrappermap.wrapper(view)
            self.drag_dest.accept_drop(wrapper, self.model, typ,
                drag_info.draggingSourceOperationMask(), parent,
                position, ids)
            return YES
        else:
            return NO

class MiroTableViewDataSource(DataSourceBase, protocols.NSTableDataSource):
    def numberOfRowsInTableView_(self, table_view):
        return len(self.model)

    def tableView_objectValueForTableColumn_row_(self, table_view, column, row):
        node = self.model.nth_iter(row).value()
        self.model.remember_row_at_index(node, row)
        return self.model.get_column_data(node.values, column)

    def tableView_writeRowsWithIndexes_toPasteboard_(self, tableview, rowIndexes,
            pasteboard):
        indexes = list_from_nsindexset(rowIndexes)
        data = [self.model[self.model.nth_iter(i)] for i in indexes]
        return self.view_writeColumnData_ToPasteboard_(tableview, data,
                pasteboard)

    def translateRow_operation_(self, row, operation):
        if operation == NSTableViewDropOn:
            return self.model.nth_iter(row), -1
        else:
            return None, row

    def tableView_validateDrop_proposedRow_proposedDropOperation_(self,
            tableview, drag_info, row, operation):
        parent, position  = self.translateRow_operation_(row, operation)
        drop_action = self.validateDrop_dragInfo_parentIter_position_(tableview,
                drag_info, parent, position)
        if isinstance(drop_action, (list, tuple)):
            # XXX nothing uses this yet
            drop_action, iter = drop_action
            tableview.setDropRow_dropOperation_(
                self.model.row_of_iter(tableview, iter),
                NSTableViewDropOn)
        return drop_action

    def tableView_acceptDrop_row_dropOperation_(self,
            tableview, drag_info, row, operation):
        parent, position = self.translateRow_operation_(row, operation)
        return self.acceptDrop_dragInfo_parentIter_position_(tableview,
                drag_info, parent, position)


class MiroOutlineViewDataSource(DataSourceBase, protocols.NSOutlineViewDataSource):
    def outlineView_child_ofItem_(self, view, child, item):
        if item is nil:
            row_list = self.model.row_list
        else:
            row_list = item.children
        return row_list.nth_iter(child).value()

    def outlineView_isItemExpandable_(self, view, item):
        if item is not nil and hasattr(item, 'children'):
            return len(item.children) > 0
        else:
            return len(self.model) > 0

    def outlineView_numberOfChildrenOfItem_(self, view, item):
        if item is not nil and hasattr(item, 'children'):
            return len(item.children)
        else:
            return len(self.model)

    def outlineView_objectValueForTableColumn_byItem_(self, view, column,
            item):
        return self.model.get_column_data(item.values, column)

    def outlineView_writeItems_toPasteboard_(self, outline_view, items,
            pasteboard):
        data = [i.values for i in items]
        return self.view_writeColumnData_ToPasteboard_(outline_view, data,
                pasteboard)

    def outlineView_validateDrop_proposedItem_proposedChildIndex_(self,
            outlineview, drag_info, item, child_index):
        if item is None:
            iter = None
        else:
            iter = self.model.iter_for_item[item]
        drop_action = self.validateDrop_dragInfo_parentIter_position_(
            outlineview, drag_info, iter, child_index)
        if isinstance(drop_action, (tuple, list)):
            drop_action, iter = drop_action
            outlineview.setDropItem_dropChildIndex_(
                iter.value(), NSOutlineViewDropOnItemIndex)
        return drop_action

    def outlineView_acceptDrop_item_childIndex_(self, outlineview, drag_info,
            item, child_index):
        if item is None:
            iter = None
        else:
            iter = self.model.iter_for_item[item]
        return self.acceptDrop_dragInfo_parentIter_position_(outlineview,
                drag_info, iter, child_index)