# include "viewmodel/tree/LogTreeModel.h"
# include "viewmodel/tree/util/Log2TreeItemConverter.h"
# include "viewmodel/displayString/util/DisplayStringProviderMapGenerator.h"
# include "viewmodel/displayString/FullFileDisplayProviderCreator.h"
# include "parser/JsonParser.h"
/*************************************************************************************************/
TreeModel : : TreeModel ( QObject * parent )
: QAbstractItemModel ( parent )
{
}
/*************************************************************************************************/
TreeModel : : ~ TreeModel ( ) = default ;
/*************************************************************************************************/
QModelIndex TreeModel : : index ( int row , int column , const QModelIndex & parent ) const
{
if ( ! hasIndex ( row , column , parent ) )
{
return { } ;
}
TreeItem * parentItem = parent . isValid ( )
? static_cast < TreeItem * > ( parent . internalPointer ( ) )
: _rootItem . get ( ) ;
if ( auto * childItem = parentItem - > child ( row ) )
{
return createIndex ( row , column , childItem ) ;
}
return { } ;
}
/*************************************************************************************************/
QModelIndex TreeModel : : parent ( const QModelIndex & index ) const
{
if ( ! index . isValid ( ) )
{
return { } ;
}
auto * childItem = static_cast < TreeItem * > ( index . internalPointer ( ) ) ;
TreeItem * parentItem = childItem - > parentItem ( ) ;
return parentItem ! = _rootItem . get ( )
? createIndex ( parentItem - > row ( ) , 0 , parentItem ) : QModelIndex { } ;
}
/*************************************************************************************************/
int TreeModel : : rowCount ( const QModelIndex & parent ) const
{
if ( parent . column ( ) > 0 )
{
return 0 ;
}
const TreeItem * parentItem = parent . isValid ( )
? static_cast < const TreeItem * > ( parent . internalPointer ( ) )
: _rootItem . get ( ) ;
return parentItem - > childCount ( ) ;
}
/*************************************************************************************************/
int TreeModel : : columnCount ( const QModelIndex & parent ) const
{
if ( parent . isValid ( ) )
{
return static_cast < TreeItem * > ( parent . internalPointer ( ) ) - > columnCount ( ) ;
}
return _rootItem - > columnCount ( ) ;
}
/*************************************************************************************************/
QVariant TreeModel : : data ( const QModelIndex & index , int role ) const
{
if ( ! index . isValid ( ) | | role ! = Qt : : DisplayRole )
{
return { } ;
}
const auto * item = static_cast < const TreeItem * > ( index . internalPointer ( ) ) ;
return item - > data ( index . column ( ) ) ;
}
/*************************************************************************************************/
Qt : : ItemFlags TreeModel : : flags ( const QModelIndex & index ) const
{
return index . isValid ( )
? QAbstractItemModel : : flags ( index ) : Qt : : ItemFlags ( Qt : : NoItemFlags ) ;
}
/*************************************************************************************************/
QVariant TreeModel : : headerData ( int section , Qt : : Orientation orientation ,
int role ) const
{
return orientation = = Qt : : Horizontal & & role = = Qt : : DisplayRole
? _rootItem - > data ( section ) : QVariant { } ;
}
/*************************************************************************************************/
void TreeModel : : showSelectedLog ( const Log_ptr log )
{
QString jsonString =
R " (
{ " id " : " stderr " , " level " : 1 , " location " : { " file " : " ../../../host-projects/sono/logic/src/viewModel/factory/ApplicationStarter.cpp " , " func " : " init " , " lineno " : 69 } , " msg " : [ " Lanat " , { " type " : " int " , " summary " : " bish tar az bish " , " content " : { " a " : " b " , " arr " : [ " a " , " b " , " c " , { " a " : " b " } ] , " c " : { " a " : 44 } } } ] , " scope " : " global " , " threadId " : 124330260010752 , " timestamp " : 1716911725703 }
) " ;
Log_ptr logf = JsonParser ( ) . parse ( jsonString ) ;
beginResetModel ( ) ;
_rootItem = SimpleLog2TreeItemConverter ( ) . convert (
logf ,
DisplayStringProviderMapGenerator : : generate (
std : : make_shared < FullFileDisplayProviderCreator > ( ) ) ) ;
endResetModel ( ) ;
}
/*************************************************************************************************/
void TreeModel : : clearView ( )
{
beginResetModel ( ) ;
_rootItem = std : : make_unique < TreeItem > ( QVariantList { tr ( " Field " ) , tr ( " Value " ) } ) ;
endResetModel ( ) ;
}