Log Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

118 lines
3.6 KiB

#include "viewmodel/tree/LogTreeModel.h"
#include "viewmodel/tree/util/Log2TreeItemConverter.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)
{
beginResetModel();
_rootItem = std::make_unique<TreeItem>(QVariantList{tr("Field"), tr("Value")});
_rootItem->appendChild(Log2TreeItemConverter::convert(log, _rootItem.get()));
endResetModel();
}
/*************************************************************************************************/
void TreeModel::clearView()
{
beginResetModel();
_rootItem = std::make_unique<TreeItem>(QVariantList{tr("Field"), tr("Value")});
endResetModel();
}