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

class XnatSearchApp(gtk.Window):

5
    args = None
6
    results = []
7
8

    def init_connection_fields(self):
Franziska Koehn's avatar
Franziska Koehn committed
9
10
        self.vbox_search.host = self.args.host
        self.vbox_search.credentials = self.args.user
11

Franziska Koehn's avatar
Franziska Koehn committed
12
13
14
    def __init__(self):
        super(XnatSearchApp, self).__init__()

15
16
17
18
19
20
        import argparse
        parser = argparse.ArgumentParser(description='search and download gui')
        parser.add_argument('--host', type=str, help='hostname or ip-address (including port).', default="http://localhost:8080")
        parser.add_argument('--user', type=str, help='user:passw', default=":")
        self.args = parser.parse_args()

Franziska Koehn's avatar
Franziska Koehn committed
21
        self.set_size_request(1000, 500)
Franziska Koehn's avatar
Franziska Koehn committed
22
23
24
25
26
        self.set_position(gtk.WIN_POS_CENTER)

        self.connect("destroy", gtk.main_quit)
        self.set_title("Xnat Search")

27
28
29
30
31
32
33
34
        # left side

        vbox_root = gtk.VBox(False, 1)

        hpaned = gtk.HPaned()
        hpaned.set_position(400)
        vbox_root.pack_start(hpaned, True, True, 5)

Franziska Koehn's avatar
Franziska Koehn committed
35
        def on_search_query(_, host, credentials, root_type, query, labels):
Franziska Koehn's avatar
Franziska Koehn committed
36
            import xnat_search
Franziska Koehn's avatar
Franziska Koehn committed
37
            user, _, passw = credentials.partition(":")
Franziska Koehn's avatar
Franziska Koehn committed
38
            fields = xnat_search.get_fields_from_labels(labels,root_type)
39
            query_results = xnat_search.search_for(host, 'xnat:mrScanData', query, fields, user, passw)
Franziska Koehn's avatar
Franziska Koehn committed
40
            xnat_search.HOST = host
Franziska Koehn's avatar
Franziska Koehn committed
41

Franziska Koehn's avatar
Franziska Koehn committed
42
            #TODO catch errors
Franziska Koehn's avatar
Franziska Koehn committed
43

Franziska Koehn's avatar
Franziska Koehn committed
44
45
46
            #httplib.ResponseNotReady
            #one message dialog for all

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
            if query_results is "DatabaseError":
                md = gtk.MessageDialog(type=gtk.MESSAGE_WARNING,
                                       buttons=gtk.BUTTONS_OK,
                                       message_format="Error! Check your query."
                                       )
                md.run()
                md.destroy()
                self.results = []
            elif query_results is "ServerNotFoundError":
                md = gtk.MessageDialog(type=gtk.MESSAGE_WARNING,
                                       buttons=gtk.BUTTONS_OK,
                                       message_format="Error! Server not found."
                                       )
                md.run()
                md.destroy()
                self.results = []
            else:
                self.results = query_results
Franziska Koehn's avatar
Franziska Koehn committed
65

66
            self.vbox_results.show_data(self.results, root_type)
67
            self.statusbar.push(0, "%s Results"%len(self.results))
68
            self.chart_view.update_chart(self.results)
Franziska Koehn's avatar
Franziska Koehn committed
69

Franziska Koehn's avatar
Franziska Koehn committed
70
        from xnatgtk.query_view import QueryView
Franziska Koehn's avatar
Franziska Koehn committed
71
72
        self.vbox_search = QueryView(homogeneous=False, spacing=1)
        self.vbox_search.connect("send-query", on_search_query)
Franziska Koehn's avatar
Franziska Koehn committed
73

Franziska Koehn's avatar
Franziska Koehn committed
74
        hpaned.add1(self.vbox_search)
Franziska Koehn's avatar
Franziska Koehn committed
75
76
77
78
79
80
81

        # right site

        vpaned = gtk.VPaned()
        vpaned.set_position(300)
        hpaned.add2(vpaned)

Franziska Koehn's avatar
Franziska Koehn committed
82
83
84
        from xnatgtk.results_view import ResultsDownloadView
        self.vbox_results = ResultsDownloadView(False,1)
        vpaned.add1(self.vbox_results)
Franziska Koehn's avatar
Franziska Koehn committed
85

86
        # chart
Franziska Koehn's avatar
Franziska Koehn committed
87

88
89
90
        from xnatgtk.chart_view import ChartView
        self.chart_view = ChartView()
        vpaned.add2(self.chart_view)
Franziska Koehn's avatar
Franziska Koehn committed
91

Franziska Koehn's avatar
Franziska Koehn committed
92
93
94
95
96
97
        # statusbar

        self.statusbar = gtk.Statusbar()
        vbox_root.pack_start(self.statusbar, False, False, 0)

        self.add(vbox_root)
98
99

        self.init_connection_fields()
Franziska Koehn's avatar
Franziska Koehn committed
100
101
        self.show_all()

Franziska Koehn's avatar
Franziska Koehn committed
102

Franziska Koehn's avatar
Franziska Koehn committed
103
XnatSearchApp()
Franziska Koehn's avatar
Franziska Koehn committed
104
gtk.gdk.threads_init()
Franziska Koehn's avatar
Franziska Koehn committed
105
gtk.main()