Squish/Robust Selection of View Items with Variable Text

From Qt Wiki
Jump to navigation Jump to search


Robust selection of items in a tree or list view with variable text

This article is similiar to to Finding list items that ends in a given string however goes into a bit more detail and is also functional for treeviews as well as listviews. Dealing with a treeview requires careful handling of "."-separated paths.

Imagine you need to select some item in a treeview, but you don't know the exact string of the item as it may have changed. Alternatively maybe the parent item of the item you wish to select has changed, but the item is still the same. You need to find the item you want in the tree, and extract the correct Squish item identifier to interact with it later on.

For example, assume you need to find a certain file in treeview, but the treeview has items with absolute paths that might be different on various machines. All you know is that the filename you are looking for will be the same, if it exists. You wish to iterate through the treeview, checking each level against some known valid path that you have. For this example, we will assume you have paths such as

server1\serverpath\some\path\test_data\test_project\filetest.db<br />server32\serverpath\some\path\test_data\test_project\filetest.db<br />C:_mirror\some\path\test_data\test_project\filetest.db

and you can't be sure which path is in the treeview (potentially as a parent or child item). This means you might need to convert

server1serverpathsomepathtestdatatestprojectfiletestb.subtem.subSubItem<code>

to

C:localmirrorsomepathtestdatatestprojectfiletestb.subtem.subSubItem

(Note that these paths are escaped so that Squish's itemview functions properly find the paths). As you can see, all three of the paths are the same from the "test_data&quot; folder onwards, so we will use that to split between the "known good&quot; parts of the path we wish to match, and the variable part we wish to replace.

def findRealPath( treeviewname, origpath ):<br /> import re

print &quot;Original path: &quot;, origpath<br /> treeView = waitForObject( treeviewname )<br /> model = treeView.model()

# we are looking for this<br /> originalParts = origpath.split( &quot;testdata&quot; )<br /> pathOnly = originalParts[1]<br /> # Remove the items separated by &quot;.&quot; (but not . which is an escaped dot in the folder path)<br /> print &quot;Now splitting items: s&amp;quot; re.split( &quot;[<sup>].&quot;, pathOnly, 1 )<br /> pieces = re.split( &quot;[</sup>].&quot;, pathOnly, 1 ) # only split once<br /> pathOnly = pieces[ 0 ]<br /> childrenItems = &quot;&quot;<br /> if len( pieces ) &gt; 1: # If this has children<br /> childrenItems = pieces[ 1 ]<br /> toplevel = model.rowCount( QModelIndex() )<br /> for row in range( toplevel ): # iterate through tree, looking for matching file<br /> # shortcut solution: check all top-level items in their first three columns. much more efficient if result is top-level<br /> for col in range( 3 ): # check first few columns<br /> idx = model.index( row, col, QModelIndex() )<br /> if checkModelItemReplacement(idx, pathOnly):<br /> return replaceModelIndex(utils.escape(str( idx.data( Qt.DisplayRole ).toString() )), childrenItems)

# didn't find what we were looking for in the top level, do a search two more levels down<br /> # check all our children as well<br /> for col in range(20):<br /> idx = model.index( row, col, QModelIndex() )<br /> childCount = model.rowCount(idx)<br /> print &quot;Checking children of item %s, found s&amp;quot; (idx.data(Qt.DisplayRole).toString(), childCount)<br /> for childRow in range(childCount):<br /> child = model.index( 0, 0, idx )<br /> print &quot;checking child %s: s&amp;quot; (childRow, child.data(Qt.DisplayRole).toString())<br /> if checkModelItemReplacement(child, pathOnly):<br /> # we need to do the replacement differently, because we need to keep the parent in the path<br /> partpieces = re.split( &quot;([<sup>]).&quot;, origpath )<br /> # find the index that has the test_data/ path. all indexes before that are parents<br /> for i in range(len(partpieces)):<br /> if &quot;testdata&amp;quot; in partpieces[i]: # stop here<br /> # since we split on [</sup>]. and captured the split text, we need to condense each pair of items<br /> num = i/2<br /> parentItems = []<br /> for k in range(num):<br /> parentItems.append( partpieces[k] + partpieces[k+1] ) # rescue the char lost when splitting<br /> parentstr = &quot;.&quot;.join(parentItems)<br /> return replaceModelIndex(utils.escape(str( child.data( Qt.DisplayRole ).toString() )), childrenItems, parentstr)

# we failed<br /> print &quot;Failed to replace: s&amp;quot; origpath<br /> return origpath

# used internally by above<br />def checkModelItemReplacement( idx, pathOnly ):<br /> contents = utils.escape(str( idx.data( Qt.DisplayRole ).toString() ))<br /> print &quot;Checking for %s in s&amp;quot; (pathOnly, contents)<br /> if pathOnly in contents: # this is the entry we are looking for<br /> return True<br /> else:<br /> return False

# used internally by above<br />def replaceModelIndex( contents, children, parents = &quot;&quot; ):<br /> print &quot;Replacing with %s and %s with parent? s&amp;quot; (contents, children, parents)<br /> replaced = contents

if children [[Image:= &quot;&quot;:
        replaced += &quot;.&quot; + children
        print &quot;added children to replaced: %s&quot; % replaced
    if parents |]]= &quot;&quot;:<br /> replaced = parents + &quot;.&quot; + replaced<br /> print &quot;added parents to replaced: s&amp;quot; replaced<br /> return replaced

As is visible in the above code sample, this code only searches three-levels-deep for a match. If your tree is deeper than 3 levels, you'll need to either add another level of drilling down, or convert it into recursive function that works on any arbitrary level. The above code can be used like so:

treeViewName = ":Your_treeview_here&quot;
itemPath = "server1serverpathsomepathtestdatatestprojectfiletestb.subtem.subSubItem&quot;
realItemPath = findRealPath( treeViewName, itemPath )