Changeset 4059

Show
Ignore:
Timestamp:
05/03/2008 05:33:31 PM (2 months ago)
Author:
ed
Message:

Added a ToolTipText? property for Nodes. This way we can set the tooltips on a per-node basis.

Added the UseNodeToolTips? property on the tree. When this is True, the ToolTipText? is set to the node currently under the mouse; if there is no node, the tree's ToolTipText? is displayed. If the node does not have a specific ToolTipText? set, the node's Caption will be displayed in the ToolTipText?.

Added the ImageSize? property to the tree. This is a two-tuple of ints, with a default of (16, 16). This will ensure that all images added to a tree are consistent in size; some of the standard bitmaps are 16x15 or otherwise not acceptable for use in a tree, especially under Windows.

Added the option 'clearImageList' to the clear() method of the tree.

Greatly improved the makeDirTree() method. Added two parameters: 'ignore' and 'expand'; the first allows you to specify file patterns to exclude from the tree (e.g., "*pyc" to exclude compiled scripts); the latter will expand the tree once it is built. Also added default folder/file images to the generated tree. The nodes also include the full path to the file in their ToolTipText?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/ui/uiwx/dTreeView.py

    r3303 r4059  
    2424        # Nodes can have objects associated with them 
    2525        self._object = None 
     26        # Custom text to display as a tooltip 
     27        self._toolTipText = None 
    2628        # Add minimal Dabo functionality 
    2729        self.afterInit() 
     
    262264 
    263265     
     266    def _getToolTipText(self): 
     267        return self._toolTipText 
     268 
     269    def _setToolTipText(self, val): 
     270        if self._constructed(): 
     271            self._toolTipText = val 
     272        else: 
     273            self._properties["ToolTipText"] = val 
     274 
     275 
    264276    BackColor = property(_getBackColor, _setBackColor, None, 
    265277            _("Background color of this node  (str, 3-tuple, or wx.Colour)") ) 
     
    325337    Siblings = property(_getSiblings, None, None, 
    326338            _("List of all nodes with the same parent node.  (list of dNodes)") ) 
    327      
     339 
     340    ToolTipText = property(_getToolTipText, _setToolTipText, None, 
     341            _("""Text to display when the mouse hovers over this node. The tree's  
     342            UseNodeToolTips property must be True for this to have any effect.  (str)""")) 
     343 
     344 
    328345 
    329346    DynamicBackColor = makeDynamicProperty(BackColor) 
     
    338355    DynamicImage = makeDynamicProperty(Image) 
    339356    DynamicSelected = makeDynamicProperty(Selected) 
    340  
     357    DynamicToolTipText = makeDynamicProperty(ToolTipText) 
     358     
    341359     
    342360 
     
    352370        # Class to use for creating nodes 
    353371        self._nodeClass = dNode 
     372        # Default size for images added to the tree. 
     373        self._imageSize = (16, 16) 
     374        # Do we set tooltips from the nodes? 
     375        self._useNodeToolTips = False 
     376        # Store the default ToolTipText while UseNodeToolTips is True 
     377        self._storedToolTipText = None 
    354378         
    355379        style = self._extractKey((properties, attProperties, kwargs), "style", 0) | wx.TR_HAS_VARIABLE_ROW_HEIGHT 
     
    405429        self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.__onTreeBeginDrag) 
    406430        self.Bind(wx.EVT_TREE_END_DRAG, self.__onTreeEndDrag) 
     431        self.Bind(wx.EVT_MOTION, self.__onTreeMouseMove) 
    407432 
    408433 
     
    446471 
    447472         
    448     def clear(self): 
     473    def clear(self, clearImageList=False): 
    449474        self.DeleteAllItems() 
    450475        self.nodes = [] 
     476        if clearImageList: 
     477            il = self.GetImageList() 
     478            if il: 
     479                il.RemoveAll() 
     480            self.__imageList = {} 
    451481     
    452482     
     
    531561        a reference to it that is retrievable via the key value. 
    532562        """ 
     563        # Default image size 
     564        wd, ht = self.ImageSize 
     565        il = self.GetImageList() 
     566        if not il: 
     567            il = wx.ImageList(wd, ht, initialCount=0) 
     568            self.AssignImageList(il) 
     569        else: 
     570            if il.GetImageCount(): 
     571                wd, ht = il.GetSize(0) 
    533572        if key is None: 
    534573            key = str(img) 
    535574        if isinstance(img, basestring): 
    536             img = dabo.ui.strToBmp(img) 
    537         il = self.GetImageList() 
    538         if not il: 
    539             il = wx.ImageList(16, 16, initialCount=0) 
    540             self.AssignImageList(il) 
     575            img = dabo.ui.strToBmp(img, width=wd, height=ht) 
    541576        idx = il.Add(img) 
    542577        self.__imageList[key] = idx 
     
    783818     
    784819     
    785     def makeDirTree(self, dirPath, wildcard=None, showHidden=False): 
    786         """Make this dTreeView show a filesystem directory hierarchy. 
     820    def makeDirTree(self, dirPath, wildcard=None, ignored=None,  
     821            showHidden=False, expand=False): 
     822        """Make this dTreeView show a filesystem directory hierarchy. You 
     823        can specify a wildcard pattern: e.g., "*py" will only include files 
     824        ending in 'py'. If no wildcard is specified, all files will be included. 
     825         
     826        You can also specify file patterns to ignore in the 'ignore' parameter. 
     827        This can be a single string of a file pattern, or a list of such patterns. 
     828        Any file matching any of these patterns will not be included in the tree. 
     829         
     830        By default, hidden files (i.e., those beginning with a period) are ignored. 
     831        You can optionally show them by passing True in the showHidden 
     832        parameter. 
     833         
     834        The tree defaults to fully collapsed; you can change it to fully  
     835        expanded by passing True in the 'expand' parameter. 
    787836 
    788837        Warning: Don't use this for huge hierarchies, as it blocks while 
     
    790839        they are opened. 
    791840        """ 
    792         self.clear() 
     841        self.clear(clearImageList=True) 
     842        # Add the standard images for a directory tree 
     843        self.addImage("folder", "folder") 
     844        self.addImage("folderopen", "folderopen") 
     845        self.addImage("normalfile", "file") 
     846        self.addImage("executablefile", "executablefile") 
     847         
    793848        # Add any trailing slash character 
    794849        self._pathNode = {} 
     
    796851        def addNode(showHid, currDir, fNames): 
    797852            prnt, nm = os.path.split(currDir) 
    798             if not showHid: 
    799                 if nm[:1] == ".": 
    800                     return 
     853            if not showHid and nm.startswith("."): 
     854                return 
    801855            try: 
    802856                nd = self._pathNode[currDir] = self._pathNode[prnt].appendChild(nm) 
     
    808862                    # parent wasn't added, because it was hidden 
    809863                    return 
     864            self.setNodeImg(nd, "folder", "normal") 
     865            self.setNodeImg(nd, "folderopen", "expanded") 
     866            nd.ToolTipText = currDir 
     867            acceptedNames = ignoredNames = None 
     868            if wildcard is not None: 
     869                acceptedNames = glob.glob(os.path.join(currDir, wildcard)) 
     870            if ignored is not None: 
     871                ignoredNames = [] 
     872                for ig in ignored: 
     873                    ignoredNames += glob.glob(os.path.join(currDir, ig)) 
    810874            for f in fNames: 
    811875                fullName = os.path.join(currDir, f) 
     
    813877                    # it will be added as a directory 
    814878                    continue 
    815                 if not showHid: 
    816                     if f[:1] == ".": 
     879                if not showHid and f.startswith("."): 
     880                    continue 
     881                if acceptedNames is not None: 
     882                    if fullName not in acceptedNames: 
    817883                        continue 
    818                 if wildcard is not None: 
    819                     res = glob.glob(os.path.join(currDir, wildcard)) 
    820                     if not fullName in res: 
     884                if ignoredNames is not None: 
     885                    if fullName in ignoredNames: 
    821886                        continue 
    822                 nd.appendChild(f) 
     887                kid = nd.appendChild(f) 
     888                self.setNodeImg(kid, "file", "normal") 
     889                kid.ToolTipText = fullName 
    823890 
    824891        def sortNode(arg, currDir, fNames): 
     
    826893                self.SortChildren(self._pathNode[currDir].itemID) 
    827894 
     895        if ignored and not isinstance(ignored, (list, tuple)): 
     896            # single string passed 
     897            ignored = [ignored] 
    828898        os.path.walk(dirPath, addNode, showHidden) 
    829899        os.path.walk(dirPath, sortNode, None) 
     900        if expand: 
     901            self.expandAll() 
    830902 
    831903 
     
    9651037 
    9661038 
     1039    def __onTreeMouseMove(self, evt): 
     1040        if self._useNodeToolTips: 
     1041            nd = self.getNodeUnderMouse() 
     1042            if nd: 
     1043                if nd.ToolTipText: 
     1044                    self.ToolTipText = nd.ToolTipText 
     1045                else: 
     1046                    self.ToolTipText = nd.Caption 
     1047            else: 
     1048                if self._storedToolTipText is not None: 
     1049                    self.ToolTipText = self._storedToolTipText 
     1050                else: 
     1051                    self.ToolTipText = "" 
     1052         
     1053 
    9671054    def _getBaseNodes(self): 
    9681055        if self.ShowRootNode: 
     
    9811068        if val: 
    9821069            self._addWindowStyleFlag(wx.TR_EDIT_LABELS) 
     1070 
     1071 
     1072    def _getImageSize(self): 
     1073        return self._imageSize 
     1074 
     1075    def _setImageSize(self, val): 
     1076        if self._constructed(): 
     1077            self._imageSize = val 
     1078        else: 
     1079            self._properties["ImageSize"] = val 
    9831080 
    9841081 
     
    11001197            # Control may not be constructed yet 
    11011198            pass 
    1102              
     1199 
     1200 
     1201    def _getUseNodeToolTips(self): 
     1202        return self._useNodeToolTips 
     1203 
     1204    def _setUseNodeToolTips(self, val): 
     1205        if self._constructed(): 
     1206            if val: 
     1207                self._storedToolTipText = self.ToolTipText 
     1208            else: 
     1209                if self._storedToolTipText is not None: 
     1210                    self.ToolTipText = self._storedToolTipText 
     1211            self._useNodeToolTips = val 
     1212        else: 
     1213            self._properties["UseNodeToolTips"] = val 
     1214 
    11031215 
    11041216    BaseNodes = property(_getBaseNodes, None, None, 
     
    11101222        _("""Specifies whether the tree labels can be edited by the user.""")) 
    11111223 
     1224    ImageSize = property(_getImageSize, _setImageSize, None, 
     1225            _("Size of images added to the tree. Default=(15, 15)  (2-tuple of int)")) 
     1226 
    11121227    MultipleSelect = property(_getMultipleSelect, _setMultipleSelect, None, 
    11131228        _("""Specifies whether more than one node may be selected at once.""")) 
     
    11381253    ShowRootNodeLines = property(_getShowRootNodeLines, _setShowRootNodeLines, None, 
    11391254        _("""Specifies whether vertical lines are shown between root siblings.""")) 
     1255     
     1256    UseNodeToolTips = property(_getUseNodeToolTips, _setUseNodeToolTips, None, 
     1257            _("""When True, the ToolTipText displayed is taken from the node.   
     1258            Default=False  (bool)""")) 
     1259 
    11401260 
    11411261 
     
    11631283        self.expandAll() 
    11641284        self.Hover = True 
    1165      
    1166     def onMouseMove(self, evt): 
    1167         nd = self.getNodeUnderMouse() 
    1168         if nd: 
    1169             self.ToolTipText = nd.Caption 
    1170         else: 
    1171             self.ToolTipText = "" 
    1172  
     1285        self.ToolTipText = _("Default ToolTip for the Tree") 
     1286        self.ImageSize = (16, 16) 
     1287     
    11731288    def onHit(self, evt): 
    1174         ## pkm: currently, Hit happens on left mouse up, which totally ignores 
    1175         ##      keyboarding through the tree. I'm wondering about mapping  
    1176         ##      TreeSelection instead... thoughts? 
    11771289        print "Hit!" 
    11781290     
     
    12401352            chk = dabo.ui.dCheckBox(mp, Caption="ShowRootNodeLines",  
    12411353                    DataSource=tree, DataField="ShowRootNodeLines") 
     1354            sz.append(chk, halign="Left") 
     1355             
     1356            chk = dabo.ui.dCheckBox(mp, Caption="UseNodeToolTips",  
     1357                    DataSource=tree, DataField="UseNodeToolTips") 
    12421358            sz.append(chk, halign="Left") 
    12431359             
     
    12551371            sz.appendSpacer(10) 
    12561372         
    1257          
    12581373        def onExpandAll(self, evt): 
    12591374            self.tree.expandAll() 
    1260          
    12611375         
    12621376        def onCollapseAll(self, evt):