1 # -*- coding: utf-8 -*-
5 from datetime import datetime
10 from parseHtml import Parser
12 from PySide.QtCore import Qt
13 from PySide.QtDeclarative import QDeclarativeView
15 def QMLModel(overview, details):
16 # Mapping from the "overview" data structure to a "plain" data
17 # structure to be used as model for the qml listview
22 'date': item['date'].strftime('%d.%m.%Y') if item['date'] else u'Fußweg',
23 'duration': item['duration'].strftime('%H:%M'),
24 'price': item['price'],
25 'change': item['change'],
26 'details': details[i],
29 if len(item['time']) == 2 and all(x is not None for x in item['time']):
31 'time_from': item['time'][0].strftime('%H:%M'),
32 'time_to': item['time'][1].strftime('%H:%M'),
35 d.update({'time_from': '-', 'time_to': '-'})
44 def __init__(self, origin, destination, origin_type='stop', destination_type='stop', parent=None):
46 self.destination = destination
47 self.origin_type = origin_type
48 self.destination_type = destination_type
53 def get_html(self, dtime=datetime.now()):
54 return urllib2.urlopen('%s?%s' % (settings.action, self.get_parameter(dtime)))
56 def open_browser(self, dtime=datetime.now()):
57 webbrowser.open('%s?%s' % (settings.action, self.get_parameter(dtime)))
59 def open_qml(self, dtime=datetime.now()):
60 p = Parser(self.get_html(dtime))
61 self.qml_model = QMLModel(p.overview, p.details)
62 self.view = QDeclarativeView(self.parent)
63 self.view.setWindowTitle('Search results')
64 self.view.setWindowFlags(Qt.Window)
65 # quick & dirty workaround
67 self.view.setAttribute(Qt.WA_Maemo5StackedWindow)
70 self.view.setResizeMode(QDeclarativeView.SizeRootObjectToView)
71 self.view.setSource('ui/Overview.qml')
72 self.view.rootObject().setProperty('model', self.qml_model)
75 def get_datetime(self, dtime):
76 return (dtime.strftime('%d.%m.%Y'), dtime.strftime('%H:%M'))
78 def get_parameter(self, dtime):
79 date, time = self.get_datetime(dtime)
81 post = {'language': 'de',
86 'anySigWhenPerfectNoOtherMatches': 1,
87 'itdLPxx_locationServerActive': '',
88 'locationServerActive': 0,
89 'typeInfo_origin': 'invalid',
90 'placeState_origin': 'empty',
91 'placeInfo_origin': 'invalid',
92 'place_origin': 'Wien',
93 'type_origin': self.origin_type, # stop/address/poi
94 'nameState_origin': 'empty',
95 'nameInfo_origin': 'invalid',
97 'name_origin': self.origin,
98 'typeInfo_destination': 'invalid',
99 'placeState_destination': 'empty',
100 'placeInfo_destination': 'invalid',
101 'place_destination': 'Wien',
102 'type_destination': self.destination_type, # stop/address/poi
103 'nameState_destination': 'empty',
104 'nameInfo_destination': 'invalid',
105 'anyType_destination': '',
106 'name_destination': self.destination,
107 'itdTripDateTimeDepArr': 'dep',
108 'itdDateDayMonthYear': date, # DD.MM.YYYY
109 'itdTime': time, # HH:MM
110 'submitbutton': 'SUCHEN'
113 params = urllib.urlencode(post)