packages feed

AndroidViewHierarchyImporter-0.1.0.0: Graphviz.hs

{- | Generation of a raphviz description of the view tree

-}
module Graphviz(
	  graphviz
	, viewName
	) where

import Data.Tree(drawTree,Tree(..))
import Types
import Data.List.Split
import Control.Monad.Trans.Writer.Lazy

_graphviz :: Show a => Tree a -> Writer String ()
_graphviz (Node a []) = return ()
_graphviz (Node a l) = do 
	let printEdge startN (Node endN _) = do 
		tell (show startN)
		tell " -> "
		tell (show endN)
		tell ";\n"
	mapM_ (printEdge a) l
	mapM_ _graphviz l


-- | Convert the view tree to a textual description in graphviz format
graphviz :: Tree String -> String
graphviz r = execWriter $ do 
	tell "digraph view {\n"
	_graphviz r
	tell "};\n"

-- | Get the shortened name of a view (class name with hexadecimal number identifying the object)
viewName :: (String,View) -> String
viewName (viewName,_) = shortName viewName

shortName :: String -> String 
shortName = mkName . reverse . wordsBy (`elem` ".@")
 where 
 	mkName (a:b:_) = b ++ "@" ++ a 
 	mkName s = error $ "at least two name should be contained in the list : " ++ show s