added input history
authorFlorian Schweikert <kelvan@logic.at>
Sun, 20 Feb 2011 21:46:49 +0000 (22:46 +0100)
committerFlorian Schweikert <kelvan@logic.at>
Sun, 20 Feb 2011 21:46:49 +0000 (22:46 +0100)
__init__.py [new file with mode: 0644]
history.py [new file with mode: 0644]
pyWienerLinien.py
settings.py

diff --git a/__init__.py b/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/history.py b/history.py
new file mode 100644 (file)
index 0000000..b74343c
--- /dev/null
@@ -0,0 +1,36 @@
+import os
+
+class History(list):
+
+    def __init__(self, hist_file, *args, **kwargs):
+        if os.path.isfile(hist_file):
+            f = open(hist_file, 'r')
+            h = map(lambda x: x.strip(), f.readlines())
+            f.close()
+            self._file = open(hist_file, 'a')
+        else:
+            self._file = open(hist_file, 'w')
+            h = []
+
+        list.__init__(self, h, *args, **kwargs)
+
+    def __add__(self, item):
+        if not item in self:
+            super(History, self).__add__(item)
+            self._file.writelines(map(lambda x: x + '\n', self))
+            self._file.flush()
+
+    def insert(self, index, item):
+        if not item in self:
+            super(History, self).insert(index, item)
+            self._file.writelines(map(lambda x: x + '\n', self))
+            self._file.flush()
+
+    def __delitem__(self, item):
+        super(History, self).__deltitem(item)
+        self._file.writelines(map(lambda x: x + '\n', self))
+        self._file.flush()
+        self._file.close()
+
+    def close(self):
+        self._file.close()
index 0602427..9e79375 100644 (file)
@@ -4,10 +4,12 @@ import sys
 import os
 import time
 import logging
+import webbrowser
 from PySide import QtCore, QtGui
 from Ui_Qt import Ui_MainWindow
 from wlSearch import Search
-import webbrowser
+from history import History
+import settings
 
 
 class WienerLinienQt(QtGui.QMainWindow, Ui_MainWindow):
@@ -16,12 +18,22 @@ class WienerLinienQt(QtGui.QMainWindow, Ui_MainWindow):
     def __init__(self):
         QtGui.QMainWindow.__init__(self)
         self.setupUi(self)
-
         self.connect(self.btnSearch, QtCore.SIGNAL("clicked()"), self.search)
 
+        self.history = History(settings.hist_file)
+        self.editOrigin.addItems(self.history)
+        self.editDestination.addItems(self.history)
+
     def search(self):
         origin = self.editOrigin.currentText()
         destination = self.editDestination.currentText()
+
+        self.history.insert(0, origin)
+        self.history.insert(0, destination)
+
+        self.editOrigin.insertItems(1, self.history)
+        self.editDestination.insertItems(1, self.history)
+
         if not origin and destination:
             self.btnSearch.setText("Search - Missing input")
         else:
@@ -36,6 +48,7 @@ class WienerLinienQt(QtGui.QMainWindow, Ui_MainWindow):
             self.btnSearch.setText("Search - Opening webbrowser")
             return True
 
+
 if __name__ == "__main__":
     app = QtGui.QApplication(sys.argv)
     w = WienerLinienQt()
index 3a75377..e1bc63a 100644 (file)
@@ -1 +1,5 @@
+from os import path
+
+folder = path.dirname(__file__)
 action = 'http://efa.vor.at/wvb/XSLT_TRIP_REQUEST2'
+hist_file = path.join(folder, '.wl_history')