query_view.py 14.1 KB
Newer Older
Franziska Koehn's avatar
Franziska Koehn committed
1
2
import gtk

Franziska Koehn's avatar
Franziska Koehn committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class QueryView(gtk.VBox):

    @property
    def credentials(self):
        return "%s:%s" % (self.entry_user.get_text(), self.entry_passw.get_text())

    @credentials.setter
    def credentials(self, new_creds):
        user, _, password = new_creds.partition(":")
        self.entry_user.set_text(user)
        self.entry_passw.set_text(password)

    @property
    def host(self):
        return self.entry_host.get_text()

    @host.setter
    def host(self, new_host):
        self.entry_host.set_text(new_host)

    def __send_query(self):
24
        self.emit("send-query", self.host, self.credentials, self.combobox.get_selected_item(), self.treeView_search.get_query(self.combobox.get_selected_item()), self.treeView_fields.get_selected_fields())
Franziska Koehn's avatar
Franziska Koehn committed
25
26
27
28

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

29
30
        import xnat_search

Franziska Koehn's avatar
Franziska Koehn committed
31
32
        import gobject
        gobject.signal_new("send-query", QueryView, gobject.SIGNAL_RUN_FIRST,
Franziska Koehn's avatar
Franziska Koehn committed
33
        gobject.TYPE_NONE, (gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT))
Franziska Koehn's avatar
Franziska Koehn committed
34

35
36
37
38
39
40
41
42
43
        # root-type

        hBox_root_type = gtk.HBox()
        self.pack_start(hBox_root_type, False, True, 0)

        label_root_type = gtk.Label()
        label_root_type.set_text("Root-Type: ")
        hBox_root_type.pack_start(label_root_type, False, True, 0)

44
45
        def changed_cb(combobox):
            selected =  combobox.get_selected_item()
46
            fields = xnat_search.get_field_labels_required_of_type(selected)
47
48
49
            self.treeView_search.set_root_type(selected)
            self.treeView_fields.show_fields(fields)

Franziska Koehn's avatar
Franziska Koehn committed
50
51
        self.combobox = ComboBoxRootType(gtk.ListStore(str))
        self.combobox.connect('changed', changed_cb)
52
        cell = gtk.CellRendererText()
Franziska Koehn's avatar
Franziska Koehn committed
53
54
        self.combobox.pack_start(cell)
        self.combobox.add_attribute(cell, 'text', 0)
55
        for t in xnat_search.get_data_types():
Franziska Koehn's avatar
Franziska Koehn committed
56
57
            self.combobox.append_text(t)
        hBox_root_type.pack_start(self.combobox, True, True,0)
58
59
60
61
62
63
64

        # Sash

        vpaned_chart = gtk.VPaned()
        vpaned_chart.set_position(250)
        self.pack_start(vpaned_chart, True, True, 0)

Franziska Koehn's avatar
Franziska Koehn committed
65
66
67
68
        # query-tree
        sw_search = gtk.ScrolledWindow()
        sw_search.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        sw_search.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
69
        vpaned_chart.add1(sw_search)
Franziska Koehn's avatar
Franziska Koehn committed
70
71
72
73

        self.treeView_search = TreeViewQuery()
        sw_search.add(self.treeView_search)

74
75
76
77
78
79
80
81
82
        #result fields
        sw_fields = gtk.ScrolledWindow()
        sw_fields.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        sw_fields.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        vpaned_chart.add2(sw_fields)

        self.treeView_fields = TreeViewResultFields()
        sw_fields.add(self.treeView_fields)

Franziska Koehn's avatar
Franziska Koehn committed
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
        # login fields
        hBox_cedentials = gtk.HBox(False,5)
        self.pack_start(hBox_cedentials, expand=False, fill=False)

        label_user =  gtk.Label()
        label_user.set_text("User:")
        hBox_cedentials.pack_start(label_user, expand=False, fill=False)
        self.entry_user = gtk.Entry()
        hBox_cedentials.pack_start(self.entry_user, expand=True, fill=True)

        label_passw =  gtk.Label()
        label_passw.set_text("Password:")
        hBox_cedentials.pack_start(label_passw, expand=False, fill=False)
        self.entry_passw = gtk.Entry()
        self.entry_passw.set_visibility(False)
        hBox_cedentials.pack_start(self.entry_passw, expand=True, fill=True)

        # host-text-field and send-query-button
        hBox_send = gtk.HBox(False,5)
        self.pack_start(hBox_send, expand=False, fill=False)

        label_host =  gtk.Label()
        label_host.set_text("Host:")
        hBox_send.pack_start(label_host, expand=False, fill=False)

        self.entry_host = gtk.Entry()
        hBox_send.pack_start(self.entry_host, expand=True, fill=True)

        self.button_send = gtk.Button(label="Send Query")
        self.button_send.connect("clicked", lambda *_: self.__send_query())
        hBox_send.pack_end(self.button_send, expand=False, fill=False)

