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

From Qt Wiki
Jump to navigation Jump to search
m (style, fixed link)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
[[Category:Tools::Squish]]
[[Category:Squish]]


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.
This article is similiar to to [[Finding_List_Items_that_End_in_a_Given_String|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.
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.
Line 7: Line 7:
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
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


<code>server1\serverpath\some\path\test_data\test_project\filetest.db
<code>
server1\serverpath\some\path\test_data\test_project\filetest.db
server32\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</code>
C:\_mirror\some\path\test_data\test_project\filetest.db
</code>


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
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


<code>server1serverpathsomepathtestdatatestprojectfiletestb.subtem.subSubItem</code>
<code>server1\serverpath\some\path\test_data\test_project\filetestdb.subtem.subSubItem</code>


to
to


<code>C:localmirrorsomepathtestdatatestprojectfiletestb.subtem.subSubItem</code>
<code>C:\_mirror\some\path\test_data\test_project\filetestdb.subtem.subSubItem</code>


(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.
(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.


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


print "Original path: ", origpath
print "Original path: ", origpath
  treeView = waitForObject( treeviewname )
  treeView = waitForObject( treeviewname )
  model = treeView.model()
  model = treeView.model()
Line 37: Line 40:
  childrenItems = ""
  childrenItems = ""
  if len( pieces ) > 1: # If this has children
  if len( pieces ) > 1: # If this has children
childrenItems = pieces[ 1 ]
  childrenItems = pieces[ 1 ]
 
  toplevel = model.rowCount( QModelIndex() )
  toplevel = model.rowCount( QModelIndex() )
  for row in range( toplevel ): # iterate through tree, looking for matching file
  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
  # 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
  for col in range( 3 ): # check first few columns
idx = model.index( row, col, QModelIndex() )
    idx = model.index( row, col, QModelIndex() )
if checkModelItemReplacement(idx, pathOnly):
    if checkModelItemReplacement(idx, pathOnly):
return replaceModelIndex(utils.escape(str( idx.data( Qt.DisplayRole ).toString() )), childrenItems)
      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
  # 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
  # check all our children as well
for col in range(20):
  for col in range(5):
idx = model.index( row, col, QModelIndex() )
    idx = model.index( row, col, QModelIndex() )
childCount = model.rowCount(idx)
    childCount = model.rowCount(idx)
print "Checking children of item %s, found s" (idx.data(Qt.DisplayRole).toString(), childCount)
    print "Checking children of item %s, found s" (idx.data(Qt.DisplayRole).toString(), childCount)
for childRow in range(childCount):
    for childRow in range(childCount):
child = model.index( 0, 0, idx )
      child = model.index( 0, 0, idx )
print "checking child %s: s" (childRow, child.data(Qt.DisplayRole).toString())
      print "checking child %s: s" (childRow, child.data(Qt.DisplayRole).toString())
if checkModelItemReplacement(child, pathOnly):
      if checkModelItemReplacement(child, pathOnly):
# we need to do the replacement differently, because we need to keep the parent in the path
        # we need to do the replacement differently, because we need to keep the parent in the path
partpieces = re.split( "([<sup>]).", origpath )
        partpieces = re.split( "([<sup>]).", origpath )
# find the index that has the test_data/ path. all indexes before that are parents
        # find the index that has the test_data/ path. all indexes before that are parents
for i in range(len(partpieces)):
        for i in range(len(partpieces)):
if "testdata" in partpieces[i]: # stop here
          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
            # since we split on [</sup>]. and captured the split text, we need to condense each pair of items
num = i/2
            num = i/2
parentItems = []
            parentItems = []
for k in range(num):
            for k in range(num):
parentItems.append( partpieces[k] + partpieces[k+1] ) # rescue the char lost when splitting
              parentItems.append( partpieces[k] + partpieces[k+1] ) # rescue the char lost when splitting
parentstr = ".".join(parentItems)
            parentstr = ".".join(parentItems)
return replaceModelIndex(utils.escape(str( child.data( Qt.DisplayRole ).toString() )), childrenItems, parentstr)
            return replaceModelIndex(utils.escape(str( child.data( Qt.DisplayRole ).toString() )), childrenItems, parentstr)


# we failed
# we failed
Line 77: Line 81:
  contents = utils.escape(str( idx.data( Qt.DisplayRole ).toString() ))
  contents = utils.escape(str( idx.data( Qt.DisplayRole ).toString() ))
  print "Checking for %s in s" (pathOnly, contents)
  print "Checking for %s in s" (pathOnly, contents)
  if pathOnly in contents: # this is the entry we are looking for
  return pathOnly in contents: # this is the entry we are looking for
return True
else:
return False


# used internally by above
# used internally by above
Line 87: Line 88:
  replaced = contents
  replaced = contents


if children [[Image:= "":
if children != "":
        replaced += "." + children
  replaced += "." + children
        print "added children to replaced: %s" % replaced
  print "added children to replaced: %s" % replaced
    if parents |]]= "":
  if parents != "":
replaced = parents + "." + replaced
    replaced = parents + "." + replaced
print "added parents to replaced: s" replaced
  print "added parents to replaced: s" replaced
  return replaced</code>
  return replaced</code>


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:
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:


<code>treeViewName = ":Your_treeview_here"
<code>
treeViewName = ":Your_treeview_here"
itemPath = "server1serverpathsomepathtestdatatestprojectfiletestb.subtem.subSubItem"
itemPath = "server1serverpathsomepathtestdatatestprojectfiletestb.subtem.subSubItem"
realItemPath = findRealPath( treeViewName, itemPath )</code>
realItemPath = findRealPath( treeViewName, itemPath )
</code>

Latest revision as of 13:25, 11 August 2020


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

server1\serverpath\some\path\test_data\test_project\filetestdb.subtem.subSubItem

to

C:\_mirror\some\path\test_data\test_project\filetestdb.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(5):
     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)
 return pathOnly in contents: # this is the entry we are looking for

# 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 != "":
   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 )