Squish/Robust Selection of View Items with Variable Text: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Add "cleanup" tag)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:Tools::Squish]]
[[Category:Tools::Squish]]



Revision as of 16:57, 3 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

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
server32\serverpath\some\path\test_data\test_project\filetest.db
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" folder onwards, so we will use that to split between the "known good" parts of the path we wish to match, and the variable part we wish to replace.

def findRealPath( treeviewname, origpath ):
 import re

print "Original path: ", origpath
 treeView = waitForObject( treeviewname )
 model = treeView.model()

# we are looking for this
 originalParts = origpath.split( "testdata" )
 pathOnly = originalParts[1]
 # Remove the items separated by "." (but not . which is an escaped dot in the folder path)
 print "Now splitting items: s" re.split( "[<sup>].", pathOnly, 1 )
 pieces = re.split( "[</sup>].", pathOnly, 1 ) # only split once
 pathOnly = pieces[ 0 ]
 childrenItems = ""
 if len( pieces ) > 1: # If this has children
 childrenItems = pieces[ 1 ]
 toplevel = model.rowCount( QModelIndex() )
 for row in range( toplevel ): # iterate through tree, looking for matching file
 # shortcut solution: check all top-level items in their first three columns. much more efficient if result is top-level
 for col in range( 3 ): # check first few columns
 idx = model.index( row, col, QModelIndex() )
 if checkModelItemReplacement(idx, pathOnly):
 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
 # check all our children as well
 for col in range(20):
 idx = model.index( row, col, QModelIndex() )
 childCount = model.rowCount(idx)
 print "Checking children of item %s, found s" (idx.data(Qt.DisplayRole).toString(), childCount)
 for childRow in range(childCount):
 child = model.index( 0, 0, idx )
 print "checking child %s: s" (childRow, child.data(Qt.DisplayRole).toString())
 if checkModelItemReplacement(child, pathOnly):
 # we need to do the replacement differently, because we need to keep the parent in the path
 partpieces = re.split( "([<sup>]).", origpath )
 # find the index that has the test_data/ path. all indexes before that are parents
 for i in range(len(partpieces)):
 if "testdata" in partpieces[i]: # stop here
 # since we split on [</sup>]. and captured the split text, we need to condense each pair of items
 num = i/2
 parentItems = []
 for k in range(num):
 parentItems.append( partpieces[k] + partpieces[k+1] ) # rescue the char lost when splitting
 parentstr = ".".join(parentItems)
 return replaceModelIndex(utils.escape(str( child.data( Qt.DisplayRole ).toString() )), childrenItems, parentstr)

# we failed
 print "Failed to replace: s" origpath
 return origpath

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

# used internally by above
def replaceModelIndex( contents, children, parents = "" ):
 print "Replacing with %s and %s with parent? s" (contents, children, parents)
 replaced = contents

if children [[Image:= "":
        replaced += "." + children
        print "added children to replaced: %s" % replaced
    if parents |]]= "":
 replaced = parents + "." + replaced
 print "added parents to replaced: s" replaced
 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" itemPath = "server1serverpathsomepathtestdatatestprojectfiletestb.subtem.subSubItem" realItemPath = findRealPath( treeViewName, itemPath )