115
        # setting start state
Franziska Koehn's avatar
Franziska Koehn committed
116
117
        self.combobox.set_active(0)
        self.treeView_search.set_root_type(self.combobox.get_selected_item())
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

class ComboBoxRootType(gtk.ComboBox):

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

    def get_selected_item(self):
        model = self.get_model()
        index = self.get_active()
        if index > -1:
            return  model[index][0]
        else:
            return None


133
134
135
136
137
class TreeViewResultFields(gtk.TreeView):

    def __init__(self):
        super(TreeViewResultFields, self).__init__()
        self.create_columns()
138
        self.store = gtk.ListStore(bool, str, bool) # is_selected, label, is_required
139
140
141
142
143
144
145
        self.set_model(self.store)

    def create_columns(self):

        def callback_toggled(cellrenderertoggle, path_string, col, *_):
            it = self.store.get_iter_from_string(path_string)
            is_active = cellrenderertoggle.get_active()
146
147
            if not self.store.get_value(it, 2):
                self.store.set(it, col, not is_active)
148
149
150
151
152
153
154
155
156
157

        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)

158
        column = gtk.TreeViewColumn("Requested Result-Fields", gtk.CellRendererText(), text=1)
159
160
161
162
        column.set_sort_column_id(1)
        column.set_resizable(True)
        self.append_column(column)

163
    def show_fields(self, fields):
164
165
        self.store.clear()
        for f in fields:
166
167
168
            self.store.append( [ f[1],  # is_selected
                                f[0],   # label
                                f[1]    # is_required
169
170
                                ])

Franziska Koehn's avatar
Franziska Koehn committed
171
172
173
174
175
176
    def get_selected_fields(self):
        results = []
        for r in self.store:
            if r[0]: # if selected
                results.append(r[1])
        return results
Franziska Koehn's avatar
Franziska Koehn committed
177
178

class TreeViewQuery(gtk.TreeView):
Franziska Koehn's avatar
Franziska Koehn committed
179
180
181

    inital_value = "..."
    tooltip = 'Delete by using right click'
182
    root_type = ''
Franziska Koehn's avatar
Franziska Koehn committed
183
184
185
186
    methods = [] #["AND", "OR"]
    types = [] #["TYPE", "TE", "TI"]
    operators = [] #["LIKE", ">", "<", "<=", ">=", "="]

187

Franziska Koehn's avatar
Franziska Koehn committed
188
    def __init__(self):
Franziska Koehn's avatar
Franziska Koehn committed
189
        super(TreeViewQuery, self).__init__()
Franziska Koehn's avatar
Franziska Koehn committed
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

        self.create_type_list()
        self.create_method_list()
        self.create_operator_list()

        self.set_rules_hint(True)
        self.create_columns()
        self.set_headers_visible(True)
        self.create_model()
        self.set_model(self.store)

        def on_treeview_button_press_event(treeview, event):
            if event.button == 3:
                x = int(event.x)
                y = int(event.y)
                path_info = treeview.get_path_at_pos(x, y)
                if path_info is not None:
                    path, col, cellx, celly = path_info
                    iter = self.store.get_iter(path)
                    if not path == (0,):
                        md = gtk.MessageDialog(type=gtk.MESSAGE_WARNING,
                                               buttons=gtk.BUTTONS_YES_NO,
                                               message_format="Remove the element and its children?"
                                               )
                        response = md.run()
                        md.destroy()
                        if response == gtk.RESPONSE_YES:
                            self.store.remove(iter)

        self.connect('button_press_event', on_treeview_button_press_event)

        self.set_tooltip_column(4)

    def create_columns(self):

        def root_changed_callback(combo, path_string, new_iter, col, del_col, *_):

            it = self.store.get_iter_from_string(path_string)
            new_value = combo.get_property("model")[new_iter][0]
            old_value = self.store.get_value(it, col)

            def append_inital_child(parent):
                last_child = self.store.iter_n_children(parent) -1
                child = self.store.iter_nth_child(parent, last_child)
                if not child is None:
                    child_value = self.store.get_value(child, 0)
                    if not child_value == self.inital_value:
                        self.store.append(parent,  (self.inital_value, '', '', False, self.tooltip))
                else:
                    self.store.append(parent,  (self.inital_value, '', '', False, self.tooltip))

            def set_new_value_and_append_child(has_operator_and_value):
                self.store.set(it, col, str(new_value))
                self.store.set(it, 3, has_operator_and_value)
                if has_operator_and_value:
                    parent = self.store.iter_parent(it)
                    if not parent is None:
                        append_inital_child(parent)
                else:
                    append_inital_child(it)

            if new_value in self.types:
                if (not old_value == self.inital_value) and (not old_value in self.types):
                    md = gtk.MessageDialog(type=gtk.MESSAGE_WARNING,
                                           buttons=gtk.BUTTONS_YES_NO,
                                           message_format="The children will be destroyed!Continue?"
                                           )
                    response = md.run()
                    md.destroy()
                    if response == gtk.RESPONSE_YES:
                        set_new_value_and_append_child(True)
                        while not self.store.iter_children(it) is None:
                            child = self.store.iter_children(it)
                            self.store.remove(child)
                else:
                    set_new_value_and_append_child(True)
            else:
                set_new_value_and_append_child(False)
                for c in del_col:
                    self.store.set(it, c, '')

        def cell_changed_callback(combo, path_string, new_iter, col, *_):
            it = self.store.get_iter_from_string(path_string)
            text = combo.get_property("model")[new_iter][0]
            self.store.set(it, col, str(text))

        def cell_edited_callback(cellrenderertext, path_string, new_text, col, *_):
            it = self.store.get_iter_from_string(path_string)
            self.store.set(it, col, new_text)

        rendererCombo = gtk.CellRendererCombo()
        rendererCombo.set_property('editable', True)
        rendererCombo.set_property ("model", self.create_combo_list_type());
        rendererCombo.set_property ("text-column", 0);
        rendererCombo.connect('changed', root_changed_callback, 0, [1,2])

