cleanup Parser
[pywienerlinien] / pyWienerLinien.py
1 #!/usr/bin/env python
2
3 import sys
4 import os.path
5 import webbrowser
6 from PySide.QtCore import SIGNAL, QTranslator, QObject
7 from PySide.QtGui import QApplication, QMainWindow
8 from Ui_Qt import Ui_MainWindow
9 from wlSearch import Search
10 from history import History
11 import settings
12
13
14 class WienerLinienQt(QMainWindow, Ui_MainWindow):
15     types = ('stop', 'address', 'poi')
16
17     def __init__(self):
18         QMainWindow.__init__(self)
19         # _s is used to keep a reference to the Search object, so it does
20         # not get destroyed when it falls out of scope (the QML view is
21         # destroyed as soon as the Search object is destroyed!)
22         self._s = None
23         self.setupUi(self)
24         self.connect(self.btnSearch, SIGNAL("clicked()"), self.search)
25         self.connect(self.actionToggle, SIGNAL("activated()"), self.toggle)
26
27         self.history = History(settings.hist_file)
28         self.editOrigin.addItems(self.history)
29         self.editDestination.addItems(self.history)
30
31         self.editOrigin.clearEditText()
32         self.editDestination.clearEditText()
33
34     def search(self):
35         origin = self.editOrigin.currentText()
36         destination = self.editDestination.currentText()
37
38         if not (origin and destination):
39             self.btnSearch.setText(self.btnSearch.tr("Search - Missing input"))
40             return False
41
42         self.history.insert(0, origin)
43         self.history.insert(0, destination)
44
45         if not origin in self.history:
46             self.editOrigin.insertItems(0, origin)
47             self.editDestination.insertItems(0, origin)
48
49         if not destination in self.history:
50             self.editOrigin.insertItems(0, destination)
51             self.editDestination.insertItems(0, destination)
52
53         self._s = Search(origin, destination, \
54                    origin_type=self.types[self.comboOrigin.currentIndex()], \
55                    destination_type=self.types[self.comboDestination.currentIndex()])
56         self._s.open_qml()
57         return True
58
59     def toggle(self):
60         eo = self.editOrigin.currentText()
61         ed = self.editDestination.currentText()
62         self.editOrigin.setEditText(ed)
63         self.editDestination.setEditText(eo)
64
65         co = self.comboOrigin.currentIndex()
66         cd = self.comboDestination.currentIndex()
67         self.comboOrigin.setCurrentIndex(cd)
68         self.comboDestination.setCurrentIndex(co)
69
70 if __name__ == "__main__":
71     app = QApplication(sys.argv)
72     t = QTranslator()
73     t.load('', directory='lang', suffix='po')
74     app.installTranslator(t)
75     w = WienerLinienQt()
76     w.show()
77     sys.exit(app.exec_())