UI/Browser.py

changeset 5011
4085e2348621
parent 4651
7f3f276d3bf3
child 5389
9b1c800daff3
--- a/UI/Browser.py	Sat Jul 02 14:54:14 2016 +0200
+++ b/UI/Browser.py	Sat Jul 02 19:14:09 2016 +0200
@@ -12,7 +12,7 @@
 import os
 
 from PyQt5.QtCore import QModelIndex, pyqtSignal, QUrl, Qt, qVersion, \
-    QCoreApplication, QItemSelectionModel
+    QCoreApplication, QItemSelectionModel, QElapsedTimer
 from PyQt5.QtGui import QDesktopServices
 from PyQt5.QtWidgets import QTreeView, QApplication, QMenu, QAbstractItemView
 
@@ -141,6 +141,10 @@
         
         self._init()    # perform common initialization tasks
         
+        self._keyboardSearchString = ""
+        self._keyboardSearchTimer = QElapsedTimer()
+        self._keyboardSearchTimer.invalidate()
+        
     def _init(self):
         """
         Protected method to perform initialization tasks common to this
@@ -729,3 +733,57 @@
         elif self.__embeddedBrowser == 2:
             e5App().getObject("UserInterface")\
                 .showPreferences("projectBrowserPage")
+    
+    def keyboardSearch(self, search):
+        """
+        Public function to search the tree via the keyboard.
+        
+        @param search the character entered via the keyboard
+        @type str
+        """
+        if self.model().rowCount() == 0:
+            return
+        
+        if self.currentIndex().isValid():
+            startIndex = self.currentIndex()
+        else:
+            startIndex = self.model().index(0, 0)
+        
+        keyboardSearchTimeWasValid = self._keyboardSearchTimer.isValid()
+        keyboardSearchTimeElapsed = self._keyboardSearchTimer.restart()
+        if not search or not keyboardSearchTimeWasValid or \
+            keyboardSearchTimeElapsed > \
+                QApplication.keyboardInputInterval():
+            self._keyboardSearchString = search.lower()
+        else:
+            self._keyboardSearchString += search.lower()
+        
+        index = startIndex
+        found = False
+        while True:
+            name = self.model().data(index)
+            if name.lower().startswith(self._keyboardSearchString) and \
+                    self._keyboardSearchType(self.model().item(index)):
+                found = True
+                break
+            
+            index = self.indexBelow(index)
+            if not index.isValid():
+                index = self.model().index(0, 0)
+            if index == startIndex:
+                break
+        
+        if found:
+            self.setCurrentIndex(index)
+    
+    def _keyboardSearchType(self, item):
+        """
+        Protected method to check, if the item is of the correct type.
+        
+        @param item reference to the item
+        @type BrowserItem
+        @return flag indicating a correct type
+        @rtype bool
+        """
+        return isinstance(
+            item, (BrowserDirectoryItem, BrowserFileItem, BrowserSysPathItem))

eric ide

mercurial