286
        column = gtk.TreeViewColumn("Query", rendererCombo, text=0)
Franziska Koehn's avatar
Franziska Koehn committed
287
288
289
290
291
292
293
        column.set_sort_column_id(0)
        column.set_min_width(100)
        column.set_resizable(True)
        self.append_column(column)

        rendererCombo = gtk.CellRendererCombo()
        rendererCombo.set_property('editable', True)
294
        rendererCombo.set_property ("model", self.create_combo_list_operator())
Franziska Koehn's avatar
Franziska Koehn committed
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
        rendererCombo.set_property ("text-column", 0);
        rendererCombo.connect('changed', cell_changed_callback, 1)

        column = gtk.TreeViewColumn("Operator", rendererCombo, text=1, editable=3)
        column.set_sort_column_id(1)
        column.set_min_width(70)
        self.append_column(column)

        rendererText = gtk.CellRendererText()
        rendererText.set_property('editable', True)
        rendererText.connect('edited', cell_edited_callback, 2)

        column = gtk.TreeViewColumn("Value", rendererText, text=2, editable=3)
        column.set_sort_column_id(2)
        column.set_min_width(100)
        column.set_resizable(True)

        self.append_column(column)

    @staticmethod
    def create_combo_list(labels):
        combo_model = gtk.ListStore(str)
        for l in labels:
            combo_model.append((str(l),))
        return combo_model

321
322
323
324
325
326
    def set_root_type(self, root_type):
        self.root_type = root_type
        self.reset_model()
        self.create_type_list()
        self.get_column(0).get_cell_renderers()[0].set_property ("model", self.create_combo_list_type()); #TODO column number???

Franziska Koehn's avatar
Franziska Koehn committed
327
328
329
330
331
332
    def create_combo_list_type(self):
        return self.create_combo_list(self.methods+self.types)

    def create_combo_list_operator(self):
        return self.create_combo_list(self.operators)

333
334
335
336
    def reset_model(self):
        self.store.clear()
        self.store.append(None, (self.inital_value, '', '', False, self.tooltip))

Franziska Koehn's avatar
Franziska Koehn committed
337
338
339
340
341
342
    def create_model(self):
        self.store = gtk.TreeStore(str, str, str, bool, str)
        self.store.append(None, (self.inital_value, '', '', False, self.tooltip))

    def create_type_list(self):
        import xnat_search
343
        self.types = xnat_search.get_field_labels_of_type(self.root_type)
Franziska Koehn's avatar
Franziska Koehn committed
344
345
346
347
348
349
350
351
352

    def create_method_list(self):
        import xnat_search
        self.methods = xnat_search.get_query_methods()

    def create_operator_list(self):
        import xnat_search
        self.operators = xnat_search.get_operators()

353
    def get_query(self, type):
Franziska Koehn's avatar
Franziska Koehn committed
354
355
        import xnat_search

356
        search_fields = xnat_search.get_fields_of_type(type)
Franziska Koehn's avatar
Franziska Koehn committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
        query = []

        def add_next_to_querry(iter):
            value = self.store.get_value(iter, 0)
            if value in self.methods:
                query.append(value)
            elif value in self.types:
                for field in search_fields:
                    if field['label'] == value:
                        value = field['field']
                operator = self.store.get_value(iter, 1)
                field_value = self.store.get_value(iter, 2)
                query.append( (value, operator, field_value) )
            child = self.store.iter_children(iter)
            if not child is None:
                add_next_to_querry(child)
            next_ = self.store.iter_next(iter)
            if not next_ is None:
                add_next_to_querry(next_)

        it = self.store.get_iter_first()
        add_next_to_querry(it)
        return query