2013-10-08 15:07:03 +08:00
|
|
|
/* treemodel.cpp
|
|
|
|
|
|
|
|
Copyright (c) 2013, Nikolaj Schlej. All rights reserved.
|
|
|
|
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
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "treeitem.h"
|
|
|
|
#include "treemodel.h"
|
|
|
|
|
2013-10-15 23:19:15 +08:00
|
|
|
TreeModel::TreeModel(TreeItem *root, QObject *parent)
|
2013-10-08 15:07:03 +08:00
|
|
|
: QAbstractItemModel(parent)
|
|
|
|
{
|
2013-10-15 23:19:15 +08:00
|
|
|
rootItem = root;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TreeModel::~TreeModel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
int role) const
|
|
|
|
{
|
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
|
|
|
|
return rootItem->data(section);
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
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-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-11-07 21:46:28 +08:00
|
|
|
UINT8 TreeModel::setItemName(const QString &data, const QModelIndex &index)
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
if(!index.isValid())
|
2013-11-07 21:46:28 +08:00
|
|
|
return ERR_INVALID_PARAMETER;
|
2013-10-08 15:07:03 +08:00
|
|
|
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
item->setName(data);
|
|
|
|
emit dataChanged(index, index);
|
2013-11-07 21:46:28 +08:00
|
|
|
return ERR_SUCCESS;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
UINT8 TreeModel::setItemText(const QString &data, const QModelIndex &index)
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
if(!index.isValid())
|
2013-11-07 21:46:28 +08:00
|
|
|
return ERR_INVALID_PARAMETER;
|
2013-10-08 15:07:03 +08:00
|
|
|
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
item->setText(data);
|
|
|
|
emit dataChanged(index, index);
|
2013-11-07 21:46:28 +08:00
|
|
|
return ERR_SUCCESS;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
UINT8 TreeModel::setItemAction(const UINT8 action, const QModelIndex &index)
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2013-11-07 21:46:28 +08:00
|
|
|
if(!index.isValid())
|
|
|
|
return ERR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
item->setAction(action);
|
2013-11-18 23:23:59 +08:00
|
|
|
emit dataChanged(this->index(0,0), index);
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT8 TreeModel::setItemCompression(const UINT8 algorithm, const QModelIndex &index)
|
|
|
|
{
|
|
|
|
if(!index.isValid())
|
|
|
|
return ERR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
item->setCompression(algorithm);
|
|
|
|
emit dataChanged(this->index(0,0), index);
|
2013-11-07 21:46:28 +08:00
|
|
|
return ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex TreeModel::addItem(const UINT8 type, const UINT8 subtype, const UINT8 compression,
|
|
|
|
const QString & name, const QString & text, const QString & info,
|
2013-11-17 17:01:11 +08:00
|
|
|
const QByteArray & header, const QByteArray & body, const QByteArray & tail,
|
|
|
|
const QModelIndex & index, 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-11-07 21:46:28 +08:00
|
|
|
if (!index.isValid())
|
2013-10-15 23:19:15 +08:00
|
|
|
parentItem = rootItem;
|
|
|
|
else
|
|
|
|
{
|
2013-11-14 18:40:39 +08:00
|
|
|
if (mode == INSERT_MODE_BEFORE || mode == INSERT_MODE_AFTER) {
|
2013-11-07 21:46:28 +08:00
|
|
|
item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
parentItem = item->parent();
|
|
|
|
parentColumn = index.parent().column();
|
|
|
|
}
|
2013-11-14 18:40:39 +08:00
|
|
|
else {
|
|
|
|
parentItem = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
parentColumn = index.column();
|
|
|
|
}
|
2013-10-15 23:19:15 +08:00
|
|
|
}
|
2013-10-08 15:07:03 +08:00
|
|
|
|
2013-11-17 17:01:11 +08:00
|
|
|
TreeItem *newItem = new TreeItem(type, subtype, compression, name, text, info, header, body, tail, parentItem);
|
2013-11-14 18:40:39 +08:00
|
|
|
if (mode == INSERT_MODE_APPEND) {
|
2013-11-07 21:46:28 +08:00
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
parentItem->appendChild(newItem);
|
|
|
|
}
|
2013-11-14 18:40:39 +08:00
|
|
|
else if (mode == INSERT_MODE_PREPEND) {
|
2013-11-07 21:46:28 +08:00
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
parentItem->prependChild(newItem);
|
|
|
|
}
|
2013-11-14 18:40:39 +08:00
|
|
|
else if (mode == INSERT_MODE_BEFORE) {
|
2013-11-07 21:46:28 +08:00
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
parentItem->insertChildBefore(item, newItem);
|
|
|
|
}
|
2013-11-14 18:40:39 +08:00
|
|
|
else if (mode == INSERT_MODE_AFTER) {
|
2013-11-07 21:46:28 +08:00
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
parentItem->insertChildAfter(item, newItem);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return QModelIndex();
|
|
|
|
|
2013-10-15 23:19:15 +08:00
|
|
|
emit layoutChanged();
|
2013-11-07 21:46:28 +08:00
|
|
|
|
|
|
|
return createIndex(newItem->row(), parentColumn, newItem);
|
|
|
|
}
|