From 9a177bafb6b6dd59ede41603d87badb3702a8f2c Mon Sep 17 00:00:00 2001 From: Thomas Perl Date: Wed, 14 Mar 2012 20:44:15 +0100 Subject: [PATCH] Favorites draft implementation --- gotovienna-qml | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ gotovienna/defaults.py | 3 +++ qml/MainPage.qml | 46 +++++++++++++++++++++++++++++++++++++ qml/main.qml | 4 ++++ 4 files changed, 112 insertions(+) diff --git a/gotovienna-qml b/gotovienna-qml index d2dc8fa..9ebfa55 100755 --- a/gotovienna-qml +++ b/gotovienna-qml @@ -19,10 +19,13 @@ from gotovienna.gps import * from gotovienna.update import * from gotovienna.config import config as conf +from gotovienna import defaults + import urllib2 import os import sys import threading +import json from datetime import time class Config(QObject): @@ -81,6 +84,58 @@ class AboutInfo(QObject): def getLicense(self): return __license__ +class FavoriteManager(QObject): + def __init__(self): + QObject.__init__(self) + self._faves = [] + if os.path.exists(defaults.favorites_file): + try: + self._faves = json.load(open(defaults.favorites_file, 'r')) + self._faves = map(tuple, self._faves) + print 'faves loaded:', self._faves + except Exception, e: + print 'faves load error:', e + + @Slot(result=int) + def getCount(self): + result = len(self._faves) + print 'getCount->', result + return result + + @Slot(int, result=unicode) + def getItem(self, index): + keys = ['gline', 'gdirection', 'gstation', 'sourceurl', 'isstation'] + result = dict(zip(keys, self._faves[index])) + result['name'] = u'%(gline)s -> %(gdirection)s @ %(gstation)s' % result + result = json.dumps(result) + print 'getItem:', index, result + return result + + def _persist(self): + print 'persist:', self._faves, '->', defaults.favorites_file + try: + fp = open(defaults.favorites_file, 'w') + json.dump(self._faves, fp) + fp.close() + except Exception, e: + print 'faves save error:', e + + @Slot(unicode, unicode, unicode, unicode, bool, int, result=bool) + def isFavorite(self, gline, gdirection, gstation, sourceurl, isstation, x): + k = (gline, gdirection, gstation, sourceurl, isstation) + return (k in self._faves) + + @Slot(unicode, unicode, unicode, unicode, bool) + def toggleFavorite(self, gline, gdirection, gstation, sourceurl, isstation): + k = (gline, gdirection, gstation, sourceurl, isstation) + if k in self._faves: + self._faves.remove(k) + else: + self._faves.append(k) + + self._persist() + + class GotoViennaListModel(QAbstractListModel): def __init__(self, objects=None): QAbstractListModel.__init__(self) @@ -91,6 +146,7 @@ class GotoViennaListModel(QAbstractListModel): def set_objects(self, objects): self._objects = objects + self.reset() def get_objects(self): return self._objects @@ -278,11 +334,14 @@ if __name__ == '__main__': # instantiate the Python object itip = Gui() + favManager = FavoriteManager() + # expose the object to QML context = view.rootContext() context.setContextProperty('itip', itip) context.setContextProperty('aboutInfo', aboutInfo) context.setContextProperty('config', config) + context.setContextProperty('favManager', favManager) if os.path.abspath(__file__).startswith('/usr/bin/'): # Assume system-wide installation, QML from /usr/share/ diff --git a/gotovienna/defaults.py b/gotovienna/defaults.py index 9f5df69..051030c 100644 --- a/gotovienna/defaults.py +++ b/gotovienna/defaults.py @@ -19,6 +19,9 @@ cache_stations = path.join(cache_folder, 'stations.json') sql_gps_query = 'SELECT name FROM stations WHERE lat > ? and lat < ? and lon > ? and lon < ?' sql_file = path.expanduser('~/.gotovienna/stations.db') +# Favorites store +favorites_file = path.expanduser('~/.gotovienna/favorites.json') + # iTip line_overview = 'http://www.wienerlinien.at/itip/linienwahl/' diff --git a/qml/MainPage.qml b/qml/MainPage.qml index c6deee0..b3582c7 100644 --- a/qml/MainPage.qml +++ b/qml/MainPage.qml @@ -13,6 +13,16 @@ Page { //property alias stationSelect: stationSelector property variant nearbyStations + function showFavorites() { + favSelector.model.clear(); + for (var i=0; i