2013-10-08 15:07:03 +08:00
|
|
|
/* treeitem.cpp
|
|
|
|
|
2014-01-11 17:20:58 +08:00
|
|
|
Copyright (c) 2014, 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
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
#include <QObject>
|
2013-10-08 15:07:03 +08:00
|
|
|
#include "treeitem.h"
|
2014-02-27 17:14:41 +08:00
|
|
|
#include "types.h"
|
2013-10-08 15:07:03 +08:00
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TreeItem::TreeItem(const UINT8 type, const UINT8 subtype, const UINT8 compression,
|
2013-12-29 23:13:46 +08:00
|
|
|
const QString & name, const QString & text, const QString & info,
|
|
|
|
const QByteArray & header, const QByteArray & body, const QByteArray & tail,
|
2013-11-07 21:46:28 +08:00
|
|
|
TreeItem *parent)
|
|
|
|
{
|
2014-02-27 17:14:41 +08:00
|
|
|
itemAction = Actions::NoAction;
|
2013-10-08 15:07:03 +08:00
|
|
|
itemType = type;
|
|
|
|
itemSubtype = subtype;
|
2013-11-07 21:46:28 +08:00
|
|
|
itemCompression = compression;
|
2013-10-08 15:07:03 +08:00
|
|
|
itemName = name;
|
|
|
|
itemText = text;
|
|
|
|
itemInfo = info;
|
|
|
|
itemHeader = header;
|
|
|
|
itemBody = body;
|
2013-11-17 17:01:11 +08:00
|
|
|
itemTail = tail;
|
2013-10-08 15:07:03 +08:00
|
|
|
parentItem = parent;
|
2013-12-29 23:13:46 +08:00
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
// Set default names
|
|
|
|
setDefaultNames();
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TreeItem::~TreeItem()
|
|
|
|
{
|
|
|
|
qDeleteAll(childItems);
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
void TreeItem::setDefaultNames()
|
|
|
|
{
|
|
|
|
itemTypeName = itemTypeToQString(itemType);
|
|
|
|
itemSubtypeName = itemSubtypeToQString(itemType, itemSubtype);
|
|
|
|
}
|
|
|
|
|
2013-10-08 15:07:03 +08:00
|
|
|
void TreeItem::appendChild(TreeItem *item)
|
|
|
|
{
|
|
|
|
childItems.append(item);
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
void TreeItem::prependChild(TreeItem *item)
|
|
|
|
{
|
|
|
|
childItems.prepend(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT8 TreeItem::insertChildBefore(TreeItem *item, TreeItem *newItem)
|
|
|
|
{
|
|
|
|
int index = childItems.indexOf(item);
|
|
|
|
if (index == -1)
|
|
|
|
return ERR_ITEM_NOT_FOUND;
|
|
|
|
childItems.insert(index, newItem);
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT8 TreeItem::insertChildAfter(TreeItem *item, TreeItem *newItem)
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2013-11-07 21:46:28 +08:00
|
|
|
int index = childItems.indexOf(item);
|
|
|
|
if (index == -1)
|
|
|
|
return ERR_ITEM_NOT_FOUND;
|
|
|
|
childItems.insert(index + 1, newItem);
|
|
|
|
return ERR_SUCCESS;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TreeItem *TreeItem::child(int row)
|
|
|
|
{
|
2013-11-07 21:46:28 +08:00
|
|
|
return childItems.value(row, NULL);
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int TreeItem::childCount() const
|
|
|
|
{
|
|
|
|
return childItems.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
int TreeItem::columnCount() const
|
|
|
|
{
|
2013-11-07 21:46:28 +08:00
|
|
|
return 5;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
QVariant TreeItem::data(int column) const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
switch(column)
|
|
|
|
{
|
2013-11-14 18:40:39 +08:00
|
|
|
case 0: //Name
|
|
|
|
return itemName;
|
|
|
|
case 1: //Action
|
2014-02-27 17:14:41 +08:00
|
|
|
return actionTypeToQString(itemAction);
|
2013-11-07 21:46:28 +08:00
|
|
|
case 2: //Type
|
2013-10-08 15:07:03 +08:00
|
|
|
return itemTypeName;
|
2013-11-07 21:46:28 +08:00
|
|
|
case 3: //Subtype
|
2013-10-08 15:07:03 +08:00
|
|
|
return itemSubtypeName;
|
2013-11-07 21:46:28 +08:00
|
|
|
case 4: //Text
|
2013-10-08 15:07:03 +08:00
|
|
|
return itemText;
|
|
|
|
default:
|
2013-11-07 21:46:28 +08:00
|
|
|
return QVariant();
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeItem *TreeItem::parent()
|
|
|
|
{
|
|
|
|
return parentItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TreeItem::setName(const QString &text)
|
|
|
|
{
|
|
|
|
itemName = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TreeItem::setText(const QString &text)
|
|
|
|
{
|
|
|
|
itemText = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TreeItem::setTypeName(const QString &text)
|
|
|
|
{
|
|
|
|
itemTypeName = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TreeItem::setSubtypeName(const QString &text)
|
|
|
|
{
|
|
|
|
itemSubtypeName = text;
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
QString TreeItem::info() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
return itemInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TreeItem::row() const
|
|
|
|
{
|
|
|
|
if (parentItem)
|
|
|
|
return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
UINT8 TreeItem::type() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
return itemType;
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
UINT8 TreeItem::subtype() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
return itemSubtype;
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
UINT8 TreeItem::compression() const
|
2013-10-15 23:19:15 +08:00
|
|
|
{
|
2013-11-07 21:46:28 +08:00
|
|
|
return itemCompression;
|
2013-10-15 23:19:15 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
QByteArray TreeItem::header() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
return itemHeader;
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
QByteArray TreeItem::body() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
return itemBody;
|
|
|
|
}
|
|
|
|
|
2013-11-17 17:01:11 +08:00
|
|
|
QByteArray TreeItem::tail() const
|
|
|
|
{
|
|
|
|
return itemTail;
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
bool TreeItem::hasEmptyHeader() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
return itemHeader.isEmpty();
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
bool TreeItem::hasEmptyBody() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
return itemBody.isEmpty();
|
2013-11-07 21:46:28 +08:00
|
|
|
}
|
|
|
|
|
2013-11-17 17:01:11 +08:00
|
|
|
bool TreeItem::hasEmptyTail() const
|
|
|
|
{
|
|
|
|
return itemTail.isEmpty();
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
UINT8 TreeItem::action() const
|
|
|
|
{
|
|
|
|
return itemAction;
|
|
|
|
}
|
2013-11-18 23:23:59 +08:00
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
void TreeItem::setAction(const UINT8 action)
|
|
|
|
{
|
|
|
|
itemAction = action;
|
2013-11-18 23:23:59 +08:00
|
|
|
|
2013-12-29 23:13:46 +08:00
|
|
|
// On insert action, set insert action for children
|
2014-02-27 17:14:41 +08:00
|
|
|
if (action == Actions::Insert)
|
2013-12-29 23:13:46 +08:00
|
|
|
for(int i = 0; i < childCount(); i++)
|
2014-02-27 17:14:41 +08:00
|
|
|
child(i)->setAction(Actions::Insert);
|
2013-12-29 23:13:46 +08:00
|
|
|
|
|
|
|
// Set rebuild action for parent, if it has no action now
|
2014-02-27 17:14:41 +08:00
|
|
|
if (parentItem && parentItem->type() != Types::Root
|
|
|
|
&& parentItem->action() == Actions::NoAction)
|
|
|
|
parentItem->setAction(Actions::Rebuild);
|
2013-11-18 23:23:59 +08:00
|
|
|
}
|
|
|
|
|
2014-01-11 17:20:58 +08:00
|
|
|
void TreeItem::setSubtype(const UINT8 subtype)
|
|
|
|
{
|
|
|
|
itemSubtype = subtype;
|
|
|
|
itemSubtypeName = itemSubtypeToQString(itemType, itemSubtype);
|
|
|
|
}
|