results_view.py 5.11 KB
Newer Older
Franziska Koehn's avatar
Franziska Koehn committed
1
2
3
4
import gtk

class ResultsDownloadView(gtk.VBox):

5
6
    def show_data(self, results, root_type):
        self.treeViewResults.show_data(results, root_type)
Franziska Koehn's avatar
Franziska Koehn committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

    def __init__(self, *args, **kwargs):
        super(ResultsDownloadView, self).__init__(*args, **kwargs)

        sw_results = gtk.ScrolledWindow()
        sw_results.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        sw_results.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.pack_start(sw_results,expand=True, fill=True)

        self.treeViewResults = TreeViewResults()
        sw_results.add(self.treeViewResults)

        buttonBox_download = gtk.HButtonBox()
        self.pack_end(buttonBox_download,expand=False, fill=False)

22
        def event_clicked_download(button, *_):
Franziska Koehn's avatar
Franziska Koehn committed
23
            import gobject
Franziska Koehn's avatar
Franziska Koehn committed
24
            import xsalogic.queries as queries
25
            enabled = (d for d in self.treeViewResults.store if d[0])
Franziska Koehn's avatar
Franziska Koehn committed
26
27
            for d in enabled:
                def stop_spinner(_row):
28
                    _row[2] = False # Hide spinner
Franziska Koehn's avatar
Franziska Koehn committed
29
                    _row[0] = False # Checkbox to false
30
31
                d[3] = 0
                d[2] = True # Show Spinner
Franziska Koehn's avatar
Franziska Koehn committed
32
                queries.download_async(d[1], button.destination, cb=stop_spinner, cb_args=(d,))
Franziska Koehn's avatar
Franziska Koehn committed
33

Franziska Koehn's avatar
Franziska Koehn committed
34
35
36
37
38
39
40
        def event_clicked_toggle_selection(*_):
            self.treeViewResults.toggle_selection()

        button_toggle_selection = gtk.Button(label="toggle Selection")
        button_toggle_selection.connect("clicked", event_clicked_toggle_selection)
        buttonBox_download.add(button_toggle_selection)

41
        def event_selection_changed(filechooserbutton, *_):
42
43
44
45
            self.button_download.destination = filechooserbutton.get_filename()

        button_folder = gtk.FileChooserButton("Select Folder")
        button_folder.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
46
        button_folder.connect("selection-changed", event_selection_changed)
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        buttonBox_download.add(button_folder)

        self.button_download = ButtonDownloadResults(label="Download Selected Items")
        self.button_download.connect("clicked", event_clicked_download)
        buttonBox_download.add(self.button_download)

class ButtonDownloadResults(gtk.Button):

    count = 0
    _destination = ""

    @property
    def destination(self):
        return self._destination

    @destination.setter
    def destination(self, value):
        self._destination = value

    def __init__(self, *args, **kwargs):
        super(ButtonDownloadResults, self).__init__(*args, **kwargs)
Franziska Koehn's avatar
Franziska Koehn committed
68
69
70
71
72



class TreeViewResults(gtk.TreeView):

73
    store = []
Franziska Koehn's avatar
Franziska Koehn committed
74
75
76
    def __init__(self):
        import gobject
        super(TreeViewResults, self).__init__()
77
78

        self.show_data([], None)
Franziska Koehn's avatar
Franziska Koehn committed
79
80
81
82
83

        gobject.timeout_add(150, self.move_spinners)

    def move_spinners(self):
        for r in self.store:
84
85
            if r[2]: # if spinner is visible
                r[3] += 1
Franziska Koehn's avatar
Franziska Koehn committed
86
87
88
        return True


89
90
91
92
    def create_columns(self, column_labels):

        for c in self.get_columns():
            self.remove_column(c)
Franziska Koehn's avatar
Franziska Koehn committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

        def callback_toggled(cellrenderertoggle, path_string, col, *_):
            it = self.store.get_iter_from_string(path_string)
            is_active = cellrenderertoggle.get_active()
            self.store.set(it, col, not is_active)

        renderer = gtk.CellRendererToggle()
        renderer.set_property('activatable', True)
        renderer.connect("toggled", callback_toggled, 0)
        column = gtk.TreeViewColumn("", renderer)
        column.add_attribute(renderer, "active", 0)
        column.set_sort_column_id(0)
        column.set_resizable(False)
        self.append_column(column)

        renderer = gtk.CellRendererSpinner()
109
        column = gtk.TreeViewColumn("", renderer, active=2, pulse=3)
Franziska Koehn's avatar
Franziska Koehn committed
110
111
        self.append_column(column)

112
113
114
115
116
        for i,cl in enumerate(column_labels, start=4):
            column = gtk.TreeViewColumn(cl, gtk.CellRendererText(), text=i)
            column.set_sort_column_id(i)
            column.set_resizable(True)
            self.append_column(column)
Franziska Koehn's avatar
Franziska Koehn committed
117

118
119
120
    def toggle_selection(self):
        for row in self.store:
            row[0] = not row[0]
Franziska Koehn's avatar
Franziska Koehn committed
121

122
123
124
125
126
    def show_data(self, data, root_type):
        import gobject
        if data == []:
            self.create_columns([])
            return
Franziska Koehn's avatar
Franziska Koehn committed
127
128


129
        keys = data.headers()
Franziska Koehn's avatar
Franziska Koehn committed
130

131
132
133
134
135
136
137
138
139
        model_columns = []
        model_columns.append(bool)                  # 0 is selected (for download selection)
        model_columns.append(gobject.TYPE_PYOBJECT) # 1 query result
        model_columns.append(bool)                  # 2 is spinner visible
        model_columns.append(int)                   # 3 spinner pulse
        for k in keys:
            model_columns.append(str)

        self.store = gtk.ListStore(*model_columns)
Franziska Koehn's avatar
Franziska Koehn committed
140

Franziska Koehn's avatar
Franziska Koehn committed
141
        for d in data:
142
143
144
145
146
147
148
149
150
151
            result = []
            result.append(True)
            result.append(d)
            result.append(False)
            result.append(0)
            for k in keys:
                result.append(d[k])

            self.store.append(result)

Franziska Koehn's avatar
Franziska Koehn committed
152
153
154
        import xsalogic.datatypereader as type_reader
        keys_ = list(type_reader.get_field_label(root_type, key) for key in keys)
        self.create_columns(keys_)
155
        self.set_model(self.store)