«Drag and drop views that share data» by Jakob Leben

on 09 May'12 11:05 in dragdropautoupdateshared

The DndView is a subclass of QDragView, implementing additional functionality. When a drag-and-drop is performed between two such views, setting one's value will update the other's as well. Use the 'value' method to get or set the value of a view.

When creating a new instance of the view, you can pass in the 'type' argument, which can be \source, \sink, or \both, defining what will be the role of the view.

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
SharedDndData
{
    var <value;
    
    *new { arg val; ^super.newCopyArgs(val) }

    value_ { arg val;
        value = val;
        this.changed(\value);
    }

    asString { ^value.asString; }
}

DndView : QDragView
{
    var views;

    *new { arg parent, bounds, type = \both;
        var view = super.new(parent, bounds);
        view.onClose = { view.object.removeDependant(view) };
        if (type === \source or: (type === \both)) {
            view.beginDragAction = { view.object };
        };
        if (type === \sink or: (type === \both)) {
            view.canReceiveDragHandler = true;
            view.receiveDragHandler = { view.object = View.currentDrag }
        };
        ^view;
    }

    object_ { arg new_obj;
        object.removeDependant(this);
        object = new_obj;
        object.addDependant(this);
        this.string = object.asString;
    }

    value { ^this.object.value }

    value_ { arg new_val;
        if (this.object.isNil) {
            this.object = SharedDndData(new_val);
        }{
            this.object.value = new_val;
        }
    }

    update { arg caller;
        if (caller === object) { this.string = this.object.asString }
    }
}
raw 1249 chars (focus & ctrl+a+c to copy)
reception
comments