2013-10-08 15:07:03 +08:00
|
|
|
/* treemodel.cpp
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
Copyright (c) 2015, Nikolaj Schlej. All rights reserved.
|
2013-12-29 23:13:46 +08:00
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
2013-10-08 15:07:03 +08:00
|
|
|
|
2013-12-29 23:13:46 +08:00
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
2013-10-08 15:07:03 +08:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "treeitem.h"
|
|
|
|
#include "treemodel.h"
|
|
|
|
|
2013-12-29 23:13:46 +08:00
|
|
|
TreeModel::TreeModel(QObject *parent)
|
2013-10-08 15:07:03 +08:00
|
|
|
: QAbstractItemModel(parent)
|
|
|
|
{
|
2014-02-27 17:14:41 +08:00
|
|
|
rootItem = new TreeItem(Types::Root);
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TreeModel::~TreeModel()
|
|
|
|
{
|
2013-12-29 23:13:46 +08:00
|
|
|
delete rootItem;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int TreeModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if (parent.isValid())
|
|
|
|
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
|
|
|
|
else
|
|
|
|
return rootItem->columnCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant TreeModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
if (role != Qt::DisplayRole && role != Qt::UserRole)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
|
|
|
|
if (role == Qt::DisplayRole)
|
|
|
|
return item->data(index.column());
|
|
|
|
else
|
|
|
|
return item->info();
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
|
2014-07-25 07:59:51 +08:00
|
|
|
int role) const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2013-12-12 19:28:39 +08:00
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
2014-07-25 07:59:51 +08:00
|
|
|
switch (section)
|
2013-12-29 23:13:46 +08:00
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return tr("Name");
|
|
|
|
case 1:
|
|
|
|
return tr("Action");
|
|
|
|
case 2:
|
|
|
|
return tr("Type");
|
|
|
|
case 3:
|
|
|
|
return tr("Subtype");
|
|
|
|
case 4:
|
|
|
|
return tr("Text");
|
|
|
|
}
|
|
|
|
}
|
2013-10-08 15:07:03 +08:00
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
|
2014-01-12 07:02:54 +08:00
|
|
|
const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
if (!hasIndex(row, column, parent))
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
TreeItem *parentItem;
|
|
|
|
|
|
|
|
if (!parent.isValid())
|
|
|
|
parentItem = rootItem;
|
|
|
|
else
|
|
|
|
parentItem = static_cast<TreeItem*>(parent.internalPointer());
|
|
|
|
|
|
|
|
TreeItem *childItem = parentItem->child(row);
|
|
|
|
if (childItem)
|
|
|
|
return createIndex(row, column, childItem);
|
|
|
|
else
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex TreeModel::parent(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
|
2013-11-15 18:48:14 +08:00
|
|
|
if (childItem == rootItem)
|
|
|
|
return QModelIndex();
|
2013-12-29 23:13:46 +08:00
|
|
|
|
2013-10-08 15:07:03 +08:00
|
|
|
TreeItem *parentItem = childItem->parent();
|
|
|
|
|
|
|
|
if (parentItem == rootItem)
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
return createIndex(parentItem->row(), 0, parentItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
int TreeModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
TreeItem *parentItem;
|
|
|
|
if (parent.column() > 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!parent.isValid())
|
|
|
|
parentItem = rootItem;
|
|
|
|
else
|
|
|
|
parentItem = static_cast<TreeItem*>(parent.internalPointer());
|
|
|
|
|
|
|
|
return parentItem->childCount();
|
|
|
|
}
|
|
|
|
|
2013-12-29 23:13:46 +08:00
|
|
|
UINT8 TreeModel::type(const QModelIndex &index) const
|
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return 0;
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
return item->type();
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
UINT32 TreeModel::attributes(const QModelIndex &index) const
|
2013-12-29 23:13:46 +08:00
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return 0;
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
2015-01-31 22:00:00 +08:00
|
|
|
return item->attributes();
|
2013-12-29 23:13:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray TreeModel::header(const QModelIndex &index) const
|
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return QByteArray();
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
return item->header();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TreeModel::hasEmptyHeader(const QModelIndex &index) const
|
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return true;
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
return item->hasEmptyHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray TreeModel::body(const QModelIndex &index) const
|
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return QByteArray();
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
return item->body();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TreeModel::hasEmptyBody(const QModelIndex &index) const
|
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return true;
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
return item->hasEmptyBody();
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
QString TreeModel::name(const QModelIndex &index) const
|
2013-12-29 23:13:46 +08:00
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2015-01-31 22:00:00 +08:00
|
|
|
return QString();
|
2013-12-29 23:13:46 +08:00
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
2015-01-31 22:00:00 +08:00
|
|
|
return item->name();
|
2013-12-29 23:13:46 +08:00
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
QString TreeModel::text(const QModelIndex &index) const
|
2013-12-29 23:13:46 +08:00
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2015-01-31 22:00:00 +08:00
|
|
|
return QString();
|
2013-12-29 23:13:46 +08:00
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
2015-01-31 22:00:00 +08:00
|
|
|
return item->text();
|
2013-12-29 23:13:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QString TreeModel::info(const QModelIndex &index) const
|
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return QString();
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
return item->info();
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT8 TreeModel::action(const QModelIndex &index) const
|
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2014-02-27 17:14:41 +08:00
|
|
|
return Actions::NoAction;
|
2013-12-29 23:13:46 +08:00
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
return item->action();
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT8 TreeModel::compression(const QModelIndex &index) const
|
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return COMPRESSION_ALGORITHM_UNKNOWN;
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
return item->compression();
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
void TreeModel::setAttributes(const QModelIndex & index, const UINT32 attributes)
|
2014-01-11 17:20:58 +08:00
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2014-01-11 17:20:58 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
2015-01-31 22:00:00 +08:00
|
|
|
item->setAttributes(attributes);
|
2014-01-11 17:20:58 +08:00
|
|
|
emit dataChanged(index, index);
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
void TreeModel::setName(const QModelIndex &index, const QString &data)
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return;
|
|
|
|
|
2013-10-08 15:07:03 +08:00
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
item->setName(data);
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
void TreeModel::setType(const QModelIndex &index, const UINT8 data)
|
2013-12-29 23:13:46 +08:00
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
2015-01-31 22:00:00 +08:00
|
|
|
item->setType(data);
|
2013-12-29 23:13:46 +08:00
|
|
|
emit dataChanged(index, index);
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
void TreeModel::setText(const QModelIndex &index, const QString &data)
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return;
|
2013-10-08 15:07:03 +08:00
|
|
|
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
item->setText(data);
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
/*QString TreeModel::name(const QModelIndex &index) const
|
2013-12-29 23:13:46 +08:00
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return QString();
|
|
|
|
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
2015-01-31 22:00:00 +08:00
|
|
|
return item->name();
|
|
|
|
}*/
|
2013-12-29 23:13:46 +08:00
|
|
|
|
|
|
|
void TreeModel::setAction(const QModelIndex &index, const UINT8 action)
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return;
|
2013-11-07 21:46:28 +08:00
|
|
|
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
item->setAction(action);
|
2014-07-25 07:59:51 +08:00
|
|
|
emit dataChanged(this->index(0, 0), index);
|
2013-11-18 23:23:59 +08:00
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
QModelIndex TreeModel::addItem(const UINT8 type, const UINT32 attributes, const UINT8 compression,
|
2014-07-25 07:59:51 +08:00
|
|
|
const QString & name, const QString & text, const QString & info,
|
2015-01-31 22:00:00 +08:00
|
|
|
const QByteArray & header, const QByteArray & body,
|
2014-07-25 07:59:51 +08:00
|
|
|
const QModelIndex & parent, const UINT8 mode)
|
2013-11-07 21:46:28 +08:00
|
|
|
{
|
2013-11-20 09:19:48 +08:00
|
|
|
TreeItem *item = 0;
|
|
|
|
TreeItem *parentItem = 0;
|
2013-10-15 23:19:15 +08:00
|
|
|
int parentColumn = 0;
|
2013-10-08 15:07:03 +08:00
|
|
|
|
2013-12-29 23:13:46 +08:00
|
|
|
if (!parent.isValid())
|
2013-10-15 23:19:15 +08:00
|
|
|
parentItem = rootItem;
|
|
|
|
else
|
|
|
|
{
|
2013-12-12 19:28:39 +08:00
|
|
|
if (mode == CREATE_MODE_BEFORE || mode == CREATE_MODE_AFTER) {
|
2013-12-29 23:13:46 +08:00
|
|
|
item = static_cast<TreeItem*>(parent.internalPointer());
|
2013-11-07 21:46:28 +08:00
|
|
|
parentItem = item->parent();
|
2013-12-29 23:13:46 +08:00
|
|
|
parentColumn = parent.parent().column();
|
2013-11-07 21:46:28 +08:00
|
|
|
}
|
2013-11-14 18:40:39 +08:00
|
|
|
else {
|
2013-12-29 23:13:46 +08:00
|
|
|
parentItem = static_cast<TreeItem*>(parent.internalPointer());
|
|
|
|
parentColumn = parent.column();
|
2013-11-14 18:40:39 +08:00
|
|
|
}
|
2013-10-15 23:19:15 +08:00
|
|
|
}
|
2013-12-29 23:13:46 +08:00
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
TreeItem *newItem = new TreeItem(type, attributes, compression, name, text, info, header, body, parentItem);
|
2013-12-12 19:28:39 +08:00
|
|
|
if (mode == CREATE_MODE_APPEND) {
|
2013-11-07 21:46:28 +08:00
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
parentItem->appendChild(newItem);
|
|
|
|
}
|
2013-12-12 19:28:39 +08:00
|
|
|
else if (mode == CREATE_MODE_PREPEND) {
|
2013-11-07 21:46:28 +08:00
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
parentItem->prependChild(newItem);
|
|
|
|
}
|
2013-12-12 19:28:39 +08:00
|
|
|
else if (mode == CREATE_MODE_BEFORE) {
|
2013-11-07 21:46:28 +08:00
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
parentItem->insertChildBefore(item, newItem);
|
|
|
|
}
|
2013-12-12 19:28:39 +08:00
|
|
|
else if (mode == CREATE_MODE_AFTER) {
|
2013-11-07 21:46:28 +08:00
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
parentItem->insertChildAfter(item, newItem);
|
|
|
|
}
|
2014-04-18 20:18:11 +08:00
|
|
|
else {
|
|
|
|
delete newItem;
|
2013-11-07 21:46:28 +08:00
|
|
|
return QModelIndex();
|
2014-04-18 20:18:11 +08:00
|
|
|
}
|
2013-12-29 23:13:46 +08:00
|
|
|
|
2013-10-15 23:19:15 +08:00
|
|
|
emit layoutChanged();
|
2013-12-29 23:13:46 +08:00
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
return createIndex(newItem->row(), parentColumn, newItem);
|
|
|
|
}
|
2013-12-29 23:13:46 +08:00
|
|
|
|
|
|
|
QModelIndex TreeModel::findParentOfType(const QModelIndex& index, UINT8 type) const
|
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
if (!index.isValid())
|
2013-12-29 23:13:46 +08:00
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
TreeItem *item;
|
|
|
|
QModelIndex parent = index;
|
|
|
|
|
2014-07-25 07:59:51 +08:00
|
|
|
for (item = static_cast<TreeItem*>(parent.internalPointer());
|
2013-12-29 23:13:46 +08:00
|
|
|
item != NULL && item != rootItem && item->type() != type;
|
|
|
|
item = static_cast<TreeItem*>(parent.internalPointer()))
|
|
|
|
parent = parent.parent();
|
|
|
|
if (item != NULL && item != rootItem)
|
|
|
|
return parent;
|
|
|
|
|
|
|
|
return QModelIndex();
|
2014-07-25 07:59:51 +08:00
|
|
|
}
|